|
1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package openbuildings\shipping |
|
5
|
|
|
* @author Ivan Kerin <[email protected]> |
|
6
|
|
|
* @copyright (c) 2013 OpenBuildings Ltd. |
|
7
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
|
8
|
|
|
*/ |
|
9
|
|
|
class Kohana_Model_Shipping_Group extends Jam_Model { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @codeCoverageIgnore |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function initialize(Jam_Meta $meta) |
|
15
|
|
|
{ |
|
16
|
|
|
$meta |
|
17
|
|
|
->behaviors(array( |
|
18
|
|
|
'paranoid' => Jam::behavior('paranoid'), |
|
19
|
|
|
)) |
|
20
|
|
|
->associations(array( |
|
21
|
|
|
'shipping' => Jam::association('belongsto', array('inverse_of' => 'locations')), |
|
22
|
|
|
'method' => Jam::association('belongsto', array('foreign_model' => 'shipping_method', 'inverse_of' => 'locations')), |
|
23
|
|
|
'location' => Jam::association('belongsto', array('inverse_of' => 'shipping_group')), |
|
24
|
|
|
'shipping_items' => Jam::association('hasmany', array('inverse_of' => 'shipping_group')), |
|
25
|
|
|
)) |
|
26
|
|
|
->fields(array( |
|
27
|
|
|
'id' => Jam::field('primary'), |
|
28
|
|
|
'price' => Jam::field('price'), |
|
29
|
|
|
'delivery_time' => Jam::field('range', array('format' => 'Model_Shipping::format_shipping_time')), |
|
30
|
|
|
'additional_item_price' => Jam::field('price', array('convert_empty' => FALSE)), |
|
31
|
|
|
'discount_threshold' => Jam::field('price'), |
|
32
|
|
|
)) |
|
33
|
|
|
->validator('shipping', 'location', 'method', 'delivery_time', array('present' => TRUE)) |
|
34
|
|
|
->validator('price', array('present' => array('allow_zero' => TRUE))) |
|
35
|
|
|
->validator('additional_item_price', 'discount_threshold', 'price', array('price' => array('greater_than_or_equal_to' => 0))) |
|
36
|
|
|
->validator('delivery_time', array('range' => array('consecutive' => TRUE, 'greater_than_or_equal_to' => 0))); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Sort Model_Shipping_Item by price, biggest price first |
|
41
|
|
|
* @param array $items |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public static function sort_by_price(array $items) |
|
45
|
|
|
{ |
|
46
|
3 |
|
Array_Util::validate_instance_of($items, 'Model_Shipping_Group'); |
|
47
|
|
|
|
|
48
|
3 |
|
usort($items, function($item1, $item2){ |
|
49
|
3 |
|
return $item1->price->is(Jam_Price::GREATER_THAN, $item2->price) ? -1 : 1; |
|
50
|
3 |
|
}); |
|
51
|
|
|
|
|
52
|
3 |
|
return $items; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function total_delivery_time() |
|
56
|
|
|
{ |
|
57
|
2 |
|
return $this->delivery_time; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get the currency for pricing calculations |
|
62
|
|
|
* @return string |
|
63
|
|
|
* @throws Kohana_Exception If brand_purchase_shipping is NULL |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function currency() |
|
66
|
|
|
{ |
|
67
|
2 |
|
return $this->get_insist('shipping')->currency(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return TRUE if total is bigger than discount_threshold |
|
72
|
|
|
* @return boolean |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function is_discounted(Jam_Price $total) |
|
75
|
|
|
{ |
|
76
|
2 |
|
return ($this->discount_threshold AND $total->is(Jam_Price::GREATER_THAN, $this->discount_threshold)); |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.