Payment_terms table normalization.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Sun, 28 Jul 2019 14:35:35 +0000 (16:35 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 19 Aug 2019 10:41:58 +0000 (12:41 +0200)
24 files changed:
admin/db/company_db.inc
admin/payment_terms.php
includes/sysnames.inc
includes/types.inc
includes/ui/ui_lists.inc
purchasing/includes/db/invoice_db.inc
purchasing/includes/db/po_db.inc
purchasing/includes/db/suppliers_db.inc
purchasing/includes/ui/invoice_ui.inc
reporting/includes/doctext.inc
reporting/rep203.php
sales/customer_invoice.php
sales/includes/cart_class.inc
sales/includes/db/customers_db.inc
sales/includes/db/recurrent_invoices_db.inc
sales/includes/db/sales_invoice_db.inc
sales/includes/db/sales_order_db.inc
sales/includes/ui/sales_order_ui.inc
sales/sales_order_entry.php
sales/view/view_invoice.php
sales/view/view_sales_order.php
sql/alter2.5.sql
sql/en_US-demo.sql
sql/en_US-new.sql

index 8e788532362e67bbcfaf57be34a72f3d94397e79..b774d2bc0d90f83a83a1fbcb37b77148181745fe 100644 (file)
@@ -86,47 +86,29 @@ function get_company_extensions($id = -1) {
        return $installed_extensions;
 }
 
-function add_payment_terms($daysOrFoll, $terms, $dayNumber)
+function add_payment_terms($terms, $type, $days=0, $early_discount=0, $early_days=0)
 {
        begin_transaction(__FUNCTION__, func_get_args());
 
-       if ($daysOrFoll) 
-       {
-               $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
-                       days_before_due, day_in_following_month)
-                       VALUES (" .
-                       db_escape($terms) . ", " . db_escape($dayNumber) . ", 0)";
-       } 
-       else 
-       {
-               $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
-                       days_before_due, day_in_following_month)
-                       VALUES (" . db_escape($terms) . ",
-                       0, " . db_escape($dayNumber) . ")";
-       }
+       $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, $daysOrFoll, $terms, $dayNumber)
+function update_payment_terms($selected_id, $terms, $type, $days=0, $early_discount=0, $early_days=0)
 {
        begin_transaction(__FUNCTION__, func_get_args());
 
-       if ($daysOrFoll) 
-       {
-               $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
-                       day_in_following_month=0,
-                       days_before_due=" . db_escape($dayNumber) . "
-                       WHERE terms_indicator = " .db_escape($selected_id);
-       } 
-       else 
-       {
-               $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
-                       day_in_following_month=" . db_escape($dayNumber) . ",
-                       days_before_due=0
-                       WHERE terms_indicator = " .db_escape($selected_id);
-       }
+       $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();
@@ -136,7 +118,7 @@ function delete_payment_terms($selected_id)
 {
        begin_transaction(__FUNCTION__, func_get_args());
 
-       $sql="DELETE FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($selected_id);
+       $sql="DELETE FROM ".TB_PREF."payment_terms WHERE id=".db_escape($selected_id);
        db_query($sql,"could not delete a payment terms");
 
        commit_transaction();
@@ -144,8 +126,7 @@ function delete_payment_terms($selected_id)
 
 function get_payment_terms($selected_id)
 {
-       $sql = "SELECT *, (t.days_before_due=0) AND (t.day_in_following_month=0) as cash_sale
-        FROM ".TB_PREF."payment_terms t WHERE terms_indicator=".db_escape($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");
 
index 7c7afeff77e5b1aaba7b68974fba248f7d31b495..1412c372c90c8bc5bb3b6b8b3542c2dc8de87cb6 100644 (file)
@@ -19,25 +19,6 @@ include($path_to_root . "/includes/ui.inc");
 
 simple_page_mode(true);
 
-//------------------------------
-//     Helper to translate record content to more intuitive form
-//
-function term_days($myrow)
-{
-       return $myrow["day_in_following_month"] != 0 ? $myrow["day_in_following_month"] :
-               $myrow["days_before_due"];
-}
-
-function term_type($myrow)
-{
-       if ($myrow["day_in_following_month"] != 0)
-               return PTT_FOLLOWING;
-
-       $days = $myrow["days_before_due"];
-
-       return $days < 0 ? PTT_PRE : ($days ? PTT_DAYS : PTT_CASH);
-}
-
 //-------------------------------------------------------------------------------------------
 
 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
@@ -63,22 +44,15 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
 
        if ($input_error != 1)
        {
-               $type = get_post('type');
-               $days = input_num('DayNumber');
-               $from_now = ($type != PTT_FOLLOWING);
-               if ($type == PTT_CASH)
-                       $days = 0;
-               if ($type == PTT_PRE)
-                       $days = -1;
 
        if ($selected_id != -1) 
        {
-               update_payment_terms($selected_id, $from_now, $_POST['terms'], $days); 
+               update_payment_terms($selected_id, get_post('terms'), get_post('type'), input_num('DayNumber', 0)); 
                        $note = _('Selected payment terms have been updated');
        } 
        else 
        {
-                       add_payment_terms($from_now, $_POST['terms'], $days);
+                       add_payment_terms(get_post('terms'), get_post('type'), input_num('DayNumber', 0));
                        $note = _('New payment terms have been added');
        }
        //run the sql from either of the above possibilites
@@ -131,16 +105,14 @@ table_header($th);
 $k = 0; //row colour counter
 while ($myrow = db_fetch($result)) 
 {
-
+       $days = $myrow['days'];
        alt_table_row_color($k);
-       $type = term_type($myrow);
-       $days = term_days($myrow);
     label_cell($myrow["terms"]);
-    label_cell($pterm_types[$type]);
-    label_cell($type == PTT_DAYS ? "$days "._("days") : ($type == PTT_FOLLOWING ? $days : _("N/A")));
-       inactive_control_cell($myrow["terms_indicator"], $myrow["inactive"], 'payment_terms', "terms_indicator");
-       edit_button_cell("Edit".$myrow["terms_indicator"], _("Edit"));
-       delete_button_cell("Delete".$myrow["terms_indicator"], _("Delete"));
+    label_cell($pterm_types[$myrow['type']]);
+    label_cell($myrow['type'] == PTT_DAYS ? "$days "._("days") : ($myrow['type'] == PTT_FOLLOWING ? $days : _("N/A")));
+       inactive_control_cell($myrow["id"], $myrow["inactive"], 'payment_terms', "id");
+       edit_button_cell("Edit".$myrow["id"], _("Edit"));
+       delete_button_cell("Delete".$myrow["id"], _("Delete"));
     end_row();
 
 }
@@ -157,7 +129,6 @@ div_start('edits');
 
 start_table(TABLESTYLE2);
 
-$day_in_following_month = $days_before_due = 0;
 if ($selected_id != -1) 
 {
        if ($Mode == 'Edit') {
@@ -165,8 +136,8 @@ if ($selected_id != -1)
                $myrow = get_payment_terms($selected_id);
 
                $_POST['terms']  = $myrow["terms"];
-               $_POST['DayNumber'] = term_days($myrow);
-               $_POST['type'] = term_type($myrow);
+               $_POST['type'] = $myrow['type'];
+               $_POST['DayNumber'] = $myrow['days'];
        }
        hidden('selected_id', $selected_id);
 }
index 1d7df6f012be6237b161f0aca6801fdd5306c328..8d3bf78f0a78efbcf435e1c61dc79dcc7cb7cc44 100644 (file)
@@ -192,7 +192,7 @@ $sys_crm_cats = array (
 //----------------------------------------------------------------------------------
 
 $pterm_types = array(
-       PTT_PRE => _("Prepayment"),
+       PTT_PREPAY => _("Prepayment"),
        PTT_CASH => _("Cash"),
        PTT_DAYS => _("After No. of Days"),
        PTT_FOLLOWING => _("Day In Following Month")
index f83f20228d65931fcdf9fe0ef4e3d3a10b07dd7a..da2b33955941f031312a3a6d7e3485d6b52291d0 100644 (file)
@@ -226,7 +226,7 @@ define('TAG_DIMENSION', 2);
 //----------------------------------------------------------------------------------
 // Payment term types
 
-define('PTT_PRE', 1);
+define('PTT_PREPAY', 1);
 define('PTT_CASH', 2);
 define('PTT_DAYS', 3);
 define('PTT_FOLLOWING', 4);
index b803ac3215b40c4f9231d2ab5742c5e96983b142..15cfe2e93dc8632c2001361be63e28223fd79a5e 100644 (file)
@@ -1473,8 +1473,8 @@ function workorders_list_row($label, $name, $selected_id=null)
 
 function payment_terms_list($name, $selected_id=null)
 {
-       $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
-       return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms', array());
+       $sql = "SELECT id, terms, inactive FROM ".TB_PREF."payment_terms";
+       return combo_input($name, $selected_id, $sql, 'id', 'terms', array());
 }
 
 function payment_terms_list_cells($label, $name, $selected_id=null)
@@ -1697,16 +1697,16 @@ function pos_list_row($label, $name, $selected_id=null, $spec_option=false, $sub
 //
 function sale_payment_list($name, $category, $selected_id=null, $submit_on_change=true, $prepayments=true)
 {
-       $sql = "SELECT terms_indicator, terms, inactive FROM ".TB_PREF."payment_terms";
+       $sql = "SELECT id, terms, inactive FROM ".TB_PREF."payment_terms";
 
        if ($category == PM_CASH) // only cash
-                       $sql .= " WHERE days_before_due=0 AND day_in_following_month=0";
+                       $sql .= " WHERE type=".PTT_CASH;
        elseif ($category == PM_CREDIT) // only delayed payments
-                       $sql .= " WHERE days_before_due".($prepayments ? '!=': '>')."0 OR day_in_following_month!=0";
+                       $sql .= " WHERE type IN(".PTT_FOLLOWING.",".PTT_DAYS.")";
        elseif (!$prepayments)
-                       $sql .= " WHERE days_before_due>=0";
+                       $sql .= " WHERE type<>".PTT_PREPAY;
 
-       return combo_input($name, $selected_id, $sql, 'terms_indicator', 'terms',
+       return combo_input($name, $selected_id, $sql, 'id', 'terms',
                array(
                        'select_submit'=> $submit_on_change,
                        'async' => true
index d59e0af538119136ad26e99d341a39c85521c11a..443a16ed30072648f7520a7a03c210e19fa000a9 100644 (file)
@@ -16,17 +16,16 @@ include_once($path_to_root . "/admin/db/attachments_db.inc");
 
 function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
 {
-       $sql = "SELECT supp.supp_name, terms.terms, terms.days_before_due, supp.dimension_id, supp.dimension2_id,
-               terms.day_in_following_month, supp.tax_included,
+       $sql = "SELECT supp.supp_name, supp.dimension_id, supp.dimension2_id, supp.tax_included,
                supp.tax_group_id, tax_group.name AS tax_group_name,
-               supp.credit_limit - Sum((ov_amount + ov_gst + ov_discount)) as cur_credit, curr_code
-               FROM ".TB_PREF."suppliers supp LEFT JOIN "
-                       .TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id, "
-                       .TB_PREF."payment_terms terms, "
-                       .TB_PREF."tax_groups tax_group
-               WHERE supp.tax_group_id = tax_group.id
-               AND supp.payment_terms=terms.terms_indicator
-               AND supp.supplier_id = ".db_escape($supplier_id)." GROUP BY supp.supp_name";
+               supp.credit_limit - Sum((ov_amount + ov_gst + ov_discount)) as cur_credit,
+               supp.curr_code,
+               terms.*
+               FROM ".TB_PREF."suppliers supp 
+                       LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id
+                       LEFT JOIN ".TB_PREF."payment_terms terms ON supp.payment_terms=terms.id
+                       LEFT JOIN ".TB_PREF."tax_groups tax_group ON supp.tax_group_id = tax_group.id
+               WHERE supp.supplier_id = ".db_escape($supplier_id);
 
        $result = db_query($sql, "The supplier record selected: " . $supplier_id . " cannot be retrieved");
 
@@ -47,9 +46,11 @@ function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
     $supp_trans->supplier_name = $myrow['supp_name'];
        $supp_trans->terms = array( 
                'description' => $myrow['terms'],
-               'days_before_due' => $myrow['days_before_due'], 
-               'day_in_following_month' => $myrow['day_in_following_month'] );
-
+               'type' => $myrow['type'], 
+               'days' => $myrow['days'],
+               'early_discount' => $myrow['early_discount'],
+               'early_days' => $myrow['early_days'],
+       );
        $supp_trans->credit = $myrow['cur_credit'];
 
        $supp_trans->tax_description = $myrow['tax_group_name'];
index 6b74c67c555049fe1e07c2cfdce1e5b6612782bb..444a57de9841cec7839ba55181c3bb7a789b7edb 100644 (file)
@@ -15,14 +15,12 @@ function get_supplier_details_to_order(&$order, $supplier_id)
 {
        $sql = "SELECT curr_code, supp_name, tax_group_id, supp.tax_included, dimension_id, dimension2_id,
                        supp.credit_limit - Sum((ov_amount + ov_gst + ov_discount)) as cur_credit,
-                               terms.terms, terms.days_before_due, terms.day_in_following_month, tg.tax_area
+                               terms.*, tg.tax_area
                FROM ".TB_PREF."suppliers supp
                         LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id
-                        LEFT JOIN ".TB_PREF."payment_terms terms ON supp.payment_terms=terms.terms_indicator
+                        LEFT JOIN ".TB_PREF."payment_terms terms ON supp.payment_terms=terms.id
                         LEFT JOIN ".TB_PREF."tax_groups tg ON supp.tax_group_id=tg.id
-               WHERE supp.supplier_id = ".db_escape($supplier_id)."
-               GROUP BY
-                         supp.supp_name";
+               WHERE supp.supplier_id = ".db_escape($supplier_id);
 
        $result = db_query($sql, "The supplier details could not be retreived");
        $myrow = db_fetch($result);
@@ -30,8 +28,11 @@ function get_supplier_details_to_order(&$order, $supplier_id)
        $order->credit = $myrow["cur_credit"];
        $order->terms = array( 
                'description' => $myrow['terms'],
-               'days_before_due' => $myrow['days_before_due'], 
-               'day_in_following_month' => $myrow['day_in_following_month'] );
+               'type' => $myrow['type'], 
+               'days' => $myrow['days'],
+               'early_discount' => $myrow['early_discount'],
+               'early_days' => $myrow['early_days'],
+       );
 
        $_POST['supplier_id'] = $supplier_id;
        $_POST['supplier_name'] = $myrow["supp_name"];
index 2648ff60e0a635667a1e009157d82bfdad5ff74f..3a4dfe0d4534f3e6fc804ca25b61b117f1032ee2 100644 (file)
@@ -114,19 +114,13 @@ function get_supplier_details($supplier_id, $to=null, $all=true)
                supp.tax_group_id
 
                FROM ".TB_PREF."suppliers supp
-                        LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id AND trans.tran_date <= '$todate',
-                        ".TB_PREF."payment_terms
-
+                        LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id AND trans.tran_date <= '$todate'
+                        LEFT JOIN ".TB_PREF."payment_terms ON supp.payment_terms = ".TB_PREF."payment_terms.id
                WHERE
-                        supp.payment_terms = ".TB_PREF."payment_terms.terms_indicator
-                        AND supp.supplier_id = $supplier_id ";
+                        supp.supplier_id = $supplier_id ";
        if (!$all)
-               $sql .= "AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) - trans.alloc > ".FLOAT_COMP_DELTA." ";  
-       $sql .= "GROUP BY
-                         supp.supp_name,
-                         ".TB_PREF."payment_terms.terms,
-                         ".TB_PREF."payment_terms.days_before_due,
-                         ".TB_PREF."payment_terms.day_in_following_month";
+               $sql .= "AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_discount) - trans.alloc > ".FLOAT_COMP_DELTA." ";
+
     $result = db_query($sql,"The customer details could not be retrieved");
     $supp = db_fetch($result);
 
index 2e9520e708db3103a68b5aacf833e1e1fb639ace..1b7fb663778ad1142bd6ed7003880e978d7e75f4 100644 (file)
@@ -583,14 +583,14 @@ function get_duedate_from_terms(&$trans)
        {
                $date = Today();
        }
-       if ($trans->terms['day_in_following_month'])
+       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["day_in_following_month"]);
+                       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_before_due"]);
+               $trans->due_date = add_days($date, $trans->terms["days"]);
        }
 }
 
index e1bcd635549e5a1434a5e82edbeaa83182dc9e0a..08a8471b6b2b86d08b5046ab6046a92253278e80 100644 (file)
@@ -39,7 +39,7 @@
                $row = get_payment_terms($this->formData['payment_terms']);
                $Payment_Terms = _("Payment Terms") . ': ' . $row["terms"];
                if ($this->formData['doctype'] == ST_SALESINVOICE && $this->formData['prepaid'])
-                       $this->formData['prepaid'] = ($row['days_before_due'] >= 0) ? 'final' : 'partial';
+                       $this->formData['prepaid'] = ($row['type'] != PTT_PREPAY) ? 'final' : 'partial';
        }
 
 
index 2a5c7bb089094d963f76abbe7e16accbe79c4fe6..112b3447e7bda08ba7df8c075903bc2eba9740f6 100644 (file)
@@ -111,7 +111,7 @@ function print_payment_report()
                WHERE ";
        if ($fromsupp != ALL_TEXT)
                $sql .= "supplier_id=".db_escape($fromsupp)." AND ";
-       $sql .= "".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
+       $sql .= "".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.id
                ORDER BY supp_name";
        $result = db_query($sql, "The customers could not be retrieved");
 
index 20af722a3ce0603ee77064c449cb4af573231b80..2bc41391938c720dae8a96b6f3bc547ae3ff056e 100644 (file)
@@ -341,7 +341,7 @@ function check_data()
                        return false;
                }
        } else {
-               if (($_SESSION['Items']->payment_terms['days_before_due'] == -1) && !count($_SESSION['Items']->prepayments)) {
+               if (($_SESSION['Items']->payment_terms['type'] == PTT_PREPAY) && !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;
                }
@@ -385,7 +385,7 @@ if(list_updated('payment')) {
        $_POST['Comments'] = '';
        $Ajax->activate('due_date');
        $Ajax->activate('options');
-       if ($order->payment_terms['cash_sale']) {
+       if ($order->payment_terms['type'] == PTT_CASH) {
                $_POST['Location'] = $order->Location = $order->pos['pos_location'];
                $order->location_name = $order->pos['location_name'];
        }
index 660fcc28e59743b29f2987a46fe72050f346e1df..6f1d3a7983b0aa0ebfbf2e9e116edec461013e6a 100644 (file)
@@ -354,7 +354,7 @@ class Cart
                $this->payment_terms = get_payment_terms($payment);
                $this->cash_discount = $cdiscount;
 
-               if ($this->payment_terms['cash_sale']) {
+               if ($this->payment_terms['type'] == PTT_CASH) {
                        $this->Location = $this->pos['pos_location'];
                        $this->location_name = $this->pos['location_name'];
                }
@@ -645,7 +645,7 @@ class Cart
                $remainder = prepaid_invoice_remainder($this->order_no);
 
                // recalculate prepaid part from payments
-               if ($this->payment_terms['days_before_due'] == -1)
+               if ($this->payment_terms['type'] == PTT_PREPAY)
                {       // this is partial invoice for selected prepayments made.
                        $paid = 0;
                        foreach($this->prepayments as $payment)
index 3191e8a676cefc2a516bcebbaacc8104009f8b20..0e47a97481a05d0a104d6b0cc1753309b240829f 100644 (file)
@@ -73,7 +73,7 @@ function get_customer_details($customer_id, $to=null, $all=true)
        $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)";
+       $sign = "IF(trans.`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, 1)";
        if ($all)
        $value = "IFNULL($sign*(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount),0)";
     else               
@@ -87,12 +87,11 @@ function get_customer_details($customer_id, $to=null, $all=true)
                                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.","
-                                .TB_PREF."payment_terms terms,"
+                                LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.tran_date <= '$todate' AND debtor.debtor_no = trans.debtor_no AND trans.type <> ".ST_CUSTDELIVERY."
+                                LEFT JOIN ".TB_PREF."payment_terms terms ON debtor.payment_terms = terms.id,"
                                 .TB_PREF."credit_status credit_status
                        WHERE
-                                       debtor.payment_terms = terms.terms_indicator
-                               AND debtor.credit_status = credit_status.id";
+                               debtor.credit_status = credit_status.id";
         if ($customer_id)
                $sql .= " AND debtor.debtor_no = ".db_escape($customer_id);
 
@@ -100,9 +99,6 @@ function get_customer_details($customer_id, $to=null, $all=true)
                $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 .= " GROUP BY
                                debtor.name,
-                               terms.terms,
-                               terms.days_before_due,
-                               terms.day_in_following_month,
                                debtor.credit_limit,
                                credit_status.dissallow_invoices,
                                credit_status.reason_description";
index 66a6a87f40581aefea23b568fd4426f62ff36f1d..5529d0c28fd0544fa7c86ea6e8e48185a6e200ee 100644 (file)
@@ -177,7 +177,7 @@ function check_sales_order_type($order_no)
 {
        $myrow = get_sales_order_header($order_no, ST_SALESORDER);
 
-       return !$myrow['prepaid'];
+       return !$myrow['prepaid'] && ! $myrow['cash_sale'];
 }
 
 function create_template_invoice($customer_id, $branch_id, $order_no, $tmpl_no, $date, $from, $to, $memo)
@@ -210,7 +210,6 @@ function create_template_invoice($customer_id, $branch_id, $order_no, $tmpl_no,
        $cart = $doc;
        $cart->trans_type = ST_SALESINVOICE;
        $cart->reference = $Refs->get_next($cart->trans_type);
-       $cart->payment_terms['cash_sale'] = false; // no way to register cash payment with recurrent invoice at once
 
        $invno = $cart->write(1);
 
index 22d34e8a646537848ed67d652f815137ac1b1371..9b9ab467ed7eea9b4dd89990929fd19b23bc8b4a 100644 (file)
@@ -184,7 +184,7 @@ function write_sales_invoice(&$invoice)
 
        if ($trans_no == 0) {
                $Refs->save(ST_SALESINVOICE, $invoice_no, $invoice->reference, null, $invoice->fixed_asset);
-               if ($invoice->payment_terms['cash_sale'] && $invoice->pos['pos_account']) {
+               if ($invoice->payment_terms['type'] == PTT_CASH && $invoice->pos['pos_account']) {
                        $amount = $items_total + $items_added_tax + $invoice->freight_cost 
                                + $freight_added_tax;
 
@@ -296,9 +296,9 @@ function prepaid_invoice_remainder($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.id"
+                       ." AND inv.payment_terms=pt.id"
+                       ." AND pt.type = ".PTT_PREPAY;
 
        $result = db_fetch(db_query($sql, "cannot find final invoice value"));
        return $result[0] ? $result[0] : 0;
index e76ea3eee2c60b563bb4e288dee218fb3b2f6eae..3296c5917c0e402e8b4cfc0e50948aa951d5faa0 100644 (file)
@@ -237,15 +237,17 @@ function get_sales_order_header($order_no, $trans_type)
          cust.tax_id,
          sorder.alloc,
          IFNULL(allocs.ord_allocs, 0)+IFNULL(inv.inv_allocs ,0) AS sum_paid,
-         sorder.prep_amount>0 as prepaid
+         sorder.prep_amount>0 as prepaid,
+         terms.type=".PTT_CASH." as cash_sale
        FROM ".TB_PREF."sales_orders sorder
                        LEFT JOIN (SELECT trans_no_to, sum(amt) ord_allocs FROM ".TB_PREF."cust_allocations
-                               WHERE trans_type_to=".ST_SALESORDER." AND trans_no_to=".db_escape($order_no)." GROUP BY trans_no_to)
-                                allocs ON sorder.trans_type=".ST_SALESORDER." AND allocs.trans_no_to=sorder.order_no
+                               WHERE trans_type_to=".ST_SALESORDER." AND trans_no_to=".db_escape($order_no)." GROUP BY trans_no_to) allocs 
+                                       ON sorder.trans_type=".ST_SALESORDER." AND allocs.trans_no_to=sorder.order_no
                        LEFT JOIN (SELECT order_, sum(alloc) inv_allocs FROM ".TB_PREF."debtor_trans 
                                WHERE type=".ST_SALESINVOICE." AND order_=".db_escape($order_no)."  GROUP BY order_)
                                 inv ON sorder.trans_type=".ST_SALESORDER." AND inv.order_=sorder.order_no
-                       LEFT JOIN ".TB_PREF."stock_master ship ON ship.stock_id = sorder.ship_via,"
+                       LEFT JOIN ".TB_PREF."stock_master ship ON ship.stock_id = sorder.ship_via
+                       LEFT JOIN ".TB_PREF."payment_terms terms ON sorder.payment_terms=terms.id,"
          .TB_PREF."debtors_master cust,"
          .TB_PREF."sales_types stype, "
          .TB_PREF."tax_groups tax_group, "
@@ -393,10 +395,10 @@ function get_invoice_duedate($terms, $invdate)
        if (!$myrow)
                return $invdate;
 
-       if ($myrow['day_in_following_month'] > 0)
-               $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
-       elseif ($myrow['days_before_due'] > 0)
-               $duedate = add_days($invdate, $myrow['days_before_due']);
+       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;
index d5e5600eaf023884f7cde538460d20cf91fb343b..49c39b873f86efe35c083f97574a8772a62e1f62 100644 (file)
@@ -125,7 +125,7 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
        elseif ($order->trans_type == ST_SALESQUOTE)
                $order->due_date = add_days($order->document_date, $SysPrefs->default_quote_valid_days());
 
-       if($order->payment_terms['cash_sale']) {
+       if($order->payment_terms['type'] == PTT_CASH) {
                $order->set_location($order->pos["pos_location"], $order->pos["location_name"]);
        } else
                $order->set_location($myrow["default_location"], $myrow["location_name"]);
@@ -309,8 +309,8 @@ function display_order_header(&$order, $editable, $date_text)
 
                                if (!in_array($order->trans_type, array(ST_SALESQUOTE, ST_SALESORDER))
                                        && ($order->pos['cash_sale'] != $order->pos['credit_sale'])
-                                       && (($order->payment_terms['cash_sale'] && !$order->pos['cash_sale']) ||
-                                               (!$order->payment_terms['cash_sale'] && !$order->pos['credit_sale']))) {
+                                       && (($order->payment_terms['type'] == PTT_CASH && !$order->pos['cash_sale']) ||
+                                               (!$order->payment_terms['type'] != PTT_CASH && !$order->pos['credit_sale']))) {
                                                        // force payment terms refresh if terms are editable
                                                        // and pos have no permitions for terms selected in customer record.
                                                        // Terms are set to first terms in allowed category below.
@@ -388,7 +388,7 @@ function display_order_header(&$order, $editable, $date_text)
                        $order->payment = get_post('payment');
                        $order->payment_terms = get_payment_terms($order->payment);
                        $order->due_date = get_invoice_duedate($order->payment, $order->document_date);
-                       if ($order->payment_terms['cash_sale']) {
+                       if ($order->payment_terms['type'] == PTT_CASH) {
                                $_POST['Location'] = $order->Location = $order->pos['pos_location'];
                                $order->location_name = $order->pos['location_name'];
                        }
@@ -589,7 +589,7 @@ function display_delivery_details(&$order)
        div_start('delivery');  
 
 
-       if ($order->payment_terms['cash_sale']) {       // Direct payment sale
+       if ($order->payment_terms['type'] == PTT_CASH) {        // Direct payment sale
                $Ajax->activate('items_table');
                display_heading(_('Cash payment'));
                start_table(TABLESTYLE2, "width='60%'");
@@ -627,7 +627,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'] == -1)
+               if ($order->payment_terms['type'] == PTT_PREPAY)
                {
                        $Ajax->addUpdate('items_table', 'prep_amount', price_format($order->get_trans_total())); // bind to items_table update
                        if (!$order->is_started())
index a604af695b2c3b10836fe9613ca55c025d8a0d1b..663bcee718b6bc2d2e720c924a6d6be47fa38890 100644 (file)
@@ -271,7 +271,7 @@ function copy_to_cart()
                $cart->payment_terms = get_payment_terms($_POST['payment']);
                $newpayment = true;
        }
-       if ($cart->payment_terms['cash_sale']) {
+       if ($cart->payment_terms['type'] == PTT_CASH) {
                if ($newpayment) 
                {
                        $cart->due_date = $cart->document_date;
@@ -390,8 +390,8 @@ function can_process() {
                display_error(_("This document cannot be processed because there is insufficient quantity for items marked."));
                return false;
        }
-       if ($_SESSION['Items']->payment_terms['cash_sale'] == 0) {
-               if (!$_SESSION['Items']->is_started() && ($_SESSION['Items']->payment_terms['days_before_due'] == -1) && ((input_num('prep_amount')<=0) ||
+       if ($_SESSION['Items']->payment_terms['type'] != PTT_CASH) {
+               if (!$_SESSION['Items']->is_started() && ($_SESSION['Items']->payment_terms['type'] == PTT_PREPAY) && ((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');
@@ -452,7 +452,7 @@ function can_process() {
                return false;
        }
 
-       if ($_SESSION['Items']->payment_terms['cash_sale'] && 
+       if ($_SESSION['Items']->payment_terms['type'] == PTT_CASH && 
                ($_SESSION['Items']->trans_type == ST_CUSTDELIVERY || $_SESSION['Items']->trans_type == ST_SALESINVOICE)) 
                $_SESSION['Items']->due_date = $_SESSION['Items']->document_date;
        return true;
index ead1bd3a4a30193a10c9b012cea682c679406ead..3cbd78dfd7284b6b88e30b67dd954af9ba21240e 100644 (file)
@@ -42,7 +42,7 @@ $branch = get_branch($myrow["branch_code"]);
 $sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
 
 display_heading(sprintf($myrow['prep_amount'] > 0 ? (
-       $paym['days_before_due']>=0 ? _("FINAL INVOICE #%d") : _("PREPAYMENT INVOICE #%d")) : _("SALES INVOICE #%d"),$trans_id));
+       $paym['type'] == PTT_PREPAY ? _("FINAL INVOICE #%d") : _("PREPAYMENT INVOICE #%d")) : _("SALES INVOICE #%d"),$trans_id));
 
 echo "<br>";
 start_table(TABLESTYLE2, "width='95%'");
index aab384b7c8c84f9d1bbe7479e8673627371f9ac6..6e51b6601574e90094121a716c64be623bc83b31 100644 (file)
@@ -78,7 +78,7 @@ label_cells(_("Deliver From Location"), $_SESSION['View']->location_name, "class
 end_row();
 
 
-if ($_SESSION['View']->payment_terms['days_before_due']<0)
+if ($_SESSION['View']->payment_terms['type'] == PTT_PREPAY)
 {
 start_row();
 label_cells(_("Payment Terms"), $_SESSION['View']->payment_terms['terms'], "class='tableheader2'");
index 3994822e62871774c571db0cb834e92110b78e01..dc3a8f619f98d30c3366552c5a857e41448921ec 100644 (file)
@@ -71,3 +71,13 @@ CREATE TABLE `1_db_trail` (
                `data` text DEFAULT NULL,
        PRIMARY KEY (`id`)
        ) ENGINE=MyISAM;
+
+# payment terms normalization, early payment support
+ALTER TABLE `0_payment_terms` CHANGE COLUMN `terms_indicator` `id` tinyint(1) NOT NULL DEFAULT '0';
+ALTER TABLE `0_payment_terms` ADD COLUMN `type` tinyint(1) NOT NULL DEFAULT '0' AFTER `terms`;
+UPDATE `0_payment_terms` SET `type`=IF(days_before_due < 0, 1, IF(day_in_following_month>0, 4, IF(days_before_due=0, 2, 3)));
+UPDATE `0_payment_terms` SET days_before_due=day_in_following_month WHERE days_before_due<=0;
+ALTER TABLE `0_payment_terms` CHANGE COLUMN `days_before_due` `days` int(11) NOT NULL DEFAULT '0';
+ALTER TABLE `0_payment_terms` DROP COLUMN `day_in_following_month`;
+ALTER TABLE `0_payment_terms` ADD COLUMN `early_discount` double NOT NULL DEFAULT '0' AFTER `days`;
+ALTER TABLE `0_payment_terms` ADD COLUMN `early_days` int(11) NOT NULL DEFAULT '0' AFTER `early_discount`;
index b62e9675d6319bf1270d7fe6a92117d6e4776734..94496c80598a6beb74960f11ec5c434a57c3c2ac 100644 (file)
@@ -1079,23 +1079,25 @@ INSERT INTO `0_locations` VALUES
 DROP TABLE IF EXISTS `0_payment_terms`;
 
 CREATE TABLE `0_payment_terms` (
-  `terms_indicator` int(11) NOT NULL AUTO_INCREMENT,
+  `id` int(11) NOT NULL AUTO_INCREMENT,
   `terms` char(80) NOT NULL DEFAULT '',
-  `days_before_due` smallint(6) NOT NULL DEFAULT '0',
-  `day_in_following_month` smallint(6) NOT NULL DEFAULT '0',
+  `type` tinyint(1) NOT NULL DEFAULT '1'
+  `days` int(11) NOT NULL DEFAULT '0'
+  `early_discount` double NOT NULL DEFAULT '0'
+  `early_days` double NOT NULL DEFAULT '0',
   `inactive` tinyint(1) NOT NULL DEFAULT '0',
-  PRIMARY KEY (`terms_indicator`),
+  PRIMARY KEY (`id`),
   UNIQUE KEY `terms` (`terms`)
 ) ENGINE=InnoDB AUTO_INCREMENT=6 ;
 
 -- Data of table `0_payment_terms` --
 
 INSERT INTO `0_payment_terms` VALUES
-('1', 'Due 15th Of the Following Month', '0', '17', '0'),
-('2', 'Due By End Of The Following Month', '0', '30', '0'),
-('3', 'Payment due within 10 days', '10', '0', '0'),
-('4', 'Cash Only', '0', '0', '0'),
-('5', 'Prepaid', '-1', '0', '0');
+('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'),
+('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');
 
 -- Structure of table `0_prices` --
 
index 9e22d3cc2579799fb7b41e0d5c3c47057c59232e..9d25c8b8d81f9d8d300842bc3f198741d5c9f16b 100644 (file)
@@ -863,23 +863,25 @@ INSERT INTO `0_locations` VALUES
 DROP TABLE IF EXISTS `0_payment_terms`;
 
 CREATE TABLE `0_payment_terms` (
-  `terms_indicator` int(11) NOT NULL AUTO_INCREMENT,
+  `id` int(11) NOT NULL AUTO_INCREMENT,
   `terms` char(80) NOT NULL DEFAULT '',
-  `days_before_due` smallint(6) NOT NULL DEFAULT '0',
-  `day_in_following_month` smallint(6) NOT NULL DEFAULT '0',
+  `type` tinyint(1) NOT NULL DEFAULT '1'
+  `days` int(11) NOT NULL DEFAULT '0'
+  `early_discount` double NOT NULL DEFAULT '0'
+  `early_days` double NOT NULL DEFAULT '0',
   `inactive` tinyint(1) NOT NULL DEFAULT '0',
-  PRIMARY KEY (`terms_indicator`),
+  PRIMARY KEY (`id`),
   UNIQUE KEY `terms` (`terms`)
 ) ENGINE=InnoDB AUTO_INCREMENT=6 ;
 
 -- Data of table `0_payment_terms` --
 
 INSERT INTO `0_payment_terms` VALUES
-('1', 'Due 15th Of the Following Month', '0', '17', '0'),
-('2', 'Due By End Of The Following Month', '0', '30', '0'),
-('3', 'Payment due within 10 days', '10', '0', '0'),
-('4', 'Cash Only', '0', '0', '0'),
-('5', 'Prepaid', '-1', '0', '0');
+('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'),
+('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');
 
 -- Structure of table `0_prices` --