Stage.php 17 KB

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