<?php

namespace app\admin\controller;

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

//采购单退货
class Purchback extends Base
{
//public $post="";
public function __construct(App $app)
{
    parent::__construct($app);
   // $this->post=$this->request->post();
}
public function create(){
    $token = isset($this->post['token']) && $this->post['token'] !=="" ?trim($this->post['token']):"";
    $returnCode=makeNo("CT");
    $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] !==""? trim($this->post['cgdNo']) :"";
    if($cgdNo==""){
        return error_show(1002,"参数cgdNo不能为空");
    }
    $cg = Db::name('purchease_order')->where(['cgdNo'=>$cgdNo,'is_del'=>0])->find();
    if($cg==""){
        return error_show(1002,"未找到采购单数据");
    }
    $gd= Db::name('good_basic')->where(['spuCode'=>$cg['good_code']])->find();
    if($gd==""){
        return error_show(1002,"未找到商品数据");
    }
   // $int= isset($gd['cat_id']) && $gd['cat_id'] !=0 ? made($gd['cat_id']):[];
    $return_num = isset($this->post['return_num']) && $this->post['return_num'] !=="" ? trim($this->post['return_num']):"";
    if($return_num==""){
        return error_show(1002,"参数return_num不能为空");
    }
    $apply_id =GetUserInfo($token);
    if(empty($apply_id)||$apply_id['code']!=0){
        return error_show(1002,"申请人数据不存在");
    }
    $rid= isset($apply_id["data"]['id']) ?  $apply_id["data"]['id'] : "";
    $rname= isset($apply_id["data"]['nickname']) ?  $apply_id["data"]['nickname'] : "";
    $good_total = $return_num*$cg['good_price'];
//    $post_company = isset($this->post['post_company']) && $this->post['post_company'] !==""? trim($this->post['post_company']) :"";
//    $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']):"";
//    $status= isset($this->post['status']) && $this->post['status'] !==""? trim($this->post['status']) :"";
    $remark =isset($this->post['remark']) && $this->post['remark'] !==""? trim($this->post['remark']) :"";
    $data = [
        "returnCode"=>$returnCode,
        "cgdNo"=>$cgdNo,
        "good_code"=>$cg['good_code'],
        "return_num"=>$return_num,
        "good_name"=>$cg['good_name'],
        "good_price"=>$cg['good_price'],
        "good_catid"=>$gd['cat_id'],
        "apply_name"=>$rname,
        "apply_id"=>$rid,
        "good_total"=>$good_total,
        "post_company"=>"",
        "post_code"=>"",
        "post_fee"=>"",
        "status"=>1,
        "remark"=>$remark,
        "is_del"=>0,
        "addtime"=>date("Y-m-d H:i:s"),
        "updatetime"=>date("Y-m-d H:i:s")
    ];
    //$data['can'] = $int;
    $pd = Db::name('purchease_back')->insert($data,true);
    if($pd>0){
        $sto = ["order_code"=>$returnCode,"status"=>1,"action_remark"=>'',"action_type"=>"create"];
        ActionLog::logAdd($this->post['token'],$sto,"CGTHD",1,$sto);
        $process=["order_code"=>$returnCode,"order_id"=>$pd,"order_status"=>1,"order_type"=>'CGTHD',"before_status"=>1];
        ProcessOrder::AddProcess($this->post['token'],$process);
        return error_show(0,"新建成功");
    }else{
        return error_show(1002,"新建失败");
    }
}
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 = [['a.is_del',"=",0],['b.is_del',"=",0]];
    $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo']!="" ? trim($this->post['cgdNo']):"";
    if($cgdNo!=""){
        $where[]=['a.cgdNo',"like", "%$cgdNo%"];
    }
    $returnCode= isset($this->post['returnCode']) && $this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
    if($returnCode!=""){
        $where[]=['a.returnCode',"like", "%$returnCode%"];
    }
    $good_type_code=isset($this->post['good_type_code']) && $this->post['good_type_code']!==""? trim($this->post['good_type_code']):"";
    if($good_type_code !=""){
        $where[]= ["b.spuCode",'like',"%$good_type_code%"];
    }
    $good_name=isset($this->post['good_name']) && $this->post['good_name']!==""? trim($this->post['good_name']):"";
    if($good_name !=""){
        $where[]= ["b.good_name",'like',"%$good_name%"];
    }
    $status=isset($this->post['status']) && $this->post['status']!==""? trim($this->post['status']):"";
    if($status!=""){
        $where[]= ["a.status",'=',$status];
    }
    $count = Db::name('purchease_back')->alias('a')->join("purchease_order b","b.cgdNo=a.cgdNo","left")
        ->where($where)->count();
    $total = ceil($count / $size);
    $page = $page >= $total ? $total : $page;
    $list = Db::name('purchease_back')->alias('a')->join("purchease_order b","b.cgdNo=a.cgdNo","left")
        ->where($where)->page($page,$size)
        ->field("a.returnCode,b.spuCode,a.good_catid,a.return_num,a.good_price,a.good_total,
        a.post_company,a.post_fee,a.status,a.cgdNo,b.good_name,b.order_type")->order("a.addtime desc")->select();
    $data=[];
    foreach ($list as $value){
        $value['can']= isset($value['good_catid']) && $value['good_catid'] !=0 ? made($value['good_catid']):[];
        $data[]=$value;
    }
    return app_show(0,"获取成功",['list'=>$data,'count'=>$count]);
}
public function status(){
    $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
    if($id==""){
        return error_show(1002,"参数id不能为空");
    }
    $dio = Db::name('purchease_back')->where(['id'=>$id,'is_del'=>0])->find();
    if(empty($dio)){
        return error_show(1002,"退货订单未找到");
    }
    $returnCode = isset($this->post['returnCode']) && $this->post['returnCode'] !==""? trim($this->post['returnCode']) :"";
    if($returnCode==""){
        return error_show(1002,"参数returnCode不能为空");
    }

    $status = isset($this->post['status'])&&$this->post['status']!=='' ? intval($this->post['status']) : "";
    if($status===""){
        return error_show(1001,'status不能为空');
    }
    $temp= $dio['status'];
    $dio['status']=$status;
    $dio['updatetime']=date("Y-m-d H:i:s");
    $str = Db::name("purchease_back")->update($dio);
    if($str){
        $sto = ["order_code"=>$id,"status"=>$temp,"action_remark"=>'',"action_type"=>"status"];
        ActionLog::logAdd($this->post['token'],$sto,"CGTHD",$dio['status'],$sto);
        $process=["order_code"=>$returnCode,"order_id"=>$dio['id'],"order_status"=>$status,"order_type"=>'CGTHD',"before_status"=>1];
        ProcessOrder::AddProcess($this->post['token'],$process);
        return error_show(0,"更新成功");
    }else{
        return error_show(1002,"更新失败");
    }
}
public function backfe(){
    $id = isset($this->post['id']) && $this->post['id'] !=="" ? intval($this->post['id']) :"";
    if($id==""){
        return error_show(1002,"参数id不能为空");
    }
    $dio = Db::name('purchease_back')->where(['id'=>$id,'is_del'=>0])->find();
    if(empty($dio)){
        return error_show(1002,"退货订单未找到");
    }
    $returnCode = isset($this->post['returnCode']) && $this->post['returnCode'] !==""? trim($this->post['returnCode']) :"";
    if($returnCode==""){
        return error_show(1002,"参数returnCode不能为空");
    }
    $post_company = isset($this->post['post_company']) && $this->post['post_company'] !==""? trim($this->post['post_company']) :"";
    if($post_company==""){
        return error_show(1002,"参数post_company 不能为空");
    }
    $post_code = isset($this->post['post_code']) && $this->post['post_code'] !==""? trim($this->post['post_code']) :"";
    if($post_code==""){
        return error_show(1002,"参数post_code不能为空");
    }
    $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !==""? trim($this->post['post_fee']) :"";
    if($post_fee==""){
        return error_show(1002,"参数post_fee不能为空");
    }
    $data=[
        "id"=>$id,
        "returnCode"=>$returnCode,
        "post_company"=>$post_company,
        "post_code"=>$post_code,
        "post_fee"=>$post_fee,
        "updatetime"=>date("Y-m-d H:i:s")
    ];
    $datainfo=Db::name('purchease_back')->save($data);
    if($datainfo){
        $sto = ["order_code"=>$id,"status"=>0,"action_remark"=>'',"action_type"=>"edit"];
        ActionLog::logAdd($this->post['token'],$sto,"BKD",0,$sto);
        $process=["order_code"=>$returnCode,"order_id"=>$dio['id'],"order_status"=>$dio['status'],"order_type"=>'CGTHD',"before_status"=>1];
        ProcessOrder::AddProcess($this->post['token'],$process);
        return error_show(0,"更新成功");
    }else{
        return error_show(1002,"更新失败");
    }

    //return $datainfo ? error_show(0,"更新成功") : error_show(1002,"更新失败");
}

    public function info(){
        $returnCode = isset($this->post['returnCode']) && $this->post['returnCode'] !==""? trim($this->post['returnCode']) :"";
        if($returnCode==""){
            return error_show(1002,"参数returnCode不能为空");
        }
        $info= Db::name("purchease_return")->where(['returnCode'=>$returnCode])->find();
        if(empty($info)){
            return error_show(1002,"未找到数据");
        }
        $dom = Db::name("purchease_order")->where(['cgdNo'=>$info['cgdNo'],'is_del'=>0])->find();
        if($dom['order_type']==3){
            $goon = Db::name("good_zixun")->where(["spuCode"=>$dom['spuCode'],"is_del"=>0])->find();
        }else {
            $goon =Db::name('good_basic')->where(['spuCode'=>$dom['spuCode']])->find();
        }
        if(empty($goon)){
            return error_show(1002,"未找到商品数据");
        }
        $supplier=Db::name("supplier")->where(["code"=>$dom['supplierNo']])->find();
        $int= isset($goon['cat_id']) && $goon['cat_id'] !=0 ? made($goon['cat_id']):[];
        //$info['good'] = isset($wsmcode['good_name']) ? $wsmcode['good_name'] : "";
        $info['good_num'] = isset($dom['good_num']) ? $dom['good_num'] : "";
        $info['spuCode'] = isset($dom['spuCode']) ? $dom['spuCode'] : "";
        $info['good_name'] = isset($dom['good_name']) ? $dom['good_name'] : "";
        $info['good_price'] = isset($dom['good_price']) ? $dom['good_price'] : "";
        $info['return_price'] = round($info['good_price']*$info['return_num'],2);
        $info['wsm_supplier'] = isset($supplier['name']) ? $supplier['name'] : "";
        $info['wsm_supplierNo'] = isset($supplier['code']) ? $supplier['code'] : "";
        $info['can']=$int;
        return app_show(0,"获取成功",$info);
    }

    /**
     * @return \think\response\Json|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $returnCode = isset($this->post['returnCode']) && $this->post['returnCode'] !==""? trim($this->post['returnCode']) :"";
        if($returnCode==""){
            return error_show(1002,"参数returnCode不能为空");
        }
        $info= Db::name("purchease_back")->where(['returnCode'=>$returnCode,"is_del"=>0])->find();
        if(empty($info)){
            return error_show(1002,"未找到数据");
        }
        $del = Db::name("purchease_back")->where(['returnCode'=>$returnCode])->save(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
        if($del){
            $sto = ["order_code"=>$returnCode,"status"=>$info['status'],"action_remark"=>'',"action_type"=>"delete"];
            ActionLog::logAdd($this->post['token'],$sto,"CGTHD",0,["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
            $process=["order_code"=>$returnCode,"order_id"=>$info['id'],"order_status"=>$info['status'],"order_type"=>'CGTHD'];
            ProcessOrder::workdel($process);
            return error_show(0,"删除成功");
        }else{
            return error_show(1002,"删除失败");
        }
    }
}