<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 14-5-19
 * Time: 下午5:13
 * To change this template use File | Settings | File Templates.
 */
class Ship_TaxRebateController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "ship/views/";
        //$this->serviceClass = new Service_TaxRebate();
    }

    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"
            );

            $taxrebateCode = trim($this->_request->getParam("taxrebate_code",""));
            $uploadTimeStart = $this->_request->getParam("upload_time_start","");
            $uploadTimeEnd = $this->_request->getParam("upload_time_end","");
            $customerCode = trim($this->_request->getParam("customer_code",""));
            $tr_type = $this->_request->getParam("tr_type","");
            //$filename = $this->_request->getParam("filename","");
            //$sessionData = $this->customerAuth->data;
            $condition = array(
                'taxrebate_code'=>$taxrebateCode,
                'customer_code'=>$customerCode,
                'upload_time_start'=>$uploadTimeStart,
                'upload_time_end'=>$uploadTimeEnd,
                'tr_type'=>$tr_type
                //'filename'=>$filename,
            );
            $count = Service_TaxRebate::getByCondition($condition,"count(*)");
            $return['total'] = $count;
            if($count){
                $rows = Service_TaxRebate::getByCondition($condition,'*', $pageSize, $page, array('tr_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }
        echo Ec::renderTpl($this->tplDirectory . "tax-rebate-list.tpl", 'layout');
    }

    public function addAction(){
        $customer_code = $this->_request->getParam("customer_code","");
        $taxrebate_code = $this->_request->getParam("taxrebate_code","");
        $result = array(
            'state'=>'0',
            'message'=>''
        );
        $row = array();
        if(empty($customer_code)){
            $result['message'] = '客户代码必须填';
            die(Zend_Json::encode($result));
        }else{
            $customerRow = Service_Customer::getByField($customer_code,'customer_code',"*");
            if(empty($customerRow)){
                $result['message'] = '客户代码不存在';
                die(Zend_Json::encode($result));
            }else{
                $row['customer_code'] = $customer_code;
            }
        }
        if(empty($taxrebate_code)){
            $result['message'] = '报关单号必须填';
            die(Zend_Json::encode($result));
        }else{
            $existTR = Service_TaxRebate::getByField($taxrebate_code,'taxrebate_code',"*");
            if(!empty($existTR)){
                $result['message'] = '报关单号已存在';
                die(Zend_Json::encode($result));
            }else{
                $row['taxrebate_code'] = $taxrebate_code;
            }
        }
        $imgs	= array();
        $fields	= empty($_FILES) ? '' : array_keys($_FILES);
        $path = APPLICATION_PATH.'/../data/images/taxrebate/'.$customer_code;
        $isDir	= Common_Common::createPath($path);
        if($_FILES['uploadIamgefile']['size']){
            $state	= Common_Common::uploadImg($_FILES, 'uploadIamgefile', $path);
            if ($state['error']) {
                $msg	= strip_tags($state['msg']);

                $result = array(
                    'state'=>'0',
                    'message'=>$msg
                );
                die(Zend_Json::encode($result));
            }
            $row['filename'] = $state['msg']['file_name'];
        }
        $row['upload_time'] = date("Y-m-d H:i:s");
        $row['service_url'] = $_SERVER['SERVER_NAME'];
        $trid = Service_TaxRebate::add($row);
        if($trid){
            $result['state'] = 1;
            $result['message'] = "报关单".$taxrebate_code."上传成功！";
        }else{
            $result['message'] = "报关单".$taxrebate_code."上传失败！";
        }
        die(json_encode($result));
    }

    public function taxRebateImageAction(){
        $trId = $this->_request->getParam("tr_id","");
        $trRow = Service_TaxRebate::getByField($trId,'tr_id',"*");
        header("Location: http://".$trRow['service_url']."/default/index/tax-rebate-image/tr_id/" . $trId);
        exit();
    }

    public function downloadImageAction(){
        $trId = $this->_request->getParam("tr_id","");
        $trRow = Service_TaxRebate::getByField($trId,'tr_id',"*");
        header("Location: http://".$trRow['service_url']."/default/index/download-tax-image/tr_id/" . $trId);
        exit();
    }

    public function deleteImageAction(){
        $trId = $this->_request->getParam("tr_id","");
        $trRow = Service_TaxRebate::getByField($trId,'tr_id',"*");
        $fullPath = APPLICATION_PATH.'/../data/images/taxrebate/'.$trRow['customer_code']."/".$trRow['filename'] ;
        if($trRow['filename']==""){
            $result = array(
                'state'=>'0',
                'message'=>'文件不存在'
            );
        }else{
            if(file_exists($fullPath)){
                $bool = unlink($fullPath);
                if($bool){
                    $updateRow = array(
                        'filename'=>"",
                    );
                    Service_TaxRebate::update($updateRow,$trId,'tr_id');
                    $result = array(
                        'state'=>'1',
                        'message'=>'删除成功'
                    );
                }else{
                    $result = array(
                        'state'=>'0',
                        'message'=>'删除失败'
                    );
                }
            }else{
                $result = array(
                    'state'=>'0',
                    'message'=>'文件不存在'
                );
            }
        }
        die(json_encode($result));
    }

    public function deleteAction(){
        $trId = $this->_request->getParam("tr_id","");
        Service_TaxRebate::delete($trId,"tr_id");
        $result = array(
            'state'=>'1',
            'message'=>'删除成功'
        );
        die(json_encode($result));
    }
}
