Fixed numeric fields to accept user native number format.
[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         // do the source account postings
17     add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
18                 -$amount, $currency);
19
20     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
21                 $date_, $payment_type, -$amount,
22                 payment_person_types::misc(), "", $currency,
23                 "Cannot insert a source bank transaction");
24
25         // do the destination account postings
26         add_gl_trans($trans_type, $trans_no, $date_, $to_account, 0, 0, "",
27                 $amount, $currency);
28
29         add_bank_trans($trans_type, $trans_no, $to_account, $ref,
30                 $date_, $payment_type, $amount,
31                 payment_person_types::misc(), "",
32                 $currency,
33                 "Cannot insert a destination bank transaction");
34
35         add_comments($trans_type, $trans_no, $date_, $memo_);
36
37         references::save_last($ref, $trans_type);
38
39         commit_transaction();
40
41         return $trans_no;
42 }
43
44 //----------------------------------------------------------------------------------
45
46 // $amount is in $from_account's currency
47
48 // returns an array of (inserted trans type, trans no)
49
50 function add_bank_transaction($trans_type, $from_account, $items, $date_,
51         $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
52 {
53         // we can only handle type 1 (payment)and type 2 (deposit)
54         if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
55                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
56
57         begin_transaction();
58
59         $currency = get_bank_account_currency($from_account);
60
61         // the gl items are already inversed/negated for type 2 (deposit)
62         $total_amount = $items->gl_items_total();
63
64     if ($person_type_id == payment_person_types::customer())
65     {
66         // we need to add a customer transaction record
67
68                 // convert to customer currency
69                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
70
71                 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
72                 $ref, $cust_amount);
73
74     }
75     elseif ($person_type_id == payment_person_types::supplier())
76     {
77         // we need to add a supplier transaction record
78                 // convert to supp currency
79                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
80
81                 // we need to negate it too
82                 $supp_amount = -$supp_amount;
83
84                 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
85                 $ref, "", $supp_amount, 0, 0);
86
87     }
88     else
89     {
90         $trans_no = get_next_trans_no($trans_type);
91     }
92
93         // do the source account postings
94     add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
95         -$total_amount, $currency, $person_type_id, $person_id);
96
97     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
98         $date_, $type, -$total_amount,
99         $person_type_id, $person_id,
100         $currency,
101         "Cannot insert a source bank transaction");
102
103         foreach ($items->gl_items as $gl_item)
104         {
105                 $is_bank_to = is_bank_account($gl_item->code_id);
106
107                 if ($trans_type == 1 AND $is_bank_to)
108                 {
109                         // we don't allow payments to go to a bank account. use transfer for this !
110                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
111                 }
112
113         // do the destination account postings
114         add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
115                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
116                 $gl_item->amount, $currency, $person_type_id, $person_id);
117
118         if ($is_bank_to)
119         {
120                 add_bank_trans($trans_type, $trans_no, $gl_item->code_id, $ref,
121                         $date_, $type, $gl_item->amount,
122                         $person_type_id, $person_id, $currency,
123                         "Cannot insert a destination bank transaction");
124         }
125         }
126
127         add_comments($trans_type, $trans_no, $date_, $memo_);
128
129         references::save_last($ref, $trans_type);
130
131         commit_transaction();
132
133         return array($trans_type, $trans_no);
134 }
135
136 //----------------------------------------------------------------------------------------
137
138 function add_bank_payment($from_account, $items, $date_,
139         $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
140 {
141         return add_bank_transaction(systypes::bank_payment(), $from_account, $items, $date_,
142                 $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_);
143 }
144
145 //---------------------------------------------------------------------------------------------
146
147 function add_bank_deposit($from_account, $items, $date_,
148         $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
149 {
150         return add_bank_transaction(systypes::bank_deposit(), $from_account, $items, $date_,
151                 $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_);
152 }
153
154 //---------------------------------------------------------------------------------------------
155
156
157 ?>