[0003780] Sales Invoice to partial delivery was improperly issued as final prepayment...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Thu, 18 May 2017 20:02:12 +0000 (22:02 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Thu, 18 May 2017 20:07:37 +0000 (22:07 +0200)
sales/customer_invoice.php
sales/includes/cart_class.inc
sales/includes/db/sales_invoice_db.inc
sales/includes/ui/sales_order_ui.inc
sales/sales_order_entry.php

index 6f38b64d718b9ef7f0472af845839c0287a8738f..6a8e82bf041a1674f6f4afb2772613e769892e0a 100644 (file)
@@ -342,7 +342,7 @@ function check_data()
                        return false;
                }
        } else {
-               if (($_SESSION['Items']->payment_terms['days_before_due'] < 0) && !count($_SESSION['Items']->prepayments)) {
+               if (($_SESSION['Items']->payment_terms['days_before_due'] == -1) && !count($_SESSION['Items']->prepayments)) {
                        display_error(_("There is no non-invoiced payments for this order. If you want to issue final invoice, select delayed or cash payment terms."));
                        return false;
                }
@@ -355,6 +355,7 @@ function check_data()
 if (isset($_POST['process_invoice']) && check_data()) {
        $newinvoice=  $_SESSION['Items']->trans_no == 0;
        copy_to_cart();
+
        if ($newinvoice) 
                new_doc_date($_SESSION['Items']->document_date);
 
index dc6e81a63adbc705d2a87071a1ff0325592c4ff9..b2d7a51f6a433749eab927143afa7a75a6592284 100644 (file)
@@ -636,12 +636,12 @@ class cart
                $remainder = prepaid_invoice_remainder($this->order_no);
 
                // recalculate prepaid part from payments
-               if ($this->payment_terms['days_before_due'] < 0)
+               if ($this->payment_terms['days_before_due'] == -1)
                {       // this is partial invoice for selected prepayments made.
                        $paid = 0;
                        foreach($this->prepayments as $payment)
                                $paid += $payment['amt'];
-                       $this->prep_amount = min($remainder, $paid);
+                       $this->prep_amount = $this->trans_no ? $paid : min($remainder, $paid);
                } else  // this is final invoice
                        $this->prep_amount = $remainder;
        }
index 54e4374a8fb3b9a755c5ba0bbb2357f572a68750..d4fda212743ccecb616148e1a1b4526f81e1fbde 100644 (file)
@@ -284,22 +284,28 @@ function get_cust_prepayment_invoice_factor($trans_no)
        return $row[0];
 }
 
+/*
+       Return yet not invoiced amount of prepayment invoice (or 0 if normal invoice)
+*/
 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
-               LEFT JOIN ".TB_PREF."payment_terms pt ON inv.payment_terms=pt.terms_indicator
+               ".TB_PREF."debtor_trans inv,
+               ".TB_PREF."payment_terms pt
                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 pt.days_before_due<0";
+                       ." AND so.payment_terms=pt.terms_indicator"
+                       ." AND inv.payment_terms=pt.terms_indicator"
+                       ." AND pt.days_before_due = -1";
 
        $result = db_fetch(db_query($sql, "cannot find final invoice value"));
-       return $result[0];
+       return $result[0] ? $result[0] : 0;
 }
 
+
 /*
        Find oldest delivery date for sales invoice
 */
index f2dd4af50800e2cccff408bdb87e0e17a1dec78c..4b697c514f6526d4dd2307e7e4fa2cedab40b085 100644 (file)
@@ -607,7 +607,7 @@ function display_delivery_details(&$order)
                table_section(1);
 
                locations_list_row(_("Deliver from Location:"), 'Location', null, false, true, $order->fixed_asset);
-               if ($order->payment_terms['days_before_due'] < 0)
+               if ($order->payment_terms['days_before_due'] == -1)
                {
                        $Ajax->addUpdate('items_table', 'prep_amount', price_format($order->get_trans_total())); // bind to items_table update
                        if (!$order->is_started())
index 3517196df208c18965e70d7b915fde0269af05be..618030a450d3a3855a42d386ef9e3778228232ef 100644 (file)
@@ -385,7 +385,7 @@ function can_process() {
                return false;
        }
        if ($_SESSION['Items']->payment_terms['cash_sale'] == 0) {
-               if (!$_SESSION['Items']->is_started() && ($_SESSION['Items']->payment_terms['days_before_due'] < 0) && ((input_num('prep_amount')<=0) ||
+               if (!$_SESSION['Items']->is_started() && ($_SESSION['Items']->payment_terms['days_before_due'] == -1) && ((input_num('prep_amount')<=0) ||
                        input_num('prep_amount')>$_SESSION['Items']->get_trans_total())) {
                        display_error(_("Pre-payment required have to be positive and less than total amount."));
                        set_focus('prep_amount');