Allowed multiply bank accounts on same gl account, removed bank trans type.
[fa-stable.git] / purchasing / includes / db / supp_trans_db.inc
1 <?php
2
3 //-------------------------------------------------------------------------------------------------------------
4
5 function add_supp_trans($type, $supplier_id, $date_, $due_date, $reference, $supp_reference,
6         $amount, $amount_tax, $discount, $err_msg="")
7 {
8         $date = date2sql($date_);
9         if ($due_date == "")
10                 $due_date = "0000-00-00";
11         else
12                 $due_date = date2sql($due_date);
13
14         $trans_no = get_next_trans_no($type);
15
16         $curr = get_supplier_currency($supplier_id);
17         $rate = get_exchange_rate_from_home_currency($curr, $date_);
18
19
20         $sql = "INSERT INTO ".TB_PREF."supp_trans (trans_no, type, supplier_id, tran_date, due_date,
21                 reference, supp_reference, ov_amount, ov_gst, rate, ov_discount) ";
22         $sql .= "VALUES ($trans_no, $type, $supplier_id, '$date', '$due_date',
23                 ".db_escape($reference).", ".db_escape($supp_reference).", $amount, $amount_tax, $rate, $discount)";
24
25         if ($err_msg == "")
26                 $err_msg = "Cannot insert a supplier transaction record";
27
28         db_query($sql, $err_msg);
29
30         return $trans_no;
31 }
32
33 //-------------------------------------------------------------------------------------------------------------
34
35 function get_supp_trans($trans_no, $trans_type=-1)
36 {
37         $sql = "SELECT ".TB_PREF."supp_trans.*, (".TB_PREF."supp_trans.ov_amount+".TB_PREF."supp_trans.ov_gst+".TB_PREF."supp_trans.ov_discount) AS Total,
38                 ".TB_PREF."suppliers.supp_name AS supplier_name, ".TB_PREF."suppliers.curr_code AS SupplierCurrCode ";
39
40         if ($trans_type == 22)
41         {
42                 // it's a payment so also get the bank account
43                 $sql .= ", ".TB_PREF."bank_accounts.bank_name, ".TB_PREF."bank_accounts.bank_account_name, ".TB_PREF."bank_accounts.bank_curr_code,
44                         ".TB_PREF."bank_accounts.account_type AS BankTransType, ".TB_PREF."bank_trans.amount AS BankAmount,
45                         ".TB_PREF."bank_trans.ref ";
46         }
47
48         $sql .= " FROM ".TB_PREF."supp_trans, ".TB_PREF."suppliers ";
49
50         if ($trans_type == 22)
51         {
52                 // it's a payment so also get the bank account
53                 $sql .= ", ".TB_PREF."bank_trans, ".TB_PREF."bank_accounts";
54         }
55
56         $sql .= " WHERE ".TB_PREF."supp_trans.trans_no=$trans_no
57                 AND ".TB_PREF."supp_trans.supplier_id=".TB_PREF."suppliers.supplier_id";
58
59         if ($trans_type > 0)
60                 $sql .= " AND ".TB_PREF."supp_trans.type=$trans_type ";
61
62         if ($trans_type == 22)
63         {
64                 // it's a payment so also get the bank account
65                 $sql .= " AND ".TB_PREF."bank_trans.trans_no =$trans_no
66                         AND ".TB_PREF."bank_trans.type=$trans_type
67                         AND ".TB_PREF."bank_accounts.id=".TB_PREF."bank_trans.bank_act ";
68         }
69
70         $result = db_query($sql, "Cannot retreive a supplier transaction");
71
72     if (db_num_rows($result) == 0)
73     {
74        // can't return nothing
75        display_db_error("no supplier trans found for given params", $sql, true);
76        exit;
77     }
78
79     if (db_num_rows($result) > 1)
80     {
81        // can't return multiple
82        display_db_error("duplicate supplier transactions found for given params", $sql, true);
83        exit;
84     }
85
86     return db_fetch($result);
87 }
88
89 //----------------------------------------------------------------------------------------
90
91 function get_supplier_transactions($extra_fields=null, $extra_conditions=null, $extra_tables=null)
92 {
93         $sql = "SELECT ".TB_PREF."supp_trans.*,
94                 ov_amount+ov_gst+ov_discount AS Total,
95                 ".TB_PREF."suppliers.supp_name, ".TB_PREF."suppliers.address,
96                 ".TB_PREF."suppliers.curr_code ";
97
98         if ($extra_fields)
99                 $sql .= ", $extra_fields ";
100
101         $sql .= " FROM ".TB_PREF."supp_trans, ".TB_PREF."suppliers ";
102         if ($extra_tables)
103                 $sql .= " ,$extra_tables ";
104
105         $sql .= " WHERE ".TB_PREF."supp_trans.supplier_id=".TB_PREF."suppliers.supplier_id";
106
107         if ($extra_conditions)
108                 $sql .= " AND $extra_conditions ";
109
110         $sql .= " ORDER BY trans_no";
111
112         return db_query($sql, "Cannot retreive supplier transactions");
113 }
114
115 //----------------------------------------------------------------------------------------
116
117 function exists_supp_trans($type, $type_no)
118 {
119         if ($type == 25)
120                 return exists_grn($type_no);
121
122         $sql = "SELECT trans_no FROM ".TB_PREF."supp_trans WHERE type=$type
123                 AND trans_no=$type_no";
124         $result = db_query($sql, "Cannot retreive a supplier transaction");
125
126     return (db_num_rows($result) > 0);
127 }
128
129 //----------------------------------------------------------------------------------------
130
131 function void_supp_trans($type, $type_no)
132 {
133         $sql = "UPDATE ".TB_PREF."supp_trans SET ov_amount=0, ov_discount=0, ov_gst=0,
134                 alloc=0 WHERE type=$type AND trans_no=$type_no";
135
136         db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
137 }
138
139 //----------------------------------------------------------------------------------------
140
141 function post_void_supp_trans($type, $type_no)
142 {
143         if ($type == 22)
144         {
145                 void_supp_payment($type, $type_no);
146                 return true;
147         }
148
149         if ($type == 20 || $type == 21)
150         {
151                 void_supp_invoice($type, $type_no);
152                 return true;
153         }
154
155         if ($type == 25)
156         {
157                 return void_grn($type_no);
158         }
159
160         return false;
161 }
162
163 //----------------------------------------------------------------------------------------
164
165 ?>