Fixed an accounting sync in GL when adding bank account transactions (no customer...
[fa-stable.git] / gl / includes / db / gl_db_banking.inc
1 <?php
2
3 //----------------------------------------------------------------------------------
4 function add_exchange_variation($trans_type, $trans_no, $date_, $account,
5     $currency, $person_type_id=null, $person_id = "")
6 {
7         if (is_company_currency($currency))
8                 return;
9         if ($date_ == null)
10                 $date_ = Today();
11         $rate = get_exchange_rate_from_home_currency($currency, $date_);        
12         $result = db_query("SELECT SUM(amount) FROM ".TB_PREF."bank_trans WHERE 
13                 bank_act='$account' AND trans_date<='".date2sql($date_)."'", 
14                 "Transactions for account $account could not be calculated");
15         $row = db_fetch_row($result);
16         $foreign_amount = $row[0];
17         $amount = get_gl_trans_from_to("", $date_, $account);
18         $diff = $amount - (round2($foreign_amount * $rate, user_price_dec()));
19         if ($diff != 0)
20         {
21                 if ($trans_type == null)
22                         $trans_type = systypes::journal_entry();
23                 if ($trans_no == null)
24                         $trans_no = get_next_trans_no($trans_type);
25                 if ($person_type_id == null)
26                         $person_type_id = payment_person_types::misc();
27                 add_gl_trans($trans_type, $trans_no, $date_, $account, 0, 0, "",
28                 -$diff, null, $person_type_id, $person_id);
29         add_gl_trans($trans_type, $trans_no, $date_, get_company_pref('exchange_diff_act'), 0, 0, "",
30                 $diff, null, $person_type_id, $person_id);
31         }       
32 }
33
34 function add_exchange_variation_all()
35 {
36         $trans_no = get_next_trans_no(0);
37         $sql = "SELECT * FROM ".TB_PREF."bank_accounts";
38         $result = db_query($sql, "could not retreive bank accounts");
39         while ($myrow = db_fetch($result))
40                 add_exchange_variation(0, $trans_no, null, $myrow['account_code'],
41                         $myrow['currency_code']);
42 }
43 //----------------------------------------------------------------------------------
44
45 function add_bank_transfer($from_account, $to_account, $date_,
46         $amount, $payment_type, $ref, $memo_)
47 {
48         begin_transaction();
49
50         $trans_type = systypes::bank_transfer();
51
52         $currency = get_bank_account_currency($from_account);
53
54         $trans_no = get_next_trans_no($trans_type);
55
56         $total = 0;
57         // do the source account postings
58     $total += add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
59                 -$amount, $currency);
60
61     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
62                 $date_, $payment_type, -$amount,
63                 payment_person_types::misc(), "", $currency,
64                 "Cannot insert a source bank transaction");
65
66         add_exchange_variation($trans_type, $trans_no, $date_, $from_account, 
67                 $currency, payment_person_types::misc(), "");
68
69         // do the destination account postings
70         $total += add_gl_trans($trans_type, $trans_no, $date_, $to_account, 0, 0, "",
71                 $amount, $currency);
72                 
73         /*Post a balance post if $total != 0 */
74         add_gl_balance($trans_type, $trans_no, $date_, -$total);        
75         
76         add_bank_trans($trans_type, $trans_no, $to_account, $ref,
77                 $date_, $payment_type, $amount,
78                 payment_person_types::misc(), "",
79                 $currency,
80                 "Cannot insert a destination bank transaction");
81
82         add_exchange_variation($trans_type, $trans_no, $date_, $from_account, 
83                 $currency, payment_person_types::misc(), "");
84         
85         add_comments($trans_type, $trans_no, $date_, $memo_);
86
87         references::save_last($ref, $trans_type);
88
89         commit_transaction();
90
91         return $trans_no;
92 }
93
94 //----------------------------------------------------------------------------------
95
96 // $amount is in $from_account's currency
97
98 // returns an array of (inserted trans type, trans no)
99
100 function add_bank_transaction($trans_type, $from_account, $items, $date_,
101         $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
102 {
103         // we can only handle type 1 (payment)and type 2 (deposit)
104         if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
105                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
106
107         $do_exchange_variance = false;
108         
109         begin_transaction();
110
111         $currency = get_bank_account_currency($from_account);
112
113         // the gl items are already inversed/negated for type 2 (deposit)
114         $total_amount = $items->gl_items_total();
115
116     if ($person_type_id == payment_person_types::customer())
117     {
118         // we need to add a customer transaction record
119
120                 // convert to customer currency
121                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
122                 // we need to negate it too
123                 $cust_amount = -$cust_amount;
124
125                 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
126                 $ref, $cust_amount);
127
128     }
129     elseif ($person_type_id == payment_person_types::supplier())
130     {
131         // we need to add a supplier transaction record
132                 // convert to supp currency
133                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
134
135                 // we need to negate it too
136                 $supp_amount = -$supp_amount;
137
138                 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
139                 $ref, "", $supp_amount, 0, 0);
140
141     }
142     else
143     {
144         $trans_no = get_next_trans_no($trans_type);
145         $do_exchange_variance = true;
146     }
147
148     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
149         $date_, $type, -$total_amount,
150         $person_type_id, $person_id,
151         $currency,
152         "Cannot insert a source bank transaction");
153         $total = 0;
154         foreach ($items->gl_items as $gl_item)
155         {
156                 $is_bank_to = is_bank_account($gl_item->code_id);
157
158                 if ($trans_type == 1 AND $is_bank_to)
159                 {
160                         // we don't allow payments to go to a bank account. use transfer for this !
161                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
162                 }
163
164         // do the destination account postings
165         $total += add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
166                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
167                 $gl_item->amount, $currency, $person_type_id, $person_id);
168
169         if ($is_bank_to)
170         {
171                 add_bank_trans($trans_type, $trans_no, $gl_item->code_id, $ref,
172                         $date_, $type, $gl_item->amount,
173                         $person_type_id, $person_id, $currency,
174                         "Cannot insert a destination bank transaction");
175                 if ($do_exchange_variance)
176                         add_exchange_variation($trans_type, $trans_no, $date_, $gl_item->code_id, 
177                                 $currency, $person_type_id, $person_id);
178         }
179         }
180
181         // do the source account postings
182     add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
183         -$total, null, $person_type_id, $person_id);
184
185     if ($do_exchange_variance)
186         add_exchange_variation($trans_type, $trans_no, $date_, $from_account, 
187                 $currency, $person_type_id, $person_id);
188
189         add_comments($trans_type, $trans_no, $date_, $memo_);
190
191         references::save_last($ref, $trans_type);
192
193         commit_transaction();
194
195         return array($trans_type, $trans_no);
196 }
197
198 //----------------------------------------------------------------------------------------
199
200 ?>