Early payment discount option moved from branch to payment terms.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Tue, 30 Jul 2019 11:48:24 +0000 (13:48 +0200)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 19 Aug 2019 10:41:58 +0000 (12:41 +0200)
20 files changed:
admin/payment_terms.php
includes/ui/allocation_cart.inc
includes/ui/ui_input.inc
js/allocate.js
js/payalloc.js
purchasing/includes/db/suppalloc_db.inc
purchasing/includes/ui/invoice_ui.inc
purchasing/includes/ui/po_ui.inc
sales/allocations/customer_allocation_main.php
sales/customer_payments.php
sales/includes/cart_class.inc
sales/includes/db/custalloc_db.inc
sales/includes/db/customers_db.inc
sales/includes/db/sales_invoice_db.inc
sales/includes/db/sales_order_db.inc
sales/includes/sales_db.inc
sales/includes/ui/sales_order_ui.inc
sales/manage/customers.php
sql/en_US-demo.sql
sql/en_US-new.sql

index 1412c372c90c8bc5bb3b6b8b3542c2dc8de87cb6..56f6652c432af93f60eb1f33f8d1784e5bf6a943 100644 (file)
@@ -38,21 +38,36 @@ if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
                display_error( _("The Terms description must be entered."));
                set_focus('terms');
        }
-
-       if ($_POST['DayNumber'] == '')
-               $_POST['DayNumber'] = 0;
+       $early_days = input_num('early_days', 0);
+       if ($early_days) {
+               if ($early_days >= $_POST['DayNumber']) {
+                       $input_error = 1;
+                       display_error(_("Early payment days option should be shorter that payment term days."));
+                       set_focus('early_days');
+               } else if (!check_num('early_discount', 0, 100) or input_num('early_discount') == 0)
+               {
+                       $input_error = 1;
+                       display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
+                       set_focus('early_discount');
+               } 
+       } else {
+               if (input_num('early_discount', 0)) {
+                       $input_error = 1;
+                       display_error(_("Early payment days option should be positive and less than payment term days."));
+                       set_focus('early_days');
+               }
+       }
 
        if ($input_error != 1)
        {
-
        if ($selected_id != -1) 
        {
-               update_payment_terms($selected_id, get_post('terms'), get_post('type'), input_num('DayNumber', 0)); 
+               update_payment_terms($selected_id, get_post('terms'), get_post('type'), input_num('DayNumber', 0), input_num('early_discount')/100, $early_days); 
                        $note = _('Selected payment terms have been updated');
        } 
        else 
        {
-                       add_payment_terms(get_post('terms'), get_post('type'), input_num('DayNumber', 0));
+                       add_payment_terms(get_post('terms'), get_post('type'), input_num('DayNumber', 0), input_num('early_discount')/100, $early_days);
                        $note = _('New payment terms have been added');
        }
        //run the sql from either of the above possibilites
@@ -98,7 +113,7 @@ $result = get_payment_terms_all(check_value('show_inactive'));
 
 start_form();
 start_table(TABLESTYLE);
-$th = array(_("Description"), _("Type"), _("Due After/Days"), "", "");
+$th = array(_("Description"), _("Type"), _("Due After/Days"), _("Early payment discount"),"", "");
 inactive_control_column($th);
 table_header($th);
 
@@ -106,10 +121,13 @@ $k = 0; //row colour counter
 while ($myrow = db_fetch($result)) 
 {
        $days = $myrow['days'];
+       $percent = $myrow['early_discount'];
+
        alt_table_row_color($k);
     label_cell($myrow["terms"]);
     label_cell($pterm_types[$myrow['type']]);
     label_cell($myrow['type'] == PTT_DAYS ? "$days "._("days") : ($myrow['type'] == PTT_FOLLOWING ? $days : _("N/A")));
+    label_cell(in_array($myrow['type'], array(PTT_FOLLOWING, PTT_DAYS)) ? ($percent==0 ? _("None") : ($percent*100).'/'.$myrow['early_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"));
@@ -138,6 +156,8 @@ if ($selected_id != -1)
                $_POST['terms']  = $myrow["terms"];
                $_POST['type'] = $myrow['type'];
                $_POST['DayNumber'] = $myrow['days'];
+               $_POST['early_discount'] = $myrow['early_discount']*100;
+               $_POST['early_days'] = $myrow['early_days'];
        }
        hidden('selected_id', $selected_id);
 }
@@ -146,9 +166,11 @@ text_row(_("Terms Description:"), 'terms', null, 40, 40);
 
 payment_type_list_row(_("Payment type:"), 'type', null, true);
 
-if ( in_array(get_post('type'), array(PTT_FOLLOWING, PTT_DAYS))) 
+if ( in_array(get_post('type'), array(PTT_FOLLOWING, PTT_DAYS))) {
        text_row_ex(_("Days (Or Day In Following Month):"), 'DayNumber', 3);
-else
+       small_amount_row(_("Days of early payment discount option:"), 'early_days', null, null, _('days'), 0);
+       small_amount_row(_("Early payment discount percent:"), 'early_discount', null, null, _('%'), 1);
+} else
        hidden('DayNumber', 0);
 
 end_table(1);
@@ -159,4 +181,3 @@ submit_add_or_update_center($selected_id == -1, '', 'both');
 end_form();
 
 end_page();
-
index b45110b35ade07d366eb4a8622f81e7d9b93ee0f..0596e2b2e2838d6c004ea677154dafc5a9ec938e 100644 (file)
@@ -52,12 +52,12 @@ class allocation
        }
 
        function add_item($type, $type_no, $date_, $due_date, $amount, $amount_allocated, 
-               $current_allocated, $ref)
+               $current_allocated, $ref, $early_discount=0, $early_days=0)
        {
                if (floatcmp($amount, 0))
                {
                        $this->allocs[count($this->allocs)] = new allocation_item($type, $type_no, 
-                               $date_, $due_date, $amount, $amount_allocated, $current_allocated, $ref);
+                               $date_, $due_date, $amount, $amount_allocated, $current_allocated, $ref, $early_discount, $early_days);
                        return true;
                } 
                else 
@@ -67,12 +67,12 @@ class allocation
        }
        
        function update_item($index, $type, $type_no, $date_, $due_date, 
-               $amount, $amount_allocated, $current_allocated, $ref)
+               $amount, $amount_allocated, $current_allocated, $ref, $early_discount=0, $early_days=0)
        {
                if (floatcmp($amount, 0))
                {
                        $this->allocs[$index] = new allocation_item($type, $type_no, 
-                               $date_, $due_date, $amount, $amount_allocated, $current_allocated, $ref);
+                               $date_, $due_date, $amount, $amount_allocated, $current_allocated, $ref, $early_discount, $early_days);
                        return true;
                } 
                else 
@@ -82,7 +82,7 @@ class allocation
        }
        
        function add_or_update_item($type, $type_no, $date_, $due_date, 
-               $amount, $amount_allocated, $current_allocated, $ref)
+               $amount, $amount_allocated, $current_allocated, $ref, $early_discount=0, $early_days=0)
        {
                for ($i = 0; $i < count($this->allocs); $i++) 
                {
@@ -90,11 +90,11 @@ class allocation
                        if (($item->type == $type) && ($item->type_no == $type_no)) 
                        {
                                return $this->update_item($i, $type, $type_no, $date_, $due_date, 
-                                       $amount, $amount_allocated, $current_allocated, $ref);
+                                       $amount, $amount_allocated, $current_allocated, $ref, $early_discount, $early_days);
                        }
                }
         return $this->add_item($type, $type_no, $date_, $due_date, 
-               $amount, $amount_allocated, $current_allocated, $ref);
+               $amount, $amount_allocated, $current_allocated, $ref, $early_discount, $early_days);
        }
 
        //
@@ -152,8 +152,8 @@ class allocation
                                        sql2date($myrow["due_date"]),
                                        $myrow["Total"], // trans total
                                        $myrow["alloc"], // trans total allocated
-                                       0,
-                                       $myrow["reference"]); // this allocation
+                                       0,                               // this allocation
+                                       $myrow["reference"], $myrow["early_discount"], $myrow["early_days"]);
                        }
                }
                if ($this->trans_no == 0) return; // this is new payment
@@ -175,7 +175,7 @@ class allocation
                                sql2date($myrow["tran_date"]),
                                sql2date($myrow["due_date"]),
                                $myrow["Total"],
-                               $myrow["alloc"] - $myrow["amt"], $myrow["amt"], $myrow["reference"]);
+                               $myrow["alloc"] - $myrow["amt"], $myrow["amt"], $myrow["reference"], $myrow["early_discount"], $myrow["early_days"]);
                }
        }
        //
@@ -210,10 +210,11 @@ class allocation
                                } else {
                                        add_cust_allocation($amount,
                                                $this->type, $this->trans_no,
-                                       $alloc_item->type, $alloc_item->type_no, $this->person_id, $this->date_);
+                                       $alloc_item->type, $alloc_item->type_no, $this->person_id, $this->date_);
 
                                        update_debtor_trans_allocation($alloc_item->type, $alloc_item->type_no, $this->person_id);
                                }
+
                                // Exchange Variations Joe Hunt 2008-09-20 ////////////////////
                                if ($alloc_item->type != ST_SALESORDER && !@$no_exchange_variations
                                        && $alloc_item->type != ST_PURCHORDER && $alloc_item->type != ST_JOURNAL && $this->type != ST_JOURNAL)
@@ -253,9 +254,11 @@ class allocation_item
        var $ref;
        
        var $current_allocated;
+       var $early_discount;    // nominal early payment discount according to payment terms
+       var $early_days;        // days for early payment
        
        function __construct($type, $type_no, $date_, $due_date, $amount, 
-               $amount_allocated, $current_allocated, $ref)
+               $amount_allocated, $current_allocated, $ref, $early_discount=0, $early_days=0)
        {
 
                $this->type = $type;
@@ -269,16 +272,18 @@ class allocation_item
                $this->amount = $amount;
                $this->amount_allocated = $amount_allocated;
                $this->current_allocated = $current_allocated;
+               $this->early_discount = $early_discount;
+               $this->early_days = $early_days;
        }
 }
 
 //--------------------------------------------------------------------------------
 
-function show_allocatable($show_totals) {
+function show_allocatable($reallocation) {
 
        global $systypes_array;
        
-    $k = $total_allocated = 0;
+    $k = $total_allocated = $total_discount = 0;
 
        $cart = $_SESSION['alloc'];
        $supp_ref = in_array($cart->type, array(ST_SUPPCREDIT, ST_SUPPAYMENT, ST_BANKPAYMENT));
@@ -288,14 +293,14 @@ function show_allocatable($show_totals) {
                display_heading(sprintf(_("Allocated amounts in %s:"), $cart->person_curr));
                start_table(TABLESTYLE, "width='60%'");
                $th = array(_("Transaction Type"), _("#"), $supp_ref ? _("Supplier Ref"): _("Ref"), _("Date"), _("Due Date"), _("Amount"),
-                       _("Other Allocations"), _("Left to Allocate"), _("This Allocation"),'','');
+                       _("Other Allocations"), _("Left to Allocate"), _("Early Payment Discount"), _("This Allocation"),'','');
 
                table_header($th);
-
+               $doc_ids = array();
                foreach ($cart->allocs as $id => $alloc_item)
                {
-                   if (floatcmp(abs($alloc_item->amount), $alloc_item->amount_allocated))
-                   {
+                       $doc_ids[] = $id;
+                       $early_discount = $alloc_item->early_discount != 0 && !date1_greater_date2($cart->date_,  add_days($alloc_item->date_, $alloc_item->early_days));
                                alt_table_row_color($k);
                        label_cell($systypes_array[$alloc_item->type]);
                                label_cell(get_trans_view_str($alloc_item->type, $alloc_item->type_no), "nowrap align='right'");
@@ -306,23 +311,41 @@ function show_allocatable($show_totals) {
                                amount_cell($alloc_item->amount_allocated);
 
                        $_POST['amount' . $id] = price_format($alloc_item->current_allocated);
-
+                       
                        $un_allocated = round((abs($alloc_item->amount) - $alloc_item->amount_allocated), 6);
                        amount_cell($un_allocated, false,'', 'maxval'.$id);
-                       amount_cells(null, "amount" . $id);//, input_num('amount' . $id));
-                               label_cell("<a href='#' name=Alloc$id onclick='allocate_all(this.name.substr(5));return true;'>"
-                                        . _("All") . "</a>");
-                               label_cell("<a href='#' name=DeAll$id onclick='allocate_none(this.name.substr(5));return true;'>"
-                                        . _("None") . "</a>".hidden("un_allocated" . $id, 
-                                        price_format($un_allocated), false));
+                       if ($early_discount) {
+                               $discount = price_format($alloc_item->early_discount*abs($alloc_item->amount));
+
+                               if ($reallocation)
+                                               label_cell($discount, 'align=center');
+                                       else
+                                               check_cells(null, 'early_disc'.$id, $discount, false, false, 'align=center');
+                                       $total_discount += $discount;
+                               } else {
+                                       label_cell(_("N/A"), 'align=center'); hidden('early_disc'.$id, 0);
+                               }
+                       amount_cells(null, "amount" . $id);
+                               if (0) {
+                                       label_cells('', '', '', 'colspan=2');
+                               } else {
+                                       label_cell("<a href='#' name=Alloc$id onclick='allocate_all(this.name.substr(5));return true;'>"
+                                                . _("All") . "</a>");
+                                       label_cell("<a href='#' name=DeAll$id onclick='allocate_none(this.name.substr(5));return true;'>"
+                                                . _("None") . "</a>".hidden("un_allocated" . $id, 
+                                                price_format($un_allocated), false));
+                               }
                                end_row();
 
                        $total_allocated += input_num('amount' . $id);
-                       }
                }
-               if ($show_totals) {
+               add_js_source('var docs=['.implode(',', $doc_ids).']');
+               if ($reallocation) {
                label_row(_("Total Allocated"), price_format($total_allocated),
-                       "colspan=8 align=right", "align=right id='total_allocated'", 3);
+                       "colspan=9 align=right", "align=right id='total_allocated'", 3);
+
+               label_row(_("Total Discount"), price_format($total_discount),
+                       "colspan=9 align=right", "align=right id='total_discount'", 3);
 
                        $amount = abs($cart->amount);
 
@@ -335,7 +358,7 @@ function show_allocatable($show_totals) {
                        $font1 = $font2 = "";
                        $left_to_allocate = price_format($amount - $total_allocated);
                label_row(_("Left to Allocate"), $font1 . $left_to_allocate . $font2, 
-                               "colspan=8 align=right", "nowrap align=right id='left_to_allocate'",
+                               "colspan=9 align=right", "nowrap align=right id='left_to_allocate'",
                                 3);
                }
                end_table(1);
@@ -405,3 +428,4 @@ function get_inv_allocations($trans_no, $trans_type, $person_id)
        }
        return $allocs;
 }
+
index 59a3676ef5664ed7ccc7bd004411ac37c2c824aa..3c2d83e0960428fa0b23f99e1d4ea21d2869e07e 100644 (file)
@@ -373,9 +373,9 @@ function checkbox($label, $name, $value=null, $submit_on_change=false, $title=fa
        if ($value === null)
                $value = get_post($name,0);
 
-       $str .= "<input"
-           .($value == 1 ? ' checked':'')
-           ." type='checkbox' name='$name' value='1'"
+       $str .= "<input class=check"
+           .($value != 0 ? ' checked':'')
+           ." type='checkbox' name='$name' value='".$value."'"
            .($submit_on_change ? " onclick='$submit_on_change'" : '')
            .($title ? " title='$title'" : '')
            ." >\n";
index 707bdf8ef841afd4ff4c9fc94a7d2aa45ccffc13..a47e8b02d2e629146988b9d170d79e0c3dbc8db9 100644 (file)
@@ -29,6 +29,21 @@ function blur_alloc(i) {
        price_format('total_allocated', total, user.pdec, 1, 1);
 }
 
+function update_totals() {
+       var amount = 0;
+       var discount = 0;
+
+       for (var i=0; i<docs.length; i++) {
+               amount += get_amount('amount'+docs[i])
+               if (document.getElementsByName('early_disc'+docs[i])[0].checked 
+                       && (get_amount('un_allocated'+docs[i]) == get_amount('amount'+docs[i])))
+                               discount += get_amount('early_disc'+docs[i]);
+       }
+       price_format('amount', amount-discount, user.pdec);
+       price_format('discount', discount, user.pdec);
+       
+}
+
 function allocate_all(doc) {
        var amount = get_amount('amount'+doc);
        var unallocated = get_amount('un_allocated'+doc);
@@ -64,6 +79,11 @@ var allocations = {
                e.onfocus = function() {
                        focus_alloc(this);
                };
+       },
+       '.check':function(e) {
+               e.onclick = function() {
+                       update_totals();
+               }
        }
 }
 
index a711e603cb40daf97dcc8924cefb339c5714eabd..1ea079866a9af88fcf1f24933dd9e5174645a3d3 100644 (file)
@@ -30,30 +30,33 @@ function blur_alloc(i) {
                }
 }
 
-function allocate_all(doc) {
-       var amount = get_amount('amount'+doc);
-       var unallocated = get_amount('un_allocated'+doc);
-       var total = get_amount('amount');
-       var left = 0;
-       total -=  (amount-unallocated);
-       left -= (amount-unallocated);
-       amount = unallocated;
-       if(left<0) {
-               total  += left;
-               amount += left;
-               left = 0;
+function update_totals() {
+       var amount = 0;
+       var discount = 0;
+
+       for (var i=0; i<docs.length; i++) {
+               amount += get_amount('amount'+docs[i])
+               if (document.getElementsByName('early_disc'+docs[i])[0].checked 
+                       && (get_amount('un_allocated'+docs[i]) == get_amount('amount'+docs[i])))
+                               discount += get_amount('early_disc'+docs[i]);
        }
-       price_format('amount'+doc, amount, user.pdec);
-       price_format('amount', total, user.pdec);
+       price_format('amount', amount-discount, user.pdec);
+       price_format('discount', discount, user.pdec);
+       
+}
+
+function allocate_all(doc) {
+       var unallocated = get_amount('maxval'+doc, 1);
+       price_format('amount'+doc, unallocated, user.pdec);
+       update_totals();
 }
 
 function allocate_none(doc) {
-       amount = get_amount('amount'+doc);
-       total = get_amount('amount');
        price_format('amount'+doc, 0, user.pdec);
-       price_format('amount', total-amount, user.pdec);
+       update_totals();
 }
 
+
 var allocations = {
        '.amount': function(e) {
                if(e.name == 'allocated_amount' || e.name == 'bank_amount')
@@ -70,6 +73,11 @@ var allocations = {
                                focus_alloc(this);
                        };
                }
+       },
+       '.check':function(e) {
+               e.onclick = function() {
+                       update_totals();
+               }
        }
 }
 
index 3d9ca4c154a1ff2a3504fb44008c74d3baacb9a6..c8ad5c46adf137c0dbd920b495adf8e7d793f9c7 100644 (file)
@@ -148,7 +148,8 @@ function get_allocatable_purch_orders($supplier_id = null, $trans_no=null, $type
                due_dates.date as due_date,
                supplier.address,
                amt,
-               supp_reference as supp_ref
+               supp_reference as supp_ref,
+               0, 0
                FROM ".TB_PREF."purch_orders as porder
                        LEFT JOIN ".TB_PREF."suppliers as supplier ON porder.supplier_id = supplier.supplier_id
                        LEFT JOIN ".TB_PREF."supp_allocations as alloc
@@ -189,29 +190,31 @@ function get_allocatable_to_supp_transactions($supplier_id=null, $trans_no=null,
                trans.due_date,
                trans.supplier_id,
                amt,
-               supp_reference
+               supp_reference,
+               pmt.early_days,
+               pmt.early_discount
 
         FROM ".TB_PREF."supp_trans as trans
+                       LEFT JOIN ".TB_PREF."suppliers supplier ON trans.supplier_id=supplier.supplier_id
+                       LEFT JOIN ".TB_PREF."payment_terms as pmt ON supplier.payment_terms=pmt.id
                        LEFT JOIN ".TB_PREF."supp_allocations as alloc
-                               ON trans.trans_no = alloc.trans_no_to AND trans.type = alloc.trans_type_to AND alloc.person_id=trans.supplier_id,"
-                       .TB_PREF."suppliers as supplier
-        WHERE
-                trans.supplier_id=supplier.supplier_id";
-       if ($supplier_id)
-               $sql .= " AND supplier.supplier_id=".db_escape($supplier_id);
+                               ON trans.trans_no = alloc.trans_no_to AND trans.type = alloc.trans_type_to AND alloc.person_id=trans.supplier_id";
 
        if ($trans_no != null and $type != null)
        {
-               $sql .= " AND alloc.trans_no_from=".db_escape($trans_no)."
+               $sql .= " WHERE alloc.trans_no_from=".db_escape($trans_no)."
                                  AND alloc.trans_type_from=".db_escape($type);
        }
        else
        {
-               $sql .= " AND round(ov_amount+ov_gst+ov_discount-alloc,6) > 0
+               $sql .= " WHERE round(ov_amount+ov_gst+ov_discount-alloc,6) > 0
                        AND trans.type NOT IN (".implode(',',array(ST_SUPPAYMENT, ST_BANKPAYMENT)).")";
-               $sql .= " GROUP BY type, trans_no";
        }
 
+       if ($supplier_id)
+               $sql .= " AND supplier.supplier_id=".db_escape($supplier_id);
+
+
        $orders = get_allocatable_purch_orders($supplier_id, $trans_no, $type);
        $sql = "($sql ORDER BY trans_no) \nUNION \n($orders)";
 
index 1b7fb663778ad1142bd6ed7003880e978d7e75f4..e677cf3accf21bb20b3592a6f8b0b10ff26e0539 100644 (file)
@@ -132,10 +132,10 @@ function invoice_header(&$supp_trans)
                $Ajax->activate('due_date');
        }
 
-    date_row(_("Due Date") . ":", 'due_date');
-
     label_row(_("Terms:"), $supp_trans->terms['description']);
 
+    date_row(_("Due Date") . ":", 'due_date');
+
        if (get_company_pref('use_dimension'))
                dimensions_list_row(_('Dimension').':', 'dimension', null, true, _('Default'), false, 1);
 
index 092e801c58952f2df1d07514d5790d49217eedc1..ccef48c3ff14340fb8bdfbfe13f0e5d4ef645317 100644 (file)
@@ -171,6 +171,8 @@ function display_po_header(&$order)
 
        table_section(2);
 
+    label_row(_("Terms:"), $order->terms['description']);
+
        if ($order->trans_type==ST_SUPPINVOICE)
                date_row(_("Due Date:"), 'due_date', '', false, 0, 0, 0, null, true);
 
@@ -180,10 +182,9 @@ function display_po_header(&$order)
                dimensions_list_row(_('Dimension').':', 'dimension', null, true, _('Default'), false, 1);
        if (get_company_pref('use_dimension') == 2)
                dimensions_list_row(_('Dimension 2').':', 'dimension2', null, true, _('Default'), false, 2);
-       locations_list_row(_("Receive Into:"), 'StkLocation', null, false, true, $order->fixed_asset); 
 
        table_section(3);
-
+       locations_list_row(_("Receive Into:"), 'StkLocation', null, false, true, $order->fixed_asset); 
     if (!isset($_POST['StkLocation']) || $_POST['StkLocation'] == "" ||
        isset($_POST['_StkLocation_update']) || !isset($_POST['delivery_address']) ||
        $_POST['delivery_address'] == "")
index 56d4312f5fb1c2fa0a40f4679ade924faf5d8770..b08d8548d8e75bf45f85609c3c2a13fa0b6e2e78 100644 (file)
@@ -32,6 +32,7 @@ if (!isset($_POST['customer_id']))
 echo "<center>" . _("Select a customer: ") . "&nbsp;&nbsp;";
 echo customer_list('customer_id', $_POST['customer_id'], true, true);
 echo "<br>";
+
 check(_("Show Settled Items:"), 'ShowSettled', null, true);
 echo "</center><br><br>";
 
@@ -65,7 +66,9 @@ function trans_view($trans)
 
 function alloc_link($row)
 {
-       return pager_link(_("Allocate"),
+       global $all_settled;
+
+       return  ($row['settled']&&!get_post('ShowSettled')) || (!$row['settled'] && $all_settled)  ? '' : pager_link(_("Allocate"),
                "/sales/allocations/customer_allocate.php?trans_no="
                        .$row["trans_no"] . "&trans_type=" . $row["type"]. "&debtor_no=" . $row["debtor_no"], ICON_ALLOC);
 }
@@ -85,6 +88,9 @@ function check_settled($row)
        return $row['settled'] == 1;
 }
 
+$all_settled = !db_num_rows(get_allocatable_to_cust_transactions($customer_id));
+if ($all_settled)
+       display_note('<b>'._("There is no unsettled transactions for this customer.").'</b>');
 
 $sql = get_allocatable_from_cust_sql($customer_id, $settled);
 
index 30d5151cca6e43e6bce3ea45e83977570af49d4c..1d23a3829e66296e6ed3cd9e7f36599773b1e090 100644 (file)
@@ -89,12 +89,21 @@ if (!isset($_POST['customer_id'])) {
        $_POST['bank_account'] = $dflt_act['id'];
 }
 if (!isset($_POST['DateBanked'])) {
-       $_POST['DateBanked'] = new_doc_date();
+       $_SESSION['alloc']->date_ =$_POST['DateBanked'] = new_doc_date();
        if (!is_date_in_fiscalyear($_POST['DateBanked'])) {
                $_POST['DateBanked'] = end_fiscalyear();
        }
 }
 
+if (get_post('_DateBanked_changed')) {
+       if (!is_date($_POST['DateBanked'])) {
+               display_error(_("The entered date is invalid. Please enter a valid date for the payment."));
+               set_focus('DateBanked');
+       } else {
+               $_SESSION['alloc']->date_ = get_post('DateBanked');
+               $Ajax->activate('_page_body');
+       }
+}
 
 if (isset($_GET['AddedID'])) {
        $payment_no = $_GET['AddedID'];
@@ -240,8 +249,8 @@ if (get_post('AddPaymentItem') && can_process()) {
        $new_pmt = !$_SESSION['alloc']->trans_no;
 
        $payment_no =  save_cust_payment($_SESSION['alloc'], get_post('customer_id'), get_post('BranchID'), get_post('bank_account'),
-                get_post('DateBanked'), get_post('ref'), input_num('amount'), input_num('discount'), get_post('memo_'),
-                input_num('charge'), input_num('bank_amount', input_num('amount')));
+               get_post('DateBanked'), get_post('ref'), input_num('amount'), input_num('discount'), get_post('memo_'),
+               input_num('charge'), input_num('bank_amount', input_num('amount')));
 
        unset($_SESSION['alloc']);
        meta_forward($_SERVER['PHP_SELF'], $new_pmt ? "AddedID=$payment_no" : "UpdatedID=$payment_no");
@@ -256,7 +265,6 @@ function read_customer_data()
        $myrow = get_customer_habit($_POST['customer_id']);
 
        $_POST['HoldAccount'] = $myrow["dissallow_invoices"];
-       $_POST['pymt_discount'] = $myrow["pymt_discount"];
        // To support Edit feature
        // If page is called first time and New entry fetch the nex reference number
        if (!$_SESSION['alloc']->trans_no && !isset($_POST['charge'])) 
@@ -338,7 +346,6 @@ read_customer_data();
 set_global_customer($_POST['customer_id']);
 if (isset($_POST['HoldAccount']) && $_POST['HoldAccount'] != 0)        
        display_warning(_("This customer account is on hold."));
-$display_discount_percent = percent_format($_POST['pymt_discount']*100) . "%";
 
 table_section(2);
 
@@ -369,8 +376,6 @@ div_end();
 
 start_table(TABLESTYLE, "width='60%'");
 
-label_row(_("Customer prompt payment discount :"), $display_discount_percent);
-
 amount_row(_("Amount of Discount:"), 'discount', null, '', $cust_currency);
 
 amount_row(_("Amount:"), 'amount', null, '', $cust_currency);
index 6f1d3a7983b0aa0ebfbf2e9e116edec461013e6a..611194bfd53889aa4ab549463d31d9addd7aca40 100644 (file)
@@ -66,7 +66,6 @@ class Cart
        var     $pos;                   // user assigned POS
        var $cash_account;
        var $account_name;
-       var $cash_discount;     // not used as of FA 2.1
        var $dimension_id;
        var $dimension2_id;
        var $payment;
@@ -344,7 +343,7 @@ class Cart
                return $ret;
        }
 
-       function set_customer($customer_id, $customer_name, $currency, $discount, $payment, $cdiscount=0)
+       function set_customer($customer_id, $customer_name, $currency, $discount, $payment)
        {
                $this->customer_name = $customer_name;
                $this->customer_id = $customer_id;
@@ -352,7 +351,6 @@ class Cart
                $this->customer_currency = $currency;
                $this->payment = $payment;
                $this->payment_terms = get_payment_terms($payment);
-               $this->cash_discount = $cdiscount;
 
                if ($this->payment_terms['type'] == PTT_CASH) {
                        $this->Location = $this->pos['pos_location'];
index a1f6a61c5e6ac2af3694a4563bd7f25404c7f8b3..d66615ee7cb8b4f0fcf3979c2d8337039ebd2dcf 100644 (file)
@@ -119,12 +119,12 @@ function get_allocatable_from_cust_sql($customer_id=null, $settled)
                .TB_PREF."debtor_trans as trans, "
                .TB_PREF."debtors_master as debtor"
        ." WHERE trans.debtor_no=debtor.debtor_no
-               AND (((type=".ST_CUSTPAYMENT." OR type=".ST_BANKDEPOSIT.") AND (trans.ov_amount > 0))
+               AND (((type=".ST_CUSTPAYMENT." OR type=".ST_BANKDEPOSIT.") AND (trans.ov_amount+ov_discount > 0))
                 OR (type=".ST_CUSTCREDIT. " AND (ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount)>0)
                 OR (type=".ST_JOURNAL. " AND (trans.ov_amount < 0)))";
 
        if (!$settled)
-               $sql .= " AND (round(abs(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount-alloc),6) > 0)";
+               $sql .= " AND (round(abs(ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount)-alloc,6) > 0)";
 
        if ($customer_id != null)
                $sql .= " AND trans.debtor_no = ".db_escape($customer_id);
@@ -148,9 +148,12 @@ function get_allocatable_sales_orders($customer_id = null, $trans_no=null, $type
                sorder.version,
                amt,
                sorder.debtor_no,
-               sorder.branch_code
+               sorder.branch_code,
+               pmt.early_days,
+               pmt.early_discount
                FROM ".TB_PREF."sales_orders as sorder
                        LEFT JOIN ".TB_PREF."debtors_master as debtor ON sorder.debtor_no = debtor.debtor_no
+                       LEFT JOIN ".TB_PREF."payment_terms as pmt ON sorder.payment_terms=pmt.id
                        LEFT JOIN ".TB_PREF."cust_allocations as alloc 
                                ON sorder.order_no = alloc.trans_no_to AND sorder.trans_type = alloc.trans_type_to AND alloc.person_id=sorder.debtor_no
                        LEFT JOIN (SELECT order_, sum(prep_amount) amount FROM ".TB_PREF."debtor_trans dt
@@ -191,10 +194,13 @@ function get_allocatable_to_cust_transactions($customer_id = null, $trans_no=nul
                trans.version,
                amt,
                trans.debtor_no,
-               trans.branch_code
+               trans.branch_code,
+               pmt.early_days,
+               pmt.early_discount
         FROM ".TB_PREF."debtor_trans as trans
                        LEFT JOIN ".TB_PREF."cust_allocations as alloc
-                               ON trans.trans_no = alloc.trans_no_to AND trans.type = alloc.trans_type_to AND alloc.person_id=trans.debtor_no,"
+                               ON trans.trans_no = alloc.trans_no_to AND trans.type = alloc.trans_type_to AND alloc.person_id=trans.debtor_no
+                       LEFT JOIN ".TB_PREF."payment_terms as pmt ON trans.payment_terms=pmt.id,"
                        .TB_PREF."debtors_master as debtor
         WHERE
                 trans.debtor_no=debtor.debtor_no";
@@ -218,9 +224,11 @@ function get_allocatable_to_cust_transactions($customer_id = null, $trans_no=nul
                                        OR
                                        trans.type = '". ST_JOURNAL."'
                                        AND ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount>0
+                                       AND ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount>alloc
                                        OR
                                        trans.type = '". ST_BANKPAYMENT."'
                                        AND ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount>0
+                                       AND ov_amount+ov_gst+ov_freight+ov_freight_tax+ov_discount>alloc
                                )";
                $sql .= " GROUP BY type, trans_no";
        }
index 0e47a97481a05d0a104d6b0cc1753309b240829f..e7a57deb9008ae3049096a902e6071056f5ee37c 100644 (file)
 ***********************************************************************/
 
 function add_customer($CustName, $cust_ref, $address, $tax_id, $curr_code,
-       $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount, $pymt_discount, 
+       $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount,
        $credit_limit, $sales_type, $notes)
 {
        $sql = "INSERT INTO ".TB_PREF."debtors_master (name, debtor_ref, address, tax_id,
                dimension_id, dimension2_id, curr_code, credit_status, payment_terms, discount, 
-               pymt_discount,credit_limit, sales_type, notes) VALUES ("
+               credit_limit, sales_type, notes) VALUES ("
                .db_escape($CustName) .", " .db_escape($cust_ref) .", "
                .db_escape($address) . ", " . db_escape($tax_id) . ","
                .db_escape($dimension_id) . ", " 
                .db_escape($dimension2_id) . ", ".db_escape($curr_code) . ", 
-               " . db_escape($credit_status) . ", ".db_escape($payment_terms) . ", " . $discount . ", 
-               " . $pymt_discount . ", " . $credit_limit 
+               " . db_escape($credit_status) .", " . db_escape($payment_terms) .", " . $discount . "," . $credit_limit 
                 .", ".db_escape($sales_type).", ".db_escape($notes) . ")";
        db_query($sql,"The customer could not be added");
 }
 
 function update_customer($customer_id, $CustName, $cust_ref, $address, $tax_id, $curr_code,
-       $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount, $pymt_discount,
+       $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount,
        $credit_limit, $sales_type, $notes)
 {
        $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($CustName) . ", 
@@ -41,7 +40,6 @@ function update_customer($customer_id, $CustName, $cust_ref, $address, $tax_id,
                credit_status=".db_escape($credit_status) . ", 
                payment_terms=".db_escape($payment_terms) . ", 
                discount=" . $discount . ", 
-               pymt_discount=" . $pymt_discount . ", 
                credit_limit=" . $credit_limit . ", 
                sales_type = ".db_escape($sales_type) . ", 
                notes=".db_escape($notes) ."
@@ -133,7 +131,7 @@ function get_customer_name($customer_id)
 
 function get_customer_habit($customer_id)
 {
-       $sql = "SELECT debtor.pymt_discount, credit_status.dissallow_invoices
+       $sql = "SELECT credit_status.dissallow_invoices
                        FROM ".TB_PREF."debtors_master debtor,"
                                .TB_PREF."credit_status credit_status
                        WHERE debtor.credit_status = credit_status.id
index 9b9ab467ed7eea9b4dd89990929fd19b23bc8b4a..f1c72672ffc5f8b59e97f69d852b80d3e188edb4 100644 (file)
@@ -188,10 +188,7 @@ function write_sales_invoice(&$invoice)
                        $amount = $items_total + $items_added_tax + $invoice->freight_cost 
                                + $freight_added_tax;
 
-                       // to use debtors.pmt_discount on cash sale:
-                       // extend invoice entry page with final amount after discount 
-                       // and change line below.
-                       $discount = 0; // $invoice->cash_discount*$amount;
+                       $discount = 0;
                        $pmtno = write_customer_payment(0, $invoice->customer_id, 
                                $invoice->Branch, $invoice->pos['pos_account'], $date_,
                                $Refs->get_next(ST_CUSTPAYMENT, null, array('customer' => $invoice->customer_id,
index 3296c5917c0e402e8b4cfc0e50948aa951d5faa0..f4d18625f6f28fb6b5f74bf48c5db00eec601b1c 100644 (file)
@@ -419,7 +419,6 @@ function get_customer_to_order($customer_id) {
                  cust.curr_code,
                  cust.discount,
                  cust.payment_terms,
-                 cust.pymt_discount,
                  cust.credit_limit - Sum(IFNULL(IF(trans.type IN(".implode(',', array(ST_CUSTCREDIT, ST_CUSTPAYMENT, ST_BANKDEPOSIT))."),
                        -1, 1) * (ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount),0)) as cur_credit
                FROM ".TB_PREF."debtors_master cust
index e870b83356c20110d4f03d10a6d9ed4bf5604cfb..6cad00f183dfdf7fcb5d1e87d44b90a042674f6c 100644 (file)
@@ -465,7 +465,7 @@ function write_recurrent_invoice($invoice_id, $description, $order_no, $debtor_n
 }
 
 function write_customer($customer_id, $name, $ref, $address, $tax_id, $curr_code, $dim1, $dim2, $credit_status,
-        $payment_terms, $discount, $pymt_discount, $credit_limit, $sales_type, $notes, $inactive, $salesman, $area, 
+        $payment_terms, $discount, $credit_limit, $sales_type, $notes, $inactive, $salesman, $area, 
         $tax_group, $location, $address, $ship_via, $notes, $bank_account, $address, $phone, $phone2, $fax, $email)
 {
        global $SysPrefs;
@@ -474,13 +474,13 @@ function write_customer($customer_id, $name, $ref, $address, $tax_id, $curr_code
 
        if ($customer_id) {
                update_customer($customer_id, $name, $ref, $address, $tax_id, $curr_code, $dim1, $dim2, $credit_status,
-                        $payment_terms, $discount/100, $pymt_discount/100, $credit_limit, $sales_type, $notes, $inactive);
+                        $payment_terms, $discount/100, $credit_limit, $sales_type, $notes, $inactive);
 
                update_record_status($customer_id, $inactive, 'debtors_master', 'debtor_no');
 
        } else {
                add_customer($name, $ref, $address,     $tax_id, $curr_code, $dim1, $dim2, $credit_status, $payment_terms,
-                       $discount / 100, $pymt_discount / 100, $credit_limit, $sales_type, $notes);
+                       $discount / 100, $credit_limit, $sales_type, $notes);
 
                $customer_id = db_insert_id();
 
index 49c39b873f86efe35c083f97574a8772a62e1f62..2d9be2dce28c69577936b438ba4025e47a0cad4d 100644 (file)
@@ -85,7 +85,7 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
        $deliver = $myrow['address']; // in case no branch address use company address
 
        $order->set_customer($customer_id, $name, $myrow['curr_code'], 
-               $myrow['discount'], $myrow['payment_terms'], $myrow['pymt_discount']);
+               $myrow['discount'], $myrow['payment_terms']);
 
        // the sales type determines the price list to be used by default
        $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included'],
index e51e4b48bc7bd9e51e5175694b2db20dfd48a4d2..4ac790c6c4d7b14224540b457052ff04f6aa1a27 100644 (file)
@@ -58,13 +58,6 @@ function can_process()
                return false;           
        } 
        
-       if (!check_num('pymt_discount', 0, 100)) 
-       {
-               display_error(_("The payment discount must be numeric and is expected to be less than 100% and greater than or equal to 0."));
-               set_focus('pymt_discount');
-               return false;           
-       } 
-       
        if (!check_num('discount', 0, 100)) 
        {
                display_error(_("The discount percentage must be numeric and is expected to be less than 100% and greater than or equal to 0."));
@@ -87,7 +80,7 @@ function handle_submit(&$selected_id)
 
        $_POST['customer_id'] = write_customer($_POST['customer_id'], $_POST['CustName'], $_POST['cust_ref'], $_POST['address'],
                        $_POST['tax_id'], $_POST['curr_code'], $_POST['dimension_id'], $_POST['dimension2_id'],
-                       $_POST['credit_status'], $_POST['payment_terms'], input_num('discount'), input_num('pymt_discount'),
+                       $_POST['credit_status'], $_POST['payment_terms'], input_num('discount'),
                        input_num('credit_limit'), $_POST['sales_type'], $_POST['notes'], @$_POST['inactive'], get_post('salesman'),
                        get_post('area'), get_post('tax_group_id'), get_post('location'), get_post('address'), get_post('ship_via'), 
                        get_post('notes'), get_post('bank_account'), get_post('address'), get_post('phone'), get_post('phone2'), get_post('fax'), get_post('email'));
@@ -175,7 +168,7 @@ function customer_settings($selected_id)
                        $_POST['credit_status']  = -1;
                        $_POST['payment_terms']  = $_POST['notes']  = '';
 
-                       $_POST['discount']  = $_POST['pymt_discount'] = percent_format(0);
+                       $_POST['discount']  = percent_format(0);
                        $_POST['credit_limit']  = price_format($SysPrefs->default_credit_limit());
                }
        }
@@ -194,7 +187,6 @@ function customer_settings($selected_id)
                $_POST['credit_status']  = $myrow["credit_status"];
                $_POST['payment_terms']  = $myrow["payment_terms"];
                $_POST['discount']  = percent_format($myrow["discount"] * 100);
-               $_POST['pymt_discount']  = percent_format($myrow["pymt_discount"] * 100);
                $_POST['credit_limit']  = price_format($myrow["credit_limit"]);
                $_POST['notes']  = $myrow["notes"];
                $_POST['inactive'] = $myrow["inactive"];
@@ -240,7 +232,6 @@ function customer_settings($selected_id)
        table_section_title(_("Sales"));
 
        percent_row(_("Discount Percent:"), 'discount', $_POST['discount']);
-       percent_row(_("Prompt Payment Discount Percent:"), 'pymt_discount', $_POST['pymt_discount']);
        amount_row(_("Credit Limit:"), 'credit_limit', $_POST['credit_limit']);
 
        payment_terms_list_row(_("Payment Terms:"), 'payment_terms', $_POST['payment_terms']);
index 94496c80598a6beb74960f11ec5c434a57c3c2ac..b1e7c9edb9dafbd6f4bb131abf39175b0abfc0db 100644 (file)
@@ -698,7 +698,6 @@ CREATE TABLE `0_debtors_master` (
   `credit_status` int(11) NOT NULL DEFAULT '0',
   `payment_terms` int(11) DEFAULT NULL,
   `discount` double NOT NULL DEFAULT '0',
-  `pymt_discount` double NOT NULL DEFAULT '0',
   `credit_limit` float NOT NULL DEFAULT '1000',
   `notes` tinytext NOT NULL,
   `inactive` tinyint(1) NOT NULL DEFAULT '0',
@@ -710,8 +709,8 @@ CREATE TABLE `0_debtors_master` (
 -- Data of table `0_debtors_master` --
 
 INSERT INTO `0_debtors_master` VALUES
-('1', 'Donald Easter LLC', 'Donald Easter', 'N/A', '123456789', 'USD', '1', '0', '0', '1', '4', '0', '0', '1000', '', '0'),
-('2', 'MoneyMaker Ltd.', 'MoneyMaker', 'N/A', '54354333', 'EUR', '1', '1', '0', '1', '1', '0', '0', '1000', '', '0');
+('1', 'Donald Easter LLC', 'Donald Easter', 'N/A', '123456789', 'USD', '1', '0', '0', '1', '4', '0', '1000', '', '0'),
+('2', 'MoneyMaker Ltd.', 'MoneyMaker', 'N/A', '54354333', 'EUR', '1', '1', '0', '1', '1', '0', '1000', '', '0');
 
 -- Structure of table `0_dimensions` --
 
index 9d25c8b8d81f9d8d300842bc3f198741d5c9f16b..e1f378d69b3ded0be3a1feb1bf74216e1fbf0879 100644 (file)
@@ -582,7 +582,6 @@ CREATE TABLE `0_debtors_master` (
   `credit_status` int(11) NOT NULL DEFAULT '0',
   `payment_terms` int(11) DEFAULT NULL,
   `discount` double NOT NULL DEFAULT '0',
-  `pymt_discount` double NOT NULL DEFAULT '0',
   `credit_limit` float NOT NULL DEFAULT '1000',
   `notes` tinytext NOT NULL,
   `inactive` tinyint(1) NOT NULL DEFAULT '0',