Payment.php 61 KB

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