Customer balances reports: additional bug fixes to balance totals calculations.
[fa-stable.git] / sales / includes / db / customers_db.inc
index 1670c426e7eceaf2e3f9b7e3bc6eb1f4041a094c..96e17ceff76ccad0456772b97fd5e58c61b967e5 100644 (file)
@@ -9,7 +9,61 @@
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
-function get_customer_details($customer_id, $to=null)
+
+function add_customer($CustName, $cust_ref, $address, $tax_id, $curr_code,
+       $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount, $pymt_discount, 
+       $credit_limit, $sales_type, $notes)
+{
+       $sql = "INSERT INTO ".TB_PREF."debtors_master (name, debtor_ref, address, tax_id,
+               dimension_id, dimension2_id, curr_code, credit_status, payment_terms, discount, 
+               pymt_discount,credit_limit, sales_type, notes) VALUES ("
+               .db_escape($CustName) .", " .db_escape($cust_ref) .", "
+               .db_escape($address) . ", " . db_escape($tax_id) . ","
+               .db_escape($dimension_id) . ", " 
+               .db_escape($dimension2_id) . ", ".db_escape($curr_code) . ", 
+               " . db_escape($credit_status) . ", ".db_escape($payment_terms) . ", " . $discount . ", 
+               " . $pymt_discount . ", " . $credit_limit 
+                .", ".db_escape($sales_type).", ".db_escape($notes) . ")";
+       db_query($sql,"The customer could not be added");
+}
+
+function update_customer($customer_id, $CustName, $cust_ref, $address, $tax_id, $curr_code,
+       $dimension_id, $dimension2_id, $credit_status, $payment_terms, $discount, $pymt_discount,
+       $credit_limit, $sales_type, $notes)
+{
+       $sql = "UPDATE ".TB_PREF."debtors_master SET name=" . db_escape($CustName) . ", 
+               debtor_ref=" . db_escape($cust_ref) . ",
+               address=".db_escape($address) . ", 
+               tax_id=".db_escape($tax_id) . ", 
+               curr_code=".db_escape($curr_code) . ", 
+               dimension_id=".db_escape($dimension_id) . ", 
+               dimension2_id=".db_escape($dimension2_id) . ", 
+               credit_status=".db_escape($credit_status) . ", 
+               payment_terms=".db_escape($payment_terms) . ", 
+               discount=" . $discount . ", 
+               pymt_discount=" . $pymt_discount . ", 
+               credit_limit=" . $credit_limit . ", 
+               sales_type = ".db_escape($sales_type) . ", 
+               notes=".db_escape($notes) ."
+               WHERE debtor_no = ".db_escape($customer_id);
+
+       db_query($sql,"The customer could not be updated");
+}
+
+function delete_customer($customer_id)
+{
+       begin_transaction();
+       delete_entity_contacts('customer', $customer_id);
+
+       $sql = "DELETE FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
+       db_query($sql,"cannot delete customer");
+       commit_transaction();
+}
+
+/*
+       This function probably should be renamed to get_customer_summary
+*/
+function get_customer_details($customer_id, $to=null, $all=true)
 {
 
        if ($to == null)
@@ -18,86 +72,51 @@ function get_customer_details($customer_id, $to=null)
                $todate = date2sql($to);
        $past1 = get_company_pref('past_due_days');
        $past2 = 2 * $past1;
-       // removed - debtor_trans.alloc from all summations
 
-    $value = "IF(".TB_PREF."debtor_trans.type=11 OR ".TB_PREF."debtor_trans.type=12 OR ".TB_PREF."debtor_trans.type=2,
-       -1, 1) *".
-      "(".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
-               .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + "
-               .TB_PREF."debtor_trans.ov_discount)";
-       $due = "IF (".TB_PREF."debtor_trans.type=10,".TB_PREF."debtor_trans.due_date,".TB_PREF."debtor_trans.tran_date)";
-    $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."payment_terms.terms,
-               ".TB_PREF."debtors_master.credit_limit, ".TB_PREF."credit_status.dissallow_invoices, ".TB_PREF."credit_status.reason_description,
+       $sign = "IF(`type` IN(".implode(',',  array(ST_CUSTCREDIT,ST_CUSTPAYMENT,ST_BANKDEPOSIT))."), -1, IF(type=".ST_JOURNAL.",IF(ov_amount>=0,-1,1), 1))";
+       $value = "$sign*(IF(trans.prep_amount, trans.prep_amount, 
+               ABS(trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount)) ".($all ? '' : "- trans.alloc").")";
+
 
-               Sum(".$value.") AS Balance,
+       $due = "IF (trans.type=10, trans.due_date, trans.tran_date)";
+    $sql = "SELECT debtor.name, debtor.curr_code, terms.terms,
+               debtor.credit_limit, credit_status.dissallow_invoices, credit_status.reason_description,
 
+               Sum(IFNULL($value,0)) AS Balance,
                Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= 0,$value,0)) AS Due,
                Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past1,$value,0)) AS Overdue1,
                Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past2,$value,0)) AS Overdue2
 
-               FROM ".TB_PREF."debtors_master,
-                        ".TB_PREF."payment_terms,
-                        ".TB_PREF."credit_status,
-                        ".TB_PREF."debtor_trans
+               FROM ".TB_PREF."debtors_master debtor
+                        LEFT JOIN ".TB_PREF."debtor_trans trans ON 
+                        trans.tran_date <= '$todate' AND debtor.debtor_no = trans.debtor_no AND trans.type <> 13 AND trans.type <> 14,
+                        ".TB_PREF."payment_terms terms,
+                        ".TB_PREF."credit_status credit_status
 
                WHERE
-                        ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator
-                        AND ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id
-                        AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id)."
-                        AND ".TB_PREF."debtor_trans.tran_date <= '$todate'
-                        AND ".TB_PREF."debtor_trans.type <> 13
-                        AND ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
-
-               GROUP BY
-                         ".TB_PREF."debtors_master.name,
-                         ".TB_PREF."payment_terms.terms,
-                         ".TB_PREF."payment_terms.days_before_due,
-                         ".TB_PREF."payment_terms.day_in_following_month,
-                         ".TB_PREF."debtors_master.credit_limit,
-                         ".TB_PREF."credit_status.dissallow_invoices,
-                         ".TB_PREF."credit_status.reason_description";
-    $result = db_query($sql,"The customer details could not be retrieved");
-
-    if (db_num_rows($result) == 0)
-    {
-
-       /*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 */
-
-       $nil_balance = true;
-
-       $sql = "SELECT ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."debtors_master.debtor_no,  ".TB_PREF."payment_terms.terms,
-               ".TB_PREF."debtors_master.credit_limit, ".TB_PREF."credit_status.dissallow_invoices, ".TB_PREF."credit_status.reason_description
-               FROM ".TB_PREF."debtors_master,
-                    ".TB_PREF."payment_terms,
-                    ".TB_PREF."credit_status
-
-               WHERE
-                    ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator
-                    AND ".TB_PREF."debtors_master.credit_status = ".TB_PREF."credit_status.id
-                    AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id);
+                        debtor.payment_terms = terms.terms_indicator
+                        AND debtor.credit_status = credit_status.id
+                        AND debtor.debtor_no = ".db_escape($customer_id)." ";
+       if (!$all)
+               $sql .= "AND ABS($value) >= 0.004 ";
+       $sql .= "GROUP BY
+                         debtor.name,
+                         terms.terms,
+                         terms.days_before_due,
+                         terms.day_in_following_month,
+                         debtor.credit_limit,
+                         credit_status.dissallow_invoices,
+                         credit_status.reason_description";
 
-       $result = db_query($sql,"The customer details could not be retrieved");
-
-    }
-    else
-    {
-       $nil_balance = false;
-    }
+    $result = db_query($sql,"The customer details could not be retrieved");
 
     $customer_record = db_fetch($result);
 
-    if ($nil_balance == true)
-    {
-       $customer_record["Balance"] = 0;
-       $customer_record["Due"] = 0;
-       $customer_record["Overdue1"] = 0;
-       $customer_record["Overdue2"] = 0;
-    }
-
     return $customer_record;
 
 }
 
+
 function get_customer($customer_id)
 {
        $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_no=".db_escape($customer_id);
@@ -115,30 +134,91 @@ function get_customer_name($customer_id)
 
        $row = db_fetch_row($result);
 
-       return $row[0];
+       return is_array($row) ? $row[0] : false;
 }
 
-function get_area_name($id)
+function get_customer_habit($customer_id)
 {
-       $sql = "SELECT description FROM ".TB_PREF."areas WHERE area_code=".db_escape($id);
+       $sql = "SELECT debtor.pymt_discount, credit_status.dissallow_invoices
+                       FROM ".TB_PREF."debtors_master debtor,"
+                               .TB_PREF."credit_status credit_status
+                       WHERE debtor.credit_status = credit_status.id
+                       AND debtor.debtor_no = ".db_escape($customer_id);
 
-       $result = db_query($sql, "could not get sales type");
+       $result = db_query($sql, "could not query customers");
 
-       $row = db_fetch_row($result);
-       return $row[0];
+       return db_fetch($result);
 }
 
-function get_salesman_name($id)
+function get_customer_contacts($customer_id, $action=null)
 {
-       $sql = "SELECT salesman_name FROM ".TB_PREF."salesman WHERE salesman_code=".db_escape($id);
+       $results = array();
+       $res = get_crm_persons('customer', $action, $customer_id);
+       while($contact = db_fetch($res))
+       {
+               if ($contact['lang'] == 'C') // Fix for improper lang in demo sql files.
+                       $contact['lang'] = '';
+               $results[] = $contact;
+       }       
+       return $results;
+}
 
-       $result = db_query($sql, "could not get sales type");
+function get_current_cust_credit($customer_id)
+{
+       $custdet = get_customer_details($customer_id);
 
-       $row = db_fetch_row($result);
-       return $row[0];
+       if (!is_array($custdet))
+               return 0;
+       return $custdet['credit_limit']-$custdet['Balance'];
+
+}
+
+function is_new_customer($id)
+{
+       $tables = array('cust_branch', 'debtor_trans', 'recurrent_invoices', 'sales_orders');
+
+       return !key_in_foreign_table($id, $tables, 'debtor_no');
+}
+
+function get_customer_by_ref($reference)
+{
+       $sql = "SELECT * FROM ".TB_PREF."debtors_master WHERE debtor_ref=".db_escape($reference);
+
+       $result = db_query($sql, "could not get customer");
+
+       return db_fetch($result);
+}
+
+//----------------------------------------------------------------------------------
+
+function get_customer_currency($customer_id=null, $branch_id=null)
+{
+    $sql = "SELECT curr_code
+       FROM ".TB_PREF."debtors_master cust
+               LEFT JOIN ".TB_PREF."cust_branch branch ON branch.debtor_no=cust.debtor_no
+       WHERE " .(isset($branch_id) ? "branch_code = ".db_escape($branch_id) : "cust.debtor_no = ".db_escape($customer_id));
+
+       $result = db_query($sql, "Retreive currency of customer $customer_id");
+
+       $myrow=db_fetch_row($result);
+       return $myrow ? $myrow[0] : get_company_currency();
 }
 
+function get_customers_search($customer)
+{
+       global $SysPrefs;
 
+       if (isset($SysPrefs->max_rows_in_search))
+               $limit = $SysPrefs->max_rows_in_search;
+       else
+               $limit = 10;
 
+       $sql = "SELECT debtor_no, name, debtor_ref, address, tax_id FROM ".TB_PREF."debtors_master 
+         WHERE (  name LIKE " . db_escape("%" . $customer. "%") . " OR 
+                debtor_ref LIKE " . db_escape("%" . $customer. "%") . " OR 
+               address LIKE " . db_escape("%" . $customer. "%") . " OR 
+            tax_id LIKE " . db_escape("%" . $customer. "%").")
+         ORDER BY name LIMIT 0,".(int)($limit);
 
-?>
\ No newline at end of file
+       return db_query($sql, "Failed in retreiving customer list.");
+}