e7a57deb9008ae3049096a902e6071056f5ee37c
[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,
14         $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount,
15         $credit_limit, $sales_type, $notes)
16 {
17         $sql = "INSERT INTO ".TB_PREF."debtors_master (name, debtor_ref, address, tax_id,
18                 dimension_id, dimension2_id, curr_code, credit_status, payment_terms, discount, 
19                 credit_limit, sales_type, notes) VALUES ("
20                 .db_escape($CustName) .", " .db_escape($cust_ref) .", "
21                 .db_escape($address) . ", " . db_escape($tax_id) . ","
22                 .db_escape($dimension_id) . ", " 
23                 .db_escape($dimension2_id) . ", ".db_escape($curr_code) . ", 
24                 " . db_escape($credit_status) .", " . db_escape($payment_terms) .", " . $discount . "," . $credit_limit 
25                  .", ".db_escape($sales_type).", ".db_escape($notes) . ")";
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,
30         $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount,
31         $credit_limit, $sales_type, $notes)
32 {
33         $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($CustName) . ", 
34                 debtor_ref=" . db_escape($cust_ref) . ",
35                 address=".db_escape($address) . ", 
36                 tax_id=".db_escape($tax_id) . ", 
37                 curr_code=".db_escape($curr_code) . ", 
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                 credit_limit=" . $credit_limit . ", 
44                 sales_type = ".db_escape($sales_type) . ", 
45                 notes=".db_escape($notes) ."
46                 WHERE debtor_no = ".db_escape($customer_id);
47
48         db_query($sql,"The customer could not be updated");
49 }
50
51 function delete_customer($customer_id)
52 {
53         begin_transaction(__FUNCTION__, func_get_args());
54         delete_entity_contacts('customer', $customer_id);
55
56         $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
57         db_query($sql,"cannot delete customer");
58         commit_transaction();
59 }
60
61 /*
62         This function probably should be renamed to get_customer_summary
63 */
64 function get_customer_details($customer_id, $to=null, $all=true)
65 {
66
67         if ($to == null)
68                 $todate = date("Y-m-d");
69         else
70                 $todate = date2sql($to);
71         $past1 = get_company_pref('past_due_days');
72         $past2 = 2 * $past1;
73         // removed - debtor_trans.alloc from all summations
74         $sign = "IF(trans.`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, 1)";
75         if ($all)
76         $value = "IFNULL($sign*(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount),0)";
77     else                
78         $value = "IFNULL($sign*(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - 
79                 trans.alloc),0)";
80         $due = "IF (trans.type=".ST_SALESINVOICE.", trans.due_date, trans.tran_date)";
81     $sql = "SELECT debtor.name, debtor.curr_code, terms.terms, debtor.credit_limit,
82                         credit_status.dissallow_invoices, credit_status.reason_description,
83                                 Sum($value) AS Balance,
84                                 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > 0,$value,0)) AS Due,
85                                 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $past1,$value,0)) AS Overdue1,
86                                 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $past2,$value,0)) AS Overdue2
87                         FROM ".TB_PREF."debtors_master debtor
88                                  LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.tran_date <= '$todate' AND debtor.debtor_no = trans.debtor_no AND trans.type <> ".ST_CUSTDELIVERY."
89                                  LEFT JOIN ".TB_PREF."payment_terms terms ON debtor.payment_terms = terms.id,"
90                                  .TB_PREF."credit_status credit_status
91                         WHERE
92                                 debtor.credit_status = credit_status.id";
93          if ($customer_id)
94                 $sql .= " AND debtor.debtor_no = ".db_escape($customer_id);
95
96         if (!$all)
97                 $sql .= " AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - trans.alloc) > ".FLOAT_COMP_DELTA;
98         $sql .= " GROUP BY
99                                 debtor.name,
100                                 debtor.credit_limit,
101                                 credit_status.dissallow_invoices,
102                                 credit_status.reason_description";
103     $result = db_query($sql,"The customer details could not be retrieved");
104
105     $customer_record = db_fetch($result);
106
107     return $customer_record;
108
109 }
110
111
112 function get_customer($customer_id)
113 {
114         $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
115
116         $result = db_query($sql, "could not get customer");
117
118         return db_fetch($result);
119 }
120
121 function get_customer_name($customer_id)
122 {
123         $sql = "SELECT name FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
124
125         $result = db_query($sql, "could not get customer");
126
127         $row = db_fetch_row($result);
128
129         return $row[0];
130 }
131
132 function get_customer_habit($customer_id)
133 {
134         $sql = "SELECT credit_status.dissallow_invoices
135                         FROM ".TB_PREF."debtors_master debtor,"
136                                 .TB_PREF."credit_status credit_status
137                         WHERE debtor.credit_status = credit_status.id
138                         AND debtor.debtor_no = ".db_escape($customer_id);
139
140         $result = db_query($sql, "could not query customers");
141
142         return db_fetch($result);
143 }
144
145 function get_customer_contacts($customer_id, $action=null)
146 {
147         $results = array();
148         $res = get_crm_persons('customer', $action, $customer_id);
149         while($contact = db_fetch($res))
150         {
151                 if ($contact['lang'] == 'C') // Fix for improper lang in demo sql files.
152                         $contact['lang'] = '';
153                 $results[] = $contact;
154         }       
155         return $results;
156 }
157
158 function get_current_cust_credit($customer_id)
159 {
160         $custdet = get_customer_details($customer_id);
161
162         return $custdet['credit_limit']-$custdet['Balance'];
163
164 }
165
166 function is_new_customer($id)
167 {
168         $tables = array('cust_branch', 'debtor_trans', 'recurrent_invoices', 'sales_orders');
169
170         return !key_in_foreign_table($id, $tables, 'debtor_no');
171 }
172
173 function get_customer_by_ref($reference)
174 {
175         $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_ref=".db_escape($reference);
176
177         $result = db_query($sql, "could not get customer");
178
179         return db_fetch($result);
180 }
181
182 //----------------------------------------------------------------------------------
183
184 function get_customer_currency($customer_id=null, $branch_id=null)
185 {
186     $sql = "SELECT curr_code
187         FROM ".TB_PREF."debtors_master cust
188                 LEFT JOIN ".TB_PREF."cust_branch branch ON branch.debtor_no=cust.debtor_no
189         WHERE " .(isset($branch_id) ? "branch_code = ".db_escape($branch_id) : "cust.debtor_no = ".db_escape($customer_id));
190
191         $result = db_query($sql, "Retreive currency of customer $customer_id");
192
193         $myrow=db_fetch_row($result);
194         return $myrow[0];
195 }
196
197 function get_customers_search($customer)
198 {
199         global $SysPrefs;
200
201         if (isset($SysPrefs->max_rows_in_search))
202                 $limit = $SysPrefs->max_rows_in_search;
203         else
204                 $limit = 10;
205
206         $sql = "SELECT debtor_no, name, debtor_ref, address, tax_id FROM ".TB_PREF."debtors_master 
207           WHERE (  name LIKE " . db_escape("%" . $customer. "%") . " OR 
208                  debtor_ref LIKE " . db_escape("%" . $customer. "%") . " OR 
209                 address LIKE " . db_escape("%" . $customer. "%") . " OR 
210              tax_id LIKE " . db_escape("%" . $customer. "%").")
211           ORDER BY name LIMIT 0,".(int)($limit);
212
213         return db_query($sql, "Failed in retreiving customer list.");
214 }