Merged all main trunk bugfixes up to release 2.0.5
[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="", $rate=0)
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         
18         if ($rate == 0)
19                 $rate = get_exchange_rate_from_home_currency($curr, $date_);
20
21
22         $sql = "INSERT INTO ".TB_PREF."supp_trans (trans_no, type, supplier_id, tran_date, due_date,
23                 reference, supp_reference, ov_amount, ov_gst, rate, ov_discount) ";
24         $sql .= "VALUES ($trans_no, $type, $supplier_id, '$date', '$due_date',
25                 ".db_escape($reference).", ".db_escape($supp_reference).", $amount, $amount_tax, $rate, $discount)";
26
27         if ($err_msg == "")
28                 $err_msg = "Cannot insert a supplier transaction record";
29
30         db_query($sql, $err_msg);
31
32         return $trans_no;
33 }
34
35 //-------------------------------------------------------------------------------------------------------------
36
37 function get_supp_trans($trans_no, $trans_type=-1)
38 {
39         $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,
40                 ".TB_PREF."suppliers.supp_name AS supplier_name, ".TB_PREF."suppliers.curr_code AS SupplierCurrCode ";
41
42         if ($trans_type == 22)
43         {
44                 // it's a payment so also get the bank account
45                 $sql .= ", ".TB_PREF."bank_accounts.bank_name, ".TB_PREF."bank_accounts.bank_account_name, ".TB_PREF."bank_accounts.bank_curr_code,
46                         ".TB_PREF."bank_accounts.account_type AS BankTransType, ".TB_PREF."bank_trans.amount AS BankAmount,
47                         ".TB_PREF."bank_trans.ref ";
48         }
49
50         $sql .= " FROM ".TB_PREF."supp_trans, ".TB_PREF."suppliers ";
51
52         if ($trans_type == 22)
53         {
54                 // it's a payment so also get the bank account
55                 $sql .= ", ".TB_PREF."bank_trans, ".TB_PREF."bank_accounts";
56         }
57
58         $sql .= " WHERE ".TB_PREF."supp_trans.trans_no=$trans_no
59                 AND ".TB_PREF."supp_trans.supplier_id=".TB_PREF."suppliers.supplier_id";
60
61         if ($trans_type > 0)
62                 $sql .= " AND ".TB_PREF."supp_trans.type=$trans_type ";
63
64         if ($trans_type == 22)
65         {
66                 // it's a payment so also get the bank account
67                 $sql .= " AND ".TB_PREF."bank_trans.trans_no =$trans_no
68                         AND ".TB_PREF."bank_trans.type=$trans_type
69                         AND ".TB_PREF."bank_accounts.id=".TB_PREF."bank_trans.bank_act ";
70         }
71
72         $result = db_query($sql, "Cannot retreive a supplier transaction");
73
74     if (db_num_rows($result) == 0)
75     {
76        // can't return nothing
77        display_db_error("no supplier trans found for given params", $sql, true);
78        exit;
79     }
80
81     if (db_num_rows($result) > 1)
82     {
83        // can't return multiple
84        display_db_error("duplicate supplier transactions found for given params", $sql, true);
85        exit;
86     }
87
88     return db_fetch($result);
89 }
90
91 //----------------------------------------------------------------------------------------
92
93 function exists_supp_trans($type, $type_no)
94 {
95         if ($type == 25)
96                 return exists_grn($type_no);
97
98         $sql = "SELECT trans_no FROM ".TB_PREF."supp_trans WHERE type=$type
99                 AND trans_no=$type_no";
100         $result = db_query($sql, "Cannot retreive a supplier transaction");
101
102     return (db_num_rows($result) > 0);
103 }
104
105 //----------------------------------------------------------------------------------------
106
107 function void_supp_trans($type, $type_no)
108 {
109         $sql = "UPDATE ".TB_PREF."supp_trans SET ov_amount=0, ov_discount=0, ov_gst=0,
110                 alloc=0 WHERE type=$type AND trans_no=$type_no";
111
112         db_query($sql, "could not void supp transactions for type=$type and trans_no=$type_no");
113 }
114
115 //----------------------------------------------------------------------------------------
116
117 function post_void_supp_trans($type, $type_no)
118 {
119         if ($type == 22)
120         {
121                 void_supp_payment($type, $type_no);
122                 return true;
123         }
124
125         if ($type == 20 || $type == 21)
126         {
127                 void_supp_invoice($type, $type_no);
128                 return true;
129         }
130
131         if ($type == 25)
132         {
133                 return void_grn($type_no);
134         }
135
136         return false;
137 }
138
139 //----------------------------------------------------------------------------------------
140
141 ?>