From 738878f7c754fa7a4532df2b031512995ebf3af0 Mon Sep 17 00:00:00 2001 From: Maxime Bourget Date: Fri, 7 Jun 2013 22:16:21 +0100 Subject: [PATCH] Add Splitter. no test yet --- includes/order_lines.inc | 9 +++- includes/splitter.inc | 93 ++++++++++++++++++++++++++++++++++++++++ order_lines_view.php | 2 + 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 includes/splitter.inc diff --git a/includes/order_lines.inc b/includes/order_lines.inc index e8d1612..8c0373b 100644 --- a/includes/order_lines.inc +++ b/includes/order_lines.inc @@ -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 index 0000000..8ff4640 --- /dev/null +++ b/includes/splitter.inc @@ -0,0 +1,93 @@ +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; + } + +} +?> diff --git a/order_lines_view.php b/order_lines_view.php index 317ca2b..362b3f0 100644 --- a/order_lines_view.php +++ b/order_lines_view.php @@ -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 = ""; -- 2.30.2