Stage.php 17 KB

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