Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
147 lines (132 sloc) 4.08 KB
<?php
/**
* Chowly Pick. Eat. Save!
*
* @copyright Copyright 2011, Martin Samson <pyrolian@gmail.com>
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace chowly\models;
class Purchases extends \chowly\extensions\data\Model{
protected static $_pdfBasePath = '/resources/purchases';
protected $_schema = array(
'_id' => array('type'=>'id'),
'customer_id' => array('type'=>'id'),
'name' => array('type'=>'string'),
'email' => array('type'=>'string'),
'address' => array('type'=>'string'),
'city' => array('type'=>'string'),
'phone' => array('type'=>'string'),
'postal' => array('type'=>'string'),
'cc_number' => array('type'=>'string'),
'cc_sc' => array('type' => 'string'),
'status' => array('type' => 'string'),
'province' => array('type'=>'string'),
'created' => array('type'=>'date'),
'updated' => array('type'=>'date'),
'offers' => array('type'=>'array', 'array'=>true)
);
public $validates = array(
'agreed_tos_privacy' => array(
array('notEmpty', 'message' => 'Must be agreed.')
),
'name' => array(
array('lengthBetween', 'min' => 1, 'max' => 255,
'message' => 'Please enter a name between 1 and 255 characters.')
),
'email' => array(
array('email', 'message' => 'Email is not valid.')
),
'address'=>array(
array('notEmpty', 'message'=>'The Cardholder Address cannot be empty.')
),
'city' => array(
array('notEmpty', 'message'=>'The Cardholder City cannot be empty.')
),
'phone' => array(
array('notEmpty', 'message'=>'The Cardholder Phone Number cannot be empty.')
),
'postal' => array(
array('notEmpty', 'message'=>'The Cardholder Postal Code cannot be empty.')
),
'cc_number' => array(
array('creditCard', 'deep'=>true, 'format' => 'any',
'message'=>'Invalid Credit Card Number.')
),
'cc_sc' => array(
array('numeric','message'=>'Invalid')
),
'status' => array(
array('inList', 'list' => array('completed','new', 'failed', 'cancelled'))
),
'province' => array(
array('inList', 'list'=>array(), 'message'=>'Invalid Province.')
)
);
protected static $_provinces = array(
'Alberta'=>'Alberta',
'British Columbia' => 'British Columbia',
'Manitoba' => 'Manitoba',
'New Brunswick' => 'New Brunswick',
'Newfoundland' => 'Newfoundland',
'Norhtwest Territories' => 'Norhtwest Territories',
'Nova Scotia' => 'Nova Scotia',
'Nunavut' => 'Nunavut',
'Ontario' => 'Ontario',
'Prince Edward Island' => 'Prince Edward Island',
'Quebec' => 'Quebec',
'Saskatchewan' => 'Saskatchewan',
'Yukon' => 'Yukon'
);
/**
* Returns the path to the folder containing the purchases pdfs.
*/
public static function pdfPath(){
return LITHIUM_APP_PATH . static::$_pdfBasePath;
}
/**
* Adds dynamic provice list to the validation list.
* @param Entity $entity
* @param array $options Options sent to parent::validate
*/
public function validates($entity, array $options = array()){
$this->validates['province'][0]['list'] = array_keys(static::$_provinces);
return parent::validates($entity,$options);
}
/**
* Process the purchase with the payment gateway
* @param Entity $entity
* @param DocumentSet $offers List of offers being purchased.
* @throws \Exception
* @return boolean
*/
public function process($entity, $offers){
if (empty($offers)){
throw new \Exception("There are no offers matching the cart items.");
}
$entity->price = 0.00;
foreach ($offers as $offer){
$entity->price += $offer->cost;
$entity->offers[] = $offer;
}
//TODO: Actual CC Processing.
$entity->status = 'completed';
$entity->cc_number = substr($entity->cc_number, -4, 4);
unset($entity->cc_sc, $entity->cc_e_month, $entity->cc_e_year);
if (!$entity->save(null,array('validate'=>false))){
throw new \Exception("The purchase could not be saved.");
}
return true;
}
/**
* If the purchase was completed (and approved)
* @param Entity $entity
* @return boolean
*/
public function isCompleted($entity){
return ($entity->status == 'completed') ? true : false;
}
public static function getProvinces(){
return static::$_provinces;
}
}
?>
Something went wrong with that request. Please try again.