Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 239 lines (206 sloc) 6.862 kB
86c8e2d @masom Some more initial work and proof of concept
authored
1 <?php
42c36a9 @masom Added copyright notice
authored
2 /**
3 * Chowly Pick. Eat. Save!
5aa77bd @masom Updated license and readme
authored
4 *
5 * @copyright Copyright 2011, Martin Samson <pyrolian@gmail.com>
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
42c36a9 @masom Added copyright notice
authored
7 */
29b26bf @masom QA Pass
authored
8 namespace chowly\models;
74c41aa @masom ?
authored
9
29b26bf @masom QA Pass
authored
10 use chowly\models\Images;
3f022ad @masom More work on error handling
authored
11 use chowly\extensions\data\InventoryException;
12 use chowly\extensions\data\OfferException;
3fa578a @masom Fixes regression introduced
authored
13 use lithium\analysis\Logger;
0c1c709 @masom Some work on tests and refactoring of offers
authored
14
eed347d @masom Models are now pluralized.
authored
15 class Offers extends \chowly\extensions\data\Model{
74c41aa @masom ?
authored
16 protected static $_states = array('published'=>'published', 'unpublished'=>'unpublished');
79aed69 @masom Some form work
authored
17 public $validates = array(
18 'name' => array(
66cd40a @masom Shows validation errors
authored
19 array('notEmpty','message'=>'Please enter a name.')
79aed69 @masom Some form work
authored
20 ),
66cd40a @masom Shows validation errors
authored
21 'description' => array('notEmpty', 'message' => 'Please enter a description.'),
79aed69 @masom Some form work
authored
22 'state' => array(
23 array('inList', 'list' => array('published', 'unpublished'))
24 ),
25 'availability' => array(
26 array('numeric', 'message'=>'Please enter a number.'),
838bf32 @masom New tests for Offers
authored
27 array('inRange', 'upper'=>256,'lower'=>0,
66cd40a @masom Shows validation errors
authored
28 'message'=>'Please enter a number between 1 and 255.')
79aed69 @masom Some form work
authored
29 ),
30 'venue_id' => array(
31 array('notEmpty', 'message'=>'There is a relationship problem...')
32 ),
33 'cost' => array(
66cd40a @masom Shows validation errors
authored
34 array('numeric','message'=>'Must be a monetary amount (ex: 33.00).')
79aed69 @masom Some form work
authored
35 )
36 );
0c1c709 @masom Some work on tests and refactoring of offers
authored
37
9b9c82a @masom Database strategy found.
authored
38 protected $_schema = array(
838bf32 @masom New tests for Offers
authored
39 '_id' => array('type' => 'id'),
d54399a @masom Work on templates
authored
40 'template_id' => array('type' => 'id'),
801afb8 @masom Added expiry field
authored
41 'venue_id' => array('type'=>'id'), // The venue
29b26bf @masom QA Pass
authored
42 'state' => array('type'=>'string', 'default' => 'unpublished'),
2b823d1 @masom Added slugs url on the index page
authored
43 'name' => array('type' => 'string','null'=>false), // Name of the coupon
44 'slug' => array('type' => 'string'), //Url friendly name
801afb8 @masom Added expiry field
authored
45 'description'=>array('type'=>'string'), // Description (if any) of the coupon
8d567fb @masom Limitations underway
authored
46 'limitations' => array('type'=>'array', 'array'=>true),
801afb8 @masom Added expiry field
authored
47 'starts' => array('type'=>'date','null'=>false), // Publication start
48 'ends'=>array('type'=>'date','null'=>false), // Publication ends
49 'expires' => array('type'=>'date','null'=>false), //Expiry date of the offer
50 'availability' => array('type'=>'integer'), // Holds how many items are available
51 'inventoryCount' => array('type'=>'integer'), // Holds number of inventory items
52 'sold' => array('type'=>'integer'), // Holds how many where sold
9b9c82a @masom Database strategy found.
authored
53 'created'=>array('type'=>'date'),
801afb8 @masom Added expiry field
authored
54 'updated'=>array('type'=>'date')
9b9c82a @masom Database strategy found.
authored
55 );
0c1c709 @masom Some work on tests and refactoring of offers
authored
56
9b9c82a @masom Database strategy found.
authored
57 public static function states(){
74c41aa @masom ?
authored
58 return static::$_states;
9b9c82a @masom Database strategy found.
authored
59 }
0c1c709 @masom Some work on tests and refactoring of offers
authored
60
9b9c82a @masom Database strategy found.
authored
61 public static function defaultState(){
62 return 'unpublished';
63 }
79aed69 @masom Some form work
authored
64
ecccada @masom ...
authored
65 public static function current(){
0c1c709 @masom Some work on tests and refactoring of offers
authored
66
67 $date = new \MongoDate();
9b9c82a @masom Database strategy found.
authored
68 $conditions = array(
0c1c709 @masom Some work on tests and refactoring of offers
authored
69 'starts' => array('$lt' => $date),
70 'ends' => array('$gt' => $date),
2677d96 @masom Fixes creating new offers
authored
71 'state'=> 'published'
ecccada @masom ...
authored
72 );
0c1c709 @masom Some work on tests and refactoring of offers
authored
73
817a073 @masom Some work on styling and viewing a offer
authored
74 $order = array(
8b13b52 @masom Some work on sessions and users
authored
75 'ends'=>'ASC',
76 'availability' => 'DESC'
817a073 @masom Some work on styling and viewing a offer
authored
77 );
0c1c709 @masom Some work on tests and refactoring of offers
authored
78
817a073 @masom Some work on styling and viewing a offer
authored
79 return static::all(compact('conditions','order'));
ecccada @masom ...
authored
80 }
29b26bf @masom QA Pass
authored
81
2b823d1 @masom Added slugs url on the index page
authored
82 /**
83 * Rebuilds the system inventory count
84 * @throws UnexpectedValueException Thrown when the inventory collection is empty.
cb7ae71 @masom Fixes a rebuild bug
authored
85 * @return integer Possible values:
86 * - 0 : Inventory sold out
87 * - 1 - inf: Number of offers modified.
2b823d1 @masom Added slugs url on the index page
authored
88 */
dcc1846 @masom Some work on rebuilding inventory
authored
89 public static function rebuildInventory(){
eed347d @masom Models are now pluralized.
authored
90 Inventories::releaseExpired();
91 $availableInventory = Inventories::getAvailable();
1b9b90d @masom More fixes with the cart system. Added missing error template for pdf…
authored
92
29b26bf @masom QA Pass
authored
93 if (count($availableInventory) == 0){
94 if (Inventories::count() == 0){
cb7ae71 @masom Fixes a rebuild bug
authored
95 throw new \UnexpectedValueException('No more inventory.');
1b9b90d @masom More fixes with the cart system. Added missing error template for pdf…
authored
96 }
cb7ae71 @masom Fixes a rebuild bug
authored
97 //Update all items as no longer available.
98 $conditions = array('state' => 'published');
99 static::update(array('availability' => 0), $conditions);
100 return 0;
1b9b90d @masom More fixes with the cart system. Added missing error template for pdf…
authored
101 }
29b26bf @masom QA Pass
authored
102
dcc1846 @masom Some work on rebuilding inventory
authored
103 $offersInventory = array();
29b26bf @masom QA Pass
authored
104 foreach ($availableInventory as $inventory){
105 if (!isset($offersInventory[(string) $inventory->offer_id])){
106 $offersInventory[(string) $inventory->offer_id] = 0;
dcc1846 @masom Some work on rebuilding inventory
authored
107 }
29b26bf @masom QA Pass
authored
108 $offersInventory[(string) $inventory->offer_id] ++;
dcc1846 @masom Some work on rebuilding inventory
authored
109 }
29b26bf @masom QA Pass
authored
110
111 foreach ($offersInventory as $offer_id => $availability){
eed347d @masom Models are now pluralized.
authored
112 static::update(compact('availability'), array('_id'=>$offer_id));
3f8ea59 @masom Some work on tickets and having models extend a new base model
authored
113 }
cb7ae71 @masom Fixes a rebuild bug
authored
114 return $offersInventory;
dcc1846 @masom Some work on rebuilding inventory
authored
115 }
29b26bf @masom QA Pass
authored
116
2b823d1 @masom Added slugs url on the index page
authored
117 /**
118 * Release specified inventory and make it available again.
119 * @param var $cart_id
120 * @param var $offer_id
121 * @return boolean
122 */
123 public static function releaseInventory($cart_id, $offer_id){
009f0a9 @masom Some work on the cart system to release inventory properly
authored
124 try{
4e7d3d6 @masom Some more work toward mongodb cart
authored
125 Inventories::release($cart_id, $offer_id);
009f0a9 @masom Some work on the cart system to release inventory properly
authored
126 }catch(InventoryException $e){
e1064dc @masom Offers are now tested
authored
127 $message = "Could not release inventory for the following reason: {$e->getMessage()}";
29b26bf @masom QA Pass
authored
128 Logger::write('error', $message);
009f0a9 @masom Some work on the cart system to release inventory properly
authored
129 return false;
130 }
29b26bf @masom QA Pass
authored
131
009f0a9 @masom Some work on the cart system to release inventory properly
authored
132 $query = array('$inc' => array('availability'=>1));
133 $conditions = array('_id' => $offer_id);
134 return static::update($query, $conditions);
135 }
29b26bf @masom QA Pass
authored
136
ecccada @masom ...
authored
137 /**
29b26bf @masom QA Pass
authored
138 * Reserve an item in the inventory
139 * @param var $offer_id The offer id
140 * @param var $cart_id The cart id the item is reserved for.
838bf32 @masom New tests for Offers
authored
141 * @return var Inventory id that got reserved
ecccada @masom ...
authored
142 */
3711717 @masom Some more work toward a mongodb-based cart
authored
143 public static function reserve($offer_id, $cart_id){
0c1c709 @masom Some work on tests and refactoring of offers
authored
144 $date = new \MongoDate();
9b9c82a @masom Database strategy found.
authored
145 $conditions = array(
0c1c709 @masom Some work on tests and refactoring of offers
authored
146 'starts' => array('$lt' => $date),
147 'ends' => array('$gt' => $date),
57d6bb1 @masom Some more polishing work
authored
148 '_id' => $offer_id,
149 'state'=> 'published'
ecccada @masom ...
authored
150 );
0c1c709 @masom Some work on tests and refactoring of offers
authored
151
838bf32 @masom New tests for Offers
authored
152 $offer = static::first(compact('conditions'));
29b26bf @masom QA Pass
authored
153 if (!$offer){
3f022ad @masom More work on error handling
authored
154 throw new OfferException("Offer not found.");
9b9c82a @masom Database strategy found.
authored
155 }
0c1c709 @masom Some work on tests and refactoring of offers
authored
156
838bf32 @masom New tests for Offers
authored
157 $error = null;
3f022ad @masom More work on error handling
authored
158 try{
3711717 @masom Some more work toward a mongodb-based cart
authored
159 $inventory = Inventories::reserve($offer_id, $cart_id);
6eb4636 @masom Coutdown on index. Start of Inventory handling
authored
160 $offer->availability--;
838bf32 @masom New tests for Offers
authored
161 }catch (InventoryException $e){
b73a2df @masom Oups wrong throw
authored
162 throw $e;
838bf32 @masom New tests for Offers
authored
163 }
164
165 if ($offer->availability < 0){
82aa43b @masom Some progress on buying coupons
authored
166 $offer->availability = 0;
838bf32 @masom New tests for Offers
authored
167 }
168
169 $offer->save(null, array('validate' => false, 'whitelist' => array('availability')));
3f022ad @masom More work on error handling
authored
170 return $inventory->_id;
ecccada @masom ...
authored
171 }
29b26bf @masom QA Pass
authored
172
1d359d3 @masom Some more work on admin panel
authored
173 public function createWithInventory($entity){
174 $entity->inventoryCount = $entity->availability;
29b26bf @masom QA Pass
authored
175
176 if (!$entity->save()){
1d359d3 @masom Some more work on admin panel
authored
177 return false;
178 }
29b26bf @masom QA Pass
authored
179
6eb4636 @masom Coutdown on index. Start of Inventory handling
authored
180 $created = 0;
29b26bf @masom QA Pass
authored
181 for ($i = 0; $i < $entity->inventoryCount; $i++){
a30c03a @masom Added sequence number and printout choices
authored
182 if (Inventories::createForOffer($entity->_id, $i)){
6eb4636 @masom Coutdown on index. Start of Inventory handling
authored
183 $created++;
0c1c709 @masom Some work on tests and refactoring of offers
authored
184 }else{
29b26bf @masom QA Pass
authored
185 Logger::write('error', "Could not create inventory item for {$entity->_id}.");
6eb4636 @masom Coutdown on index. Start of Inventory handling
authored
186 }
187 }
29b26bf @masom QA Pass
authored
188
189 if ($created != $entity->inventoryCount){
1d359d3 @masom Some more work on admin panel
authored
190 throw new \InventoryException("Only {$created} item created.");
6eb4636 @masom Coutdown on index. Start of Inventory handling
authored
191 }
838bf32 @masom New tests for Offers
authored
192
1d359d3 @masom Some more work on admin panel
authored
193 return true;
194 }
29b26bf @masom QA Pass
authored
195
1d359d3 @masom Some more work on admin panel
authored
196 public function publish($entity){
197 $entity->state = 'published';
198 return $entity->save(null,array('validate'=>false,'whitelist'=>array('state')));
6eb4636 @masom Coutdown on index. Start of Inventory handling
authored
199 }
0c1c709 @masom Some work on tests and refactoring of offers
authored
200
6eb4636 @masom Coutdown on index. Start of Inventory handling
authored
201 public function unpublish($entity){
202 $entity->state = 'unpublished';
8b13b52 @masom Some work on sessions and users
authored
203 return $entity->save(null,array('validate'=>false,'whitelist'=>array('state')));
6eb4636 @masom Coutdown on index. Start of Inventory handling
authored
204 }
29b26bf @masom QA Pass
authored
205
cd164e2 @masom Fixed file upload possible bug
authored
206 public function getErrors(){
207 return $this->_errors;
208 }
29b26bf @masom QA Pass
authored
209
cd164e2 @masom Fixed file upload possible bug
authored
210 public function save($entity, $data = null, array $options = array()) {
211 $files = array();
29b26bf @masom QA Pass
authored
212 if (isset($data['image'])){
57d6bb1 @masom Some more polishing work
authored
213 $files['image'] = $data['image'];
214 unset($data['image']);
215 }
29b26bf @masom QA Pass
authored
216
217 if (!$entity->_id){
57d6bb1 @masom Some more polishing work
authored
218 $entity->_id = new \MongoId();
219 }
29b26bf @masom QA Pass
authored
220
cd164e2 @masom Fixed file upload possible bug
authored
221 $this->_errors = array();
29b26bf @masom QA Pass
authored
222 foreach ($files as $key => $file){
223 if (!$file['tmp_name'] || empty($file['tmp_name'])){
224 continue;
225 }
226
eed347d @masom Models are now pluralized.
authored
227 $image = Images::create();
cd164e2 @masom Fixed file upload possible bug
authored
228 $imageData = array('file'=> $file, 'parent_id'=> $entity->_id, 'parent_type'=>'offer');
29b26bf @masom QA Pass
authored
229 if ($image->save($imageData)){
cd164e2 @masom Fixed file upload possible bug
authored
230 $data[$key] = $image->_id;
231 }else{
29b26bf @masom QA Pass
authored
232 $this->_errors[] = "Image {$key} could not be saved.";
cd164e2 @masom Fixed file upload possible bug
authored
233 }
234 }
235 return parent::save($entity, $data, $options);
236 }
86c8e2d @masom Some more initial work and proof of concept
authored
237 }
29b26bf @masom QA Pass
authored
238
86c8e2d @masom Some more initial work and proof of concept
authored
239 ?>
Something went wrong with that request. Please try again.