Stage.php 17 KB

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