Payment.php 54 KB

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