Payment.php 60 KB

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