[0000095] Inbalance double entry on Documents (2. run)
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Thu, 4 Dec 2008 18:51:28 +0000 (18:51 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Thu, 4 Dec 2008 18:51:28 +0000 (18:51 +0000)
gl/includes/db/gl_db_banking.inc
gl/includes/db/gl_db_trans.inc
purchasing/includes/db/invoice_db.inc
purchasing/includes/db/supp_payment_db.inc
sales/includes/db/payment_db.inc
sales/includes/db/sales_credit_db.inc
sales/includes/db/sales_invoice_db.inc

index 799d37fb22acfea583790455381f8537dafbbcdc..4bf4687b6c6d45928cbb9e6d4ee23e25dd2ce762 100644 (file)
@@ -24,9 +24,11 @@ function add_bank_transfer($from_account, $to_account, $date_,
                "Cannot insert a source bank transaction");
 
        // do the destination account postings
-       add_gl_trans($trans_type, $trans_no, $date_, $to_account, 0, 0, "",
-               -$total, null);
-
+       $total += add_gl_trans($trans_type, $trans_no, $date_, $to_account, 0, 0, "",
+               $amount, $currency);
+       /*Post a balance post if $total != 0 */
+       add_gl_balance($trans_type, $trans_no, $date_, -$total);        
+       
        add_bank_trans($trans_type, $trans_no, $to_account, $ref,
                $date_, $payment_type, $amount,
                payment_person_types::misc(), "",
index fe547a8954957706cad7c195aac1e7b44101bf82..c681a0fbbc09de9ca08dda7a5f36c892ce5e64a1 100644 (file)
@@ -72,6 +72,17 @@ function add_gl_trans_std_cost($type, $trans_id, $date_, $account, $dimension, $
                return 0;
 }
 
+// Function for even out rounding problems
+function add_gl_balance($type, $trans_id, $date_, $amount, $person_type_id=null, $person_id=null)
+{
+       $amount = round($amount, user_price_dec());
+       if ($amount != 0)
+               return add_gl_trans($type, $trans_id, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
+                       $amount, null, $person_type_id, $person_id, "The balanced GL transaction could not be inserted");
+       else
+               return 0;
+}      
+
 //--------------------------------------------------------------------------------
 
 function get_gl_transactions($from_date, $to_date, $trans_no=0,
index b19897da6b2ffd0c1007250f5b4df81488c6ec7f..fe26bf44d567cae680e124aef047738123b99602 100644 (file)
@@ -141,8 +141,13 @@ function add_supp_invoice($supp_trans) // do not receive as ref because we chang
                $supp_trans->reference, $supp_trans->supp_reference,
                $invoice_items_total, $tax_total, $supp_trans->ov_discount);
 
+       $total = 0;
     /* Now the control account */
     $supplier_accounts = get_supplier_accounts($supp_trans->supplier_id);
+    $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $supplier_accounts["payable_account"], 0, 0,
+               -($invoice_items_total +  $tax_total + $supp_trans->ov_discount),
+               $supp_trans->supplier_id,
+               "The general ledger transaction for the control total could not be added");
 
     /*Loop through the GL Entries and create a debit posting for each of the accounts entered */
 
@@ -155,7 +160,6 @@ function add_supp_invoice($supp_trans) // do not receive as ref because we chang
     between the std cost and the currency cost charged as converted at the ex rate of of the invoice is written off
     to the purchase price variance account applicable to the item being invoiced.
     */
-       $total = 0;
     foreach ($supp_trans->gl_codes as $entered_gl_code)
     {
 
@@ -257,9 +261,10 @@ function add_supp_invoice($supp_trans) // do not receive as ref because we chang
                        "A general ledger transaction for the tax amount could not be added");
        }
     }
-       add_gl_trans($trans_type, $invoice_id, $date_, $supplier_accounts["payable_account"], 0, 0, "",
-               -$total, null, payment_person_types::supplier(), $supp_trans->supplier_id, 
-               "The general ledger transaction for the control total could not be added");
+       
+       /*Post a balance post if $total != 0 */
+       add_gl_balance($trans_type, $invoice_id, $date_, -$total, payment_person_types::supplier(), $supp_trans->supplier_id);  
+
        add_comments($trans_type, $invoice_id, $date_, $supp_trans->Comments);
 
        references::save_last($supp_trans->reference, $trans_type);
index ce85db523f6c3cdd9187d188e385e223ef6d05f5..48e4c8cb774d4b978a9ec00a3b043bd1f18d263a 100644 (file)
@@ -29,9 +29,11 @@ function add_supp_payment($supplier_id, $date_, $payment_type, $bank_account,
 
        // Now debit creditors account with payment + discount
 
+       $total = 0;
     $supplier_accounts = get_supplier_accounts($supplier_id);
+       $total += add_gl_trans_supplier($trans_type, $payment_id, $date_, $supplier_accounts["payable_account"], 0, 0,
+               $supp_amount + $supp_discount, $supplier_id, "", $rate);
 
-       $total = 0;
        // Now credit discount received account with discounts
        if ($supp_discount != 0)
        {
@@ -45,8 +47,9 @@ function add_supp_payment($supplier_id, $date_, $payment_type, $bank_account,
                        -$supp_amount, $supplier_id, "", $rate);
        }
 
-       add_gl_trans($trans_type, $payment_id, $date_, $supplier_accounts["payable_account"], 0, 0, "",
-               -$total, null, payment_person_types::supplier(), $supplier_id);
+       /*Post a balance post if $total != 0 */
+       add_gl_balance($trans_type, $payment_id, $date_, -$total, payment_person_types::supplier(), $supplier_id);      
+
    /*now enter the bank_trans entry */
        add_bank_trans($trans_type, $payment_id, $bank_account, $ref,
                $date_, $payment_type, -($amount), payment_person_types::supplier(),
index 1d7dfa0651df59ff85dcc690db1e27830fea507c..75392b6a2d303f368edec2ef1ff0cf59f1cc1120 100644 (file)
@@ -36,6 +36,12 @@ function write_customer_payment($trans_no, $customer_id, $branch_id, $bank_accou
                $discount_account = $company_record["default_prompt_payment_act"];
        }
 
+       if (($discount + $amount) != 0) {
+               /* Now Credit Debtors account with receipts + discounts */
+               $total += add_gl_trans_customer(12, $payment_no, $date_,
+                       $debtors_account, 0, 0, -($discount + $amount), $customer_id,
+                       "Cannot insert a GL transaction for the debtors account credit", $rate);
+       }
        if ($discount != 0)     {
                /* Now Debit discount account with discounts allowed*/
                $total += add_gl_trans_customer(12, $payment_no, $date_,
@@ -43,12 +49,8 @@ function write_customer_payment($trans_no, $customer_id, $branch_id, $bank_accou
                        "Cannot insert a GL transaction for the payment discount debit", $rate);
        }
 
-       if (($discount + $amount) != 0) {
-               /* Now Credit Debtors account with receipts + discounts */
-               add_gl_trans(12, $payment_no, $date_, $debtors_account, 0, 0, "",
-                       -$total, null, payment_person_types::customer(), $customer_id, 
-                       "Cannot insert a GL transaction for the debtors account credit");
-       }
+       /*Post a balance post if $total != 0 */
+       add_gl_balance(12, $payment_no, $date_, -$total, payment_person_types::customer(), $customer_id);       
 
        /*now enter the bank_trans entry */
        add_bank_trans(12, $payment_no, $bank_account, $ref,
index d196ee062ae7a8cc096d361ec62a8087100c14fc..5629c5286fe9402d919d9e31c7b31f7a780499fc 100644 (file)
@@ -123,6 +123,16 @@ function write_credit_note($credit_note, $write_off_acc)
                    $credit_date, $credit_type, $write_off_acc, $branch_data);
        } /*end of credit_line loop */
 
+       /*Post credit note transaction to GL credit debtors,
+       debit freight re-charged and debit sales */
+
+       if (($credit_note_total + $credit_note->freight_cost) != 0) {
+
+               $total += add_gl_trans_customer(11, $credit_no, $credit_date, $branch_data["receivables_account"], 0, 0,
+                       -($credit_note_total + $credit_note->freight_cost + $items_added_tax + $freight_added_tax),
+                       $credit_note->customer_id,
+                       "The total debtor GL posting for the credit note could not be inserted");
+       }
 
        if ($credit_note->freight_cost !=0) {
                $total += add_gl_trans_customer(11, $credit_no, $credit_date, $company_data["freight_act"], 0, 0,
@@ -141,15 +151,9 @@ function write_credit_note($credit_note, $write_off_acc)
                                "A tax GL posting for this credit note could not be inserted");
                }
        }
-       /*Post credit note transaction to GL credit debtors,
-       debit freight re-charged and debit sales */
-
-       if (($credit_note_total + $credit_note->freight_cost) != 0) {
+       /*Post a balance post if $total != 0 */
+       add_gl_balance(11, $credit_no, $credit_date, -$total, payment_person_types::customer(), $credit_note->customer_id);     
 
-               add_gl_trans(11, $credit_no, $credit_date, $branch_data["receivables_account"], 0, 0, "",
-                       -$total, null, payment_person_types::customer(), $credit_note->customer_id, 
-                       "The total debtor GL posting for the credit note could not be inserted");
-       }
        add_comments(11, $credit_no, $credit_date, $credit_note->Comments);
 
        if ($trans_no == 0) {
index 112dbe13d955ed47b744763a8b911b7e3e5aa69d..d01fc36b75589ee7ae5a3abe79bc96ae00707a50 100644 (file)
@@ -121,6 +121,12 @@ function write_sales_invoice(&$invoice)
                } /*quantity dispatched is more than 0 */
        } /*end of delivery_line loop */
 
+       if (($items_total + $charge_shipping) != 0) {
+               $total += add_gl_trans_customer(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0,
+                       ($items_total + $charge_shipping + $items_added_tax + $freight_added_tax),
+                       $invoice->customer_id, "The total debtor GL posting could not be inserted");
+       }
+
        if ($charge_shipping != 0) {
                $total += add_gl_trans_customer(10, $invoice_no, $date_, $company_data["freight_act"], 0, 0,
                        -$invoice->get_tax_free_shipping(), $invoice->customer_id,
@@ -138,12 +144,9 @@ function write_sales_invoice(&$invoice)
                }
        }
 
-       /*Post debtors transaction to GL debit debtors, credit freight re-charged and credit sales */
-       if (($items_total + $charge_shipping) != 0) {
-               add_gl_trans(10, $invoice_no, $date_, $branch_data["receivables_account"], 0, 0, "",
-                       -$total, null, payment_person_types::customer(), $invoice->customer_id, 
-                       "The total debtor GL posting could not be inserted");
-       }
+       /*Post a balance post if $total != 0 */
+       add_gl_balance(10, $invoice_no, $date_, -$total, payment_person_types::customer(), $invoice->customer_id);      
+
        add_comments(10, $invoice_no, $date_, $invoice->Comments);
 
        if ($trans_no == 0) {