Merged changes form main trunk (2.2.8, 2.2.9)
[fa-stable.git] / sales / includes / db / sales_order_db.inc
index 2d2e46964fd5616009d5c407577d06d97f203261..27b4fe9a81461d1d695a12aa4c57cae04c112060 100644 (file)
@@ -21,7 +21,7 @@ function add_sales_order(&$order)
        $order_type = 0; // this is default on new order
        $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,
-               contact_email, freight_cost, from_stk_loc, delivery_date)
+               freight_cost, from_stk_loc, delivery_date, payment_terms)
                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) .",". 
@@ -33,10 +33,10 @@ function add_sales_order(&$order)
                        db_escape($order->deliver_to) . "," .
                        db_escape($order->delivery_address) . ", " .
                        db_escape($order->phone) . ", " . 
-                       db_escape($order->email) . ", " .
                        db_escape($order->freight_cost) .", " . 
                        db_escape($order->Location) .", " .
-                       db_escape($del_date) . ")";
+                       db_escape($del_date) . "," .
+                       db_escape($order->payment) . ")";
 
        db_query($sql, "order Cannot be Added");
 
@@ -118,12 +118,17 @@ function delete_sales_order($order_no, $trans_type)
 {
        begin_transaction();
 
-       $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no . " AND trans_type=".$trans_type;
+       $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . db_escape($order_no) 
+               . " AND trans_type=".db_escape($trans_type);
+
        db_query($sql, "order Header Delete");
 
-       $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no . " AND trans_type=".$trans_type;
+       $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" 
+               .db_escape($order_no) . " AND trans_type=".db_escape($trans_type);
        db_query($sql, "order Detail Delete");
 
+       delete_reference($trans_type, $order_no);
+
        add_audit_trail($trans_type, $order_no, Today(), _("Deleted."));
        commit_transaction();
 }
@@ -153,7 +158,7 @@ function update_sales_order($order)
 
        begin_transaction();
 
-       $sql = "UPDATE ".TB_PREF."sales_orders SET type =".$order->so_type." ,
+       $sql = "UPDATE ".TB_PREF."sales_orders SET type =".db_escape($order->so_type)." ,
                debtor_no = " . db_escape($order->customer_id) . ",
                branch_code = " . db_escape($order->Branch) . ",
                customer_ref = ". db_escape($order->cust_ref) .",
@@ -165,11 +170,11 @@ function update_sales_order($order)
                deliver_to = " . db_escape($order->deliver_to) . ",
                delivery_address = " . db_escape($order->delivery_address) . ",
                contact_phone = " .db_escape($order->phone) . ",
-               contact_email = " .db_escape($order->email) . ",
                freight_cost = " .db_escape($order->freight_cost) .",
                from_stk_loc = " .db_escape($order->Location) .",
                delivery_date = " .db_escape($del_date). ",
-               version = ".($version+1)."
+               version = ".($version+1).",
+               payment_terms = " .db_escape($order->payment). "
         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");
@@ -196,8 +201,8 @@ function update_sales_order($order)
                                FROM ".TB_PREF."loc_stock, "
                                  .TB_PREF."locations
                                WHERE ".TB_PREF."loc_stock.loc_code=".TB_PREF."locations.loc_code
-                                AND ".TB_PREF."loc_stock.stock_id = '" . $line->stock_id . "'
-                                AND ".TB_PREF."loc_stock.loc_code = '" . $order->Location . "'";
+                                AND ".TB_PREF."loc_stock.stock_id = ".db_escape($line->stock_id)."
+                                AND ".TB_PREF."loc_stock.loc_code = ".db_escape($order->Location);
                        $res = db_query($sql,"a location could not be retreived");
                        $loc = db_fetch($res);
                        if ($loc['email'] != "")
@@ -216,10 +221,11 @@ function update_sales_order($order)
                        }
                }
                $sql = "INSERT INTO ".TB_PREF."sales_order_details
-                (order_no, trans_type, stk_code,  description, unit_price, quantity,
+                (id, order_no, trans_type, stk_code,  description, unit_price, quantity,
                  discount_percent, qty_sent)
                 VALUES (";
-               $sql .= $order_no . ",".$order->trans_type.","
+               $sql .= db_escape($line->id ? $line->id : 0) . ","
+                 .$order_no . ",".$order->trans_type.","
                  .db_escape($line->stock_id) . ","
                  .db_escape($line->item_description) . ", "
                  .db_escape($line->price) . ", "
@@ -261,34 +267,33 @@ function update_sales_order($order)
 
 function get_sales_order_header($order_no, $trans_type)
 {
-       $sql = "SELECT ".TB_PREF."sales_orders.*, "
-         .TB_PREF."debtors_master.name, "
-         .TB_PREF."debtors_master.curr_code, "
-         .TB_PREF."debtors_master.email AS master_email, "
-         .TB_PREF."locations.location_name, "
-         .TB_PREF."debtors_master.payment_terms, "
-         .TB_PREF."debtors_master.discount, "
-         .TB_PREF."sales_types.sales_type, "
-         .TB_PREF."sales_types.id AS sales_type_id, "
-         .TB_PREF."sales_types.tax_included, "
-         .TB_PREF."shippers.shipper_name, "
-         .TB_PREF."tax_groups.name AS tax_group_name , "
-         .TB_PREF."tax_groups.id AS tax_group_id
-       FROM ".TB_PREF."sales_orders, "
-         .TB_PREF."debtors_master, "
-         .TB_PREF."sales_types, "
-         .TB_PREF."tax_groups, "
-         .TB_PREF."cust_branch, "
-         .TB_PREF."locations, "
-         .TB_PREF."shippers
-       WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
-               AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code
-               AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
-               AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no
-               AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
-               AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
-               AND ".TB_PREF."sales_orders.trans_type = " . $trans_type ."
-               AND ".TB_PREF."sales_orders.order_no = " . $order_no ;
+       $sql = "SELECT sorder.*, "
+         ."cust.name, "
+         ."cust.curr_code, "
+         ."loc.location_name, "
+         ."cust.discount, "
+         ."stype.sales_type, "
+         ."stype.id AS sales_type_id, "
+         ."stype.tax_included, "
+         ."ship.shipper_name, "
+         ."tax_group.name AS tax_group_name , "
+         ."tax_group.id AS tax_group_id "
+       ."FROM ".TB_PREF."sales_orders sorder, "
+         .TB_PREF."debtors_master cust,"
+         .TB_PREF."sales_types stype, "
+         .TB_PREF."tax_groups tax_group, "
+         .TB_PREF."cust_branch branch,"
+         .TB_PREF."locations loc, "
+         .TB_PREF."shippers ship
+       WHERE sorder.order_type=stype.id
+               AND branch.branch_code = sorder.branch_code
+               AND branch.tax_group_id = tax_group.id
+               AND sorder.debtor_no = cust.debtor_no
+               AND loc.loc_code = sorder.from_stk_loc
+               AND ship.shipper_id = sorder.ship_via
+               AND sorder.trans_type = " . db_escape($trans_type) ."
+               AND sorder.order_no = " . db_escape($order_no );
+
        $result = db_query($sql, "order Retreival");
 
        $num = db_num_rows($result);
@@ -319,7 +324,8 @@ function get_sales_order_details($order_no, $trans_type) {
                        .TB_PREF."stock_master.overhead_cost AS standard_cost
        FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master
        WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id
-       AND order_no =" . $order_no . " AND trans_type = " . $trans_type . " ORDER BY id";
+       AND order_no =" . db_escape($order_no) 
+               ." AND trans_type = " . db_escape($trans_type) . " ORDER BY id";
 
        return db_query($sql, "Retreive order Line Items");
 }
@@ -334,10 +340,10 @@ function read_sales_order($order_no, &$order, $trans_type)
        $order->trans_no = array($order_no=> $myrow["version"]);
 
        $order->set_customer($myrow["debtor_no"], $myrow["name"],
-         $myrow["curr_code"], $myrow["discount"]);
+         $myrow["curr_code"], $myrow["discount"], $myrow["payment_terms"]);
 
        $order->set_branch($myrow["branch_code"], $myrow["tax_group_id"],
-         $myrow["tax_group_name"], $myrow["contact_phone"], $myrow["contact_email"]);
+         $myrow["tax_group_name"], $myrow["contact_phone"]);
 
        $order->set_sales_type($myrow["sales_type_id"], $myrow["sales_type"], 
            $myrow["tax_included"], 0); // no default price calculations on edit
@@ -375,13 +381,18 @@ function read_sales_order($order_no, &$order, $trans_type)
 function sales_order_has_deliveries($order_no)
 {
        $sql = "SELECT SUM(qty_sent) FROM ".TB_PREF.
-       "sales_order_details WHERE order_no=$order_no AND trans_type=".ST_SALESORDER."";
+       "sales_order_details WHERE order_no=".db_escape($order_no)
+       ." AND trans_type=".ST_SALESORDER."";
 
        $result = db_query($sql, "could not query for sales order usage");
 
        $row = db_fetch_row($result);
 
-       return ($row[0] > 0);
+       if ($row[0] > 0)
+               return true;  // 2010-04-21 added check for eventually voided deliveries, Joe Hunt
+       $sql = "SELECT order_ FROM ".TB_PREF."debtor_trans WHERE type=".ST_CUSTDELIVERY." AND order_=".db_escape($order_no);
+       $result = db_query($sql,"The related delivery notes could not be retreived");
+       return (db_num_rows($result) > 0);      
 }
 
 //----------------------------------------------------------------------------------------
@@ -390,28 +401,26 @@ function close_sales_order($order_no)
 {
        // set the quantity of each item to the already sent quantity. this will mark item as closed.
        $sql = "UPDATE ".TB_PREF."sales_order_details
-               SET quantity = qty_sent WHERE order_no = $order_no AND trans_type=".ST_SALESORDER."";
+               SET quantity = qty_sent WHERE order_no = ".db_escape($order_no)
+               ." AND trans_type=".ST_SALESORDER."";
 
        db_query($sql, "The sales order detail record could not be updated");
 }
 
 //---------------------------------------------------------------------------------------------------------------
 
-function get_invoice_duedate($debtorno, $invdate)
+function get_invoice_duedate($terms, $invdate)
 {
        if (!is_date($invdate))
        {
                return new_doc_date();
        }
-       $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
-               ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
-               ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
-
-       $result = db_query($sql,"The customer details could not be retrieved");
-       $myrow = db_fetch($result);
-
-       if (db_num_rows($result) == 0)
+       
+       $myrow = get_payment_terms($terms);
+       
+       if (!$myrow)
                return $invdate;
+
        if ($myrow['day_in_following_month'] > 0)
                $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
        else
@@ -433,6 +442,7 @@ function get_customer_to_order($customer_id) {
                  .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, "
                  .TB_PREF."credit_status, "
@@ -440,7 +450,7 @@ function get_customer_to_order($customer_id) {
                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 = '" . $customer_id . "'";
+               AND ".TB_PREF."debtors_master.debtor_no = ".db_escape($customer_id);
 
        $result =db_query($sql,"Customer Record Retreive");
        return  db_fetch($result);
@@ -452,9 +462,7 @@ function get_branch_to_order($customer_id, $branch_id) {
                $sql = "SELECT ".TB_PREF."cust_branch.br_name, "
                        .TB_PREF."cust_branch.br_address, "
                        .TB_PREF."cust_branch.br_post_address, "
-                       .TB_PREF."cust_branch.phone, "
-                       .TB_PREF."cust_branch.email,
-                         default_location, location_name, default_ship_via, "
+                       ." default_location, location_name, default_ship_via, "
                        .TB_PREF."tax_groups.name AS tax_group_name, "
                        .TB_PREF."tax_groups.id AS tax_group_id
                        FROM ".TB_PREF."cust_branch, "
@@ -462,9 +470,90 @@ function get_branch_to_order($customer_id, $branch_id) {
                          .TB_PREF."locations
                        WHERE ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
                                AND ".TB_PREF."locations.loc_code=default_location
-                               AND ".TB_PREF."cust_branch.branch_code='" . $branch_id . "'
-                               AND ".TB_PREF."cust_branch.debtor_no = '" . $customer_id . "'";
+                               AND ".TB_PREF."cust_branch.branch_code=".db_escape($branch_id)."
+                               AND ".TB_PREF."cust_branch.debtor_no = ".db_escape($customer_id);
 
            return db_query($sql,"Customer Branch Record Retreive");
 }
+
+function get_sql_for_sales_orders_view($selected_customer, $trans_type)
+{
+       global $selected_stock_item;
+       
+       $sql = "SELECT 
+                       sorder.order_no,
+                       sorder.reference,
+                       debtor.name,
+                       branch.br_name,"
+                       .($_POST['order_view_mode']=='InvoiceTemplates' 
+                               || $_POST['order_view_mode']=='DeliveryTemplates' ?
+                        "sorder.comments, " : "sorder.customer_ref, ")
+                       ."sorder.ord_date,
+                       sorder.delivery_date,
+                       sorder.deliver_to,
+                       Sum(line.unit_price*line.quantity*(1-line.discount_percent))+freight_cost AS OrderValue,
+                       sorder.type,
+                       debtor.curr_code,
+                       Sum(line.qty_sent) AS TotDelivered,
+                       Sum(line.quantity) AS TotQuantity
+               FROM ".TB_PREF."sales_orders as sorder, "
+                       .TB_PREF."sales_order_details as line, "
+                       .TB_PREF."debtors_master as debtor, "
+                       .TB_PREF."cust_branch as branch
+                       WHERE sorder.order_no = line.order_no
+                       AND sorder.trans_type = line.trans_type
+                       AND sorder.trans_type = $trans_type
+                       AND sorder.debtor_no = debtor.debtor_no
+                       AND sorder.branch_code = branch.branch_code
+                       AND debtor.debtor_no = branch.debtor_no";
+
+       if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
+       {
+               // search orders with number like 
+               $number_like = "%".$_POST['OrderNumber'];
+               $sql .= " AND sorder.order_no LIKE ".db_escape($number_like)
+                               ." GROUP BY sorder.order_no";
+       }
+       elseif (isset($_POST['OrderReference']) && $_POST['OrderReference'] != "")
+       {
+               // search orders with reference like 
+               $number_like = "%".$_POST['OrderReference']."%";
+               $sql .= " AND sorder.reference LIKE ".db_escape($number_like)
+                               ." GROUP BY sorder.order_no";
+       }
+       else    // ... or select inquiry constraints
+       {
+               if ($_POST['order_view_mode']!='DeliveryTemplates' && $_POST['order_view_mode']!='InvoiceTemplates')
+               {
+                       $date_after = date2sql($_POST['OrdersAfterDate']);
+                       $date_before = date2sql($_POST['OrdersToDate']);
+
+                       $sql .=  " AND sorder.ord_date >= '$date_after'"
+                                       ." AND sorder.ord_date <= '$date_before'";
+               }
+               if ($trans_type == ST_SALESQUOTE && !check_value('show_all'))
+                       $sql .= " AND sorder.delivery_date >= '".date2sql(Today())."'";
+               if ($selected_customer != -1)
+                       $sql .= " AND sorder.debtor_no=".db_escape($selected_customer);
+
+               if (isset($selected_stock_item))
+                       $sql .= " AND line.stk_code=".db_escape($selected_stock_item);
+
+               if (isset($_POST['StockLocation']) && $_POST['StockLocation'] != ALL_TEXT)
+                       $sql .= " AND sorder.from_stk_loc = ".db_escape($_POST['StockLocation']);
+
+               if ($_POST['order_view_mode']=='OutstandingOnly')
+                       $sql .= " AND line.qty_sent < line.quantity";
+               elseif ($_POST['order_view_mode']=='InvoiceTemplates' || $_POST['order_view_mode']=='DeliveryTemplates')
+                       $sql .= " AND sorder.type=1";
+
+               $sql .= " GROUP BY sorder.order_no,
+                                       sorder.debtor_no,
+                                       sorder.branch_code,
+                                       sorder.customer_ref,
+                                       sorder.ord_date,
+                                       sorder.deliver_to";
+       }
+       return $sql;
+}
 ?>
\ No newline at end of file