Additional bugfixes for prepayment handling in sales reports (Customer Balances,...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 21 Jun 2022 09:54:28 +0000 (11:54 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Wed, 22 Jun 2022 19:25:06 +0000 (21:25 +0200)
includes/dashboard.inc
reporting/rep102.php
reporting/rep107.php
sales/customer_invoice.php
sales/includes/db/customers_db.inc
sales/includes/db/sales_invoice_db.inc

index f666f26f9135d3fe7e9d81613c1d78e40d605b5e..c8636644a777f62fe0b1ccd3befb8e453afcbc59 100644 (file)
@@ -788,10 +788,12 @@ function customer_aging($today, $width="33")
        $today1 = date2sql($today);
        $past1 = get_company_pref('past_due_days');
        $past2 = 2 * $past1;
-       // removed - debtor_trans.alloc from all summations
+
        $sign = "IF(`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, 1)";
-       $value = "IFNULL($sign*((trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - 
-               trans.alloc)*trans.rate),0)";
+
+       $value = "$sign*(IF(trans.prep_amount, trans.prep_amount,
+               ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount)) - trans.alloc)*trans.rate";
+
        $due = "IF (trans.type=".ST_SALESINVOICE.", trans.due_date, trans.tran_date)";
     $sql = "SELECT Sum($value) AS Balance,
                                Sum(IF ((TO_DAYS('$today1') - TO_DAYS($due)) > 0,$value,0)) AS Due,
@@ -804,7 +806,7 @@ function customer_aging($today, $width="33")
                        WHERE
                                        debtor.payment_terms = terms.terms_indicator
                                AND debtor.credit_status = credit_status.id";
-               $sql .= " AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount) - trans.alloc > ".FLOAT_COMP_DELTA;
+               $sql .= " AND ABS(IF(trans.prep_amount, trans.prep_amount, ABS(trans.ov_amount) + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount) - trans.alloc) > ".FLOAT_COMP_DELTA;
     $result = db_query($sql,"The customer details could not be retrieved");
 
     $row = db_fetch($result);
index fe7ff84a206eae16c237684c3e39af4691c5855b..76cbe1a5805d4538872fa32329f5bfb89619e09e 100644 (file)
@@ -34,22 +34,24 @@ function get_invoices($customer_id, $to, $all=true)
        $PastDueDays2 = 2 * $PastDueDays1;
 
        $sign = "IF(`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, 1)";
+
        $value = "$sign*(IF(trans.prep_amount, trans.prep_amount,
                ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount)) ".($all ? '' : "- trans.alloc").")";
+
        $due = "IF (type=".ST_SALESINVOICE.", due_date, tran_date)";
 
        $sql = "SELECT type, reference, tran_date,
-               $sign*$value as Balance,
-               IF ((TO_DAYS('$todate') - TO_DAYS($due)) > 0,$sign*$value,0) AS Due,
-               IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $PastDueDays1,$sign*$value,0) AS Overdue1,
-               IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $PastDueDays2,$sign*$value,0) AS Overdue2
+               $value as Balance,
+               IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0) AS Due,
+               IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays1,$value,0) AS Overdue1,
+               IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $PastDueDays2,$value,0) AS Overdue2
 
                FROM ".TB_PREF."debtor_trans trans
 
                WHERE type <> ".ST_CUSTDELIVERY."
                        AND debtor_no = $customer_id 
                        AND tran_date <= '$todate'
-                       AND ABS(IF(trans.prep_amount, trans.prep_amount, ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount) ".($all ? '' : '-trans.alloc').") > " . FLOAT_COMP_DELTA;
+                       AND ABS($value) > " . FLOAT_COMP_DELTA;
 
        $sql .= "ORDER BY tran_date";
 
index 0ac5f03b803e9e0e82392da61263f08f10de7a1b..1362d5d1591816a6304cf7fa8f2765f14c4a2ff6 100644 (file)
@@ -61,8 +61,6 @@ print_invoices();
 function print_invoices()
 {
        global $path_to_root, $SysPrefs;
-       
-       $show_this_payment = true; // include payments invoiced here in summary
 
        include_once($path_to_root . "/reporting/includes/pdf_report.inc");
 
@@ -143,8 +141,11 @@ function print_invoices()
                        // calculate summary start row for later use
                        $summary_start_row = $rep->bottomMargin + (15 * $rep->lineHeight);
 
+                       $show_this_payment = $rep->formData['prepaid'] == 'partial'; // include payments invoiced here in summary
+
                        if ($rep->formData['prepaid'])
                        {
+                               
                                $result = get_sales_order_invoices($myrow['order_']);
                                $prepayments = array();
                                while($inv = db_fetch($result))
index e4ab5158322f95ef6fffea1a7f20fdf724a76317..3bf5a6bbb7e147e5f0e1d3ccc5723995fe4d2f61 100644 (file)
@@ -649,10 +649,10 @@ if ($prepaid)
                        $allocs += $pmt['amt'];
                }
        }
-
        label_row(_("Payments received:"), implode(',', $list));
        label_row(_("Invoiced here:"), price_format($_SESSION['Items']->prep_amount), 'class=label');
-       label_row(_("Left to be invoiced:"), price_format($_SESSION['Items']->get_trans_total()-max($_SESSION['Items']->prep_amount, $allocs)), 'class=label');
+       label_row($_SESSION['Items']->payment_terms['days_before_due'] == -1 ? _("Left to be invoiced:") : _("Invoiced so far:"),
+               price_format($_SESSION['Items']->get_trans_total()-max($_SESSION['Items']->prep_amount, $allocs)), 'class=label');
 }
 
 textarea_row(_("Memo:"), 'Comments', null, 50, 4);
index 1707ef9082b1c3953e0b1510d010ac9ce2ee1303..036253cd9922da135f1709522b82c835ab87f71d 100644 (file)
@@ -78,14 +78,13 @@ function get_customer_details($customer_id, $to=null, $all=true)
                ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount)) ".($all ? '' : "- trans.alloc").")";
 
        $due = "IF (trans.type=".ST_SALESINVOICE.", trans.due_date, trans.tran_date)";
-
        $sql = "SELECT debtor.name, debtor.curr_code, terms.terms,
                debtor.credit_limit, credit_status.dissallow_invoices, credit_status.reason_description,
 
-               Sum($sign*IFNULL($value,0)) AS Balance,
-               Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > 0,$sign*$value,0)) AS Due,
-               Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $past1,$sign*$value,0)) AS Overdue1,
-               Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $past2,$sign*$value,0)) AS Overdue2
+               Sum(IFNULL($value,0)) AS Balance,
+               Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0)) AS Due,
+               Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past1,$value,0)) AS Overdue1,
+               Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past2,$value,0)) AS Overdue2
 
                FROM ".TB_PREF."debtors_master debtor
                        LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.tran_date <= '$todate' AND debtor.debtor_no = trans.debtor_no AND trans.type <> ". ST_CUSTDELIVERY.",
index ccbcc0eb032dd7c7adeeaa00bdada22ed4ea3c88..a80232ecd5c78b16ce33623b45f8bbf8aec5c8d1 100644 (file)
@@ -84,7 +84,6 @@ function write_sales_invoice(&$invoice)
        } else {
                $prepaid_factor = 1;
        }
-
         // write_customer_trans have to be called after optional void_cust_allocations above
        $invoice_no = write_customer_trans(ST_SALESINVOICE, $trans_no, $invoice->customer_id,
                $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
@@ -300,18 +299,14 @@ function get_cust_prepayment_invoice_factor($trans_no)
 */
 function prepaid_invoice_remainder($order)
 {
-       $sql = "SELECT so.total - IFNULL(SUM(inv.prep_amount),0) FROM "
-               .TB_PREF."sales_orders so,
-               ".TB_PREF."debtor_trans inv,
-               ".TB_PREF."payment_terms pt
+       $sql = "SELECT so.total - SUM(IFNULL(inv.prep_amount,0)) FROM "
+               .TB_PREF."sales_orders so
+                       LEFT JOIN ".TB_PREF."payment_terms pt ON so.payment_terms=pt.terms_indicator AND pt.days_before_due = -1
+                       LEFT JOIN ".TB_PREF."debtor_trans inv ON inv.type=".ST_SALESINVOICE ." AND inv.order_=so.order_no
                WHERE  so.order_no=".db_escape($order)
                        ." AND so.trans_type=".ST_SALESORDER
-                       ." AND inv.type=".ST_SALESINVOICE
-                       ." AND inv.order_=so.order_no"
-                       ." AND so.payment_terms=pt.terms_indicator"
-                       ." AND inv.payment_terms=pt.terms_indicator"
-                       ." AND pt.days_before_due = -1";
-
+                       ." AND so.payment_terms=pt.terms_indicator";
+                       
        $result = db_fetch(db_query($sql, "cannot find final invoice value"));
        return $result[0] ? $result[0] : 0;
 }