<?php
class Ceive_UseBackBatchController extends Ec_Controller_Action
{
	public function preDispatch()
	{
		$this->tplDirectory = "ceive/views/ceive_use/";
		$this->serviceClass = new Service_CeiveUseBackBatch();
	}

	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(

						'cubb_code',
						'cu_code',
						'cud_id',
						'cubb_status',
						'cubb_username',
						'cubb_add_user_id',
						'product_id',
						'product_barcode',
						'cubb_back_quantity',
						'cubb_add_time',
				);
				$showFields = $this->serviceClass->getFieldsAlias($showFields);
				$rows = $this->serviceClass->getByCondition($condition,$showFields, $pageSize, $page, array('cud_id desc'));
				
				$return['batchStatus'] = Common_Status::backBatchStatus('auto');
				$return['data'] = $rows;
				$return['state'] = 1;
				$return['message'] = "";
			}
			die(Zend_Json::encode($return));
		}
		echo Ec::renderTpl($this->tplDirectory . "ceive_use_back_batch_index.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, 'cud_id')) {
			$rows=$this->serviceClass->getVirtualFields($rows);
			$result = array('state' => 1, 'message' => '', 'data' => $rows);
		}
		die(Zend_Json::encode($result));
	}
	/**
	 * @author william-fan
	 * @todo 扫描归还单号
	 */
	public function scanghAction(){
		if ($this->_request->isPost()) {
			$cubb_code = $this->_request->getParam('cubb_code','');
			$result = Service_CeiveUseProcess::scanProcess($cubb_code);
			
			die(Zend_Json::encode($result));
		}
		echo Ec::renderTpl($this->tplDirectory . "scan_back.tpl", 'layout');
	}

}	
	