From c0eb1a92fb89bf2aca40ffa921c9d7565116dd61 Mon Sep 17 00:00:00 2001 From: olayiwola-compucorp Date: Tue, 14 May 2024 13:04:44 +0100 Subject: [PATCH 1/3] BTHAMM-15: Add custom delay before updating line item total --- js/sales-order-contribution.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/js/sales-order-contribution.js b/js/sales-order-contribution.js index 5de753872..4fe8418fd 100644 --- a/js/sales-order-contribution.js +++ b/js/sales-order-contribution.js @@ -1,13 +1,15 @@ (function ($, _) { const waitForElement = function ($, elementPath, callBack) { - (new window.MutationObserver(function () { - callBack($, $(elementPath)); - })).observe(document.querySelector(elementPath), { - attributes: true - }); + window.setTimeout(function () { + if ($(elementPath).length) { + callBack($, $(elementPath)); + } else { + window.waitForElement($, elementPath, callBack); + } + }, 500); }; - $(document).one('crmLoad', function () { + $(document).one('crmLoad', async function () { const params = CRM.vars['uk.co.compucorp.civicase']; const salesOrderId = params.sales_order; const salesOrderStatusId = params.sales_order_status_id; @@ -29,6 +31,9 @@ }]; if (Array.isArray(lineItems)) { + CRM.$.blockUI(); + CRM.$('form#Contribution').css('visibility', 'hidden'); + await new Promise(resolve => setTimeout(resolve, 2000)); CRM.api4(apiRequest).then(function (batch) { const caseSalesOrder = batch.caseSalesOrders[0]; @@ -37,7 +42,6 @@ }); $('#total_amount').val(0); - $('#lineitem-add-block').show().removeClass('hiddenElement'); $('#contribution_status_id').val(batch.optionValues[0].value); $('#source').val(`Quotation ${caseSalesOrder.id}`).trigger('change'); $('#contact_id').select2('val', caseSalesOrder.client_id).trigger('change'); @@ -52,6 +56,9 @@ $(`[name^=${caseCustomField}_]`).val(caseSalesOrder.case_id).trigger('change'); $(`[name^=${quotationCustomField}_]`).val(caseSalesOrder.id).trigger('change'); }); + }).finally(() => { + CRM.$.unblockUI(); + CRM.$('form#Contribution').css('visibility', 'visible'); }); } @@ -73,10 +80,9 @@ const total = quantity * parseFloat(unitPrice); - $('input[id^="item_unit_price"]', row).val(unitPrice); $('input[id^="item_line_total"]', row).val(CRM.formatMoney(total, true)); - $('input[id^="item_tax_amount"]', row).val(taxAmount); + $('input[id^="item_unit_price"]', row).val(unitPrice).trigger('change'); count++; } From fbbac39d08db3b73f25832e0c172dc669a2e47ed Mon Sep 17 00:00:00 2001 From: olayiwola-compucorp Date: Mon, 27 May 2024 06:38:12 +0100 Subject: [PATCH 2/3] BTHAMM-15: Resolve incorrect total due to round off --- CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php b/CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php index 63379b52d..70248d17c 100644 --- a/CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php +++ b/CRM/Civicase/Service/CaseSalesOrderLineItemsGenerator.php @@ -147,7 +147,7 @@ private function lineItemToContributionLineItem(array $item) { 'entity_table' => 'civicrm_contribution', 'financial_type_id' => $item['financial_type_id'], 'line_total' => round($item['total'], 2), - 'unit_price' => round($item['unit_price'], 2), + 'unit_price' => $item['unit_price'], ]; } From e10157d5b2c8214108ee25b1d8d0c33cbcc74402 Mon Sep 17 00:00:00 2001 From: olayiwola-compucorp Date: Mon, 27 May 2024 06:52:36 +0100 Subject: [PATCH 3/3] BTHAMM-15: Add delay between lineitem population --- js/sales-order-contribution.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/js/sales-order-contribution.js b/js/sales-order-contribution.js index 4fe8418fd..9167ae2f3 100644 --- a/js/sales-order-contribution.js +++ b/js/sales-order-contribution.js @@ -33,24 +33,26 @@ if (Array.isArray(lineItems)) { CRM.$.blockUI(); CRM.$('form#Contribution').css('visibility', 'hidden'); - await new Promise(resolve => setTimeout(resolve, 2000)); + await new Promise(resolve => setTimeout(resolve, 1000)); CRM.api4(apiRequest).then(function (batch) { const caseSalesOrder = batch.caseSalesOrders[0]; lineItems.forEach(lineItem => { - addLineItem(lineItem.qty, lineItem.unit_price, lineItem.label, lineItem.financial_type_id, lineItem.tax_amount); + setTimeout( + () => addLineItem(lineItem.qty, lineItem.unit_price, lineItem.label, lineItem.financial_type_id, lineItem.tax_amount), + 2000 + ); }); - $('#total_amount').val(0); + $('input[id="total_amount"]').trigger('change'); $('#contribution_status_id').val(batch.optionValues[0].value); $('#source').val(`Quotation ${caseSalesOrder.id}`).trigger('change'); $('#contact_id').select2('val', caseSalesOrder.client_id).trigger('change'); - $('input[id="total_amount"]', 'form.CRM_Contribute_Form_Contribution').trigger('change'); $(``).insertBefore('#source'); $(``).insertBefore('#source'); $(``).insertBefore('#source'); $(``).insertBefore('#source'); - $('#totalAmount, #totalAmountORaddLineitem, #totalAmountORPriceSet, #price_set_id, #choose-manual').hide(); + $(' #totalAmountORaddLineitem, #totalAmountORPriceSet, #price_set_id, #choose-manual').hide(); waitForElement($, '#customData', function ($, elem) { $(`[name^=${caseCustomField}_]`).val(caseSalesOrder.case_id).trigger('change');