actions.class.php 3.68 KB
Newer Older
Игорь's avatar
init    
Игорь 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
<?php

class mainActions extends sfActions
{
    private function isOutAvailable($account)
    {
        return $account['courier_money_available'] >= Yavinesu::getCourierOutMin();
    }

    public function executeIndex(sfWebRequest $request)
    {
        $balance = 0;

        $exports = array();
        if($this->getUser()->isAuthenticated()){
            $account = $this->getUser()->getAccount();

            $exports = Doctrine_Query::create()
                ->select("e.id, e.weight, e.logic_type, e.sum, c.title AS cat_title, c.id AS cat_id, c.price AS cat_price, c.min_weight AS cat_min_weight")
                ->from("Export e")
                ->leftJoin("e.Cat c")
                //->where("e.logic_type = 'wait'")
                ->andWhere("e.user_id = ?", $account->getId())
                ->orderBy("e.logic_type DESC, e.created_at DESC")
                ->fetchArray();

            $export_q = Doctrine_Query::create()
                ->select("e1.sum")
                ->from("Export e1")
                //->where("e1.logic_type != 'wait'")
                ->andWhere("e1.user_id = ?", $account->getId())
                ->fetchArray();

            if(count($export_q) > 0){
                foreach ($export_q as $item){
                    $balance = $balance + $item['sum'];
                }
            }
        }

        $cat_q = Doctrine_Query::create()
            ->select("c.id, c.title, c.min_weight, c.price")
            ->from("Cat c")
            ->orderBy("c.order_id")
            ->fetchArray();
        
        $this->balance = $balance;
        $this->cats = $cat_q;
        $this->exports = $exports;
    }

    public function executeOut(sfWebRequest $request)
    {
        $this->form = new cashOutForm();
        if($request->isMethod('post')){
            if(csSettings::get('mass_payment_on')){
                $this->form->bind($request->getParameter($this->form->getName()));
                if($this->form->isValid()){
                    $account = $this->getUser()->getAccount();
                    if($this->isOutAvailable($account)){

                        /*добавить проверку на подсчет всех денег по таблице courierMoney*/

                        $money_out = new CourierMoneyOut();
                        $money_out['courier_id'] = $account->getId();
                        $money_out['status'] = 'wait';
                        $money_out['created'] = date('Y-m-d H:i:s');
                        $money_out['total'] = $account['courier_money_available'];
                        $money_out->save();
                        if($money_out){
                            $result = $money_out->massPaymentCreate($this->form->getValue('card'));
                            if($result['state'] === 'success'){
                                $this->completed = true;
                            }else{
                                $this->form->getErrorSchema()->addError(new sfValidatorError(new sfValidatorSchema(), $result['message']));
                            }
                        }else{
                            $this->form->getErrorSchema()->addError(new sfValidatorError(new sfValidatorSchema(), 'Техническая ошибка. Попробуйте позднее'));
                        }
                    }else{
                        $this->form->getErrorSchema()->addError(new sfValidatorError(new sfValidatorSchema(), 'Нельзя вывести меньше минимальной суммы вывода'));
                    }
                }
            }else{
                $this->completed = true;
            }
        }
    }
    
    public function executeError404(sfWebRequest $request)
    {

    }
}