Skip to content

Instantly share code, notes, and snippets.

@brunokruse
Created March 4, 2014 21:22
Show Gist options
  • Save brunokruse/9355975 to your computer and use it in GitHub Desktop.
Save brunokruse/9355975 to your computer and use it in GitHub Desktop.
public function deal_datas($private_token, $campaign_ids) {
$campaigns = $this->find('all', array(
'fields' => array(
'User.country_id',
'User.street',
'User.zipcode',
'User.location',
'User.phone',
'Campaign.id',
'Campaign.deal_type',
'Campaign.company_url',
'Campaign.name',
'Campaign.company_name',
'Campaign.start_date',
'Campaign.end_date',
'Campaign.promotion_name',
'Campaign.promotion_description',
'Campaign.barcode',
'Campaign.product_logo',
'Campaign.product_picture',
'Campaign.lottery_type',
'Campaign.redemption_frequency_count',
'Campaign.redemption_frequency_interval',
'Campaign.redemptions_limit',
'Campaign.lottery_end_date',
'Campaign.lottery_code',
'Campaign.lottery_contact',
'Campaign.lottery_contact_phone',
'Campaign.scan_limit',
'Campaign.rating',
'Campaign.category_id',
'Campaign.status',
'Campaign.standard_price',
'Campaign.discount_nach_price',
'Campaign.discount',
'Campaign.discount_percent',
'Campaign.discount_count',
'Campaign.discount_reference'
),
'conditions' => array(
'Campaign.id' => $campaign_ids
),
'contain' => array(
'User',
'Saving' => array('conditions' => array('Saving.private_token' => $private_token), 'fields' => array('Saving.id', 'Saving.created')),
'Scan' => array('conditions' => array('Scan.private_token' => $private_token), 'fields' => array('Scan.id')),
'Social' => array('conditions' => array('Social.private_token' => $private_token), 'fields' => array('Social.id')),
'Geocode' => array(
'fields' => array('lng', 'lat', 'formatted_address', 'phone'),
'Opening' => array('fields' => array('title', 'start', 'end'))
)
//'Rating' => array('fields' => array('SUM(Rating.value) as sum, count(*) as count'))
)
));
$redemptions = $this->Redemption->find('all', array(
'fields' => array('campaign_id', 'max(created) AS last_redeemed'),
'conditions' => array('private_token' => $private_token, 'campaign_id' => $campaign_ids),
'recursive' => -1,
'group' => 'campaign_id'));
$lastRedemptions = Hash::combine($redemptions, '{n}.Redemption.campaign_id', '{n}.0.last_redeemed');
$result = array();
foreach ($campaigns as $ckey => $campaign) {
$savingCount = count($campaign['Saving']);
// cut off seconds
if (isset($campaign['Geocode'])) {
foreach ($campaign['Geocode'] AS $geocodeIndex => $geocode) {
foreach ($geocode['Opening'] AS $openingIndex => $opening) {
$campaign['Geocode'][$geocodeIndex]['Opening'][$openingIndex]['start'] = substr($opening['start'], 0, 5);
$campaign['Geocode'][$geocodeIndex]['Opening'][$openingIndex]['end'] = substr($opening['end'], 0, 5);
}
}
}
$addedTimestamp = 0;
if ($savingCount > 0) {
$addedTimestamp = strtotime($campaign['Saving'][0]['created']);
}
$endtime = strtotime($campaign['Campaign']['end_date']);
$phone = '';
if ($campaign['User']['phone']) {
$phone = $campaign['User']['phone'];
}
// check redeemed
$lastRedeemed = 0;
$redemptionsLimit = 0;
$countRedeemed = 0;
if (isset($lastRedemptions[$campaign['Campaign']['id']])) {
// check reached redemption frequency
$createdCondition = array();
switch ($campaign['Campaign']['redemption_frequency_interval']) {
// day
case 'day':
$createdCondition = array('created >' => date('Y-m-d H:i:s', time() - DAY));
break;
// week
case 'week':
$createdCondition = array('created >' => date('Y-m-d H:i:s', time() - WEEK));
break;
// month (30 days)
case 'month':
$createdCondition = array('created >' => date('Y-m-d H:i:s', time() - MONTH));
break;
}
$countRedeemed = $this->Redemption->find('first', array(
'fields' => array('count(*) AS count_redeemed'),
'conditions' => $createdCondition +
array(
'private_token' => $private_token,
'campaign_id' => $campaign['Campaign']['id']
),
'recursive' => -1));
$countRedeemed = isset($countRedeemed[0]['count_redeemed']) ? $countRedeemed[0]['count_redeemed'] : 0;
if ($countRedeemed >= $campaign['Campaign']['redemption_frequency_count']) {
// invalid
$lastRedeemed = strtotime($lastRedemptions[$campaign['Campaign']['id']]);
} else {
$valid = $this->Redemption->checkRedemptionLimit($campaign['Campaign']['id']);
if (!$valid) {
// count redeemed not higher than count
$lastRedeemed = strtotime($lastRedemptions[$campaign['Campaign']['id']]);
$redemptionsLimit = $campaign['Campaign']['redemptions_limit'];
}
}
}
if ($countRedeemed >= $redemptionsLimit) {
$data = array('id' => $campaign['Campaign']['id'],
'status' => 0,
'modified' => 'NOW()',
$this->Campaign->save($data);
}
$result[$ckey] = array(
'id' => $campaign['Campaign']['id'],
'type' => $campaign['Campaign']['deal_type'],
'locked' => false,
'entered_lottery' => false,
'added' => $addedTimestamp,
// 0 if valid
'last_redeemed' => $lastRedeemed,
// 0 if valid
'redemptions_limit' => $redemptionsLimit,
'count_redeemed' => $countRedeemed,
'saving' => $savingCount,
'adress' => Data::formatAddress($campaign['User']),
'phone' => $phone,
'company_url' => $campaign['Campaign']['company_url'],
'campaign_url' => 'http://ec2-54-201-243-213.us-west-2.compute.amazonaws.com/?site=view&id=' . $campaign['Campaign']['id'],
'title' => $campaign['Campaign']['name'],
'company' => $campaign['Campaign']['company_name'],
'end_time' => $endtime,
'promotion_name' => $campaign['Campaign']['promotion_name'],
'promotion_description' => $campaign['Campaign']['promotion_description'],
'barcode' => $campaign['Campaign']['barcode'],
'product_logo' => $campaign['Campaign']['product_logo'],
'product_picture' => $campaign['Campaign']['product_picture'],
'rating' => $campaign['Campaign']['rating'],
'category_id' => $campaign['Campaign']['category_id'],
'status' => $campaign['Campaign']['status'],
'discount' => $campaign['Campaign']['discount'],
'discount_percent' => Data::formatNumberOutputStrict($campaign['Campaign']['discount_percent']),
'standard_price' => Data::formatNumberOutputStrict($campaign['Campaign']['standard_price']),
'discount_nach_price' => Data::formatNumberOutputStrict($campaign['Campaign']['discount_nach_price']),
'discount_count' => $campaign['Campaign']['discount_count'],
'discount_reference' => $campaign['Campaign']['discount_reference'],
'Geocode' => $campaign['Geocode']
);
switch ($campaign['Campaign']['deal_type']) {
case 'lottery':
$enteredLotteryCount = $this->SpecialSaving->find('count', array(
'recursive' => -1,
'conditions' => array(
'private_token' => $private_token,
'campaign_id' => $campaign['Campaign']['id']
)
));
if ($enteredLotteryCount > 0) {
$result[$ckey]['entered_lottery'] = true;
}
$result[$ckey]['locked'] = true;
if (time() >= (strtotime($campaign['Campaign']['lottery_end_date']))) {
$winner = $this->Winner->find('count', array(
'conditions' => array(
'Winner.private_token' => $private_token,
'Winner.campaign_id' => $campaign['Campaign']['id']
)
));
if ($winner) {
$result[$ckey]['winner'] = true;
if (!empty($campaign['Campaign']['lottery_type']) AND $campaign['Campaign']['lottery_type'] == 'special') {
$result[$ckey]['lottery_contact'] = $campaign['Campaign']['lottery_contact'];
$result[$ckey]['lottery_contact_phone'] = $campaign['Campaign']['lottery_contact_phone'];
$result[$ckey]['lottery_code'] = $campaign['Campaign']['lottery_code'];
$result[$ckey]['discount'] = '';
$result[$ckey]['discount_percent'] = '';
} else {
$result[$ckey]['locked'] = false;
}
}
}
$result[$ckey]['lottery_end_date'] = strtotime($campaign['Campaign']['lottery_end_date']);
break;
case 'unlock':
$scanCount = count($campaign['Scan']);
$result[$ckey]['number_of_scan'] = $scanCount;
$result[$ckey]['scan_limit'] = (int) $campaign['Campaign']['scan_limit'];
if ($campaign['Campaign']['scan_limit'] < $scanCount) {
$result[$ckey]['locked'] = true;
}
break;
case 'social':
if (count($campaign['Social']) == 0) {
$result[$ckey]['locked'] = true;
}
break;
default:
break;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment