6aebe6f4a2dab753be9f0342eee334224039cd24
[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_, $bank_trans_type_id,
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, bank_trans_type_id, amount, person_type_id, person_id) ";
30
31         $sql .= "VALUES ($type, $trans_no, '$bank_act', ".db_escape($ref).", '$sqlDate', '$bank_trans_type_id',
32                 $amount_bank, $person_type_id, '$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, name AS BankTransType
56                 FROM ".TB_PREF."bank_trans, ".TB_PREF."bank_accounts, ".TB_PREF."bank_trans_types
57                 WHERE ".TB_PREF."bank_trans_types.id = ".TB_PREF."bank_trans.bank_trans_type_id
58                 AND ".TB_PREF."bank_accounts.account_code=".TB_PREF."bank_trans.bank_act ";
59         if ($type != null)
60                 $sql .= " AND type=$type ";
61         if ($trans_no != null)
62                 $sql .= " AND ".TB_PREF."bank_trans.trans_no = $trans_no ";
63         if ($person_type_id != null)
64                 $sql .= " AND ".TB_PREF."bank_trans.person_type_id = $person_type_id ";
65         if ($person_id != null)
66                 $sql .= " AND ".TB_PREF."bank_trans.person_id = '$person_id'";
67         $sql .= " ORDER BY trans_date, ".TB_PREF."bank_trans.id";
68
69         return db_query($sql, "query for bank transaction");
70 }
71
72 //----------------------------------------------------------------------------------------
73
74 function get_gl_trans_value($account, $type, $trans_no)
75 {
76         $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE account='$account' AND type=$type AND type_no=$trans_no";
77
78         $result = db_query($sql, "query for gl trans value");
79
80         $row = db_fetch_row($result);
81         return $row[0];
82 }
83
84 //----------------------------------------------------------------------------------------
85
86 function void_bank_trans($type, $type_no, $nested=false)
87 {
88         if (!$nested)
89                 begin_transaction();
90
91         $sql = "UPDATE ".TB_PREF."bank_trans SET amount=0
92                 WHERE type=$type AND trans_no=$type_no";
93
94         $result = db_query($sql, "could not void bank transactions for type=$type and trans_no=$type_no");
95
96         void_gl_trans($type, $type_no, true);
97
98         // in case it's a customer trans - probably better to check first
99         void_cust_allocations($type, $type_no);
100         void_customer_trans($type, $type_no);
101
102         // in case it's a supplier trans - probably better to check first
103         void_supp_allocations($type, $type_no);
104         void_supp_trans($type, $type_no);
105
106         if (!$nested)
107                 commit_transaction();
108 }
109
110 //----------------------------------------------------------------------------------
111
112 ?>