Cleanup in db_pager get_sql and testing it.
[fa-stable.git] / sales / includes / db / sales_order_db.inc
index fb57fa457e3914f82d22e63f45a899b22b2ae098..e851f747ecab892c10cd9a643463456c45707963 100644 (file)
@@ -12,7 +12,7 @@
 //----------------------------------------------------------------------------------------
 function add_sales_order(&$order)
 {
-       global $loc_notification, $path_to_root, $Refs;
+       global $SysPrefs, $path_to_root, $Refs;
 
        begin_transaction();
        hook_db_prewrite($order, $order->trans_type);
@@ -45,7 +45,7 @@ function add_sales_order(&$order)
 
        $order->trans_no = array($order_no=>0);
 
-       if ($loc_notification == 1)
+       if ($SysPrefs->loc_notification() == 1)
        {
                include_once($path_to_root . "/inventory/includes/inventory_db.inc");
                $st_ids = array();
@@ -55,30 +55,8 @@ 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'];
-                               }
-                       }
-               }
+               if ($SysPrefs->loc_notification() == 1 && is_inventory_item($line->stock_id))
+                       $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 .
@@ -99,24 +77,8 @@ function add_sales_order(&$order)
        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();
-       }
+       if ($SysPrefs->loc_notification() == 1 && count($st_ids) > 0)
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
        return $order_no;
 }
 
@@ -158,7 +120,7 @@ function update_sales_order_version($order)
 
 function update_sales_order($order)
 {
-       global $loc_notification, $path_to_root, $Refs;
+       global $SysPrefs, $path_to_root, $Refs;
 
        $del_date = date2sql($order->due_date);
        $ord_date = date2sql($order->document_date);
@@ -167,8 +129,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,7 +157,18 @@ 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");
 
-       if ($loc_notification == 1)
+       $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 ($SysPrefs->loc_notification() == 1)
        {
                include_once($path_to_root . "/inventory/includes/inventory_db.inc");
                $st_ids = array();
@@ -202,33 +178,9 @@ 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'];
-                               }
-                       }
-               }
+               if ($SysPrefs->loc_notification() == 1 && is_inventory_item($line->stock_id))
+                       $loc = calculate_reorder_level($order->Location, $line, $st_ids, $st_names, $st_num, $st_reorder); 
+
                if (!$line->id) //new line
                        $sql = "INSERT INTO ".TB_PREF."sales_order_details
                         (order_no, trans_type, stk_code,  description, unit_price, quantity,
@@ -260,27 +212,11 @@ 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)
-       {
-               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();
-       }
+       if ($SysPrefs->loc_notification() == 1 && count($st_ids) > 0)
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
 }
 
 //----------------------------------------------------------------------------------------
@@ -528,8 +464,8 @@ function get_branch_to_order($customer_id, $branch_id) {
        OutstandingOnly
        PrepaidOrders
 */
-function get_sql_for_sales_orders_view($selected_customer, $trans_type, $trans_no, $filter, 
-       $stock_item=null, $from='', $to='', $ref='', $location='', $customer_id=ALL_TEXT)
+function get_sql_for_sales_orders_view($trans_type, $trans_no, $filter, 
+       $stock_item='', $from='', $to='', $ref='', $location=ALL_TEXT, $customer_id=ALL_TEXT)
 {
 
        $sql = "SELECT 
@@ -579,8 +515,7 @@ function get_sql_for_sales_orders_view($selected_customer, $trans_type, $trans_n
        elseif ($ref != "")
        {
                // search orders with reference like 
-               $number_like = "%".$ref."%";
-               $sql .= " AND sorder.reference LIKE ".db_escape($number_like);
+               $sql .= " AND sorder.reference LIKE ".db_escape('%' . $ref . '%');
 //                             ." GROUP BY sorder.order_no";
        }
        else    // ... or select inquiry constraints
@@ -597,13 +532,13 @@ function get_sql_for_sales_orders_view($selected_customer, $trans_type, $trans_n
                if ($trans_type == ST_SALESQUOTE && !check_value('show_all'))
                        $sql .= " AND sorder.delivery_date >= '".date2sql(Today())."' AND line.qty_sent=0"; // show only outstanding, not realized quotes
 
-               if ($selected_customer != -1)
-                       $sql .= " AND sorder.debtor_no=".db_escape($selected_customer);
+               //if ($selected_customer != -1)
+               //      $sql .= " AND sorder.debtor_no=".db_escape($selected_customer);
 
-               if (isset($stock_item))
+               if ($stock_item != ALL_TEXT)
                        $sql .= " AND line.stk_code=".db_escape($stock_item);
 
-               if ($location)
+               if ($location != ALL_TEXT)
                        $sql .= " AND sorder.from_stk_loc = ".db_escape($location);
 
                if ($filter=='OutstandingOnly')
@@ -627,6 +562,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)
 {
@@ -659,4 +595,13 @@ 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
+
+//---------------------------------------------------------------------------------------------
+//
+// Mark/unmark sales order as template.
+//
+function sales_order_set_template($id, $status)
+{
+       $sql = "UPDATE ".TB_PREF."sales_orders SET type = ".db_escape($status)." WHERE order_no=".db_escape($id);
+       db_query($sql, "Can't change sales order type");
+}