*** empty log message ***
[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         add_forms_for_sys_type($trans_type,     $trans_no, $from_account, $to_account);         
38         
39         references::save_last($ref, $trans_type);                                                                       
40         
41         commit_transaction();
42         
43         return $trans_no;       
44 }
45
46 //----------------------------------------------------------------------------------
47
48 // $amount is in $from_account's currency
49
50 // returns an array of (inserted trans type, trans no)
51
52 function add_bank_transaction($trans_type, $from_account, $items, $date_,
53         $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
54 {
55         // we can only handle type 1 (payment)and type 2 (deposit)
56         if ($trans_type != systypes::bank_payment() && $trans_type != systypes::bank_deposit())
57                 display_db_error("Invalid type ($trans_type) sent to add_bank_transaction");
58
59         begin_transaction();
60         
61         $currency = get_bank_account_currency($from_account);   
62         
63         // the gl items are already inversed/negated for type 2 (deposit)
64         $total_amount = $items->gl_items_total();
65         
66     if ($person_type_id == payment_person_types::customer()) 
67     {
68         // we need to add a customer transaction record
69                 
70                 // convert to customer currency
71                 $cust_amount = exchange_from_to($total_amount, $currency, get_customer_currency($person_id), $date_);
72                 
73                 $trans_no = add_customer_trans($trans_type, $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         add_forms_for_sys_type($trans_type,     $trans_no, $person_type_id, $person_id);        
132         
133         references::save_last($ref, $trans_type);       
134         
135         commit_transaction();
136         
137         return array($trans_type, $trans_no);
138 }
139
140 //----------------------------------------------------------------------------------------
141
142 function add_bank_payment($from_account, $items, $date_, 
143         $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
144 {
145         return add_bank_transaction(systypes::bank_payment(), $from_account, $items, $date_,
146                 $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_);
147 }
148
149 //---------------------------------------------------------------------------------------------
150
151 function add_bank_deposit($from_account, $items, $date_, 
152         $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_)
153 {
154         return add_bank_transaction(systypes::bank_deposit(), $from_account, $items, $date_,
155                 $person_type_id, $person_id, $person_detail_id, $type, $ref, $memo_);
156 }
157
158 //---------------------------------------------------------------------------------------------
159
160
161 ?>