BulkUpdater work.
[order_line_extra.git] / includes / order_lines.inc
index d17022263c86f66a4052aa8485eb25f989999b91..00c1193b67220be696c5f020cf83b80bc1512526 100644 (file)
@@ -23,46 +23,100 @@ function extract_cell($td) {
                return $matches[1];
        }
        else {
-print_r('cacou');
                return $td;
        }
 }
 
+/* remove unchecked data so each
+ * funcion doesn't to check what's been checked or not. 
+ */
+function filter_data(&$data) {
+       if(!isset($data['detail'])) return;
+       $new_details = array();
+       foreach(@$data['detail'] as $detail_id => $values) {
+               if(@$values['check'] === 'on') $new_details[$detail_id]=$values;
+       }
+       $data['detail'] = $new_details;
+}
+
+function post_to_detail_ids() {
+       if(!isset($_POST['detail'])) return;
+       $detail_ids = array();
+       foreach($_POST['detail'] as $detail_id => $values) {
+               array_push($detail_ids, $detail_id);
+       }
+
+       return $detail_ids;
+}
 
 function update_extra_order_details() {
        if(!isset($_POST['Update']) || $_POST['Update'] != 'Update')  return;
 
+       $cart = post_to_detail_ids();
+       if($cart === null) return;
 begin_transaction();
+       hook_db_prewrite($cart, 'order_xtra');
        // update in database all field
+       $date_fields = array('hold_until_date', 'required_date', 'expiry_date', 'priority');
        foreach($_POST['detail'] as $detail_id => $values) {
                $set = array();
-               if($comment = $values['comment'])  array_push($set, " comment='$comment' ");
-               if(isset($values['required_date'])) {
-                       $required_date = trim($values['required_date']);
-                       if($required_date == "")  {
-                               array_push($set," required_date = NULL");
-                       } else {
-                               $date = date2sql($required_date);
-                               array_push($set," required_date='$date' ");
+               $comment=$values['comment'];
+         array_push($set, " comment='$comment' "); // erase comment if needed.
+               foreach($date_fields as $date_field) {
+                       if(isset($values[$date_field])) {
+                               $input_date = trim($values[$date_field]);
+                               if($input_date == "")  {
+                                       // Only the priority field can be set to null.
+                                       if($date_field == 'priority') array_push($set," $date_field = NULL");
+                               } else {
+                                       $date = date2sql($input_date);
+                                       array_push($set," $date_field='$date' ");
+                               }
                        }
                }
 
                if(!empty($set))  {
                        $sql = "UPDATE ".TB_PREF."sales_order_details  SET ".implode($set,', ')."  WHERE id = $detail_id ";
-                       
+
                        if(!db_query($sql)) {
                                display_error('Problem while updating order details. Try again');
                                cancel_transaction();
                                return;
                        }
                }
-       
        }
+       hook_db_postwrite($cart, 'order_xtra');
+       commit_transaction();
+
 
        display_notification('Order details updated');
 
 }
 
+function split_order_details() {
+       if(!isset($_POST['Split']) || $_POST['Split'] != 'Split')  return;
+       $splitter = new Splitter($_POST);
+       begin_transaction();
+       $cart = post_to_detail_ids();
+       if($cart === null) return;
+       hook_db_prewrite($cart, 'order_xtra');
+               if($splitter->splitAll()) display_warning("Items have been split.");
+       hook_db_postwrite($cart, 'order_xtra');
+       commit_transaction();
+}
+
+function bulk_update_order_details() {
+       if(!isset($_POST['Bulk']) || $_POST['Bulk'] != 'Bulk')  return;
+       $bulk_updater = new BulkUpdater($_POST);
+       begin_transaction();
+       $cart = post_to_detail_ids();
+       if($cart === null) return;
+       hook_db_prewrite($cart, 'order_xtra');
+       $bulk_updater->update();
+       hook_db_postwrite($cart, 'order_xtra');
+       commit_transaction();
+}
+
 function compute_input_name($row, $field) {
        $row_id = $row['id'];
        return "detail[$row_id][$field]";
@@ -75,13 +129,13 @@ function view_link($dummy, $order_no)
 
 function item_link($dummy, $stock_id)
 {
-  return pager_link( _($stock_id),
+       return pager_link( _($stock_id),
        "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
 }
 
 function order_link($row)
 {
-  return pager_link( _("Sales Order"),
+       return pager_link( _("Sales Order"),
        "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
 }
 
@@ -91,7 +145,7 @@ function customer_link($row) {
 
 function customer_link2($name, $id) {
        return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
-       
+
 }
 
 function aggregate_comment($row) {
@@ -119,9 +173,9 @@ function available_quantity($row, $available) {
        return "<span class='$class'>$available<span>";
 }
 
-function input_date_details($row, $date) {
+function input_date_details($row, $field_name, $date) {
 $row_id = $row['id'];
-       $name = compute_input_name($row, 'required_date');
+       $name = compute_input_name($row, $field_name);
 
 $_POST[$name] = sql2date($date);
 
@@ -135,6 +189,22 @@ $_POST[$name] = sql2date($date);
 ";
 }
 
+function input_priority_date_details($row, $date) {
+       return input_date_details($row, 'priority', $date);
+}
+
+function input_hold_until_date_details($row, $date) {
+       return input_date_details($row, 'hold_until_date', $date);
+}
+
+function input_required_date_details($row, $date) {
+       return input_date_details($row, 'required_date', $date);
+}
+
+function input_expiry_date_details($row, $date) {
+       return input_date_details($row, 'expiry_date', $date);
+}
+
 function input_comment_details($row, $comment) {
 $row_id = $row['id'];
        return "
@@ -142,7 +212,13 @@ $row_id = $row['id'];
 ";
 }
 
-function get_order_details_extra($customer_id, $location) {
+function input_check_details($row, $dummy) {
+       $row_id = $row['id'];
+               return "
+               <input type='checkbox' class='check_detail' name='detail[$row_id][check]'>";
+}
+
+function get_order_details_extra($customer_id, $location, $item_like) {
        $sql = "SELECT sod.id
        , so.order_no
        , stk_code
@@ -165,6 +241,15 @@ function get_order_details_extra($customer_id, $location) {
        WHERE sod.quantity > qty_sent
        ";
 
+       if($item_like) {
+                       if($item_like[0]=='/')  {
+                               $regexp = substr($item_like, 1);
+                               $sql .= " AND stk_code RLIKE '$regexp'";
+                       }
+                       else 
+                               $sql .= " AND stk_code LIKE '$item_like'";
+       }
+
        return $sql;
 }