Coede reorganization to reuse sql query by db_pager.
[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 exists_supp_trans($type, $type_no)
92 {
93         if ($type == 25)
94                 return exists_grn($type_no);
95
96         $sql = "SELECT trans_no FROM ".TB_PREF."supp_trans WHERE type=$type
97                 AND trans_no=$type_no";
98         $result = db_query($sql, "Cannot retreive a supplier transaction");
99
100     return (db_num_rows($result) > 0);
101 }
102
103 //----------------------------------------------------------------------------------------
104
105 function void_supp_trans($type, $type_no)
106 {
107         $sql = "UPDATE ".TB_PREF."supp_trans SET ov_amount=0, ov_discount=0, ov_gst=0,
108                 alloc=0 WHERE type=$type AND trans_no=$type_no";
109
110         db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
111 }
112
113 //----------------------------------------------------------------------------------------
114
115 function post_void_supp_trans($type, $type_no)
116 {
117         if ($type == 22)
118         {
119                 void_supp_payment($type, $type_no);
120                 return true;
121         }
122
123         if ($type == 20 || $type == 21)
124         {
125                 void_supp_invoice($type, $type_no);
126                 return true;
127         }
128
129         if ($type == 25)
130         {
131                 return void_grn($type_no);
132         }
133
134         return false;
135 }
136
137 //----------------------------------------------------------------------------------------
138
139 ?>