actions.class.php 6.88 KB
Newer Older
Яков's avatar
first  
Яков committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?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;
    }
}