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_Jam_Behavior_Shippable_Purchase extends Jam_Behavior { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @codeCoverageIgnore |
13
|
|
|
*/ |
14
|
|
|
public function initialize(Jam_Meta $meta, $name) |
15
|
|
|
{ |
16
|
|
|
parent::initialize($meta, $name); |
17
|
|
|
|
18
|
|
|
$meta |
19
|
|
|
->associations(array( |
20
|
|
|
'shipping_address' => Jam::association('belongsto', array( |
21
|
|
|
'foreign_model' => 'address', |
22
|
|
|
'dependent' => Jam_Association::DELETE, |
23
|
|
|
)), |
24
|
|
|
)) |
25
|
|
|
->fields(array( |
26
|
|
|
'shipping_same_as_billing' => Jam::field('boolean', array('default' => TRUE)), |
27
|
|
|
'shipping_required' => Jam::field('boolean', array('in_db' => FALSE)), |
28
|
|
|
)) |
29
|
|
|
->events() |
30
|
|
|
->bind('model.add_item', array($this, 'add_item')); |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
public function model_before_check(Model_Purchase $purchase, Jam_Event_Data $data) |
|
|
|
|
34
|
|
|
{ |
35
|
2 |
|
if ($purchase->shipping_required) |
36
|
|
|
{ |
37
|
1 |
|
if ($purchase->shipping_same_as_billing AND ! $purchase->billing_address) |
38
|
|
|
{ |
39
|
1 |
|
$purchase->errors()->add('billing_address', 'present'); |
40
|
|
|
} |
41
|
1 |
|
elseif ( ! $purchase->shipping_same_as_billing AND ! $purchase->shipping_address) |
42
|
|
|
{ |
43
|
1 |
|
$purchase->errors()->add('shipping_address', 'present'); |
44
|
|
|
} |
45
|
|
|
else |
46
|
|
|
{ |
47
|
1 |
|
$purchase->shipping_address()->fields_required = TRUE; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
if ($purchase->items_count(array('can_ship' => FALSE))) |
51
|
|
|
{ |
52
|
1 |
|
$purchase->errors()->add('brand_purchases', 'cannot_ship'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
2 |
|
} |
56
|
|
|
|
57
|
2 |
|
public function add_item(Model_Purchase $purchase, Jam_Event_Data $data, Model_Purchase_Item $purchase_item) |
58
|
|
|
{ |
59
|
2 |
|
if (($brand_purchase = $purchase_item->brand_purchase) AND $purchase->shipping_country()) |
60
|
|
|
{ |
61
|
2 |
|
if ( ! $brand_purchase->shipping) |
62
|
|
|
{ |
63
|
2 |
|
$brand_purchase->build('shipping'); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$brand_purchase->shipping->build_item_from($purchase_item); |
67
|
|
|
} |
68
|
2 |
|
} |
69
|
|
|
|
70
|
2 |
|
public function model_call_shipping_country(Model_Purchase $purchase, Jam_Event_Data $data, Model_Location $shipping_country = NULL) |
71
|
|
|
{ |
72
|
2 |
|
if ($shipping_country !== NULL) |
73
|
|
|
{ |
74
|
2 |
|
if ($purchase->shipping_same_as_billing) |
75
|
|
|
{ |
76
|
2 |
|
$purchase->billing_address->country = $shipping_country; |
77
|
2 |
|
$purchase->billing_address = $purchase->billing_address; |
78
|
|
|
} |
79
|
|
|
else |
80
|
|
|
{ |
81
|
1 |
|
$purchase->shipping_address->country = $shipping_country; |
82
|
1 |
|
$purchase->shipping_address = $purchase->shipping_address; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$data->return = $purchase; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
$address = $purchase->shipping_address(); |
89
|
|
|
|
90
|
2 |
|
if ($address AND $address->country) |
91
|
|
|
{ |
92
|
2 |
|
$data->return = $address->country; |
93
|
|
|
} |
94
|
2 |
|
} |
95
|
|
|
|
96
|
2 |
|
public function model_call_shipping_address(Model_Purchase $purchase, Jam_Event_Data $data) |
97
|
|
|
{ |
98
|
2 |
|
$data->return = $purchase->shipping_same_as_billing ? $purchase->billing_address : $purchase->shipping_address; |
99
|
2 |
|
} |
100
|
|
|
} |
101
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.