Stage.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\Pay;
  4. use app\cxinv\model\PayInfo;
  5. use app\cxinv\model\PaymentOrder;use app\cxinv\model\PayPayment;
  6. use app\cxinv\model\PayReturn;
  7. use app\cxinv\model\RoleAction;
  8. use app\user\model\AccountCompany;
  9. use think\App;
  10. use think\facade\Validate;
  11. class Stage extends Base{
  12. public function __construct(App $app) {
  13. parent::__construct($app);
  14. $this->model =new PayPayment();
  15. }
  16. public function create(){
  17. $params = $this->request->param(["payNo"=>"","cgdNos"=>[]],"post","trim");
  18. $valid = Validate::rule([
  19. "payNo|对账单申请编号"=>"require|max:255",
  20. "cgdNos|对账单付款申请明细"=>"require|array"
  21. ]);
  22. if (!$valid->check($params)) return error($valid->getError());
  23. $pay = Pay::where(["payNo"=>$params["payNo"],"is_del"=>0])->findOrEmpty();
  24. if ($pay->isEmpty()) return error("对账单申请不存在");
  25. if($pay->status!=2) return error("对账单申请状态不正确");
  26. $cgdInfo= PayInfo::where(['payNo'=>$params['payNo'],'is_del'=>0,'status'=>1])->select();
  27. if($cgdInfo->isEmpty()) return error("对账单付款申请明细不存在");
  28. $cgdNos = $cgdInfo->column("cgdNo");
  29. $dzNo= makeNo("DZ");
  30. $val= Validate::rule([
  31. "sequenceNo|采购单号"=>"require|max:255",
  32. "rela_fee|关联付款金额"=>"require|float"]);
  33. $total_fee=0;
  34. $relaData=[];
  35. foreach ($params["cgdNos"] as $v){
  36. if(!$val->check($v)) return error($val->getError());
  37. if(!in_array($v["sequenceNo"],$cgdNos)) return error("{$v["sequenceNo"]}采购单号不在当前对账单中");
  38. $relaData[]=[
  39. "dzNo"=>$dzNo,
  40. "payNo"=>$params["payNo"],
  41. "cgdNo"=>$v["sequenceNo"],
  42. "rela_fee"=>$v["rela_fee"],
  43. "status"=>0
  44. ];
  45. $total_fee+=$v["rela_fee"];
  46. }
  47. $ment =[
  48. 'payNo'=>$params['payNo'],
  49. 'dzNo'=>$dzNo,
  50. 'apply_id'=>$this->uid,
  51. 'apply_name'=>$this->uname,
  52. 'pay_fee'=>$total_fee,
  53. 'return_img'=>'',
  54. 'status'=>1,
  55. 'addtime'=>date('Y-m-d H:i:s'),
  56. 'updatetime'=>date('Y-m-d H:i:s')
  57. ];
  58. if($total_fee>$pay->wpay_fee) return error("对账单未付金额不足");
  59. if($total_fee <=0) return error("付款金额不正确");
  60. $this->model->startTrans();
  61. try{
  62. $cerste= $this->model->create($ment);
  63. if($cerste->isEmpty()) throw new \Exception("创建付款申请失败");
  64. $relaSte= (new \app\cxinv\model\PaymentOrder)->saveAll($relaData);
  65. if(!$relaSte) throw new \Exception("创建付款申请明细失败");
  66. $pay->wpay_fee=$pay->wpay_fee-$total_fee;
  67. $pay->pay_fee = $pay->pay_fee+$total_fee;
  68. $paySte=$pay->save();
  69. if(!$paySte) throw new \Exception("更新付款申请失败");
  70. $this->model->commit();
  71. event('report_code',['type'=>'add','key'=>'dzNo','value'=>$dzNo,
  72. 'main'=>['key'=>'cgdNo','value'=>array_column($params['cgdNos'],"sequenceNo")]]);
  73. }catch (\Exception $e){
  74. $this->model->rollback();
  75. return error($e->getMessage());
  76. }
  77. return success('创建付款申请成功');
  78. }
  79. public function list(){
  80. $params= $this->request->param(["startTime"=>"","endTime"=>"","payNo"=>"","supplierNo"=>"","supplierName"=>"",
  81. "companyNo"=>"","apply_name"=>"","status"=>"","dzNo"=>"","relaComNo"=>"","is_comon"=>"",'pay_type'=>'1',"page"=>1,
  82. "size"=>20],"post","trim");
  83. $where=[['a.is_del','=',0],['pay_type','=',$params['pay_type']]];
  84. $check = RoleAction::checkRole($this->roleid, [77,117]);
  85. if ($check) $where[]=['apply_id','=',$this->uid];
  86. if ($params['startTime']!=='') $where[]=['a.addtime','>=',startTime($params['startTime'])];
  87. if ($params['endTime']!=='') $where[]=['a.addtime','<=',endTime($params['endTime'])];
  88. if ($params['supplierNo']!=='') $where[]=['supplierNo','like','%'.$params['supplierNo'].'%'];
  89. if ($params['supplierName']!=='') $where[]=['supplierName','like','%'.$params['supplierName'].'%'];
  90. if ($params['companyNo']!=='') $where[]=['companyNo','like','%'.$params['companyNo'].'%'];
  91. if ($params['apply_name']!=='') $where[]=['apply_name','like','%'.$params['apply_name'].'%'];
  92. if ($params['status']!=='') $where[]=['a.status','=',$params['status']];
  93. if ($params['dzNo']!=='') $where[]=['dzNo','like','%'.$params['dzNo'].'%'];
  94. if ($params['payNo']!=='') $where[]=['a.payNo','like','%'.$params['payNo'].'%'];
  95. if ($params['relaComNo']!=='') $where[]=['companyNo|supplierNo','like','%'.$params['relaComNo'].'%'];
  96. if ($params['is_comon']!=='') $where[]=['is_comon','=',$params['is_comon']];
  97. $list = $this->model->alias("a")
  98. ->leftJoin("pay b","`a`.`payNo` = `b`.`payNo` AND b.is_del = 0 ")
  99. ->where($where)
  100. ->field("`a`.`id` AS `id`,
  101. `a`.`payNo` AS `payNo`,
  102. `b`.`supplierNo`,
  103. `b`.`supplierName`,
  104. `b`.`apay_fee` ,
  105. `b`.`total_fee` AS `total_fee`,
  106. `a`.`apply_name`,
  107. `a`.`apply_id`,
  108. `b`.`is_comon`,
  109. `b`.`winv_fee` ,
  110. `b`.`ainv_fee` ,
  111. `b`.`wpay_fee` ,
  112. `b`.`pay_status` ,
  113. `b`.`inv_status` ,
  114. `a`.`pay_fee` AS `dpay_fee`,
  115. `a`.`return_img`,
  116. `a`.`return_time`,
  117. `a`.`status` AS `dstatus`,
  118. `b`.`status`,
  119. `b`.`remark` AS `remark`,
  120. `a`.`addtime` ,
  121. `b`.`companyNo` ,
  122. `b`.`companyName` ,
  123. `a`.`dzNo` AS `dzNo`")
  124. ->order("a.id DESC")
  125. ->paginate(['list_rows'=>$params['size'],'page'=>$params['page']]);
  126. foreach ($list->items() as $k=>&$v){
  127. $v['orderNum'] = PayInfo::where([['payNo','=',$v['payNo']],['is_del','=',0],['status','=',1]])->count();
  128. $v['has_account'] = AccountCompany::SupplierHasAcount($v['supplierNo']);
  129. }
  130. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  131. }
  132. public function status(){
  133. $params=$this->request->param(["dzNo"=>"","status"=>"","remark"=>"","return_image"=>"","return_time"=>""],"post","trim");
  134. $valid =Validate::rule([
  135. "dzNo|付款申请编号"=>"require|max:255",
  136. "status|审核状态"=>"require|number|in:1,2,3,4,5,6,7",
  137. "remark|备注"=>"max:255",
  138. "return_image|回执图片"=>"requireIf:status=4|max:255",
  139. "return_time|回执时间"=>"requireIf:status=4|date"
  140. ]);
  141. if(!$valid->check($params)) return error($valid->getError());
  142. $stage = $this->model->where([['dzNo','=',$params['dzNo']],['is_del','=',0]])->findOrEmpty();
  143. if($stage->isEmpty()) return error("付款申请不存在");
  144. $pay = Pay::where([['payNo','=',$stage['payNo']],['is_del','=',0]])->findOrEmpty();
  145. if($pay->isEmpty()) return error("对账申请单不存在");
  146. if($pay->status!=2) return error("对账单状态不正确");
  147. if($pay->pay_fee<$stage['pay_fee']) return error("对账付款申请金额有误,请确认对账申请金额");
  148. $this->model->startTrans();
  149. try{
  150. $stage->status=$params['status'];
  151. $stage->remark=$params['remark'];
  152. if($params['status']==4){
  153. $stage->return_img=$params['return_image'];
  154. $stage->return_time=$params['return_time'];
  155. }
  156. $stageSte=$stage->save();
  157. if(!$stageSte) throw new \Exception("更新付款申请状态失败");
  158. $this->model->commit();
  159. }catch (\Exception $e){
  160. $this->model->rollback();
  161. return error($e->getMessage());
  162. }
  163. return success('更新付款申请状态成功');
  164. }
  165. public function delete(){
  166. $dzNo=$this->request->param("dzNo","post","trim");
  167. $valid =Validate::rule([
  168. "dzNo|付款申请编号"=>"require|max:255"
  169. ]);
  170. if(!$valid->check(['dzNo'=>$dzNo])) return error($valid->getError());
  171. $stage = $this->model->where([['dzNo','=',$dzNo],['is_del','=',0]])->findOrEmpty();
  172. if($stage->isEmpty()) return error("付款申请不存在");
  173. $pay = Pay::where([['payNo','=',$stage['payNo']],['is_del','=',0]])->findOrEmpty();
  174. if($pay->isEmpty()) return error("对账申请单不存在");
  175. if($stage->status==4) return error("已回执的付款申请不能删除");
  176. $this->model->startTrans();
  177. try{
  178. $stage->status=8;
  179. $stage->is_del=1;
  180. $stageSte=$stage->save();
  181. if(!$stageSte) throw new \Exception("删除付款申请失败");
  182. $this->model->commit();
  183. }catch (\Exception $e){
  184. $this->model->rollback();
  185. return error($e->getMessage());
  186. }
  187. return success('删除付款申请成功');
  188. }
  189. public function info(){
  190. $dzNo=$this->request->param("dzNo","post","trim");
  191. $valid =Validate::rule([
  192. "dzNo|付款申请编号"=>"require|max:255"
  193. ]);
  194. if(!$valid->check(['dzNo'=>$dzNo])) return error($valid->getError());
  195. $stage = $this->model->where([['dzNo','=',$dzNo],['is_del','=',0]])->findOrEmpty();
  196. if($stage->isEmpty()) return error("付款申请不存在");
  197. $pay = Pay::where([['payNo','=',$stage['payNo']],['is_del','=',0]])->findOrEmpty();
  198. if($pay->isEmpty()) return error("对账申请单不存在");
  199. $stage['pay_apply_id'] = $pay['apply_id']??'';
  200. $stage['pay_apply_name'] = $pay['apply_name']??'';
  201. $stage['supplierNo'] = $pay['supplierNo']??'';
  202. $stage['supplierName'] = $pay['supplierName']??'';
  203. $stage['companyNo'] = $pay['companyNo']??'';
  204. $stage['companyName'] = $pay['companyName']??'';
  205. $stage['total_fee'] = $pay['total_fee']??'';
  206. $stage['cgdlist'] = PaymentOrder::alias("a")
  207. ->leftJoin("cgd_info b","a.cgdNo=b.sequenceNo")
  208. ->where(["dzNo"=>$stage['dzNo'],"b.is_del"=>0])
  209. ->field("b.*,a.id rela_id,dzNo,cgdNo,rela_fee,a.status as rela_status")
  210. ->select();
  211. return success('获取成功',$stage);
  212. }
  213. public function Back(){
  214. $params=$this->request->param(["dzNo"=>"","reason"=>"","is_del"=>0],"post","trim");
  215. $valid =Validate::rule([
  216. "dzNo|付款申请编号"=>"require|max:255",
  217. "reason|退款原因"=>"require|max:255"
  218. ]);
  219. if(!$valid->check($params)) return error($valid->getError());
  220. $stage = $this->model->where([['dzNo','=',$params['dzNo']],['is_del','=',0]])->findOrEmpty();
  221. if($stage->isEmpty()) return error("付款申请不存在");
  222. if($stage->status!=4) return error("付款申请状态不正确");
  223. $pay = Pay::where([['payNo','=',$stage['payNo']],['is_del','=',0]])->findOrEmpty();
  224. if($pay->isEmpty()) return error("对账申请单不存在");
  225. if($this->level==2 && $pay->is_comon==0){
  226. $boolen= AccountCompany::SupplierHasAcount($pay->supplierNo);
  227. if($boolen) return error("该供应商已经开通账号,不允许当前账号操作");
  228. }
  229. $isR=PayReturn::where(['orderCode'=>$params['dzNo'],'status'=>[1,2],'is_del'=>0])->findOrEmpty();
  230. if(!$isR->isEmpty())return error_show(1004,'付款信息退款流程已存在');
  231. $reCode= makeNo("RP");
  232. $data = [
  233. 'returnCode'=>$reCode,
  234. 'returnType'=>2,
  235. 'payNo'=>$stage['payNo'],
  236. 'orderCode'=>$stage['dzNo'],
  237. 'reason'=>$params['reason'],
  238. 'returnImg'=>'',
  239. 'remark'=>'',
  240. 'status'=>1,
  241. 'apply_id'=>$this->uid,
  242. 'apply_name'=>$this->uname,
  243. 'is_del'=>0,
  244. 'addtime'=>date('Y-m-d H:i:s'),
  245. 'updatetime'=>date('Y-m-d H:i:s'),
  246. ];
  247. $this->model->startTrans();
  248. try{
  249. $create= PayReturn::create($data);
  250. if(!$create) throw new \Exception("退款申请创建失败");
  251. $this->model->commit();
  252. }catch (\Exception $e){
  253. $this->model->rollback();
  254. return error($e->getMessage());
  255. }
  256. return success('退款款申请创建成功');
  257. }
  258. public function BackList(){
  259. $param =$this->request->only(['relaComNo'=>'','companyNo'=>'','supplierNo'=>'','start'=>'','end'=>'','returnCode'=>'','payNo'=>'',
  260. 'status'=>'','dzNo'=>'','pay_type'=>1,'is_comon'=>'','page'=>1,'size'=>15],'post','trim');
  261. $condition=[['a.is_del','=',0],['a.returnType','=',2],['pay_type','=',$param['pay_type']]];
  262. $check = RoleAction::checkRole($this->roleid,[131,129]);
  263. if($check) $condition[]=['a.apply_id','=',$this->uid];
  264. if($param['relaComNo']!='') $condition[]=['b.companyNo|b.supplierNo','=',$param['relaComNo']];
  265. if($param['companyNo']!='') $condition[]=['b.companyNo','=',$param['companyNo']];
  266. if($param['supplierNo']!='') $condition[]=['b.supplierNo','=',$param['supplierNo']];
  267. if($param['start']!='') $condition[]=['a.addtime','>=',date('Y-m-d H:i:s',strtotime($param['start']))];
  268. if($param['end']!='') $condition[]=['a.addtime','<=',date('Y-m-d 23:59:59',strtotime($param['end']))];
  269. if($param['status']!='') $condition[]=['a.status','=',$param['status']];
  270. if($param['dzNo']!='') $condition[]=['a.orderCode','like',"%{$param['dzNo']}%"];
  271. if($param['returnCode']!='') $condition[]=['a.returnCode','like',"%{$param['returnCode']}%"];
  272. if($param['payNo']!='') $condition[]=['a.payNo','like',"%{$param['payNo']}%"];
  273. if(isset($param['is_comon'])&&$param['is_comon']!=='') $condition []=['is_comon','=',$param['is_comon']];
  274. $list = PayReturn::alias('a')
  275. ->leftJoin('pay b','a.payNo=b.payNo and b.is_del=0')
  276. ->leftJoin('pay_payment c','c.dzNo=a.orderCode and c.is_del=0')
  277. ->where($condition)
  278. ->field('a.*,b.supplierNo,b.is_comon,b.supplierName,b.companyNo,b.companyName,b.total_fee,b.apay_fee,b.wpay_fee,b.pay_fee,c.pay_fee pay_pay_fee')
  279. ->order('a.id desc')
  280. ->paginate(["list_rows"=>$param['size'],"page"=>$param['page']]);
  281. foreach ($list->items() as $key => &$value){
  282. $value['has_account'] = AccountCompany::SupplierHasAcount($value['supplierNo']);
  283. }
  284. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  285. }
  286. public function BackStatus(){
  287. $params = $this->request->param(["returnCode"=>"","status"=>"","remark"=>"","returnImg"=>""],"post","trim");
  288. $valid =Validate::rule([
  289. "returnCode|退款单号"=>"require|max:255",
  290. "status|退款状态"=>"require|in:2,3",
  291. "remark|备注"=>"requireIf:status,3|max:255",
  292. "returnImg|退款凭证"=>"max:255"
  293. ]);
  294. if(!$valid->check($params)) return error($valid->getError());
  295. $return = PayReturn::where([['returnCode','=',$params['returnCode']],['is_del','=',0]])->findOrEmpty();
  296. if($return->isEmpty()) return error("退款申请不存在");
  297. $return->status=$params['status'];
  298. $return->remark=$params['remark'];
  299. $return->returnImg=$params['returnImg'];
  300. $this->model->startTrans();
  301. try{
  302. $returnSte=$return->save();
  303. if(!$returnSte) throw new \Exception("退款申请状态修改失败");
  304. if($params['status']==2){
  305. $stage = $this->model->where([['dzNo','=',$return['orderCode']],['is_del','=',0]])->findOrEmpty();
  306. if($stage->isEmpty()) throw new \Exception("付款申请不存在");
  307. $stage->status=5;
  308. $stageSte=$stage->save();
  309. if(!$stageSte) throw new \Exception("付款申请状态修改失败");
  310. }
  311. $this->model->commit();
  312. }catch (\Exception $e){
  313. $this->model->rollback();
  314. return error($e->getMessage());
  315. }
  316. return success("退款申请状态修改成功");
  317. }
  318. public function BackDetail(){
  319. $param = $this->request->only(['returnCode'=>''],'post','trim');
  320. $valide=Validate::rule([
  321. 'returnCode|退票申请编号'=>'require|max:255',
  322. ]);
  323. if($valide->check($param)==false)return error($valide->getError());
  324. $detail=PayReturn::alias('a')
  325. ->leftJoin('pay b','a.payNo=b.payNo and b.is_del=0')
  326. ->leftJoin('pay_payment c','a.orderCode=c.dzNo')
  327. ->where(['returnCode'=>$param['returnCode']])
  328. ->field('a.*,b.supplierNo,b.supplierName,b.companyNo,b.companyName,b.total_fee,b.apay_fee,b.wpay_fee,b.pay_fee,c.pay_fee pay_pay_fee')
  329. ->findOrEmpty();
  330. if($detail->isEmpty())return error("退票申请不存在");
  331. return success("获取成功",$detail);
  332. }
  333. }