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 13function 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) 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 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 28 db_query($sql,"The customer could not be added"); 29} 30 31function update_customer($customer_id, $CustName, $cust_ref, $address, $tax_id, $curr_code, 32 $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount, $pymt_discount, 33 $credit_limit, $sales_type, $notes) 34{ 35 $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($CustName) . ", 36 debtor_ref=" . db_escape($cust_ref) . ", 37 address=".db_escape($address) . ", 38 tax_id=".db_escape($tax_id) . ", 39 curr_code=".db_escape($curr_code) . ", 40 dimension_id=".db_escape($dimension_id) . ", 41 dimension2_id=".db_escape($dimension2_id) . ", 42 credit_status=".db_escape($credit_status) . ", 43 payment_terms=".db_escape($payment_terms) . ", 44 discount=" . $discount . ", 45 pymt_discount=" . $pymt_discount . ", 46 credit_limit=" . $credit_limit . ", 47 sales_type = ".db_escape($sales_type) . ", 48 notes=".db_escape($notes) ." 49 WHERE debtor_no = ".db_escape($customer_id); 50 51 db_query($sql,"The customer could not be updated"); 52} 53 54function delete_customer($customer_id) 55{ 56 begin_transaction(); 57 delete_entity_contacts('customer', $customer_id); 58 59 $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);; 60 db_query($sql,"cannot delete customer"); 61 commit_transaction(); 62} 63 64function 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 if ($all) 75 $value = "IFNULL(IF(trans.type=11 OR trans.type=12 OR trans.type=2, -1, 1) 76 * (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount),0)"; 77 else 78 $value = "IFNULL(IF(trans.type=11 OR trans.type=12 OR trans.type=2, -1, 1) 79 * (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - 80 trans.alloc),0)"; 81 $due = "IF (trans.type=10, trans.due_date, trans.tran_date)"; 82 $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."payment_terms.terms, 83 ".TB_PREF."debtors_master.credit_limit, ".TB_PREF."credit_status.dissallow_invoices, ".TB_PREF."credit_status.reason_description, 84 85 Sum(IFNULL($value,0)) 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 90 FROM ".TB_PREF."debtors_master 91 LEFT JOIN ".TB_PREF."debtor_trans trans ON 92 trans.tran_date <= '$todate' AND ".TB_PREF."debtors_master.debtor_no = trans.debtor_no AND trans.type <> 13 93, 94 ".TB_PREF."payment_terms, 95 ".TB_PREF."credit_status 96 97 WHERE 98 ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator 99 AND ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id 100 AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id)." "; 101 if (!$all) 102 $sql .= "AND ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount - trans.alloc) > ".FLOAT_COMP_DELTA." "; 103 $sql .= "GROUP BY 104 ".TB_PREF."debtors_master.name, 105 ".TB_PREF."payment_terms.terms, 106 ".TB_PREF."payment_terms.days_before_due, 107 ".TB_PREF."payment_terms.day_in_following_month, 108 ".TB_PREF."debtors_master.credit_limit, 109 ".TB_PREF."credit_status.dissallow_invoices, 110 ".TB_PREF."credit_status.reason_description"; 111 $result = db_query($sql,"The customer details could not be retrieved"); 112 113 $customer_record = db_fetch($result); 114 115 return $customer_record; 116 117} 118 119 120function get_customer($customer_id) 121{ 122 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id); 123 124 $result = db_query($sql, "could not get customer"); 125 126 return db_fetch($result); 127} 128 129function get_customer_name($customer_id) 130{ 131 $sql = "SELECT name FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id); 132 133 $result = db_query($sql, "could not get customer"); 134 135 $row = db_fetch_row($result); 136 137 return $row[0]; 138} 139 140function get_customer_habit($customer_id) 141{ 142 $sql = "SELECT ".TB_PREF."debtors_master.pymt_discount, 143 ".TB_PREF."credit_status.dissallow_invoices 144 FROM ".TB_PREF."debtors_master, ".TB_PREF."credit_status 145 WHERE ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id 146 AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id); 147 148 $result = db_query($sql, "could not query customers"); 149 150 return db_fetch($result); 151} 152 153function get_customer_contacts($customer_id, $action=null) 154{ 155 $results = array(); 156 $res = get_crm_persons('customer', $action, $customer_id); 157 while($contact = db_fetch($res)) 158 { 159 if ($contact['lang'] == 'C') // Fix for improper lang in demo sql files. 160 $contact['lang'] = ''; 161 $results[] = $contact; 162 } 163 return $results; 164} 165 166function get_current_cust_credit($customer_id) 167{ 168 $custdet = get_customer_details($customer_id); 169 170 return $custdet['credit_limit']-$custdet['Balance']; 171 172} 173 174function is_new_customer($id) 175{ 176 $tables = array('cust_branch', 'debtor_trans', 'recurrent_invoices', 'sales_orders'); 177 178 return !key_in_foreign_table($id, $tables, 'debtor_no'); 179} 180 181function get_customer_by_ref($reference) 182{ 183 $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_ref=".db_escape($reference); 184 185 $result = db_query($sql, "could not get customer"); 186 187 return db_fetch($result); 188} 189 190?>