[0000095] Inbalance double entry on Documents (2. run)
[fa-stable.git] / gl / includes / db / gl_db_banking.inc
1 <?php
2
3 //----------------------------------------------------------------------------------
4
5 function add_bank_transfer($from_account, $to_account, $date_,
6         $amount, $payment_type, $ref, $memo_)
7 {
8         begin_transaction();
9
10         $trans_type = systypes::bank_transfer();
11
12         $currency = get_bank_account_currency($from_account);
13
14         $trans_no = get_next_trans_no($trans_type);
15
16         $total = 0;
17         // do the source account postings
18     $total += add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
19                 -$amount, $currency);
20
21     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
22                 $date_, $payment_type, -$amount,
23                 payment_person_types::misc(), "", $currency,
24                 "Cannot insert a source bank transaction");
25
26         // do the destination account postings
27         $total += add_gl_trans($trans_type, $trans_no, $date_, $to_account, 0, 0, "",
28                 $amount, $currency);
29         /*Post a balance post if $total != 0 */
30         add_gl_balance($trans_type, $trans_no, $date_, -$total);        
31         
32         add_bank_trans($trans_type, $trans_no, $to_account, $ref,
33                 $date_, $payment_type, $amount,
34                 payment_person_types::misc(), "",
35                 $currency,
36                 "Cannot insert a destination bank transaction");
37
38         add_comments($trans_type, $trans_no, $date_, $memo_);
39
40         references::save_last($ref, $trans_type);
41
42         commit_transaction();
43
44         return $trans_no;
45 }
46
47 //----------------------------------------------------------------------------------
48
49 // $amount is in $from_account's currency
50
51 // returns an array of (inserted trans type, trans no)
52
53 function add_bank_transaction($trans_type, $from_account, $items, $date_,
54         $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
55 {
56         // we can only handle type 1 (payment)and type 2 (deposit)
57         if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
58                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
59
60         begin_transaction();
61
62         $currency = get_bank_account_currency($from_account);
63
64         // the gl items are already inversed/negated for type 2 (deposit)
65         $total_amount = $items->gl_items_total();
66
67     if ($person_type_id == payment_person_types::customer())
68     {
69         // we need to add a customer transaction record
70
71                 // convert to customer currency
72                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
73                 // we need to negate it too
74                 $cust_amount = -$cust_amount;
75
76                 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
77                 $ref, $cust_amount);
78
79     }
80     elseif ($person_type_id == payment_person_types::supplier())
81     {
82         // we need to add a supplier transaction record
83                 // convert to supp currency
84                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
85
86                 // we need to negate it too
87                 $supp_amount = -$supp_amount;
88
89                 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
90                 $ref, "", $supp_amount, 0, 0);
91
92     }
93     else
94     {
95         $trans_no = get_next_trans_no($trans_type);
96     }
97
98     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
99         $date_, $type, -$total_amount,
100         $person_type_id, $person_id,
101         $currency,
102         "Cannot insert a source bank transaction");
103         $total = 0;
104         foreach ($items->gl_items as $gl_item)
105         {
106                 $is_bank_to = is_bank_account($gl_item->code_id);
107
108                 if ($trans_type == 1 AND $is_bank_to)
109                 {
110                         // we don't allow payments to go to a bank account. use transfer for this !
111                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
112                 }
113
114         // do the destination account postings
115         $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
116                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
117                 $gl_item->amount, $currency, $person_type_id, $person_id);
118
119         if ($is_bank_to)
120         {
121                 add_bank_trans($trans_type, $trans_no, $gl_item->code_id, $ref,
122                         $date_, $type, $gl_item->amount,
123                         $person_type_id, $person_id, $currency,
124                         "Cannot insert a destination bank transaction");
125         }
126         }
127
128         // do the source account postings
129     add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
130         -$total, null, $person_type_id, $person_id);
131
132         add_comments($trans_type, $trans_no, $date_, $memo_);
133
134         references::save_last($ref, $trans_type);
135
136         commit_transaction();
137
138         return array($trans_type, $trans_no);
139 }
140
141 //----------------------------------------------------------------------------------------
142
143 ?>