<?php

/**
 * Created by PhpStorm.
 * User: william
 * Date: 2020/12/12
 * Time: 14:05
 * 目前海关暂时不允许从电商账册直接做5034退港出区,需要从电商张转到保税账里面才可以
 */
class Service_ShipBatch_ShipInvProcess{
    private $sbArr;
    private $products;
    private $orderList;
    private $exportList;
    private $tuigangHzList; //退港核注清单
    private $splitNumber = 50;
    private $tuigang;
    private $bilInfo = array(
        'ex_bill_no' => 'T5225W000064',//转出的电商账册
        'im_bill_no' => 'T5225W000063',//转入的保税账册
    );
    public function setBillInfo($bwNo){
        //象嵘的账册
        if($bwNo =='T5225W000064'){
            $this->bilInfo =[
                'ex_bill_no' => 'T5225W000064',//转出的电商账册
                'im_bill_no' => 'T5225W000063',//转入的物流账册
            ];
        }else{
            //斑鹿的账册
            $this->bilInfo =[
                'ex_bill_no' => 'T5225W000046',//转出的电商账册
                'im_bill_no' => 'T5225W000031',//转入的物流账册
            ];
        }
    }
    private $tax_method = '3';

    public function __construct($sbArr) {
        $this->setSb($sbArr);
    }

    public function setSb($sbArr) {
        $this->sbArr = $sbArr;
    }
    public function setOrderList($orderList){
        $this->orderList = $orderList;
    }
    public function getOrderList(){
        return $this->orderList;
    }
    /**
     * @todo 区内调拨获取调拨账册信息
     */
  
    public function getBillInfo() {
        return $this->bilInfo;
    }
    /**
     * @todo 获取订单号
     */
    public function getSbOrders() {
        $orderList = array();
        if (!empty($this->sbArr)) {
            foreach ($this->sbArr as $k => $sbCode) {
                $shipbatch = Service_ShipBatch::getByField($sbCode, 'sb_code');
                if (!empty($shipbatch)) {
                    $sbCondition = array(
                        'sb_code' => $sbCode
                    );
                    $sbRows = Service_ShipBatchDetail::getByCondition($sbCondition, '*');
                    if (!empty($sbRows)) {
                        foreach ($sbRows as $ks => $row) {
                            $obCondition = array(
                                "ob_no" => $row['ob_no']
                            );
                            $outDetails = Service_OutboundBagDetail::getByCondition($obCondition, "*");
                            foreach ($outDetails as $ko => $odRow) {
                                $orderList[] = $odRow['order_code'];
                            }
                        }
                    }
                }
            }
        }
        $this->orderList = $orderList;
        /*echo 'fffff';
        print_r($this->orderList);exit;*/
        return $orderList;
    }
    /**
     * Note:获取转入的数据
     * User: Yao
     * Date: 2020/12/18
     * Time: 14:12
     * @throws Exception
     */
    public function getExportImByOrderList(){
        try {
            $this->getProducts(); //获取商品统计
            $this->getProductsDel(); //将商品信息在物流账册有库存的情况下剥离出去
            $this->getExportImDetail(); //获取核注详情
        } catch (Exception $e) {
            throw $e;
        }
    }
    /**
     * Note:获取转出的数据
     * User: Yao
     * Date: 2020/12/18
     * Time: 14:12
     * @throws Exception
     */
    public function getExportExByOrderList(){
        try {
            $this->getProducts(); //获取商品统计
            $this->getProductsDel(); //将商品信息在物流账册有库存的情况下剥离出去
            $this->getExportExDetail(); //获取核注详情
        } catch (Exception $e) {
            throw $e;
        }
    }
    /**
     * @todo 获取转入数据
     */
    public function getExportIm() {
        try {
            $this->getSbOrders(); //获取出货单订单
            $this->getProducts(); //获取商品统计
            $this->getProductsDel(); //将商品信息在物流账册有库存的情况下剥离出去
            $this->getExportImDetail(); //获取核注详情
        } catch (Exception $e) {
    
           throw $e;
        }
    }

    /**
     * @todo 获取转出核注数据
     */
    public function getExportEx() {
        try {
            $this->getSbOrders(); //获取出货单订单
            $this->getProducts(); //获取商品统计
            $this->getProductsDel(); //将商品信息在物流账册有库存的情况下剥离出去
            $this->getExportExDetail(); //获取核注详情
        } catch (Exception $e) {
        
           throw $e;
        }
    }

    /**
     * @todo 获取退港核注清单
     */
    public function getTuigangHz(){
        try{
            $this->getSbOrders(); //获取出货单订单
            $this->getProducts(); //获取商品统计
            $this->geTuigangDetail(); //获取核注详情(会拆分)
        }catch (Exception $e){
            echo $e->getMessage();
        }
    }
    /**
     * @todo 获取导出的数据
     */
    public function getExportList() {
        return $this->exportList;
    }
    public function getDataBykey($key){
        return $this->$key;
    }
    /**
     * @todo 获取产品的账册信息(转出方账册信息)
     */
    public function getBillNO($product_id, $op_quantity) {
        //var_dump($product_id);
        //exit;
        $product = Service_Product::getByField($product_id);
        if($product['customer_code']=='C0421'){
            //修复421账册到进口里面了
            $condition = array(
                'product_sku'=>$product['product_sku'],
                'customer_code'=>'C0426'
            );
            $ps = Service_Product::getByCondition($condition,"*",1,1);
            if(count($ps)>1){
                throw new Exception("商品".$product['product_sku']."转出C0426,不止一个商品");
            }
            $p = $ps[0];
            $product_id = $p['product_id'];
            $product = $p;
        }
        $billBookProudct = Service_BillBookProduct::getBillRecord($product_id);
        //var_dump($billBookProudct);
        $account_no = $this->bilInfo['ex_bill_no'];
        $recordeNo = '';
        $result = array(
            'state' => 0,
            'message' => '',
            'error' => array(),
            'data' => array(),
            'product' => [], //带出商品以便有错误时候能显示
        );

        if (empty($billBookProudct)) {
            $error[] = $product['product_sku'] . '转出方账册信息不存在'.$this->bilInfo['ex_bill_no'];
        }
        //查询法定数量
        $hum = Service_HsUomMap::getByField($product_id, 'product_id');
        if (empty($hum)) {
            $error[] = $product['product_sku'] . '法定数量信息不存在';
        }
        $declare_value = $product['product_declared_value'];
        $currency = Service_Currency::getByField($product['currency_code'], 'currency_code');
        if ($declare_value <= 0) {
            $error[] = '商品申报价值小于0';
        }
        if ($product['currency_code']) {
            $currency = Service_Currency::getByField($product['currency_code'], 'currency_code');
            if ($currency['currency_hs_code'] != '') {
                $product['currency_hs_code'] = $currency['currency_hs_code'];
            }
        }
        $result['product'] = $product;
        $data = []; //返回核扣的数据
        /*print_r($billBookProudct);
        exit;*/

        if ($billBookProudct) {
            $billBookId = array();
            foreach ($billBookProudct as $bill) {
                $billBookId[] = $bill['bill_book_id'];
            }
            //$gt = $op_quantity -1;
            $gt = 0.1; //大于0的账册从小到达排序,然后
            $billBooks = Service_BillBook::getByCondition(array(
                'id_array' => $billBookId,
                'warehous_account_no' => $account_no,
                'state'=>'0',
                'can_use_quantity_gt' => $gt
            ), '*', '0', '0', array('can_use_quantity asc '));
            if (empty($billBooks)) {
                $error[] = $product['product_sku'] . '账册信息不存在'.$account_no;
            }
            $hekouNumber = $op_quantity; //总剩余核扣账册库存，初始化等于数量
            //var_dump($billBooks);
            foreach ($billBooks as $kb => $rb) {
                //$info = array();
                $billBook = Service_BillBook::getByIdForUpdate($rb['id'], 'id', '*');
                //var_dump($billBook);exit;
                $info = $billBook;
                $info['product_id'] = $product_id; //初始化为传的参数，bill_book的product_id存在一品多个商品关联情况不准确
                $info['product_sku'] = $product['product_sku'];
                $info['tax_method'] = $this->tax_method;
                $info['hum'] = $hum;
                if($hekouNumber == 0){
                    break;
                }
                if ($billBook['can_use_quantity'] >= $hekouNumber) {
                    $info['qty'] = $hekouNumber;
                    $info['qty1'] = sprintf("%.5f",$hekouNumber * $hum['hum_quantity_law']);
                    $info['qty2'] = sprintf("%.5f",$hekouNumber * $hum['hum_quantity_second']);
                    $info['zc_declareprice'] = $billBook['decalar_price'];
                    $info['zc_decalar_currency'] = $billBook['decalar_currency'];
                    $hekouNumber = $hekouNumber - $info['qty'];
                    $data[] = $info;
                    break; //库存够了跳出循环
                } else {
                    // $op_quantity = $op_quantity - $billBook['can_use_quantity'];
                    $info['qty'] = $billBook['can_use_quantity']; //库存不够把这个备案序号的库存全出完
                    $info['tax_method'] = $this->tax_method;
                    $info['qty1'] = sprintf("%.5f",$info['qty'] * $hum['hum_quantity_law']);
                    $info['qty2'] = sprintf("%.5f",$info['qty'] * $hum['hum_quantity_second']);
                    $info['zc_declareprice'] = $billBook['decalar_price'];
                    $info['zc_decalar_currency'] = $billBook['decalar_currency'];
                    $hekouNumber = $hekouNumber - $info['qty'];
                    $data[] = $info;
                }
            }
            if ($hekouNumber != 0) {
                $error[] = $product['product_sku'] . "账册库存不够核扣";
            }
        }
        if (!empty($error)) {
            $result['message'] = "库存获取有问题";
            $result['error'] = $error;
        } else {
            $result['data'] = $data;
            $result['message'] = "获取数据成功";
            $result['state'] = 1;
        }
        /*echo "result";
        print_r($result);*/
        return $result;
    }

    /**
     * @todo 根据转出方信息获取转入方信息
     * @row 是转入方信息的数组
     */
    public function getImBillNO($row) {
        $result = array(
            'state' => 0,
            'message' => '',
            'error' => array(),
            'data' => array(),
            'product' => [],
        );
        if (isset($row['product'])) {
            $result['product'] = $row['product'];
        }
        //print_r($row);
        //($row);
        $error = []; //错误信息
        if ($row['state'] == 1) {
            $account_no = $this->bilInfo['im_bill_no'];
            //转出校验成功,开始做转入
            $product = $row['product'];
            /*print_r($product);
            exit;*/
            $product_id = $product['product_id'];

            if ($product_id > 0) {
                $billBookProudct = Service_BillBookProduct::getBillRecord($product_id);
                $bookFlag = false; //转入到转出的账册，转出账册是否有库存标志，如果有就转入到该账册里面，避免产生过多的备案序号
                if ($billBookProudct) {
                    $billBookId = array();
                    foreach ($billBookProudct as $bill) {
                        $billBookId[] = $bill['bill_book_id'];
                    }
                    $billBook = Service_BillBook::getByCondition(array(
                        'id_array' => $billBookId,
                        'warehous_account_no' => $account_no,
                        'state'=>'0',
                        //'can_use_quantity_gt'=>0
                    ), '*', '1', '1', array('stock_quantity desc')); //取最大库存
                    if (!empty($billBook)) {
                        $bookFlag = true; //存在转入放的账册库存
                    }
                }
                $dataIms = array(); //转入方数据
                //获取转入方的账册数据和校验
                if ($bookFlag) {
                    //转入方有账册
                    $billBook = $billBook[0];
                    $data = $row['data']; //转出方数据

                    foreach ($data as $kd => $vd) {
                        if ($vd['hs_code'] != $billBook['hs_code']) {
                            $error[] = "转出方商品:" . $vd['product_sku'] . "商品海关编码:" . $vd['hs_code'] . "和转入方海关编码" . $billBook['hs_code'] . "不一致";
                        }
                        if ($vd['country_code'] != $billBook['country_code']) {
                            $error[] = "转出方商品:" . $vd['product_sku'] . "商品海关编码:" . $vd['hs_code'] . "和转入方海关编码" . $billBook['hs_code'] . "不一致";
                        }
                        $dataIm = $billBook;
                        $dataIm['recorde_no_info'] = $billBook['recorde_no']; //转入方有账册都转入到这个账册序号下,变个键值,避免转出方的键值
                        $dataIm['qty'] = $vd['qty'];
                        $dataIm['qty1'] = sprintf("%.5f",$vd['qty'] * $vd['hum']['hum_quantity_law']);
                        $dataIm['qty2'] = sprintf("%.5f",$vd['qty'] * $vd['hum']['hum_quantity_second']);

                        $dataIm['zc_declareprice'] = $vd['zc_declareprice'];
                        $dataIm['zc_decalar_currency'] = $vd['zc_decalar_currency'];

                        $dataIms[] = $dataIm;
                    }
                } else {
                    //转入方没数据
                    $data = $row['data']; //转出方数据
                    foreach ($data as $kd => $vd) {
                        $dataIm = $vd;
                        $dataIm['recorde_no_info'] = ""; //转入方没有账册不填
                        $goods_code = $product['goods_id'];
                        $dataIm['goods_code'] = $goods_code; //没有账册,用商品的料号
                        $dataIm['qty'] = $vd['qty'];
                        $dataIm['qty1'] = sprintf("%.5f",$vd['qty'] * $vd['hum']['hum_quantity_law']);
                        $dataIm['qty2'] = sprintf("%.5f",$vd['qty'] * $vd['hum']['hum_quantity_second']);

                        $dataIm['zc_declareprice'] = $vd['zc_declareprice'];
                        $dataIm['zc_decalar_currency'] = $vd['zc_decalar_currency'];

                        $dataIms[] = $dataIm;
                        //var_dump($dataIms);
                        //exit;
                    }
                }
            } else {
                $error[] = "产品id错误" . $product_id;
            }
            if (!empty($error)) {
                $result['error'] = $error;
            } else {
                $result['state'] = 1; //没有问题
                $result['data'] = $dataIms;
            }
        } else {
            $result['message'] = "转入方数据有问题";
            $result['error'] = $row['error'];
        }
        /*echo "biiiii";
        print_r($result);*/
        //exit;
        return $result;
    }
    /**
     * @todo 获取退港账册信息
     */
    public function getTuigangNo($product_id,$op_quantity){
        $product = Service_Product::getByField($product_id);
        if($product['customer_code']=='C0421'){
            //修复421账册到进口里面了
            $condition = array(
                'product_sku'=>$product['product_sku'],
                'customer_code'=>'C0426'
            );
            $ps = Service_Product::getByCondition($condition,"*",1,1);
            if(count($ps)>1){
                throw new Exception("商品".$product['product_sku']."转出C0426,不止一个商品");
            }
            $p = $ps[0];
            $product_id = $p['product_id'];
            $product = $p;
        }
        $billBookProudct = Service_BillBookProduct::getBillRecord($product_id);
        //$account_no = $this->bilInfo['ex_bill_no'];
        $account_no = $this->bilInfo['im_bill_no'];

        //var_dump($account_no);
        $recordeNo = '';
        $result = array(
            'state'=>0,
            'message'=>'',
            'error' =>array(),
            'data'=>array(),
            'product'=>[], //带出商品以便有错误时候能显示
        );
        if(empty($billBookProudct)){
            $error[] = $product['product_sku'].'转入方账册信息不存在';
        }
        //查询法定数量
        $hum = Service_HsUomMap::getByField($product_id,'product_id');
        if(empty($hum)){
            $error[] = $product['product_sku'].'法定数量信息不存在';
        }
        $declare_value = $product['product_declared_value'];
        $currency = Service_Currency::getByField($product['currency_code'],'currency_code');
        if($declare_value<=0){
            $error[] = '商品申报价值小于0';
        }
        if($product['currency_code']){
            $currency = Service_Currency::getByField($product['currency_code'],'currency_code');
            if($currency['currency_hs_code']!=''){
                $product['currency_hs_code'] = $currency['currency_hs_code'];
            }
        }
        $result['product'] = $product;
        $data=[]; //返回核扣的数据
        //print_r($billBookProudct);
        //exit;

        if ($billBookProudct) {
            $billBookId = array();
            foreach ($billBookProudct as $bill) {
                $billBookId[] = $bill['bill_book_id'];
            }
            //$gt = $op_quantity -1;
            $gt = $op_quantity; //大于0的账册从小到达排序,然后
            $billBooks = Service_BillBook::getByCondition(array(
                'id_array' => $billBookId,
                'warehous_account_no'=>$account_no,
                'can_use_quantity_gt'=>$gt
            ), '*', '1', '1', array('stock_quantity asc'));
            if (empty($billBooks)) {
                $error[] = $product['product_sku'].'账册信息不存在->'.$account_no;
            }else{
                $hekouNumber = $op_quantity; //总剩余核扣账册库存，初始化等于数量
                $rb = $billBooks[0];
                $billBook = Service_BillBook::getByIdForUpdate($rb['id'],'id','*');
                //var_dump($billBook);exit;
                $info = $billBook;
                $info['product_id'] = $product_id; //初始化为传的参数，bill_book的product_id存在一品多个商品关联情况不准确
                $info['product_sku'] = $product['product_sku'];
                $info['tax_method'] = $this->tax_method;
                $info['hum'] = $hum;
                if ($billBook['can_use_quantity'] >= $op_quantity) {
                    $info['qty'] = $op_quantity;
                    $hekouNumber = $hekouNumber - $info['qty'];
                    $info['qty1'] = $op_quantity*$hum['hum_quantity_law'];
                    $info['qty2'] = $op_quantity*$hum['hum_quantity_second'];
                    $data[] = $info;
                }else{
                    $error[] = $product['product_sku']."账册库存不够核扣";
                }
            }
        }else{
            $error[] = $product['product_sku'].'无匹配的账册信息';
        }
        if(!empty($error)){
            $result['message'] =  "库存获取有问题";
            $result['error'] = $error;
        }else{
            $result['data'] = $data;
            $result['message'] = "获取数据成功";
            $result['state'] = 1;
        }
        /*echo "result";
        print_r($result);*/
        return $result;
    }
    /**
     * @todo 获取商品信息
     */
    public function getProducts() {
        $codeList = $this->orderList;
        $codeList = array_unique($codeList); //去除重复项

        $condition = [
            'order_code_arr' => $codeList
        ];
        //print_r($condition);
        $orderTotal = [];
        $exportList = [];
        $gdsSeqNo = 1;
        $total = 0;
        $result = array(
            'state' => '0',
            'message' => '',
            'data' => [],
            'error' => [],
        );
        if (empty($codeList)) {
            $result['message'] = "订单为空";
            return $result;
        }
        $orders = Service_Orders::getByCondition($condition, "*");

        foreach ($orders as $k => $order) {
            //获取商品信息
            $conditionp = array(
                'order_code' => $order['order_code']
            );
            $orderProducts = Service_OrderProduct::getByCondition($conditionp, '*');
            foreach ($orderProducts as $kop => $oop) {
                //$exportList[$cdetail['item_record_no']] = $content;
                $content = array(
                    'product_id' => $oop['product_id'],
                    'qty' => $oop['op_quantity'],
                );
                if (!isset($exportList[$oop['product_id']])) {
                    $exportList[$oop['product_id']] = $content;
                } else {
                    $exportList[$oop['product_id']]['qty'] = $exportList[$oop['product_id']]['qty'] + $oop['op_quantity'];
                }
            }
        }
        ksort($exportList);
        $this->products = $exportList;
        //获取转出账册信息
        /*echo "pppp";
        print_r($this->products);
        exit;*/
    }

    /**
     * @todo 区内互转模式下有可能物流账册有库存了（限制C0421的品到C0426账册下查）
     */
    public function getProductsDel(){
        if(!empty($this->products)){
            foreach ($this->products as $k=>$op){
                //$p['product_id'], $p['qty']
                $product_id = $op['product_id'];
                $product = Service_Product::getByField($product_id);

                if($product['customer_code']=='C0421'){
                    //修复421账册到进口里面了
                    $condition = array(
                        'product_sku'=>$product['product_sku'],
                        'customer_code'=>'C0426'
                    );
                    $ps = Service_Product::getByCondition($condition,"*",1,1);
                    if(count($ps)>1){
                        throw new Exception("商品".$product['product_sku']."转出C0426,不止一个商品");
                    }
                    $p = $ps[0];
                    $product_id = $p['product_id'];

                    $billBookProudct = Service_BillBookProduct::getBillRecord($product_id);
                    $account_no = $this->bilInfo['im_bill_no'];

                    if ($billBookProudct) {
                        $billBookId = array();
                        foreach ($billBookProudct as $bill) {
                            $billBookId[] = $bill['bill_book_id'];
                        }
                        //$gt = $op_quantity -1;
                        $gt = 0.1; //大于0的账册从小到达排序,然后
                        $billBooks = Service_BillBook::getByCondition(array(
                            'id_array' => $billBookId,
                            'warehous_account_no' => $account_no,
                            'state'=>'0',
                            'can_use_quantity_gt' => $gt
                        ), '*', '0', '0', array('can_use_quantity asc '));
                        /*echo "boooook";
                        var_dump($billBooks);*/
                        if (!empty($billBooks)) {
                            //$error[] = $product['product_sku'] . '账册信息不存在'.$account_no;

                            //取一个账册库存数据
                            $billBook = $billBooks[0];
                            $billBook = Service_BillBook::getByIdForUpdate($billBook['id'], 'id', '*');
                            //$billBook['can_use_quantity'] = $billBook['can_use_quantity']+5; //63没库存测试用的
                            /*echo "sssfff";
                            var_dump($billBook);
                            var_dump($op['qty']);
                            exit;*/
                            if($billBook['can_use_quantity']>=$op['qty']){
                                //物流账册够转入的数据，这个品不用从电商账册转出也不用从物流账册转入
                                unset($this->products[$k]);
                            }else{
                                //物流账册不够转入的数据，这个品把物流账册的数量去掉在转入。
                                $this->products[$k]['qty'] = $this->products[$k]['qty']-$billBook['can_use_quantity'];
                            }
                        }
                    }
                }
            }
        }
    }


    /**
     * @todo 获取申报信息
     */
    protected function getExportImDetail() {
        /*echo "aaaaa";
        var_dump($this->products);
        exit;*/
        $exportList = array();
        $gdsSeqNo = 1;
        if(empty($this->products)){
            $this->exportList = $exportList;
            return;
        }
        foreach ($this->products as $k => $product) {
            //print_r($product);
            //exit;
            $row = $this->getBillNO($product['product_id'], $product['qty']);
            $imRow = $this->getImBillNO($row);
            $product = array();
            if (isset($imRow['product'])) {
                $product = $imRow['product'];
            }
            $product_declare = $product['product_declared_value'];
            $currency_hs_code = $product['currency_hs_code'];
            //$currency_code =

            /*print_r($imRow);
            exit;*/
            if ($imRow['state'] == 1) {
                $cdetails = $imRow['data'];
                foreach ($cdetails as $kc => $cdetail) {
                    //print_r($cdetail);
                    $putrec_seqno = "";
                    if (isset($cdetail['recorde_no_info']) && $cdetail['recorde_no_info'] != '') {
                        $putrec_seqno = $cdetail['recorde_no_info'];
                    }
                    //$price = $product['']
                    $content = array(
                        'gds_seqno' => $gdsSeqNo,
                        'putrec_seqno' => $putrec_seqno,//备案序号
                        'gds_mtno' => $cdetail['goods_code'],//商品料号
                        'gdecd' => $cdetail['hs_code'],//商品编码(必填)
                        'gds_nm' => $cdetail['product_title'],
                        'gds_spcf_model_desc' => $cdetail['specification'],//规格型号
                        'dcl_unitcd' => $cdetail['decalare_unit'],//申报单位
                        'lawf_unitcd' => $cdetail['legal_unit'],//法定单位
                        'secd_lawf_unitcd' => $cdetail['second_legal_unit'],//法定第二单位
                        'natcd' => $cdetail['country_code'],//国别
                        'dcl_uprc_amt' => $cdetail['zc_declareprice'], //$product_declare,//申报单价，取底账单价(转入方单价和和币制取转出方的一致)
                        'dcl_total_amt' => $cdetail['zc_declareprice'] * $cdetail['qty'],//申报总价，取底账单价*数量
                        'dcl_currcd' => $cdetail['zc_decalar_currency'], //$currency_hs_code,取底账币种
                        'dcl_qty' => $cdetail['qty'],//申报数量
                        'lawf_qty' => $cdetail['qty1'],//法定数量
                        'secd_lawf_qty' => $cdetail['qty2'],//第二法定数量
                        'decalar_currency'=>$cdetail['zc_decalar_currency'],//申报币种（取转出方的数据）
                        'use_cd' => '',//用途
                        //重量比例因子
                        //第一比例因子
                        //第二比例因子
                        //毛重	净重
                        //用途
                        'lvyrlf_modecd' => '3',//征免方式
                        'ucns_verno' => '',//单耗版本号
                        'entry_gds_seqno' => $gdsSeqNo,//报关单商品序号
                        //申请表序号
                        'rmk' => '',//备注
                        'destination_natcd' => '142',//最终目的国
                        // 'net_wt' => $productInfo['product_weight'],
                    );
                    $exportList[] = array('data' => $content, 'state' => 1);
                    $gdsSeqNo++;
                }
            } else {
                $exportList[] = array('data' => [], 'state' => 0, 'error' => $imRow['error'], 'product' => $product);
            }

        }
        //print_r($this->exportList);
        $this->exportList = $exportList;
    }

    /**
     * @todo 获取转出申报信息
     */
    protected function getExportExDetail() {
        $exportList = array();
        $gdsSeqNo = 1;
        if(empty($this->products)){
            $this->exportList = $exportList;
            return;
        }
        foreach ($this->products as $k => $p) {
            //print_r($product);
            //exit;
            $imRow = $this->getBillNO($p['product_id'], $p['qty']); //获取转出数据
            /*print_r($imRow);
            exit;*/
            $product = array();
            if (isset($imRow['product'])) {
                $product = $imRow['product'];
            }
            $product_declare = $product['product_declared_value'];
            $currency_hs_code = $product['currency_hs_code'];
            //$currency_code =

           /* print_r($imRow);
            exit;*/
            if ($imRow['state'] == 1) {
                $cdetails = $imRow['data'];
                foreach ($cdetails as $kc => $cdetail) {
                    //print_r($cdetail);
                    $putrec_seqno = "";
                    if (isset($cdetail['recorde_no']) && $cdetail['recorde_no'] != '') {
                        $putrec_seqno = $cdetail['recorde_no'];
                    }
                    //$price = $product['']
                    $content = array(
                        'gds_seqno' => $gdsSeqNo,
                        'putrec_seqno' => $putrec_seqno,//备案序号
                        'gds_mtno' => $cdetail['goods_code'],//商品料号
                        'gdecd' => $cdetail['hs_code'],//商品编码(必填)
                        'gds_nm' => $cdetail['product_title'],
                        'gds_spcf_model_desc' => $cdetail['specification'],//规格型号
                        'dcl_unitcd' => $cdetail['decalare_unit'],//申报单位
                        'lawf_unitcd' => $cdetail['legal_unit'],//法定单位
                        'secd_lawf_unitcd' => $cdetail['second_legal_unit'],//法定第二单位
                        'natcd' => $cdetail['country_code'],//国别
                        'dcl_uprc_amt' =>$cdetail['decalar_price'], //$product_declare,//申报单价，取底账单价
                        'dcl_total_amt' => $cdetail['decalar_price'] * $cdetail['qty'],//申报总价,取底账单价*数量
                        'dcl_currcd' => $cdetail['decalar_currency'], //$currency_hs_code, 取底账币种
                        'dcl_qty' => $cdetail['qty'],//申报数量
                        'lawf_qty' => $cdetail['qty1'],//法定数量
                        'secd_lawf_qty' => $cdetail['qty2'],//第二法定数量
                        'use_cd' => '',//用途
                        //重量比例因子
                        //第一比例因子
                        //第二比例因子
                        //毛重	净重
                        //用途
                        'lvyrlf_modecd' => '3',//征免方式
                        'ucns_verno' => '',//单耗版本号
                        'entry_gds_seqno' => $gdsSeqNo,//报关单商品序号
                        //申请表序号
                        'rmk' => '',//备注
                        'destination_natcd' => '142',//最终目的国
                        // 'net_wt' => $productInfo['product_weight'],
                    );
                    $exportList[] = array('data' => $content, 'state' => 1);
                    $gdsSeqNo++;
                }
            } else {
                $exportList[] = array('data' => [], 'state' => 0, 'error' => $imRow['error'], 'product' => $product);
            }
        }
        //print_r($this->exportList);
        $this->exportList = $exportList;
    }
    /**
     * @todo 获取退港核注清单
     */
    private function geTuigangDetail(){
        //var_dump($this->products);
        //拆分商品50个拆一个
        //$products = array_slice($this->products);
        $codeList = array_slice($this->products,0,$this->splitNumber);
        $products = [];
        $products[] = $codeList; //拆分成多个核注清单
        $page = 1;
        while(!empty($codeList)){
            $codeList = array_slice($this->products,$page*$this->splitNumber,$this->splitNumber);
            if(!empty($codeList)){
                $products[] = $codeList;
            }
            $page++;
        }
        /*print_r($products);
        exit;*/
        /*print_r($products);
        exit;*/
        $tuigangHz = array();
        foreach($products as $k=>$pRow){
            $exportList = array();
            $gdsSeqNo=1;
            foreach($pRow as $k=>$p){
                $imRow = $this->getTuigangNo($p['product_id'],$p['qty']); //获取转出数据
                /* print_r($imRow);
                 exit;*/
                $product = array();
                if(isset($imRow['product'])){
                    $product = $imRow['product'];
                }
                $product_declare = $product['product_declared_value'];
                $currency_hs_code = $product['currency_hs_code'];
                //$currency_code =
                //退港币制固定澳元，并且单价总价转成澳元



                /*print_r($imRow);
                exit;*/
                if($imRow['state']==1){
                    $cdetails = $imRow['data'];
                    foreach($cdetails as $kc=>$cdetail){
                        //print_r($cdetail);
                        $putrec_seqno = "";
                        if(isset($cdetail['recorde_no']) && $cdetail['recorde_no']!=''){
                            $putrec_seqno = $cdetail['recorde_no'];
                        }
                        //$price = $product['']
                        //单价转成澳元
                        $decalar_price = Service_Currency::converByHscode($cdetail['decalar_currency'],601,$cdetail['decalar_price']);//都转成澳元
                        $totalprice = sprintf("%.4f",$cdetail['decalar_price'] * $cdetail['qty']);
                        $content = array(
                            'gds_seqno'=>$gdsSeqNo,
                            'putrec_seqno' => $putrec_seqno,//备案序号
                            'gds_mtno' => $cdetail['goods_code'],//商品料号
                            'gdecd' => $cdetail['hs_code'],//商品编码(必填)
                            'gds_nm' => $cdetail['product_title'],
                            'gds_spcf_model_desc' => $cdetail['specification'],//规格型号
                            'dcl_unitcd' => $cdetail['decalare_unit'],//申报单位
                            'lawf_unitcd' => $cdetail['legal_unit'],//法定单位
                            'secd_lawf_unitcd' => $cdetail['second_legal_unit'],//法定第二单位
                            'natcd' => $cdetail['country_code'],//国别
                            'dcl_uprc_amt' => $decalar_price,//sprintf("%.4f",$cdetail['decalar_price']),//申报单价
                            'dcl_total_amt' => $totalprice,//申报总价
                            'dcl_currcd' => '601',//$cdetail['decalar_currency'], //$currency_hs_code, 取底账数据,固定601
                            'dcl_qty' => $cdetail['qty'],//申报数量
                            'lawf_qty' => $cdetail['qty1'],//法定数量
                            'secd_lawf_qty' => $cdetail['qty2'],//第二法定数量
                            'use_cd'=>'',//用途
                            //重量比例因子
                            //第一比例因子
                            //第二比例因子
                            //毛重	净重
                            //用途
                            'lvyrlf_modecd' => '3',//征免方式
                            'ucns_verno'=>'',//单耗版本号
                            'entry_gds_seqno'=>$gdsSeqNo,//报关单商品序号
                            //申请表序号
                            'rmk'=>'',//备注
                            'destination_natcd' => '110',//最终目的国
                            // 'net_wt' => $productInfo['product_weight'],
                        );
                        $exportList[] = array('data'=>$content,'state'=>1);
                        $gdsSeqNo++;
                    }
                }else{
                    $exportList[] = array('data'=>[],'state'=>0,'error'=>$imRow['error'],'product'=>$product);
                }

            }
            $tuigangHz[] = $exportList;
        }
        $this->tuigang = $tuigangHz;
        //print_r($tuigangHz);exit;
        return $tuigangHz;
        //print_r($tuigangHz);
    }

}