Payment.php 54 KB

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