From 818b261ff38e293470bc713a070d5a0e12f06814 Mon Sep 17 00:00:00 2001 From: Karen Attfield Date: Fri, 9 Feb 2024 17:13:16 +0000 Subject: [PATCH] Revert "CRM: Making sure invoice total count factors in credit notes and refunds correctly in invoice editor, preview and pdf view" (#35569) --- ...-34211-fix-crm-invoice-total-credit-refund | 4 ++ .../includes/ZeroBSCRM.DAL3.Obj.Invoices.php | 66 ++++++++++--------- .../crm/includes/ZeroBSCRM.InvoiceBuilder.php | 53 +++++++-------- .../crm/js/ZeroBSCRM.admin.invoicebuilder.js | 13 ++-- 4 files changed, 69 insertions(+), 67 deletions(-) create mode 100644 projects/plugins/crm/changelog/revert-34211-fix-crm-invoice-total-credit-refund diff --git a/projects/plugins/crm/changelog/revert-34211-fix-crm-invoice-total-credit-refund b/projects/plugins/crm/changelog/revert-34211-fix-crm-invoice-total-credit-refund new file mode 100644 index 0000000000000..08b0593520925 --- /dev/null +++ b/projects/plugins/crm/changelog/revert-34211-fix-crm-invoice-total-credit-refund @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Invoices: Changing total amount calculation in preview and pdf when refunds or credit notes are applied, back to pre 6.4.0 implementation. diff --git a/projects/plugins/crm/includes/ZeroBSCRM.DAL3.Obj.Invoices.php b/projects/plugins/crm/includes/ZeroBSCRM.DAL3.Obj.Invoices.php index b21784bd2a7d9..ed00153274981 100644 --- a/projects/plugins/crm/includes/ZeroBSCRM.DAL3.Obj.Invoices.php +++ b/projects/plugins/crm/includes/ZeroBSCRM.DAL3.Obj.Invoices.php @@ -2752,52 +2752,58 @@ public function getOutstandingBalance($invoiceID=-1){ if (is_array($invoice)){ // get total due - $invoiceTotalValue = 0.0; if (isset($invoice['total'])) $invoiceTotalValue = (float)$invoice['total']; + $invoice_total_value = 0.0; + if ( isset( $invoice['total'] ) ) { + $invoice_total_value = (float) $invoice['total']; + // this one'll be a rolling sum + $transactions_total_value = 0.0; - // this one'll be a rolling sum - $transactions_total_value = 0.0; + // cycle through trans + calc existing balance + if ( isset( $invoice['transactions'] ) && is_array( $invoice['transactions'] ) ) { - // cycle through trans + calc existing balance - if ( isset( $invoice['transactions'] ) && is_array( $invoice['transactions'] ) ) { + // got trans + foreach ( $invoice['transactions'] as $transaction ) { - // got trans - foreach ( $invoice['transactions'] as $transaction ) { + // should we also check for status=completed/succeeded? (leaving for now, will let check all): - // should we also check for status=completed/succeeded? (leaving for now, will let check all): + // get amount + $transaction_amount = 0.0; - // get amount - $transaction_amount = 0.0; + if ( isset( $transaction['total'] ) ) { + $transaction_amount = (float) $transaction['total']; - if ( isset( $transaction['total'] ) ) { - $transaction_amount = (float) $transaction['total']; - } + if ( $transaction_amount > 0 ) { + + switch ( $transaction['type'] ) { + case __( 'Sale', 'zero-bs-crm' ): + // these count as debits against invoice. + $transactions_total_value -= $transaction_amount; - switch ( $transaction['type'] ) { + break; - case __( 'Sale', 'zero-bs-crm' ): - // these count as debits against invoice. - $transactions_total_value -= $transaction_amount; + case __( 'Refund', 'zero-bs-crm' ): + case __( 'Credit Note', 'zero-bs-crm' ): + // these count as credits against invoice. + $transactions_total_value += $transaction_amount; - break; + break; - case __( 'Refund', 'zero-bs-crm' ): - case __( 'Credit Note', 'zero-bs-crm' ): - // These count as credits against invoice, and should be added. - $transactions_total_value -= abs( (float) $transaction_amount ); + } // / switch on type (sale/refund) - break; + } // / if trans > 0 - } // / switch on type (sale/refund) + } // / if isset - } // / each trans + } // / each trans - // should now have $transactionsTotalValue & $invoiceTotalValue - // ... so we sum + return. - return $invoiceTotalValue + $transactions_total_value; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase + // should now have $transactions_total_value & $invoice_total_value + // ... so we sum + return. + return $invoice_total_value + $transactions_total_value; - } // / if has trans + } // / if has trans + } // if isset invoice total - } // / if retrieved inv + } // / if retrieved inv } // / if invoice_id > 0 diff --git a/projects/plugins/crm/includes/ZeroBSCRM.InvoiceBuilder.php b/projects/plugins/crm/includes/ZeroBSCRM.InvoiceBuilder.php index 48c0592b6b854..0ea17c091872b 100644 --- a/projects/plugins/crm/includes/ZeroBSCRM.InvoiceBuilder.php +++ b/projects/plugins/crm/includes/ZeroBSCRM.InvoiceBuilder.php @@ -621,22 +621,23 @@ function zeroBSCRM_invoicing_generateStatementHTML_v3( $contact_id = -1, $return // ignore if status_bool (non-completed status) $partial['status_bool'] = (int) $partial['status_bool']; - if ( isset( $partial ) && $partial['status_bool'] == 1 && isset( $partial['total'] ) ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual - - switch ( $partial['type'] ) { - case __( 'Sale', 'zero-bs-crm' ): - // these count as debits against invoice. - $balance = $balance - $partial['total']; - $payments += $partial['total']; - - break; - case __( 'Refund', 'zero-bs-crm' ): - case __( 'Credit Note', 'zero-bs-crm' ): - // These count as credits against invoice, and should be added. - $balance -= abs( (float) $partial['total'] ); - $payments -= abs( (float) $partial['total'] ); - - break; + if ( isset( $partial ) && $partial['status_bool'] == 1 && isset( $partial['total'] ) && $partial['total'] > 0 ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual + + // v3.0+ has + or - partials. Account for that: + if ( $partial['type_accounting'] === 'credit' ) { + + // credit note, or refund + $balance = $balance + $partial['total']; + // add to payments + $payments += $partial['total']; + + } else { + + // assume debit + $balance = $balance - $partial['total']; + + // add to payments + $payments += $partial['total']; } } } // /foreach @@ -1085,20 +1086,14 @@ function zeroBSCRM_invoicing_generateInvoiceHTML( $invoice_id = -1, $template = // ignore if status_bool (non-completed status) $partial['status_bool'] = (int) $partial['status_bool']; if ( isset( $partial ) && $partial['status_bool'] == 1 ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual - // v3.0+ has + or - partials. Account for that. - switch ( $partial['type'] ) { - - case __( 'Sale', 'zero-bs-crm' ): - // these count as debits against invoice. - $balance = $balance - $partial['total']; - break; - - case __( 'Refund', 'zero-bs-crm' ): - case __( 'Credit Note', 'zero-bs-crm' ): - // These count as credits against invoice, and should be added. - $balance -= abs( (float) $partial['total'] ); - break; + // v3.0+ has + or - partials. Account for that: + if ( $partial['type_accounting'] === 'credit' ) { + // credit note, or refund + $balance = $balance + $partial['total']; + } else { + // assume debit + $balance = $balance - $partial['total']; } $partials_table .= ''; diff --git a/projects/plugins/crm/js/ZeroBSCRM.admin.invoicebuilder.js b/projects/plugins/crm/js/ZeroBSCRM.admin.invoicebuilder.js index 2f8c5461cf38b..0a57d09869866 100644 --- a/projects/plugins/crm/js/ZeroBSCRM.admin.invoicebuilder.js +++ b/projects/plugins/crm/js/ZeroBSCRM.admin.invoicebuilder.js @@ -1569,10 +1569,8 @@ function zbscrm_JS_calc_amount_due() { // get var v = jQuery( '.zbs-partial-value', ele ).text(); - var label = jQuery( '.zlabel', ele ).text(); - // detect +- - var multiplier = 1; // gets turned to -1 if negative () + var multiplier = 1; // gets turned to -1 if negotive () // got -? if ( v.includes( '(' ) ) { @@ -1581,11 +1579,10 @@ function zbscrm_JS_calc_amount_due() { } v = parseFloat( v ) * multiplier; - if ( label.includes( 'Refund' ) || label.includes( 'Credit' ) ) { - amount_due -= Math.abs( v ); - } else { - amount_due -= v; - } + // debug console.log('amount:',[jQuery('.zbs-partial-value',ele).text(),amount_due,v]); + + // do it :) + amount_due -= v; } ); jQuery( '#inv-amount-due' ).html( amount_due.toFixed( zbs_root.currencyOptions.noOfDecimals ) );