123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\controller;
- use app\admin\model\CgdInfo;
- use app\admin\model\CompanyInfo;
- use app\admin\model\FhdChild;
- use app\admin\model\Pay;
- use app\admin\model\PayInfo;
- use app\admin\model\QrdInfo;
- use app\admin\model\SupplierInfo;
- use think\App;
- use think\facade\Validate;
- class Track extends Base
- {
- protected $model=null;
- public function __construct(App $app) {
- parent::__construct($app);
- $this->model=new \app\admin\model\Track();
- }
- //运费单创建
- public function create(){
- $param = $this->request->post('list/a', '', 'trim');
- if(empty($param))$this->error("参数不能为空");
- $rule =Validate::rule([
- "outChildCode|发货工单编号"=>"require|max:255",
- "supplierNo|物流供货商"=>'require|max:255',
- "companyNo|业务公司"=>'require|max:255',
- "post_fee|物流费用"=>'require|float',
- "remark|备注"=>'max:255',
- ]);
- $supplier = (new SupplierInfo())->where(["code"=>array_unique(array_column($param,"supplierNo"))])->column("name","code");
- $business = (new CompanyInfo())->where(["companyNo"=>array_unique(array_column($param,"companyNo"))])->column("company_name","companyNo");
- $outChild = (new FhdChild())->where(["outChildCode"=>array_unique(array_column($param,'outChildCode'))]) ->column('orderCode','outChildCode');
- $cgdArr = (new CgdInfo())->where(["qrdCode"=>array_unique($outChild)])->column("sequenceNo","qrdCode");
- $sale = QrdInfo::where(['cxCode|sequenceNo'=>array_unique($outChild),'is_del'=>0])
- ->field("if(cxCode='',sequenceNo,cxCode) as cxCode,ownerName,ownerid")
- ->select()->toArray();
- $saleName =array_column($sale,'ownerName',"cxCode");
- $saleId =array_column($sale,'ownerid',"cxCode");
- $data=[];
- foreach ($param as $key=>$item){
- if($rule->check($item)==false)$this->error($rule->getError());
- if(!isset($outChild[$item['outChildCode']]))$this->error("发货工单{$item['outChildCode']}未找到或未发货");
- $temp=[
- "TrackCode"=>substr(makeNo('TK'), 0, -2) . str_pad(strval($key), 2, '0', STR_PAD_LEFT),
- "orderCode"=>$outChild[$item['outChildCode']],
- "cgdNo"=>$cgdArr[$outChild[$item['outChildCode']]]??"",
- "saler"=>$saleName[$outChild[$item['outChildCode']]]??"",
- "salerid"=>$saleId[$outChild[$item['outChildCode']]]??"",
- "outChildCode"=>$item['outChildCode'],
- "supplierNo"=>$item['supplierNo'],
- "supplierName"=>$supplier[$item['supplierNo']]??"",
- 'companyNo'=>$item['companyNo'],
- 'companyName'=>$business[$item['companyNo']]??'',
- 'post_fee'=>$item['post_fee'],
- 'apply_name'=>$this->uname,
- 'apply_id'=>$this->uid,
- 'remark'=>$item['remark'],
- ];
- $data[]=$temp;
- }
- $this->model->saveAll($data);
- $this->success("运费单创建成功");
- }
- //***列表
- public function list(){
- $param=$this->request->param(["outChildCode"=>"","order_type"=>"","order_source"=>"","supplierNo"=>"",
- "companyNo"=>"","status"=>"","orderCode"=>"","cgdNo"=>"","create_start"=>"","create_end"=>"",'page'=>1,'size'=>10],"post","trim");
- $where=[];
- $param['outChildCode']==''?:$where[]=["outChildCode","like","%{$param['outChildCode']}%"];
- $param['orderCode']==''?:$where[]=["orderCode","like","%{$param['orderCode']}%"];
- $param['cgdNo']==''?:$where[]=["cgdNo","like","%{$param['cgdNo']}%"];
- $param['order_type']==''?:$where[]=["order_type","=",$param['order_type']];
- $param['order_source']==''?:$where[]=["order_source","=",$param['order_source']];
- $param['supplierNo']==''?:$where[]=["supplierNo","=",$param['supplierNo']];
- $param['companyNo']==''?:$where[]=["companyNo","=",$param['companyNo']];
- $param['status']===''?:$where[]=["status","=",$param['status']];
- $param['create_start']==''?:$where[]=["create_time",">=",startTime($param['create_start'])];
- $param['create_end']==''?:$where[]=["create_time","<=",endTime($param['create_end'])];
- $list=$this->model->with(['fhdChild'])->where($where)->order("id desc")->paginate(["list_rows"=>$param['size'],
- "page"=>$param['page']]);
- $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list->items(),'supplierNo')));
- foreach ($list->items() as &$value){
- $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
-
- }
- $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
- }
-
- //***列表
- public function fhdList(){
- $param=$this->request->param(['outChildCode'=>'','order_type'=>'','order_source'=>'','supplierNo'=>'',
- 'companyNo'=>'','orderCode'=>'','page'=>1,'size'=>10],'post','trim');
- $where=[];
- $param['outChildCode']==''?:$where[]=['outChildCode','like',"%{$param['outChildCode']}%"];
- $param['orderCode']==''?:$where[]=['orderCode','like',"%{$param['orderCode']}%"];
- $param['order_type']==''?:$where[]=['order_type','=',$param['order_type']];
- $param['order_source']==''?:$where[]=['order_source','=',$param['order_source']];
- $param['supplierNo']==''?:$where[]=['supplierNo','=',$param['supplierNo']];
- $param['companyNo']==''?:$where[]=['companyNo','=',$param['companyNo']];
-
- $list=(new FhdChild())->where($where)->order('id desc')->paginate(['list_rows'=>$param['size'],
- 'page'=>$param['page']]);
- $this->success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
- }
- //***列表
- public function info(){
- $param=$this->request->param(["id"=>""],'post','trim');
- $valid=Validate::rule(["id"=>"require|number|gt:0"]);
- if($valid->check($param)==false)$this->error($valid->getError());
- $list=$this->model->with(['fhdChild'])->order('id desc')->findOrEmpty($param['id']);
- $this->success('获取成功',$list);
- }
- //***列表
- public function query(){
- $param=$this->request->param(['outChildCode'=>[],'order_type'=>'','order_source'=>'','supplierNo'=>'',
- 'companyNo'=>'','status'=>'','orderCode'=>[],'cgdNo'=>[],"page"=>1,"size"=>100],'post','trim');
- $where=[];
- empty($param['outChildCode'])??$where[]=['outChildCode','in',$param['outChildCode']];
- empty($param['cgdNo'])??$where[]=['cgdNo','in',$param['cgdNo']];
- empty($param['orderCode'])??$where[]=['orderCode','in',$param['orderCode']];
- // $param['orderCode']==''??$where[]=['orderCode','like',"%{$param['orderCode']}%"];
- // $param['cgdNo']==''??$where[]=['cgdNo','like',"%{$param['cgdNo']}%"];
- $param['order_type']==''??$where[]=['order_type','=',$param['order_type']];
- $param['order_source']==''??$where[]=['order_source','=',$param['order_source']];
- $param['supplierNo']==''??$where[]=['supplierNo','=',$param['supplierNo']];
- $param['companyNo']==''??$where[]=['companyNo','=',$param['companyNo']];
- $param['status']==''??$where[]=['status','=',$param['status']];
- $list=$this->model->with(['fhdChild'])->order('id desc')->paginate(['list_rows'=>$param['size'],
- 'page'=>$param['page']]);
- $this->success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
- }
- public function PayAdd(){
- $param=$this->request->param(['cids'=>[]],'post','trim');
- $valid = Validate::rule(['cids|对账数据集合'=>'require|array']);
- if($valid->check($param)==false)$this->error($valid->getError());
- $PayObject = new \app\admin\model\Track();
- $objall= $PayObject->where(['id'=>$param['cids']])->select();
- if($objall->isEmpty()) $this->error('对账数据不能为空');
- $supplierNo= array_column($objall->toArray(),'supplierNo');
- $supplierName= array_column($objall->toArray(),'supplierName');
- if(count(array_unique($supplierNo))>1){
- $this->error('对账数据供应商不唯一');
- }
- $companyNo= array_column($objall->toArray(),'companyNo');
- $companyName= array_column($objall->toArray(),'companyName');
- if(count(array_unique($companyNo))>1){
- $this->error('对账数据业务公司不唯一');
- }
- if ($this->level == 2) {
- //判断是否开通供应商账号
- $temp = check_has_account_by_supplierNos([$supplierNo[0]]);
- if (isset($temp['data'][$supplierNo[0]])) $this->error( '该供应商已经开通账号,不允许当前账号操作');
- }
- $pay= new Pay();
- $data = [
- 'payNo'=>makeNo('PAY'),
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'total_fee'=>'0',
- 'pay_type'=>2,
- 'supplierNo'=>$supplierNo[0],
- 'supplierName'=>$supplierName[0],
- 'companyNo'=>$companyNo[0],
- 'companyName'=>$companyName[0],
- 'wpay_fee'=>'0',
- 'apay_fee'=>'0',
- 'ainv_fee'=>'0',
- 'winv_fee'=>'0',
- 'remark'=>'',
- 'status'=>1,
- ];
- $pay->startTrans();
- try{
- $payinfo=[];
- foreach ($objall as $item){
- if($item->status!=0) throw new \Exception("{$item->TrackCode}已对账");
- $temp=[];
- $temp['cgdNo']=$item['TrackCode'];
- $temp['total_fee']=$item['post_fee'];
- $temp['apay_fee']=0;
- $temp['wpay_fee']=$item['post_fee'];
- $temp['winv_fee']=0;
- $temp['ainv_fee']=$item['post_fee'];
- $temp['payNo']=$data['payNo'];
- $temp['addtime']=date('Y-m-d H:i:s');
- $temp['updatetime']=date('Y-m-d H:i:s');
- $payinfo[]=$temp;
- $data['total_fee']=bcadd($data['total_fee'],$item['post_fee'],2);
- $data['wpay_fee']= bcadd($data['wpay_fee'],$item['post_fee'],2);
- $data['winv_fee']= bcadd($data['winv_fee'],$item['post_fee'],2);
- }
- $info =(new PayInfo())->saveAll($payinfo);
- if($info){
- $create=$pay->save($data);
- if($create)$objall->update(['status'=>1]);
- $pay->commit();
- }else throw new \Exception("对账单生成失败");
- }catch (\Exception $e){
- $pay->rollback();
- $this->error($e->getMessage());
- }
- $this->success('对账单添加成功',['payNo'=>$data['payNo']]);
- }
-
- /**对账单未对账前编辑8**/
- public function PaySave(){
- $param=$this->request->param(['cids'=>[],'payNo'=>''],'post','trim');
- $valid = Validate::rule(['cids|对账数据集合'=>'require|array','payNo|对账编号'=>'require']);
- if($valid->check($param)==false)$this->error($valid->getError());
- $PayObject = new \app\admin\model\Track();
- $pay= new Pay();
- $objall= $PayObject->where(['id'=>$param['cids']])->select();
- if($objall->isEmpty()) $this->error('对账数据不能为空');
- $PayInfo = $pay->where(['payNo'=>$param['payNo']])->findOrEmpty();
- if($PayInfo->isEmpty()) $this->error('未找到对账单数据');
-
- $supplierNo= array_column($objall->toArray(),'supplierNo');
- $supplierName= array_column($objall->toArray(),'supplierName');
- if(count(array_unique($supplierNo))>1){
- $this->error('对账数据供应商不唯一');
- }
- $companyNo= array_column($objall->toArray(),'companyNo');
- $companyName= array_column($objall->toArray(),'companyName');
- if(count(array_unique($companyNo))>1){
- $this->error('对账数据业务公司不唯一');
- }
- if ($this->level == 2) {
- //判断是否开通供应商账号
- $temp = check_has_account_by_supplierNos([$supplierNo[0]]);
- if (isset($temp['data'][$supplierNo[0]])) $this->error( '该供应商已经开通账号,不允许当前账号操作');
- }
- $trackArr = PayInfo::where(['payNo'=>$param['payNo'],'is_del'=>0,'status'=>1])->column('cgdNo');
- $add=$remove=[];
- if(empty($trackArr)==false){
- $remove= array_diff($trackArr,array_column($objall->toArray(),'TrackCode'));
- $add= array_diff(array_column($objall->toArray(),'TrackCode'),$trackArr);
- }
- $data = [
- 'apply_id'=>$this->uid,
- 'apply_name'=>$this->uname,
- 'total_fee'=>'0',
- 'supplierNo'=>$supplierNo[0],
- 'supplierName'=>$supplierName[0],
- 'companyNo'=>$companyNo[0],
- 'companyName'=>$companyName[0],
- 'wpay_fee'=>'0',
- 'winv_fee'=>'0',
- ];
- $pay->startTrans();
- try{
- $payinfo=[];
- foreach ($objall as $item){
- $data['total_fee']=bcadd($data['total_fee'],$item['post_fee'],2);
- $data['wpay_fee']= bcadd($data['wpay_fee'],$item['post_fee'],2);
- $data['winv_fee']= bcadd($data['winv_fee'],$item['post_fee'],2);
- if(!empty($add)&& in_array($item['TrackCode'],$add)){
- $temp=[];
- if($item->status!=0) throw new \Exception("{$item->TrackCode}已对账");
- $temp['cgdNo']=$item['TrackCode'];
- $temp['total_fee']=$item['post_fee'];
- $temp['apay_fee']=0;
- $temp['wpay_fee']=$item['post_fee'];
- $temp['winv_fee']=0;
- $temp['ainv_fee']=$item['post_fee'];
- $temp['payNo']=$param['payNo'];
- $temp['addtime']=date('Y-m-d H:i:s');
- $temp['updatetime']=date('Y-m-d H:i:s');
- $payinfo[]=$temp;
- }
-
- }
- if(!empty($payinfo))(new PayInfo())->saveAll($payinfo);
- $up = $PayInfo->save($data);
- if($up){
- $objall->update(['status'=>1]);
- if(!empty($remove)){
- $PayObject->where(['TrackCode'=>$remove,'status'=>1])->update(['status'=>0]);
- PayInfo::where(['cgdNo'=>$remove,'status'=>1,'is_del'=>0])->update(['status'=>0,'is_del'=>1]);
- }
-
- }
- $pay->commit();
- }catch (\Exception $exception){
- $pay->rollback();
- $this->error($exception->getMessage());
- }
- $this->success('更新成功');
- }
-
- public function delete(){
- $param=$this->request->param(['id'=>''],'post','trim');
- $valid=Validate::rule(['id'=>'require|number|gt:0']);
- if($valid->check($param)==false)$this->error($valid->getError());
- $info=$this->model->order('id desc')->findOrEmpty($param['id']);
- if($info->isEmpty()) $this->error("未找到数据");
- if($info->status!=0)$this->error('运单状态对账中');
- $del= $info->delete();
- $del? $this->success('删除成功'):$this->error('删除失败');
- }
- }
|