<?php

namespace app\admin\controller;

use app\admin\model\ActionLog;
use app\admin\model\GoodLog;
use app\admin\model\ProcessOrder;
use app\BaseController;
use think\App;
use think\facade\Db;

//调拨  allot_info(从表)和allot_stock(主表)
//调拨之后,修改库存,采购单的仓库编号是否也需要修改
class Allot extends BaseController
{
    public $post= "";
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->post=$this->request->post();
    }
    public function list(){
        $page = isset($this->post['page']) && $this->post['page'] !=="" ? intval($this->post['page']) :"1";
        $size = isset($this->post['size']) && $this->post['size'] !=="" ? intval($this->post['size']) :"10";
        $where =[['is_del',"=",0]];
        $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !== "" ? trim($this->post['allot_code']) : "";
        if ($allot_code !== "") {
           // $where['allot_code'] = $allot_code;
            $where[]=['allot_code',"=",$allot_code];
        }
        $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !== "" ? trim($this->post['wsm_out']) : "";
        if ($wsm_out !== "") {
            //$where['wsm_out'] = $wsm_out;
            $where[]=['wsm_out',"=",$wsm_out];
        }
        $wsm_in = isset($this->post['wsm_in']) && $this->post['wsm_in'] !== "" ? trim($this->post['wsm_in']) : "";
        if ($wsm_in !== "") {
           // $where['wsm_in'] = $wsm_in;
            $where[]=['wsm_in',"=",$wsm_in];
        }
        $start= isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start']:"";
        if ($start !="") {
            //$where = ["addtime"=>Db::raw(">= '{$start}'")];
            $where[]=['addtime',">=",$start];
        }
        $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] :"";
        if($end !=""){
           // $where = ["addtime"=>Db::raw("<= '{$end}'")];
            $where[]=['addtime',"<=",$end];
        }

        $out_supplierNo =isset($this->post['wsm_out_supplierNo']) &&$this->post['wsm_out_supplierNo'] !=="" ? trim($this->post['wsm_out_supplierNo']):"";
        if($out_supplierNo!=""){
            $supplier = Db::name("supplier")->where(["code"=>$out_supplierNo])->find();
            if(empty($supplier)){
                return error_show(1004,"未找到出库供应商信息");
            }
            $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"wsm_type"=>1,"supplierNo"=>$out_supplierNo])->column("wsm_code");
            //$condtion["c.wsm_code"] = $wsmcode;
            $where[]=['wsm_out',"in",$wsmcode];
        }
        $out_companyNo = isset($this->post['out_companyNo']) && $this->post['out_companyNo'] !== "" ? trim($this->post['out_companyNo']) : "";
        if ($out_companyNo !== "") {

            $where[]=["out_companyNo","=",$out_companyNo];
        }
        $in_companyNo = isset($this->post['in_companyNo']) && $this->post['in_companyNo'] !== "" ? trim($this->post['in_companyNo']) : "";
        if ($in_companyNo !== "") {

            $where[]=["in_companyNo","=",$in_companyNo];
        }

        $in_supplierNo =isset($this->post['wsm_in_supplierNo']) &&$this->post['wsm_in_supplierNo'] !=="" ? trim($this->post['wsm_in_supplierNo']):"";
        if($in_supplierNo!=""){
            $supplier = Db::name("supplier")->where(["code"=>$in_supplierNo])->find();
            if(empty($supplier)){
                return error_show(1004,"未找到入库供应商信息");
            }
            $wsmcode = Db::name("warehouse_info")->where(["is_del"=>0,"wsm_type"=>1,"supplierNo"=>$in_supplierNo])->column("wsm_code");
            //$condtion["c.wsm_code"] = $wsmcode;
            $where[]=['wsm_in',"in",$wsmcode];
        }
        $status = isset($this->post['status']) && $this->post['status']!=="" ? intval($this->post['status']):"";
        if($status!==""){
            $where[]=['status',"=",$status];
        }

        $count = Db::name('allot_stock')->where($where)->count();
        $total = ceil($count/$size);
        $page = $page >= $total ? $total : $page;
        $list = Db::name('allot_stock')->where($where)->order("addtime desc")->page($page,$size)->select()->toArray();
      // var_dump(Db::name('allot_stock')->getLastSql());
        $data=[];
        foreach ($list as $value){
            $db = Db::name('warehouse_info')
                ->alias('a')
                ->join("supplier b","b.code=a.supplierNo","left")
                ->join("business bus","bus.companyNo=a.companyNo","left")
                ->field("a.name,a.wsm_code,b.name rname,b.code,bus.company")
                ->where(['a.wsm_code'=>$value['wsm_out'],"a.wsm_type"=>1,'a.is_del'=>0])
                ->find();
            $value['name'] =isset($db['name']) ? $db['name']:"";
            $value['wsm_code'] =isset($db['wsm_code']) ? $db['wsm_code']:"";
            $value['rname'] = isset($db['rname']) ? $db['rname'] : "";
            $value['code'] = isset($db['code']) ? $db['code'] : "";
            $value['out_company_name'] = isset($db['company']) ? $db['company'] : "";
            $dc = Db::name('warehouse_info')
                ->alias('a')
                ->join("business bus","bus.companyNo=a.companyNo","left")
                ->join("supplier b","b.code=a.supplierNo","left")
                ->field("a.name,a.wsm_code,b.name tname,b.code,bus.company")
                ->where(['a.wsm_code'=>$value['wsm_in'],"a.wsm_type"=>1,'a.is_del'=>0])
                ->find();
            $value['wsm_in_name'] =isset($dc['name']) ? $dc['name']:"";
            $value['wsm_in_code'] =isset($dc['wsm_code']) ? $dc['wsm_code']:"";
            $value['tname'] =isset($dc['tname']) ? $dc['tname']:"";
            $value['rcode'] =isset($dc['code']) ? $dc['code']:"";
            $value['in_company_name'] =isset($dc['company']) ? $dc['company']:"";
            $data []=$value;
        }
        return app_show(0,"获取成功",['count'=>$count,'list'=>$data]);
    }
    public function create(){

        $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
    $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !=="" ? trim($this->post['wsm_out']) :"";
    if($wsm_out==""){
        return error_show(1002,"出库仓库编号不能为空");
    }
    $wsm_in = isset($this->post['wsm_in']) && $this->post['wsm_in'] !=="" ? trim($this->post['wsm_in']) :"";
    if($wsm_in==""){
        return error_show(1002,"入库仓库不能为空");
    }

        //根据出入库的仓库编码,查出对应的公司、业务公司
        $warehouse_temp_info = Db::name('warehouse_info')
            ->where(['is_del' => 0, 'status' => 1])
            ->whereIn('wsm_code', [$wsm_out, $wsm_in])
            ->column('supplierNo,companyNo', 'wsm_code');//supplierNo所属供货商,companyNo业务企业

//        $in_companyNo = isset($this->post['in_companyNo']) && $this->post['in_companyNo'] !=="" ? trim($this->post['in_companyNo']) :"";
//        if($in_companyNo==""){
//            return error_show(1002,"in_companyNo不能为空");
//        }
//        $out_companyNo = isset($this->post['out_companyNo']) && $this->post['out_companyNo'] !=="" ? trim($this->post['out_companyNo']) :"";
//        if($out_companyNo==""){
//            return error_show(1002,"out_companyNo不能为空");
//        }
//        $company =Db::name("business")->where(['companyNo'=>$in_companyNo,"is_del"=>0])->find();
//        if($company==false){
//            return error_show(1002,"未找到入库业务公司");
//        }
//        $company =Db::name("business")->where(['companyNo'=>$out_companyNo,"is_del"=>0])->find();
//        if($company==false){
//            return error_show(1002,"未找到出库业务公司");
//        }
//    $good_type_code = isset($this->post['good_type_code']) && $this->post['good_type_code'] !=="" ? trim($this->post['good_type_code']) :"";
//    if($good_type_code==""){
//        return error_show(1002,"商品属性不能为空");
//    }
    $allot_code=makeNo("XK");
//    $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
//        if($allot_code==""){
//            return error_show(1002,"调拨编号不能为空");
//        }
    $apply_id =GetUserInfo($token);
        if(empty($apply_id)||$apply_id['code']!=0){
            return error_show(1002,"申请人数据不存在");
        }
        $rm= isset($apply_id["data"]['id']) ?  $apply_id["data"]['id'] : "";
        $ri= isset($apply_id["data"]['nickname']) ?  $apply_id["data"]['nickname'] : "";
    $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"" ;
    $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
    $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
    $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? trim($this->post['remark']) : "";
    $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) :"0";
        if($status==""){
            return error_show(1002,"状态不能为空");
        }
    $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
    if($dain==""){
        return error_show(1002,"商品不能为空");
        }
    Db::startTrans();
    try {
            $data=[
                "allot_code"=>$allot_code,
                "wsm_out"=>$wsm_out,
                "wsm_in"=>$wsm_in,
                "out_companyNo"=>$warehouse_temp_info[$wsm_out]['companyNo'],
                "in_companyNo"=>$warehouse_temp_info[$wsm_in]['companyNo'],
                "apply_name"=>$ri,
                "apply_id"=>$rm,
                "post_name"=>$post_name,
                "post_code"=>$post_code,
                "post_fee"=>$post_fee,
                "remark"=>$remark,
                "status"=>$status,
                "is_del"=>0,
                "addtime"=>date("Y-m-d H:i:s"),
                "updatetime"=>date("Y-m-d H:i:s"),
            ];
            $pd=Db::name('allot_stock')->insert($data,true);
            $dm=[];
            if($pd>0) {
                $order = ["order_code"=>$allot_code,"status"=>$status,"action_remark"=>'',"action_type"=>"create"];
                ActionLog::logAdd($this->post['token'],$order,"DBD",$status,$this->post);
                foreach ($dain as $value) {
                    $st = Db::name("good")->alias("b")
                        ->join("good_stock c", "c.spuCode = b.spuCode", "left")
                        ->join("warehouse_info v","v.wsm_code=c.wsm_code","left")
                        ->join("supplier n","n.code=v.supplierNo","left")
                        ->where(['c.wsm_code' => $wsm_out,"v.wsm_type"=>1, 'b.spuCode' => $value['spuCode'], 'b.is_del' => 0])->where("c.is_del=0 or c.is_del is null")
                        ->field("b.spuCode,b.good_name,b.good_unit,c.wsm_code,c.usable_stock,c.good_type_code,c.wait_out_stock,c.wait_in_stock,n.name")->find();

                    if (empty($st)) {
                        Db::rollback();
                        return error_show(1003, "商品不能为空");
                        }
                    if($value['allot_num']>$st['usable_stock']){
                        return error_show(2000,"库存数量不足");
                    }
                        $temp = [];
                        $temp['good_name'] = $st['good_name'];
                        $temp['allot_code'] =$allot_code;
                        $temp['good_type_code'] = $value['spuCode'];
                        $temp['allot_num'] = $value['allot_num'];
                        $temp['usable_num'] = 0;
                        $temp['error_num'] = 0;
                        $temp['error_remark'] = "";
                        $temp['stock_num'] = 0;
                        $temp['error_code']="";
                        $temp['good_num']=$st['usable_stock'];
                        $temp['addtime'] = date("Y-m-d H:i:s");
                        $temp['updatetime'] = date("Y-m-d H:i:s");
                        $dm[]=$temp;
                    }
                $io = Db::name('allot_info')->insertAll($dm);
                if ($io) {
                    $process=["order_code"=>$allot_code,"order_id"=>$pd,"order_status"=>$status,"order_type"=>'DBD'];
                    ProcessOrder::AddProcess($token,$process);
                    Db::commit();
                    return error_show(0, "调拨创建成功");
                }
            }
            Db::rollback();
            return error_show(1002,"调拨创建失败");
        }catch(\Exception $e){
            Db::rollback();
            return error_show(1005,$e->getMessage());
        }
    }
    public function info(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
        if($id==""){
            return error_show(1002,"参数id不能为空");
        }
        $str = Db::name('allot_stock') ->where(['id'=>$id,'is_del'=>0])->find();
        if(empty($str)){
            return error_show(1003,"未找到调拨数据");
        }
        $stv = Db::name('warehouse_info')->where(['wsm_code'=>$str['wsm_out'],"wsm_type"=>1,'is_del'=>0])->field('supplierNo,wsm_code,name')->find();
        if(empty($stv)){
            return error_show(1002,"未找到仓库数据");
        }
        $vr = Db::name('supplier')->where(['code'=>$stv['supplierNo'],'is_del'=>0])->field('name,code as rcode')->find();
        if(empty($vr)){
            return error_show(1001,"未找到供应商数据");
        }
        $st = Db::name('warehouse_info')->where(['wsm_code'=>$str['wsm_in'],"wsm_type"=>1,'is_del'=>0])->field('supplierNo,wsm_code,name')->find();
        if(empty($st)){
            return error_show(1003,"未找到仓库数据");
        }
        $var = Db::name('supplier')->where(['code'=>$st['supplierNo'],'is_del'=>0])->field('name,code')->find();
        if(empty($var)){
            return error_show(1004,"未找到供应商数据");
        }
        // var_dump(Db::name('supplier')->getLastSql());
        $str ['wsm_out_name']=$vr['name'];
        $str ['wsm_in_name']=$var['name'];
        $str ['ckc_name']=$stv['name'];
        $str ['ckr_name']=$st['name'];
        $str['out_code']=$vr['rcode'];
        $str['in_code']=$var['code'];
//        $str = Db::name('allot_stock')->alias('a')->join('warehouse_info b',"b.wsm_code=a.wsm_out","left")
//            ->join('supplier c',"c.code=b.supplierNo","left")->field("c.name,c.code,")
//            ->where(['a.id'=>$id,'a.is_del'=>0])->find();
       // var_dump( Db::name('allot_stock')->getLastSql());

        $vmp = Db::name('allot_info')->where(['allot_code'=>$str['allot_code'],'is_del'=>0])->select();

            $data=[];
        foreach ($vmp as $k=>$value){
            $s =Db::name('good_stock')->where(['spuCode'=>$value['good_type_code'],'wsm_code'=>$str['wsm_out']])
                ->find();
            if($str['status']<=1) {
                $value['usable_num'] = $s['usable_stock'];
            }
            $value['error_msg']="";
            if($value['error_code']!=""){
                $msg =Db::name("result_info")->where(['result_code'=>$value['error_code'],"is_del"=>0])->find();
                $value['error_code_name']= isset($msg['result']) &&$msg['result']!="" ? $msg['result']:"";
            }
            $data[]=$value;
        }
        $str['item']=$data;
        if(empty($str)){
            return error_show(1002,"未找到调拨编号");
        }else{
            return app_show(0,"获取成功",$str);
        }
    }
    public function edit(){
        $token =isset($this->post['token']) && $this->post['token'] !=="" ? trim($this->post['token']) :"";
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
        if($id===""){
            return  error_show(1002,"参数id不能为空");
        }
        $etid = Db::name("allot_stock")->where(["id"=>$id,"is_del"=>0])->find();
        if(empty($etid)){
            return error_show(1002,"未找到数据");
        }
        $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
        $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
       $apply_id =GetUserInfo($token);
        if(empty($apply_id)||$apply_id['code']!=0){
            return error_show(1002,"申请人数据不存在");
        }
        $rm= isset($apply_id["data"]['id']) ?  $apply_id["data"]['id'] : "";
        $ri= isset($apply_id["data"]['nickname']) ?  $apply_id["data"]['nickname'] : "";
        $wsm_out = isset($this->post['wsm_out']) && $this->post['wsm_out'] !=="" ? trim($this->post['wsm_out']) :"" ;
        if($wsm_out==""){
            return error_show(1002,"出库仓库不能为空");
        }
        $wsm_in= isset($this->post['wsm_in']) && $this->post['wsm_in'] !=="" ? trim($this->post['wsm_in']) :"" ;
        if($wsm_in==""){
            return error_show(1002,"入库仓库不能为空");
        }
        $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
//        $status= isset($this->post['status']) && $this->post['status'] !=="" ? trim($this->post['status']) :"" ;
//        if($status==""){
//            return error_show(1002,"状态不能为空");
//        }
        $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
        if($dain==""){
            return error_show(1002,"商品不能为空");
        }
        Db::startTrans();
        try{
            $data=[
               "apply_id"=>$rm,
                "apply_name"=>$ri,
                "wsm_out"=>$wsm_out,
                "wsm_in"=>$wsm_in,
                "post_fee"=>$post_fee,
                "post_name"=>$post_name,
                "post_code"=>$post_code,
                "updatetime"=>date('Y-m-d H:i:s'),
            ];
            $da = Db::name('allot_stock')->where(["id"=>$id,"is_del"=>0])->save($data);
            if($da>0) {
                $order = ["order_code"=>$etid['allot_code'],"status"=>$etid['status'],"action_remark"=>'',"action_type"=>"edit"];
                ActionLog::logAdd($this->post['token'],$order,"DBD",$etid['status'],$this->post);
                foreach ($dain as $value) {
                    $st =Db::name("good")->alias("b")
                        ->join("good_stock c", "c.spuCode = b.spuCode", "left")
                        ->join("warehouse_info v","v.wsm_code=c.wsm_code","left")
                        ->join("supplier n","n.code=v.supplierNo","left")
                        ->where(['c.wsm_code' => $wsm_out,"v.wsm_type"=>1, 'b.spuCode' => $value['spuCode'], 'b.is_del' => 0, 'a.is_del' => 0])->where("c.is_del=0 or c.is_del is null")
                        ->field("b.spuCode,b.good_name,b.good_unit,c.wsm_code,c.usable_stock,c.good_type_code,c.wait_out_stock,c.wait_in_stock,n.name")->find();
                    if (empty($st)) {
                        Db::rollback();
                        return error_show(1003, "商品不能为空");
                    }
                    if(isset($value['id'])&&$value['id']!==""){
                        $good = Db::name('allot_info')->where(["id"=>$value['id'],"is_del"=>0])->find();
                        if(empty($good)||$good['allot_code']!=$etid['allot_code']){
                            Db::rollback();
                            return error_show(2000,"商品不属于此次调拨");
                        }
                    }

                    if($value['allot_num']>$st['usable_stock']){
                        Db::rollback();
                        return error_show(2000,"库存数量不足");
                    }
                    $temp = [];
                   isset($value['id'])&&$value['id']!=="" ? $temp['id'] = $value['id']:'';
                    $temp['good_name'] = $st['good_name'];
                    $temp['good_type_code'] = $value['spuCode'];
                    $temp['allot_num'] = $value['allot_num'];
                    $temp['usable_num'] = 0;
                    $temp['error_num'] = 0;
                    $temp['error_remark'] = "";
                    $temp['allot_code'] = $etid['allot_code'];
                    $temp['stock_num'] = 0;
                    $temp['good_num']=$st['usable_stock'];
                    $temp['error_code']="";
                    $temp['is_del']=$value['is_del'];
                    isset($value['id'])&&$value['id']!=="" ? '' :$temp['addtime'] = date("Y-m-d H:i:s");
                    $temp['updatetime'] = date("Y-m-d H:i:s");
                    $np = Db::name('allot_info')->save($temp);
                    if ($np==false) {
                        Db::rollback();
                        return error_show(1003, "调拨更新失败");
                    }
                }
                $process=["order_code"=>$etid['allot_code'],"order_id"=>$etid['id'],"order_status"=>$etid['status'],"order_type"=>'DBD'];
                ProcessOrder::AddProcess($token,$process);
                Db::commit();
                return error_show(0, "调拨更新成功");
            }
            Db::rollback();
            return error_show(1003, "调拨更新失败");
        }catch (\Exception $e){
            Db::rollback();
            return error_show(1005,$e->getMessage());
        }
    }
    public function del(){
        $id = $this->post['id'] && $this->post['id'] !=="" ? intval($this->post['id']) :"";
        if($id===""){
            return  error_show(1004,"参数id不能为空");
        }
        $str= Db::name('allot_stock')->where(['id'=>$id,'is_del'=>0])->find();
        if(empty($str)){
            return error_show(1002,"未找到数据");
        }
        $end = Db::name('allot_stock')->update(['id'=>$id,'is_del'=>1,'updatetime'=>date("Y-m-d H:i:s")]);
        if($end){
            $order = ["order_code"=>$str['allot_code'],"status"=>$str['status'],"action_remark"=>'',"action_type"=>"del"];
            ActionLog::logAdd($this->post['token'],$order,"DBD",$str['status'],$this->post);
            $process=["order_code"=>$str['allot_code'],"order_status"=>$str['status'],"order_type"=>'DBD'];
            ProcessOrder::workdel($process);
            return error_show(0,"删除成功");
        }else{
            return error_show(1002,"删除失败");
        }
    }
    public function status(){
        $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"" ;
        if($id==""){
            return error_show(1002,"调拨编号不能为空");
        }
        $remark = isset($this->post['remark']) && $this->post['remark'] !=="" ? trim($this->post['remark']) :"";
        $status = isset($this->post['status']) && $this->post['status'] !=="" ? intval($this->post['status']) :"";
        if($status===""){
            return error_show(1002,"审核状态不能为空");
        }
        $st = Db::name('allot_stock')->where(['id'=>$id,"is_del"=>0])->find();
        if(empty($st)){
            return error_show(1002,"调拨信息未找到");
        }
        Db::startTrans();
        try{
            $sr= $st['status'];
            $st['remark']=$remark;
            $st['status']=$status;
            $st['updatetime']= date('Y-m-d H:i:s');
            $sv = Db::name('allot_stock')->save($st);
            if($sv){
                $order = ["order_code"=>$st['allot_code'],"status"=>$sr,"action_remark"=>'',"action_type"=>"del"];
                ActionLog::logAdd($this->post['token'],$order,"DBD",$status,$this->post);
                if($status==2 || $status==1){
                    $vmp = Db::name('allot_info')->where(['allot_code'=>$st['allot_code'],"is_del"=>0])->select();
                    foreach ($vmp as $value){
                        $stock = Db::name("good_stock")->where(["spuCode"=>$value['good_type_code'],"wsm_code"=>$st['wsm_out'],"is_del"=>0])->find();
                        //var_dump($value['allot_num'],$stock['usable_stock']);
                        if($value['allot_num']>$stock['usable_stock']){
                            Db::rollback();
                            return error_show(2000,"超出库存数量");
                        }
                        if($status==2){
                            $stock['usable_stock']-=$value['allot_num'];
                            $stock['wait_out_stock']+=$value['allot_num'];
                            $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
                            $stock['updatetime']= date('Y-m-d H:i:s');
                            $Db = Db::name('good_stock')->update($stock);
                            //var_dump(Db::name('good_stock')->getLastSql());
                            if($Db==false){
                                Db::rollback();
                                return error_show(1002,"状态更新失败");
                            }
                        }
                    }
                }
                $process=["order_code"=>$st['allot_code'],"order_id"=>$st['id'],"order_status"=>$st['status'],"order_type"=>'DBD'];
                ProcessOrder::AddProcess($this->post['token'],$process);
                Db::commit();
                return error_show(0,"状态更新成功");
            }
            Db::rollback();
            return error_show(1003,"状态更新失败");
        }catch (\Exception $e) {
            Db::rollback();
            return error_show(1005, $e->getMessage());
        }
    }
    /*出库方发货*/
    public function getont(){
        $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
        if(empty($allot_code)){
            return error_show(1002,"调拨单编号不能为空");
        }
        $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
        if($al==""){
            return error_show(1003,"调拨编号未找到");
        }
        $post_name = isset($this->post['post_name']) && $this->post['post_name'] !=="" ? trim($this->post['post_name']) :"";
        if($post_name==""){
            return error_show(1002,"物流公司不能为空");
        }
        $post_code = isset($this->post['post_code']) && $this->post['post_code'] !=="" ? trim($this->post['post_code']) :"";
        if($post_code==""){
            return error_show(1002,"物流单号不能为空");
        }
        $post_fee= isset($this->post['post_fee']) && $this->post['post_fee'] !=="" ? trim($this->post['post_fee']) :"" ;
        if($post_fee===""){
            return error_show(1002,"物流费用不能为空");
        }
        Db::startTrans();
        try {
            $data=[
                "allot_code"=>$allot_code,
                "post_name"=>$post_name,
                "post_code"=>$post_code,
                "post_fee"=>$post_fee,
                "status"=>3,
                "is_del"=>0,
                "updatetime"=>date("Y-m-d H:i:s"),
            ];
            $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code])->update($data);
            if($datainfo){
                $order = ["order_code"=>$al['allot_code'],"status"=>$al['status'],"action_remark"=>'',"action_type"=>"del"];
                ActionLog::logAdd($this->post['token'],$order,"DBD",3,$this->post);
                    $vnp = Db::name('allot_info')->where(['allot_code'=>$al['allot_code'],'is_del'=>0])->select();
                   // var_dump(Db::name('allot_info')->getLastSql());
                $good_data=[];
                    foreach ($vnp as $value){
                        $stock = Db::name("good_stock")->where(["spuCode"=>$value['good_type_code'],"wsm_code"=>$al['wsm_out'],"is_del"=>0])->find();
                       if($stock['wait_out_stock']<$value['allot_num']){
                           Db::rollback();
                           return error_show(2000,"超出库存数量");
                       }
                        $stock['wait_out_stock']-=$value['allot_num'];
                        $stock['intra_stock']+=$value['allot_num'];
                        $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
                        $stock['updatetime']= date('Y-m-d H:i:s');
                        $dr = Db::name('good_stock')->update($stock);
                        if($dr==false){
                            Db::rollback();
                            return error_show(1002,"发货更新失败");
                        }
                        //商品变动日志表,good_log_code字段存储调拨单号
                        $good_data[] = ['good_log_code' => $allot_code, "stock_id" => $stock['id'], "type" => 1, 'stock' => $value['allot_num'], "stock_name" => "intra_stock"];
                        $good_data[] = ['good_log_code' => $allot_code, "stock_id" => $stock['id'], "type" => 2, 'stock' => $value['allot_num'], "stock_name" => "wait_out_stock"];
                    }
                GoodLog::LogAdd($this->post['token'],$good_data,'allotsend');
                $process=["order_code"=>$allot_code,"order_id"=>$al['id'],"order_status"=>3,"order_type"=>'DBD'];
                ProcessOrder::AddProcess($this->post['token'],$process);
                Db::commit();
                return error_show(0,"发货更新成功");
            }
            Db::rollback();
            return error_show(1003,"发货更新失败");
        }catch (\Exception $e) {
            Db::rollback();
            return error_show(1005, $e->getMessage());
        }
    }
    /*入库方验货*/
    public function getin(){
        $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
        if(empty($allot_code)){
            return error_show(1002,"调拨单编号不能为空");
        }
        $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
        if($al==""){
            return error_show(1003,"调拨编号未找到");
        }
        $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
        if($dain==""){
            return error_show(1002,"商品不能为空");
        }
        Db::startTrans();
        try {
        $data=[
            "allot_code"=>$allot_code,
            "is_del"=>0,
            "status"=>4,
            "updatetime"=>date("Y-m-d H:i:s"),
        ];
        $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
        if($datainfo>0) {
            $order = ["order_code"=>$al['allot_code'],"status"=>$al['status'],"action_remark"=>'',"action_type"=>"del"];
            ActionLog::logAdd($this->post['token'],$order,"DBD",4,$this->post);
            $good_data=[];
            foreach ($dain as $value) {
                $st = Db::name("good")->alias("b")
                    ->join("good_stock c", "c.spuCode = b.spuCode", "left")->where(['wsm_code' => $al['wsm_out'], 'b.spuCode' => $value['spuCode'], 'b.is_del' => 0, 'a.is_del' => 0])->where("c.is_del=0 or c.is_del is null")
                    ->field("b.spuCode,b.good_name,c.wsm_code,c.usable_stock")->find();
                if (empty($st)) {
                    Db::rollback();
                    return error_show(1003, "未找到商品");
                }
                $n = Db::name('allot_info')->where(['good_type_code'=>$value['spuCode'],'allot_code'=> $al['allot_code'],'is_del'=>0])->find();
                if(empty($n)){
                    Db::rollback();
                    return error_show(10032, "未找到数据");
                }
//                if($value['allot_num']>$st['usable_stock']){
//                    return error_show(2000,"库存数量不足");
//                }
                $temp = [];
                $temp['error_code']=$value['error_code'];
                $temp['good_num']=$st['usable_stock'];
                $temp['usable_num'] = $value['usable_num'];
                $temp['error_num'] = $value['error_num'];
                $temp['error_remark'] = $value['error_remark'];
                $temp['stock_num']=$n['allot_num'];
                $temp['updatetime'] = date("Y-m-d H:i:s");
                $np = Db::name('allot_info')->where(['good_type_code'=>$value['spuCode'],'allot_code'=> $al['allot_code'],'is_del'=>0])->save($temp);
                if($np==false){
                    Db::rollback();
                    return error_show(1001,"数据更新失败");
                }

                $stock = Db::name("good_stock")->where(["spuCode"=>$value['spuCode'],"wsm_code"=>$al['wsm_out'],
                    "is_del"=>0])->find();
                if($stock['intra_stock']<$n['allot_num']){
                    Db::rollback();
                    return error_show(2000,"超出库存数量");
                }
                $stock['updatetime']= date('Y-m-d H:i:s');
                $stock['intra_stock']-=$n['allot_num'];
                $dr = Db::name('good_stock')->update($stock);
                if($dr==false){
                    Db::rollback();
                    return error_show(1002,"数据更新失败");
                }
                //商品变动日志表,good_log_code字段存储调拨单号
                $good_data[]=['good_log_code'=>$allot_code,"stock_id"=>$stock['id'],"type"=>2,'stock'=>$n['allot_num'],"stock_name"=>"intra_stock"];
                $instock = Db::name("good_stock")->where(["spuCode"=>$value['spuCode'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
                if($instock==false){
                   $instock =[];
                    $instock['spuCode']=$value['spuCode'];
                    $instock['wsm_code']=$al['wsm_in'];
                    $instock['wait_in_stock']=0;
                    $instock['wait_out_stock']=0;
                    $instock['usable_stock']=0;
                    $instock['intra_stock']=0;
                    $instock['total_stock']=0;
                    $instock['status']=1;
                    $instock['is_del']=0;
                    $instock['warn_stock']=0;
                    $instock['addtime']= date('Y-m-d H:i:s');
                }
                $instock['wait_in_stock']+=$n['allot_num'];
                $instock['updatetime']= date('Y-m-d H:i:s');
                $sk = Db::name('good_stock')->save($instock);
//               $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
                if ($sk==false) {
                    Db::rollback();
                    return error_show(1003, "入库验货更新失败");
                }
                //商品变动日志表,good_log_code字段存储调拨单号
                $good_data[]=['good_log_code'=>$allot_code,"stock_id"=>isset($instock['id'])?$instock['id']: Db::name("good_stock")->getLastInsID(),"type"=>1,'stock'=>$n['allot_num'],"stock_name"=>"wait_in_stock"];
            }
            GoodLog::LogAdd($this->post['token'],$good_data,'allotin');
            $process=["order_code"=>$allot_code,"order_id"=>$al['id'],"order_status"=>4,"order_type"=>'DBD'];
            ProcessOrder::AddProcess($this->post['token'],$process);
            Db::commit();
            return error_show(0, "入库验货更新成功");
        }
        Db::rollback();
        return error_show(1002, "入库验货更新失败");
    }catch (\Exception $e){
        Db::rollback();
        return error_show(1005,$e->getMessage());
    }
}
/*验货审核*/
public function vesio(){
    $allot_code = isset($this->post['allot_code']) && $this->post['allot_code'] !=="" ? trim($this->post['allot_code']) :"";
    if(empty($allot_code)){
        return error_show(1002,"调拨单编号不能为空");
    }
    $al = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->find();
    if($al==""){
        return error_show(1003,"调拨编号未找到");
    }
    $dain = isset($this->post['good']) && $this->post['good'] !=="" ? $this->post['good']:"";
    if($dain==""){
        return error_show(1002,"商品不能为空");
    }
    Db::startTrans();
    try {
        $data=[
            "allot_code"=>$allot_code,
            "status"=>5,
            "is_del"=>0,
            "updatetime"=>date("Y-m-d H:i:s"),
        ];
        $datainfo = Db::name('allot_stock')->where(['allot_code'=>$allot_code,"is_del"=>0])->save($data);
        if($datainfo>0) {
            $order = ["order_code"=>$al['allot_code'],"status"=>$al['status'],"action_remark"=>'',"action_type"=>"del"];
            ActionLog::logAdd($this->post['token'],$order,"DBD",5,$this->post);
            foreach ($dain as $value) {
                $st = Db::name("good")->alias("b")
                    ->join("good_stock c", "c.spuCode = b.spuCode", "left")->where(['wsm_code' => $al['wsm_in'], 'b.spuCode' => $value['spuCode'], 'b.is_del' => 0, 'a.is_del' => 0])->where("c.is_del=0 or c.is_del is null")
                    ->field("b.spuCode,b.good_name,c.wsm_code,c.usable_stock")->find();
                if (empty($st)) {
                    Db::rollback();
                    return error_show(1003, "商品不能为空");
                }
                $temp = [];
                $temp['error_code']="";
                $temp['good_num']=$st['usable_stock'];
                $temp['error_num'] = $value['error_num'];
                $temp['stock_num'] = $value['stock_num'];
                $temp['error_remark'] = $value['error_remark'];
                $temp['updatetime'] = date("Y-m-d H:i:s");
                $np = Db::name('allot_info')->where(['good_type_code'=>$value['spuCode'],'allot_code'=> $al['allot_code']])->save($temp);
                if($np==false){
                    Db::rollback();
                    return error_show(1001,"数据更新失败");
                }
                $p = Db::name('allot_info')->where(['good_type_code'=>$value['spuCode'],'allot_code'=>$al['allot_code'],"is_del"=>0])->find();
                if($p==false){
                    Db::rollback();
                    return error_show(1002,"未找到数据");
                }
                $stock = Db::name("good_stock")->where(["spuCode"=>$value['spuCode'],"wsm_code"=>$al['wsm_in'],"is_del"=>0])->find();
                if($stock['wait_in_stock']<$p['stock_num']){
                    Db::rollback();
                   return error_show(2000,"超出待入库存数量");
               }
                $stock['wait_in_stock']-=$p['stock_num'];
                $stock['usable_stock']+=$p['stock_num'];
                $stock['updatetime']=date('Y-m-d H:i:s');
                $stock['total_stock']=$stock['wait_out_stock']+$stock['usable_stock'];
                $dr = Db::name('good_stock')->update($stock);
                if ($dr==false) {
                    Db::rollback();
                    return error_show(1004, "验货更新失败");
                }
                //商品变动日志表,good_log_code字段存储调拨单号
                $good_data[] = ['good_log_code' => $allot_code, "stock_id" => $stock['id'], "type" => 2, 'stock' => $p['stock_num'], "stock_name" => "wait_in_stock"];
                $good_data[] = ['good_log_code' => $allot_code, "stock_id" => $stock['id'], "type" => 1, 'stock' => $p['stock_num'], "stock_name" => "usable_stock"];
            }
            GoodLog::LogAdd($this->post['token'],$good_data,'allotincheck');
            $process=["order_code"=>$allot_code,"order_id"=>$al['id'],"order_status"=>5,"order_type"=>'DBD'];
            ProcessOrder::AddProcess($this->post['token'],$process);
            Db::commit();
            return error_show(0, "验货更新成功");
        }
        Db::rollback();
        return error_show(1003, "验货更新失败");
    }catch (\Exception $e){
        Db::rollback();
        return error_show(1005,$e->getMessage());
    }
}
}