Dimension is missing in Supplier Invoice View, GL Rows.
[fa-stable.git] / sales / includes / db / sales_order_db.inc
index e4eaa5c15a4d69908b393cad61ea32a49e6feb6b..5ead6dd49e4e1d1578d7c6ea74217582d9ef1aec 100644 (file)
@@ -55,29 +55,7 @@ function add_sales_order(&$order)
        foreach ($order->line_items as $line)
        {
                if ($loc_notification == 1 && is_inventory_item($line->stock_id))
-               {
-                       $sql = "SELECT ".TB_PREF."loc_stock.*, ".TB_PREF."locations.location_name, ".TB_PREF."locations.email
-                               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 . "'";
-                       $res = db_query($sql,"a location could not be retreived");
-                       $loc = db_fetch($res);
-                       if ($loc['email'] != "")
-                       {
-                               $qoh = get_qoh_on_date($line->stock_id, $order->Location);
-                               $qoh -= get_demand_qty($line->stock_id, $order->Location);
-                               $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
-                               $qoh -= $line->quantity;
-                               if ($qoh < $loc['reorder_level'])
-                               {
-                                       $st_ids[] = $line->stock_id;
-                                       $st_names[] = $line->item_description;
-                                       $st_num[] = $qoh - $loc['reorder_level'];
-                                       $st_reorder[] = $loc['reorder_level'];
-                               }
-                       }
-               }
+                       $loc = calculate_reorder_level($order->Location, $line, $st_ids, $st_names, $st_num, $st_reorder); 
 
                $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, trans_type, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
                $sql .= $order_no . ",".$order->trans_type .
@@ -98,23 +76,7 @@ function add_sales_order(&$order)
        commit_transaction();
 
        if ($loc_notification == 1 && count($st_ids) > 0)
-       {
-               require_once($path_to_root . "/reporting/includes/class.mail.inc");
-               $company = get_company_prefs();
-               $mail = new email($company['coy_name'], $company['email']);
-               $from = $company['coy_name'] . " <" . $company['email'] . ">";
-               $to = $loc['location_name'] . " <" . $loc['email'] . ">";
-               $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
-               $msg = "\n";
-               for ($i = 0; $i < count($st_ids); $i++)
-                       $msg .= $st_ids[$i] . " " . $st_names[$i] . ", " . _("Re-Order Level") . ": " . $st_reorder[$i] . ", " . _("Below") . ": " . $st_num[$i] . "\n";
-               $msg .= "\n" . _("Please reorder") . "\n\n";
-               $msg .= $company['coy_name'];
-               $mail->to($to);
-               $mail->subject($subject);
-               $mail->text($msg);
-               $ret = $mail->send();
-       }
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
        return $order_no;
 }
 
@@ -165,6 +127,7 @@ function update_sales_order($order)
        $total = $order->get_trans_total();
 
        begin_transaction();
+       hook_db_prewrite($order, $order->trans_type);
 
        $sql = "UPDATE ".TB_PREF."sales_orders SET type =".db_escape($order->so_type)." ,
                debtor_no = " . db_escape($order->customer_id) . ",
@@ -188,7 +151,14 @@ 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");
 
@@ -203,32 +173,16 @@ function update_sales_order($order)
        foreach ($order->line_items as $line)
        {
                if ($loc_notification == 1 && is_inventory_item($line->stock_id))
-               {
-                       $sql = "SELECT ".TB_PREF."loc_stock.*, "
-                                 .TB_PREF."locations.location_name, "
-                                 .TB_PREF."locations.email
-                               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 = ".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'] != "")
-                       {
-                               $qoh = get_qoh_on_date($line->stock_id, $order->Location);
-                               $qoh -= get_demand_qty($line->stock_id, $order->Location);
-                               $qoh -= get_demand_asm_qty($line->stock_id, $order->Location);
-                               $qoh -= $line->quantity;
-                               if ($qoh < $loc['reorder_level'])
-                               {
-                                       $st_ids[] = $line->stock_id;
-                                       $st_names[] = $line->item_description;
-                                       $st_num[] = $qoh - $loc['reorder_level'];
-                                       $st_reorder[] = $loc['reorder_level'];
-                               }
-                       }
+                       $loc = calculate_reorder_level($order->Location, $line, $st_ids, $st_names, $st_num, $st_reorder); 
+               if($line->id) {
+                       $sql = "UPDATE ".TB_PREF."sales_order_details
+                                               SET description = ".db_escape($line->item_description) . ", 
+                                                               unit_price = ".db_escape($line->price) . ",
+                                                               quantity = ".db_escape($line->quantity) . ",
+                                                               discount_percent = ".db_escape($line->discount_percent) . "
+                                               WHERE id = {$line->id}";
                }
+               else {
                $sql = "INSERT INTO ".TB_PREF."sales_order_details
                 (id, order_no, trans_type, stk_code,  description, unit_price, quantity,
                  discount_percent, qty_sent)
@@ -241,6 +195,7 @@ function update_sales_order($order)
                  .db_escape($line->quantity) . ", "
                  .db_escape($line->discount_percent) . ", "
                  .db_escape($line->qty_done) ." )";
+               }
 
                db_query($sql, "Old order Cannot be Inserted");
 
@@ -248,27 +203,11 @@ function update_sales_order($order)
 
        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)
-       {
-               require_once($path_to_root . "/reporting/includes/class.mail.inc");
-               $company = get_company_prefs();
-               $mail = new email($company['coy_name'], $company['email']);
-               $from = $company['coy_name'] . " <" . $company['email'] . ">";
-               $to = $loc['location_name'] . " <" . $loc['email'] . ">";
-               $subject = _("Stocks below Re-Order Level at " . $loc['location_name']);
-               $msg = "\n";
-               for ($i = 0; $i < count($st_ids); $i++)
-                       $msg .= $st_ids[$i] . " " . $st_names[$i] . ", "
-                         . _("Re-Order Level") . ": " . $st_reorder[$i] . ", "
-                         . _("Below") . ": " . $st_num[$i] . "\n";
-               $msg .= "\n" . _("Please reorder") . "\n\n";
-               $msg .= $company['coy_name'];
-               $mail->to($to);
-               $mail->subject($subject);
-               $mail->text($msg);
-               $ret = $mail->send();
-       }
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
 }
 
 //----------------------------------------------------------------------------------------
@@ -278,28 +217,28 @@ 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 "
-       ."FROM ".TB_PREF."sales_orders sorder"
+       ."FROM ".TB_PREF."sales_orders sorder 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 );
 
@@ -357,7 +296,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"]);
 
@@ -576,4 +515,4 @@ function get_sql_for_sales_orders_view($selected_customer, $trans_type, $trans_n
                                        sorder.deliver_to";
        return $sql;
 }
-?>
\ No newline at end of file
+?>