Moving 2.0 development version to main trunk.
[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                 // we need to negate it too
71                 $cust_amount = -$cust_amount;
72
73                 $trans_no = write_customer_trans($trans_type, 0, $person_id, $person_detail_id, $date_,
74                 $ref, $cust_amount);
75
76     }
77     elseif ($person_type_id == payment_person_types::supplier())
78     {
79         // we need to add a supplier transaction record
80                 // convert to supp currency
81                 $supp_amount = exchange_from_to($total_amount, $currency, get_supplier_currency($person_id), $date_);
82
83                 // we need to negate it too
84                 $supp_amount = -$supp_amount;
85
86                 $trans_no = add_supp_trans($trans_type, $person_id, $date_, '',
87                 $ref, "", $supp_amount, 0, 0);
88
89     }
90     else
91     {
92         $trans_no = get_next_trans_no($trans_type);
93     }
94
95         // do the source account postings
96     add_gl_trans($trans_type, $trans_no, $date_, $from_account, 0, 0, "",
97         -$total_amount, $currency, $person_type_id, $person_id);
98
99     add_bank_trans($trans_type, $trans_no, $from_account, $ref,
100         $date_, $type, -$total_amount,
101         $person_type_id, $person_id,
102         $currency,
103         "Cannot insert a source bank transaction");
104
105         foreach ($items->gl_items as $gl_item)
106         {
107                 $is_bank_to = is_bank_account($gl_item->code_id);
108
109                 if ($trans_type == 1 AND $is_bank_to)
110                 {
111                         // we don't allow payments to go to a bank account. use transfer for this !
112                         display_db_error("invalid payment entered. Cannot pay to another bank account", "");
113                 }
114
115         // do the destination account postings
116         add_gl_trans($trans_type, $trans_no, $date_, $gl_item->code_id,
117                 $gl_item->dimension_id, $gl_item->dimension2_id, $gl_item->reference,
118                 $gl_item->amount, $currency, $person_type_id, $person_id);
119
120         if ($is_bank_to)
121         {
122                 add_bank_trans($trans_type, $trans_no, $gl_item->code_id, $ref,
123                         $date_, $type, $gl_item->amount,
124                         $person_type_id, $person_id, $currency,
125                         "Cannot insert a destination bank transaction");
126         }
127         }
128
129         add_comments($trans_type, $trans_no, $date_, $memo_);
130
131         references::save_last($ref, $trans_type);
132
133         commit_transaction();
134
135         return array($trans_type, $trans_no);
136 }
137
138 //----------------------------------------------------------------------------------------
139
140 ?>