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 ***********************************************************************/
13 function add_customer($CustName, $cust_ref, $address, $tax_id, $curr_code,
14 $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount, $pymt_discount,
15 $credit_limit, $sales_type, $notes)
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 pymt_discount,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 . ",
25 " . $pymt_discount . ", " . $credit_limit
26 .", ".db_escape($sales_type).", ".db_escape($notes) . ")";
27 db_query($sql,"The customer could not be added");
30 function update_customer($customer_id, $CustName, $cust_ref, $address, $tax_id, $curr_code,
31 $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount, $pymt_discount,
32 $credit_limit, $sales_type, $notes)
34 $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($CustName) . ",
35 debtor_ref=" . db_escape($cust_ref) . ",
36 address=".db_escape($address) . ",
37 tax_id=".db_escape($tax_id) . ",
38 curr_code=".db_escape($curr_code) . ",
39 dimension_id=".db_escape($dimension_id) . ",
40 dimension2_id=".db_escape($dimension2_id) . ",
41 credit_status=".db_escape($credit_status) . ",
42 payment_terms=".db_escape($payment_terms) . ",
43 discount=" . $discount . ",
44 pymt_discount=" . $pymt_discount . ",
45 credit_limit=" . $credit_limit . ",
46 sales_type = ".db_escape($sales_type) . ",
47 notes=".db_escape($notes) ."
48 WHERE debtor_no = ".db_escape($customer_id);
50 db_query($sql,"The customer could not be updated");
53 function delete_customer($customer_id)
56 delete_entity_contacts('customer', $customer_id);
58 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
59 db_query($sql,"cannot delete customer");
64 This function probably should be renamed to get_customer_summary
66 function get_customer_details($customer_id, $to=null, $all=true)
70 $todate = date("Y-m-d");
72 $todate = date2sql($to);
73 $past1 = get_company_pref('past_due_days');
75 // removed - debtor_trans.alloc from all summations
76 $sign = "IF(`type` IN(".implode(',', array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, 1)";
78 $value = "IFNULL($sign*(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount),0)";
80 $value = "IFNULL($sign*(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount -
82 $due = "IF (trans.type=".ST_SALESINVOICE.", trans.due_date, trans.tran_date)";
83 $sql = "SELECT debtor.name, debtor.curr_code, terms.terms, debtor.credit_limit,
84 credit_status.dissallow_invoices, credit_status.reason_description,
85 Sum($value) AS Balance,
86 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > 0,$value,0)) AS Due,
87 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $past1,$value,0)) AS Overdue1,
88 Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) > $past2,$value,0)) AS Overdue2
89 FROM ".TB_PREF."debtors_master debtor
90 LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.tran_date <= '$todate' AND debtor.debtor_no = trans.debtor_no AND trans.type <> ".ST_CUSTDELIVERY.","
91 .TB_PREF."payment_terms terms,"
92 .TB_PREF."credit_status credit_status
94 debtor.payment_terms = terms.terms_indicator
95 AND debtor.credit_status = credit_status.id";
97 $sql .= " AND debtor.debtor_no = ".db_escape($customer_id);
100 $sql .= " AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - trans.alloc) > ".FLOAT_COMP_DELTA;
104 terms.days_before_due,
105 terms.day_in_following_month,
107 credit_status.dissallow_invoices,
108 credit_status.reason_description";
109 $result = db_query($sql,"The customer details could not be retrieved");
111 $customer_record = db_fetch($result);
113 return $customer_record;
118 function get_customer($customer_id)
120 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
122 $result = db_query($sql, "could not get customer");
124 return db_fetch($result);
127 function get_customer_name($customer_id)
129 $sql = "SELECT name FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
131 $result = db_query($sql, "could not get customer");
133 $row = db_fetch_row($result);
138 function get_customer_habit($customer_id)
140 $sql = "SELECT debtor.pymt_discount, credit_status.dissallow_invoices
141 FROM ".TB_PREF."debtors_master debtor,"
142 .TB_PREF."credit_status credit_status
143 WHERE debtor.credit_status = credit_status.id
144 AND debtor.debtor_no = ".db_escape($customer_id);
146 $result = db_query($sql, "could not query customers");
148 return db_fetch($result);
151 function get_customer_contacts($customer_id, $action=null)
154 $res = get_crm_persons('customer', $action, $customer_id);
155 while($contact = db_fetch($res))
157 if ($contact['lang'] == 'C') // Fix for improper lang in demo sql files.
158 $contact['lang'] = '';
159 $results[] = $contact;
164 function get_current_cust_credit($customer_id)
166 $custdet = get_customer_details($customer_id);
168 return $custdet['credit_limit']-$custdet['Balance'];
172 function is_new_customer($id)
174 $tables = array('cust_branch', 'debtor_trans', 'recurrent_invoices', 'sales_orders');
176 return !key_in_foreign_table($id, $tables, 'debtor_no');
179 function get_customer_by_ref($reference)
181 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_ref=".db_escape($reference);
183 $result = db_query($sql, "could not get customer");
185 return db_fetch($result);
188 //----------------------------------------------------------------------------------
190 function get_customer_currency($customer_id=null, $branch_id=null)
192 $sql = "SELECT curr_code
193 FROM ".TB_PREF."debtors_master cust
194 LEFT JOIN ".TB_PREF."cust_branch branch ON branch.debtor_no=cust.debtor_no
195 WHERE " .(isset($branch_id) ? "branch_code = ".db_escape($branch_id) : "cust.debtor_no = ".db_escape($customer_id));
197 $result = db_query($sql, "Retreive currency of customer $customer_id");
199 $myrow=db_fetch_row($result);
203 function get_customers_search($customer)
207 if (isset($SysPrefs->max_rows_in_search))
208 $limit = $SysPrefs->max_rows_in_search;
212 $sql = "SELECT debtor_no, name, debtor_ref, address, tax_id FROM ".TB_PREF."debtors_master
213 WHERE ( name LIKE " . db_escape("%" . $customer. "%") . " OR
214 debtor_ref LIKE " . db_escape("%" . $customer. "%") . " OR
215 address LIKE " . db_escape("%" . $customer. "%") . " OR
216 tax_id LIKE " . db_escape("%" . $customer. "%").")
217 ORDER BY name LIMIT 0,".(int)($limit);
219 return db_query($sql, "Failed in retreiving customer list.");