<?php
class Shipment_ShipBatchDetailController extends Ec_Controller_Action
{
    public function preDispatch()
    {
        $this->tplDirectory = "shipment/views/order/";
        $this->serviceClass = new Service_ShipBatchDetail();
    }
	/*出货总单管理*/
    public function listAction()
    {
        $deliveryCode = $this->_request->getParam('deliveryCode', '');
        $shipBatch = Service_ShipBatch::getByField($deliveryCode,"sb_code","*");
        $warehouse = Service_Warehouse::getByField($shipBatch['warehouse_id'],"warehouse_id","*");
        $user = Service_User::getByField($shipBatch['creater_id'],'user_id','*');
        $shipBatch['user_code'] = $user['user_code'];
        $shipBatch['warehouse_code'] = $warehouse['warehouse_code'];
        $this->view->shipBatch = $shipBatch;
        $this->view->shipBatchStatus = Common_Type::shipBatchStatus();
        $shipbatchAttribute = Service_ShipBatchAttribute::getByField($deliveryCode,'sb_code');
        $this->view->shipBatchAttribute = $shipbatchAttribute;
        $formtype=Service_FormType::getByField($shipbatchAttribute['form_type'],'form_type');
        $this->view->formtype=$formtype;
        $trade=Service_TradeMode::getByField($shipbatchAttribute['trade_mode'],'trade_mode');

        $this->view->trade=$trade['trade_mode_name'];

       // var_dump($shipbatchAttribute);
        /*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(

                    'sb_code',
                    'ob_no',
                    'creater_id',
                    'ob_weight',
                    'sbd_add_time',
                    'sbd_id',
                );
                $showFields = $this->serviceClass->getFieldsAlias($showFields);
                $rows = $this->serviceClass->getByCondition($condition, $showFields, $pageSize, $page, array('sbd_id desc'));
                $return['data'] = $rows;
                $return['state'] = 1;
                $return['message'] = "";
            }
            die(Zend_Json::encode($return));
        }*/
        $package = Service_ShipBatchDetail::getByCondition(array('sb_code'=>$deliveryCode),"*");
        $obNoArr = array();
        $ob_weight = 0;
        foreach($package as $key=>$value){
            $ob_weight += $value['ob_weight'];
            $user = Service_User::getByField($value['creater_id'],"user_id","*");
            $package[$key]['user_code'] = $user['user_code'];
            if(!in_array($value['ob_no'],$obNoArr))
                $obNoArr[] = $value['ob_no'];
        }
        $shipOrder = array();
        foreach($obNoArr as $v){
            $outbound_bag_detail = Service_OutboundBagDetail::getByCondition(array('ob_no'=>$v),"*");
            foreach($outbound_bag_detail as $key=>$value){
                $soRow = Service_ShipOrder::getByField($value['so_code'],"so_code","*");
                $warehouse = Service_Warehouse::getByField($soRow['warehouse_id'],'warehouse_id',"*");
                $soRow['warehouse_code'] = $warehouse['warehouse_code'];
				$order_code = $soRow['order_code'];	
				$orderRow = Service_Orders::getByField($order_code, 'order_code');
				$soRow['order_currency_code'] = $orderRow['currency_code']; 
				$soRow['so_declared_value'] = $orderRow['parcel_declared_value']; 
				$customer_id =  $orderRow['customer_id'];
				$customer_array = Service_Customer::getByField($customer_id,'customer_id');				
				$soRow['customer_currency_code'] = $customer_array['customer_currency'];				
				
				
                $shipOrder[] = $soRow;
            }
        }
        $shipBatchlog = Service_ShipBatchLog::getByCondition(array('sb_code'=>$deliveryCode),"*");
        foreach($shipBatchlog as $key=>$value){
            if($value['user_id']!="-1"){
                $user = Service_User::getByField($value['user_id'],'user_id',"*");
                $shipBatchlog[$key]['user_code'] = $user['user_code'];
            }
        }
        $this->view->shipBatchLog = $shipBatchlog;
        $this->view->shipOrder = $shipOrder;
        $this->view->package = $package;
        $this->view->deliveryCode = $deliveryCode;
        $this->view->obWeight = $ob_weight;        
        echo Ec::renderTpl($this->tplDirectory . "ship_batch_detail_index.tpl", 'layout');
    }
    /**
     * @author william-fan
     * @todo 用于修改毛净重
     */
    public function editweightAction(){
        $result = array(
            'ask'=>'0',
            'message'=>'修改失败'
        );
        $db = Common_Common::getAdapter();
        $db->beginTransaction();
        $userAuth = new Zend_Session_Namespace('userAuthorization');
        $user_id = $userAuth->userId;
        try{
            //print_r($this->_request->getParams());
            $sb_code = $this->_request->getParam('sb_code','');
            $gross_wt = $this->_request->getParam('gross_wt','');
            $net_wt = $this->_request->getParam('net_wt','');

            $shipBatch = Service_ShipBatch::getByField($sb_code,'sb_code');
            if($shipBatch['sb_status']!='0'){
                throw new Exception('出区单状态不为草稿不能修改');
            }
            if(!($gross_wt>0 && $net_wt>0)){
                throw new Exception('毛重净重都要大于0');
            }
            if($gross_wt<=$net_wt){
                throw new Exception('毛重小于净重');
            }
            $attributeRow = array(
                'gross_wt'=>$gross_wt,
                'net_wt'=>$net_wt
            );
            if(Service_ShipBatchAttribute::update($attributeRow,$sb_code,'sb_code')){
                $shipBatchLogRow = array(
                    'sb_id'=>$shipBatch['sb_id'],
                    'sb_code'=>$shipBatch['sb_code'],
                    'sbl_type'=>'0',
                    'user_id'=>$user_id,
                    'sbl_status_from'=>$shipBatch['sb_status'],
                    'sbl_status_to'=>$shipBatch['sb_status'],
                    'sbl_note'=>"修改毛重{$gross_wt}净重{$net_wt}",
                    'sbl_add_time'=>date("Y-m-d H:i:s",time()),
                    'sbl_ip'=>Common_Common::getIP(),
                );
                Service_ShipBatchLog::add($shipBatchLogRow);
            }else{
                throw new Exception('未更新或者更新出错');
            }
            $db->commit();
            //$db->rollback();
            $result['ask'] = '1';
            $result['message'] = '净重毛重修改成功！';
        }catch (Exception $e){
            $db->rollback();
            $result['message'] = $e->getMessage();
        }
        die(json_encode($result));
    }

}