Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////////////////////////
- //
- // Leasing Function
- // Description:
- // Maintainer: Matt Campbell <mcampbell at bigmachines dot com>
- // Last Modified: 21/6/2012
- //
- /////////////////////////////////////////////////////////////////////////////
- // ========= Initialization of Variables ==============
- // Initialize any variables which need to persist across/outside of several loops.
- modelValueDict = dict("float"); // Dictionary containing the total model values
- modelDocNums = string[]; // Array containing the Document Numbers of models
- leaseTermDict = dict("string"); // Lease term in months (leaseTerm_line)
- leaseCompanyDict = dict("string"); // Lease company (leaseCompany_line)
- monthlyPaymentDict = dict("float"); // Monthly Payment (monthlyPayment_line)
- paymentOverrideDict = dict("float"); // Monthly Payment Override (monthlyPaymentOverride_line)
- totLeaseAmt = 0.0; // Total Lease Amount (totalLeaseAmount_quote)
- totMonthlyPayment = 0.0; // Total Monthly Payment (totalMonthlyPayment_quote)
- returnString = ""; // Contains the formatted string of attributes to modify
- // =========== Definition of Constants ================
- numDecimal = 2;
- //hundredPercent = 100.0;
- // ================= Calculations =====================
- // Gathers and structures the necessary information.
- // Quote level attributes
- // Iterate through line_process
- for line in line_process {
- //print line.discountType_line;
- //print line.leaseTerm_line;
- docNumber = ""; // Document Number (_document_number)
- parentDoc = ""; // Parent Document Number (_parent_doc_number)
- extNetPrice = 0.0; // Extended Net Price (extendedNetPrice_line)
- // ============== Total Model Value ===================
- docNumber = line._document_number;
- parentDoc = line._parent_doc_number;
- extNetPrice = line.extendedNetPrice_line;
- // Skip if line is a service item
- //if( line._part_custom_field2 == "customService" ) { continue; }
- if( find(lower(line._part_number), "service") <> -1 ) { continue; }
- //print line._part_number;
- if( parentDoc == "" ) { // Line is a model
- // Store attributes in dictionaries to be used later, using the model docNumber as the key
- append(modelDocNums, docNumber);
- put(leaseTermDict, docNumber, line.leaseTerm_line);
- put(leaseCompanyDict, docNumber, line.leaseCompany_line);
- put(monthlyPaymentDict, docNumber, line.monthlyPayment_line);
- put(paymentOverrideDict, docNumber, line.monthlypaymentOverride_line);
- //print line.leaseTerm_line;
- } elif( containskey(modelValueDict, parentDoc) ) { // Line belongs to an existing model
- // Add to the Total Model Value, using the model docNumber as the key
- totModelValue = get(modelValueDict, parentDoc);
- totModelValue = totModelValue + extNetPrice;
- put(modelValueDict, parentDoc, totModelValue);
- } else { // Line belongs to a new model
- // Create a new entry in the Total Model Value dictionary, using the model docNumber as the key
- totModelValue = extNetPrice;
- put(modelValueDict, parentDoc, totModelValue);
- }
- }
- //print leaseTermDict;
- // Iterate through each model
- for model in modelDocNums { // 'model' is the model _document_number
- // Initialize single iteration variables
- //rateFactor = 0.0; // Rate Factor (ThirdPartyRates data table)
- rateFactor = 1.0;
- totFinanceAmt = 0.0; // Total Finance Amount (totalFinanceAmount_line)
- //monthlyPayment = 0.0; // Monthly Payment (monthlyPayment_line)
- adjFinanceAmt = 0.0; // Adjusted Finance Amount (adjustedFinanceAmount_line)
- // Load values from dictionaries
- leaseTerm = get(leaseTermDict, model); // Lease term in months (leaseTerm_line)
- leaseCompany = get(leaseCompanyDict, model); // Lease company (leaseCompany_line)
- totModelValue = get(modelValueDict, model); // Total Model Value (totalModelValue_line)
- monthlyPayment = get(monthlyPaymentDict, model); // Monthly Payment (monthlyPayment_line)
- paymentOverride = get(paymentOverrideDict, model); // Monthly Payment Override (monthlyPaymentOverride_line)
- //print leaseTerm;
- //print leaseCompany;
- if( leaseTerm == "none" ) { continue; } // Skip models with no leasing option set
- // ================== Rate Factor =====================
- // Expected table schema: LeaseCompany | LeaseTerm | RateFactor
- rateFactorRecord = bmql("SELECT RateFactor FROM ThirdPartyRates WHERE LeaseCompany LIKE $leaseCompany AND LeaseTerm LIKE $leaseTerm");
- //print rateFactorRecord;
- for record in rateFactorRecord {
- rateFactor = atof(get(record, "RateFactor"));
- //print rateFactor;
- }
- if( paymentOverride == 0.0 ) {
- monthlyPayment = totModelValue * rateFactor;
- monthlyPayment = round(monthlyPayment,numDecimal);
- totFinanceAmt = monthlyPayment * atof(leaseTerm);
- totFinanceAmt = round(totFinanceAmt,numDecimal);
- // Add to quote totals
- totMonthlyPayment = totMonthlyPayment + monthlyPayment;
- totLeaseAmt = totLeaseAmt + totFinanceAmt;
- } else {
- // Below lines commented out because validation/constraint was moved to a constraint rule
- // Validate paymentOverride
- //if( paymentOverride < monthlyPayment ) {
- //paymentOverride = monthlyPayment;
- //} elif( paymentOverride > (monthlyPayment * 1.10) ) {
- //paymentOverride = monthlyPayment * 1.10;
- //}
- adjFinanceAmt = paymentOverride / rateFactor;
- adjFinanceAmt = round(adjFinanceAmt,numDecimal);
- // Add to quote totals
- totMonthlyPayment = totMonthlyPayment + paymentOverride;
- totLeaseAmt = totLeaseAmt + adjFinanceAmt;
- }
- // Add to quote totals
- // Append to return string
- returnString = returnString + model + "~" + "totalFinanceAmount_line" + "~" + string(totFinanceAmt) + "|"
- + model + "~" + "monthlyPayment_line" + "~" + string(monthlyPayment) + "|"
- + model + "~" + "adjustedFinanceAmount_line" + "~" + string(adjFinanceAmt) + "|"
- + model + "~" + "totalModelValue_line" + "~" + string(totModelValue) + "|"
- + model + "~" + "monthlyPaymentOverride_line" + "~" + string (paymentOverride) + "|"
- ;
- }
- // Quote level calculations
- // Append to return string
- returnString = returnString + "1" + "~" + "totalLeaseAmount_quote" + "~" + string(totLeaseAmt) + "|"
- + "1" + "~" + "totalMonthlyPayment_quote" + "~" + string(totMonthlyPayment) + "|";
- // Return values in format 'documentNumber~attributeName~attributeValue|'
- return returnString;
Add Comment
Please, Sign In to add comment