Current credit display implemented.
authorJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 21 Jun 2010 17:19:07 +0000 (17:19 +0000)
committerJanusz Dobrowolski <janusz@frontaccounting.eu>
Mon, 21 Jun 2010 17:19:07 +0000 (17:19 +0000)
includes/sysnames.inc
includes/ui/ui_input.inc
purchasing/includes/db/po_db.inc
purchasing/includes/db/suppliers_db.inc
purchasing/includes/ui/po_ui.inc
purchasing/po_entry_items.php
sales/customer_delivery.php
sales/includes/cart_class.inc
sales/includes/db/customers_db.inc
sales/includes/db/sales_order_db.inc
sales/includes/ui/sales_order_ui.inc

index a60c6c947a4a58054d43658b724f70055d12d0a8..bf48ad9c5c0e3214713c99b77d67192711635fc9 100644 (file)
@@ -147,5 +147,14 @@ $sys_crm_cats = array (
        'shipper' => _("Shipper"),
        'company' => _("Company internal")
 );
+//----------------------------------------------------------------------------------
+
+$pterm_types = array(
+       PTT_PRE => _("Prepayment"),
+       PTT_CASH => _("Cash"),
+       PTT_DAYS => _("After No. of Days"),
+       PTT_FOLLOWING => _("Day In Following Month")
+);
+
 
 ?>
\ No newline at end of file
index 9717a95ca34c151ac41b1f6211fd0244cbea4c4e..272d515b9965283b1770fc8ce02f65ba077b0b00 100644 (file)
@@ -899,4 +899,29 @@ function inactive_control_column(&$th) {
                $Ajax->activate('_page_body');
        }
 }
+
+function customer_credit_row($customer, $credit, $parms='')
+{
+       global $path_to_root;
+       
+       label_row( _("Current Credit:"),
+               "<a target='_blank' " . ($credit<0 ? 'class="redfg"' : '')
+               ."href='$path_to_root/sales/inquiry/customer_inquiry.php?customer_id=".$customer."'"
+               ." onclick=\"javascript:openWindow(this.href,this.target); return false;\" >"
+               . price_format($credit)
+               ."</a>", $parms);
+}
+
+function supplier_credit_row($supplier, $credit, $parms='')
+{
+       global $path_to_root;
+       
+       label_row( _("Current Credit:"),
+               "<a target='_blank' " . ($credit<0 ? 'class="redfg"' : '')
+               ."href='$path_to_root/purchasing/inquiry/supplier_inquiry.php?supplier_id=".$supplier."'"
+               ." onclick=\"javascript:openWindow(this.href,this.target); return false;\" >"
+               . price_format($credit)
+               ."</a>", $parms);
+}
+
 ?>
\ No newline at end of file
index d401f5e67ca612344195b5bea292938eb908f4d9..41ed94f09872aa42921849ad7126c6ab85f339e3 100644 (file)
@@ -29,14 +29,15 @@ function add_po(&$po_obj)
        begin_transaction();
 
      /*Insert to purchase order header record */
-     $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address) VALUES(";
+     $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address, total) VALUES(";
      $sql .= db_escape($po_obj->supplier_id) . "," .
          db_escape($po_obj->Comments) . ",'" .
          date2sql($po_obj->orig_order_date) . "', " .
                 db_escape($po_obj->reference) . ", " .
          db_escape($po_obj->supp_ref) . ", " .
          db_escape($po_obj->Location) . ", " .
-         db_escape($po_obj->delivery_address) . ")";
+         db_escape($po_obj->delivery_address) . ", " .
+         db_escape($po_obj->get_trans_total()) . ")";
 
        db_query($sql, "The purchase order header record could not be inserted");
 
@@ -77,7 +78,8 @@ function update_po(&$po_obj)
                requisition_no= ". db_escape( $po_obj->supp_ref). ",
                into_stock_location=" . db_escape($po_obj->Location). ",
                ord_date='" . date2sql($po_obj->orig_order_date) . "',
-               delivery_address=" . db_escape($po_obj->delivery_address);
+               delivery_address=" . db_escape($po_obj->delivery_address).",
+               total=". db_escape($po_obj->get_trans_total());
     $sql .= " WHERE order_no = " . $po_obj->order_no;
        db_query($sql, "The purchase order could not be updated");
 
@@ -113,7 +115,7 @@ function update_po(&$po_obj)
 
 function read_po_header($order_no, &$order)
 {
-       $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name,
+       $sql = "SELECT ".TB_PREF."purch_orders.*, ".TB_PREF."suppliers.supp_name, ".TB_PREF."suppliers.tax_group_id,
                ".TB_PREF."suppliers.curr_code, ".TB_PREF."locations.location_name
                FROM ".TB_PREF."purch_orders, ".TB_PREF."suppliers, ".TB_PREF."locations
                WHERE ".TB_PREF."purch_orders.supplier_id = ".TB_PREF."suppliers.supplier_id
@@ -121,7 +123,7 @@ function read_po_header($order_no, &$order)
                AND ".TB_PREF."purch_orders.order_no = ".db_escape($order_no);
 
        $result = db_query($sql, "The order cannot be retrieved");
-
+_vd($sql);
        if (db_num_rows($result) == 1)
        {
 
@@ -131,6 +133,8 @@ function read_po_header($order_no, &$order)
        $order->supplier_id = $myrow["supplier_id"];
        $order->supplier_name = $myrow["supp_name"];
        $order->curr_code = $myrow["curr_code"];
+               $order->tax_group_id = $myrow['tax_group_id'];
+               $order->credit = get_current_supp_credit($order->supplier_id);
 
        $order->orig_order_date = sql2date($myrow["ord_date"]);
        $order->Comments = $myrow["comments"];
index 972d552a04ba3f0fb9644df3d13de32e01032561..b0fae283637a4ccf5cae54c73cd8595b1dd01c4a 100644 (file)
@@ -85,68 +85,37 @@ function get_supplier_details($supplier_id, $to=null)
        $past2 = 2 * $past1;
        // removed - supp_trans.alloc from all summations
 
-    $value = "(".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)";
-       $due = "IF (".TB_PREF."supp_trans.type=".ST_SUPPINVOICE." OR ".TB_PREF."supp_trans.type=".ST_SUPPCREDIT.",".TB_PREF."supp_trans.due_date,".TB_PREF."supp_trans.tran_date)";
-    $sql = "SELECT ".TB_PREF."suppliers.supp_name, ".TB_PREF."suppliers.curr_code, ".TB_PREF."payment_terms.terms,
+    $value = "(trans.ov_amount + trans.ov_gst + trans.ov_discount)";
+       $due = "IF (trans.type=".ST_SUPPINVOICE." OR trans.type=".ST_SUPPCREDIT.",trans.due_date,trans.tran_date)";
+    $sql = "SELECT supp.supp_name, supp.curr_code, ".TB_PREF."payment_terms.terms,
 
-               Sum($value) AS Balance,
+               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
+               Sum(IF ((TO_DAYS('$todate') - TO_DAYS($due)) >= $past2,$value,0)) AS Overdue2,
+               supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1) 
+                       * (ov_amount + ov_gst + ov_discount),0)) as cur_credit,
+               supp.tax_group_id
 
-               FROM ".TB_PREF."suppliers,
-                        ".TB_PREF."payment_terms,
-                        ".TB_PREF."supp_trans
+               FROM ".TB_PREF."suppliers supp
+                        LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id AND trans.tran_date <= '$todate',
+                        ".TB_PREF."payment_terms
 
                WHERE
-                        ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
-                        AND ".TB_PREF."suppliers.supplier_id = $supplier_id
-                        AND ".TB_PREF."supp_trans.tran_date <= '$todate'
-                        AND ".TB_PREF."suppliers.supplier_id = ".TB_PREF."supp_trans.supplier_id
+                        supp.payment_terms = ".TB_PREF."payment_terms.terms_indicator
+                        AND supp.supplier_id = $supplier_id
 
                GROUP BY
-                         ".TB_PREF."suppliers.supp_name,
+                         supp.supp_name,
                          ".TB_PREF."payment_terms.terms,
                          ".TB_PREF."payment_terms.days_before_due,
                          ".TB_PREF."payment_terms.day_in_following_month";
 
     $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."suppliers.supp_name, ".TB_PREF."suppliers.curr_code, ".TB_PREF."suppliers.supplier_id,  ".TB_PREF."payment_terms.terms
-                       FROM ".TB_PREF."suppliers,
-                                ".TB_PREF."payment_terms
-                       WHERE
-                                ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
-                                AND ".TB_PREF."suppliers.supplier_id = ".db_escape($supplier_id);
-
-       $result = db_query($sql,"The customer details could not be retrieved");
-
-    }
-    else
-    {
-       $nil_balance = false;
-    }
-
     $supp = db_fetch($result);
 
-    if ($nil_balance == true)
-    {
-       $supp["Balance"] = 0;
-       $supp["Due"] = 0;
-       $supp["Overdue1"] = 0;
-       $supp["Overdue2"] = 0;
-    }
-
     return $supp;
-
 }
 
 function get_supplier($supplier_id)
@@ -188,4 +157,11 @@ function get_supplier_contacts($supplier_id, $action=null)
        return $results;
 }
 
+function get_current_supp_credit($supplier_id)
+{
+       $suppdet = get_supplier_details($supplier_id);
+       return $suppdet['cur_credit'];
+
+}
+
 ?>
\ No newline at end of file
index 1e6f4f79bfdaf5ea560d60b45ce14ea7a6a79500..6f50643fe33f20eeef969d3b502288eba56d916b 100644 (file)
@@ -42,12 +42,20 @@ function copy_to_cart()
 
 function get_supplier_details_to_order(&$order, $supplier_id)
 {
-       $sql = "SELECT curr_code,supp_name FROM ".TB_PREF."suppliers
-               WHERE supplier_id = '$supplier_id'";
+       $sql = "SELECT curr_code, supp_name, tax_group_id,
+                       supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1) 
+                               * (ov_amount + ov_gst + ov_discount),0)) as cur_credit
+               FROM ".TB_PREF."suppliers supp
+                        LEFT JOIN ".TB_PREF."supp_trans trans ON supp.supplier_id = trans.supplier_id
+               WHERE supp.supplier_id = '$supplier_id'
+               GROUP BY
+                         supp.supp_name";
+
        $result = db_query($sql, "The supplier details could not be retreived");
 
        $myrow = db_fetch($result);
-
+       $order->credit = $myrow["cur_credit"];
+       $order->tax_group_id = $myrow["tax_group_id"];
        $order->curr_code = $_POST['curr_code'] = $myrow["curr_code"];
        $order->supplier_name = $_POST['supplier_name'] = $myrow["supp_name"];
        $order->supplier_id = $_POST['supplier_id'] = $supplier_id;
@@ -125,6 +133,10 @@ function display_po_header(&$order)
                        $_POST['OrderDate']);
        }
 
+       supplier_credit_row($order->supplier_id, $order->credit);
+
+       table_section(2);
+
     if ($editable)
     {
        ref_row(_("Reference:"), 'ref');
@@ -135,8 +147,6 @@ function display_po_header(&$order)
        label_row(_("Reference:"), $order->reference);
     }
 
-       table_section(2);
-
        // check this out?????????
        //if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
        //      $_POST['OrderDate'] = $order->orig_order_date;
index 756add4d4259c6e27d993ed938fba77463da005b..ed6308c953d613f746320154e8ea2e8073b46fc1 100644 (file)
@@ -14,6 +14,7 @@ $page_security = 'SA_PURCHASEORDER';
 include_once($path_to_root . "/purchasing/includes/po_class.inc");
 include_once($path_to_root . "/includes/session.inc");
 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
+include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
 include_once($path_to_root . "/reporting/includes/reporting.inc");
 
 set_page_security( @$_SESSION['PO']->trans_type,
index 2284f18a87ad9ec73a293b7a919f060c7971cade..814ff8dcc7595fd0c4fc0f3375fec6bb242a271f 100644 (file)
@@ -377,6 +377,7 @@ start_table(TABLESTYLE, "width=90%");
 if (!isset($_POST['due_date']) || !is_date($_POST['due_date'])) {
        $_POST['due_date'] = get_invoice_duedate($_SESSION['Items']->payment, $_POST['DispatchDate']);
 }
+customer_credit_row($_SESSION['Items']->customer_id, $_SESSION['Items']->credit, "class='tableheader2'");
 start_row();
 date_cells(_("Invoice Dead-line"), 'due_date', '', null, 0, 0, 0, "class='tableheader2'");
 end_row();
index 5cf5f7bdda80e522eb13cd267a68a36eddf3ae20..f5a120583b7653046d263e9b4ae15259960cdd63 100644 (file)
@@ -74,6 +74,8 @@ class cart
        var $dimension2_id;
        var $payment;
        var $payment_terms; // cached payment terms
+       var $credit;
+       
        //-------------------------------------------------------------------------
        //
        //  $trans_no==0 => open new/direct document
@@ -238,6 +240,7 @@ class cart
                                        $this->due_date =
                                                add_days($this->document_date, $SysPrefs->default_delivery_required_by());
                        }
+               $this->credit = get_current_cust_credit($this->customer_id);
                $this->pos = user_pos();
                $pos = get_sales_point($this->pos);
                if (!$pos['cash_sale'] && !$pos['credit_sale']) 
@@ -305,6 +308,7 @@ class cart
                $this->payment_terms = get_payment_terms($payment);
                $this->cash = $this->payment_terms['cash_sale'];
                $this->cash_discount = $cdiscount;
+               $this->credit = get_current_cust_credit($customer_id);
        }
 
        function set_branch($branch_id, $tax_group_id, $tax_group_name, $phone='', $email='')
@@ -466,7 +470,7 @@ class cart
     // Adjustment for swiss franken, we always have 5 rappen = 1/20 franken
     if ($this->customer_currency == 'CHF') {
                        $val = $taxes['1']['Value'];
-      $val1 = (floatval((intval(round(($val*20),0)))/20));
+                       $val1 = (floatval((intval(round(($val*20),0)))/20));
                        $taxes['1']['Value'] = $val1;
                } 
                return $taxes;
@@ -500,7 +504,22 @@ class cart
                else
                        return round($this->freight_cost*$tax_rate/100,  user_price_dec());
        }
+       /*
+               Returns transaction value including all taxes
+       */
+       function get_trans_total() {
+               
+               $total = $this->get_items_total() + $this->freight_cost;
+               $dec = user_price_dec();
+               if (!$this->tax_included ) {
+                       $total += $this->get_shipping_tax();
+                       $taxes = $this->get_taxes();
+                       foreach($taxes as $tax)
+                               $total += round($tax['Value'], $dec);
+               }
 
+               return $total;
+       }
 } /* end of class defintion */
 
 class line_details
index 5f6748818f7e64be17a22f8e0d492977d651d77d..a9a6301b8f39ad65f0ca7d6680d69da9483de6ff 100644 (file)
@@ -72,33 +72,29 @@ function get_customer_details($customer_id, $to=null)
        $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)";
+    $value = "IFNULL(IF(trans.type=11 OR trans.type=12 OR trans.type=2, -1, 1) 
+       * (trans.ov_amount + trans.ov_gst + trans.ov_freight + trans.ov_freight_tax + trans.ov_discount),0)";
+
+       $due = "IF (trans.type=10, trans.due_date, 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,
 
-               Sum(".$value.") AS Balance,
-
+               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,
+               FROM ".TB_PREF."debtors_master
+                        LEFT JOIN ".TB_PREF."debtor_trans trans ON 
+                        trans.tran_date <= '$todate' AND ".TB_PREF."debtors_master.debtor_no = trans.debtor_no AND trans.type <> 13
+,
                         ".TB_PREF."payment_terms,
-                        ".TB_PREF."credit_status,
-                        ".TB_PREF."debtor_trans
+                        ".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.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,
@@ -110,46 +106,13 @@ function get_customer_details($customer_id, $to=null)
                          ".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);
-
-       $result = db_query($sql,"The customer details could not be retrieved");
-
-    }
-    else
-    {
-       $nil_balance = false;
-    }
-
     $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);
@@ -193,5 +156,12 @@ function get_customer_contacts($customer_id, $action=null)
        return $results;
 }
 
+function get_current_cust_credit($customer_id)
+{
+       $custdet = get_customer_details($customer_id);
+
+       return $custdet['credit_limit']-$custdet['Balance'];
+
+}
 
 ?>
\ No newline at end of file
index 27b4fe9a81461d1d695a12aa4c57cae04c112060..4ccc0fe810f327f0d3b2459079ec77169c49ef80 100644 (file)
@@ -19,9 +19,10 @@ function add_sales_order(&$order)
        $order_no = get_next_trans_no($order->trans_type);
        $del_date = date2sql($order->due_date);
        $order_type = 0; // this is default on new order
+       $total = $order->get_trans_total();
        $sql = "INSERT INTO ".TB_PREF."sales_orders (order_no, type, debtor_no, trans_type, branch_code, customer_ref, reference, comments, ord_date,
                order_type, ship_via, deliver_to, delivery_address, contact_phone,
-               freight_cost, from_stk_loc, delivery_date, payment_terms)
+               freight_cost, from_stk_loc, delivery_date, payment_terms, total)
                VALUES (" .db_escape($order_no) . "," .db_escape($order_type) . "," . db_escape($order->customer_id) .
                 ", " .db_escape($order->trans_type) . "," .db_escape($order->Branch) . ", ".
                        db_escape($order->cust_ref) .",". 
@@ -36,7 +37,8 @@ function add_sales_order(&$order)
                        db_escape($order->freight_cost) .", " . 
                        db_escape($order->Location) .", " .
                        db_escape($del_date) . "," .
-                       db_escape($order->payment) . ")";
+                       db_escape($order->payment) . "," .
+                       db_escape($total). ")";
 
        db_query($sql, "order Cannot be Added");
 
@@ -155,6 +157,7 @@ function update_sales_order($order)
        $ord_date = date2sql($order->document_date);
        $order_no =  key($order->trans_no);
        $version= current($order->trans_no);
+       $total = $order->get_trans_total();
 
        begin_transaction();
 
@@ -174,7 +177,8 @@ function update_sales_order($order)
                from_stk_loc = " .db_escape($order->Location) .",
                delivery_date = " .db_escape($del_date). ",
                version = ".($version+1).",
-               payment_terms = " .db_escape($order->payment). "
+               payment_terms = " .db_escape($order->payment). ",
+               total = ". db_escape($total) ."
         WHERE order_no=" . $order_no ."
         AND trans_type=".$order->trans_type." AND version=".$version;
        db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
@@ -431,26 +435,29 @@ function get_invoice_duedate($terms, $invdate)
 function get_customer_to_order($customer_id) {
 
        // Now check to ensure this account is not on hold */
-       $sql = "SELECT ".TB_PREF."debtors_master.name, "
-                 .TB_PREF."debtors_master.address, "
-                 .TB_PREF."credit_status.dissallow_invoices, "
-                 .TB_PREF."debtors_master.sales_type AS salestype, "
-                 .TB_PREF."debtors_master.dimension_id, "
-                 .TB_PREF."debtors_master.dimension2_id, "
-                 .TB_PREF."sales_types.sales_type, "
-                 .TB_PREF."sales_types.tax_included, "
-                 .TB_PREF."sales_types.factor, "
-                 .TB_PREF."debtors_master.curr_code, "
-                 .TB_PREF."debtors_master.discount,"
-                 .TB_PREF."debtors_master.payment_terms,"
-                 .TB_PREF."debtors_master.pymt_discount
-               FROM ".TB_PREF."debtors_master, "
+       $sql = "SELECT cust.name, 
+                 cust.address, "
+                 .TB_PREF."credit_status.dissallow_invoices, 
+                 cust.sales_type AS salestype,
+                 cust.dimension_id,
+                 cust.dimension2_id,
+                 stype.sales_type,
+                 stype.tax_included,
+                 stype.factor,
+                 cust.curr_code,
+                 cust.discount,
+                 cust.payment_terms,
+                 cust.pymt_discount,
+                 cust.credit_limit - Sum(IFNULL(IF(trans.type=11 OR trans.type=12 OR trans.type=2,
+                       -1, 1) * (ov_amount + ov_gst + ov_freight + ov_freight_tax + ov_discount),0)) as cur_credit
+               FROM ".TB_PREF."debtors_master cust
+                 LEFT JOIN ".TB_PREF."debtor_trans trans ON trans.type!=".ST_CUSTDELIVERY." AND trans.debtor_no = cust.debtor_no,"
                  .TB_PREF."credit_status, "
-                 .TB_PREF."sales_types
-               WHERE ".TB_PREF."debtors_master.sales_type="
-                 .TB_PREF."sales_types.id
-               AND ".TB_PREF."debtors_master.credit_status=".TB_PREF."credit_status.id
-               AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id);
+                 .TB_PREF."sales_types stype
+               WHERE cust.sales_type=stype.id
+                       AND cust.credit_status=".TB_PREF."credit_status.id
+                       AND cust.debtor_no = ".db_escape($customer_id)
+               ." GROUP by cust.debtor_no";
 
        $result =db_query($sql,"Customer Record Retreive");
        return  db_fetch($result);
index 57be11958de99cba29686777a7cf22882bc2576a..3fef87209bdf3200269ff544bf5e365166082d8e 100644 (file)
@@ -90,6 +90,8 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
        $order->set_sales_type($myrow['salestype'], $myrow['sales_type'], $myrow['tax_included'],
            $myrow['factor']);
 
+       $order->credit = $myrow['cur_credit'];
+
        if ($order->trans_type != ST_SALESORDER && $order->trans_type != ST_SALESQUOTE)
        {
                $order->dimension_id = $myrow['dimension_id'];
@@ -135,7 +137,7 @@ function get_customer_details_to_order(&$order, $customer_id, $branch_id)
 
 function display_order_summary($title, &$order, $editable_items=false)
 {
-       global $path_to_root, $SysPrefs;
+       global $SysPrefs;
 
        display_heading($title);
 
@@ -363,14 +365,19 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
 
        ref_row(_("Reference").':', 'ref', _('Reference number unique for this document type'), null, '');
 
+       table_section(2);
+
        if (!is_company_currency($order->customer_currency))
        {
-           table_section(2);
-
                label_row(_("Customer Currency:"), $order->customer_currency);
                exchange_rate_display($order->customer_currency, get_company_currency(),
                        ($editable ? $_POST['OrderDate'] : $order->document_date));
        }
+
+       customer_credit_row($_POST['customer_id'], $order->credit);
+
+       label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
+
        table_section(3);
 
        if (($order->trans_type != ST_CUSTDELIVERY) && ($order->pos != -1)) { // editable payment type
@@ -406,8 +413,6 @@ function display_order_header(&$order, $editable, $date_text, $display_tax_group
                $change_prices = 1;
        }
 
-       label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
-       
        table_section(4);
 
        if ($editable)