<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends salebase {

    
    public function action_add(){
        $id=$_REQUEST['id'];
        $model = parent::model('purchase', 'sa');
        $opmode="add";
        if($id){
            $opmode="edit";
            $vo=$model->get_data($id);
            $number=$vo['kehu_number'];
        } else {
            $next_id=$model->popup($id);
            $number="CG".sprintf("%04d", $next_id);
        }
        unset($model);

        $model = parent::model('supplier', 'sa');
        list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
        unset($model);

        $model = parent::model('product', 'am');
        list($total1, $data1) = $model->getAllList(array('searchsql'=>$searchsql));

        $var_array = array(
            'opmode'=>$opmode,
            'vo'=>$vo,
            'number'=>$number,
            'gys'=>$data,
            'product'=>$data1
        ); 

        TPL::assign($var_array);          
        TPL::display($this->cptpl.'purchase/add.tpl');
    }



    public function action_save(){
        $arr=$_POST;
        $id=$arr['id'];
        $model = parent::model('purchase', 'sa');
        
        $result = $model->doadd($arr);
        
        if (true === $result) {
            $data['code']=1;
            $data['msg']="申请成功";
        }
        else {
            $data['msg']="申请失败";
        }

        echo json_encode($data);
    }

    public function action_noStatus()
    {
        $arr=$_POST;
        $id=$arr['id'];
        $model = parent::model('purchase', 'sa');
        $result = $model->noStatus($id);
        if (true === $result) {
            $data['code']=1;
            $data['msg']="已驳回";
        }
        else {
            $data['msg']="驳回失败";
        }
        echo json_encode($data);
    }



    public function action_notPurchase()
    {
        $model = parent::model('purchase', 'sa');
        list($total, $data) = $model->notReview(array('page'=>$this->page, 'pagesize'=>$this->pagesize));
        unset($model);
        $model = parent::model('supplier', 'sa');
        foreach ($data as $key => $value) {
            $supplier_name = $model->get_data($value['supplier_id']);
            $data[$key]['supplier_name'] = $supplier_name['supplier_name'];
        }
        unset($model);

        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($total/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$total,
            'showpage'=>XPage::sale($total, $this->pagesize, $this->page, $url, 10),
            'urlitem'=>$this->_urlitem,
            'comeurl'=>$this->_comeurl,
            'tid'=>$this->tid,
            'sname'=>$this->sname,
            'notPurchase'=>$data,
            'category_select'=>$category_select,
            'modtitle'=>$this->modtitle,
            'sname'=>$sname,
            'scatid'=>$scatid,
        );
        // var_dump($var_array['notPurchase']);die;
        TPL::assign($var_array);
        TPL::display($this->cptpl.'purchase/notPurchase.tpl');
    }


    public function action_status()
    {
        $id = XRequest::getArray('id');
        $model = parent::model('purchase', 'sa');
        $result = $model->get_data($id);
        unset($model);
        
        $var_array = array(
          'purchase'=>$result,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'purchase/status.tpl');
    }

    public function action_doStatus()
    {
        $arr=$_POST;
        $id=$arr['id'];
        $model = parent::model('purchase', 'sa');
        $result = $model->doStatus($id);
        if (true === $result) {
            $data['code']=1;
            $data['msg']="审核通过";
        }
        else {
            $data['msg']="审核失败";
        }

        echo json_encode($data);
    }

    public function action_storageList()
    {
        $model = parent::model('purchase', 'sa');
        list($total, $data) = $model->storage(array('page'=>$this->page, 'pagesize'=>$this->pagesize));
        unset($model);
        $model = parent::model('supplier', 'sa');
        foreach ($data as $key => $value) {
            $supplier_name = $model->get_data($value['supplier_id']);
            $data[$key]['supplier_name'] = $supplier_name['supplier_name'];
        }
        unset($model);

        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($total/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$total,
            'showpage'=>XPage::sale($total, $this->pagesize, $this->page, $url, 10),
            'urlitem'=>$this->_urlitem,
            'comeurl'=>$this->_comeurl,
            'tid'=>$this->tid,
            'sname'=>$this->sname,
            'storage'=>$data,
            'category_select'=>$category_select,
            'modtitle'=>$this->modtitle,
            'sname'=>$sname,
            'scatid'=>$scatid,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'purchase/storageList.tpl');
    }

    public function action_storage()
    {
        $id=$_GET['id'];
        $model = parent::model('purchase', 'sa');
        $result = $model->get_data($id);
        unset($model);

        $var_array = array(
          'purchase'=>$result,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'purchase/storage.tpl');
    }


    public function action_doStorage()
    {
        $array=$_POST;
        $model = parent::model('purchase', 'sa');
        $result = $model->doStorage($array);
        unset($model);



        if (true === $result) {
            $data['code']=1;
            $data['msg']="收货成功";
        }
        else {
            $data['msg']="收货失败";
        }

        echo json_encode($data);
    }


    public function action_endPurchase()
    {
        $model = parent::model('purchase', 'sa');
        list($total, $data) = $model->endPurchase(array('page'=>$this->page, 'pagesize'=>$this->pagesize));
        unset($model);
        $model = parent::model('supplier', 'sa');
        foreach ($data as $key => $value) {
            $supplier_name = $model->get_data($value['supplier_id']);
            $data[$key]['supplier_name'] = $supplier_name['supplier_name'];
        }
        unset($model);

        $var_array = array(
            'page'=>$this->page,
            'nextpage'=>$this->page+1,
            'prepage'=>$this->page-1,
            'pagecount'=>ceil($total/$this->pagesize),
            'pagesize'=>$this->pagesize,
            'total'=>$total,
            'showpage'=>XPage::sale($total, $this->pagesize, $this->page, $url, 10),
            'urlitem'=>$this->_urlitem,
            'comeurl'=>$this->_comeurl,
            'tid'=>$this->tid,
            'sname'=>$this->sname,
            'storage'=>$data,
            'category_select'=>$category_select,
            'modtitle'=>$this->modtitle,
            'sname'=>$sname,
            'scatid'=>$scatid,
        );
        TPL::assign($var_array);
        TPL::display($this->cptpl.'purchase/endPurchase.tpl');
    }

    public function action_del(){
            $ids = XRequest::getArray('id');
            $ids = substr($ids,0,strlen($ids)-1); ;
            $array_id = explode(",", $ids);
            if(empty($array_id)){
                 $data['msg']='操作失败';
                 echo json_encode($data);
                die;
            }
            $model = parent::model('purchase', 'sa');
            foreach($array_id as $v){
               $res=$model->del($v);
            }

            if($res){
                 $data['code']=1;
                 $data['msg']='删除成功';
            } else {
                 $data['msg']='操作失败';
            }
             echo json_encode($data);
            die;
}
}