/
Purchase.php
360 lines (306 loc) · 8.65 KB
/
Purchase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php defined('SYSPATH') OR die('No direct script access.');
use OpenBuildings\Monetary\Monetary;
use Clippings\Freezable\FreezableTrait;
use Clippings\Freezable\FreezableInterface;
/**
* @package Openbuildings\Purchases
* @author Ivan Kerin <ikerin@gmail.com>
* @copyright (c) 2013 OpenBuildings Ltd.
* @license http://spdx.org/licenses/BSD-3-Clause
*
* @property int|null $id
* @property string $currency
* @property string|null $monetary
* @property bool $is_frozen
* @property int|string|null $created_at
* @property int|string|null $updated_at
* @property Model_Brand_Purchase[] $brand_purchases
* @property Model_Brand[] $brands
* @property Model_User $creator
* @property Model_Address $billing_address
* @property Model_Payment $payment
*/
class Kohana_Model_Purchase extends Jam_Model implements Purchasable, FreezableInterface {
use FreezableTrait {
freeze as parentFreeze;
unfreeze as parentUnfreeze;
}
protected $_monetary;
/**
* @codeCoverageIgnore
*/
public static function initialize(Jam_Meta $meta)
{
$meta
->name_key('number')
->behaviors(array(
'tokenable' => Jam::behavior('tokenable', array('uppercase' => TRUE, 'field' => 'number')),
'paranoid' => Jam::behavior('paranoid'),
))
->associations(array(
'brand_purchases' => Jam::association('hasmany', array(
'inverse_of' => 'purchase',
'foreign_model' => 'brand_purchase',
'delete_on_remove' => Jam_Association::DELETE,
'dependent' => Jam_Association::DELETE,
)),
'brands' => Jam::association('manytomany', array(
'join_table' => 'brand_purchases',
'readonly' => TRUE,
)),
'creator' => Jam::association('creator', array('required' => FALSE)),
'billing_address' => Jam::association('belongsto', array(
'foreign_model' => 'address',
'dependent' => Jam_Association::DELETE,
)),
'payment' => Jam::association('hasone', array('inverse_of' => 'purchase', 'dependent' => Jam_Association::DELETE)),
))
->fields(array(
'id' => Jam::field('primary'),
'currency' => Jam::field('string'),
'monetary' => Jam::field('serialized'),
'is_frozen' => Jam::field('boolean'),
'created_at' => Jam::field('timestamp', array('auto_now_create' => TRUE, 'format' => 'Y-m-d H:i:s')),
'updated_at' => Jam::field('timestamp', array('auto_now_update' => TRUE, 'format' => 'Y-m-d H:i:s')),
))
->validator('currency', array('currency' => TRUE));
}
/**
* Iterate through the existing brand_purchases and return the one that is linked to this brand.
* If none exist build one and return it
*
* @param Model_Brand $brand
* @return Model_Brand_Purchase
*/
public function find_or_build_brand_purchase(Model_Brand $brand)
{
$brand_purchase = $this->find_brand_purchase($brand);
if ($brand_purchase)
{
return $brand_purchase;
}
return $this->brand_purchases->build(array('brand' => $brand));
}
/**
* Iterate through the existing brand_purchases and return the one that is linked to this brand.
* If none exist return NULL
*
* @param Model_Brand $brand
* @return Model_Brand_Purchase
*/
public function find_brand_purchase(Model_Brand $brand)
{
$brand_purchases = $this->brand_purchases->as_array('brand_id');
if (isset($brand_purchases[$brand->id()]))
{
return $brand_purchases[$brand->id()];
}
return NULL;
}
/**
* Add item to the brand_purchase that matches the brand given, if it exists, update the quantity, if the brand_purchase does not exist, build it.
*
* @param Model_Brand $brand
* @param Model_Purchase_Item $new_item
* @trigger model.add_item event passing $new_item
*/
public function add_item($brand, Model_Purchase_Item $new_item)
{
$brand_purchase = $this->find_or_build_brand_purchase($brand);
$brand_purchase->add_or_update_item($new_item);
$new_item->brand_purchase = $brand_purchase;
$this->meta()->events()->trigger('model.add_item', $this, array($new_item));
return $this;
}
public function remove_item($brand, Model_Purchase_Item $item)
{
$brand_purchase = $this->find_brand_purchase($brand);
if ($brand_purchase)
{
$brand_purchase->remove_item($item);
if (0 === $brand_purchase->items_count('product'))
{
$this->brand_purchases->remove($brand_purchase);
}
$this->meta()->events()->trigger('model.remove_item', $this, array($item));
}
return $this;
}
/**
* Return Monetary::instance() if not frozen.
* Freezable field.
*
* @return OpenBuildings\Monetary\Monetary
*/
public function monetary()
{
return $this->isFrozen() ? $this->monetary : Monetary::instance();
}
/**
* Return the currency for all the field in the purchase
*
* @return string
*/
public function currency()
{
return $this->currency;
}
/**
* The currency used in "humanizing" any of the price fields in the purchase
*
* @return string
*/
public function display_currency()
{
return $this->currency;
}
/**
* Return purchase_items, aggregated from all the brand_purchases. Can pass filters.
*
* @param array|string $types filters
* @return array Model_Purchase_Items
*/
public function items($types = NULL)
{
$items = array();
foreach ($this->brand_purchases->as_array() as $brand_purchase)
{
$items = array_merge($items, $brand_purchase->items($types));
}
return $items;
}
/**
* Return the sum purchase items count from all brand_purchases
*
* @param array|string $types filters
* @return integer
*/
public function items_count($types = NULL)
{
return count($this->items($types));
}
/**
* Return the sum of the quantities of all the purchase_items
* @param array|string $types filters
* @return integer
*/
public function items_quantity($types = NULL)
{
$quantities = array_map(function($item) {
return $item->quantity;
}, $this->items($types));
return $quantities ? array_sum($quantities) : 0;
}
/**
* Run update items on all the brand_purchases
*
* @return Model_Purchase $this
*/
public function update_items()
{
foreach ($this->brand_purchases->as_array() as $brand_purchase)
{
$brand_purchase->update_items();
}
return $this;
}
/**
* Replace the purchase items from a given type, removing old items
*
* @param array $items array of new items
* @param array|string $types filters
* @return Model_Purchase $this
*/
public function replace_items($items, $types = NULL)
{
$grouped = Model_Purchase_Item::group_by_brand_purchase($items);
$current = $this->brand_purchases->as_array('id');
$replaced = array_intersect_key($grouped, $current);
$removed = array_diff_key($current, $grouped);
foreach ($replaced as $index => $items)
{
$current[$index]->replace_items($items, $types);
}
$this->brand_purchases->remove(array_values($removed));
return $this;
}
/**
* Return the sum of all the prices from the purchase items
*
* @param array|string $types filters
* @return Jam_Price
*/
public function total_price($types = NULL)
{
$prices = array_map(function($item) { return $item->total_price(); }, $this->items($types));
return Jam_Price::sum($prices, $this->currency(), $this->monetary(), $this->display_currency());
}
/**
* Return TRUE if there is a payment and its status is "paid"
*
* @return boolean
*/
public function is_paid()
{
return ($this->payment AND $this->payment->status === Model_Payment::PAID);
}
/**
* Return the date when the payment has been created
*
* @return string|NULL
*/
public function paid_at()
{
return $this->is_paid() ? $this->payment->created_at : NULL;
}
public function recheck()
{
$this->brand_purchases = array_map(function($item){
return $item->set('items', $item->items);
}, $this->brand_purchases->as_array());
return $this->check();
}
public function freeze()
{
$this->parentFreeze();
$this->freezeCollection();
return $this;
}
public function unfreeze()
{
$this->parentUnfreeze();
$this->unfreezeCollection();
return $this;
}
public function isFrozen()
{
return $this->is_frozen;
}
protected function setFrozen($frozen)
{
$this->is_frozen = (bool) $frozen;
return $this;
}
public function performFreeze()
{
$this->monetary = $this->monetary();
}
public function performUnfreeze()
{
$this->monetary = NULL;
}
public function freezeCollection()
{
foreach ($this->brand_purchases->as_array() as $brand_purchase)
{
$brand_purchase->freeze();
}
}
public function unfreezeCollection()
{
foreach ($this->brand_purchases->as_array() as $brand_purchase)
{
$brand_purchase->unfreeze();
}
}
}