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