From: Janusz Dobrowolski Date: Sun, 28 Jul 2019 14:35:35 +0000 (+0200) Subject: Payment_terms table normalization. X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;h=0e9e4cf4f01431531dfed299bb501955bb0818ed;hp=89e8ca2be9235215ac2e2bf5e58d7eedd2dda84d;p=fa-stable.git Payment_terms table normalization. --- diff --git a/admin/db/company_db.inc b/admin/db/company_db.inc index 8e788532..b774d2bc 100644 --- a/admin/db/company_db.inc +++ b/admin/db/company_db.inc @@ -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"); diff --git a/admin/payment_terms.php b/admin/payment_terms.php index 7c7afeff..1412c372 100644 --- a/admin/payment_terms.php +++ b/admin/payment_terms.php @@ -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); } diff --git a/includes/sysnames.inc b/includes/sysnames.inc index 1d7df6f0..8d3bf78f 100644 --- a/includes/sysnames.inc +++ b/includes/sysnames.inc @@ -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") diff --git a/includes/types.inc b/includes/types.inc index f83f2022..da2b3395 100644 --- a/includes/types.inc +++ b/includes/types.inc @@ -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); diff --git a/includes/ui/ui_lists.inc b/includes/ui/ui_lists.inc index b803ac32..15cfe2e9 100644 --- a/includes/ui/ui_lists.inc +++ b/includes/ui/ui_lists.inc @@ -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 diff --git a/purchasing/includes/db/invoice_db.inc b/purchasing/includes/db/invoice_db.inc index d59e0af5..443a16ed 100644 --- a/purchasing/includes/db/invoice_db.inc +++ b/purchasing/includes/db/invoice_db.inc @@ -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']; diff --git a/purchasing/includes/db/po_db.inc b/purchasing/includes/db/po_db.inc index 6b74c67c..444a57de 100644 --- a/purchasing/includes/db/po_db.inc +++ b/purchasing/includes/db/po_db.inc @@ -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"]; diff --git a/purchasing/includes/db/suppliers_db.inc b/purchasing/includes/db/suppliers_db.inc index 2648ff60..3a4dfe0d 100644 --- a/purchasing/includes/db/suppliers_db.inc +++ b/purchasing/includes/db/suppliers_db.inc @@ -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); diff --git a/purchasing/includes/ui/invoice_ui.inc b/purchasing/includes/ui/invoice_ui.inc index 2e9520e7..1b7fb663 100644 --- a/purchasing/includes/ui/invoice_ui.inc +++ b/purchasing/includes/ui/invoice_ui.inc @@ -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"]); } } diff --git a/reporting/includes/doctext.inc b/reporting/includes/doctext.inc index e1bcd635..08a8471b 100644 --- a/reporting/includes/doctext.inc +++ b/reporting/includes/doctext.inc @@ -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'; } diff --git a/reporting/rep203.php b/reporting/rep203.php index 2a5c7bb0..112b3447 100644 --- a/reporting/rep203.php +++ b/reporting/rep203.php @@ -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"); diff --git a/sales/customer_invoice.php b/sales/customer_invoice.php index 20af722a..2bc41391 100644 --- a/sales/customer_invoice.php +++ b/sales/customer_invoice.php @@ -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']; } diff --git a/sales/includes/cart_class.inc b/sales/includes/cart_class.inc index 660fcc28..6f1d3a79 100644 --- a/sales/includes/cart_class.inc +++ b/sales/includes/cart_class.inc @@ -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) diff --git a/sales/includes/db/customers_db.inc b/sales/includes/db/customers_db.inc index 3191e8a6..0e47a974 100644 --- a/sales/includes/db/customers_db.inc +++ b/sales/includes/db/customers_db.inc @@ -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"; diff --git a/sales/includes/db/recurrent_invoices_db.inc b/sales/includes/db/recurrent_invoices_db.inc index 66a6a87f..5529d0c2 100644 --- a/sales/includes/db/recurrent_invoices_db.inc +++ b/sales/includes/db/recurrent_invoices_db.inc @@ -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); diff --git a/sales/includes/db/sales_invoice_db.inc b/sales/includes/db/sales_invoice_db.inc index 22d34e8a..9b9ab467 100644 --- a/sales/includes/db/sales_invoice_db.inc +++ b/sales/includes/db/sales_invoice_db.inc @@ -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; diff --git a/sales/includes/db/sales_order_db.inc b/sales/includes/db/sales_order_db.inc index e76ea3ee..3296c591 100644 --- a/sales/includes/db/sales_order_db.inc +++ b/sales/includes/db/sales_order_db.inc @@ -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; diff --git a/sales/includes/ui/sales_order_ui.inc b/sales/includes/ui/sales_order_ui.inc index d5e5600e..49c39b87 100644 --- a/sales/includes/ui/sales_order_ui.inc +++ b/sales/includes/ui/sales_order_ui.inc @@ -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()) diff --git a/sales/sales_order_entry.php b/sales/sales_order_entry.php index a604af69..663bcee7 100644 --- a/sales/sales_order_entry.php +++ b/sales/sales_order_entry.php @@ -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; diff --git a/sales/view/view_invoice.php b/sales/view/view_invoice.php index ead1bd3a..3cbd78df 100644 --- a/sales/view/view_invoice.php +++ b/sales/view/view_invoice.php @@ -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 "
"; start_table(TABLESTYLE2, "width='95%'"); diff --git a/sales/view/view_sales_order.php b/sales/view/view_sales_order.php index aab384b7..6e51b660 100644 --- a/sales/view/view_sales_order.php +++ b/sales/view/view_sales_order.php @@ -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'"); diff --git a/sql/alter2.5.sql b/sql/alter2.5.sql index 3994822e..dc3a8f61 100644 --- a/sql/alter2.5.sql +++ b/sql/alter2.5.sql @@ -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`; diff --git a/sql/en_US-demo.sql b/sql/en_US-demo.sql index b62e9675..94496c80 100644 --- a/sql/en_US-demo.sql +++ b/sql/en_US-demo.sql @@ -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` -- diff --git a/sql/en_US-new.sql b/sql/en_US-new.sql index 9e22d3cc..9d25c8b8 100644 --- a/sql/en_US-new.sql +++ b/sql/en_US-new.sql @@ -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` --