[0000101] More wonderful rounding issues. Rerun.
[fa-stable.git] / sales / includes / db / payment_db.inc
1 <?php
2 /*
3   Write/update customer payment.
4 */
5 function write_customer_payment($trans_no, $customer_id, $branch_id, $bank_account,
6         $date_, $receipt_type, $ref, $amount, $discount, $memo_, $rate=0)
7 {
8         begin_transaction();
9
10         $company_record = get_company_prefs();
11
12         $payment_no = write_customer_trans(12, $trans_no, $customer_id, $branch_id, 
13                 $date_, $ref, $amount, $discount, 0, 0, 0, 0, 0, 0, 0, "", 0, $rate);
14
15         if ($trans_no != 0) {
16           delete_comments(12, $trans_no);
17           void_bank_trans(12, $trans_no, true);
18           void_gl_trans(12, $trans_no, true);
19           void_cust_allocations(12, $trans_no, $date_);
20         }
21         $total = 0;
22         /* Bank account entry first */
23         $total += add_gl_trans_customer(12, $payment_no, $date_,
24                 $bank_account, 0, 0, $amount,  $customer_id,
25                 "Cannot insert a GL transaction for the bank account debit", $rate);
26
27         if ($branch_id != reserved_words::get_any_numeric()) {
28
29                 $branch_data = get_branch_accounts($branch_id);
30
31                 $debtors_account = $branch_data["receivables_account"];
32                 $discount_account = $branch_data["payment_discount_account"];
33
34         } else {
35                 $debtors_account = $company_record["debtors_act"];
36                 $discount_account = $company_record["default_prompt_payment_act"];
37         }
38
39         if ($discount != 0)     {
40                 /* Now Debit discount account with discounts allowed*/
41                 $total += add_gl_trans_customer(12, $payment_no, $date_,
42                         $discount_account, 0, 0, $discount, $customer_id,
43                         "Cannot insert a GL transaction for the payment discount debit", $rate);
44         }
45
46         if (($discount + $amount) != 0) {
47                 /* Now Credit Debtors account with receipts + discounts */
48                 $total += add_gl_trans(12, $payment_no, $date_, $debtors_account, 0, 0, "", 
49                         -$total, null, payment_person_types::customer(), $customer_id, 
50                         "Cannot insert a GL transaction for the debtors account credit");
51         }
52
53         /*Post a balance post if $total != 0 */
54         add_gl_balance(12, $payment_no, $date_, -$total, payment_person_types::customer(), $customer_id);       
55
56         /*now enter the bank_trans entry */
57         add_bank_trans(12, $payment_no, $bank_account, $ref,
58                 $date_, $receipt_type, $amount, payment_person_types::customer(), $customer_id,
59                 get_customer_currency($customer_id));
60
61         add_comments(12, $payment_no, $date_, $memo_);
62
63         references::save_last($ref, 12);
64
65         commit_transaction();
66
67         return $payment_no;
68 }
69
70 //-------------------------------------------------------------------------------------------------
71
72 function void_customer_payment($type, $type_no)
73 {
74         begin_transaction();
75
76         void_bank_trans($type, $type_no, true);
77         void_gl_trans($type, $type_no, true);
78         void_cust_allocations($type, $type_no);
79         void_customer_trans($type, $type_no);
80
81         commit_transaction();
82 }
83
84
85 ?>