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