Payment.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\BaseController;
  5. use app\admin\model\ReportCode;
  6. use think\facade\Db;
  7. class Payment extends BaseController
  8. {
  9. /**
  10. *付款详细列表
  11. * @return \think\Response
  12. */
  13. public function paymentList()
  14. {
  15. $post = $this->post;
  16. $condition = "a.is_del=0 ";
  17. // $check = checkRole($this->roleid,49);
  18. // if($check){
  19. // $condition .=" and `a`.`apply_id` = {$this->uid}";
  20. // }
  21. $startTime= isset($post['startTime'])&&$post['startTime']!="" ? trim($post['startTime']) :"";
  22. $endTime= isset($post['endTime'])&&$post['endTime']!="" ? trim($post['endTime']) :"";
  23. $supplierNo= isset($post['supplierNo'])&&$post['supplierNo']!="" ? trim($post['supplierNo']) :"";
  24. $payNo= isset($post['payNo'])&&$post['payNo']!="" ? trim($post['payNo']) :"";
  25. $apply= isset($post['apply_name'])&&$post['apply_name']!="" ? trim($post['apply_name']) :"";
  26. $paystatus= isset($post['status'])&&$post['status']!="" ? $post['status'] :"";
  27. if($startTime!=""){
  28. $condition.=" and `b`.`addtime` >= '{$startTime}'";
  29. }
  30. if($endTime!=""){
  31. $condition.=" and `b`.`addtime` <= '{$endTime}'";
  32. }
  33. if($supplierNo!=""){
  34. $condition.=" and `a`.`supplierNo` like '%{$supplierNo}%'";
  35. }
  36. if($payNo!=""){
  37. $condition.=" and `a`.`payNo` like '%{$payNo}%'";
  38. }
  39. if($apply!=""){
  40. $condition .=" and b.apply_name like '%{$apply}%'";
  41. }
  42. if($paystatus!=""){
  43. $condition .=" and `b`.`status` = {$paystatus}";
  44. }
  45. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :1;
  46. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  47. $count = Db::name("pay_payment")->alias('b')
  48. ->join("pay a","`a`.`payNo` = `b`.`payNo` AND b.is_del = 0 ",'left')
  49. ->where($condition)->count();
  50. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  51. $page = $page>=$total?intval($total):$page;
  52. $list = Db::name("pay_payment")->alias('b')
  53. ->join("pay a","`a`.`payNo` = `b`.`payNo` AND ( `b`.`is_del` = 0 ) ",'left')
  54. ->where($condition)->page(intval($page),$size)->order("b.addtime desc")
  55. ->field(" `b`.`id` AS `id`,
  56. `a`.`payNo` AS `payNo`,
  57. `a`.`supplierNo`,
  58. `a`.`apay_fee` ,
  59. `a`.`total_fee` AS `total_fee`,
  60. `b`.`apply_name`,
  61. `b`.`apply_id`,
  62. `a`.`winv_fee` ,
  63. `a`.`ainv_fee` ,
  64. `a`.`wpay_fee` ,
  65. `a`.`pay_status` ,
  66. `a`.`inv_status` ,
  67. `b`.`pay_fee` AS `dpay_fee`,
  68. `b`.`return_img`,
  69. `b`.`return_time`,
  70. `b`.`status` AS `dstatus`,
  71. `a`.`status`,
  72. `a`.`remark` AS `remark`,
  73. `b`.`addtime` ,
  74. `a`.`companyNo` ,
  75. `a`.`companyName` ,
  76. `b`.`dzNo` AS `dzNo`
  77. ")->select();
  78. $data=[];
  79. foreach ($list as $key=>$value){
  80. $value['ordeNum'] = Db::name("pay_info")->where([["payNo","=",$value["payNo"]],["is_del","=",0]])->count();
  81. $data[]=$value;
  82. }
  83. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  84. }
  85. /**
  86. *对账单列表
  87. * @return \think\Response
  88. */
  89. public function payList()
  90. {
  91. $post = $this->post;
  92. $condition = [["is_del","=",0]];
  93. $check = checkRole($this->roleid,49);
  94. if($check){
  95. $condition[]=["apply_id","=",$this->uid];
  96. }
  97. $startTime= isset($post['startTime'])&&$post['startTime']!="" ? trim($post['startTime']) :"";
  98. $endTime= isset($post['endTime'])&&$post['endTime']!="" ? trim($post['endTime']) :"";
  99. $supplierNo= isset($post['supplierNo'])&&$post['supplierNo']!="" ? trim($post['supplierNo']) :"";
  100. $payNo= isset($post['payNo'])&&$post['payNo']!="" ? trim($post['payNo']) :"";
  101. $paystatus= isset($post['pay_status'])&&$post['pay_status']!="" ? $post['pay_status'] :"";
  102. $inv_status= isset($post['inv_status'])&&$post['inv_status']!="" ? $post['inv_status'] :"";
  103. $status= isset($post['status'])&&$post['status']!="" ? $post['status'] :"";
  104. if($startTime!=""){
  105. $condition[]=["addtime",">=",$startTime];
  106. }
  107. if($endTime!=""){
  108. $condition[]=["addtime","<=",$endTime];
  109. }
  110. if($supplierNo!=""){
  111. $condition[]=["supplierNo","like","%$supplierNo%"];
  112. }
  113. if($payNo!=""){
  114. $condition[]=["payNo","like","%$payNo%"];
  115. }
  116. if($paystatus!=""){
  117. $condition[]=["pay_status","=",$paystatus];
  118. }
  119. if($inv_status!=""){
  120. $condition[]=["inv_status","=",$inv_status];
  121. }
  122. if($status!=""){
  123. $condition[]=["status","=",$status];
  124. }
  125. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :1;
  126. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  127. $count = Db::name("pay")->where($condition)->count();
  128. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  129. $page = $page>=$total?intval($total):$page;
  130. $list = Db::name("pay")->where($condition)->page(intval($page),$size)->order("addtime desc")->select();
  131. $data=[];
  132. foreach ($list as $key=>$value){
  133. $value['ordeNum'] = Db::name("pay_info")->where([["payNo","=",$value["payNo"]],["is_del","=",0]])->count();
  134. $data[]=$value;
  135. }
  136. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  137. }
  138. /**
  139. * 新建对账付款数据
  140. * @return \think\Response
  141. */
  142. public function PayAdd()
  143. {
  144. $post = $this->post;
  145. $cids = isset($post['cids'])&&$post['cids']!="" ? trim($post['cids']) :"";
  146. if($cids==""){
  147. return error_show(1003,"参数cids不能为空");
  148. }
  149. $cgdall =Db::name("cgd_info")->where(["id"=>explode(",",$cids)])->select()->toArray();
  150. if(empty($cgdall)){
  151. return error_show(1003,"采购单数据不能为空");
  152. }
  153. $statusAll = array_column($cgdall,"status");
  154. if(in_array(1,$statusAll)){
  155. return error_show(1003,"采购单存在已对账数据");
  156. }
  157. if(in_array(2,$statusAll)){
  158. return error_show(1003,"采购单存在不核算数据");
  159. }
  160. $supplierNo= array_column($cgdall,"supplierNo");
  161. $supplierName= array_column($cgdall,"supplierName");
  162. if(count(array_unique($supplierNo))>1){
  163. return error_show(1003,"采购单数据供应商不一致");
  164. }
  165. $companyNo= array_column($cgdall,"companyNo");
  166. $companyName= array_column($cgdall,"companyName");
  167. if(count(array_unique($companyNo))>1){
  168. return error_show(1003,"采购单数据业务公司不一致");
  169. }
  170. $payNo = makeNo("PAY");
  171. Db::startTrans();
  172. try {
  173. $data=[];
  174. $paydata=[
  175. "payNo"=>$payNo,
  176. "apply_id"=>$this->uid,
  177. "apply_name"=>$this->uname,
  178. "total_fee"=>0,
  179. "supplierNo"=>$supplierNo[0],
  180. "supplierName"=>$supplierName[0],
  181. "companyNo"=>$companyNo[0],
  182. "companyName"=>$companyName[0],
  183. "wpay_fee"=>0,
  184. "apay_fee"=>0,
  185. "ainv_fee"=>0,
  186. "winv_fee"=>0,
  187. "remark"=>"",
  188. "status"=>0,
  189. "addtime"=>date("Y-m-d H:i:s"),
  190. "updatetime"=>date("Y-m-d H:i:s"),
  191. ];
  192. foreach ($cgdall as $key=>$value){
  193. $temp=[];
  194. $temp['cgdNo']=$value['sequenceNo'];
  195. $temp['total_fee']=$value['totalPrice'];
  196. $temp['apay_fee']=round($value['apay_fee'],2);
  197. $temp['wpay_fee']=round($value['wpay_fee'],2);
  198. $temp['winv_fee']=round($value['winv_fee'],2);
  199. $temp['ainv_fee']=round($value['ainv_fee'],2);
  200. $temp['payNo']=$payNo;
  201. $temp['addtime']=date("Y-m-d H:i:s");
  202. $temp['updatetime']=date("Y-m-d H:i:s");
  203. $data[]=$temp;
  204. $paydata['total_fee']+= $temp['total_fee'];
  205. $paydata['apay_fee']+= $temp['apay_fee'];
  206. $paydata['wpay_fee']+= $temp['wpay_fee'];
  207. $paydata['ainv_fee']+= $temp['ainv_fee'];
  208. $paydata['winv_fee']+= $temp['winv_fee'];
  209. $report=ReportCode::where(["cgdNo"=>$value['sequenceNo']])->find();
  210. if($report)$report->setField("payNo",$payNo);
  211. }
  212. $paydata['total_fee']= round($paydata['total_fee'],2);
  213. $paydata['apay_fee']= round($paydata['apay_fee'],2);
  214. $paydata['wpay_fee']= round($paydata['wpay_fee'],2);
  215. $paydata['ainv_fee']= round($paydata['ainv_fee'],2);
  216. $paydata['winv_fee']= round($paydata['winv_fee'],2);
  217. $info = Db::name("pay_info")->insertAll($data);
  218. if($info!=0){
  219. $payDA=Db::name("pay")->insert($paydata);
  220. if($payDA){
  221. $cgddup= Db::name("cgd_info")->where("id","in",$cids)->save(['status'=>1,"updatetime"=>date("Y-m-d
  222. H:i:s")]);
  223. if($cgddup){
  224. Db::commit();
  225. return app_show(0,"添加成功",["payNo"=>$payNo]);
  226. }
  227. }
  228. }
  229. Db::rollback();
  230. return error_show(1003,"添加失败");
  231. }catch(\Exception $e){
  232. Db::rollback();
  233. return error_show(1003,$e->getMessage());
  234. }
  235. }
  236. /**对账单未审核前可需改编辑
  237. * @return \think\response\Json|void
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\DbException
  240. * @throws \think\db\exception\ModelNotFoundException
  241. * @throws \think\exception\DbException
  242. */
  243. public function PaySave()
  244. {
  245. $post = $this->post;
  246. $cids = isset($post['cids'])&&$post['cids']!="" ? trim($post['cids']) :"";
  247. if($cids==""){
  248. return error_show(1003,"参数cids不能为空");
  249. }
  250. $payNo = isset($post['payNo'])&&$post['payNo']!="" ? trim($post['payNo']) :"";
  251. if($payNo==""){
  252. return error_show(1003,"参数 payNo 不能为空");
  253. }
  254. $payinfo = Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  255. if($payinfo==false){
  256. return error_show(1003,"未找到对账单数据");
  257. }
  258. if($payinfo['status']!=1){
  259. return error_show(1003,"对账单已提交审核");
  260. }
  261. $cgdall =Db::name("cgd_info")->where(["id"=>explode(",",$cids)])->select()->toArray();
  262. if(empty($cgdall)){
  263. return error_show(1003,"采购单数据不能为空");
  264. }
  265. $supplierNo= array_column($cgdall,"supplierNo");
  266. $supplierName= array_column($cgdall,"supplierName");
  267. if(count(array_unique($supplierNo))>1){
  268. return error_show(1003,"采购单数据供应商不一致");
  269. }
  270. $companyNo= array_column($cgdall,"companyNo");
  271. $companyName= array_column($cgdall,"companyName");
  272. if(count(array_unique($companyNo))>1){
  273. return error_show(1003,"采购单数据业务公司不一致");
  274. }
  275. $cgdArr=array_column($cgdall,"sequenceNo");
  276. $cgdNo = Db::name("pay_info")->where([["payNo","=",$payNo],["is_del","=",0]])->column("cgdNo");
  277. $add=[];
  278. $remove=[];
  279. if(!empty($cgdNo)){
  280. $remove = array_diff($cgdNo,$cgdArr);
  281. $add = array_diff($cgdArr,$cgdNo);
  282. }
  283. Db::startTrans();
  284. try {
  285. $data=[];
  286. $paydata=[
  287. "supplierNo"=>$supplierNo[0],
  288. "supplierName"=>$supplierName[0],
  289. "companyNo"=>$companyNo[0],
  290. "companyName"=>$companyName[0],
  291. "wpay_fee"=>0,
  292. "apay_fee"=>0,
  293. "ainv_fee"=>0,
  294. "winv_fee"=>0,
  295. "total_fee"=>0,
  296. "updatetime"=>date("Y-m-d H:i:s")
  297. ];
  298. foreach ($cgdall as $key=>$value){
  299. if(!empty($add)&& in_array($value['sequenceNo'],$add)){
  300. $temp=[];
  301. $temp['cgdNo']=$value['sequenceNo'];
  302. $temp['total_fee']=$value['totalPrice'];
  303. $temp['apay_fee']=round($value['apay_fee'],2);
  304. $temp['wpay_fee']=round($value['wpay_fee'],2);
  305. $temp['winv_fee']=round($value['winv_fee'],2);
  306. $temp['ainv_fee']=round($value['ainv_fee'],2);
  307. $temp['payNo']=$payNo;
  308. $temp['addtime']=date("Y-m-d H:i:s");
  309. $temp['updatetime']=date("Y-m-d H:i:s");
  310. $data[]=$temp;
  311. $report=ReportCode::where(["cgdNo"=>$value['sequenceNo']])->find();
  312. if($report)$report->setField("payNo",$payNo);
  313. }
  314. $paydata['total_fee']+= $value['totalPrice'];
  315. $paydata['apay_fee']+= $value['apay_fee'];
  316. $paydata['wpay_fee']+= $value['wpay_fee'];
  317. $paydata['ainv_fee']+= $value['ainv_fee'];
  318. $paydata['winv_fee']+= $value['winv_fee'];
  319. }
  320. $paydata['total_fee']= round($paydata['total_fee'],2);
  321. $paydata['apay_fee']= round($paydata['apay_fee'],2);
  322. $paydata['wpay_fee']= round($paydata['wpay_fee'],2);
  323. $paydata['ainv_fee']= round($paydata['ainv_fee'],2);
  324. $paydata['winv_fee']= round($paydata['winv_fee'],2);
  325. $payDA=Db::name("pay")->where($payinfo)->update($paydata);
  326. if($payDA){
  327. if(!empty($remove)){
  328. foreach ($remove as $value){
  329. $report=ReportCode::where(["cgdNo"=>$value])->find();
  330. if($report)$report->rmField("payNo",$payNo);
  331. }
  332. $payrm = Db::name("pay_info")->where(["cgdNo"=>$remove,"is_del"=>0])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  333. if($payrm==false){
  334. Db::rollback();
  335. return error_show(1004,"修改失败");
  336. }
  337. $cgdrm= Db::name("cgd_info")->where([["sequenceNo","in",$remove],["status","=",1]])->save(['status'=>0, "updatetime"=>date("Y-m-d H:i:s")]);
  338. if($cgdrm==false){
  339. Db::rollback();
  340. return error_show(1005,"修改失败");
  341. }
  342. }
  343. if(!empty($add)){
  344. $cgddup= Db::name("cgd_info")->where([["sequenceNo","in",$add],["status","=",0]])->save(['status'=>1, "updatetime"=>date("Y-m-d H:i:s")]);
  345. if($cgddup==false){
  346. Db::rollback();
  347. return error_show(1006,"修改失败");
  348. }
  349. }
  350. if(!empty($data)){
  351. $info = Db::name("pay_info")->insertAll($data);
  352. if($info==false){
  353. Db::rollback();
  354. return error_show(1003,"修改失败");
  355. }
  356. }
  357. Db::commit();
  358. return app_show(0,"修改成功",["payNo"=>$payNo]);
  359. }
  360. Db::rollback();
  361. return error_show(1007,"添加失败");
  362. }catch(\Exception $e){
  363. Db::rollback();
  364. return error_show(1003,$e->getMessage());
  365. }
  366. }
  367. /**
  368. * 对账付款状态审核 status 0 待提交 1 待采购审核 2 待财务审核 3 审核成功 4 采购驳回 5 财务驳回
  369. * @param \think\Request $request
  370. * @return \think\Response
  371. */
  372. public function PayStatus()
  373. {
  374. $post = $this->post;
  375. $payNo = isset($post['payNo']) && $post['payNo']!="" ? trim($post['payNo']):"";
  376. if($payNo==""){
  377. return error_show(1004,"参数payNo不能为空");
  378. }
  379. $status= isset($post['status']) && $post['status']!=="" ? trim($post['status']):"";
  380. if($status===""){
  381. return error_show(1004,"参数status不能为空");
  382. }
  383. $data = Db::name("pay")->where([["payNo","=",$payNo],["is_del","=",0]])->find();
  384. if($data==false){
  385. return error_show(1004,"未能找到对应数据");
  386. }
  387. $remark = isset($post['remark']) && $post['remark']!=""? trim($post['remark']):"";
  388. $cgdNo = Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  389. if(empty($cgdNo)){
  390. return error_show(1004,"未能找到对应采购单数据");
  391. }
  392. Db::startTrans();
  393. try{
  394. $update=[
  395. "status"=>$status,
  396. "remark"=>$remark,
  397. "updatetime"=>date("Y-m-d H:i:s")
  398. ];
  399. $result = Db::name("pay")->where("payNo","=",$payNo)->update($update);
  400. if($result){
  401. if($status==4 || $status==5){
  402. $cgdup =Db::name("cgd_info")->where(["sequenceNo"=>$cgdNo,"status"=>1])->update(['status'=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  403. if($cgdup==false){
  404. Db::rollback();
  405. return error_show(1004,"对账驳回失败");
  406. }
  407. foreach ($cgdNo as $value){
  408. $report=ReportCode::where(["cgdNo"=>$value])->find();
  409. if($report)$report->rmField("payNo",$payNo);
  410. }
  411. }
  412. Db::commit();
  413. return app_show(0,"状态更新成功");
  414. }
  415. Db::rollback();
  416. return error_show(1004,"状态更新失败");
  417. }catch (\Exception $e){
  418. Db::rollback();
  419. return error_show(1004,$e->getMessage());
  420. }
  421. }
  422. /**删除未审核完成的对账单
  423. * @return \think\response\Json|void
  424. * @throws \think\db\exception\DataNotFoundException
  425. * @throws \think\db\exception\DbException
  426. * @throws \think\db\exception\ModelNotFoundException
  427. */
  428. public function payDel(){
  429. $post = $this->post;
  430. $payNo = isset($post['payNo']) && $post['payNo']!="" ? trim($post['payNo']):"";
  431. if($payNo==""){
  432. return error_show(1004,"参数payNo不能为空");
  433. }
  434. $data = Db::name("pay")->where([["payNo","=",$payNo],["is_del","=",0]])->find();
  435. if($data==false){
  436. return error_show(1004,"未能找到对应数据");
  437. }
  438. if($data['status']==4){
  439. return error_show(1004,"对账单已审核通过");
  440. }
  441. $cgdNo = Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  442. if(empty($cgdNo)){
  443. return error_show(1004,"未能找到对应采购单数据");
  444. }
  445. Db::startTrans();
  446. try{
  447. $payDel = ["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
  448. $pay =Db::name("pay")->where($data)->update($payDel);
  449. if($pay){
  450. $infoup =Db::name("pay_info")->where(["cgdNo"=>$cgdNo,"is_del"=>0])->update($payDel);
  451. if($infoup==false){
  452. Db::rollback();
  453. return error_show(1004,"对账删除失败");
  454. }
  455. $cgdup =Db::name("cgd_info")->where(["sequenceNo"=>$cgdNo,"status"=>1])->update(['status'=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  456. if($cgdup==false){
  457. Db::rollback();
  458. return error_show(1004,"对账删除失败");
  459. }
  460. foreach ($cgdNo as $value){
  461. $report=ReportCode::where(["cgdNo"=>$value])->find();
  462. if($report)$report->rmField("payNo",$payNo);
  463. }
  464. Db::commit();
  465. return error_show(0,"对账删除成功");
  466. }
  467. Db::rollback();
  468. return error_show(1004,"对账删除失败");
  469. }catch (\Exception $e){}
  470. }
  471. /**
  472. *
  473. * @param int $id
  474. * @return \think\Response
  475. */
  476. public function stageAdd()
  477. {
  478. $post = $this->post;
  479. $payNo = isset($post['payNo'])&& $post['payNo']!="" ? trim($post['payNo']) :"";
  480. if($payNo==""){
  481. return error_show(1004,"参数payNo 不能为空");
  482. }
  483. $payinfo =Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  484. if($payinfo==false){
  485. return error_show(1004,"未找到对账信息");
  486. }
  487. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  488. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  489. if($payinfo['status']!=3){
  490. return error_show(1004,"对账未审核完成");
  491. }
  492. $pay_fee = isset($post['pay_fee'])&& $post['pay_fee']!="" ? floatval($post['pay_fee']) :"";
  493. if($pay_fee==""){
  494. return error_show(1004,"参数 pay_fee 不能为空或零");
  495. }
  496. if($payinfo['wpay_fee']< $pay_fee){
  497. return error_show(1004,"对账单未付金额不足");
  498. }
  499. $dzno=makeNo("DZ");
  500. Db::startTrans();
  501. try {
  502. $ment =[
  503. "payNo"=>$payNo,
  504. "dzNo"=>$dzno,
  505. "apply_id"=>$this->uid,
  506. "apply_name"=>$this->uname,
  507. "pay_fee"=>$pay_fee,
  508. "return_img"=>'',
  509. "status"=>0,
  510. "addtime"=>date("Y-m-d H:i:s"),
  511. "updatetime"=>date("Y-m-d H:i:s")
  512. ];
  513. $payment =Db::name("pay_payment")->insert($ment);
  514. if($payment){
  515. $update=[
  516. "pay_fee"=> round($payinfo['pay_fee']+$pay_fee,2),
  517. "wpay_fee"=> round($payinfo['wpay_fee']-$pay_fee,2),
  518. "updatetime"=>date("Y-m-d H:i:s")
  519. ];
  520. $payup = Db::name("pay")->where($payinfo)->update($update);
  521. if($payup){
  522. foreach ($cgdNo as $value){
  523. $report=ReportCode::where(["cgdNo"=>$value])->find();
  524. if($report)$report->setField("DzNo",$dzno);
  525. }
  526. Db::commit();
  527. return app_show(0,"付款申请添加成功",["dzNo"=>$dzno]);
  528. }
  529. }
  530. Db::rollback();
  531. return error_show(1004,"付款申请添加失败");
  532. }catch (\Exception $e){
  533. Db::rollback();
  534. return error_show(1005,$e->getMessage());
  535. }
  536. }
  537. /**
  538. * 付款申请审核状态
  539. * 付款审核状态 0待发起 1待业务审核 2 待财务审核 3待付款回执 4 付款回执 5 业务驳回 6 财务驳回
  540. * @param int $id
  541. * @return \think\Response
  542. */
  543. public function stageStatus()
  544. {
  545. $post = $this->post;
  546. $dzNo = isset($post['dzNo'])&& $post['dzNo']!="" ? trim($post['dzNo']) :"";
  547. if($dzNo==""){
  548. return error_show(1004,"参数 dzNo 不能为空");
  549. }
  550. $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
  551. if($status==""){
  552. return error_show(1004,"参数status 不能为空");
  553. }
  554. if(!in_array($status,[0,1,2,3,4,5,6])){
  555. return error_show(1004,"参数status 无效值");
  556. }
  557. $payment= Db::name("pay_payment")->where([['dzNo',"=",$dzNo],['is_del',"=",0]])->find();
  558. if($payment==false){
  559. return error_show(1005,"未找到付款申请信息");
  560. }
  561. $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
  562. if($payinfo==false){
  563. return error_show(1005,"未找到对账信息");
  564. }
  565. if($payinfo['status']!=3){
  566. return error_show(1005,"对账信息未完成审核");
  567. }
  568. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
  569. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  570. $remark = isset($post['remark'])? trim($post['remark']) :"";
  571. $image = isset($post['return_image'])? trim($post['return_image']) :"";
  572. $return_time = isset($post['return_time'])? trim($post['return_time']) :"";
  573. if($status==4){
  574. if($image=="")return error_show(1005,"付款回单图片不能为空");
  575. if($return_time=="")return error_show(1005,"付款回单时间不能为空");
  576. if($payinfo['pay_fee']< $payment['pay_fee'])return error_show(1005,"对账付款申请金额有误,请确认对账申请金额");
  577. }
  578. Db::startTrans();
  579. try{
  580. $mentupdate =["status"=>$status,"remark"=>$remark,"return_img"=>$image,"updatetime"=>date("Y-m-d H:i:s")];
  581. if($status==4) $mentupdate['return_time'] = $return_time;
  582. $payup=Db::name("pay_payment")->where($payment)->update($mentupdate);
  583. if($payup){
  584. if (in_array($status,[4,5,6])){
  585. if($status==4){ //审核通过 扣减对账付款中的金额 添加到已付金额
  586. $payupdate =[
  587. "apay_fee"=>$payinfo['apay_fee']+$payment['pay_fee'],
  588. "pay_fee"=>$payinfo['pay_fee']-$payment['pay_fee'],
  589. "pay_status" => ($payinfo['pay_fee']-$payment['pay_fee'])==0 && $payinfo['wpay_fee']==0 ?3:2,
  590. "updatetime" => date("Y-m-d H:i:s")
  591. ];
  592. }
  593. if($status==5||$status==6){//审核驳回 扣减对账付款中的金额 返回到待付金额中
  594. $payupdate =[
  595. "wpay_fee"=>$payinfo['wpay_fee']+$payment['pay_fee'],
  596. "pay_fee"=>$payinfo['pay_fee']-$payment['pay_fee'],
  597. "pay_status" => $payinfo['apay_fee']==0 ?1:2,
  598. "updatetime" => date("Y-m-d H:i:s")
  599. ];
  600. foreach ($cgdNo as $value){
  601. $report=ReportCode::where(["cgdNo"=>$value])->find();
  602. if($report)$report->rmField("DzNo",$dzNo);
  603. }
  604. }
  605. $pay = Db::name("pay")->where($payinfo)->update($payupdate);
  606. if($pay==false){
  607. Db::rollback();
  608. return error_show(1003,"对账状态修改失败");
  609. }
  610. }
  611. Db::commit();
  612. return app_show(0,"状态修改成功");
  613. }else{
  614. Db::rollback();
  615. return error_show(1003,"状态修改失败");
  616. }
  617. }catch (\Exception $e){
  618. Db::rollback();
  619. return error_show(1008,$e->getMessage());
  620. }
  621. }
  622. /**对账申请删除
  623. * @return \think\response\Json|void
  624. * @throws \think\db\exception\DataNotFoundException
  625. * @throws \think\db\exception\DbException
  626. * @throws \think\db\exception\ModelNotFoundException
  627. */
  628. public function stageDel(){
  629. $post = $this->post;
  630. $dzNo = isset($post['dzNo'])&& $post['dzNo']!="" ? trim($post['dzNo']) :"";
  631. if($dzNo==""){
  632. return error_show(1004,"参数 dzNo 不能为空");
  633. }
  634. $payment= Db::name("pay_payment")->where([['dzNo',"=",$dzNo],['is_del',"=",0]])->find();
  635. if($payment==false){
  636. return error_show(1005,"未找到付款申请信息");
  637. }
  638. $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
  639. if($payinfo==false){
  640. return error_show(1005,"未找到付款信息");
  641. }
  642. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
  643. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  644. Db::startTrans();
  645. try{
  646. $paym = Db::name("pay_payment")->where($payment)->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  647. if($paym){
  648. if(in_array($payment['status'],[1,2,3,4])){
  649. if(in_array($payment['status'],[1,2,3])){
  650. $payinfo['wpay_fee']+=$payment['pay_fee'];
  651. $payinfo['pay_fee']-=$payment['pay_fee'];
  652. $payinfo['updatetime']=date("Y-m-d H:i:s");
  653. }
  654. if($payment['status']==4){
  655. $payinfo['wpay_fee']+=$payment['pay_fee'];
  656. $payinfo['apay_fee']-=$payment['pay_fee'];
  657. $payinfo['pay_status']=$payinfo['apay_fee']==0? 1 : 2 ;
  658. $payinfo['updatetime']=date("Y-m-d H:i:s");
  659. foreach ($cgdNo as $value){
  660. $report=ReportCode::where(["cgdNo"=>$value])->find();
  661. if($report)$report->rmField("DzNo",$dzNo);
  662. }
  663. }
  664. $pay = Db::name("pay")->save($payinfo);
  665. if($pay==false){
  666. Db::rollback();
  667. return error_show(1003,"对账状态修改失败");
  668. }
  669. }
  670. Db::commit();
  671. return app_show(0,"对账申请删除成功");
  672. }
  673. }catch (\Exception $e){
  674. Db::rollback();
  675. return error_show(1003,$e->getMessage());
  676. }
  677. }
  678. /**
  679. * 采购单列表
  680. * @param \think\Request $request
  681. * @param int $id
  682. * @return \think\Response
  683. */
  684. public function CgdList()
  685. {
  686. $post = $this->post;
  687. $condition = [];
  688. $check = checkRole($this->roleid,46);
  689. // if($check){
  690. // $condition []=["ownerid","=",$this->uid];
  691. // }
  692. $sequenceNo = isset($post['sequenceNo']) && $post['sequenceNo']!='' ? trim($post['sequenceNo']) :"";
  693. if($sequenceNo!=""){
  694. $condition []=["sequenceNo","like","%$sequenceNo%"];
  695. }
  696. $status = isset($post['status']) && $post['status']!=='' ? intval($post['status']) :"";
  697. if($status!==""){
  698. if($status!==''){
  699. if($status==3){
  700. $condition[]=["status","<>",2];
  701. }else{
  702. $condition[]=["status","=",$status];
  703. }
  704. }
  705. }
  706. $companyNo = isset($post['companyNo']) && $post['companyNo']!='' ? trim($post['companyNo']) :"";
  707. if($companyNo!=""){
  708. $condition []=["companyNo","like","%$companyNo%"];
  709. }
  710. $companyName = isset($post['companyName']) && $post['companyName']!='' ? trim($post['companyName']) :"";
  711. if($companyName!=""){
  712. $condition []=["companyName","like","%$companyName%"];
  713. }
  714. $bkCode = isset($post['bkCode']) && $post['bkCode']!='' ? trim($post['bkCode']) :"";
  715. if($bkCode!=""){
  716. $condition []=["bkCode","like","%$bkCode%"];
  717. }
  718. $goodNo = isset($post['goodNo']) && $post['goodNo']!='' ? trim($post['goodNo']) :"";
  719. if($goodNo!=""){
  720. $condition []=["goodNo","like","%$goodNo%"];
  721. }
  722. $goodName = isset($post['goodName']) && $post['goodName']!='' ? trim($post['goodName']) :"";
  723. if($goodName!=""){
  724. $condition []=["goodName","like","%$goodName%"];
  725. }
  726. $supplierName = isset($post['supplierName']) && $post['supplierName']!='' ? trim($post['supplierName']) :"";
  727. if($supplierName!=""){
  728. $condition []=["supplierName","like","%$supplierName%"];
  729. }
  730. $supplierNo = isset($post['supplierNo']) && $post['supplierNo']!='' ? trim($post['supplierNo']) :"";
  731. if($supplierNo!=""){
  732. $condition []=["supplierNo","like","%$supplierNo%"];
  733. }
  734. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :0;
  735. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  736. $count = Db::name("cgd_info")->where($condition)->count();
  737. $total = ceil($count/$size);
  738. $page = $page>=$total?intval($total):$page;
  739. $list = Db::name("cgd_info")->where($condition)->page($page,$size)->order('createdTime desc')->select();
  740. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  741. }
  742. //采购单信息
  743. public function cgdinfo(){
  744. $post=$this->post;
  745. $sequenceNo = isset($post['sequenceNo']) && $post['sequenceNo']!='' ? trim($post['sequenceNo']) :"";
  746. if($sequenceNo==''){
  747. return error_show(1004,"参数 sequenceNo 不能为空");
  748. }
  749. $cgdinfo = Db::name("cgd_info")->where(["sequenceNo"=>$sequenceNo])->findOrEmpty();
  750. if(empty($cgdinfo)){
  751. return error_show(1004,"采购单信息未找到");
  752. }
  753. $cgdinfo['catInfo'] = json_decode($cgdinfo['catInfo'],true);
  754. $payinfo =Db::name("pay_info")->alias("a")->leftJoin("pay b","a.payNo=b.payNo")
  755. ->where(["a.is_del"=>0,"b.is_del"=>0,"a.cgdNo"=>$sequenceNo])->field("b.inv_status,b.pay_status,b.status")
  756. ->find();
  757. $cgdinfo["cgd_pay_status"]=$payinfo['pay_status']??1;
  758. $cgdinfo["cgd_inv_status"]=$payinfo['inv_status']??1;
  759. $cgdinfo["cgd_status"]=$payinfo['status']??0;
  760. return app_show(0,"获取成功",$cgdinfo);
  761. }
  762. //todo
  763. public function PayInfo()
  764. {
  765. $post = $this->post;
  766. $payNo =isset($post['payNo'])&&$post['payNo']!='' ? trim($post['payNo']):"";
  767. if($payNo==""){
  768. return error_show(1004,"参数 payNo 不能为空");
  769. }
  770. $payinfo =Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  771. if($payinfo==false){
  772. return error_show(1004,"对账单数据未找到");
  773. }
  774. $pay =Db::name("pay_info")->alias("a")->leftJoin("cgd_info b","a.cgdNo=b.sequenceNo")
  775. ->where(["a.is_del"=>0,"b.is_del"=>0,"a.payNo"=>$payNo])->field("b.*")
  776. ->select()->toArray();
  777. foreach ($pay as &$value){
  778. $value['catInfo'] = json_decode($value['catInfo'],true);
  779. }
  780. $payinfo['cgdlist'] = $pay;
  781. return app_show(0,"获取成功",$payinfo);
  782. }
  783. public function DzInfo()
  784. {
  785. $post = $this->post;
  786. $DzNo =isset($post['DzNo'])&&$post['DzNo']!='' ? trim($post['DzNo']):"";
  787. if($DzNo==""){
  788. return error_show(1004,"参数 DzNo 不能为空");
  789. }
  790. $payinfo =Db::name("pay_payment")->where(["dzNo"=>$DzNo,"is_del"=>0])->find();
  791. if($payinfo==false){
  792. return error_show(1004,"对账单付款申请数据未找到");
  793. }
  794. $pay =Db::name("pay")->where(["payNo"=>$payinfo['payNo'],"is_del"=>0])->find();
  795. if($pay==false){
  796. return error_show(1004,"对账单数据未找到");
  797. }
  798. $payinfo['pay_apply_id'] = $pay['apply_id']??'';
  799. $payinfo['pay_apply_name'] = $pay['apply_name']??'';
  800. $payinfo['supplierNo'] = $pay['supplierNo']??'';
  801. $payinfo['supplierName'] = $pay['supplierName']??'';
  802. $payinfo['companyNo'] = $pay['companyNo']??'';
  803. $payinfo['companyName'] = $pay['companyName']??'';
  804. $payinfo['total_fee'] = $pay['total_fee']??'';
  805. return app_show(0,"获取成功",$payinfo);
  806. }
  807. /**发票新建添加
  808. * @return \think\response\Json|void
  809. * @throws \think\db\exception\DataNotFoundException
  810. * @throws \think\db\exception\DbException
  811. * @throws \think\db\exception\ModelNotFoundException
  812. * @throws \think\exception\DbException
  813. */
  814. public function invAdd(){
  815. $post = $this->post;
  816. $payNo = isset($post['payNo'])&&$post['payNo']!='' ? $post['payNo']:"";
  817. if($payNo==""){
  818. return error_show(1004,"参数 payNo 不能为空");
  819. }
  820. $payinfo =Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  821. if($payinfo==false){
  822. return error_show(1004,"对账单数据未找到");
  823. }
  824. if($payinfo['status']!=3){
  825. return error_show(1004,"对账单数据未完成审核");
  826. }
  827. $type = isset($post['invType']) && $post['invType']!="" ? intval($post['invType']):"";
  828. if($type==''){
  829. return error_show(1004,"参数 invType 不能为空");
  830. }
  831. $invNumber =isset($post['invNumber'])&& $post['invNumber']!='' ? trim($post['invNumber']):"";
  832. $invCode =isset($post['invCode'])&& $post['invCode']!='' ? trim($post['invCode']):"";
  833. $checkNumber =isset($post['checkNumber'])&& $post['checkNumber']!='' ? trim($post['checkNumber']):"";
  834. $openTime =isset($post['open_time'])&& $post['open_time']!='' ? trim($post['open_time']):"";
  835. $subtotal_amount =isset($post['subtotal_amount'])&& $post['subtotal_amount']!='' ? floor($post['subtotal_amount']):"0";
  836. if($type==1){
  837. if($invNumber=="") return error_show(1004,"参数 invNumber 不能为空");
  838. if($invCode=="") return error_show(1004,"参数 invCode 不能为空");
  839. // if($checkNumber=="") return error_show(1004,"参数 checkNumber 不能为空");
  840. if($openTime=="") return error_show(1004,"参数 open_time 不能为空");
  841. }
  842. $invimg = isset($post['inv_img'])&& $post['inv_img']!='' ? trim($post['inv_img']):"";
  843. $invName = isset($post['invName'])&& $post['invName']!='' ? trim($post['invName']):"";
  844. if($type==2){
  845. if($invimg=="")return error_show(1004,"参数 inv_img 不能为空");
  846. if($invName=="")return error_show(1004,"参数 invName 不能为空");
  847. }
  848. if ($type==3){
  849. }
  850. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  851. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  852. Db::startTrans();
  853. try{
  854. $hpNo=makeNo("hp");
  855. $data=[
  856. "payNo"=>$payNo,
  857. "hpNo"=>$hpNo,
  858. "apply_id"=>$this->uid,
  859. "apply_name"=>$this->uname,
  860. "inv_fee"=> $subtotal_amount,
  861. "invType"=> $type,
  862. "invoiceType"=>'',
  863. "inv_img"=>$invimg,
  864. "invName"=>$invName,
  865. "invoiceNumber"=>$invNumber,
  866. "invoiceCode"=>$invCode,
  867. "checkNumber"=>$checkNumber,
  868. "open_time"=>$openTime,
  869. "addtime"=>date("Y-m-d H:i:s"),
  870. "updatetime"=>date("Y-m-d H:i:s")
  871. ];
  872. $inadd =Db::name("pay_invoice")->insert($data);
  873. if ($inadd){
  874. foreach ($cgdNo as $value){
  875. $report=ReportCode::where(["cgdNo"=>$value])->find();
  876. if($report)$report->setField("hpNo",$hpNo);
  877. }
  878. Db::commit();
  879. return app_show(0,"回票新建成功",['hpNo'=>$hpNo]);
  880. }
  881. Db::rollback();
  882. return error_show(1004,"回票申请新建失败");
  883. }catch (\Exception $e){
  884. Db::rollback();
  885. return error_show(1004,$e->getMessage());
  886. }
  887. }
  888. /**
  889. * 发票审核状态
  890. * 0 上传发票,1待识别验证 2 待校验识别 3 待财务审核 6 待认证 7认证成功待确认完成 8回票完成 9识别失败 10 验证失败 11 财务驳回 12 认证失败 13校验超过次数 第二天校验
  891. * @return \think\response\Json|void
  892. * @throws \think\db\exception\DataNotFoundException
  893. * @throws \think\db\exception\DbException
  894. * @throws \think\db\exception\ModelNotFoundException
  895. * @throws \think\exception\DbException
  896. */
  897. public function invStatus(){
  898. $post = $this->post;
  899. $hpNo = isset($post['hpNo'])&& $post['hpNo']!="" ? trim($post['hpNo']) :"";
  900. if($hpNo==""){
  901. return error_show(1004,"参数 hpNo 不能为空");
  902. }
  903. $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
  904. if($status==""){
  905. return error_show(1004,"参数status 不能为空");
  906. }
  907. if(!in_array($status,[0,1,2,3,4,5,6,7,8,9,10,11,12])){
  908. return error_show(1004,"参数status 无效值");
  909. }
  910. $payment= Db::name("pay_invoice")->where([['hpNo',"=",$hpNo],['is_del',"=",0]])->find();
  911. if($payment==false){
  912. return error_show(1005,"未找到回票申请信息");
  913. }
  914. $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
  915. if($payinfo==false){
  916. return error_show(1005,"未找到对账信息");
  917. }
  918. if($payinfo['status']!=3){
  919. return error_show(1005,"对账信息未完成审核");
  920. }
  921. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
  922. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  923. if($payinfo['inv_fee']< $payment['inv_fee'])return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  924. $remark = $post['remark']??'';
  925. Db::startTrans();
  926. try {
  927. if($payment['invType']==1&&$status==1){
  928. $status=2;
  929. }
  930. $invup =["status"=>$status,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  931. $inv =Db::name("pay_invoice")->where($payment)->update($invup);
  932. if($inv){
  933. if($status==8){
  934. $payupdate =[
  935. "ainv_fee"=>$payinfo['ainv_fee']+$payment['inv_fee'],
  936. "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
  937. "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['winv_fee']==0?3:2,
  938. "updatetime" => date("Y-m-d H:i:s")
  939. ];
  940. $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  941. if($pay==false){
  942. Db::rollback();
  943. return error_show(1003,"回票申请状态更新成功");
  944. }
  945. }
  946. if($status==11){
  947. $payupdate =[
  948. "winv_fee"=>$payinfo['winv_fee']+$payment['inv_fee'],
  949. "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
  950. "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['ainv_fee']==0?1:2,
  951. "updatetime" => date("Y-m-d H:i:s")
  952. ];
  953. $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  954. if($pay==false){
  955. Db::rollback();
  956. return error_show(1003,"回票申请状态更新成功");
  957. }
  958. foreach ($cgdNo as $value){
  959. $report=ReportCode::where(["cgdNo"=>$value])->find();
  960. if($report)$report->rmField("hpNo",$hpNo);
  961. }
  962. }
  963. Db::commit();
  964. return app_show(0,"回票申请更新成功");
  965. }
  966. Db::rollback();
  967. return error_show(1003,"回票申请状态更新成功");
  968. }catch (\Exception $e){
  969. Db::rollback();
  970. return error_show(1004,$e->getMessage());
  971. }
  972. }
  973. /**发票列表识别
  974. * @param int $id
  975. * @return \think\Response
  976. */
  977. public function InvList()
  978. {
  979. $post = $this->post;
  980. $condition = [["a.is_del","=",0 ],["b.is_del","=",0 ]];
  981. $invtype = isset($post['invType'])&&$post['invType']!='' ? intval($post['invType']):"";
  982. if ($invtype!=''){
  983. $condition[]=["a.invType","=",$invtype];
  984. }
  985. $hpNo = isset($post['hpNo'])&&$post['hpNo']!='' ? trim($post['hpNo']):"";
  986. if ($hpNo!=''){
  987. $condition[]=["a.hpNo","like","%$hpNo%"];
  988. }
  989. $invoiceType = isset($post['invoiceType'])&&$post['invoiceType']!='' ? intval($post['invoiceType']):"";
  990. if ($invoiceType!=''){
  991. $condition[]=["a.invoiceType","=",$invoiceType];
  992. }
  993. $status = isset($post['status'])&&$post['status']!="" ? intval($post['status']):"";
  994. if($status!=""){
  995. $condition[]=["a.status","=",$status];
  996. }
  997. $invNumber = isset($post['invNumber'])&&$post['invNumber']!="" ? trim($post['invNumber']):"";
  998. if($invNumber!=''){
  999. $condition[]=["invoiceNumber","=",$invNumber];
  1000. }
  1001. $invCode = isset($post['invCode'])&&$post['invCode']!="" ? trim($post['invCode']):"";
  1002. if($invCode!=''){
  1003. $condition[]=["invoiceCode","=",$invCode];
  1004. }
  1005. $start = isset($post['start']) && $post['start']!="" ? $post['start'] :"";
  1006. if($start!=""){
  1007. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1008. }
  1009. $end = isset($post['end']) && $post['end']!="" ? $post['end'] :"";
  1010. if($end!=""){
  1011. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1012. }
  1013. $open_start = isset($post['open_start']) && $post['open_start']!="" ? $post['open_start'] :"";
  1014. if($open_start!=""){
  1015. $condition[]=["a.open_time",">=",$open_start." 00:00:00"];
  1016. }
  1017. $open_end = isset($post['open_end']) && $post['open_end']!="" ? $post['open_end'] :"";
  1018. if($open_end!=""){
  1019. $condition[]=["a.open_time","<=",$open_end." 23:59:59"];
  1020. }
  1021. $apply_id = isset($post['apply_id']) && $post['apply_id']!="" ? $post['apply_id'] :"";
  1022. if($apply_id!=""){
  1023. $condition[]=["a.apply_id","=",$apply_id];
  1024. }
  1025. $apply_name = isset($post['apply_name']) && $post['apply_name']!="" ? trim($post['apply_name']):"";
  1026. if($apply_name!=""){
  1027. $condition[]=["a.apply_name","like","%$apply_name%"];
  1028. }
  1029. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']):1;
  1030. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']):10;
  1031. $count =Db::name("pay_invoice")->alias("a")->leftJoin("pay b","a.payNo=b.payNo")->where($condition)->count();
  1032. $total = ceil($count/$size);
  1033. $page = $page>$total? intval($total):$page;
  1034. $list = Db::name("pay_invoice")->alias("a")->leftJoin("pay b","a.payNo=b.payNo")->field("a.*")->where
  1035. ($condition)->page($page,$size)->order("addtime desc")->select()->toArray();
  1036. foreach ($list as &$value){
  1037. $invoinfo =Db::name("invoice_info")->where(["hpNo"=>$value['hpNo'],"status"=>1])->find();
  1038. $value['buyer_name'] = $invoinfo['buyer_name']??"";
  1039. $value['buyer_id'] = $invoinfo['buyer_id']??"";
  1040. $value['buyer_address'] = $invoinfo['buyer_address']??"";
  1041. $value['buyer_bank'] = $invoinfo['buyer_bank']??"";
  1042. $value['seller_name'] = $invoinfo['seller_name']??"";
  1043. $value['seller_id'] = $invoinfo['seller_id']??"";
  1044. $value['seller_address'] = $invoinfo['seller_address']??"";
  1045. $value['seller_bank'] = $invoinfo['seller_bank']??"";
  1046. $value['subtotal_amount'] = $invoinfo['subtotal_amount']??"";
  1047. $value['subtotal_tax'] = $invoinfo['subtotal_tax']??"";
  1048. $value['total'] = $invoinfo['total']??"";
  1049. $value['receiver'] = $invoinfo['receiver']??"";
  1050. $value['issuer'] = $invoinfo['issuer']??"";
  1051. $value['reivewer'] = $invoinfo['reivewer']??"";
  1052. $value['remarks'] = $invoinfo['remarks']??"";
  1053. $value['item_list'] = isset($invoinfo['item_list']) &&$invoinfo['item_list']!='' ?json_decode($invoinfo['item_list'],true):"";
  1054. }
  1055. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1056. }
  1057. /**发票删除
  1058. * @return \think\response\Json|void
  1059. * @throws \think\db\exception\DataNotFoundException
  1060. * @throws \think\db\exception\DbException
  1061. * @throws \think\db\exception\ModelNotFoundException
  1062. */
  1063. public function invDel(){
  1064. $post = $this->post;
  1065. $hpNo = isset($post['hpNo'])&&$post['hpNo']!="" ? trim($post['hpNo']) :"";
  1066. if ($hpNo==""){
  1067. return error_show(1004,"参数 hpNo 不能为空");
  1068. }
  1069. $payinv =Db::name("pay_invoice")->where(["hpNo"=>$hpNo,"is_del"=>0])->find();
  1070. if($payinv==false){
  1071. return error_show(1004,"对账单回票申请未找到数据");
  1072. }
  1073. $payinfo= Db::name("pay")->where([['payNo',"=",$payinv['payNo']],['is_del',"=",0]])->find();
  1074. if($payinfo==false){
  1075. return error_show(1005,"未找到对账信息");
  1076. }
  1077. if($payinfo['status']!=3){
  1078. return error_show(1005,"对账信息未完成审核");
  1079. }
  1080. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payinv['payNo'],"is_del"=>0])->column("cgdNo");
  1081. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  1082. Db::startTrans();
  1083. try{
  1084. $update=["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
  1085. $invup =Db::name("pay_invoice")->where($payinv)->update($update);
  1086. if($invup){
  1087. if($payinv['status']==8){
  1088. if($payinfo['ainv_fee']< $payinv['inv_fee']){
  1089. Db::rollback();
  1090. return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1091. }
  1092. $payupdate =[
  1093. "ainv_fee"=>$payinfo['ainv_fee']-$payinv['inv_fee'],
  1094. "winv_fee"=>$payinfo['winv_fee']+$payinv['inv_fee'],
  1095. "inv_status" => ($payinfo['ainv_fee']-$payinv['inv_fee'])==0?1:2,
  1096. "updatetime" => date("Y-m-d H:i:s")
  1097. ];
  1098. $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1099. if($pay==false){
  1100. Db::rollback();
  1101. return error_show(1003,"删除失败");
  1102. }
  1103. foreach ($cgdNo as $value){
  1104. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1105. if($report)$report->rmField("hpNo",$hpNo);
  1106. }
  1107. }
  1108. Db::commit();
  1109. return app_show(0,"删除成功");
  1110. }
  1111. }catch (\Exception $e){
  1112. Db::rollback();
  1113. return error_show(1003,$e->getMessage());
  1114. }
  1115. }
  1116. public function HpInfo(){
  1117. $post = $this->post;
  1118. $hpNo = isset($post['hpNo'])&&$post['hpNo']!="" ? trim($post['hpNo']) :"";
  1119. if ($hpNo==""){
  1120. return error_show(1004,"参数 hpNo 不能为空");
  1121. }
  1122. $payinv =Db::name("pay_invoice")->where(["hpNo"=>$hpNo,"is_del"=>0])->find();
  1123. if($payinv==false){
  1124. return error_show(1004,"对账单回票申请未找到数据");
  1125. }
  1126. $payinfo= Db::name("pay")->where([['payNo',"=",$payinv['payNo']],['is_del',"=",0]])->find();
  1127. if($payinfo==false){
  1128. return error_show(1005,"未找到对账信息");
  1129. }
  1130. $payinv['pay_apply_id'] = $payinfo['apply_id']??'';
  1131. $payinv['pay_apply_name'] = $payinfo['apply_name']??'';
  1132. $payinv['supplierNo'] = $payinfo['supplierNo']??'';
  1133. $payinv['supplierName'] = $payinfo['supplierName']??'';
  1134. $payinv['companyNo'] = $payinfo['companyNo']??'';
  1135. $payinv['companyName'] = $payinfo['companyName']??'';
  1136. $payinv['total_fee'] = $payinfo['total_fee']??'';
  1137. $invinfo =Db::name("invoice_info")->where(["hpNo"=>$hpNo])->findOrEmpty();
  1138. if(!empty($invinfo)){
  1139. $invinfo['item_list'] =json_decode($invinfo['item_list'],true);
  1140. }
  1141. $payinv["info"]=$invinfo;
  1142. return app_show(0,'获取成功',$payinv);
  1143. }
  1144. public function UnPay(){
  1145. $post = $this->post;
  1146. $codeNo = isset($post['id'])&&$post['id']!=="" ? trim($post['id']) :'';
  1147. if($codeNo===""){
  1148. return error_show(1004,'参数 id 不能为空');
  1149. }
  1150. $update = Db::name('cgd_info')->where("id","in",$codeNo)->update(['status'=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  1151. if($update){
  1152. return app_show(0,'更新成功');
  1153. }else{
  1154. return error_show(1004,'更新失败');
  1155. }
  1156. }
  1157. }