Merged last changes from stable.
[fa-stable.git] / sales / includes / db / sales_order_db.inc
index 5c72a6e4f351f544fe0cf0d8382fa2b939d591bc..b3cd6553952df0fa6b4118c549e894c1ae49c4be 100644 (file)
@@ -167,8 +167,11 @@ function update_sales_order($order)
        $total = $order->get_trans_total();
 
        begin_transaction();
+       hook_db_prewrite($order, $order->trans_type);
+
        if ($order->trans_type == ST_SALESORDER)
                $allocs = get_payments_for($order_no, ST_SALESORDER);
+
        $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) . ",
@@ -192,6 +195,17 @@ function update_sales_order($order)
         AND trans_type=".$order->trans_type." AND version=".$version;
        db_query($sql, "order Cannot be Updated, this can be concurrent edition conflict");
 
+       $id_tokeep = array();
+       foreach ($order->line_items as $line) {
+               array_push($id_tokeep , $line->id);
+       }
+       $id_list = implode(', ', $id_tokeep);
+       
+       $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . db_escape($order_no) . " AND trans_type=".$order->trans_type;
+       $sql .= " AND id NOT IN ($id_list)";
+
+       db_query($sql, "Old order Cannot be Deleted");
+
        if ($loc_notification == 1)
        {
                include_once($path_to_root . "/inventory/includes/inventory_db.inc");
@@ -260,6 +274,8 @@ function update_sales_order($order)
                reallocate_payments($order_no, ST_SALESORDER, $ord_date, $total, $allocs);
        add_audit_trail($order->trans_type, $order_no, $order->document_date, _("Updated."));
        $Refs->save($order->trans_type, $order_no, $order->reference);
+
+       hook_db_postwrite($order, $order->trans_type);
        commit_transaction();
        if ($loc_notification == 1 && count($st_ids) > 0)
        {
@@ -290,12 +306,14 @@ function get_sales_order_header($order_no, $trans_type)
        $sql = "SELECT sorder.*, "
          ."cust.name, "
          ."cust.curr_code, "
+         ."cust.address, "
          ."loc.location_name, "
          ."cust.discount, "
          ."stype.sales_type, "
          ."stype.id AS sales_type_id, "
          ."stype.tax_included, "
-         ."ship.shipper_name, "
+         ."stype.factor, "
+         ."ship.shipper_name, "
          ."tax_group.name AS tax_group_name , "
          ."tax_group.id AS tax_group_id, "
          ."cust.tax_id,"
@@ -308,19 +326,18 @@ function get_sales_order_header($order_no, $trans_type)
                                 allocs ON sorder.trans_type=".ST_SALESORDER." AND allocs.trans_no_to=sorder.order_no
                        LEFT JOIN (SELECT order_, sum(alloc) inv_allocs FROM ".TB_PREF."debtor_trans 
                                WHERE type=".ST_SALESINVOICE." AND order_=".db_escape($order_no)."  GROUP BY order_)
-                                inv ON sorder.trans_type=".ST_SALESORDER." AND inv.order_=sorder.order_no,"
+                                inv ON sorder.trans_type=".ST_SALESORDER." AND inv.order_=sorder.order_no
+                       LEFT JOIN ".TB_PREF."shippers ship ON  ship.shipper_id = sorder.ship_via,"
          .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
+         .TB_PREF."locations loc
        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 );
 
@@ -378,7 +395,7 @@ function read_sales_order($order_no, &$order, $trans_type)
          $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
+           $myrow["tax_included"], $myrow["factor"]); // no default price calculations on edit
 
        $order->set_location($myrow["from_stk_loc"], $myrow["location_name"]);
 
@@ -626,6 +643,7 @@ function get_sql_for_sales_orders_view($selected_customer, $trans_type, $trans_n
                                        sorder.deliver_to";
        return $sql;
 }
+
 //--------------------------------------------------------------------------------------------------
 function update_prepaid_so_line($line_id, $qty_invoiced)
 {
@@ -658,4 +676,3 @@ function is_sales_order_started($order_no)
        $result = db_fetch(db_query($sql, "cannot retrieve sales invoices for sales order"));
        return $result[0];
 }
-?>
\ No newline at end of file