Guest User

Untitled

a guest
Dec 11th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Leasing Function
  4. // Description:
  5. // Maintainer: Matt Campbell <mcampbell at bigmachines dot com>
  6. // Last Modified: 21/6/2012
  7. //
  8. /////////////////////////////////////////////////////////////////////////////
  9.  
  10.  
  11. // ========= Initialization of Variables ==============
  12. // Initialize any variables which need to persist across/outside of several loops.
  13. modelValueDict = dict("float"); // Dictionary containing the total model values
  14. modelDocNums = string[]; // Array containing the Document Numbers of models
  15. leaseTermDict = dict("string"); // Lease term in months (leaseTerm_line)
  16. leaseCompanyDict = dict("string"); // Lease company (leaseCompany_line)
  17. monthlyPaymentDict = dict("float"); // Monthly Payment (monthlyPayment_line)
  18. paymentOverrideDict = dict("float"); // Monthly Payment Override (monthlyPaymentOverride_line)
  19. totLeaseAmt = 0.0; // Total Lease Amount (totalLeaseAmount_quote)
  20. totMonthlyPayment = 0.0; // Total Monthly Payment (totalMonthlyPayment_quote)
  21. returnString = ""; // Contains the formatted string of attributes to modify
  22.  
  23. // =========== Definition of Constants ================
  24. numDecimal = 2;
  25. //hundredPercent = 100.0;
  26.  
  27.  
  28. // ================= Calculations =====================
  29. // Gathers and structures the necessary information.
  30.  
  31. // Quote level attributes
  32.  
  33. // Iterate through line_process
  34. for line in line_process {
  35. //print line.discountType_line;
  36. //print line.leaseTerm_line;
  37. docNumber = ""; // Document Number (_document_number)
  38. parentDoc = ""; // Parent Document Number (_parent_doc_number)
  39. extNetPrice = 0.0; // Extended Net Price (extendedNetPrice_line)
  40.  
  41. // ============== Total Model Value ===================
  42. docNumber = line._document_number;
  43. parentDoc = line._parent_doc_number;
  44. extNetPrice = line.extendedNetPrice_line;
  45.  
  46. // Skip if line is a service item
  47. //if( line._part_custom_field2 == "customService" ) { continue; }
  48. if( find(lower(line._part_number), "service") <> -1 ) { continue; }
  49. //print line._part_number;
  50.  
  51. if( parentDoc == "" ) { // Line is a model
  52. // Store attributes in dictionaries to be used later, using the model docNumber as the key
  53. append(modelDocNums, docNumber);
  54. put(leaseTermDict, docNumber, line.leaseTerm_line);
  55. put(leaseCompanyDict, docNumber, line.leaseCompany_line);
  56. put(monthlyPaymentDict, docNumber, line.monthlyPayment_line);
  57. put(paymentOverrideDict, docNumber, line.monthlypaymentOverride_line);
  58. //print line.leaseTerm_line;
  59. } elif( containskey(modelValueDict, parentDoc) ) { // Line belongs to an existing model
  60. // Add to the Total Model Value, using the model docNumber as the key
  61. totModelValue = get(modelValueDict, parentDoc);
  62. totModelValue = totModelValue + extNetPrice;
  63. put(modelValueDict, parentDoc, totModelValue);
  64. } else { // Line belongs to a new model
  65. // Create a new entry in the Total Model Value dictionary, using the model docNumber as the key
  66. totModelValue = extNetPrice;
  67. put(modelValueDict, parentDoc, totModelValue);
  68. }
  69. }
  70. //print leaseTermDict;
  71.  
  72. // Iterate through each model
  73. for model in modelDocNums { // 'model' is the model _document_number
  74. // Initialize single iteration variables
  75. //rateFactor = 0.0; // Rate Factor (ThirdPartyRates data table)
  76. rateFactor = 1.0;
  77. totFinanceAmt = 0.0; // Total Finance Amount (totalFinanceAmount_line)
  78. //monthlyPayment = 0.0; // Monthly Payment (monthlyPayment_line)
  79. adjFinanceAmt = 0.0; // Adjusted Finance Amount (adjustedFinanceAmount_line)
  80. // Load values from dictionaries
  81. leaseTerm = get(leaseTermDict, model); // Lease term in months (leaseTerm_line)
  82. leaseCompany = get(leaseCompanyDict, model); // Lease company (leaseCompany_line)
  83. totModelValue = get(modelValueDict, model); // Total Model Value (totalModelValue_line)
  84. monthlyPayment = get(monthlyPaymentDict, model); // Monthly Payment (monthlyPayment_line)
  85. paymentOverride = get(paymentOverrideDict, model); // Monthly Payment Override (monthlyPaymentOverride_line)
  86.  
  87. //print leaseTerm;
  88. //print leaseCompany;
  89.  
  90. if( leaseTerm == "none" ) { continue; } // Skip models with no leasing option set
  91.  
  92. // ================== Rate Factor =====================
  93. // Expected table schema: LeaseCompany | LeaseTerm | RateFactor
  94. rateFactorRecord = bmql("SELECT RateFactor FROM ThirdPartyRates WHERE LeaseCompany LIKE $leaseCompany AND LeaseTerm LIKE $leaseTerm");
  95. //print rateFactorRecord;
  96. for record in rateFactorRecord {
  97. rateFactor = atof(get(record, "RateFactor"));
  98. //print rateFactor;
  99. }
  100.  
  101.  
  102. if( paymentOverride == 0.0 ) {
  103. monthlyPayment = totModelValue * rateFactor;
  104. monthlyPayment = round(monthlyPayment,numDecimal);
  105. totFinanceAmt = monthlyPayment * atof(leaseTerm);
  106. totFinanceAmt = round(totFinanceAmt,numDecimal);
  107.  
  108. // Add to quote totals
  109. totMonthlyPayment = totMonthlyPayment + monthlyPayment;
  110. totLeaseAmt = totLeaseAmt + totFinanceAmt;
  111.  
  112. } else {
  113. // Below lines commented out because validation/constraint was moved to a constraint rule
  114. // Validate paymentOverride
  115. //if( paymentOverride < monthlyPayment ) {
  116. //paymentOverride = monthlyPayment;
  117. //} elif( paymentOverride > (monthlyPayment * 1.10) ) {
  118. //paymentOverride = monthlyPayment * 1.10;
  119. //}
  120. adjFinanceAmt = paymentOverride / rateFactor;
  121. adjFinanceAmt = round(adjFinanceAmt,numDecimal);
  122.  
  123. // Add to quote totals
  124. totMonthlyPayment = totMonthlyPayment + paymentOverride;
  125. totLeaseAmt = totLeaseAmt + adjFinanceAmt;
  126. }
  127.  
  128. // Add to quote totals
  129.  
  130. // Append to return string
  131. returnString = returnString + model + "~" + "totalFinanceAmount_line" + "~" + string(totFinanceAmt) + "|"
  132. + model + "~" + "monthlyPayment_line" + "~" + string(monthlyPayment) + "|"
  133. + model + "~" + "adjustedFinanceAmount_line" + "~" + string(adjFinanceAmt) + "|"
  134. + model + "~" + "totalModelValue_line" + "~" + string(totModelValue) + "|"
  135. + model + "~" + "monthlyPaymentOverride_line" + "~" + string (paymentOverride) + "|"
  136. ;
  137. }
  138.  
  139. // Quote level calculations
  140.  
  141.  
  142. // Append to return string
  143. returnString = returnString + "1" + "~" + "totalLeaseAmount_quote" + "~" + string(totLeaseAmt) + "|"
  144. + "1" + "~" + "totalMonthlyPayment_quote" + "~" + string(totMonthlyPayment) + "|";
  145.  
  146. // Return values in format 'documentNumber~attributeName~attributeValue|'
  147. return returnString;
Add Comment
Please, Sign In to add comment