Payment.php 60 KB

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