<?php
class Validate_PreCheckBillValidate{
    public $error = array();
    public $waring = array();
    public $data;
    public function __construct($data)
    {
        if ($data != null) {
            $this->data = $data;
        }
        $this->error = array();
    }
    public function set($data)
    {
        $this->data = $data;
    }
    /**
     * @todo 校验预录入数据和底账数据
     */
    public function checkPreDataDetail(){
        if($this->data['product_type']=='2'){
            $this->checkNoBillProduct();
        }else{
            $this->checkBill();
        }
    }
    /**
     * @todo 校验海关编码和第一单位第二单位
     */
    private function checkHscode(){
        $data = $this->data;
        $hs_code = $this->data['code_ts'];
        $g_no = $data['g_no'];
        if(empty($hs_code)){
            $this->error[] = "第{$g_no}项海关编码为空";
        }else{
            $hsRow = Service_Hscodes::getByField($hs_code,'hs_code');
            if(empty($hsRow)){
                $this->error[] = "第{$g_no}项海关编码系统不存在";
            }else{
                $hsUom = Service_HsUom::getByField($hs_code,'hs_code');
                if(empty($hsUom)){
                    $this->error[] = "第{$g_no}项系统申报单位不存在";
                }else{
                    if($hsUom['pu_code_law']!=$data['first_unit']){
                        $this->error[] = "第{$g_no}海关编码法定第一单位:{$hsUom['pu_code_law']}和输入数据{$data['first_unit']}不一致";
                    }
                    if($hsUom['pu_code_second']!=$data['second_unit']){
                        $this->error[] = "第{$g_no}海关编码法定第二单位:{$hsUom['pu_code_second']}和输入数据{$data['second_unit']}不一致";
                    }
                }
            }
        }
    }
    /**
     * @todo 校验申报价值
     */
    private function checkDeclPrice(){
        $data = $this->data;
        $decl_price = $data['decl_price'];
        $g_no = $data['g_no'];
        if(!is_numeric($decl_price) && $decl_price>0){
            $this->error[] = "第{$g_no}项申报单价:{$decl_price}必须是大于0的数字";
        }
    }
    /**
     * @todo 校验一日游无账册的商品
     */
    public function checkNoBillProduct(){
        $data = $this->data;
        $g_no = $data['g_no'];
        if($data['goods_id']==''){
            $this->error[] = "第{$g_no}项料件号不能为空";
        }
        if($data['contr_item']>0){
            $contr_item = $data['contr_item'];
            $bgp = Service_BgProduct::getByField($contr_item,'contr_item');
            if(empty($bgp)){
                $this->error[] = "第{$g_no}项历史入库一日游数据无备案序号:{$contr_item}数据";
            }else{
                //有数据情况下校验数据
                if($bgp['goods_id']!=$data['goods_id']){
                    $this->error[] = "第{$g_no}项历史入库一日游库数据料号:({$bgp['goods_id']})和输入数据:({$data['goods_id']})不一致";
                }
                if($bgp['gmodel']!=$data['gmodel']){
                    $this->error[] = "第{$g_no}项历史入库一日游库数据申报要素:({$bgp['gmodel']})和输入数据:({$data['gmodel']})不一致";
                }
                if($bgp['gname']!=$data['gname']){
                    $this->error[] = "第{$g_no}项历史入库一日游库数据申报品名:({$bgp['gname']})和输入数据:({$data['gname']})不一致";
                }
                if($bgp['hs_code']!=$data['code_ts']){
                    $this->error[] = "第{$g_no}项历史入库一日游库数据海关编码:({$bgp['hs_code']})和输入数据:({$data['code_ts']})不一致";
                }
                if($bgp['gunit']!=$data['gunit']){
                    $this->error[] = "第{$g_no}项历史入库一日游库数据申报单位:({$bgp['gunit']})和输入数据:({$data['gunit']})不一致";
                }
                if($bgp['first_unit']!=$data['first_unit']){
                    $this->error[] = "第{$g_no}项历史入库一日游库数据法定单位:({$bgp['first_unit']})和输入数据:({$data['first_unit']})不一致";
                }
                if($bgp['second_unit']!=$data['second_unit']){
                    $this->error[] = "第{$g_no}项历史入库一日游库数据第二单位:({$bgp['second_unit']})和输入数据:({$data['second_unit']})不一致";
                }
                if($bgp['origin_country']!=$data['origin_country']){
                    $this->error[] = "第{$g_no}项历史入库一日游库数据原产国:({$bgp['origin_country']})和输入数据:({$data['origin_country']})不一致";
                }
                $this->checkHscode();
                $this->checkDeclPrice();
            }
        }else{
            //没有备案序号的查询
            //$this->error[] = "第{$g_no}项料件号备案序号不能为空";
            $this->checkHscode();
            $this->checkDeclPrice();
        }
    }
    /**
     * @todo 校验有账册的商品
     */
    public function checkBill(){
        $data = $this->data;
        $g_no = $data['g_no'];
        if($data['goods_id']==''){
            $this->error[] = "第{$g_no}项料件号不能为空";
        }
        if($data['contr_item']>0){
            $contr_item = $data['contr_item'];
            //$bgp = Service_BgProduct::getByField($contr_item,'contr_item');

            $bilBook = Service_BillBook::getByField($contr_item,'recorde_no');
            if(empty($bilBook)){
                $this->error[] = "第{$g_no}项账册信息无备案序号:{$contr_item}数据";
            }else{
                //有数据情况下校验数据
                if($bilBook['goods_code']!=$data['goods_id']){
                    $this->error[] = "第{$g_no}项账册信息数据料号:({$bilBook['goods_code']})和输入数据:({$data['goods_id']})不一致";
                }
                if($bilBook['specification']!=$data['gmodel']){
                    $this->error[] = "第{$g_no}项账册信息数据数据申报要素:({$bilBook['specification']})和输入数据:({$data['gmodel']})不一致";
                }
                if($bilBook['decalare_unit']!=$data['gunit']){
                    $this->error[] = "第{$g_no}项账册信息数据申报单位:({$bilBook['decalare_unit']})和输入数据:({$data['gunit']})不一致";
                }
                if($bilBook['hs_code']!=$data['code_ts']){
                    $this->error[] = "第{$g_no}项账册信息数据海关编码:({$bilBook['hs_code']})和输入数据:({$data['code_ts']})不一致";
                }
                if($bilBook['product_title']!=$data['gname']){
                    $this->error[] = "第{$g_no}项账册信息数据品名:({$bilBook['product_title']})和输入数据:({$data['gname']})不一致";
                }
                if($bilBook['first_unit']!=$data['first_unit']){
                    $this->error[] = "第{$g_no}项账册信息数据数据法定单位:({$bilBook['first_unit']})和输入数据:({$data['first_unit']})不一致";
                }
                if($bilBook['second_unit']!=$data['second_unit']){
                    $this->error[] = "第{$g_no}项账册信息数据第二单位:({$bilBook['second_unit']})和输入数据:({$data['second_unit']})不一致";
                }
                if($bilBook['country_code']!=$data['origin_country']){
                    $this->error[] = "第{$g_no}项账册信息数据原产国:({$bilBook['country_code']})和输入数据:({$data['origin_country']})不一致";
                }
                $this->checkHscode();
                $this->checkDeclPrice();
            }
        }else{
            //没有备案序号的查询
            //$this->error[] = "第{$g_no}项料件号备案序号不能为空";
            $this->checkHscode();
            $this->checkDeclPrice();
        }
    }


}