class Split {
public $start_date;
public $end_date;
+ public $quantity=0;
+ public $quantity_dispatched=0;
+
function __construct($start_date, $period=null) {
$this->start_date = $start_date;
$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 $max_quantity;
public function __construct(array $data) {
$this->details_id = array();
$this->start_date = $data['start_date'];
$this->end_date = $data['end_date'];
$this->days = date_diff2($this->end_date, $this->start_date, 'd');
+
+ $this->max_quantity = $data['max_quantity'];
}
protected function loadDetail($detail_id) {
* 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) {
+ public function split($row) {
$splits = array();
$quantity = $row['quantity'];
$quantity_sent = $row['qty_sent'];
- if($quantity >= $quantity_sent) return;
+ if($quantity >= $quantity_sent) return $splits;
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;
+ if($nsplit == 1) return $splits;
$period = $this->days/$nsplit;
array_push($splits, $split = new Split($start_date, $period));
+ $split->quantity_dispatched = $quantity_dispatched;
$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) {
+ if($split->$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;
public function testConstruct() {
$start_date = '2013/05/01';
$end_date = '2013/05/31';
+ $max_quantity = 6;
$details = array('1' => 'details 1', '2' => 'details', '3' => 'd3');
$splitter = new Splitter(array('detail' => $details
- , 'start_date'=> $start_date
- , 'end_date' => $end_date));
+ ,'start_date'=> $start_date
+ ,'end_date' => $end_date
+ ,'max_quantity' => $max_quantity));
$this->assertEquals($start_date, $splitter->start_date);
$this->assertEquals($end_date, $splitter->end_date);
+ $this->assertEquals($max_quantity, $splitter->max_quantity);
$this->assertEquals($splitter->days(), 30);
+ return $splitter;
+ }
+
+ /**
+ * @depends testConstruct
+ */
+ public function testSplit($splitter) {
+ $this->assertSplit($splitter, 5, 0, array());
+ $this->assertSplit($splitter, 15, 11, array());
+ $this->assertSplit($splitter, 30, 24, array());
+ $this->assertSplit($splitter, 10, 0
+ ,array("6 0 2013/05/01 2013/05/16"
+ ,"4 0 2013/05/16 2013/05/31"));
+ $this->assertSplit($splitter, 18, 10
+ ,array(" 12 10 2013/05/01 2013/05/21"
+ ,"6 0 2013/05/21 2013/05/31"));
+ }
+
+ public function assertSplit(Splitter $splitter, $quantity, $quantity_dispatched, array $expected_splits) {
+ $splits = $splitter->split(array('quantity' => $quantity, 'qty_sent' => $quantity_dispatched));
+
+ if(empty($expected_splits)) return $this->assertEmpty($splits);
+ foreach(array_combine($expected_splits, $splits) as $ex => $split) {
+ $this->assertEquals(explode(" ", $ex), array($split->quantity
+ ,$split->quantity_dispatched
+ ,$split->start_date
+ ,$split->end_date));
+
+ }
}
}
?>