<?php

/**
 * discount actions.
 *
 * @package    sf
 * @subpackage discount
 * @author     Atma
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class discountActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        if ($request->hasParameter('discount')) {
            $data = $request->getParameter('discount');
            $discount = Doctrine::getTable('Discount')->findOneByUrl($data['url']);
            if ($discount) {
                $date_now = new DateTime('now');
                $date_now->modify('+1 day');
                $date_limit = new DateTime('now');
                $date_limit->modify('-30 day');
                sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
                $check_discounts = Doctrine_Query::create()
                    ->select()
                    ->from('DiscountPhone dp')
                    ->where('dp.phone = ?', $data['phone'])
                    ->andWhere('dp.discount_id != ?', $discount->getId())
                    ->andWhere('dp.order_id is null')
                    ->andWhere('dp.created_at BETWEEN STR_TO_DATE(\'' . $date_limit->format('Y-m-d H:i:s') . '\', \'%Y-%m-%d %H:%i:%s\') 
  AND STR_TO_DATE(\'' . $date_now->format('Y-m-d H:i:s') . '\', \'%Y-%m-%d %H:%i:%s\')')
                    ->fetchArray();
                if (count($check_discounts) == 1) {
                    echo include_partial('discount/form', array('status' => 'error', 'error' => 'У вас уже есть скидка ' . $check_discounts[0]["active_discount"] . '%'));
                } else {
                    $check_phone = Doctrine_Query::create()
                        ->select()
                        ->from('DiscountPhone dp')
                        ->where('dp.phone = ?', $data['phone'])
                        ->andWhere('dp.discount_id = ?', $discount->getId())
                        ->fetchArray();
                    if (count($check_phone) == 0) {
                        $discount_phone = new DiscountPhone();
                        $discount_phone->setDiscountId($discount->getId());
                        $discount_phone->setPhone($data['phone']);
                        $discount_phone->save();
                        $this->getUser()->setAttribute('number_of_views', 3);
                        $this->getUser()->setAttribute('phone_number', $data['phone']);
                        $this->getUser()->setAttribute('active_discount', 3);
                        echo include_partial('discount/form', array('status' => 'complete'));
                    } else {
                        if (is_numeric($check_phone[0]['order_id'])) {
                            echo include_partial('discount/form', array('status' => 'error', 'error' => 'Вы уже использовали данную скидку'));
                        } else {
                            $date_created = new DateTime($check_phone[0]['created_at']);
                            $interval = $date_now->diff($date_created);
                            $interval = $interval->format('%a');
                            $remained = 30 - $interval;
                            if (intval($remained) <= 30) {
                                echo include_partial('discount/form', array('status' => 'error', 'error' => "У вас уже есть данная скидка.\n Осталось " . $remained . " дней"));
                            } else {
                                echo include_partial('discount/form', array('status' => 'error', 'error' => 'Вы не можете активировать скидку второй раз'));
                            }
                        }
                    }
                }
            }
        }
        return sfView::NONE;
    }

    public function executeCheck_discount(sfWebRequest $request)
    {
        if ($request->hasParameter('phone') && $request->getParameter('phone') != false) {
            $date_now = new DateTime('now');
            $date_limit = new DateTime('now');
            $date_limit->modify('-30 day');
            $discount = Doctrine_Query::create()
                ->select()
                ->from('DiscountPhone dp')
                ->where('dp.phone = ?', $request->getParameter('phone'))
                ->andWhere('dp.order_id is null')
                ->andWhere('dp.created_at BETWEEN STR_TO_DATE(\'' . $date_limit->format('Y-m-d H:i:s') . '\', \'%Y-%m-%d %H:%i:%s\') 
  AND STR_TO_DATE(\'' . $date_now->format('Y-m-d H:i:s') . '\', \'%Y-%m-%d %H:%i:%s\')')
                ->execute();
            if (count($discount) > 0) {
                $discount = $discount->getFirst();
                $this->getUser()->setAttribute('active_discount', $discount->getActiveDiscount());
                $this->getUser()->setAttribute('phone', $request->getParameter('phone'));
                //sfContext::getInstance()->getConfiguration()->loadHelpers('Components');
                //$body = include_component('basket', 'basket');
                echo json_encode(array('status' => 'success', 'active_discount' => $discount->getActiveDiscount(), 'body' => $this->getComponent('basket', 'basket')));
            } else {
                $this->getUser()->setAttribute('active_discount', false);
                $this->getUser()->setAttribute('phone', false);
                echo json_encode(array('status' => 'success', 'active_discount' => 0, 'body' => $this->getComponent('basket', 'basket')));
            }
        }
        return sfView::NONE;
    }
    public function executeCheck_discount_term (sfWebRequest $request) {
        if ($this->getUser()->hasAttribute('active_discount') && $this->getUser()->getAttribute('active_discount') != false && $this->getUser()->hasAttribute('phone_number') && $this->getUser()->getAttribute('phone_number') != false) {
            $date_now = new DateTime('now');
            $date_limit = new DateTime('now');
            $date_limit->modify('-30 day');
            $discount = Doctrine_Query::create()
                ->select()
                ->from('DiscountPhone dp')
                ->where('dp.phone = ?', $this->getUser()->getAttribute('phone_number'))
                ->andWhere('dp.order_id is null')
                ->andWhere('dp.created_at BETWEEN STR_TO_DATE(\'' . $date_limit->format('Y-m-d H:i:s') . '\', \'%Y-%m-%d %H:%i:%s\') 
  AND STR_TO_DATE(\'' . $date_now->format('Y-m-d H:i:s') . '\', \'%Y-%m-%d %H:%i:%s\')')
                ->execute();
            if (count($discount) > 0) {
                $discount = $discount->getFirst();
                echo json_encode(array('status' => 'success', 'active_discount' => $discount->getActiveDiscount()));
            } else {
                echo json_encode(array('status' => 'success', 'active_discount' => 0));
            }
        }else {
            echo json_encode(array('status' => 'success', 'active_discount' => 0));
        }
        return sfView::NONE;
    }
}
