Allowed multiply bank accounts on same gl account, removed bank trans type.
[fa-stable.git] / gl / includes / db / gl_db_bank_trans.inc
1 <?php
2
3 //----------------------------------------------------------------------------------
4
5 // add a bank transaction
6 // $amount is in $currency
7 // $date_ is display date (non-sql)
8
9 function add_bank_trans($type, $trans_no, $bank_act, $ref, $date_,
10         $amount, $person_type_id, $person_id, $currency="", $err_msg="")
11 {
12         $sqlDate = date2sql($date_);
13
14         // convert $amount to the bank's currency
15         if ($currency != "")
16         {
17         $bank_account_currency = get_bank_account_currency($bank_act);
18         $to_bank_currency = get_exchange_rate_from_to($currency, $bank_account_currency, $date_);
19         $amount_bank = ($amount / $to_bank_currency);
20         }
21         else
22                 $amount_bank = $amount;
23
24
25         // Also store the rate to the home
26         //$BankToHomeCurrencyRate = get_exchange_rate_to_home_currency($bank_account_currency, $date_);
27
28         $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, bank_act, ref,
29                 trans_date, amount, person_type_id, person_id) ";
30
31         $sql .= "VALUES ($type, $trans_no, '$bank_act', ".db_escape($ref).", '$sqlDate',
32                 $amount_bank, $person_type_id, ". db_escape($person_id).")";
33
34         if ($err_msg == "")
35                 $err_msg = "The bank transaction could not be inserted";
36
37         db_query($sql, $err_msg);
38 }
39
40 //----------------------------------------------------------------------------------------
41
42 function exists_bank_trans($type, $type_no)
43 {
44         $sql = "SELECT trans_no FROM ".TB_PREF."bank_trans WHERE type=$type
45                 AND trans_no=$type_no";
46         $result = db_query($sql, "Cannot retreive a bank transaction");
47
48     return (db_num_rows($result) > 0);
49 }
50
51 //----------------------------------------------------------------------------------------
52
53 function get_bank_trans($type, $trans_no=null, $person_type_id=null, $person_id=null)
54 {
55         $sql = "SELECT *, bank_account_name, account_code, bank_curr_code
56                 FROM ".TB_PREF."bank_trans, ".TB_PREF."bank_accounts
57                 WHERE ".TB_PREF."bank_accounts.id=".TB_PREF."bank_trans.bank_act ";
58         if ($type != null)
59                 $sql .= " AND type=$type ";
60         if ($trans_no != null)
61                 $sql .= " AND ".TB_PREF."bank_trans.trans_no = $trans_no ";
62         if ($person_type_id != null)
63                 $sql .= " AND ".TB_PREF."bank_trans.person_type_id = $person_type_id ";
64         if ($person_id != null)
65                 $sql .= " AND ".TB_PREF."bank_trans.person_id = '$person_id'";
66         $sql .= " ORDER BY trans_date, ".TB_PREF."bank_trans.id";
67
68         return db_query($sql, "query for bank transaction");
69 }
70
71 //----------------------------------------------------------------------------------------
72
73 function get_gl_trans_value($account, $type, $trans_no)
74 {
75         $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE account='$account' AND type=$type AND type_no=$trans_no";
76
77         $result = db_query($sql, "query for gl trans value");
78
79         $row = db_fetch_row($result);
80         return $row[0];
81 }
82
83 //----------------------------------------------------------------------------------------
84
85 function void_bank_trans($type, $type_no, $nested=false)
86 {
87         if (!$nested)
88                 begin_transaction();
89
90         $sql = "UPDATE ".TB_PREF."bank_trans SET amount=0
91                 WHERE type=$type AND trans_no=$type_no";
92
93         $result = db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
94
95         void_gl_trans($type, $type_no, true);
96
97         // in case it's a customer trans - probably better to check first
98         void_cust_allocations($type, $type_no);
99         void_customer_trans($type, $type_no);
100
101         // in case it's a supplier trans - probably better to check first
102         void_supp_allocations($type, $type_no);
103         void_supp_trans($type, $type_no);
104
105         if (!$nested)
106                 commit_transaction();
107 }
108
109 //----------------------------------------------------------------------------------
110
111 ?>