<?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)
    {

    }
}
