<?php
class Merchant_OrderUploadController extends Ec_Controller_Action{
	public function preDispatch ()
	{
		$this->tplDirectory = "merchant/views/order/";
		// $this->tplDirectory = "merchant/account_config/";
		$this->customerAuth = new Zend_Session_Namespace("customerAuth");
	}
	/**
	 * @author william-fan
	 * @todo 用于下载订单导入的模板
	 */
	public function downOrderTempleteAction(){
		$file = $this->_request->getParam('file','');
        $type = 1;
        $fileType = 'xlsx';//注意excel2005的插件不能生成下拉菜单
		switch($file){
			case 'orderupload':
                $type = 2;//横排
				break;
            case 'orderuploadproduct':
                $type = 1;//竖排
                break;
		}
        $init = array(
            'template' => 'order',
            'type' => $type,
            'excelType' => 'xlsx',
        );
        $obj = new Common_ExcelTemplate( $init );
        $fullPath = $obj->getTemplate();
        if( empty( $fullPath ) ) {
            die('模板文件不存在');
        }

		//方法2
		$filename = 'order_batch_upload_template.'.$fileType;
		header("Content-Type: APPLICATION/OCTET-STREAM");
		//Force the download
		$header="Content-Disposition: attachment; filename=".$filename.";";
		header($header );
		// 	header("Content-Transfer-Encoding: binary");
		// 	header("Content-Length: ".$len);
		echo file_get_contents($fullPath);
		exit;
	}
	/**
	 * @author william-fan
	 * @todo 用于订单的导入
	 */
	public function batchOrderAction(){
		$customer = $this->_customerAuth;
		$customerId = $customer['id'];
		//print_r($customer);
		$customerId = $customer['id'];
		echo Ec::renderTpl($this->tplDirectory . "order-batch.tpl",'noleftlayout');
	}
	/**
	 * @author william-fan
	 * @todo 的订单导入检查
	 */
	public function batchcheckAction(){
	    set_time_limit(0);
	    ini_set('memory_limit','1024M');
		//header('Content-type:text/html;charset=utf-8');
		//set_time_limit(300);
		$orderUpload = new Zend_Session_Namespace('orderUpload');
		$field = 'orderFile';
		$xlsData = Service_OrderUpload::getXLSData($_FILES, $field);
		if(isset($xlsData['ask']) && $xlsData['ask']=='0'){
			//echo 'sss';
			$this->view->uploadinfo = $xlsData;
			echo Ec::renderTpl($this->tplDirectory . "order-batch.tpl",'noleftlayout');
		}else{
            $init = array(
                'template'=>'order',
                'type'=>1,
            );
            $obj = new Common_ExcelTemplate( $init );
            $columns = $obj->getOrderColumns();
		    $is_vertical = ( count( $columns ) == count( array_filter( $xlsData[0] ) ) ) ? TRUE : FALSE; // 是否是SKU竖排上传模式
			$orderRows = Service_OrderUpload::getDataArrByKey($xlsData, $is_vertical);
			
			$orderUpload->uploadData = $orderRows;
			$doupload=true;
			if(!empty($orderRows)){
				foreach($orderRows as $key=>$order){
					if($order['is_valid']=='0'){
						$doupload = false;
					}
				}
			}
			$this->view->doupload=false;
			$this->view->uploadData=$orderRows;
			
			echo Ec::renderTpl($this->tplDirectory . "order-upload-edit.tpl",'noleftlayout');
		}
	}
	/**
	 * @author william-fan
	 * @todo 用于订单导入
	 */
	public function importAction(){
		set_time_limit(0);
		ini_set('memory_limit','1024M');
		
		$orderUpload = new Zend_Session_Namespace('orderUpload');
		$sessionData = $orderUpload->uploadData;
		$ids = $this->_request->getParam('select');
		$process = new Service_OrderProcess();
		
		$success =0;
		$false =0;
		$successHtml = '';
		$failseHtml = '';
		$html = '';
		$index=1;
		$doupload = true;
		foreach($sessionData as $key=>$order){
			if($order['is_valid']=='0'){
				$doupload = false;
				$html = '上传订单中有不符合要求的订单,已终止上传';
			}
		}
		if($doupload){
			foreach ($ids as $val){
					
				$status = 1;
				$orderRow = $sessionData[$val];
				$result = $process->createOrderTransaction($orderRow);
				if($result['ask']=='1'){
					$successHtml.="<p>第{$index}个订单".$result['ordersCode'].'创建成功'.'</p>';
                                        if($result['messageTip']){
                                            $successHtml.=$result['messageTip'];
                                        }
                                        $successHtml.='</p>';
					$success++;
				}else{
					$failseHtml.="<p>第{$index}个订单".$result['message'];
					if(isset($result['error'])){
						$error = $result['error'];
						if(!empty($error)){
							foreach($error as $key=>$err){
								$failseHtml.="<div class='error'>{$err}</div>";
							}
						}
					}
					$failseHtml.='</p>';
					$false++;
				}
				$index++;
			}
			$uploadResult = "<p>成功{$success}失败$false</p>";
			$html = $html.$successHtml.$failseHtml.$uploadResult;
		}

		$this->view->uploadResult=$html;
		unset($orderUpload->uploadData);
		echo Ec::renderTpl($this->tplDirectory . "order-batch.tpl",'noleftlayout');
	}
	
	
}