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