Additional access control related ui helpers added: check_edit_access, access_post...
[fa-stable.git] / sales / includes / db / sales_order_db.inc
index 86ba5aa39a2e2bfbdb8b2caa4ad37ba86176c324..ff61aaa5eef5b8c091a48ef38a43c2741131fa52 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,7 +55,7 @@ function add_sales_order(&$order)
        }
        foreach ($order->line_items as $line)
        {
-               if ($loc_notification == 1 && is_inventory_item($line->stock_id))
+               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 (";
@@ -77,7 +77,7 @@ function add_sales_order(&$order)
        hook_db_postwrite($order, $order->trans_type);
        commit_transaction();
 
-       if ($loc_notification == 1 && count($st_ids) > 0)
+       if ($SysPrefs->loc_notification() == 1 && count($st_ids) > 0)
                send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
        return $order_no;
 }
@@ -120,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);
@@ -168,7 +168,7 @@ function update_sales_order($order)
 
        db_query($sql, "Old order Cannot be Deleted");
 
-       if ($loc_notification == 1)
+       if ($SysPrefs->loc_notification() == 1)
        {
                include_once($path_to_root . "/inventory/includes/inventory_db.inc");
                $st_ids = array();
@@ -178,7 +178,7 @@ function update_sales_order($order)
        }
        foreach ($order->line_items as $line)
        {
-               if ($loc_notification == 1 && is_inventory_item($line->stock_id))
+               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
@@ -215,7 +215,7 @@ function update_sales_order($order)
 
        hook_db_postwrite($order, $order->trans_type);
        commit_transaction();
-       if ($loc_notification == 1 && count($st_ids) > 0)
+       if ($SysPrefs->loc_notification() == 1 && count($st_ids) > 0)
                send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
 }
 
@@ -464,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 
@@ -489,7 +489,8 @@ function get_sql_for_sales_orders_view($selected_customer, $trans_type, $trans_n
                        prep_amount,
                        allocs.ord_payments,
                        inv.inv_payments,
-                       sorder.total
+                       sorder.total,
+                       sorder.trans_type
                FROM ".TB_PREF."sales_orders as sorder
                LEFT JOIN (SELECT trans_no_to, sum(amt) ord_payments FROM ".TB_PREF."cust_allocations WHERE trans_type_to=".ST_SALESORDER." GROUP BY trans_no_to)
                         allocs ON sorder.trans_type=".ST_SALESORDER." AND allocs.trans_no_to=sorder.order_no
@@ -515,8 +516,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
@@ -533,13 +533,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')
@@ -596,3 +596,13 @@ function is_sales_order_started($order_no)
        $result = db_fetch(db_query($sql, "cannot retrieve sales invoices for sales order"));
        return $result[0];
 }
+
+//---------------------------------------------------------------------------------------------
+//
+// 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");
+}