1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This is a base |
5
|
|
|
* |
6
|
|
|
* @package openbuildings\promotions |
7
|
|
|
* @author Ivan Kerin <[email protected]> |
8
|
|
|
* @author Yasen Yanev <[email protected]> |
9
|
|
|
* @copyright (c) 2013 OpenBuildings Ltd. |
10
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
11
|
|
|
*/ |
12
|
|
|
class Kohana_Model_Promotion extends Jam_Model implements Sellable { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @codeCoverageIgnore |
16
|
|
|
*/ |
17
|
|
|
public static function initialize(Jam_Meta $meta) |
18
|
|
|
{ |
19
|
|
|
$meta |
|
|
|
|
20
|
|
|
->unique_key(function($key){ |
|
|
|
|
21
|
|
|
return is_numeric($key) ? 'id' : 'identifier'; |
22
|
|
|
}) |
23
|
|
|
->table('promotions') |
24
|
|
|
->associations(array( |
25
|
|
|
'purchase_items' => Jam::association('hasmany', array( |
26
|
|
|
'as' => 'reference', |
27
|
|
|
'foreign_model' => 'purchase_item_promotion', |
28
|
|
|
)), |
29
|
|
|
'promo_codes' => Jam::association('hasmany', array( |
30
|
|
|
'inverse_of' => 'promotion', |
31
|
|
|
)) |
32
|
|
|
)) |
33
|
|
|
->fields(array( |
34
|
|
|
'id' => Jam::field('primary'), |
35
|
|
|
'name' => Jam::field('string'), |
36
|
|
|
'currency' => Jam::field('string'), |
37
|
|
|
'identifier' => Jam::field('string'), |
38
|
|
|
'description' => Jam::field('text'), |
39
|
|
|
'priority' => Jam::field('integer', array('default' => 1)), |
40
|
|
|
'model' => Jam::field('polymorphic'), |
41
|
|
|
'created_at' => Jam::field('timestamp', array( |
42
|
|
|
'format' => 'Y-m-d H:i:s', |
43
|
|
|
'auto_now_create' => TRUE, |
44
|
|
|
)), |
45
|
|
|
'expires_at' => Jam::field('timestamp', array( |
46
|
|
|
'format' => 'Y-m-d H:i:s', |
47
|
|
|
)), |
48
|
|
|
)) |
49
|
|
|
->validator('name', array('present' => TRUE)) |
50
|
|
|
->validator('currency', array('currency' => TRUE)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @codeCoverageIgnore |
55
|
|
|
*/ |
56
|
|
|
public function price_for_purchase_item(Model_Purchase_Item $purchase_item) |
57
|
|
|
{ |
58
|
|
|
throw new Kohana_Exception('Not a valid promotion'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @codeCoverageIgnore |
63
|
|
|
*/ |
64
|
|
|
public function applies_to(Model_Brand_Purchase $purchase) |
65
|
|
|
{ |
66
|
|
|
throw new Kohana_Exception('Not a valid promotion'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Build a purchase item for this promotion (inverseof "reference") |
71
|
|
|
* @return Model_Purchase_Item |
72
|
|
|
*/ |
73
|
1 |
|
public function build_purchase_item() |
74
|
|
|
{ |
75
|
1 |
|
return $this->purchase_items->build(array('model' => 'purchase_item_promotion')); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function currency() |
79
|
|
|
{ |
80
|
1 |
|
return $this->currency; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
public function is_expired() |
84
|
|
|
{ |
85
|
3 |
|
return ($this->expires_at !== NULL AND strtotime($this->expires_at) < time()); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* If the promotion applies to the brand_purchase - add a purchase_item for this promotion, otherwise remove the associated purchase item |
90
|
|
|
* @param Model_Brand_Purchase $brand_purchase |
|
|
|
|
91
|
|
|
*/ |
92
|
1 |
|
public function update_brand_purchase_items($applies, & $items) |
93
|
|
|
{ |
94
|
1 |
|
$promo_item = Jam::build('purchase_item_promotion', array('reference' => $this)); |
95
|
|
|
|
96
|
1 |
|
$has_existing_promotion = false; |
97
|
1 |
|
foreach ($items as $index => $item){ |
98
|
1 |
|
if ($item->is_same($promo_item)) { |
99
|
1 |
|
$has_existing_promotion = true; |
100
|
1 |
|
if ( ! $applies) { |
101
|
1 |
|
unset($items[$index]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
if ($applies && ! $has_existing_promotion) { |
107
|
1 |
|
$items[] = $promo_item; |
108
|
|
|
} |
109
|
1 |
|
} |
110
|
|
|
} |
111
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.