The Customer Branch Sales Account now overrides the Item Sales Accounts if a Sales...
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Sat, 14 Jun 2008 08:49:22 +0000 (08:49 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Sat, 14 Jun 2008 08:49:22 +0000 (08:49 +0000)
If setting the Customer Branch AR Account to a Bank Account (and hereby also Cash Account) the  transactions from Direct Invoice and Credit Note will be allocated immediately when placing.

CHANGELOG.txt
includes/ui/ui_lists.inc
sales/includes/db/sales_credit_db.inc
sales/includes/db/sales_invoice_db.inc
sales/manage/customer_branches.php

index 05037830aed18274e83c30d4476449a0a5e9846d..24f9db98bfa285b2b042c183ddd49793da57f099 100644 (file)
@@ -19,6 +19,16 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+14-Jun-2008 Joe Hunt
+! The Customer Branch Sales Account now overrides the Item Sales Accounts if a Sales Account is here.
+  Can now be set to 'Use Item Sales Accounts', which is the first choice in list and default.
+! If setting the Customer Branch AR Account to a Bank Account (and hereby also Cash Account) the 
+  transactions from Direct Invoice and Credit Note will be allocated immediately when placing.
+$ /includes/ui/ui_lists.inc
+  /sales/includes/db/sales_invoice_db.inc
+  /sales/includes/db/sales_credit_db.inc
+  /sales/manage/customer_branches.php
+  .
 12-Jun-2008 Joe Hunt
 + New inventory sales report. Shows total sales - costs - contribution per category, location, detailed/summary and period.
 $ /reporting/reports_main.php
index 102227c4727448887ac46b83e958d464117e358e..454932cd32898ed7fed7963574aa622482a74856 100644 (file)
@@ -1231,7 +1231,7 @@ function gl_account_types_list_row($label, $name, $selected_id, $all_option=fals
 
 //-----------------------------------------------------------------------------------------------
 function gl_all_accounts_list($name, $selected_id, $skip_bank_accounts=false,
-       $show_group=false, $cells=false)
+       $show_group=false, $cells=false, $all_option=false)
 {
        if ($skip_bank_accounts)
                $sql = "SELECT chart.account_code, chart.account_name, type.name
@@ -1248,6 +1248,8 @@ function gl_all_accounts_list($name, $selected_id, $skip_bank_accounts=false,
        combo_input($name, $selected_id, $sql, 'chart.account_code', 'chart.account_name',
        array(
                'format' => '_format_account' .  ($show_group ? '2' : ''),
+               'spec_option' => $all_option===true ?  _("Use Item Sales Accounts") : $all_option,
+               'spec_id' => '',
                'order' => 'account_code',
                'search_box' => $cells,
                        'search_submit' => false,
@@ -1269,21 +1271,21 @@ function _format_account2($row)
 }
 
 function gl_all_accounts_list_cells($label, $name, $selected_id, $skip_bank_accounts=false,
-       $show_group=false, $cells=false)
+       $show_group=false, $cells=false, $all_option=false)
 {
        if ($label != null)
                echo "<td>$label</td>\n";
        echo "<td>";
-       gl_all_accounts_list($name, $selected_id, $skip_bank_accounts, $show_group, $cells);
+       gl_all_accounts_list($name, $selected_id, $skip_bank_accounts, $show_group, $cells, $all_option);
        echo "</td>\n";
 }
 
 function gl_all_accounts_list_row($label, $name, $selected_id, $skip_bank_accounts=false,
-       $show_group=false, $cells=false)
+       $show_group=false, $cells=false, $all_option=false)
 {
        echo "<tr>\n";
        gl_all_accounts_list_cells($label, $name, $selected_id, $skip_bank_accounts,
-               $show_group, $cells);
+               $show_group, $cells, $all_option);
        echo "</tr>\n";
 }
 
index a7f41e33d551f12e4adcc7151b159aad9c88f551..bb284f04d8326ff965e668ae0ff998a946147802 100644 (file)
@@ -41,6 +41,12 @@ function write_credit_note($credit_note, $write_off_acc)
            $items_added_tax = 0;
            $freight_added_tax = 0;
        }
+       // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
+       // the transaction will be settled at once.
+       if (is_bank_account($branch_data['receivables_account']))
+               $alloc = $credit_note_total + $items_added_tax + $credit_note->freight_cost + $freight_added_tax;
+       else
+               $alloc = 0;
 
 //     $sales_order=$invoice->order_no;        //?
 //    if (is_array($sales_order)) $sales_order = $sales_order[0]; //?
@@ -54,9 +60,8 @@ function write_credit_note($credit_note, $write_off_acc)
                $credit_note_total, 0, $items_added_tax,
                $credit_note->freight_cost, $freight_added_tax,
                $credit_note->sales_type,
-               $credit_note->order_no, $credit_invoice, $credit_note->ship_via
-               //, $credit_note->due_date
-               );
+               $credit_note->order_no, $credit_invoice, $credit_note->ship_via,
+               null, $alloc); // 2008-06-14 extra $alloc
 
        if ($trans_no==0) {
                $credit_note->trans_no = array($credit_no=>0);
@@ -231,13 +236,21 @@ function add_gl_trans_credit_costs($order, $order_line, $credit_no, $date_,
 
                //Post sales transaction to GL credit sales
 
-               add_gl_trans_customer(11, $credit_no, $date_, $stock_gl_codes["sales_account"], 0, 0,
+               // 2008-06-14. If there is a Branch Sales Account, then override with this,
+               // else take the Item Sales Account
+               if ($branch_data['sales_account'] != "")
+                       $sales_account = $branch_data['sales_account'];
+               else
+                       $sales_account = $stock_gl_codes['sales_account'];
+               add_gl_trans_customer(11, $credit_no, $date_, $sales_account, $stock_gl_codes["dimension_id"],
+                       $stock_gl_codes["dimension2_id"],
                        ($line_taxfree_price * $order_line->qty_dispatched), $order->customer_id,
                        "The credit note GL posting could not be inserted");
 
                if ($order_line->discount_percent != 0) {
 
-                       add_gl_trans_customer(11, $credit_no, $date_, $branch_data["sales_discount_account"], 0, 0,
+                       add_gl_trans_customer(11, $credit_no, $date_, $branch_data["sales_discount_account"],
+                               $stock_gl_codes["dimension_id"], $stock_gl_codes["dimension2_id"],
                                -($line_taxfree_price * $order_line->qty_dispatched * $order_line->discount_percent),
                                $order->customer_id,
                                "The credit note discount GL posting could not be inserted");
index 3627eb19354ae0daf4de38a9891d17795ed345bb..8270ea6eaf3f92c2d9d7f9cc72f4556fece6dfd5 100644 (file)
@@ -43,6 +43,12 @@ function write_sales_invoice(&$invoice)
            $items_added_tax = 0;
            $freight_added_tax = 0;
        }
+       // 2006-06-14. If the Customer Branch AR Account is set to a Bank Account,
+       // the transaction will be settled at once.
+       if (is_bank_account($branch_data['receivables_account']))
+               $alloc = $items_total + $items_added_tax + $invoice->freight_cost + $freight_added_tax;
+       else
+               $alloc = 0;
 
        /* Insert/update the debtor_trans */
        $sales_order = $invoice->order_no;
@@ -53,7 +59,7 @@ function write_sales_invoice(&$invoice)
                $invoice->Branch, $date_, $invoice->reference, $items_total, 0,
                $items_added_tax, $invoice->freight_cost, $freight_added_tax,
                $invoice->sales_type, $sales_order, $delivery_no,
-               $invoice->ship_via, $invoice->due_date);
+               $invoice->ship_via, $invoice->due_date, $alloc); // 2008-06-14 extra $alloc
 
        if ($trans_no == 0) {
                $invoice->trans_no = array($invoice_no=>0);
@@ -87,11 +93,17 @@ function write_sales_invoice(&$invoice)
 
                if ($invoice_line->qty_dispatched != 0) {
                        $stock_gl_code = get_stock_gl_code($invoice_line->stock_id);
+                       // 2008-06-14. If there is a Branch Sales Account, then override with this,
+                       // else take the Item Sales Account
+                       if ($branch_data['sales_account'] != "")
+                               $sales_account = $branch_data['sales_account'];
+                       else
+                               $sales_account = $stock_gl_code['sales_account'];
 
                        if ($invoice_line->line_price() != 0) {
                                //Post sales transaction to GL credit sales
 
-                                       add_gl_trans_customer(10, $invoice_no, $date_, $stock_gl_code["sales_account"],
+                                       add_gl_trans_customer(10, $invoice_no, $date_, $sales_account,
                                                $stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"],
                                                (-$line_taxfree_price * $invoice_line->qty_dispatched),
                                                $invoice->customer_id, "The sales price GL posting could not be inserted");
index 1ca32ec712908bd5e09b2df92a257d2dd75f7519..de40042e49fbb21a5461fb94a3511e5cb5f17477 100644 (file)
@@ -22,7 +22,7 @@ check_db_has_tax_groups(_("There are no tax groups defined in the system. At lea
 
 //-----------------------------------------------------------------------------------------------
 
-if (isset($_GET['debtor_no'])) 
+if (isset($_GET['debtor_no']))
 {
        $_POST['customer_id'] = strtoupper($_GET['debtor_no']);
        $_POST['New'] = "1";
@@ -34,14 +34,14 @@ if (isset($_GET['SelectedBranch']))
        unset($_POST['New']);
 }
 
-if (!isset($_GET['SelectedBranch']) && !isset($_POST['AddUpdate'])) 
+if (!isset($_GET['SelectedBranch']) && !isset($_POST['AddUpdate']))
 {
        $_POST['New'] = "1";
 }
 
 //-----------------------------------------------------------------------------------------------
 
-if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
+if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM']))
 {
 
        //initialise no input errors assumed initially before we test
@@ -49,13 +49,13 @@ if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM']))
 
        //first off validate inputs sensible
 
-       if (strlen($_POST['br_name']) == 0) 
+       if (strlen($_POST['br_name']) == 0)
        {
                $input_error = 1;
                display_error(_("The Branch name cannot be empty."));
        }
 
-       if ($input_error != 1) 
+       if ($input_error != 1)
        {
 
                //if (!isset($_POST['New']))
@@ -83,7 +83,7 @@ if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM']))
                WHERE branch_code =".db_escape($_POST['branch_code']) . "
                AND debtor_no=".db_escape($_POST['customer_id']);
 
-               } 
+               }
                else
                {
                        /*Selected branch is null cos no item selected on first time round so must be adding a  record must be submitting new entries in the new Customer Branches form */
@@ -91,18 +91,18 @@ if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM']))
                                salesman, phone, fax,
                                contact_name, area, email, tax_group_id, sales_account, receivables_account, payment_discount_account, sales_discount_account, default_location,
                                br_post_address, disable_trans, default_ship_via)
-                               VALUES (".db_escape($_POST['customer_id']). ",".db_escape($_POST['br_name']) . ", " 
-                                       .db_escape($_POST['br_address']) . ", ".db_escape($_POST['salesman']) . ", " 
+                               VALUES (".db_escape($_POST['customer_id']). ",".db_escape($_POST['br_name']) . ", "
+                                       .db_escape($_POST['br_address']) . ", ".db_escape($_POST['salesman']) . ", "
                                        .db_escape($_POST['phone']) . ", ".db_escape($_POST['fax']) . ","
-                                       .db_escape($_POST['contact_name']) . ", ".db_escape($_POST['area']) . "," 
-                                       .db_escape($_POST['email']) . ", ".db_escape($_POST['tax_group_id']) . ", " 
-                                       .db_escape($_POST['sales_account']) . ", " 
-                                       .db_escape($_POST['receivables_account']) . ", " 
-                                       .db_escape($_POST['payment_discount_account']) . ", " 
-                                       .db_escape($_POST['sales_discount_account']) . ", " 
-                                       .db_escape($_POST['default_location']) . ", " 
-                                       .db_escape($_POST['br_post_address']) . "," 
-                                       .db_escape($_POST['disable_trans']) . ", " 
+                                       .db_escape($_POST['contact_name']) . ", ".db_escape($_POST['area']) . ","
+                                       .db_escape($_POST['email']) . ", ".db_escape($_POST['tax_group_id']) . ", "
+                                       .db_escape($_POST['sales_account']) . ", "
+                                       .db_escape($_POST['receivables_account']) . ", "
+                                       .db_escape($_POST['payment_discount_account']) . ", "
+                                       .db_escape($_POST['sales_discount_account']) . ", "
+                                       .db_escape($_POST['default_location']) . ", "
+                                       .db_escape($_POST['br_post_address']) . ","
+                                       .db_escape($_POST['disable_trans']) . ", "
                                        .db_escape($_POST['default_ship_via']) . ")";
                }
 
@@ -112,8 +112,8 @@ if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM']))
                meta_forward($_SERVER['PHP_SELF'], "debtor_no=" . $_POST['customer_id']);
        }
 
-} 
-elseif (isset($_GET['delete'])) 
+}
+elseif (isset($_GET['delete']))
 {
        //the link to delete a selected record was clicked instead of the submit button
 
@@ -122,22 +122,22 @@ elseif (isset($_GET['delete']))
        $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE branch_code='" . $_POST['branch_code']. "' AND debtor_no = '" . $_POST['customer_id']. "'";
        $result = db_query($sql,"could not query debtortrans");
        $myrow = db_fetch_row($result);
-       if ($myrow[0] > 0) 
+       if ($myrow[0] > 0)
        {
                display_error(_("Cannot delete this branch because customer transactions have been created to this branch."));
 
-       } 
-       else 
+       }
+       else
        {
                $sql= "SELECT COUNT(*) FROM ".TB_PREF."sales_orders WHERE branch_code='" . $_POST['branch_code']. "' AND debtor_no = '" . $_POST['customer_id']. "'";
                $result = db_query($sql,"could not query sales orders");
 
                $myrow = db_fetch_row($result);
-               if ($myrow[0] > 0) 
+               if ($myrow[0] > 0)
                {
                        display_error(_("Cannot delete this branch because sales orders exist for it. Purge old sales orders first."));
-               } 
-               else 
+               }
+               else
                {
                        $sql="DELETE FROM ".TB_PREF."cust_branch WHERE branch_code='" . $_POST['branch_code']. "' AND debtor_no='" . $_POST['customer_id']. "'";
                        db_query($sql,"could not delete branch");
@@ -170,7 +170,7 @@ if ($num_branches)
 
        $th = array(_("Name"), _("Contact"), _("Sales Person"), _("Area"),
                _("Phone No"), _("Fax No"), _("E-mail"), _("Tax Group"), "", "");
-       table_header($th);      
+       table_header($th);
 
        while ($myrow = db_fetch($result))
        {
@@ -186,7 +186,7 @@ if ($num_branches)
                edit_link_cell("debtor_no=" . $_POST['customer_id']. "&SelectedBranch=" . $myrow["branch_code"]);
                delete_link_cell("debtor_no=" . $_POST['customer_id']. "&SelectedBranch=" . $myrow["branch_code"]. "&delete=yes");
                end_row();
-       } 
+       }
        end_table();
        //END WHILE LIST LOOP
 }
@@ -194,10 +194,10 @@ else
        display_note(_("The selected customer does not have any branches. Please create at least one branch."));
 //else
 //{
-//}    
+//}
 
 
-if (!isset($_POST['New'])) 
+if (!isset($_POST['New']))
 {
        hyperlink_params($_SERVER['PHP_SELF'], _("New Customer Branch"), "debtor_no=" . $_POST['customer_id']);
 }
@@ -208,7 +208,7 @@ echo "<tr valign=top><td>"; // outer table
 echo "<table>";
 
 
-if (!isset($_POST['New']) && $num_branches) 
+if (!isset($_POST['New']) && $num_branches)
 {
 
        //editing an existing branch
@@ -238,8 +238,8 @@ if (!isset($_POST['New']) && $num_branches)
     $_POST['receivables_account'] = $myrow['receivables_account'];
     $_POST['payment_discount_account'] = $myrow['payment_discount_account'];
 
-} 
-else 
+}
+else
 { //end of if $SelectedBranch only do the else when a new record is being entered
 
        $sql = "SELECT name, address, email
@@ -251,11 +251,14 @@ else
        $_POST['br_address'] = $_POST['br_post_address'] = $myrow["address"];
        $_POST['branch_code'] = "";
        $_POST['email'] = $myrow['email'];
-       if (!isset($_POST['sales_account']) || !isset($_POST['sales_discount_account'])) 
+       if (!isset($_POST['sales_account']) || !isset($_POST['sales_discount_account']))
        {
                $company_record = get_company_prefs();
 
-           $_POST['sales_account'] = $company_record["default_sales_act"];
+           // $_POST['sales_account'] = $company_record["default_sales_act"];
+           // 2008-06-14. Changed so if no account here the Item Sales Account is taken
+           // Will be set if it should override the item sales account
+           $_POST['sales_account'] = '';
            $_POST['sales_discount_account'] = $company_record['default_sales_discount_act'];
            $_POST['receivables_account'] = $company_record['debtors_act'];
            $_POST['payment_discount_account'] = $company_record['default_prompt_payment_act'];
@@ -298,7 +301,9 @@ echo"<table>";
 
 table_section_title(_("GL Accounts"));
 
-gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account']);
+// 2006-06-14. Changed gl_al_accounts_list to have an optional all_option 'Use Item Sales Accounts'
+gl_all_accounts_list_row(_("Sales Account:"), 'sales_account', $_POST['sales_account'], false,
+       false, false, true);
 
 gl_all_accounts_list_row(_("Sales Discount Account:"), 'sales_discount_account', $_POST['sales_discount_account']);