Skip to content

Instantly share code, notes, and snippets.

@anil826
Created August 13, 2019 16:14
Show Gist options
  • Save anil826/fd7f127d06e78fa60c14360bd3e253c2 to your computer and use it in GitHub Desktop.
Save anil826/fd7f127d06e78fa60c14360bd3e253c2 to your computer and use it in GitHub Desktop.
syngenta
$(document).ready(function() {
$("body.div.alert.alert-danger.alert-dismissible").hide();
var is_sales_orders = false;
var is_marketing_early_pay = false;
var is_marketing_totals = false;
var is_discretionary = true;
var is_pricelineAmountSum = false;
var is_paidAmount = false;
var is_replantCharges =false
var is_loyalty = false;
var is_additional_fees = false;
var approvalStatus;
var is_Calculation_Include = true;
var is_EP = false;
var is_discount = false;
var is_promotion = false;
var sales_orders = [];
var marketing_early_pay = [];
var marketing_totals = [];
var discretionary = [];
var pricelineAmountSum = [];
var paidAmount = [];
var replantCharges = [];
var loyaltyAmount = [];
var additionalFees = [];
var EPData = [];
var promotionData = [];
var discountTotal = [];
var all_pdf_data = [];
var all_salesorder_grower_order_type = [];
var seedGrossTotal = 0;
if (formyoula.form_fields["90b1-9176-df05"]) {
$('#component-ae7d-a2f8-4b2d img').attr('crossorigin','anonymous');
formyoula.event_trigger("main:spinner_dialog", { show: true , "title": "Processing...", "message" : "Please wait while your Grower PDF statements are getting generated.. Please do not close the browser You can continue doing your work in the other tab"});
var gIds = formyoula.form_fields["90b1-9176-df05"].get("value");
var reseller_Id = formyoula.form_fields["59f0-5e4a-3860"].get("value");
var sales_year_id = formyoula.form_fields["a13f-b721-f6e9"].get("value");
var grower_id_query_string = '';
gIds.split(',').forEach(function(item) {
grower_id_query_string = grower_id_query_string + "'" + item + "',";
});
grower_id_query_string = grower_id_query_string.replace(/,\s*$/, "");
console.log('grower_id_query_string:'+grower_id_query_string);
var query_string = "SELECT id, giic_salesyear__c, giic_salesyear__r.NAME, giic_sales_rep__c, giic_sales_rep__r.NAME, giic_seller_id__c, gii__account__c, giic_soldtoreseller__c, giic_soldtoreseller__r.fsap_id__c, giic_soldtoreseller__r.NAME, giic_soldtoreseller__r.phone, giic_soldtoreseller__r.email__c, giic_soldtoreseller__r.shippingstreet, giic_soldtoreseller__r.shippingcity, giic_soldtoreseller__r.shippingstate, giic_soldtoreseller__r.shippingpostalcode, giic_soldtoreseller__r.shippingcountry, gii__account__r.fsap_id__c, gii__account__r.NAME, gii__account__r.phone, gii__account__r.email__c, gii__account__r.billingstreet, gii__account__r.billingcity, gii__account__r.billingstate, gii__account__r.billingpostalcode, gii__account__r.billingcountry, gii__totalamount__c, giic_ordertype__c, giic_ApprovalStatus__c, (SELECT gii__status__c, giic_net_position_units__c, giic_NetPositionProductAmount__c, gii__orderquantity__c, giic_product_description__c, giic_Product_Local_Description__c, gii__unitprice__c,gii__OriginalUnitPrice__c, gii__product__r.gii__productreference__r.crop_group__c, gii__product__r.gii__productreference__r.Material_Stacked_Traits__c, giic_discountrate__c, giic_impactedqty__c, giic_replantreasoncode__c, giic_ReplantDiscount__c FROM gii__salesorder__r WHERE ( giic_net_position_units__c > 0 AND (gii__product__r.gii__productreference__r.crop_group__c = 'COR: CORN' OR gii__product__r.gii__productreference__r.crop_group__c = 'SYB: SOYBEAN' )) order by giic_product_description__c) FROM gii__salesorder__c WHERE gii__account__c IN (" + grower_id_query_string + ")AND giic_salesyear__c = '" + sales_year_id + "' AND giic_soldtoreseller__c = '" + reseller_Id + "' AND ( giic_ordertype__c = 'Grower Order' OR giic_ordertype__c = 'Replant Order' OR giic_ordertype__c = 'Grower Order-FOC' ) AND giic_SalesOrderStatus__c != 'Canceled' ORDER BY gii__account__c, giic_ordertype__c";
var marketing_competitive_allowance_query = "SELECT COUNT(Id) NumberOfRecords, giic_SalesOrder__c, giic_PricingDescription__c, giic_SalesOrderLine__r.gii__Product__r.gii__ProductReference__r.Material_Division_US__c, AVG(giic_Rate__c) AverageRate, SUM(giic_AmountValue__c) TotalAmount,SUM(giic_Rate__c) TotalRate,giic_Crop__c FROM gii__PricingLine__c WHERE (giic_SalesOrder__r.gii__Account__c IN (" + grower_id_query_string + ") AND ((giic_PricingDescription__c = 'Volume Discount' AND giic_Rate__c != 0) OR giic_PricingDescription__c IN ('Competitive Allowance', 'Volume Discount', 'Growing Grower Discount','New Grower Discount', 'Returning Grower Discount')) AND giic_SalesOrder__r.giic_OrderType__c = 'Grower Order' AND giic_SalesOrder__r.giic_SalesYear__c = '" + sales_year_id + "' AND giic_SalesOrder__r.giic_SoldToReseller__c = '" + reseller_Id + "' ) GROUP BY giic_SalesOrder__c, giic_PricingDescription__c, giic_SalesOrderLine__r.gii__Product__r.gii__ProductReference__r.Material_Division_US__c, giic_SalesOrder__r.gii__Account__c,giic_Crop__c ORDER BY giic_SalesOrder__r.gii__Account__c, giic_PricingDescription__c,giic_Crop__c";
var marketing_early_pay_query = "SELECT Id, giic_SalesOrder__c, giic_EP_Qualified_Discounts__c, giic_SalesOrder__r.giic_OrderType__c, giic_SalesOrder__r.gii__Account__r.Name FROM gii__PricingLine__c WHERE giic_SalesOrder__r.gii__Account__c IN (" + grower_id_query_string + ") AND giic_PricingDescription__c = 'Early Pay Discount' AND giic_SalesOrder__r.giic_OrderType__c = 'Grower Order' AND giic_SalesOrder__r.giic_SalesYear__c = '" + sales_year_id + "' AND giic_SalesOrder__r.giic_SoldToReseller__c = '" + reseller_Id + "' AND giic_AmountValue__c>0 AND giic_IsClone__c = false ORDER BY giic_SalesOrder__r.gii__Account__c, giic_SalesOrder__c, Id DESC";
var discretionary_query = "Select giic_Crop__c, giic_Discount_Apply_Type__c, giic_Tier1_Date__c, giic_PricingDescription__c, giic_AmountValue__c, giic_SalesOrder__r.gii__Account__c From gii__PricingLine__c WHERE giic_Promotion__c= 'DiscAmt' and giic_Record_Type__c='Grower Discretionary Dollars' and giic_SalesOrder__r.gii__Account__c IN (" + grower_id_query_string + ") AND giic_SalesOrder__r.giic_SalesYear__c = '" + sales_year_id + "' AND giic_SalesOrder__r.giic_SoldToReseller__c = '" + reseller_Id + "'";
var pricelineAmountSum_query = "SELECT SUM(giic_AmountValue__c) amt, giic_Crop__c, giic_SalesOrder__r.gii__Account__c Acc from gii__PricingLine__c where giic_Record_Type__c ='Grower Program' AND giic_Promotion__c!= 'Steward' AND giic_IsClone__c = false and giic_SalesOrder__r.gii__Account__c IN (" + grower_id_query_string + ") and giic_Crop__c <> '' AND giic_SalesOrder__r.giic_SalesYear__c ='" + sales_year_id + "' and giic_SalesOrder__r.giic_SoldToReseller__c = '" + reseller_Id + "' group by giic_SalesOrder__r.gii__Account__c,giic_Crop__c";
var totalAmountPaid_query = "select sum(giic_Deposit_Amount__c) amt, giic_Grower_Number_Lookup__c from giic_Account_Payment__c where giic_Applied_Amount__c <> 0 and giic_Crop_Year_Lookup__c = '" + sales_year_id + "' and giic_Dealer_Number_Lookup__c = '" + reseller_Id + "' and giic_Grower_Number_Lookup__c IN (" + grower_id_query_string + ") group by giic_Grower_Number_Lookup__c";
var replant_charges_query = "Select Id, gii__TotalOrderAmount__c, giic_TotalReplantDiscount__c, giic_ApprovalStatus__c,giic_OrderType__c,gii__account__c From gii__SalesOrder__c WHERE gii__account__c IN (" + grower_id_query_string + ") AND giic_SalesYear__c = '" + sales_year_id + "' AND giic_SoldToReseller__c = '" + reseller_Id + "' AND giic_OrderType__c = 'Replant Order' Order BY gii__Account__c, giic_OrderType__c";
var loyalty_query = "SELECT sum(giic_AmountValue__c) amountvalue, min(giic_ratevalue__c) rate, giic_Crop__c, giic_SalesOrder__r.gii__Account__c laccount FROM gii__PricingLine__c WHERE giic_Promotion__c = 'Loyalty' and giic_SalesOrder__r.gii__Account__c IN (" + grower_id_query_string + ") AND giic_SalesOrder__r.giic_OrderType__c = 'Grower Order' AND giic_SalesOrder__r.giic_SalesYear__c = '" + sales_year_id + "' AND giic_SalesOrder__r.giic_SoldToReseller__c = '" + reseller_Id + "' AND giic_AmountValue__c>0 group by giic_SalesOrder__r.gii__Account__c,giic_Crop__c";
var additional_fees_query = "SELECT giic_Fee__c,giic_Net_Position_Units__c,gii__SalesOrder__c FROM gii__SalesOrderLine__c WHERE giic_Net_Position_Units__c > 0 AND gii__SalesOrder__r.gii__Account__c IN (" + grower_id_query_string + ") AND gii__SalesOrder__r.giic_SalesYear__c = '" + sales_year_id + "' AND gii__SalesOrder__r.giic_SoldToReseller__c = '" + reseller_Id + "' and (giic_Fee__c !=null and giic_Fee__c>0)";
var EP_query = "SELECT Id, giic_SalesOrder__c, giic_EP_Qualified_Discounts__c, giic_SalesOrder__r.giic_OrderType__c, giic_SalesOrder__r.gii__Account__r.Name FROM gii__PricingLine__c WHERE giic_SalesOrder__r.gii__Account__c IN (" + grower_id_query_string + ") AND giic_PricingDescription__c = 'Early Pay Discount' AND giic_SalesOrder__r.giic_OrderType__c = 'Grower Order' AND giic_SalesOrder__r.giic_SalesYear__c = '" + sales_year_id + "' AND giic_SalesOrder__r.giic_SoldToReseller__c = '" + reseller_Id + "' ORDER BY giic_SalesOrder__r.gii__Account__c, giic_SalesOrder__c, createddate desc";
var promotion_query = "SELECT giic_Tier1DiscountPercent__c, giic_Tier2DiscountPercent__c,giic_Tier3DiscountPercent__c, giic_Tier4DiscountPercent__c, giic_Tier5DiscountPercent__c, giic_Tier6DiscountPercent__c, giic_Tier7DiscountPercent__c, giic_Tier8DiscountPercent__c, giic_Tier_1_Display_Date__c, giic_Tier_2_Display_Date__c, giic_Tier_3_Display_Date__c, giic_Tier_4_Display_Date__c, giic_Tier_5_Display_Date__c, giic_Tier_6_Display_Date__c, giic_Tier_7_Display_Date__c, giic_Tier_8_Display_Date__c, giic_Tier1Date__c, giic_Tier2Date__c, giic_Tier3Date__c, giic_Tier4Date__c, giic_Tier5Date__c, giic_Tier6Date__c, giic_Tier7Date__c, giic_Tier8Date__c, Name FROM gii__Promotion__c WHERE giic_PromotionType__c IN ('EP-Cash','EP-Fin') AND giic_SalesYear__c = '" + sales_year_id + "' ORDER BY Name";
var totalDiscount_query = "SELECT SUM(giic_AmountValue__c) discountTotal, giic_SalesOrder__r.gii__Account__c Acc from gii__PricingLine__c where giic_Record_Type__c ='Grower Program' AND giic_Promotion__c NOT IN ('Steward', 'EP') AND giic_IsClone__c = false and giic_SalesOrder__r.gii__Account__c IN (" + grower_id_query_string + ") and giic_Crop__c <> '' AND giic_SalesOrder__r.giic_SalesYear__c ='" + sales_year_id + "' and giic_SalesOrder__r.giic_SoldToReseller__c = '" + reseller_Id + "' group by giic_SalesOrder__r.gii__Account__c";
formyoula.sfdc.query(query_string, function(data) {
is_sales_orders = true;
sales_orders = data.records;
all_salesorder_grower_order_type = $.grep(sales_orders, function(e){ return e.giic_OrderType__c == 'Grower Order'; });
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
formyoula.sfdc.query(marketing_competitive_allowance_query, function(data) {
marketing_totals = data.records;
is_marketing_totals = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
formyoula.sfdc.query(marketing_early_pay_query, function(data) {
$.each(data.records, function (i, e) {
var matchingItems = $.grep(marketing_early_pay, function (item) {
return item.giic_SalesOrder__c === e.giic_SalesOrder__c;
});
if (matchingItems.length === 0){
marketing_early_pay.push(e);
}
});
//console.log(marketing_early_pay);
is_marketing_early_pay = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_discount && is_promotion){
process_record(0);
}
});
// Discretionary
formyoula.sfdc.query(discretionary_query, function(data) {
discretionary = data.records;
is_discretionary = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
// Pricing line amount sum
formyoula.sfdc.query(pricelineAmountSum_query, function(data) {
pricelineAmountSum = data.records;
is_pricelineAmountSum = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
// Total Paid Amount
formyoula.sfdc.query(totalAmountPaid_query, function(data) {
paidAmount = data.records;
is_paidAmount = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
// REplant Charges
formyoula.sfdc.query(replant_charges_query, function(data) {
replantCharges = data.records;
is_replantCharges = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
// Loyalty Amount
formyoula.sfdc.query(loyalty_query, function(data) {
loyaltyAmount = data.records;
is_loyalty = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
// Additional fee and discount
formyoula.sfdc.query(additional_fees_query, function(data) {
additionalFees = data.records;
is_additional_fees = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
//Early Pay
formyoula.sfdc.query(EP_query, function(data) {
$.each(data.records, function (i, e) {
var matchingItemsEP = $.grep(EPData, function (item) {
return item.giic_SalesOrder__c === e.giic_SalesOrder__c;
});
if (matchingItemsEP.length === 0){
EPData.push(e);
}
});
is_EP = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
// Total discount query except EP discount
formyoula.sfdc.query(totalDiscount_query, function(data) {
discountTotal = data.records;
is_discount = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
// EP Promotion
formyoula.sfdc.query(promotion_query, function(data) {
promotionData = data.records;
is_promotion = true;
if(is_sales_orders && is_marketing_early_pay && is_marketing_totals && is_discretionary && is_pricelineAmountSum && is_paidAmount && is_replantCharges && is_loyalty && is_additional_fees && is_EP && is_discount && is_promotion){
process_record(0);
}
});
function genrate_pdf(record_index) {
//Get and collect the pdf data
//var get pdf data
formyoula.get_pdf_html("", function(pdf) {
formyoula.form_fields["9c0d-5b18-a96e"].set({ "value": '' }, { silent: true }); // corn SOLI
formyoula.form_fields["898b-4aec-5acb"].set({ "value": '' }, { silent: true }); // Soy SOLI
formyoula.form_fields["0006-ae7e-65ed"].set({ "value": '' }, { silent: true }); // FOC - corn SOLI
formyoula.form_fields["6b03-cdd4-9231"].set({ "value": '' }, { silent: true }); // FOC - Soy SOLI
// formyoula.form_fields["6425-b431-bf67"].set({ "value": '' }, { silent: true }); // replant Soya SOLI
// formyoula.form_fields["1a0b-f2a2-f726"].set({ "value": '' }, { silent: true }); // replant corn SOLI
formyoula.form_fields["747d-1780-0116"].set({ "value": '' }, { silent: true }); // discretionary corn
formyoula.form_fields["e2eb-727c-d1e3"].set({ "value": '' }, { silent: true }); // discretionary soybean
formyoula.form_fields["bd38-e22e-b4c7"].set("value",0);
formyoula.form_fields["b6d3-a096-45a9"].set("value",0);
formyoula.form_fields["357c-b91b-fbac"].set("value",0);
formyoula.form_fields["0018-fc90-bae0"].set("value",0);
formyoula.form_fields["d335-19cb-1695"].set("value",0);
formyoula.form_fields["7fc3-3488-b85d"].set("value",0);
formyoula.form_fields["ce3f-5256-e1ac"].set("value",0);
formyoula.form_fields["a3fa-f754-843f"].set("value",0);
formyoula.form_fields["532d-0144-e2c1"].set("value",0);
formyoula.form_fields["46af-d0c9-c3fd"].set("value",0);
formyoula.form_fields["80ff-7753-39e8"].set("value",0);
formyoula.form_fields["a3f4-e408-a819"].set("value",0);
formyoula.form_fields["6ed2-7ac9-d5f0"].set("value",0);
console.log('----genrate_pdf---record_index------------')
console.log(formyoula.form_fields["e2eb-727c-d1e3"].get('value'));
console.log(pdf.pdf_html)
console.log('------genrate_pdf-----record_index--------')
var opt = {
margin: [0.3, 0.2, 0.3, 0.2],
filename: (pdf.pdfFileName),
html2canvas: {
scale: 6,
letterRendering: true,
windowWidth: 595,
windowHeight: 830,
useCORS: true
},
jsPDF: { unit: 'in', format: 'a4', orientation: 'portrait' }
};
html2pdf().from(pdf.pdf_html).set(opt).toPdf().output('datauristring').then(function(pdfAsString) {
// The PDF has been converted to a Data URI string and passed to this function.
// Use pdfAsString however you like (ZIP, send as email, etc)
var pdf_data = {}
pdf_data[pdf.pdfFileName] = pdfAsString;
all_pdf_data.push(pdf_data);
console.log(all_pdf_data)
return process_record(record_index + 1);
});
});
}
function process_record(i){
if(i<all_salesorder_grower_order_type.length){
var current_marketing_totals = $.grep(marketing_totals, function(e){ return e.giic_SalesOrder__c == all_salesorder_grower_order_type[i].Id; });
var current_marketing_early_pay = $.grep(marketing_early_pay, function(e){ return e.giic_SalesOrder__c == all_salesorder_grower_order_type[i].Id; });
var replant_orders = $.grep(sales_orders, function(e){ return (e.giic_OrderType__c == 'Replant Order' && e.gii__Account__c == all_salesorder_grower_order_type[i].gii__Account__c); })[0];
var free_of_charge_orders = $.grep(sales_orders, function(e){ return (e.giic_OrderType__c == 'Grower Order-FOC' && e.gii__Account__c == all_salesorder_grower_order_type[i].gii__Account__c); })[0];
var discretionary_data = $.grep(discretionary, function(e){ return (e.giic_SalesOrder__r.gii__Account__c == all_salesorder_grower_order_type[i].gii__Account__c); });
var pricingLineTotals_Corn = $.grep(pricelineAmountSum, function(e){ return (e.Acc == all_salesorder_grower_order_type[i].gii__Account__c && e.giic_Crop__c == '01'); });
var pricingLineTotals_Soybean = $.grep(pricelineAmountSum, function(e){ return (e.Acc == all_salesorder_grower_order_type[i].gii__Account__c && e.giic_Crop__c == '3C'); });
var TotalPaidAmount = $.grep(paidAmount, function(e){ return (e.giic_Grower_Number_Lookup__c == all_salesorder_grower_order_type[i].gii__Account__c); });
formyoula.form_fields["5246-a3b8-80bf"].set("value", 0);
if(TotalPaidAmount.length > 0){
console.log('TotalPaidAmount::',TotalPaidAmount[0].amt);
formyoula.form_fields["5246-a3b8-80bf"].set("value", TotalPaidAmount[0].amt);
}
var ReplantChargesDetails = $.grep(replantCharges, function(e){ return (e.gii__Account__c == all_salesorder_grower_order_type[i].gii__Account__c) ; });
console.log('ReplantChargesDetails:',ReplantChargesDetails);
var loyaltyDetails = $.grep(loyaltyAmount, function(e){ return e.laccount == all_salesorder_grower_order_type[i].gii__Account__c;});
console.log('loyaltyDetails:',loyaltyDetails);
var additionFeeDetails = $.grep(additionalFees, function(e){ return e.gii__SalesOrder__c ==all_salesorder_grower_order_type[i].Id;});
console.log('additionalFees:',additionFeeDetails);
var EP_Details = $.grep(EPData, function(e){ return e.giic_SalesOrder__c == all_salesorder_grower_order_type[i].Id; });
console.log('EP details::::',EP_Details);
var totalDiscountDetails = $.grep(discountTotal, function(e){ return (e.Acc == all_salesorder_grower_order_type[i].gii__Account__c) ; });
console.log('discountTotal:',totalDiscountDetails);
var EP_Promotion_Details = $.grep(promotionData, function(e){ return e; });
console.log('EP_Promotion_Details::',EP_Promotion_Details);
//set all form fields data
formyoula.form_fields["a10c-a47f-5d2c"].set("value", all_salesorder_grower_order_type[i].giic_SoldToReseller__r.Name);
//set seed advisor Number
formyoula.form_fields["2cd8-7034-02b3"].set("value", all_salesorder_grower_order_type[i].giic_SoldToReseller__r.FSAP_ID__c);
//set seed advisor Street
formyoula.form_fields["a83e-8e0d-87ed"].set("value", all_salesorder_grower_order_type[i].giic_SoldToReseller__r.ShippingStreet);
//set seed advisor City
formyoula.form_fields["d469-8a8d-ac87"].set("value", all_salesorder_grower_order_type[i].giic_SoldToReseller__r.ShippingCity);
//set seed advisor State
formyoula.form_fields["c502-ae63-d5b3"].set("value", all_salesorder_grower_order_type[i].giic_SoldToReseller__r.ShippingState);
//set seed advisor Zipcode
formyoula.form_fields["c73e-cd96-a394"].set("value", all_salesorder_grower_order_type[i].giic_SoldToReseller__r.ShippingPostalCode);
//set seed advisor Phone
formyoula.form_fields["53a9-052e-bc82"].set("value", all_salesorder_grower_order_type[i].giic_SoldToReseller__r.Phone);
//set seed advisor email
formyoula.form_fields["5517-dbcc-89b9"].set("value", all_salesorder_grower_order_type[i].giic_SoldToReseller__r.Email__c);
//set Sales year name
formyoula.form_fields["c6d9-46bb-c23e"].set("value", all_salesorder_grower_order_type[i].giic_SalesYear__r.Name);
//set Sales Rep Name
if(all_salesorder_grower_order_type[i].giic_Sales_Rep__c){
formyoula.form_fields["925e-666b-23b8"].set("value", all_salesorder_grower_order_type[i].giic_Sales_Rep__r.Name);
}
//set Grower Name
formyoula.form_fields["b71f-037a-60ea"].set("value", all_salesorder_grower_order_type[i].gii__Account__r.Name);
//set Gower advisor Number
formyoula.form_fields["0bf9-ca7c-5777"].set("value", all_salesorder_grower_order_type[i].gii__Account__r.FSAP_ID__c);
//set Gower advisor Street
formyoula.form_fields["886b-b9da-4adc"].set("value", all_salesorder_grower_order_type[i].gii__Account__r.BillingStreet);
//set Gower advisor City
formyoula.form_fields["82d2-aede-1c3d"].set("value", all_salesorder_grower_order_type[i].gii__Account__r.BillingCity);
//set Gower advisor State
formyoula.form_fields["daa0-ae38-c892"].set("value", all_salesorder_grower_order_type[i].gii__Account__r.BillingState);
//set Gower advisor Zipcode
formyoula.form_fields["67de-5fed-e13b"].set("value", all_salesorder_grower_order_type[i].gii__Account__r.BillingPostalCode);
//set Gower advisor Phone
formyoula.form_fields["4af9-306c-0071"].set("value", all_salesorder_grower_order_type[i].gii__Account__r.Phone);
//set Gower advisor email
formyoula.form_fields["dbae-0fe7-8ad5"].set("value", all_salesorder_grower_order_type[i].gii__Account__r.Email__c);
//set all seeds total amount
//formyoula.form_fields["c324-46c6-a3ed"].set("value", all_salesorder_grower_order_type[i].gii__TotalAmount__c);
console.log('giic_ApprovalStatus__c:'+ all_salesorder_grower_order_type[i].giic_ApprovalStatus__c);
var approvalStatusSO = all_salesorder_grower_order_type[i].giic_ApprovalStatus__c == "Pending" ? "Submitted" : all_salesorder_grower_order_type[i].giic_ApprovalStatus__c ;
console.log('giic_ApprovalStatus__c:::'+ approvalStatusSO);
formyoula.form_fields["824e-6d40-44dd"].set("value", approvalStatusSO);
/*Load SFDC table element for Corn SOLI*/
//Variable to get all corn based SOLI records
seedGrossTotal = 0;
formyoula.form_fields["c324-46c6-a3ed"].set("value",seedGrossTotal);
//if(all_salesorder_grower_order_type[i].giic_OrderType__c == 'Grower Order'){
var sales_order_lineitems_corn = $.grep(all_salesorder_grower_order_type[i].gii__SalesOrder__r.records, function(e){ return e.gii__Product__r.gii__ProductReference__r.Crop_Group__c === 'COR: CORN'; });
formyoula.form_fields["9c0d-5b18-a96e"].set({ "value": "" }, { silent: true }); // corn SOLI
formyoula.form_fields["532d-0144-e2c1"].set("value", 0).trigger("change");
formyoula.form_fields["bd38-e22e-b4c7"].set("value", 0).trigger("change");
formyoula.form_fields["1e77-ed0b-fe06"].set("value", 0);
console.log('in corn oli',sales_order_lineitems_corn);
if(sales_order_lineitems_corn.length > 0){
//formyoula.form_fields["9c0d-5b18-a96e"].trigger("table:row:new", sales_order_lineitems_corn);
formyoula.form_fields["9c0d-5b18-a96e"].set({"value": sales_order_lineitems_corn}, { silent: true }).trigger("change");
for(var ii =0; ii<sales_order_lineitems_corn.length; ii++){
var prodAmount = sales_order_lineitems_corn[ii].giic_Net_Position_Units__c * sales_order_lineitems_corn[ii].gii__OriginalUnitPrice__c;
sales_order_lineitems_corn[ii].giic_NetPositionProductAmount__c = prodAmount;
sales_order_lineitems_corn[ii].giic_comments__c = sales_order_lineitems_corn[ii].gii__Product__r.gii__ProductReference__r.Material_Stacked_Traits__c;
}
formyoula.form_fields["532d-0144-e2c1"].set("value", sales_order_lineitems_corn.map(o=>o.giic_NetPositionProductAmount__c).reduce((a,c)=>a+c));
formyoula.form_fields["bd38-e22e-b4c7"].set("value", sales_order_lineitems_corn.map(o=>o.giic_Net_Position_Units__c).reduce((a,c)=>a+c));
seedGrossTotal = seedGrossTotal + sales_order_lineitems_corn.map(o=>o.giic_NetPositionProductAmount__c).reduce((a,c)=>a+c);
var amountcorn = formyoula.form_fields["532d-0144-e2c1"].get("value");
var unitcorn = formyoula.form_fields["bd38-e22e-b4c7"].get("value");
var listCorn = amountcorn/unitcorn;
var listvalcorn = Number(listCorn).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["a420-ce12-4916"].set("value", listvalcorn);
}
console.log('seedGrossTotal corn:'+seedGrossTotal);
/*Load SFDC table element for Soybean SOLI*/
//Variable to get all Soybean based SOLI records
var sales_order_lineitems_soybean = $.grep(all_salesorder_grower_order_type[i].gii__SalesOrder__r.records, function(e){ return e.gii__Product__r.gii__ProductReference__r.Crop_Group__c === 'SYB: SOYBEAN'; });
formyoula.form_fields["898b-4aec-5acb"].set({ "value": "" }, { silent: true }); // SOY SOLI
formyoula.form_fields["d335-19cb-1695"].set("value", 0).trigger("change");
formyoula.form_fields["357c-b91b-fbac"].set("value", 0).trigger("change");
formyoula.form_fields["ff03-8c3e-e64c"].set("value", 0);
console.log('in soy oli',sales_order_lineitems_soybean);
if(sales_order_lineitems_soybean.length > 0){
console.log('in soy if');
formyoula.form_fields["898b-4aec-5acb"].set({"value": sales_order_lineitems_soybean}, { silent: true }).trigger("change");
for(var ij =0; ij<sales_order_lineitems_soybean.length; ij++){
var prodAmount = sales_order_lineitems_soybean[ij].giic_Net_Position_Units__c * sales_order_lineitems_soybean[ij].gii__OriginalUnitPrice__c;
sales_order_lineitems_soybean[ij].giic_NetPositionProductAmount__c = prodAmount;
sales_order_lineitems_soybean[ij].giic_comments__c = sales_order_lineitems_soybean[ij].gii__Product__r.gii__ProductReference__r.Material_Stacked_Traits__c;
}
formyoula.form_fields["d335-19cb-1695"].set("value", sales_order_lineitems_soybean.map(o=>o.giic_NetPositionProductAmount__c).reduce((a,c)=>a+c));
formyoula.form_fields["357c-b91b-fbac"].set("value", sales_order_lineitems_soybean.map(o=>o.giic_Net_Position_Units__c).reduce((a,c)=>a+c));
seedGrossTotal = seedGrossTotal + sales_order_lineitems_soybean.map(o=>o.giic_NetPositionProductAmount__c).reduce((a,c)=>a+c);
var amountsoy = formyoula.form_fields["d335-19cb-1695"].get("value");
var unitsoy = formyoula.form_fields["357c-b91b-fbac"].get("value");
var listSoy = amountsoy/unitsoy;
var listvalsoy = Number(listSoy).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["eefe-3793-f25b"].set("value", listvalsoy);
}
console.log('seedGrossTotal Soy:'+seedGrossTotal);
formyoula.form_fields["c324-46c6-a3ed"].set("value",seedGrossTotal);
//}
// Additional Fee Calculation
var TotalAdditionalFee = 0;
if(additionFeeDetails.length>0){
for(var j=0; j<additionFeeDetails.length ; j++){
var fee = additionFeeDetails[j].giic_Fee__c;
var unit = additionFeeDetails[j].giic_Net_Position_Units__c;
TotalAdditionalFee = TotalAdditionalFee + (fee * unit);
}
formyoula.form_fields["6ed2-7ac9-d5f0"].set("value", TotalAdditionalFee);
}
console.log('TotalAdditionalFee::'+TotalAdditionalFee);
//discretionary
var discretionary_corn = $.grep(discretionary_data, function(e){ return e.giic_Crop__c =='01'; });
var discretionary_soya = $.grep(discretionary_data, function(e){ return e.giic_Crop__c =='3C'; });
var finalAmountCorn = 0;
var finalAmountSoya = 0;
var finalAmountDiscAmt = 0;
formyoula.form_fields["747d-1780-0116"].set({"value": "" }, { silent: true });
formyoula.form_fields["e2eb-727c-d1e3"].set({"value": "" }, { silent: true });
formyoula.form_fields["46dc-fdb7-c847"].set("value", 0).trigger("change");
formyoula.form_fields["2f88-a626-5b0c"].set("value", 0).trigger("change");
formyoula.form_fields["9ad0-bbc0-c218"].set("value", 0).trigger("change");
if(discretionary_corn.length > 0){
console.log('in bal if',discretionary_corn);
for(j=0;j<discretionary_corn.length;j++){
// var crop = discretionary_corn[j].giic_Crop__c =='01'?'Corn':'Soybean';
discretionary_corn[j].giic_Crop__c = 'Corn';
var adjamountAfterBal = discretionary_corn[j].giic_Discount_Apply_Type__c == 'Credit' ? -discretionary_corn[j].giic_AmountValue__c : discretionary_corn[j].giic_AmountValue__c;
finalAmountCorn = finalAmountCorn + adjamountAfterBal;
finalAmountDiscAmt = finalAmountDiscAmt + adjamountAfterBal;
var adjamountAfterBalPDF = discretionary_corn[j].giic_Discount_Apply_Type__c == 'Credit' ? '($' + Number(discretionary_corn[j].giic_AmountValue__c).toLocaleString(undefined,{ minimumFractionDigits: 2 } )+ ')': '$' +Number(discretionary_corn[j].giic_AmountValue__c).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
discretionary_corn[j].giic_AmountValue__c = adjamountAfterBalPDF;
var tierDateAfterBal = moment(discretionary_corn[j].giic_Tier1_Date__c, "YYYY-MM-DD").format("MM/DD/YY") ;
discretionary_corn[j].giic_Tier1_Date__c = tierDateAfterBal;
}
formyoula.form_fields["747d-1780-0116"].set({"value": discretionary_corn}, { silent: true }).trigger("change");
var famount = "";
if(finalAmountCorn>0){
famount = '$' + Number(finalAmountCorn).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["2f88-a626-5b0c"].set("value",famount);
}else{
var absVal = Math.abs(finalAmountCorn);
var valAB = Number(absVal).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
famount = '($'+ valAB +')';
formyoula.form_fields["2f88-a626-5b0c"].set("value",famount);
}
}
if(discretionary_soya.length > 0){
console.log('in bal if',discretionary_soya);
for(j=0;j<discretionary_soya.length;j++){
// var crop = discretionary_soya[j].giic_Crop__c =='01'?'Corn':'Soybean';
discretionary_soya[j].giic_Crop__c = 'Soybean';
var adjamountAfterBal = discretionary_soya[j].giic_Discount_Apply_Type__c == 'Credit' ? -discretionary_soya[j].giic_AmountValue__c : discretionary_soya[j].giic_AmountValue__c;
finalAmountSoya = finalAmountSoya + adjamountAfterBal;
finalAmountDiscAmt = finalAmountDiscAmt + adjamountAfterBal;
var adjamountAfterBalPDF = discretionary_soya[j].giic_Discount_Apply_Type__c == 'Credit' ? '($' + Number(discretionary_soya[j].giic_AmountValue__c).toLocaleString(undefined,{ minimumFractionDigits: 2 } )+ ')': '$' +Number(discretionary_soya[j].giic_AmountValue__c).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
discretionary_soya[j].giic_AmountValue__c = adjamountAfterBalPDF;
var tierDateAfterBal = moment(discretionary_soya[j].giic_Tier1_Date__c, "YYYY-MM-DD").format("MM/DD/YY") ;
discretionary_soya[j].giic_Tier1_Date__c = tierDateAfterBal;
}
formyoula.form_fields["e2eb-727c-d1e3"].set({"value": discretionary_soya}, { silent: true }).trigger("change");
console.log("Soya -- Data -- logs")
console.log(formyoula.form_fields["e2eb-727c-d1e3"].get('value'));
console.log("Soya -- Data -- logs");
var famount = "";
if(finalAmountSoya>0){
famount = '$' + Number(finalAmountSoya).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["9ad0-bbc0-c218"].set("value",famount);
}else{
var absVal = Math.abs(finalAmountSoya);
var valAB = Number(absVal).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
famount = '($'+ valAB +')';
formyoula.form_fields["9ad0-bbc0-c218"].set("value",famount);
}
}
var discDollarAmt = "";
if(finalAmountDiscAmt>0){
discDollarAmt = '$' + Number(finalAmountDiscAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["46dc-fdb7-c847"].set("value",discDollarAmt);
}else{
var absVal = Math.abs(finalAmountDiscAmt);
var valAB = Number(absVal).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
discDollarAmt = '($'+ valAB +')';
formyoula.form_fields["46dc-fdb7-c847"].set("value",discDollarAmt);
}
// Pricing Line amount sum
//var pricingTotal_corn = $.grep(pricingLineTotals, function(e){ return (e.giic_Crop__c == '01'); });
if(pricingLineTotals_Corn.length > 0){
formyoula.form_fields["46af-d0c9-c3fd"].set("value", pricingLineTotals_Corn["0"].amt);
}else{
formyoula.form_fields["46af-d0c9-c3fd"].set("value", 0);
}
//var pricingTotal_soy = $.grep(pricingLineTotals, function(e){ return (e.giic_Crop__c == '3C'); });
if(pricingLineTotals_Soybean.length > 0){
formyoula.form_fields["7fc3-3488-b85d"].set("value", pricingLineTotals_Soybean["0"].amt);
}else{
formyoula.form_fields["7fc3-3488-b85d"].set("value", 0);
}
console.log('pricingline sum : corn::'+formyoula.form_fields["46af-d0c9-c3fd"].get("value")+ '..soy::'+ formyoula.form_fields["7fc3-3488-b85d"].get("value"));
var cornDisc = formyoula.form_fields["46af-d0c9-c3fd"].get("value");
var soyDisc = formyoula.form_fields["7fc3-3488-b85d"].get("value");
var programDiscount = cornDisc + soyDisc;
formyoula.form_fields["de8b-f90e-5b1f"].set("value", programDiscount);
var replant_order_total = 0;
var cornReplant = 0;
var soyReplant = 0;
formyoula.form_fields["05e2-b20e-2751"].set({ "value": "" }, { silent: true }); // corn SOLI
formyoula.form_fields["6425-b431-bf67"].set({ "value": "" }, { silent: true }); // soy SOLI
formyoula.form_fields["3e84-02f6-63e9"].set("value", 0);
formyoula.form_fields["53f5-18cf-e081"].set("value", 0);
formyoula.form_fields["ac0e-58dd-6db3"].set("value", 0);
if(replant_orders){
console.log('in replant_order:',replant_orders);
var replant_sales_order_lineitems_corn = $.grep(replant_orders.gii__SalesOrder__r.records, function(e){ return e.gii__Product__r.gii__ProductReference__r.Crop_Group__c === 'COR: CORN'; });
//formyoula.form_fields["05e2-b20e-2751"].set({ "value": "" }, { silent: true }); // corn SOLI
formyoula.form_fields["ac0e-58dd-6db3"].set("value", 0.0);
formyoula.form_fields["1a0b-f2a2-f726"].set("value",0.0);
formyoula.form_fields["3e84-02f6-63e9"].set("value",0);
formyoula.form_fields["b6d3-a096-45a9"].set("value",0);
formyoula.form_fields["80ff-7753-39e8"].set("value",0);
if(replant_sales_order_lineitems_corn.length > 0){
formyoula.form_fields["05e2-b20e-2751"].set({"value": replant_sales_order_lineitems_corn}, { silent: true }).trigger("change");
// formyoula.form_fields["ac0e-58dd-6db3"].set("value", replant_orders.gii__TotalAmount__c);
for(var cr=0; cr<replant_sales_order_lineitems_corn.length; cr++){
cornReplant = cornReplant + (replant_sales_order_lineitems_corn[cr].gii__OrderQuantity__c * replant_sales_order_lineitems_corn[cr].gii__OriginalUnitPrice__c);
}
replant_order_total = replant_orders.gii__TotalAmount__c;
formyoula.form_fields["1a0b-f2a2-f726"].set("value", replant_order_total);
formyoula.form_fields["3e84-02f6-63e9"].set("value", replant_sales_order_lineitems_corn.map(o=>o.giic_ImpactedQty__c).reduce((a,c)=>a+c));
formyoula.form_fields["b6d3-a096-45a9"].set("value", replant_sales_order_lineitems_corn.map(o=>o.gii__OrderQuantity__c).reduce((a,c)=>a+c));
formyoula.form_fields["80ff-7753-39e8"].set("value", replant_sales_order_lineitems_corn.map(o=>o.giic_ReplantDiscount__c).reduce((a,c)=>a+c));
formyoula.form_fields["68e9-7f2c-befb"].set("value", replant_sales_order_lineitems_corn.map(o=>o.giic_NetPositionProductAmount__c).reduce((a,c)=>a+c));
}
var replant_sales_order_lineitems_soya = $.grep(replant_orders.gii__SalesOrder__r.records, function(e){ return e.gii__Product__r.gii__ProductReference__r.Crop_Group__c === 'SYB: SOYBEAN'; });
//formyoula.form_fields["6425-b431-bf67"].set({ "value": "" }, { silent: true }); // soy SOLI
formyoula.form_fields["ac0e-58dd-6db3"].set("value", 0.0);
formyoula.form_fields["1a0b-f2a2-f726"].set("value",0.0);
formyoula.form_fields["53f5-18cf-e081"].set("value",0);
formyoula.form_fields["0018-fc90-bae0"].set("value",0);
formyoula.form_fields["ce3f-5256-e1ac"].set("value",0);
if(replant_sales_order_lineitems_soya.length > 0){
formyoula.form_fields["6425-b431-bf67"].set({"value": replant_sales_order_lineitems_soya}, { silent: true }).trigger("change");
//formyoula.form_fields["ac0e-58dd-6db3"].set("value", replant_orders.gii__TotalAmount__c);
for(var sr=0; sr<replant_sales_order_lineitems_soya.length; sr++){
soyReplant = soyReplant + (replant_sales_order_lineitems_soya[sr].gii__OrderQuantity__c * replant_sales_order_lineitems_soya[sr].gii__OriginalUnitPrice__c);
}
//soyReplant = replant_orders.gii__TotalAmount__c;
replant_order_total = replant_orders.gii__TotalAmount__c;
formyoula.form_fields["1a0b-f2a2-f726"].set("value", replant_order_total);
formyoula.form_fields["53f5-18cf-e081"].set("value", replant_sales_order_lineitems_soya.map(o=>o.giic_ImpactedQty__c).reduce((a,c)=>a+c));
formyoula.form_fields["0018-fc90-bae0"].set("value", replant_sales_order_lineitems_soya.map(o=>o.gii__OrderQuantity__c).reduce((a,c)=>a+c));
formyoula.form_fields["ce3f-5256-e1ac"].set("value", replant_sales_order_lineitems_soya.map(o=>o.giic_ReplantDiscount__c).reduce((a,c)=>a+c));
formyoula.form_fields["2751-a366-4fd9"].set("value", replant_sales_order_lineitems_soya.map(o=>o.giic_NetPositionProductAmount__c).reduce((a,c)=>a+c));
}
var replantTotal = cornReplant + soyReplant;
// formyoula.form_fields["ac0e-58dd-6db3"].set("value", replantTotal);
formyoula.form_fields["ac0e-58dd-6db3"].set("value", replant_orders.gii__TotalAmount__c);
}
//Calculation
var replant_qty_impact_corn = formyoula.form_fields["3e84-02f6-63e9"].get("value") == "" ? 0 : formyoula.form_fields["3e84-02f6-63e9"].get("value");
var replant_qty_impact_soybean = formyoula.form_fields["53f5-18cf-e081"].get("value") == "" ? 0 : formyoula.form_fields["53f5-18cf-e081"].get("value");
var replant_qty_impact_total = replant_qty_impact_corn + replant_qty_impact_soybean;
formyoula.form_fields["81e3-484f-222a"].set("value", replant_qty_impact_total);
console.log('replant_qty_impact_total::'+replant_qty_impact_total+'..field value::'+formyoula.form_fields["81e3-484f-222a"].get("value"));
var replant_qty_corn = formyoula.form_fields["b6d3-a096-45a9"].get("value") == "" ? 0 : formyoula.form_fields["b6d3-a096-45a9"].get("value");
var replant_qty_soybean = formyoula.form_fields["0018-fc90-bae0"].get("value") == "" ? 0 : formyoula.form_fields["0018-fc90-bae0"].get("value");
var replant_qty_total = replant_qty_corn + replant_qty_soybean;
formyoula.form_fields["2bfd-cd12-131e"].set("value", replant_qty_total);
console.log('replant_qty_total::'+replant_qty_total+'..field value::'+formyoula.form_fields["2bfd-cd12-131e"].get("value"));
var replant_discount_corn = formyoula.form_fields["80ff-7753-39e8"].get("value") == "" ? 0 : formyoula.form_fields["80ff-7753-39e8"].get("value");
var replant_discount_soybean = formyoula.form_fields["ce3f-5256-e1ac"].get("value") == "" ? 0 : formyoula.form_fields["ce3f-5256-e1ac"].get("value");
var replant_discount_total = replant_discount_corn + replant_discount_soybean;
formyoula.form_fields["a9c3-24c7-c220"].set("value", replant_discount_total);
console.log('replant_discount_total::'+replant_discount_total+'..field value::'+formyoula.form_fields["a9c3-24c7-c220"].get("value"));
var corn_unit = formyoula.form_fields["bd38-e22e-b4c7"].get("value") == "" ? 0 : formyoula.form_fields["bd38-e22e-b4c7"].get("value");
var corn_final_units = corn_unit - replant_qty_corn;
formyoula.form_fields["146e-eefe-a31f"].set("value",corn_final_units);
var soybean_unit = formyoula.form_fields["357c-b91b-fbac"].get("value") == "" ? 0 : formyoula.form_fields["357c-b91b-fbac"].get("value");
var soybean_final_units = soybean_unit - replant_qty_soybean;
formyoula.form_fields["4ef7-c9e5-bdf0"].set("value",soybean_final_units);
console.log('final units: corn::'+corn_final_units+'...soybean::'+soybean_final_units);
//Replant Charges
formyoula.form_fields["1a0b-f2a2-f726"].set("value",0);
formyoula.form_fields["8db5-6cb6-27dc"].set("value","");
if(ReplantChargesDetails.length>0){
var replantTotal = ReplantChargesDetails["0"].gii__TotalOrderAmount__c;
var replantDiscount = ReplantChargesDetails["0"].giic_TotalReplantDiscount__c;
var replantStatus = ReplantChargesDetails["0"].giic_ApprovalStatus__c;
console.log('replantTotal:'+replantTotal+'..replantDiscount:'+replantDiscount+'..replantStatus:'+replantStatus);
var replant_Charges = replantTotal - replantDiscount;
formyoula.form_fields["1a0b-f2a2-f726"].set("value",replant_Charges);
formyoula.form_fields["8db5-6cb6-27dc"].set("value",replantStatus);
}else{
formyoula.form_fields["1a0b-f2a2-f726"].set("value",0);
formyoula.form_fields["8db5-6cb6-27dc"].set("value","");
}
var replantPurchaseTotal = (formyoula.form_fields["68e9-7f2c-befb"].get("value") + formyoula.form_fields["2751-a366-4fd9"].get("value"))==0?0:(formyoula.form_fields["68e9-7f2c-befb"].get("value") + formyoula.form_fields["2751-a366-4fd9"].get("value"));
formyoula.form_fields["1d1f-0825-23ed"].set("value",replantPurchaseTotal);
// set total purchase excluding replant
var cornTotal = formyoula.form_fields["532d-0144-e2c1"].get("value") - formyoula.form_fields["68e9-7f2c-befb"].get("value");
var cornUnits = formyoula.form_fields["bd38-e22e-b4c7"].get("value") - formyoula.form_fields["b6d3-a096-45a9"].get("value");
var soyaTotal = formyoula.form_fields["d335-19cb-1695"].get("value") - formyoula.form_fields["2751-a366-4fd9"].get("value");
var soyaUnits = formyoula.form_fields["357c-b91b-fbac"].get("value") - formyoula.form_fields["0018-fc90-bae0"].get("value");
var totalpurchase = cornTotal + soyaTotal;
var cornVal = Number(cornTotal).toLocaleString('en-US',{ currency: 'USD', style:'currency' } );
var soyaVal = Number(soyaTotal).toLocaleString('en-US',{ currency: 'USD', style:'currency' } );
var totalVal = Number(totalpurchase).toLocaleString('en-US',{ currency: 'USD', style:'currency' } );
var puchaseStr = "<tr><td>&nbsp;Total Purchase (excluding Replant)</td><td style=\"text-align: center;\">"+cornUnits+"</td><td style=\"text-align: center;\">$"+cornVal+"</td><td style=\"text-align: center;\">"+soyaUnits+"</td><td style=\"text-align: center;\">$"+soyaVal+"</td><td style=\"text-align: center;\">&nbsp;</td><td style=\"text-align: center;\">$"+totalVal+"</td></tr>";
formyoula.form_fields["3fa7-5c5d-7092"].set("value",puchaseStr);
var cornTotalDiscount = 0;
var soyaTotalDiscount = 0;
var totalDiscount = 0;
//Set Marketing - Volume Discount
var volume_discount_corn = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'Volume Discount' && e.giic_Crop__c == '01'); });
if(volume_discount_corn.length > 0 && is_Calculation_Include==false){
var volDisc = Number(volume_discount_corn["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["df51-4f26-6f8b"].set("value", volume_discount_corn["0"].TotalAmount);
formyoula.form_fields["4034-37d1-7e4d"].set("value", volDisc);
cornTotalDiscount = cornTotalDiscount + volume_discount_corn["0"].TotalAmount;
}else if(volume_discount_corn.length > 0 && is_Calculation_Include==true){
var volDiscVal = (cornTotal * volume_discount_corn["0"].AverageRate)/100;
var volDisc = Number(volume_discount_corn["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["df51-4f26-6f8b"].set("value", volDiscVal);
formyoula.form_fields["4034-37d1-7e4d"].set("value", volDisc);
cornTotalDiscount = cornTotalDiscount + volDiscVal;
}else{
formyoula.form_fields["df51-4f26-6f8b"].set("value", 0).trigger("change");
formyoula.form_fields["4034-37d1-7e4d"].set("value", 0).trigger("change");
}
var volume_discount_soy = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'Volume Discount' && e.giic_Crop__c == '3C'); });
if(volume_discount_soy.length > 0 && is_Calculation_Include==false){
var volDisc = Number(volume_discount_soy["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["2e72-a051-f0ae"].set("value", volume_discount_soy["0"].TotalAmount);
formyoula.form_fields["bbce-286a-c5e1"].set("value", volDisc);
soyaTotalDiscount = soyaTotalDiscount + volume_discount_soy["0"].TotalAmount;
}else if(volume_discount_soy.length > 0 && is_Calculation_Include==true){
var volDiscVal = (soyaTotal * volume_discount_soy["0"].AverageRate)/100;
var volDisc = Number(volume_discount_soy["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["2e72-a051-f0ae"].set("value", volDiscVal);
formyoula.form_fields["bbce-286a-c5e1"].set("value", volDisc);
soyaTotalDiscount = soyaTotalDiscount + volDiscVal;
}else{
formyoula.form_fields["2e72-a051-f0ae"].set("value", 0).trigger("change");
formyoula.form_fields["bbce-286a-c5e1"].set("value", 0).trigger("change");
}
var volumeDiscountTotal = formyoula.form_fields["df51-4f26-6f8b"].get("value") + formyoula.form_fields["2e72-a051-f0ae"].get("value");
var volumeDiscountRate = Number((volumeDiscountTotal * 100)/totalpurchase).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["1d29-9ca8-79b1"].set("value",volumeDiscountTotal);
formyoula.form_fields["7588-de4d-f5f8"].set("value",volumeDiscountRate);
totalDiscount = totalDiscount + volumeDiscountTotal;
//Set Marketing - Competitive Allowance - Corn
var competitive_allowance_corn = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'Competitive Allowance' && e.giic_Crop__c == '01'); });
if(competitive_allowance_corn.length > 0){
formyoula.form_fields["b2d7-1b09-8caa"].set("value", competitive_allowance_corn["0"].TotalAmount);
var cornRate = Number((competitive_allowance_corn["0"].TotalAmount * 100)/cornTotal).toLocaleString(undefined,{ minimumFractionDigits: 2 } );;
formyoula.form_fields["e515-05b5-502a"].set("value", cornRate);
cornTotalDiscount = cornTotalDiscount + competitive_allowance_corn["0"].TotalAmount;
}else{
formyoula.form_fields["b2d7-1b09-8caa"].set("value", 0);
formyoula.form_fields["e515-05b5-502a"].set("value", 0);
}
//Set Marketing - Competitive Allowance - SoyBean
var competitive_allowance_soy = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'Competitive Allowance' && e.giic_Crop__c == '3C'); });
if(competitive_allowance_soy.length > 0){
formyoula.form_fields["6870-d666-580b"].set("value", competitive_allowance_soy["0"].TotalAmount);
var soyaRate = Number((competitive_allowance_soy["0"].TotalAmount * 100)/soyaTotal).toLocaleString(undefined,{ minimumFractionDigits: 2 } );;
formyoula.form_fields["cb90-350f-db18"].set("value", soyaRate);
soyaTotalDiscount = soyaTotalDiscount + competitive_allowance_soy["0"].TotalAmount;
}else{
formyoula.form_fields["6870-d666-580b"].set("value", 0);
formyoula.form_fields["cb90-350f-db18"].set("value", 0);
}
var compTotal = formyoula.form_fields["b2d7-1b09-8caa"].get("value") + formyoula.form_fields["6870-d666-580b"].get("value");
var compDiscountRate = Number((compTotal * 100)/totalpurchase).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["a119-7c9e-6049"].set("value",compTotal);
formyoula.form_fields["6fb5-5ee7-78f0"].set("value",compDiscountRate);
totalDiscount = totalDiscount + compTotal;
//Set Gorwing Grower Discount
var growing_grower_discount_corn = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'Growing Grower Discount' && e.giic_Crop__c == '01'); });
if(growing_grower_discount_corn.length > 0 && is_Calculation_Include==false){
var growerDisc = Number(growing_grower_discount_corn["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["6141-4117-3d50"].set("value", growing_grower_discount_corn["0"].TotalAmount);
formyoula.form_fields["8015-3ed0-6454"].set("value", growerDisc);
cornTotalDiscount = cornTotalDiscount + growing_grower_discount_corn["0"].TotalAmount;
}else if(growing_grower_discount_corn.length > 0 && is_Calculation_Include==true){
var growerVal = (cornTotal * growing_grower_discount_corn["0"].AverageRate)/100;
var growerDisc = Number(growing_grower_discount_corn["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["6141-4117-3d50"].set("value", growerVal);
formyoula.form_fields["8015-3ed0-6454"].set("value", growerDisc);
cornTotalDiscount = cornTotalDiscount + growerVal;
}else{
formyoula.form_fields["6141-4117-3d50"].set("value", 0).trigger("change");
formyoula.form_fields["8015-3ed0-6454"].set("value", 0).trigger("change");
}
var growing_grower_discount_soy = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'Growing Grower Discount' && e.giic_Crop__c == '3C'); });
if(growing_grower_discount_soy.length > 0 && is_Calculation_Include==false){
var growerDisc = Number(growing_grower_discount_soy["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["a293-ca4d-812b"].set("value", growing_grower_discount_soy["0"].TotalAmount);
formyoula.form_fields["bf4d-f048-27b4"].set("value", growerDisc);
soyaTotalDiscount = soyaTotalDiscount + growing_grower_discount_soy["0"].TotalAmount;
}else if(growing_grower_discount_soy.length > 0 && is_Calculation_Include==true){
var growerVal = (soyaTotal * growing_grower_discount_soy["0"].AverageRate)/100;
var growerDisc = Number(growing_grower_discount_soy["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["a293-ca4d-812b"].set("value", growerVal);
formyoula.form_fields["bf4d-f048-27b4"].set("value", growerDisc);
soyaTotalDiscount = soyaTotalDiscount + growerVal;
}else{
formyoula.form_fields["a293-ca4d-812b"].set("value", 0).trigger("change");
formyoula.form_fields["bf4d-f048-27b4"].set("value", 0).trigger("change");
}
var growerDiscountTotal = formyoula.form_fields["6141-4117-3d50"].get("value") + formyoula.form_fields["a293-ca4d-812b"].get("value");
var growerDiscountRate = Number((growerDiscountTotal * 100)/totalpurchase).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["00af-daa0-bf0b"].set("value",growerDiscountTotal);
formyoula.form_fields["bc3d-1fc2-174d"].set("value",growerDiscountRate);
totalDiscount = totalDiscount + growerDiscountTotal;
//Set New Grower Discount
var new_grower_discount_corn = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'New Grower Discount' && e.giic_Crop__c == '01'); });
if(new_grower_discount_corn.length > 0 && is_Calculation_Include==false){
formyoula.form_fields["214c-0c53-a38d"].set("value", new_grower_discount_corn["0"].TotalAmount);
formyoula.form_fields["9b78-cde9-18a9"].set("value", Number(new_grower_discount_corn["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
cornTotalDiscount = cornTotalDiscount + new_grower_discount_corn["0"].TotalAmount;
}else if(new_grower_discount_corn.length > 0 && is_Calculation_Include==true){
var newGrTotal = (cornTotal * new_grower_discount_corn["0"].AverageRate)/100;
formyoula.form_fields["214c-0c53-a38d"].set("value", newGrTotal);
formyoula.form_fields["9b78-cde9-18a9"].set("value", Number(new_grower_discount_corn["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
cornTotalDiscount = cornTotalDiscount + newGrTotal;
}else{
formyoula.form_fields["214c-0c53-a38d"].set("value", 0).trigger("change");
formyoula.form_fields["9b78-cde9-18a9"].set("value", 0);
}
var new_grower_discount_soy = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'New Grower Discount' && e.giic_Crop__c == '3C'); });
if(new_grower_discount_soy.length > 0 && is_Calculation_Include==false){
formyoula.form_fields["97d7-2ffd-bba3"].set("value", new_grower_discount_soy["0"].TotalAmount);
formyoula.form_fields["21ae-8712-b3f4"].set("value", new_grower_discount_soy["0"].AverageRate);
soyaTotalDiscount = soyaTotalDiscount + new_grower_discount_soy["0"].TotalAmount;
}else if(new_grower_discount_soy.length > 0 && is_Calculation_Include==true){
var newGrTotal = (soyaTotal * new_grower_discount_soy["0"].AverageRate)/100;
formyoula.form_fields["97d7-2ffd-bba3"].set("value", newGrTotal);
formyoula.form_fields["21ae-8712-b3f4"].set("value", Number(new_grower_discount_soy["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
soyaTotalDiscount = soyaTotalDiscount + newGrTotal;
}else{
formyoula.form_fields["97d7-2ffd-bba3"].set("value", 0).trigger("change");
formyoula.form_fields["21ae-8712-b3f4"].set("value", 0);
}
var newGrDiscTotal = formyoula.form_fields["214c-0c53-a38d"].get("value") + formyoula.form_fields["97d7-2ffd-bba3"].get("value");
var newGrDiscRate = Number((newGrDiscTotal * 100)/totalpurchase).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["3c50-5443-26fe"].set("value",newGrDiscTotal);
formyoula.form_fields["cbbf-e5ff-e514"].set("value",newGrDiscRate);
totalDiscount = totalDiscount + newGrDiscTotal;
//Set Returning Grower Discount
var returning_grower_discount_corn = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'New Grower Discount' && e.giic_Crop__c == '01'); });
if(returning_grower_discount_corn.length > 0 && is_Calculation_Include==false){
formyoula.form_fields["ec07-58e4-35de"].set("value", returning_grower_discount_corn["0"].TotalAmount);
formyoula.form_fields["97d9-e326-6e6d"].set("value", Number(returning_grower_discount_corn["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
cornTotalDiscount = cornTotalDiscount + returning_grower_discount_corn["0"].TotalAmount;
}else if(returning_grower_discount_corn.length > 0 && is_Calculation_Include==true){
var returnGrTotal = (cornTotal * returning_grower_discount_corn["0"].AverageRate)/100;
formyoula.form_fields["ec07-58e4-35de"].set("value", returnGrTotal);
formyoula.form_fields["97d9-e326-6e6d"].set("value", Number(returning_grower_discount_corn["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
cornTotalDiscount = cornTotalDiscount + returnGrTotal;
}else{
formyoula.form_fields["ec07-58e4-35de"].set("value", 0).trigger("change");
formyoula.form_fields["97d9-e326-6e6d"].set("value", 0);
}
var returning_grower_discount_soy = $.grep(current_marketing_totals, function(e){ return (e.giic_PricingDescription__c == 'New Grower Discount' && e.giic_Crop__c == '3C'); });
if(returning_grower_discount_soy.length > 0 && is_Calculation_Include==false){
formyoula.form_fields["7e2d-93fe-ff92"].set("value", returning_grower_discount_soy["0"].TotalAmount);
formyoula.form_fields["8fda-7887-f113"].set("value", Number(returning_grower_discount_soy["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
soyaTotalDiscount = soyaTotalDiscount + returning_grower_discount_soy["0"].TotalAmount;
}else if(returning_grower_discount_soy.length > 0 && is_Calculation_Include==true){
var returnGrTotal = (soyaTotal * returning_grower_discount_soy["0"].AverageRate)/100;
formyoula.form_fields["7e2d-93fe-ff92"].set("value", returnGrTotal);
formyoula.form_fields["8fda-7887-f113"].set("value", Number(returning_grower_discount_soy["0"].AverageRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
soyaTotalDiscount = soyaTotalDiscount + returnGrTotal;
}else{
formyoula.form_fields["7e2d-93fe-ff92"].set("value", 0).trigger("change");
formyoula.form_fields["8fda-7887-f113"].set("value", 0);
}
var returnGrDiscTotal = formyoula.form_fields["ec07-58e4-35de"].get("value") + formyoula.form_fields["7e2d-93fe-ff92"].get("value");
var returnGrDiscRate = Number((returnGrDiscTotal * 100)/totalpurchase).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["3c50-5443-26fe"].set("value",returnGrDiscTotal);
formyoula.form_fields["8ea6-9e25-c3d9"].set("value",returnGrDiscRate);
totalDiscount = totalDiscount + returnGrDiscTotal;
console.log('marketing discount total :'+cornTotalDiscount+'..'+soyaTotalDiscount +'...'+totalDiscount);
var finalCornTotalDiscount = cornTotal - cornTotalDiscount;
var finalSoyaTotalDiscount = soyaTotal - soyaTotalDiscount;
var finalTotalPurchase = totalpurchase - totalDiscount;
formyoula.form_fields["206a-7610-9276"].set("value",finalCornTotalDiscount);
formyoula.form_fields["e480-e257-5e4b"].set("value",finalSoyaTotalDiscount);
formyoula.form_fields["4f44-92e7-2ac6"].set("value",finalTotalPurchase);
var finalCornRate = (cornTotalDiscount*100)/cornTotal;
var finalSoyaRate = (soyaTotalDiscount*100)/soyaTotal;
var finalDiscRate = (totalDiscount*100)/totalpurchase;
formyoula.form_fields["db87-da5e-b62f"].set("value", Number(finalCornRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
formyoula.form_fields["7950-d636-85b7"].set("value", Number(finalSoyaRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
formyoula.form_fields["5660-19cf-e23d"].set("value", Number(finalDiscRate).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
//Set Marketing - Early Pay discount
formyoula.form_fields["dc6c-0872-e45b"].set("value", 0);
var early_pay_discount = $.grep(current_marketing_totals, function(e){ return e.giic_PricingDescription__c == 'Early Pay Discount'; });
//console.log(early_pay_discount);
var ep_discount_discription = '';
formyoula.form_fields["e5e5-04d1-ba77"].set("value", "");
if(early_pay_discount.length > 0){
formyoula.form_fields["dc6c-0872-e45b"].set("value", early_pay_discount["0"].TotalAmount);
if(current_marketing_early_pay.length > 0){
if(current_marketing_early_pay["0"].giic_EP_Qualified_Discounts__c){
var data = jQuery.parseJSON(current_marketing_early_pay["0"].giic_EP_Qualified_Discounts__c);
var ep_discount_discription = '';
$.each(data, function(key, val) {
var disc = Number(val.DiscountPercent).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var depositAmount = Number(val.DepositAmt).toLocaleString('en-US',{ currency: 'USD', style:'currency' } );
var calDiscAmt = (val.DepositAmt * val.DiscountPercent)/100;
var discAmount = Number(val.DiscountAmount).toLocaleString('en-US',{ currency: 'USD', style:'currency' } );
if (val.Type == 'Cash') {
ep_discount_discription = ep_discount_discription + 'EP Cash discount ' + disc + '% applied for payment ' + depositAmount + ' amounting ' + discAmount + '<br/>&nbsp;';
}
else if (val.Type == 'Fin') {
ep_discount_discription = ep_discount_discription + 'EP Finance discount ' + disc + '% applied for payment ' + depositAmount + ' amounting ' + discAmount + '<br/>&nbsp;';
}
else {
ep_discount_discription = ep_discount_discription + 'EP Credit discount ' + disc + '% applied for payment ' + depositAmount + ' amounting ' + discAmount;
}
});
formyoula.form_fields["e5e5-04d1-ba77"].set("value", ep_discount_discription);
}
}else{
formyoula.form_fields["e5e5-04d1-ba77"].set("value", "");
}
}
/*Load SFDC table element for Corn SOLI*/
//Variable to get all corn based Free of Charge SOLI records
formyoula.form_fields["fbd2-8046-4700"].set("value", 0);
formyoula.form_fields["8810-8873-c4f7"].set("value", 0);
if(free_of_charge_orders){
var FOC_sales_order_lineitems_corn = $.grep(free_of_charge_orders.gii__SalesOrder__r.records, function(e){ return e.gii__Product__r.gii__ProductReference__r.Crop_Group__c === 'COR: CORN'; });
formyoula.form_fields["0006-ae7e-65ed"].set({ "value": "" }, { silent: true }); // corn SOLI
formyoula.form_fields["fbd2-8046-4700"].set("value", 0);
if(FOC_sales_order_lineitems_corn.length > 0){
formyoula.form_fields["0006-ae7e-65ed"].set({"value": FOC_sales_order_lineitems_corn}, { silent: true }).trigger("change");
formyoula.form_fields["fbd2-8046-4700"].set("value", FOC_sales_order_lineitems_corn.map(o=>o.giic_NetPositionProductAmount__c).reduce((a,c)=>a+c));
}
/*Load SFDC table element for Soybean SOLI*/
//Variable to get all Soybean based Free of Charge SOLI records
var FOC_sales_order_lineitems_soybean = $.grep(free_of_charge_orders.gii__SalesOrder__r.records, function(e){ return e.gii__Product__r.gii__ProductReference__r.Crop_Group__c === 'SYB: SOYBEAN'; });
//console.log(sales_order_lineitems_soybean);
formyoula.form_fields["6b03-cdd4-9231"].set({ "value": "" }, { silent: true }); // corn SOLI
formyoula.form_fields["8810-8873-c4f7"].set("value", 0);
if(FOC_sales_order_lineitems_soybean.length > 0){
formyoula.form_fields["6b03-cdd4-9231"].set({"value": FOC_sales_order_lineitems_soybean}, { silent: true }).trigger("change");
formyoula.form_fields["8810-8873-c4f7"].set("value", FOC_sales_order_lineitems_soybean.map(o=>o.giic_NetPositionProductAmount__c).reduce((a,c)=>a+c));
}
}
//Early Pay Section
var ep_discription = '';
formyoula.form_fields["e453-01d9-ed25"].set("value","");
//console.log('EP_Details.length::'+EP_Details.length);
var cashCount = 0;
var finCount = 0;
var paymentEP =0;
//if(EP_Details.length > 0){
// if(EP_Details["0"].giic_EP_Qualified_Discounts__c){
//var data = jQuery.parseJSON(EP_Details["0"].giic_EP_Qualified_Discounts__c);
var today = new Date();
var currentDate = today.getFullYear()+'-'+("0"+(today.getMonth()+1)).slice(-2) +'-'+("0" + today.getDate()).slice(-2);
//var currentDate = "2019-01-01";
console.log('date:'+currentDate);
var queryStr = [];
var tierDate = [];
var promotype = [];
var discPer = [];
var cashBal = 0;
var FinBal = 0;
// $.each(data, function(key, val) {
//promotype[key] = val.Type == 'Cash'? 'Early Pay - Cash Payment' : 'Early Pay - Finance/Credit';
if((EP_Promotion_Details["0"].Name == 'Early Pay - Cash Payment' && cashCount ==0) && (EP_Promotion_Details["1"].Name == 'Early Pay - Finance/Credit' && finCount ==0)){
if(currentDate <= EP_Promotion_Details["0"].giic_Tier_1_Display_Date__c){
var discCP = EP_Promotion_Details["0"].giic_Tier1DiscountPercent__c/100;
var discAmtCP = (cashBal/(1-discCP)) * discCP;
var amtCP = Number(discAmtCP).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discFC = EP_Promotion_Details["1"].giic_Tier1DiscountPercent__c/100;
var discAmtFC = (FinBal/(1-discFC)) * discFC;
var amtFC = Number(discAmtFC).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discAmt = discAmtCP + discAmtFC;
paymentEP = paymentEP+discAmt;
var dAmt = Number(discAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
ep_discription = ep_discription + "<tr><td>&nbsp;"+EP_Promotion_Details["0"].giic_Tier_1_Display_Date__c+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["0"].giic_Tier1DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtCP+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["1"].giic_Tier1DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtFC+"<td style=\"text-align:center;\">$"+dAmt+"</td></tr>";
console.log('desc:Tier_1::'+ep_discription);
}
if(currentDate <= EP_Promotion_Details["0"].giic_Tier_2_Display_Date__c){
var discCP = EP_Promotion_Details["0"].giic_Tier2DiscountPercent__c/100;
var discAmtCP = (cashBal/(1-discCP)) * discCP;
var amtCP = Number(discAmtCP).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discFC = EP_Promotion_Details["1"].giic_Tier2DiscountPercent__c/100;
var discAmtFC = (FinBal/(1-discFC)) * discFC;
var amtFC = Number(discAmtFC).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discAmt = discAmtCP + discAmtFC;
paymentEP = paymentEP+discAmt;
var dAmt = Number(discAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
ep_discription = ep_discription + "<tr><td>&nbsp;"+EP_Promotion_Details["0"].giic_Tier_2_Display_Date__c+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["0"].giic_Tier2DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtCP+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["1"].giic_Tier2DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtFC+"<td style=\"text-align:center;\">$"+dAmt+"</td></tr>";
console.log('desc:Tier_2::'+ep_discription);
}
if(currentDate <= EP_Promotion_Details["0"].giic_Tier_3_Display_Date__c){
var discCP = EP_Promotion_Details["0"].giic_Tier3DiscountPercent__c/100;
var discAmtCP = (cashBal/(1-discCP)) * discCP;
var amtCP = Number(discAmtCP).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discFC = EP_Promotion_Details["1"].giic_Tier3DiscountPercent__c/100;
var discAmtFC = (FinBal/(1-discFC)) * discFC;
var amtFC = Number(discAmtFC).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discAmt = discAmtCP + discAmtFC;
paymentEP = paymentEP+discAmt;
var dAmt = Number(discAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
ep_discription = ep_discription + "<tr><td>&nbsp;"+EP_Promotion_Details["0"].giic_Tier_3_Display_Date__c+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["0"].giic_Tier3DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtCP+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["1"].giic_Tier3DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtFC+"<td style=\"text-align:center;\">$"+dAmt+"</td></tr>";
console.log('desc:Tier_3::'+ep_discription);
}
if(currentDate <= EP_Promotion_Details["0"].giic_Tier_4_Display_Date__c){
var discCP = EP_Promotion_Details["0"].giic_Tier4DiscountPercent__c/100;
var discAmtCP = (cashBal/(1-discCP)) * discCP;
var amtCP = Number(discAmtCP).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discFC = EP_Promotion_Details["1"].giic_Tier4DiscountPercent__c/100;
var discAmtFC = (FinBal/(1-discFC)) * discFC;
var amtFC = Number(discAmtFC).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discAmt = discAmtCP + discAmtFC;
paymentEP = paymentEP+discAmt;
var dAmt = Number(discAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
ep_discription = ep_discription + "<tr><td>&nbsp;"+EP_Promotion_Details["0"].giic_Tier_4_Display_Date__c+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["0"].giic_Tier4DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtCP+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["1"].giic_Tier4DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtFC+"<td style=\"text-align:center;\">$"+dAmt+"</td></tr>";
console.log('desc:Tier_4::'+ep_discription);
}
if(currentDate <= EP_Promotion_Details["0"].giic_Tier_5_Display_Date__c){
var discCP = EP_Promotion_Details["0"].giic_Tier5DiscountPercent__c/100;
var discAmtCP = (cashBal/(1-discCP)) * discCP;
var amtCP = Number(discAmtCP).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discFC = EP_Promotion_Details["1"].giic_Tier5DiscountPercent__c/100;
var discAmtFC = (FinBal/(1-discFC)) * discFC;
var amtFC = Number(discAmtFC).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discAmt = discAmtCP + discAmtFC;
paymentEP = paymentEP+discAmt;
var dAmt = Number(discAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
ep_discription = ep_discription + "<tr><td>&nbsp;"+EP_Promotion_Details["0"].giic_Tier_5_Display_Date__c+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["0"].giic_Tier5DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtCP+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["1"].giic_Tier5DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtFC+"<td style=\"text-align:center;\">$"+dAmt+"</td></tr>";
console.log('desc:Tier_5::'+ep_discription);
}
if(currentDate <= EP_Promotion_Details["0"].giic_Tier_6_Display_Date__c){
var discCP = EP_Promotion_Details["0"].giic_Tier6DiscountPercent__c/100;
var discAmtCP = (cashBal/(1-discCP)) * discCP;
var amtCP = Number(discAmtCP).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discFC = EP_Promotion_Details["1"].giic_Tier6DiscountPercent__c/100;
var discAmtFC = (FinBal/(1-discFC)) * discFC;
var amtFC = Number(discAmtFC).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discAmt = discAmtCP + discAmtFC;
paymentEP = paymentEP+discAmt;
var dAmt = Number(discAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
ep_discription = ep_discription + "<tr><td>&nbsp;"+EP_Promotion_Details["0"].giic_Tier_6_Display_Date__c+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["0"].giic_Tier6DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtCP+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["1"].giic_Tier6DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtFC+"<td style=\"text-align:center;\">$"+dAmt+"</td></tr>";
console.log('desc:Tier_6::'+ep_discription);
}
if(currentDate <= EP_Promotion_Details["0"].giic_Tier_7_Display_Date__c){
var discCP = EP_Promotion_Details["0"].giic_Tier7DiscountPercent__c/100;
var discAmtCP = (cashBal/(1-discCP)) * discCP;
var amtCP = Number(discAmtCP).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discFC = EP_Promotion_Details["1"].giic_Tier7DiscountPercent__c/100;
var discAmtFC = (FinBal/(1-discFC)) * discFC;
var amtFC = Number(discAmtFC).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discAmt = discAmtCP + discAmtFC;
paymentEP = paymentEP+discAmt;
var dAmt = Number(discAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
ep_discription = ep_discription + "<tr><td>&nbsp;"+EP_Promotion_Details["0"].giic_Tier_7_Display_Date__c+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["0"].giic_Tier7DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtCP+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["1"].giic_Tier7DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtFC+"<td style=\"text-align:center;\">$"+dAmt+"</td></tr>";
console.log('desc:Tier_7::'+ep_discription);
}
if(currentDate <= EP_Promotion_Details["0"].giic_Tier_8_Display_Date__c){
var discCP = EP_Promotion_Details["0"].giic_Tier8DiscountPercent__c/100;
var discAmtCP = (cashBal/(1-discCP)) * discCP;
var amtCP = Number(discAmtCP).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discFC = EP_Promotion_Details["1"].giic_Tier8DiscountPercent__c/100;
var discAmtFC = (FinBal/(1-discFC)) * discFC;
var amtFC = Number(discAmtFC).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
var discAmt = discAmtCP + discAmtFC;
paymentEP = paymentEP+discAmt;
var dAmt = Number(discAmt).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
ep_discription = ep_discription + "<tr><td>&nbsp;"+EP_Promotion_Details["0"].giic_Tier_8_Display_Date__c+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["0"].giic_Tier8DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtCP+"</td><td style=\"text-align:center;\">"+EP_Promotion_Details["1"].giic_Tier8DiscountPercent__c+"%</td><td style=\"text-align:center;\">$"+amtFC+"<td style=\"text-align:center;\">$"+dAmt+"</td></tr>";
console.log('desc:Tier_8::'+ep_discription);
}
cashCount = cashCount + 1;
finCount = finCount + 1;
}
//console.log('Ep_description::'+ep_discription);
//console.log('payment :'+paymentEP);
//});
//}
formyoula.form_fields["e453-01d9-ed25"].set("value",ep_discription);
formyoula.form_fields["6af3-0135-9384"].set("value",paymentEP);
//}else{
//formyoula.form_fields["e453-01d9-ed25"].set("value","");
//}
var EPWeighted = (paymentEP*100)/finalTotalPurchase;
formyoula.form_fields["ce8a-a41c-5d7f"].set("value",Number(EPWeighted).toLocaleString(undefined,{ minimumFractionDigits: 2 } ));
// Set subtotal and estimated balance
formyoula.form_fields["c324-46c6-a3ed"].set("value",totalpurchase); // Gross order total
var replantDisc = formyoula.form_fields["a9c3-24c7-c220"].get("value"); //Replant Line discount
var replantPurchaseVal = formyoula.form_fields["1d1f-0825-23ed"].get("value"); //Replant Purchase Total
var marketingDiscount = totalDiscount + paymentEP + replantDisc - replantPurchaseVal;
formyoula.form_fields["de8b-f90e-5b1f"].set("value", marketingDiscount); // Discount value
var subTotal = totalpurchase - marketingDiscount;
console.log('subtotal :'+subTotal);
var stotal = subTotal>=0 ? '$'+Number(subTotal).toLocaleString(undefined,{ minimumFractionDigits: 2 } ) : '($'+Number(Math.abs(subTotal)).toLocaleString(undefined,{ minimumFractionDigits: 2 } )+')';
console.log('stotal::'+stotal);
formyoula.form_fields["d4c5-d94b-171a"].set("value", stotal);
var paymentDisc = formyoula.form_fields["6af3-0135-9384"].get("value"); // Payment
var balance = subTotal - paymentDisc;
var stotal = balance>=0 ? '$'+Number(balance).toLocaleString(undefined,{ minimumFractionDigits: 2 } ) : '($'+Number(Math.abs(balance)).toLocaleString(undefined,{ minimumFractionDigits: 2 } )+')';
formyoula.form_fields["bb89-188c-704f"].set("value", stotal);
//var discretionary = formyoula.form_fields["46dc-fdb7-c847"].get("value");
var Estimated_Balance = balance + TotalAdditionalFee - finalAmountCorn;
console.log('Estimated_Balance:'+Estimated_Balance);
var Ebal = "";
if(Estimated_Balance>=0){
Ebal = '$' + Number(Estimated_Balance).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
formyoula.form_fields["8729-3688-8e2e"].set("value",Ebal);
}else{
var absVal = Math.abs(Estimated_Balance);
var valAB = Number(absVal).toLocaleString(undefined,{ minimumFractionDigits: 2 } );
Ebal = '($'+ valAB +')';
formyoula.form_fields["8729-3688-8e2e"].set("value",Ebal);
}
//console.log(formyoula.get_pdf_html());
genrate_pdf(i);
//console.log('After all pre-fill done!!!!: ', i);
}else{
console.log(all_pdf_data,"all_pdf_data");
var zip = new JSZip();
var zip_file_name = window.formyoula.form_fields["a10c-a47f-5d2c"].get("value");
var pdf = zip.folder(zip_file_name + '_PDFs');
//ZIP implementation
for (var pdf_index = 0; pdf_index < all_pdf_data.length; pdf_index++) {
//Get PDF Name via ke
console.log(all_pdf_data[pdf_index])
var pdf_name = Object.keys(all_pdf_data[pdf_index]);
//Add file to pdf object
pdf.file(pdf_name, all_pdf_data[pdf_index][pdf_name].split('base64,')[1], { base64: true });
}
//Zip generate
zip.generateAsync({ type: "blob" })
.then(function(content) {
// see FileSaver.js
saveAs(content, zip_file_name + '_Statements.zip');
formyoula.event_trigger("main:spinner_dialog", { show: false });
var realConfirm=window.confirm;
window.confirm=function(){
window.confirm=realConfirm;
return true;
};
setTimeout(function(){
$(".cancel").click();
window.close();
},4000);
});
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment