Moved all SQL statements from PHP files into relevant *_db.inc files.
[fa-stable.git] / sales / includes / db / customers_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 function add_customer($CustName, $cust_ref, $address, $tax_id, $curr_code, $email, $dimension_id, $dimension2_id,
14         $credit_status, $payment_terms, $discount, $pymt_discount, $credit_limit, $sales_type, $notes)
15 {                       
16         $sql = "INSERT INTO ".TB_PREF."debtors_master (name, debtor_ref, address, tax_id, email, dimension_id, dimension2_id,  
17                 curr_code, credit_status, payment_terms, discount, pymt_discount,credit_limit,  
18                 sales_type, notes) VALUES (".db_escape($CustName) .", " .db_escape($cust_ref) .", "
19                 .db_escape($address) . ", " . db_escape($tax_id) . ","
20                 .db_escape($email) . ", ".db_escape($dimension_id) . ", " 
21                 .db_escape($dimension2_id) . ", ".db_escape($curr_code) . ", 
22                 " . db_escape($credit_status) . ", ".db_escape($payment_terms) . ", " . $discount . ", 
23                 " . $pymt_discount . ", " . $credit_limit 
24                  .", ".db_escape($sales_type).", ".db_escape($notes) . ")";
25
26         db_query($sql,"The customer could not be added");
27 }
28
29 function update_customer($customer_id, $CustName, $cust_ref, $address, $tax_id, $curr_code, $email, $dimension_id, $dimension2_id,
30                         $credit_status, $payment_terms, $discount, $pymt_discount, $credit_limit, $sales_type, $notes)
31 {
32         $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($CustName) . ", 
33                 debtor_ref=" . db_escape($cust_ref) . ",
34                 address=".db_escape($address) . ", 
35                 tax_id=".db_escape($tax_id) . ", 
36                 curr_code=".db_escape($curr_code) . ", 
37                 email=".db_escape($email) . ", 
38                 dimension_id=".db_escape($dimension_id) . ", 
39                 dimension2_id=".db_escape($dimension2_id) . ", 
40                 credit_status=".db_escape($credit_status) . ", 
41                 payment_terms=".db_escape($payment_terms) . ", 
42                 discount=" . $discount . ", 
43                 pymt_discount=" . $pymt_discount . ", 
44                 credit_limit=" . $credit_limit . ", 
45                 sales_type = ".db_escape($sales_type) . ", 
46                 notes=".db_escape($notes) . "
47                 WHERE debtor_no = ".db_escape($customer_id);
48
49         db_query($sql,"The customer could not be updated");
50 }
51
52 function delete_customer($customer_id, $escaped=false)
53 {
54         if (!$escaped)
55                 $customer_id = db_escape($customer_id);
56         $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no=$customer_id";
57         db_query($sql,"cannot delete customer");
58 }
59
60 function get_customer_details($customer_id, $to=null)
61 {
62
63         if ($to == null)
64                 $todate = date("Y-m-d");
65         else
66                 $todate = date2sql($to);
67         $past1 = get_company_pref('past_due_days');
68         $past2 = 2 * $past1;
69         // removed - debtor_trans.alloc from all summations
70
71     $value = "IF(".TB_PREF."debtor_trans.type=11 OR ".TB_PREF."debtor_trans.type=12 OR ".TB_PREF."debtor_trans.type=2,
72         -1, 1) *".
73       "(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
74                 .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + "
75                 .TB_PREF."debtor_trans.ov_discount)";
76         $due = "IF (".TB_PREF."debtor_trans.type=10,".TB_PREF."debtor_trans.due_date,".TB_PREF."debtor_trans.tran_date)";
77     $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."payment_terms.terms,
78                 ".TB_PREF."debtors_master.credit_limit, ".TB_PREF."credit_status.dissallow_invoices, ".TB_PREF."credit_status.reason_description,
79
80                 Sum(".$value.") AS Balance,
81
82                 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0)) AS Due,
83                 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past1,$value,0)) AS Overdue1,
84                 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past2,$value,0)) AS Overdue2
85
86                 FROM ".TB_PREF."debtors_master,
87                          ".TB_PREF."payment_terms,
88                          ".TB_PREF."credit_status,
89                          ".TB_PREF."debtor_trans
90
91                 WHERE
92                          ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator
93                          AND ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id
94                          AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id)."
95                          AND ".TB_PREF."debtor_trans.tran_date <= '$todate'
96                          AND ".TB_PREF."debtor_trans.type <> 13
97                          AND ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
98
99                 GROUP BY
100                           ".TB_PREF."debtors_master.name,
101                           ".TB_PREF."payment_terms.terms,
102                           ".TB_PREF."payment_terms.days_before_due,
103                           ".TB_PREF."payment_terms.day_in_following_month,
104                           ".TB_PREF."debtors_master.credit_limit,
105                           ".TB_PREF."credit_status.dissallow_invoices,
106                           ".TB_PREF."credit_status.reason_description";
107     $result = db_query($sql,"The customer details could not be retrieved");
108
109     if (db_num_rows($result) == 0)
110     {
111
112         /*Because there is no balance - so just retrieve the header information about the customer - the choice is do one query to get the balance and transactions for those customers who have a balance and two queries for those who don't have a balance OR always do two queries - I opted for the former */
113
114         $nil_balance = true;
115
116         $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."debtors_master.debtor_no,  ".TB_PREF."payment_terms.terms,
117                 ".TB_PREF."debtors_master.credit_limit, ".TB_PREF."credit_status.dissallow_invoices, ".TB_PREF."credit_status.reason_description
118                 FROM ".TB_PREF."debtors_master,
119                      ".TB_PREF."payment_terms,
120                      ".TB_PREF."credit_status
121
122                 WHERE
123                      ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator
124                      AND ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id
125                      AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id);
126
127         $result = db_query($sql,"The customer details could not be retrieved");
128
129     }
130     else
131     {
132         $nil_balance = false;
133     }
134
135     $customer_record = db_fetch($result);
136
137     if ($nil_balance == true)
138     {
139         $customer_record["Balance"] = 0;
140         $customer_record["Due"] = 0;
141         $customer_record["Overdue1"] = 0;
142         $customer_record["Overdue2"] = 0;
143     }
144
145     return $customer_record;
146
147 }
148
149 function get_customer($customer_id)
150 {
151         $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
152
153         $result = db_query($sql, "could not get customer");
154
155         return db_fetch($result);
156 }
157
158 function get_customer_name($customer_id)
159 {
160         $sql = "SELECT name FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
161
162         $result = db_query($sql, "could not get customer");
163
164         $row = db_fetch_row($result);
165
166         return $row[0];
167 }
168
169 function get_customer_habit($customer_id)
170 {
171         $sql = "SELECT ".TB_PREF."debtors_master.pymt_discount,
172                 ".TB_PREF."credit_status.dissallow_invoices
173                 FROM ".TB_PREF."debtors_master, ".TB_PREF."credit_status
174                 WHERE ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id
175                         AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id);
176
177         $result = db_query($sql, "could not query customers");
178
179         return db_fetch($result);
180 }
181
182
183
184 ?>