Add Splitter. no test yet
authorMaxime Bourget <bmx007@gmail.com>
Fri, 7 Jun 2013 21:16:21 +0000 (22:16 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Fri, 7 Jun 2013 21:16:21 +0000 (22:16 +0100)
includes/order_lines.inc
includes/splitter.inc [new file with mode: 0644]
order_lines_view.php

index e8d161224d3e21a135abf973441c58df0a79a35d..8c0373b39f8895d096d4bd215763952e23fe06cb 100644 (file)
@@ -28,7 +28,6 @@ print_r('cacou');
        }
 }
 
-
 function update_extra_order_details() {
        if(!isset($_POST['Update']) || $_POST['Update'] != 'Update')  return;
 
@@ -68,6 +67,14 @@ commit_transaction();
 
 }
 
+function split_order_details() {
+       if(!isset($_POST['Split']) || $_POST['Split'] != 'Split')  return;
+       $splitter = new Splitter($_POST);
+       begin_transaction();
+               $splitter->splitAll();
+       commit_transaction();
+}
+
 function compute_input_name($row, $field) {
        $row_id = $row['id'];
        return "detail[$row_id][$field]";
diff --git a/includes/splitter.inc b/includes/splitter.inc
new file mode 100644 (file)
index 0000000..8ff4640
--- /dev/null
@@ -0,0 +1,93 @@
+<?php
+require_once($path_to_root.'/'.'includes/date_functions.inc');
+class Split {
+       public $stard_date;
+       public $end_date;
+
+       function __constructor($start_date, $period) {
+                       $this->start_date = $this->end_date = $start_date;
+                       $this->extend($period);
+       }
+
+       function extend($days) {
+                       $this->end_date = add_days($this->end_date, $days);
+       }
+}
+class Splitter {
+       public $detail_ids = array() ;
+       public $start_date;
+       public $end_date;
+       protected $days;
+       public $quantity_max;
+
+       public function __construct(array $data) {
+               foreach($data['detail'] as $detail_id => $detail) {
+                       array_push($this->details_id, $detail_id);
+               }
+               $start_date = $data['start_date'];
+               $end_date = $data['end_date'];
+               $days = date_diff2($start_date, $end_date, 'd');
+       }
+
+       protected function loadDetail($detail_id) {
+               $sql = "SELECT *
+                                               FROM ".TB_PREF."sales_order_details
+                                               WHERE id = $detail_id";
+               $result = db_query($sql);
+               return db_fetch($result);
+       }
+
+       public function splitAll() {
+               foreach($this->detail_ids as $detail_id) {
+                               $detail = $this->loadDetail($detail_id);
+                               $splits = $this->split($detail);
+                               $this->saveSplits($detail, $splits);
+               }
+       }
+
+
+       
+       /* This function splits on order detail in bits of a specified size.
+ * Each split  starting at the end time of the previous one.
+ * the first split starts at the start_date and ends at the end_date.
+ * The 'splitting' split the whole quantity not only the quantity left to dispatch
+ * However, fully dispatched split won't be split in real, but merged with the next one
+ */
+       protected function split($row) {
+               $splits = array();
+               $quantity = $row['quantity'];
+               $quantity_sent = $row['qty_sent'];
+
+               if($quantity >= $quantity_sent) return;
+               if(($quantity-$quantity_sent) % $this->max_quantity < $this->max_quantity) return;
+               
+               // determine the number of split needed. This will give us the lenght of a split.
+               $nsplit = ceiling($initial_quantity/$this->max_quantity);
+               if($nsplit == 1) return;
+
+               $period = $this->days/$nsplit;
+
+               array_push($splits, $split = new Split($start_date, $period));
+               $split_quantity = min($quantity, $this->max_quantity);
+               while($split_quantity > 0) {
+                                       $split->quantity += $split_quantity;
+                               // Check if the split has been entirely dispatch or not.
+                               if($quantity_dispatched > $split->quantity) {
+                                       //extend the split
+                                       $split->extend($period);
+                               }
+                               else {
+                                       // create a new split
+                                       array_push($splits, $split = new Split($split->end_date, $period));
+                                       $quantity_dispatched = 0; // we don't need to check anymore.
+                                       
+                               }
+                               $quantity -= $split_quantity;
+                               $split_quantity = min($quantity, $this->max_quantity);
+               }
+
+               return $splits;
+       }
+
+}
+?>
index 317ca2bee4c9e36800a3b978c671e2cae48f2ad8..362b3f0112aa1b2716d4128508de09f3918f3109 100644 (file)
@@ -23,7 +23,9 @@ add_access_extensions();
 
 $_SESSION['page_title'] = _($help_context = "Edit lines extra parameters");
 
+// Process POST
 update_extra_order_details();
+split_order_details();
 
 
 $js = "";