Skip to content

Commit

Permalink
add delivery_time calculations, make them freezable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Kerin committed Sep 18, 2013
1 parent d266c7d commit 62a14cf
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 25 deletions.
3 changes: 3 additions & 0 deletions classes/Kohana/Jam/Behavior/Shippable/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function initialize(Jam_Meta $meta, $name)
->events()
->bind('model.update_items', array($this, 'update_shipping_items'))
->bind('model.filter_items', array($this, 'filter_shipping_items'));

$behaviors = $meta->behaviors();
$behaviors['freezable']->_associations[] = 'shipping';
}

public function model_call_items_by_shipping_method(Model_Store_Purchase $store_purchase, Jam_Event_Data $data)
Expand Down
41 changes: 41 additions & 0 deletions classes/Kohana/Model/Shipping/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ class Kohana_Model_Shipping_Item extends Jam_Model {
public static function initialize(Jam_Meta $meta)
{
$meta
->behaviors(array(
'freezable' => Jam::behavior('freezable', array('fields' => 'total_delivery_time', 'parent' => 'store_purchase_shipping')),
))
->associations(array(
'store_purchase_shipping' => Jam::association('belongsto', array('inverse_of' => 'items')),
'purchase_item' => Jam::association('belongsto'),
'shipping_group' => Jam::association('belongsto', array('inverse_of' => 'shipping_items')),
))
->fields(array(
'id' => Jam::field('primary'),
'total_delivery_time' => Jam::field('range'),
))
->validator('purchase_item', 'shipping_group', array('present' => TRUE));
}
Expand Down Expand Up @@ -236,6 +240,43 @@ public function total_price()
return $this->price()->add($additional_items_price);
}

/**
* Shipping group's delivery_time
* @return Jam_Range
*/
public function delivery_time()
{
return $this->get_insist('shipping_group')->delivery_time;
}

/**
* Shipping's processing_time
* @return Jam_Range
*/
public function processing_time()
{
return $this->shipping_insist()->processing_time;
}

/**
* Return the delivary time min / max days - summed processing and delivery times
* Freezable
* @return Jam_Range
*/
public function total_delivery_time()
{
if ($this->delivery_time)
{
$total_delivery_time = $this->delivery_time;
}
else
{
$total_delivery_time = Jam_Range::sum(array($this->delivery_time(), $this->processing_time()));
}

return $total_delivery_time;
}

/**
* Return additional_items_price() multiplied by quantity()
* @return Jam_Price
Expand Down
7 changes: 6 additions & 1 deletion classes/Kohana/Model/Shipping/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public static function initialize(Jam_Meta $meta)
{
$meta
->associations(array(
'shipping' => Jam::association('belongsto', array('inverse_of' => 'methods')),
'store' => Jam::association('belongsto', array('inverse_of' => 'methods')),
'shippings' => Jam::association('manytomany', array(
'foreign_key' => 'method_id',
'join_table' => 'shipping_groups',
'readonly' => TRUE,
)),
'shipping_groups' => Jam::association('hasmany', array('inverse_of' => 'method')),
))
->fields(array(
Expand Down
17 changes: 17 additions & 0 deletions classes/Kohana/Model/Store/Purchase/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class Kohana_Model_Store_Purchase_Shipping extends Jam_Model implements Sellable
public static function initialize(Jam_Meta $meta)
{
$meta
->behaviors(array(
'freezable' => Jam::behavior('freezable', array('children' => 'items', 'parent' => 'store_purchase')),
))
->associations(array(
'store_purchase' => Jam::association('belongsto'),
'location' => Jam::association('belongsto'),
Expand All @@ -39,6 +42,20 @@ public function price(Model_Purchase_Item $item)
return Model_Shipping_Item::compute_price($items, $total_price);
}

/**
* Get the merge of all total_delivery_time ranges from the items
* By getting the maximum min and max amounts.
* @return Jam_Range
*/
public function total_delivery_time()
{
$times = array_map(function($item){
return $item->total_delivery_time();
}, $this->items->as_array());

return Jam_Range::merge($times);
}

/**
* Total price for the purchased items
* @throws Kohana_Exception If store_purchase is NULL
Expand Down
33 changes: 15 additions & 18 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions tests/test_data/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ DROP TABLE IF EXISTS `shipping_methods`;
CREATE TABLE `shipping_methods` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`shipping_id` int(11) UNSIGNED NOT NULL,
`store_id` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `store_purchase_shippings`;
CREATE TABLE `store_purchase_shippings` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`store_purchase_shipping_id` int(11) UNSIGNED NOT NULL,
`location_id` int(11) UNSIGNED NOT NULL,
`store_purchase_id` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Expand All @@ -180,6 +180,7 @@ CREATE TABLE `shipping_items` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`purchase_item_id` int(11) UNSIGNED NOT NULL,
`shipping_group_id` int(11) UNSIGNED NOT NULL,
`delivery_time` varchar(100) NOT NULL,
`store_purchase_shipping_id` int(11) UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Expand Down Expand Up @@ -283,11 +284,11 @@ VALUES
(2,'Green',298.90,1);


INSERT INTO `shipping_methods` (`id`, `name`, `shipping_id`)
INSERT INTO `shipping_methods` (`id`, `name`, `store_id`)
VALUES
(1,'Post', 0),
(2,'Courier', 0),
(3,'Custom', 1);
(3,'Custom', 0);


INSERT INTO `shippings` (`id`, `name`, `currency`, `processing_time`, `ships_from_id`, `store_id`)
Expand All @@ -306,9 +307,9 @@ VALUES
(5, '12.00', '2|4', 1, 1, 3),
(6, '5.00', '2|3', 2, 1, 3);

INSERT INTO `store_purchase_shippings` (`id`, `store_purchase_id`)
INSERT INTO `store_purchase_shippings` (`id`, `store_purchase_id`, `location_id`)
VALUES
(1, 1);
(1, 1, 3);

INSERT INTO `shipping_items` (`id`, `store_purchase_shipping_id`, `purchase_item_id`, `shipping_group_id`)
VALUES
Expand Down
58 changes: 58 additions & 0 deletions tests/tests/Model/Shipping/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,64 @@ public function test_price()
$this->assertEquals(new Jam_Price(7.5091987684914, 'EUR', $monetary), $price);
}

/**
* @covers Model_Shipping_Item::delivery_time
*/
public function test_delivery_time()
{
$range = new Jam_Range(array(10, 12));

$item = Jam::build('shipping_item', array(
'shipping_group' => array(
'delivery_time' => $range,
),
));

$this->assertEquals($range, $item->delivery_time());
}

/**
* @covers Model_Shipping_Item::processing_time
*/
public function test_processing_time()
{
$range = new Jam_Range(array(10, 12));

$item = Jam::build('shipping_item', array(
'shipping_group' => array(
'shipping' => array(
'processing_time' => $range,
)
),
));

$this->assertEquals($range, $item->processing_time());
}

/**
* @covers Model_Shipping_Item::total_delivery_time
*/
public function test_total_delivery_time()
{
$item = $this->getMock('Model_Shipping_Item', array('processing_time', 'delivery_time'), array('shipping_item'));

$item
->expects($this->once())
->method('processing_time')
->will($this->returnValue(new Jam_Range(array(10, 23))));

$item
->expects($this->once())
->method('delivery_time')
->will($this->returnValue(new Jam_Range(array(2, 13))));

$this->assertEquals(new Jam_Range(array(12, 36)), $item->total_delivery_time());

$item->delivery_time = new Jam_Range(array(4, 5));

$this->assertEquals(new Jam_Range(array(4, 5)), $item->total_delivery_time());
}

/**
* @covers Model_Shipping_Item::additional_item_price
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/tests/Model/Store/Purchase/ShippingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,30 @@ public function test_build_items_from_errors($wrong_object, $expected_exception_

$store_purchase_shipping->build_items_from($purchase_items, $post);
}

/**
* @covers Model_Store_Purchase_Shipping::total_delivery_time
*/
public function test_total_delivery_time()
{
$item1 = $this->getMock('Model_Shipping_Item', array('total_delivery_time'), array('shipping_item'));

$item1
->expects($this->once())
->method('total_delivery_time')
->will($this->returnValue(new Jam_Range(array(10, 23))));

$item2 = $this->getMock('Model_Shipping_Item', array('total_delivery_time'), array('shipping_item'));

$item2
->expects($this->once())
->method('total_delivery_time')
->will($this->returnValue(new Jam_Range(array(2, 34))));

$shipping = Jam::build('store_purchase_shipping', array(
'items' => array($item1, $item2),
));

$this->assertEquals(new Jam_Range(array(10, 34)), $shipping->total_delivery_time());
}
}

0 comments on commit 62a14cf

Please sign in to comment.