Payment.php 60 KB

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