Skip to content

Commit

Permalink
Revert "CRM: Making sure invoice total count factors in credit notes …
Browse files Browse the repository at this point in the history
…and refunds correctly in invoice editor, preview and pdf view" (#35569)
  • Loading branch information
coder-karen authored Feb 9, 2024
1 parent 1e225a3 commit 818b261
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -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.
66 changes: 36 additions & 30 deletions projects/plugins/crm/includes/ZeroBSCRM.DAL3.Obj.Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
53 changes: 24 additions & 29 deletions projects/plugins/crm/includes/ZeroBSCRM.InvoiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 .= '<tr class="total-top">';
Expand Down
13 changes: 5 additions & 8 deletions projects/plugins/crm/js/ZeroBSCRM.admin.invoicebuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( '(' ) ) {
Expand All @@ -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 ) );
Expand Down

0 comments on commit 818b261

Please sign in to comment.