Track.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\model\CgdInfo;
  5. use app\admin\model\CompanyInfo;
  6. use app\admin\model\FhdChild;
  7. use app\admin\model\Pay;
  8. use app\admin\model\PayInfo;
  9. use app\admin\model\QrdInfo;
  10. use app\admin\model\SupplierInfo;
  11. use think\App;
  12. use think\facade\Validate;
  13. class Track extends Base
  14. {
  15. protected $model=null;
  16. public function __construct(App $app) {
  17. parent::__construct($app);
  18. $this->model=new \app\admin\model\Track();
  19. }
  20. //运费单创建
  21. public function create(){
  22. $param = $this->request->post('list/a', '', 'trim');
  23. if(empty($param))$this->error("参数不能为空");
  24. $rule =Validate::rule([
  25. "outChildCode|发货工单编号"=>"require|max:255",
  26. "supplierNo|物流供货商"=>'require|max:255',
  27. "companyNo|业务公司"=>'require|max:255',
  28. "post_fee|物流费用"=>'require|float',
  29. "remark|备注"=>'max:255',
  30. ]);
  31. $supplier = (new SupplierInfo())->where(["code"=>array_unique(array_column($param,"supplierNo"))])->column("name","code");
  32. $business = (new CompanyInfo())->where(["companyNo"=>array_unique(array_column($param,"companyNo"))])->column("company_name","companyNo");
  33. $outChild = (new FhdChild())->where(["outChildCode"=>array_unique(array_column($param,'outChildCode'))]) ->column('orderCode','outChildCode');
  34. $cgdArr = (new CgdInfo())->where(["qrdCode"=>array_unique($outChild)])->column("sequenceNo","qrdCode");
  35. $sale = QrdInfo::where(['cxCode|sequenceNo'=>array_unique($outChild),'is_del'=>0])
  36. ->field("if(cxCode='',sequenceNo,cxCode) as cxCode,ownerName,ownerid")
  37. ->select()->toArray();
  38. $saleName =array_column($sale,'ownerName',"cxCode");
  39. $saleId =array_column($sale,'ownerid',"cxCode");
  40. $data=[];
  41. foreach ($param as $key=>$item){
  42. if($rule->check($item)==false)$this->error($rule->getError());
  43. if(!isset($outChild[$item['outChildCode']]))$this->error("发货工单{$item['outChildCode']}未找到或未发货");
  44. $temp=[
  45. "TrackCode"=>substr(makeNo('TK'), 0, -2) . str_pad(strval($key), 2, '0', STR_PAD_LEFT),
  46. "orderCode"=>$outChild[$item['outChildCode']],
  47. "cgdNo"=>$cgdArr[$outChild[$item['outChildCode']]]??"",
  48. "saler"=>$saleName[$outChild[$item['outChildCode']]]??"",
  49. "salerid"=>$saleId[$outChild[$item['outChildCode']]]??"",
  50. "outChildCode"=>$item['outChildCode'],
  51. "supplierNo"=>$item['supplierNo'],
  52. "supplierName"=>$supplier[$item['supplierNo']]??"",
  53. 'companyNo'=>$item['companyNo'],
  54. 'companyName'=>$business[$item['companyNo']]??'',
  55. 'post_fee'=>$item['post_fee'],
  56. 'apply_name'=>$this->uname,
  57. 'apply_id'=>$this->uid,
  58. 'remark'=>$item['remark'],
  59. ];
  60. $data[]=$temp;
  61. }
  62. $this->model->saveAll($data);
  63. $this->success("运费单创建成功");
  64. }
  65. //***列表
  66. public function list(){
  67. $param=$this->request->param(["outChildCode"=>"","order_type"=>"","order_source"=>"","supplierNo"=>"",
  68. "companyNo"=>"","status"=>"","orderCode"=>"","cgdNo"=>"","create_start"=>"","create_end"=>"",'page'=>1,'size'=>10],"post","trim");
  69. $where=[];
  70. $param['outChildCode']==''?:$where[]=["outChildCode","like","%{$param['outChildCode']}%"];
  71. $param['orderCode']==''?:$where[]=["orderCode","like","%{$param['orderCode']}%"];
  72. $param['cgdNo']==''?:$where[]=["cgdNo","like","%{$param['cgdNo']}%"];
  73. $param['order_type']==''?:$where[]=["order_type","=",$param['order_type']];
  74. $param['order_source']==''?:$where[]=["order_source","=",$param['order_source']];
  75. $param['supplierNo']==''?:$where[]=["supplierNo","=",$param['supplierNo']];
  76. $param['companyNo']==''?:$where[]=["companyNo","=",$param['companyNo']];
  77. $param['status']===''?:$where[]=["status","=",$param['status']];
  78. $param['create_start']==''?:$where[]=["create_time",">=",startTime($param['create_start'])];
  79. $param['create_end']==''?:$where[]=["create_time","<=",endTime($param['create_end'])];
  80. $list=$this->model->with(['fhdChild'])->where($where)->order("id desc")->paginate(["list_rows"=>$param['size'],
  81. "page"=>$param['page']]);
  82. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list->items(),'supplierNo')));
  83. foreach ($list->items() as &$value){
  84. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  85. }
  86. $this->success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  87. }
  88. //***列表
  89. public function fhdList(){
  90. $param=$this->request->param(['outChildCode'=>'','order_type'=>'','order_source'=>'','supplierNo'=>'',
  91. 'companyNo'=>'','orderCode'=>'','page'=>1,'size'=>10],'post','trim');
  92. $where=[];
  93. $param['outChildCode']==''?:$where[]=['outChildCode','like',"%{$param['outChildCode']}%"];
  94. $param['orderCode']==''?:$where[]=['orderCode','like',"%{$param['orderCode']}%"];
  95. $param['order_type']==''?:$where[]=['order_type','=',$param['order_type']];
  96. $param['order_source']==''?:$where[]=['order_source','=',$param['order_source']];
  97. $param['supplierNo']==''?:$where[]=['supplierNo','=',$param['supplierNo']];
  98. $param['companyNo']==''?:$where[]=['companyNo','=',$param['companyNo']];
  99. $list=(new FhdChild())->where($where)->order('id desc')->paginate(['list_rows'=>$param['size'],
  100. 'page'=>$param['page']]);
  101. $this->success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  102. }
  103. //***列表
  104. public function info(){
  105. $param=$this->request->param(["id"=>""],'post','trim');
  106. $valid=Validate::rule(["id"=>"require|number|gt:0"]);
  107. if($valid->check($param)==false)$this->error($valid->getError());
  108. $list=$this->model->with(['fhdChild'])->order('id desc')->findOrEmpty($param['id']);
  109. $this->success('获取成功',$list);
  110. }
  111. //***列表
  112. public function query(){
  113. $param=$this->request->param(['outChildCode'=>[],'order_type'=>'','order_source'=>'','supplierNo'=>'',
  114. 'companyNo'=>'','status'=>'','orderCode'=>[],'cgdNo'=>[],"page"=>1,"size"=>100],'post','trim');
  115. $where=[];
  116. empty($param['outChildCode'])??$where[]=['outChildCode','in',$param['outChildCode']];
  117. empty($param['cgdNo'])??$where[]=['cgdNo','in',$param['cgdNo']];
  118. empty($param['orderCode'])??$where[]=['orderCode','in',$param['orderCode']];
  119. // $param['orderCode']==''??$where[]=['orderCode','like',"%{$param['orderCode']}%"];
  120. // $param['cgdNo']==''??$where[]=['cgdNo','like',"%{$param['cgdNo']}%"];
  121. $param['order_type']==''??$where[]=['order_type','=',$param['order_type']];
  122. $param['order_source']==''??$where[]=['order_source','=',$param['order_source']];
  123. $param['supplierNo']==''??$where[]=['supplierNo','=',$param['supplierNo']];
  124. $param['companyNo']==''??$where[]=['companyNo','=',$param['companyNo']];
  125. $param['status']==''??$where[]=['status','=',$param['status']];
  126. $list=$this->model->with(['fhdChild'])->order('id desc')->paginate(['list_rows'=>$param['size'],
  127. 'page'=>$param['page']]);
  128. $this->success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  129. }
  130. public function PayAdd(){
  131. $param=$this->request->param(['cids'=>[]],'post','trim');
  132. $valid = Validate::rule(['cids|对账数据集合'=>'require|array']);
  133. if($valid->check($param)==false)$this->error($valid->getError());
  134. $PayObject = new \app\admin\model\Track();
  135. $objall= $PayObject->where(['id'=>$param['cids']])->select();
  136. if($objall->isEmpty()) $this->error('对账数据不能为空');
  137. $supplierNo= array_column($objall->toArray(),'supplierNo');
  138. $supplierName= array_column($objall->toArray(),'supplierName');
  139. if(count(array_unique($supplierNo))>1){
  140. $this->error('对账数据供应商不唯一');
  141. }
  142. $companyNo= array_column($objall->toArray(),'companyNo');
  143. $companyName= array_column($objall->toArray(),'companyName');
  144. if(count(array_unique($companyNo))>1){
  145. $this->error('对账数据业务公司不唯一');
  146. }
  147. if ($this->level == 2) {
  148. //判断是否开通供应商账号
  149. $temp = check_has_account_by_supplierNos([$supplierNo[0]]);
  150. if (isset($temp['data'][$supplierNo[0]])) $this->error( '该供应商已经开通账号,不允许当前账号操作');
  151. }
  152. $pay= new Pay();
  153. $data = [
  154. 'payNo'=>makeNo('PAY'),
  155. 'apply_id'=>$this->uid,
  156. 'apply_name'=>$this->uname,
  157. 'total_fee'=>'0',
  158. 'pay_type'=>2,
  159. 'supplierNo'=>$supplierNo[0],
  160. 'supplierName'=>$supplierName[0],
  161. 'companyNo'=>$companyNo[0],
  162. 'companyName'=>$companyName[0],
  163. 'wpay_fee'=>'0',
  164. 'apay_fee'=>'0',
  165. 'ainv_fee'=>'0',
  166. 'winv_fee'=>'0',
  167. 'remark'=>'',
  168. 'status'=>1,
  169. ];
  170. $pay->startTrans();
  171. try{
  172. $payinfo=[];
  173. foreach ($objall as $item){
  174. if($item->status!=0) throw new \Exception("{$item->TrackCode}已对账");
  175. $temp=[];
  176. $temp['cgdNo']=$item['TrackCode'];
  177. $temp['total_fee']=$item['post_fee'];
  178. $temp['apay_fee']=0;
  179. $temp['wpay_fee']=$item['post_fee'];
  180. $temp['winv_fee']=0;
  181. $temp['ainv_fee']=$item['post_fee'];
  182. $temp['payNo']=$data['payNo'];
  183. $temp['addtime']=date('Y-m-d H:i:s');
  184. $temp['updatetime']=date('Y-m-d H:i:s');
  185. $payinfo[]=$temp;
  186. $data['total_fee']=bcadd($data['total_fee'],$item['post_fee'],2);
  187. $data['wpay_fee']= bcadd($data['wpay_fee'],$item['post_fee'],2);
  188. $data['winv_fee']= bcadd($data['winv_fee'],$item['post_fee'],2);
  189. }
  190. $info =(new PayInfo())->saveAll($payinfo);
  191. if($info){
  192. $create=$pay->save($data);
  193. if($create)$objall->update(['status'=>1]);
  194. $pay->commit();
  195. }else throw new \Exception("对账单生成失败");
  196. }catch (\Exception $e){
  197. $pay->rollback();
  198. $this->error($e->getMessage());
  199. }
  200. $this->success('对账单添加成功',['payNo'=>$data['payNo']]);
  201. }
  202. /**对账单未对账前编辑8**/
  203. public function PaySave(){
  204. $param=$this->request->param(['cids'=>[],'payNo'=>''],'post','trim');
  205. $valid = Validate::rule(['cids|对账数据集合'=>'require|array','payNo|对账编号'=>'require']);
  206. if($valid->check($param)==false)$this->error($valid->getError());
  207. $PayObject = new \app\admin\model\Track();
  208. $pay= new Pay();
  209. $objall= $PayObject->where(['id'=>$param['cids']])->select();
  210. if($objall->isEmpty()) $this->error('对账数据不能为空');
  211. $PayInfo = $pay->where(['payNo'=>$param['payNo']])->findOrEmpty();
  212. if($PayInfo->isEmpty()) $this->error('未找到对账单数据');
  213. $supplierNo= array_column($objall->toArray(),'supplierNo');
  214. $supplierName= array_column($objall->toArray(),'supplierName');
  215. if(count(array_unique($supplierNo))>1){
  216. $this->error('对账数据供应商不唯一');
  217. }
  218. $companyNo= array_column($objall->toArray(),'companyNo');
  219. $companyName= array_column($objall->toArray(),'companyName');
  220. if(count(array_unique($companyNo))>1){
  221. $this->error('对账数据业务公司不唯一');
  222. }
  223. if ($this->level == 2) {
  224. //判断是否开通供应商账号
  225. $temp = check_has_account_by_supplierNos([$supplierNo[0]]);
  226. if (isset($temp['data'][$supplierNo[0]])) $this->error( '该供应商已经开通账号,不允许当前账号操作');
  227. }
  228. $trackArr = PayInfo::where(['payNo'=>$param['payNo'],'is_del'=>0,'status'=>1])->column('cgdNo');
  229. $add=$remove=[];
  230. if(empty($trackArr)==false){
  231. $remove= array_diff($trackArr,array_column($objall->toArray(),'TrackCode'));
  232. $add= array_diff(array_column($objall->toArray(),'TrackCode'),$trackArr);
  233. }
  234. $data = [
  235. 'apply_id'=>$this->uid,
  236. 'apply_name'=>$this->uname,
  237. 'total_fee'=>'0',
  238. 'supplierNo'=>$supplierNo[0],
  239. 'supplierName'=>$supplierName[0],
  240. 'companyNo'=>$companyNo[0],
  241. 'companyName'=>$companyName[0],
  242. 'wpay_fee'=>'0',
  243. 'winv_fee'=>'0',
  244. ];
  245. $pay->startTrans();
  246. try{
  247. $payinfo=[];
  248. foreach ($objall as $item){
  249. $data['total_fee']=bcadd($data['total_fee'],$item['post_fee'],2);
  250. $data['wpay_fee']= bcadd($data['wpay_fee'],$item['post_fee'],2);
  251. $data['winv_fee']= bcadd($data['winv_fee'],$item['post_fee'],2);
  252. if(!empty($add)&& in_array($item['TrackCode'],$add)){
  253. $temp=[];
  254. if($item->status!=0) throw new \Exception("{$item->TrackCode}已对账");
  255. $temp['cgdNo']=$item['TrackCode'];
  256. $temp['total_fee']=$item['post_fee'];
  257. $temp['apay_fee']=0;
  258. $temp['wpay_fee']=$item['post_fee'];
  259. $temp['winv_fee']=0;
  260. $temp['ainv_fee']=$item['post_fee'];
  261. $temp['payNo']=$param['payNo'];
  262. $temp['addtime']=date('Y-m-d H:i:s');
  263. $temp['updatetime']=date('Y-m-d H:i:s');
  264. $payinfo[]=$temp;
  265. }
  266. }
  267. if(!empty($payinfo))(new PayInfo())->saveAll($payinfo);
  268. $up = $PayInfo->save($data);
  269. if($up){
  270. $objall->update(['status'=>1]);
  271. if(!empty($remove)){
  272. $PayObject->where(['TrackCode'=>$remove,'status'=>1])->update(['status'=>0]);
  273. PayInfo::where(['cgdNo'=>$remove,'status'=>1,'is_del'=>0])->update(['status'=>0,'is_del'=>1]);
  274. }
  275. }
  276. $pay->commit();
  277. }catch (\Exception $exception){
  278. $pay->rollback();
  279. $this->error($exception->getMessage());
  280. }
  281. $this->success('更新成功');
  282. }
  283. public function delete(){
  284. $param=$this->request->param(['id'=>''],'post','trim');
  285. $valid=Validate::rule(['id'=>'require|number|gt:0']);
  286. if($valid->check($param)==false)$this->error($valid->getError());
  287. $info=$this->model->order('id desc')->findOrEmpty($param['id']);
  288. if($info->isEmpty()) $this->error("未找到数据");
  289. if($info->status!=0)$this->error('运单状态对账中');
  290. $del= $info->delete();
  291. $del? $this->success('删除成功'):$this->error('删除失败');
  292. }
  293. }