<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-10-14
 * Time: 下午4:53
 * To change this template use File | Settings | File Templates.
 */
class Customer_CustomerWarehouseController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "customer/views/default/";
        $this->serviceClass = new Service_CustomerWarehouse();
    }

    public function listAction()
    {
        if ($this->_request->isPost()) {
            $page = $this->_request->getParam('page', 1);
            $pageSize = $this->_request->getParam('pageSize', 20);

            $page = $page ? $page : 1;
            $pageSize = $pageSize ? $pageSize : 20;

            $return = array(
                "state" => 0,
                "message" => "No Data"
            );

            $params = $this->_request->getParams();
            $condition = $this->serviceClass->getMatchFields($params);

            $count = $this->serviceClass->getByCondition($condition, 'count(*)');
            $return['total'] = $count;

            if ($count) {
                $showFields = array(
                    'cw_id',
                    'customer_code',
                    'warehouse_id',
                    'warehouse_area',
                    'warehouse_time',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition, $showFields, $pageSize, $page);
                foreach($rows as $key=>$value){
                    $warehouse = Service_Warehouse::getByField($value['E2'],"warehouse_id","*");
                    $customer_company_name = Service_Customer::getByField($value['E1'], "customer_code", "customer_company_name");
                    $rows[$key]['warehouse_code'] = $warehouse['warehouse_code'];
                    $rows[$key]['customer_company_name'] = $customer_company_name['customer_company_name'];
                }
                $return['data'] = $rows;
                $return['state'] = 1;
                //$return['warehouses'] = $warehouses;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        $warehouses = Service_Warehouse::getAll();
        $this->view->warehouses = $warehouses;

        echo Ec::renderTpl($this->tplDirectory . "customer_warehouse_index.tpl", 'layout');
    }

    public function editAction()
    {
        $return = array(
            'state' => 0,
            'message' => '',
            'errorMessage' => array('Fail.')
        );
        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            $row = array(
                'cw_id'=>'',
                'customer_code'=>'',
                'warehouse_id' => '',
                'warehouse_area' => '',
                'warehouse_time' => '',
            );
            $row = $this->serviceClass->getMatchEditFields($params, $row);
            $paramId = $row['cw_id'];
            if (!empty($row['cw_id'])) {
                unset($row['cw_id']);
            }
            $errorArr = $this->serviceClass->validator($row);
            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message' => '',
                    'errorMessage' => $errorArr
                );
                die(Zend_Json::encode($return));
            }
            $row['customer_code'] = strtoupper($row['customer_code']);
            $customer = Service_Customer::getByField($row['customer_code'],'customer_code');
            if(empty($customer)){
                $errorArr[] = array("客户{$row['customer_code']}不存在");
            }
            if(!is_numeric($row['warehouse_area'])){
                $errorArr[] = array('面积必须是数字');
            }
            if($paramId){
                $condition = array(
                    'customer_code'=>$row['customer_code'],
                    'warehouse_id'=>$row['warehouse_id'],
                );
                $customerWarehouse = Service_CustomerWarehouse::getByCondition($condition,'*',1,1);
                if(!empty($customerWarehouse)){
                    if(!($row['customer_code']==$customerWarehouse[0]['customer_code']) && ($row['warehouse_id']==$customerWarehouse[0]['warehouse_id'])){
                        $errorArr[] = array('该客户该仓库已经设置');
                    }
                }
            }else{
                $condition = array(
                    'customer_code'=>$row['customer_code'],
                    'warehouse_id'=>$row['warehouse_id'],
                );
                $customerWarehouse = Service_CustomerWarehouse::getByCondition($condition,'*',1,1);
                if(!empty($customerWarehouse)){
                    $errorArr[] = array('该客户该仓库已经设置');
                }
            }
            if (!empty($errorArr)) {
                $return = array(
                    'state' => 0,
                    'message' => '',
                    'errorMessage' => $errorArr
                );
                die(Zend_Json::encode($return));
            }
            if (!empty($paramId)) {
                $result = $this->serviceClass->update($row, $paramId);
            } else {
                $result = $this->serviceClass->add($row);
            }
            if ($result) {
                $return['state'] = 1;
                $return['message'] = array('Success.');
            }
            die(Zend_Json::encode($return));
        }
        $paramId = $this->_request->getParam('paramId', '0');
        $isEdit = 0;
        if (!empty($paramId)) {
            $customerWarehouse = Service_CustomerWarehouse::getByField($paramId);
            if(!empty($customerWarehouse)){
                $isEdit =1;
            }
            $this->view->customerwarehouse=$customerWarehouse;
        }
        $warehouses = Service_Warehouse::getByCondition(array(),'*');
        $this->view->isEdit = $isEdit;
        $this->view->warehouses = $warehouses;
        echo Ec::renderTpl($this->tplDirectory . "customer-warehouse-edit.tpl", 'layout');
    }

    public function getByJsonAction()
    {
        $result = array('state' => 0, 'message' => 'Fail', 'data' => array());
        $paramId = $this->_request->getParam('paramId', '');
        if (!empty($paramId) && $rows = $this->serviceClass->getByField($paramId, 'customer_id')) {
            $rows = $this->serviceClass->getVirtualFields($rows);
            $result = array('state' => 1, 'message' => '', 'data' => $rows);
        }
        die(Zend_Json::encode($result));
    }
    public function deleteAction()
    {
        $result = array(
            "state" => 0,
            "message" => "Fail."
        );
        if ($this->_request->isPost()) {
            $paramId = $this->_request->getPost('paramId');
            if (!empty($paramId)) {
                if ($this->serviceClass->delete($paramId)) {
                    $result['state'] = 1;
                    $result['message'] = 'Success.';
                }
            }
        }
        die(Zend_Json::encode($result));
    }
}
