Payment terms related functions moved to separate file, common function for calculati...
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Wed, 31 Jul 2019 09:46:36 +0000 (11:46 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 19 Aug 2019 10:41:58 +0000 (12:41 +0200)
15 files changed:
admin/db/company_db.inc
admin/db/payment_terms_db.inc [new file with mode: 0644]
includes/db/sql_functions.inc
purchasing/includes/db/invoice_db.inc
purchasing/includes/ui/invoice_ui.inc
purchasing/includes/ui/po_ui.inc
sales/customer_delivery.php
sales/customer_invoice.php
sales/includes/cart_class.inc
sales/includes/db/recurrent_invoices_db.inc
sales/includes/db/sales_order_db.inc
sales/includes/ui/sales_order_ui.inc
sales/sales_order_entry.php
sql/en_US-demo.sql
sql/en_US-new.sql

index b774d2bc0d90f83a83a1fbcb37b77148181745fe..c7e7c1aeff890945ba6a3b88bdde9229eb07ad8c 100644 (file)
@@ -9,6 +9,8 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
+include_once($path_to_root. "/admin/db/payment_terms_db.inc");
+
 /*
        Update main or gl company setup.
 */
@@ -86,89 +88,6 @@ function get_company_extensions($id = -1) {
        return $installed_extensions;
 }
 
-function add_payment_terms($terms, $type, $days=0, $early_discount=0, $early_days=0)
-{
-       begin_transaction(__FUNCTION__, func_get_args());
-
-       $sql = "INSERT INTO ".TB_PREF."payment_terms (terms, type, days, early_discount, early_days)
-               VALUES (" .db_escape($terms) . "," . db_escape($type) . ", ".db_escape($days). ", ".db_escape($early_discount). ", ".db_escape($early_days).")";
-
-       db_query($sql,"The payment term could not be added");
-
-       commit_transaction();
-}
-
-function update_payment_terms($selected_id, $terms, $type, $days=0, $early_discount=0, $early_days=0)
-{
-       begin_transaction(__FUNCTION__, func_get_args());
-
-       $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
-               type=".db_escape($type).",
-               days=" . db_escape($days).",
-               early_discount=".db_escape($early_discount).",
-               early_days=".db_escape($early_days).
-               " WHERE id = " .db_escape($selected_id);
-
-       db_query($sql,"The payment term could not be updated");
-
-       commit_transaction();
-}
-
-function delete_payment_terms($selected_id)
-{
-       begin_transaction(__FUNCTION__, func_get_args());
-
-       $sql="DELETE FROM ".TB_PREF."payment_terms WHERE id=".db_escape($selected_id);
-       db_query($sql,"could not delete a payment terms");
-
-       commit_transaction();
-}
-
-function get_payment_terms($selected_id)
-{
-       $sql = "SELECT * FROM ".TB_PREF."payment_terms t WHERE id=".db_escape($selected_id);
-
-       $result = db_query($sql,"could not get payment term");
-
-       return db_fetch($result);
-}
-
-function get_payment_terms_all($show_inactive)
-{
-       $sql = "SELECT * FROM ".TB_PREF."payment_terms";
-       if (!$show_inactive) $sql .= " WHERE !inactive";
-       return db_query($sql,"could not get payment terms");
-}
-/*
-       Return number of records in tables, where some foreign key $id is used.
-       $id - searched key value
-       $tables - array of table names (without prefix); when table name is used as a key, then
-               value is name of foreign key field. For numeric keys $stdkey field name is used.
-       $stdkey - standard name of foreign key.
-*/
-function key_in_foreign_table($id, $tables, $stdkey)
-{
-
-       if (!is_array($tables))
-               $tables = array($tables);
-
-       $sqls = array();
-       foreach ($tables as $tbl => $key) {
-               if (is_numeric($tbl)) {
-                       $tbl = $key;
-                       $key = $stdkey;
-               }
-               $sqls[] = "(SELECT COUNT(*) as cnt FROM `".TB_PREF."$tbl` WHERE `$key`=".db_escape($id).")\n";
-       }
-
-       $sql = "SELECT sum(cnt) FROM (". implode(' UNION ', $sqls).") as counts";
-
-       $result = db_query($sql, "check relations for ".implode(',',$tables)." failed");
-       $count = db_fetch($result);
-
-       return $count[0];
-}
-
 //---------------------------------------------------------------------------------------------
 //
 // Resets $theme references in users records to 'default'.
diff --git a/admin/db/payment_terms_db.inc b/admin/db/payment_terms_db.inc
new file mode 100644 (file)
index 0000000..3934622
--- /dev/null
@@ -0,0 +1,91 @@
+<?php
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
+
+function add_payment_terms($terms, $type, $days=0, $early_discount=0, $early_days=0)
+{
+       begin_transaction(__FUNCTION__, func_get_args());
+
+       $sql = "INSERT INTO ".TB_PREF."payment_terms (terms, type, days, early_discount, early_days)
+               VALUES (" .db_escape($terms) . "," . db_escape($type) . ", ".db_escape($days). ", ".db_escape($early_discount). ", ".db_escape($early_days).")";
+
+       db_query($sql,"The payment term could not be added");
+
+       commit_transaction();
+}
+
+function update_payment_terms($selected_id, $terms, $type, $days=0, $early_discount=0, $early_days=0)
+{
+       begin_transaction(__FUNCTION__, func_get_args());
+
+       $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
+               type=".db_escape($type).",
+               days=" . db_escape($days).",
+               early_discount=".db_escape($early_discount).",
+               early_days=".db_escape($early_days).
+               " WHERE id = " .db_escape($selected_id);
+
+       db_query($sql,"The payment term could not be updated");
+
+       commit_transaction();
+}
+
+function delete_payment_terms($selected_id)
+{
+       begin_transaction(__FUNCTION__, func_get_args());
+
+       $sql="DELETE FROM ".TB_PREF."payment_terms WHERE id=".db_escape($selected_id);
+       db_query($sql,"could not delete a payment terms");
+
+       commit_transaction();
+}
+
+function get_payment_terms($selected_id)
+{
+       $sql = "SELECT * FROM ".TB_PREF."payment_terms t WHERE id=".db_escape($selected_id);
+
+       $result = db_query($sql,"could not get payment term");
+
+       return db_fetch($result);
+}
+
+function get_payment_terms_all($show_inactive)
+{
+       $sql = "SELECT * FROM ".TB_PREF."payment_terms";
+       if (!$show_inactive) $sql .= " WHERE !inactive";
+       return db_query($sql,"could not get payment terms");
+}
+
+/*
+       Calculate due date using terms data provided either as table of payment data or payment terms id
+*/
+function get_payment_due_date($terms_data, $date)
+{
+       if (!is_array($terms_data))
+               $terms_data = get_payment_terms($terms_data);
+
+       if (!is_date($date))
+               $date = new_doc_date();
+       
+       if (!$terms_data)
+               return $date;
+
+       if ($terms_data['type'] == PTT_FOLLOWING) {
+               $end = end_month(add_months(begin_month($date), 1));
+               $duedate = add_days(end_month($date), $terms_data['days']);
+               if (date1_greater_date2($duedate, $end))
+                       $duedate = $end;
+       } elseif ($terms_data['type'] == PTT_DAYS)
+               $duedate = add_days($date, $terms_data['days']);
+       else
+               $duedate = $date;
+       return $duedate;
+}
index 3b4b5acd499511998fd0ef9ba80c8d6a10d355d7..56392e6d9fdb7eef8c2edbd838cb7517ab2d512e 100644 (file)
@@ -101,3 +101,33 @@ function running_total_sql($table, $column, $index)
                        (SELECT @total:=0) total_var";
 }
 
+/*
+       Return number of records in tables, where some foreign key $id is used.
+       $id - searched key value
+       $tables - array of table names (without prefix); when table name is used as a key, then
+               value is name of foreign key field. For numeric keys $stdkey field name is used.
+       $stdkey - standard name of foreign key.
+*/
+function key_in_foreign_table($id, $tables, $stdkey)
+{
+
+       if (!is_array($tables))
+               $tables = array($tables);
+
+       $sqls = array();
+       foreach ($tables as $tbl => $key) {
+               if (is_numeric($tbl)) {
+                       $tbl = $key;
+                       $key = $stdkey;
+               }
+               $sqls[] = "(SELECT COUNT(*) as cnt FROM `".TB_PREF."$tbl` WHERE `$key`=".db_escape($id).")\n";
+       }
+
+       $sql = "SELECT sum(cnt) FROM (". implode(' UNION ', $sqls).") as counts";
+
+       $result = db_query($sql, "check relations for ".implode(',',$tables)." failed");
+       $count = db_fetch($result);
+
+       return $count[0];
+}
+
index 443a16ed30072648f7520a7a03c210e19fa000a9..11fed129f8f7dccfcade4ac8a2f53f3bb6123445 100644 (file)
@@ -38,12 +38,7 @@ function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
                if (!is_date_in_fiscalyear($supp_trans->tran_date))
                        $supp_trans->tran_date = end_fiscalyear();
        }
-       if ($supp_trans->supplier_id != $supplier_id)
-           get_duedate_from_terms($supp_trans);
 
-    $supp_trans->supplier_id = $supplier_id;
-    $supp_trans->tax_included = $myrow['tax_included'];
-    $supp_trans->supplier_name = $myrow['supp_name'];
        $supp_trans->terms = array( 
                'description' => $myrow['terms'],
                'type' => $myrow['type'], 
@@ -51,6 +46,13 @@ function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
                'early_discount' => $myrow['early_discount'],
                'early_days' => $myrow['early_days'],
        );
+
+       if ($supp_trans->supplier_id != $supplier_id)
+           $supp_trans->due_date = get_payment_due_date($supp_trans->terms, $supp_trans->tran_date);
+
+    $supp_trans->supplier_id = $supplier_id;
+    $supp_trans->tax_included = $myrow['tax_included'];
+    $supp_trans->supplier_name = $myrow['supp_name'];
        $supp_trans->credit = $myrow['cur_credit'];
 
        $supp_trans->tax_description = $myrow['tax_group_name'];
index e677cf3accf21bb20b3592a6f8b0b10ff26e0539..14953a2fbaff67d984ff397c4b6f262e815b41db 100644 (file)
@@ -109,6 +109,9 @@ function invoice_header(&$supp_trans)
                $supp_trans->clear_items();
                read_supplier_details_to_trans($supp_trans, $_POST['supplier_id']);
                copy_from_trans($supp_trans);
+               $supp_trans->due_date = get_payment_due_date($supp_trans->terms, $supp_trans->tran_date);
+               $_POST['due_date'] = $supp_trans->due_date;
+               $Ajax->activate('due_date');
        }
 
        date_row(_("Date") . ":", 'tran_date', '', true, 0, 0, 0, "", true);
@@ -127,7 +130,7 @@ function invoice_header(&$supp_trans)
        if (isset($_POST['_tran_date_changed'])) {
                $Ajax->activate('_ex_rate');
                $supp_trans->tran_date = $_POST['tran_date'];
-               get_duedate_from_terms($supp_trans);
+               $supp_trans->due_date = get_payment_due_date($supp_trans->terms, $supp_trans->tran_date);
                $_POST['due_date'] = $supp_trans->due_date;
                $Ajax->activate('due_date');
        }
@@ -573,26 +576,3 @@ function display_grn_items(&$supp_trans, $mode=0)
 
        return $total_grn_value;
 }
-
-//--------------------------------------------------------------------------------------------------
-function get_duedate_from_terms(&$trans)
-{
-       $date = $trans->tran_date;
-
-       if (!is_date($date))
-       {
-               $date = Today();
-       }
-       if ($trans->terms['type'] == PTT_FOLLOWING)
-       { /*Its a day in the following month when due */
-               $trans->due_date =
-                       add_days(end_month($date), $trans->terms["days"]);
-       }
-       else
-       { /*Use the Days Before Due to add to the invoice date */
-               $trans->due_date = add_days($date, $trans->terms["days"]);
-       }
-}
-
-//--------------------------------------------------------------------------------------------------
-
index ccef48c3ff14340fb8bdfbfe13f0e5d4ef645317..65ed33e584ceebe84392f414a24bf55240108989 100644 (file)
@@ -124,7 +124,8 @@ function display_po_header(&$order)
        if ($order->supplier_id != get_post('supplier_id',-1)) {
                $old_supp = $order->supplier_id;
                get_supplier_details_to_order($order, $_POST['supplier_id']); 
-           get_duedate_from_terms($order);
+           $order->due_date = get_payment_due_date($order->terms, $order->tran_date);
+
                $_POST['due_date'] = $order->due_date;
 
                // supplier default price update
@@ -146,7 +147,7 @@ function display_po_header(&$order)
 
        if (isset($_POST['_OrderDate_changed'])) {
                $order->tran_date = $_POST['OrderDate'];
-           get_duedate_from_terms($order);
+           $order->due_date = get_payment_due_date($order->terms, $order->tran_date);
            $_POST['due_date'] = $order->due_date;
                $Ajax->activate('due_date');
        }
index 1bec9393a2cfedb7c76171f43d0c5a1cb9034118..637c238d70145fc61683989750072b2696edf4d9 100644 (file)
@@ -373,7 +373,7 @@ echo "</td><td>";// outer table
 start_table(TABLESTYLE, "width='90%'");
 
 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
-       $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->payment, $_POST['DispatchDate']);
+       $_POST['due_date'] = get_payment_due_date($_SESSION['Items']->payment, $_POST['DispatchDate']);
 }
 customer_credit_row($_SESSION['Items']->customer_id, $_SESSION['Items']->credit, "class='tableheader2'");
 
index 2bc41391938c720dae8a96b6f3bc547ae3ff056e..e46e38e7c3517afdf5c52bdab1b137cfa42f5611 100644 (file)
@@ -189,7 +189,7 @@ if (isset($_POST['Update'])) {
        $Ajax->activate('Items');
 }
 if (isset($_POST['_InvoiceDate_changed'])) {
-       $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->payment, $_POST['InvoiceDate']);
+       $_POST['due_date'] = get_payment_due_date($_SESSION['Items']->payment, $_POST['InvoiceDate']);
        $Ajax->activate('due_date');
 }
 
@@ -381,7 +381,7 @@ if(list_updated('payment')) {
        copy_to_cart();
        $order->payment = get_post('payment');
        $order->payment_terms = get_payment_terms($order->payment);
-       $_POST['due_date'] = $order->due_date = get_invoice_duedate($order->payment, $order->document_date);
+       $_POST['due_date'] = $order->due_date = get_payment_due_date($order->payment, $order->document_date);
        $_POST['Comments'] = '';
        $Ajax->activate('due_date');
        $Ajax->activate('options');
@@ -486,7 +486,7 @@ date_cells(_("Date"), 'InvoiceDate', '', $_SESSION['Items']->trans_no == 0,
        0, 0, 0, "class='tableheader2'", true);
 
 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
-       $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->payment, $_POST['InvoiceDate']);
+       $_POST['due_date'] = get_payment_due_date($_SESSION['Items']->payment, $_POST['InvoiceDate']);
 }
 
 date_cells(_("Due Date"), 'due_date', '', null, 0, 0, 0, "class='tableheader2'");
index 611194bfd53889aa4ab549463d31d9addd7aca40..b35b25f4591d85b84cecfdb298f6930b4d490ef6 100644 (file)
@@ -158,7 +158,7 @@ class Cart
                        $this->dimension2_id = $cust['dimension2_id'];
                }
                if ($type == ST_SALESINVOICE) {
-                       $this->due_date = get_invoice_duedate($this->payment, $this->document_date);
+                       $this->due_date = get_payment_due_date($this->payment, $this->document_date);
                }
 
                $this->src_docs = $this->trans_no;
@@ -270,7 +270,7 @@ class Cart
                        }       
                        if ($type == ST_SALESINVOICE) {
                                $this->due_date =
-                                       get_invoice_duedate($this->payment, $this->document_date);
+                                       get_payment_due_date($this->payment, $this->document_date);
                        } else
                                $this->due_date =
                                        add_days($this->document_date, $SysPrefs->default_delivery_required_by());
index 5529d0c28fd0544fa7c86ea6e8e48185a6e200ee..7aca37cf46f21723a8537e682effbe926b4de997 100644 (file)
@@ -194,7 +194,7 @@ function create_template_invoice($customer_id, $branch_id, $order_no, $tmpl_no,
        $doc->trans_no = 0;
        $doc->document_date = $date;
 
-       $doc->due_date = get_invoice_duedate($doc->payment, $doc->document_date);
+       $doc->due_date = get_payment_due_date($doc->payment, $doc->document_date);
 
        $doc->reference = $Refs->get_next($doc->trans_type, null, array('customer' => $customer_id, 'branch' => $branch_id,
                'date' => $date));
index f4d18625f6f28fb6b5f74bf48c5db00eec601b1c..7abe76217ad711556e905822c690619b234c7dd4 100644 (file)
@@ -383,27 +383,6 @@ function close_sales_order($order_no)
 
 //---------------------------------------------------------------------------------------------------------------
 
-function get_invoice_duedate($terms, $invdate)
-{
-       if (!is_date($invdate))
-       {
-               return new_doc_date();
-       }
-       
-       $myrow = get_payment_terms($terms);
-       
-       if (!$myrow)
-               return $invdate;
-
-       if ($myrow['type'] == PTT_FOLLOWING)
-               $duedate = add_days(end_month($invdate), $myrow['days']);
-       elseif ($myrow['type'] == PTT_DAYS)
-               $duedate = add_days($invdate, $myrow['days']);
-       else
-               $duedate = $invdate;
-       return $duedate;
-}
-
 function get_customer_to_order($customer_id) {
 
        // Now check to ensure this account is not on hold */
index 2d9be2dce28c69577936b438ba4025e47a0cad4d..1a75eba3fecc6af5ca63d31845fd3fe913cde77d 100644 (file)
@@ -118,7 +118,7 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
        $order->set_delivery($myrow["default_ship_via"], $myrow["br_name"],
                $address);
        if ($order->trans_type == ST_SALESINVOICE) {
-               $order->due_date = get_invoice_duedate($order->payment, $order->document_date);
+               $order->due_date = get_payment_due_date($order->payment, $order->document_date);
        }
        elseif ($order->trans_type == ST_SALESORDER)
                $order->due_date = add_days($order->document_date, $SysPrefs->default_delivery_required_by());
@@ -387,7 +387,7 @@ function display_order_header(&$order, $editable, $date_text)
                if (get_post('payment') !== $order->payment) {
                        $order->payment = get_post('payment');
                        $order->payment_terms = get_payment_terms($order->payment);
-                       $order->due_date = get_invoice_duedate($order->payment, $order->document_date);
+                       $order->due_date = get_payment_due_date($order->payment, $order->document_date);
                        if ($order->payment_terms['type'] == PTT_CASH) {
                                $_POST['Location'] = $order->Location = $order->pos['pos_location'];
                                $order->location_name = $order->pos['location_name'];
@@ -434,7 +434,7 @@ function display_order_header(&$order, $editable, $date_text)
                        }
                        $Ajax->activate('_ex_rate');
                        if ($order->trans_type == ST_SALESINVOICE) {
-                               $_POST['delivery_date'] = get_invoice_duedate(get_post('payment'), get_post('OrderDate'));
+                               $_POST['delivery_date'] = get_payment_due_date(get_post('payment'), get_post('OrderDate'));
                        } else 
                                $_POST['delivery_date'] = add_days(get_post('OrderDate'), $SysPrefs->default_delivery_required_by());
                        $Ajax->activate('items_table');
index 663bcee718b6bc2d2e720c924a6d6be47fa38890..6f434d86a63f9065087ff174f5af5106f4131ed5 100644 (file)
@@ -664,7 +664,7 @@ function create_cart($type, $trans_no)
                $doc->trans_no = 0;
                $doc->document_date = new_doc_date();
                if ($type == ST_SALESINVOICE) {
-                       $doc->due_date = get_invoice_duedate($doc->payment, $doc->document_date);
+                       $doc->due_date = get_payment_due_date($doc->payment, $doc->document_date);
                        $doc->pos = get_sales_point(user_pos());
                } else
                        $doc->due_date = $doc->document_date;
index b1e7c9edb9dafbd6f4bb131abf39175b0abfc0db..43fc5110f8cdebb69af4ba962758ea353934f52f 100644 (file)
@@ -1093,7 +1093,7 @@ CREATE TABLE `0_payment_terms` (
 
 INSERT INTO `0_payment_terms` VALUES
 ('1', 'Due 15th Of the Following Month', '4', '17', '0', '0', '0'),
-('2', 'Due By End Of The Following Month', '4', '30', '0', '0', '0'),
+('2', 'Due By End Of The Following Month', '4', '31', '0', '0', '0'),
 ('3', 'Payment due within 10 days', '10', '3', '0', '0', '0', '0'),
 ('4', 'Cash Only', '2', '0', '0', '0', '0'),
 ('5', 'Prepaid', '1',  '0', '0', '0', '0');
index e1f378d69b3ded0be3a1feb1bf74216e1fbf0879..30a44cd8673b1775e4901b9dbd4e4252bda73b26 100644 (file)
@@ -877,7 +877,7 @@ CREATE TABLE `0_payment_terms` (
 
 INSERT INTO `0_payment_terms` VALUES
 ('1', 'Due 15th Of the Following Month', '4', '17', '0', '0', '0'),
-('2', 'Due By End Of The Following Month', '4', '30', '0', '0', '0'),
+('2', 'Due By End Of The Following Month', '4', '31', '0', '0', '0'),
 ('3', 'Payment due within 10 days', '3', '10', '0', '0', '0'),
 ('4', 'Cash Only', '2', '0', '0', '0', '0'),
 ('5', 'Prepaid', '1', '0', '0', '0', '0');