From 247a958cc67f9479eb746984b23a660ff9334563 Mon Sep 17 00:00:00 2001 From: Janusz Dobrowolski Date: Thu, 18 May 2017 22:02:12 +0200 Subject: [PATCH] [0003780] Sales Invoice to partial delivery was improperly issued as final prepayment invoice. --- sales/customer_invoice.php | 3 ++- sales/includes/cart_class.inc | 4 ++-- sales/includes/db/sales_invoice_db.inc | 14 ++++++++++---- sales/includes/ui/sales_order_ui.inc | 2 +- sales/sales_order_entry.php | 2 +- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/sales/customer_invoice.php b/sales/customer_invoice.php index 6f38b64d..6a8e82bf 100644 --- a/sales/customer_invoice.php +++ b/sales/customer_invoice.php @@ -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); diff --git a/sales/includes/cart_class.inc b/sales/includes/cart_class.inc index dc6e81a6..b2d7a51f 100644 --- a/sales/includes/cart_class.inc +++ b/sales/includes/cart_class.inc @@ -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; } diff --git a/sales/includes/db/sales_invoice_db.inc b/sales/includes/db/sales_invoice_db.inc index 54e4374a..d4fda212 100644 --- a/sales/includes/db/sales_invoice_db.inc +++ b/sales/includes/db/sales_invoice_db.inc @@ -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 */ diff --git a/sales/includes/ui/sales_order_ui.inc b/sales/includes/ui/sales_order_ui.inc index f2dd4af5..4b697c51 100644 --- a/sales/includes/ui/sales_order_ui.inc +++ b/sales/includes/ui/sales_order_ui.inc @@ -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()) diff --git a/sales/sales_order_entry.php b/sales/sales_order_entry.php index 3517196d..618030a4 100644 --- a/sales/sales_order_entry.php +++ b/sales/sales_order_entry.php @@ -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'); -- 2.30.2