<?php
// ini_set('display_errors', 1);
/**
 * MainController
 * Feel free to delete the methods and replace them with your own code.
 *
 * @author JieRen
 */
class SaleController extends DooController {

    public $data;

    /**
     * 	构造函数
     */
    function __construct() {
        session_start();
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
    }

    /**
     * 	索引列表页
     */
    function index() {
        if (empty($_SESSION['main'])) {
            return Doo::conf()->APP_URL . 'm';
        }
        if(isset($_POST['submit'])&&isset($_POST['salename'])&&isset($_POST['promocode'])){
            if (preg_match('/^1[3458][0-9]{9}$/', $_POST['promocode'])) {
                Doo::loadModel('m/sale');
                $saleObj = new Sale();
                $saleObj->salename = $_POST['salename'];
                $saleObj->promocode = $_POST['promocode'];
                $saleObj->total = 0;
                $saleObj->usez = 0;
                if($saleObj->insert()){
//                    exit('添加完成');
                }
            }
        }
        Doo::loadModel('m/sale');
        Doo::loadHelper('DooPager');
        $sale = new Sale();
        $totalArchive = $sale->count();
        $pager = new DooPager(Doo::conf()->APP_URL . "m/sale/page", $totalArchive, 30, 10);
        if (!empty($this->params['pindex']))
            $pager->paginate(intval($this->params['pindex']));
        else
            $pager->paginate(1);
        $saleArray = $sale->find(array('select' => 'id, salename, promocode, total, usez', 'desc' => 'id', 'limit' => $pager->limit, 'asArray' => true));
        foreach ($saleArray as $key => $value) {
            $saleArray[$key]['unuse'] = $value['total']-$value['usez'];
        }
        $this->data['userArray'] = $saleArray;
        $moneyArray = $sale->find(array('select' => 'sum(total) as total,sum(usez) as usez', 'desc' => 'id', 'asArray' => true));
        $this->data['total'] = $moneyArray[0]['total']+$moneyArray[0]['usez'];
        $this->data['usez'] = $moneyArray[0]['usez'];
        $this->data['unpay'] = $moneyArray[0]['total'];
        $this->data['pager'] = $pager->output;
        $this->data['selectsale'] = true;
        $this->render('admin/sale', $this->data,true);
    }

    /**
     *  确认提取
     */
    function doCash() {
        if (empty($_SESSION['main'])) {
            return Doo::conf()->APP_URL . 'm';
        }
        Doo::loadModel('m/sale');
        if(isset($_POST['submit'])&&isset($_POST['confirm'])&&isset($_POST['money'])){
            $conStr = $_POST['confirm'];
            $monStr = intval($_POST['money']);
            $pidStr = $this->params['pid'];
            Doo::loadModel('m/sale');
            $sale = new Sale();
            $saleArray = $sale->getById($pidStr,array('asArray' => true));
            // var_dump($saleArray[0]);
            if ($saleArray[0]['total']>=$monStr) {
                // TODO：减去提取的金额
                // TODO：写入记录表
                $sale->id = $saleArray[0]['id'];
                $sale->total = new DooDbExpression('total-' . $monStr);
                $sale->usez = new DooDbExpression('usez+' . $monStr);
                $sale->update();
                Doo::loadModel('m/billrecord');
                $brecord = new Billrecord();
                $brecord->salename = $saleArray[0]['salename'];
                $brecord->total = $saleArray[0]['total'];
                $brecord->usez = $saleArray[0]['usez'];
                $brecord->curr = $monStr;
                $brecord->opuser = $conStr;
                $brecord->optime = time();
                $brecord->insert();
                // exit('已操作成功');
            }
        }
        // $sale = new Sale();
        // $saleArray = $sale->find(array('select' => 'id, salename, promocode, total, usez', 'desc' => 'id', 'asArray' => true));
        // $this->data['userArray'] = $saleArray;
        // $this->data['unuse'] = $saleArray['total']-$saleArray['usez'];
//        $this->data['uid'] = $this->params['pid'];
//        $this->render('admin/sale_cash', $this->data,true);
        return '/m/sale';
    }


}

?>