Payment.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  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']) :date("Y-m-d H:i:s");
  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. $daList=[];
  925. foreach ($invArr as $item){
  926. if(!isset($item['invType'])||$item['invType']=='') return error_show(1004,"参数 invType 不能为空");
  927. if($item['invType']==1){
  928. if(!isset($item['invNumber'])||$item['invNumber']=='') return error_show(1004,"参数 invNumber 不能为空");
  929. if(!isset($item['invoiceType'])||$item['invoiceType']=='') return error_show(1004,"参数 invoiceType 不能为空");
  930. if(!isset($item['invCode'])||$item['invCode']=='') return error_show(1004,"参数 invCode 不能为空");
  931. if(!isset($item['open_time'])||$item['open_time']=='') return error_show(1004,"参数 open_time 不能为空");
  932. if(!isset($item['subtotal_amount'])||$item['subtotal_amount']=='') return error_show(1004,"参数 subtotal_amount 不能为空");
  933. }
  934. if($item['invType']==2){
  935. if(!isset($item['inv_img'])||$item['inv_img']=='') return error_show(1004,"参数 inv_img 不能为空");
  936. if(!isset($item['invName'])||$item['invName']=='') return error_show(1004,"参数 invName 不能为空");
  937. }
  938. if($item['invType']==3){
  939. if(!isset($item['inv_img'])||$item['inv_img']=='') return error_show(1004,"参数 inv_img 不能为空");
  940. if(!isset($item['invName'])||$item['invName']=='') return error_show(1004,"参数 invName 不能为空");
  941. }
  942. $hpNo=makeNo("hp");
  943. $data=[
  944. "payNo"=>$payNo,
  945. "hpNo"=>$hpNo,
  946. "apply_id"=>$this->uid,
  947. "apply_name"=>$this->uname,
  948. "inv_fee"=> 0,//核算金额
  949. "inv_subtotal_amount"=> $item['subtotal_amount']??0, //票面金额
  950. "invType"=> $item['invType'],
  951. "invoiceType"=>$item['invoiceType']??'',
  952. "inv_img"=>$item['inv_img']??'',
  953. "invName"=>$item['invName']??'',
  954. "invoiceNumber"=>$item['invNumber']??'',
  955. "invoiceCode"=>$item['invCode']??'',
  956. "status"=>1,
  957. "checkNumber"=>$item['checkNumber']??'',
  958. "open_time"=>isset($item['open_time'])&&$item['open_time']!=''? date("Y-m-d",strtotime($item['open_time'])):'',
  959. "addtime"=>date("Y-m-d H:i:s"),
  960. "updatetime"=>date("Y-m-d H:i:s")
  961. ];
  962. $daList[]=$data;
  963. }
  964. if(empty($daList)) return error_show(1004,"录入数据不能为空");
  965. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  966. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  967. Db::startTrans();
  968. try{
  969. $inadd =Db::name("pay_invoice")->insertAll($daList);
  970. if ($inadd){
  971. foreach ($cgdNo as $value){
  972. $report=ReportCode::where(["cgdNo"=>$value])->find();
  973. if($report)$report->setField("hpNo",$hpNo);
  974. }
  975. Db::commit();
  976. return app_show(0,"回票新建成功");
  977. }
  978. Db::rollback();
  979. return error_show(1004,"回票申请新建失败");
  980. }catch (\Exception $e){
  981. Db::rollback();
  982. return error_show(1004,$e->getMessage());
  983. }
  984. }
  985. /**
  986. * 发票审核状态
  987. * 1待系统验证 2 买方公司审核3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数
  988. * @return \think\response\Json|void
  989. * @throws \think\db\exception\DataNotFoundException
  990. * @throws \think\db\exception\DbException
  991. * @throws \think\db\exception\ModelNotFoundException
  992. * @throws \think\exception\DbException
  993. */
  994. public function invStatus(){
  995. $post = $this->post;
  996. $hpNo = isset($post['hpNo'])&& $post['hpNo']!="" ? trim($post['hpNo']) :"";
  997. if($hpNo==""){
  998. return error_show(1004,"参数 hpNo 不能为空");
  999. }
  1000. $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
  1001. if($status==""){
  1002. return error_show(1004,"参数status 不能为空");
  1003. }
  1004. // if(!in_array($status,[0,1,2,3,4,5,6,7,8,9,10,11,12])){
  1005. // return error_show(1004,"参数status 无效值");
  1006. // }
  1007. if(!in_array($status,[0,1,2,3,4,5,6,7])){
  1008. return error_show(1004,"参数status 无效值");
  1009. }
  1010. $payment= Db::name("pay_invoice")->where([['hpNo',"=",$hpNo],['is_del',"=",0]])->find();
  1011. if($payment==false){
  1012. return error_show(1005,"未找到回票申请信息");
  1013. }
  1014. $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
  1015. if($payinfo==false){
  1016. return error_show(1005,"未找到对账信息");
  1017. }
  1018. if($payinfo['status']!=2){
  1019. return error_show(1005,"对账信息未完成审核");
  1020. }
  1021. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
  1022. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  1023. if($payinfo['inv_fee']< $payment['inv_fee'])return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1024. $remark = $post['remark']??'';
  1025. Db::startTrans();
  1026. try {
  1027. if(in_array($payment['invoiceType'],['normal','electronic','toll','roll']) && $status==3){
  1028. $status=4; //普票无需认证
  1029. }
  1030. $invup =["status"=>$status,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  1031. $inv =Db::name("pay_invoice")->where($payment)->update($invup);
  1032. if($inv){
  1033. if($status==4){
  1034. $payupdate =[
  1035. "ainv_fee"=>$payinfo['ainv_fee']+$payment['inv_fee'],
  1036. "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
  1037. "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['winv_fee']==0?3:2,
  1038. "updatetime" => date("Y-m-d H:i:s")
  1039. ];
  1040. $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1041. if($pay==false){
  1042. Db::rollback();
  1043. return error_show(1003,"回票申请状态更新成功");
  1044. }
  1045. }
  1046. if($status==6 || $status==5|| $status==7){
  1047. $payupdate =[
  1048. "winv_fee"=>$payinfo['winv_fee']+$payment['inv_fee'],
  1049. "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
  1050. "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['ainv_fee']==0?1: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. foreach ($cgdNo as $value){
  1059. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1060. if($report)$report->rmField("hpNo",$hpNo);
  1061. }
  1062. }
  1063. Db::commit();
  1064. return app_show(0,"回票申请更新成功");
  1065. }
  1066. Db::rollback();
  1067. return error_show(1003,"回票申请状态更新成功");
  1068. }catch (\Exception $e){
  1069. Db::rollback();
  1070. return error_show(1004,$e->getMessage());
  1071. }
  1072. }
  1073. /**发票列表识别
  1074. * @param int $id
  1075. * @return \think\Response
  1076. */
  1077. public function InvList()
  1078. {
  1079. $post = $this->post;
  1080. $condition = [["a.is_del","=",0 ],["b.is_del","=",0 ]];
  1081. $invtype = isset($post['invType'])&&$post['invType']!='' ? intval($post['invType']):"";
  1082. if ($invtype!=''){
  1083. $condition[]=["a.invType","=",$invtype];
  1084. }
  1085. $companyNo = isset($post['companyNo'])&&$post['companyNo']!='' ? trim($post['companyNo']):"";
  1086. if($companyNo!=""){
  1087. $condition[]=["b.companyNo","=",$companyNo];
  1088. }
  1089. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1090. if($relaComNo!=""){
  1091. $condition[]=["b.companyNo|b.supplierNo","=",$relaComNo];
  1092. }
  1093. $supplierNo = isset($post['supplierNo'])&&$post['supplierNo']!='' ? trim($post['supplierNo']):"";
  1094. if($supplierNo!=""){
  1095. $condition[]=["b.supplierNo","=",$supplierNo];
  1096. }
  1097. $hpNo = isset($post['hpNo'])&&$post['hpNo']!='' ? trim($post['hpNo']):"";
  1098. if ($hpNo!=''){
  1099. $condition[]=["a.hpNo","like","%$hpNo%"];
  1100. }
  1101. $payNo = isset($post['payNo'])&&$post['payNo']!='' ? trim($post['payNo']):"";
  1102. if ($payNo!=''){
  1103. $condition[]=["a.payNo","like","%$payNo%"];
  1104. }
  1105. $invoiceType = isset($post['invoiceType'])&&$post['invoiceType']!='' ? intval($post['invoiceType']):"";
  1106. if ($invoiceType!=''){
  1107. $condition[]=["a.invoiceType","=",$invoiceType];
  1108. }
  1109. $status = isset($post['status'])&&$post['status']!=="" ? intval($post['status']):"";
  1110. if($status!==""){
  1111. $condition[]=["a.status","=",$status];
  1112. }
  1113. $invNumber = isset($post['invNumber'])&&$post['invNumber']!="" ? trim($post['invNumber']):"";
  1114. if($invNumber!=''){
  1115. $condition[]=["invoiceNumber","=",$invNumber];
  1116. }
  1117. $checkApi = isset($post['checkApi'])&&$post['checkApi']!="" ? intval($post['checkApi']):"";
  1118. if($checkApi!==''){
  1119. $condition[]=["checkApi","=",$checkApi];
  1120. }
  1121. $invCode = isset($post['invCode'])&&$post['invCode']!="" ? trim($post['invCode']):"";
  1122. if($invCode!=''){
  1123. $condition[]=["invoiceCode","=",$invCode];
  1124. }
  1125. $start = isset($post['start']) && $post['start']!="" ? $post['start'] :"";
  1126. if($start!=""){
  1127. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1128. }
  1129. $end = isset($post['end']) && $post['end']!="" ? $post['end'] :"";
  1130. if($end!=""){
  1131. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1132. }
  1133. $open_start = isset($post['open_start']) && $post['open_start']!="" ? $post['open_start'] :"";
  1134. if($open_start!=""){
  1135. $condition[]=["a.open_time",">=",$open_start." 00:00:00"];
  1136. }
  1137. $open_end = isset($post['open_end']) && $post['open_end']!="" ? $post['open_end'] :"";
  1138. if($open_end!=""){
  1139. $condition[]=["a.open_time","<=",$open_end." 23:59:59"];
  1140. }
  1141. $apply_id = isset($post['apply_id']) && $post['apply_id']!="" ? $post['apply_id'] :"";
  1142. if($apply_id!=""){
  1143. $condition[]=["a.apply_id","=",$apply_id];
  1144. }
  1145. $apply_name = isset($post['apply_name']) && $post['apply_name']!="" ? trim($post['apply_name']):"";
  1146. if($apply_name!=""){
  1147. $condition[]=["a.apply_name","like","%$apply_name%"];
  1148. }
  1149. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']):1;
  1150. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']):10;
  1151. $count =Db::name("pay_invoice")->alias("a")->leftJoin("pay b","a.payNo=b.payNo")->where($condition)->count();
  1152. $total = ceil($count/$size);
  1153. $page = $page>$total? intval($total):$page;
  1154. $list = Db::name("pay_invoice")->alias("a")->leftJoin("pay b","a.payNo=b.payNo")->field("a.*")->where
  1155. ($condition)->page($page,$size)->order("addtime desc")->select()->toArray();
  1156. foreach ($list as &$value){
  1157. $invoinfo =Db::name("invoice_info")->where(["hpNo"=>$value['hpNo'],"status"=>1])->find();
  1158. $value['buyer_name'] = $invoinfo['buyer_name']??"";
  1159. $value['buyer_id'] = $invoinfo['buyer_id']??"";
  1160. $value['buyer_address'] = $invoinfo['buyer_address']??"";
  1161. $value['buyer_bank'] = $invoinfo['buyer_bank']??"";
  1162. $value['seller_name'] = $invoinfo['seller_name']??"";
  1163. $value['seller_id'] = $invoinfo['seller_id']??"";
  1164. $value['seller_address'] = $invoinfo['seller_address']??"";
  1165. $value['seller_bank'] = $invoinfo['seller_bank']??"";
  1166. $value['subtotal_amount'] = $invoinfo['subtotal_amount']??"";
  1167. $value['subtotal_tax'] = $invoinfo['subtotal_tax']??"";
  1168. $value['total'] = $invoinfo['total']??"";
  1169. $value['receiver'] = $invoinfo['receiver']??"";
  1170. $value['issuer'] = $invoinfo['issuer']??"";
  1171. $value['reivewer'] = $invoinfo['reivewer']??"";
  1172. $value['remarks'] = $invoinfo['remarks']??"";
  1173. $value['invoiceType_cn'] = $this->invoiceType[$value['invoiceType']]??'';
  1174. $value['invStatus_cn'] = $this->invStatus[$value['invStatus']]??'';
  1175. $value['item_list'] = isset($invoinfo['item_list']) &&$invoinfo['item_list']!='' ?json_decode($invoinfo['item_list'],true):"";
  1176. }
  1177. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1178. }
  1179. /**发票删除
  1180. * @return \think\response\Json|void
  1181. * @throws \think\db\exception\DataNotFoundException
  1182. * @throws \think\db\exception\DbException
  1183. * @throws \think\db\exception\ModelNotFoundException
  1184. */
  1185. public function invDel(){
  1186. $post = $this->post;
  1187. $hpNo = isset($post['hpNo'])&&$post['hpNo']!="" ? trim($post['hpNo']) :"";
  1188. if ($hpNo==""){
  1189. return error_show(1004,"参数 hpNo 不能为空");
  1190. }
  1191. $payinv =Db::name("pay_invoice")->where(["hpNo"=>$hpNo,"is_del"=>0])->find();
  1192. if($payinv==false){
  1193. return error_show(1004,"对账单回票申请未找到数据");
  1194. }
  1195. $payinfo= Db::name("pay")->where([['payNo',"=",$payinv['payNo']],['is_del',"=",0]])->find();
  1196. if($payinfo==false){
  1197. return error_show(1005,"未找到对账信息");
  1198. }
  1199. if($payinfo['status']!=2){
  1200. return error_show(1005,"对账信息未完成审核");
  1201. }
  1202. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payinv['payNo'],"is_del"=>0])->column("cgdNo");
  1203. if(empty($cgdNo)) return error_show(1004,"未找到对账采购单信息");
  1204. Db::startTrans();
  1205. try{
  1206. $update=["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
  1207. $invup =Db::name("pay_invoice")->where($payinv)->update($update);
  1208. if($invup){
  1209. if($payinv['status']==4){
  1210. if($payinfo['ainv_fee']< $payinv['inv_fee']){
  1211. Db::rollback();
  1212. return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1213. }
  1214. $payupdate =[
  1215. "ainv_fee"=>$payinfo['ainv_fee']-$payinv['inv_fee'],
  1216. "winv_fee"=>$payinfo['winv_fee']+$payinv['inv_fee'],
  1217. "inv_status" => ($payinfo['ainv_fee']-$payinv['inv_fee'])==0?1:2,
  1218. "updatetime" => date("Y-m-d H:i:s")
  1219. ];
  1220. $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1221. if($pay==false){
  1222. Db::rollback();
  1223. return error_show(1003,"删除失败");
  1224. }
  1225. foreach ($cgdNo as $value){
  1226. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1227. if($report)$report->rmField("hpNo",$hpNo);
  1228. }
  1229. }
  1230. Db::commit();
  1231. return app_show(0,"删除成功");
  1232. }
  1233. }catch (\Exception $e){
  1234. Db::rollback();
  1235. return error_show(1003,$e->getMessage());
  1236. }
  1237. }
  1238. public function HpInfo(){
  1239. $post = $this->post;
  1240. $hpNo = isset($post['hpNo'])&&$post['hpNo']!="" ? trim($post['hpNo']) :"";
  1241. if ($hpNo==""){
  1242. return error_show(1004,"参数 hpNo 不能为空");
  1243. }
  1244. $payinv =Db::name("pay_invoice")->where(["hpNo"=>$hpNo,"is_del"=>0])->find();
  1245. if($payinv==false){
  1246. return error_show(1004,"对账单回票申请未找到数据");
  1247. }
  1248. $payinfo= Db::name("pay")->where([['payNo',"=",$payinv['payNo']],['is_del',"=",0]])->find();
  1249. if($payinfo==false){
  1250. return error_show(1005,"未找到对账信息");
  1251. }
  1252. $payinv['pay_apply_id'] = $payinfo['apply_id']??'';
  1253. $payinv['pay_apply_name'] = $payinfo['apply_name']??'';
  1254. $payinv['supplierNo'] = $payinfo['supplierNo']??'';
  1255. $payinv['supplierName'] = $payinfo['supplierName']??'';
  1256. $payinv['companyNo'] = $payinfo['companyNo']??'';
  1257. $payinv['companyName'] = $payinfo['companyName']??'';
  1258. $payinv['total_fee'] = $payinfo['total_fee']??'';
  1259. $invinfo =Db::name("invoice_info")->where(["hpNo"=>$hpNo])->findOrEmpty();
  1260. if(!empty($invinfo)){
  1261. $invinfo['item_list'] =json_decode($invinfo['item_list'],true);
  1262. }
  1263. $payinv['invoiceType_cn'] = $this->invoiceType[$payinv['invoiceType']]??'';
  1264. $payinv['invStatus_cn'] = $this->invStatus[$payinv['invStatus']]??'';
  1265. $payinv["info"]=$invinfo;
  1266. return app_show(0,'获取成功',$payinv);
  1267. }
  1268. /**
  1269. * @return \think\response\Json|void
  1270. * @throws \think\db\exception\DbException
  1271. */
  1272. public function UnPay(){
  1273. $post = $this->post;
  1274. $codeNo = isset($post['id'])&&$post['id']!=="" ? trim($post['id']) :'';
  1275. if($codeNo===""){
  1276. return error_show(1004,'参数 id 不能为空');
  1277. }
  1278. $update = Db::name('cgd_info')->where("id","in",$codeNo)->update(['status'=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  1279. if($update){
  1280. return app_show(0,'更新成功');
  1281. }else{
  1282. return error_show(1004,'更新失败');
  1283. }
  1284. }
  1285. /**
  1286. * @return \think\response\Json|void
  1287. */
  1288. public function ticketInfo(){
  1289. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  1290. if($num==''){
  1291. return error_show(1004,'参数 number 不能为空');
  1292. }
  1293. $condition=[];
  1294. $condition[]=["number","=",$num];
  1295. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1296. if($invNo!=='')$condition[]=["hpNo","=",$invNo];
  1297. $info =Db::name("invoice_info")->where($condition)->findOrEmpty();
  1298. if(!empty($info)){
  1299. $info['item_list']=json_decode($info['item_list'],true);
  1300. }
  1301. return empty($info)?error_show(1005,"为找到数据"):app_show(0,"获取成功",$info);
  1302. }
  1303. /**
  1304. * @return \think\response\Json|void
  1305. * @throws \think\db\exception\DataNotFoundException
  1306. * @throws \think\db\exception\DbException
  1307. * @throws \think\db\exception\ModelNotFoundException
  1308. */
  1309. public function hpinvList(){
  1310. $post=$this->request->only(["hpNo"=>"","page"=>1,"size"=>15],"post","trim");
  1311. $where=[];
  1312. if($post["hpNo"]!="") $where[]=["hpNo","=",$post['hpNo']];
  1313. $count=Db::name("invoice_info")->where($where)->count();
  1314. $total =ceil($count/$post['size']);
  1315. $page= $post['page']>$total ? intval($total):intval($post['page']);
  1316. $list = Db::name("invoice_info")
  1317. ->where($where)
  1318. ->json(["item_list"])
  1319. ->page($page,intval($post['size']))
  1320. ->order("id desc")
  1321. ->select()->toArray();
  1322. foreach ($list as &$value){
  1323. $value['type_name'] = $this->invoiceType[$value['type']]??"";
  1324. }
  1325. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1326. }
  1327. }