Created
April 9, 2014 20:04
-
-
Save brunokruse/10308906 to your computer and use it in GitHub Desktop.
scans/add
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function add() { | |
Configure::write('debug', 2); | |
try { | |
if (!$this->checkKey()) { | |
throw new Exception('Empty key', 10016); | |
} | |
if (empty($this->request->data['private_token'])) { | |
throw new Exception('Empty private token', 10002); | |
} | |
if (empty($this->request->data['qr_code'])) { | |
throw new Exception('Parameters are missing', 10005); | |
} | |
$result = array('code' => 200, 'time' => time()); | |
$id = $this->__get_id($this->request->data['qr_code']); | |
if ($id == false) { | |
throw new Exception('Bar Code Validation error', 10011); | |
} | |
// get our campaign | |
$campaign = $this->Scan->Campaign->find('first', array( | |
'conditions' => array('Campaign.id' => $id, 'Campaign.is_deleted' => false), | |
'recursive' => -1, | |
'fields' => array('deal_type', | |
'Campaign.id', | |
'Campaign.scan_frequency_count', | |
'Campaign.scan_frequency_interval', | |
'redemptions_limit') | |
)); | |
if (empty($campaign['Campaign']['id'])) { | |
throw new Exception('Campaign not found', 10012); | |
} | |
// scan to unlock deal type | |
$scanAllowed = true; // default to allow | |
$isSaved = true; | |
if ($campaign['Campaign']['deal_type'] == 'unlock') { | |
// check scan frequency | |
$createdCondition = array(); | |
$createdCondition = $this->generate_created_condition($campaign); | |
// count the number of scans for a campaign | |
$countScanned = $this->count_scans_for_campaign($campaign, $createdCondition); | |
// - | |
// 1. check to make sure we are not over the scan frequency count | |
// - | |
if ($countScanned >= $campaign['Campaign']['scan_frequency_count']) { | |
CakeLog::write('debug', '00. we reached the limit scan is not allowed: ' . $countScanned . ' -- ' . $campaign['Campaign']['scan_frequency_count']); | |
$scanAllowed = false; | |
$result['code'] = 10022; | |
$result['message'] = __('Scan is not possible, over the frequency count.'); | |
//$result['too_recent_scan'] = $countScannedResults[0]['last_created']; | |
} else { | |
// check saved coupons | |
$countSaved = $this->count_saved_deals_for_campaign($campaign, $createdCondition); | |
if ($createdCondition != array()) { | |
$countScanned = $this->count_scans_for_campaign($campaign, $createdCondition); | |
} | |
if ($countSaved == 0 AND $countScanned >= 1) { | |
$isSaved = false; | |
$result['campaign_id'] = $campaign['Campaign']['id']; | |
} | |
} | |
} | |
// - | |
// 2. scans are not allowed if we are over the campaign redemption limit | |
// - | |
// conditions in which scans are denied | |
if ($scanAllowed && !empty($campaign['Campaign']['redemptions_limit'])) { | |
$valid = $this->Scan->Campaign->Redemption->checkRedemptionLimit($campaign['Campaign']['id']); | |
if (!$valid) { | |
// deal closed | |
$result['closed'] = 1; | |
$scanAllowed = false; | |
} | |
} | |
if (!$isSaved) { | |
$scanAllowed = false; | |
unset($isSaved); | |
} | |
if ($scanAllowed) { | |
$saveData = array( | |
'private_token' => $this->request->data['private_token'], | |
'lat' => $this->request->data['latitute'], | |
'lng' => $this->request->data['longitude'], | |
'campaign_id' => $campaign['Campaign']['id'], | |
'code' => $this->request->data['qr_code'] | |
); | |
$this->Scan->create(); | |
if (!$this->Scan->save($saveData)) { | |
throw new Exception('Validation error', 10001); | |
} | |
$result['campaign_id'] = $campaign['Campaign']['id']; | |
} | |
} catch (Exception $exception) { | |
$result['code'] = $exception->getCode(); | |
$result['message'] = $exception->getMessage(); | |
switch ($result['code']) { | |
case 10001: | |
$result['validationErrors'] = $this->Scan->validationErrors; | |
break; | |
} | |
} | |
$this->set(array( | |
'result' => $result, | |
'_serialize' => array('result') | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment