Payment.php 73 KB

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