Payment.php 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\Admin\controller;
  4. use app\BaseController;
  5. use think\facade\Db;
  6. use think\Request;
  7. class Payment extends BaseController
  8. {
  9. /**
  10. * 显示资源列表
  11. *
  12. * @return \think\Response
  13. */
  14. public function PayList()
  15. {
  16. $post =$this->request->post();
  17. $token = isset($post['token']) ? trim($post['token']) : "";
  18. if($token==""){
  19. return error_show(101,'token不能为空');
  20. }
  21. $effetc = VerifyTokens($token);
  22. if(!empty($effetc) && $effetc['code']!=0){
  23. return error_show($effetc['code'],$effetc['message']);
  24. }
  25. $userinfo = GetUserInfo($token);
  26. if(!isset($userinfo['code'])|| $userinfo['code']!=0){
  27. return error_show(101,'未能获取用户信息');
  28. }
  29. $condition = "a.status!=0";
  30. $check = checkRole($userinfo['data']['roleid'],49);
  31. if($check){
  32. $condition .=" and `a`.`creater` = {$userinfo['data']['id']}";
  33. }
  34. $name= isset($post['name'])&&$post['name']!="" ? trim($post['name']) :"";
  35. $contector= isset($post['contector'])&&$post['contector']!="" ? trim($post['contector']) :"";
  36. $startTime= isset($post['startTime'])&&$post['startTime']!="" ? trim($post['startTime']) :"";
  37. $endTime= isset($post['endTime'])&&$post['endTime']!="" ? trim($post['endTime']) :"";
  38. $supplierNo= isset($post['supplierNo'])&&$post['supplierNo']!="" ? trim($post['supplierNo']) :"";
  39. $payNo= isset($post['payNo'])&&$post['payNo']!="" ? trim($post['payNo']) :"";
  40. $apply= isset($post['apply'])&&$post['apply']!="" ? trim($post['apply']) :"";
  41. $paystatus= isset($post['pay_status'])&&$post['pay_status']!="" ? $post['pay_status'] :"";
  42. $inv_status= isset($post['inv_status'])&&$post['inv_status']!="" ? $post['inv_status'] :"";
  43. if($name!=""){
  44. $condition .=" and `c`.`name`like '%{$name}%'";
  45. }
  46. if($contector!=""){
  47. $condition .=" and `c`.`contector` like '%{$contector}%'";
  48. }
  49. if($startTime!=""){
  50. $condition.=" and `d`.`addtime` >= '{$startTime}'";
  51. }
  52. if($endTime!=""){
  53. $condition.=" and `d`.`addtime` <= '{$endTime}'";
  54. }
  55. if($supplierNo!=""){
  56. $condition.=" and `a`.`supplierNo` like '%{$supplierNo}%'";
  57. }
  58. if($payNo!=""){
  59. $condition.=" and `a`.`payNo` like '%{$payNo}%'";
  60. }
  61. if($apply!=""){
  62. $condition .=" and a.apply_name like '%{$apply}%'";
  63. }
  64. if($paystatus!=""){
  65. $condition .=" and `d`.`pay_status` = {$paystatus}";
  66. }
  67. if($inv_status!=""){
  68. $condition .=" and `d`.`status` = {$inv_status}";
  69. }
  70. // echo $condition;
  71. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :1;
  72. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  73. $count = Db::name("pay")->alias('a')
  74. ->join('cfp_supplier_info c',' `a`.`supplierNo` = `c`.`code` ','left')
  75. ->join("cfp_pay_stages d",'`a`.`payNo` = `d`.`payNo` AND ( `d`.`is_del` = 0 ) ','left')
  76. ->join("cfp_order_rate p",'a.`inv_rate` = `p`.`id` ','left')
  77. ->where($condition)->count();
  78. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  79. $page = $page>=$total?intval($total):$page;
  80. $list = Db::name("pay")->alias('a')
  81. ->join('cfp_supplier_info c',' `a`.`supplierNo` = `c`.`code` ','left')
  82. ->join("cfp_pay_stages d",'`a`.`payNo` = `d`.`payNo` AND ( `d`.`is_del` = 0 ) ','left')
  83. ->join("cfp_order_rate p",'a.`inv_rate` = `p`.`id` ','left')
  84. ->where($condition)->page(intval($page),$size)->order("addtime desc")
  85. ->field(" `a`.`id` AS `id`,
  86. `a`.`payNo` AS `payNo`,
  87. `a`.`supplierNo` AS `supplierNo`,
  88. `a`.`pay_fee` AS `pay_fee`,
  89. `a`.`total_fee` AS `total_fee`,
  90. `a`.`run_fee` AS `run_fee`,
  91. `a`.`invoiceNo` AS `invoiceNo`,
  92. `a`.`pay_method` AS `pay_method`,
  93. `a`.`pay_rate` AS `pay_rate`,
  94. `c`.`name` AS `name`,
  95. `c`.`contector` AS `contector`,
  96. `a`.`wait_fee` AS `wait_fee`,
  97. `d`.`id` AS `sid`,
  98. `d`.`pay_fee` AS `dpay_fee`,
  99. `d`.`pay_rate` AS `dpay_rate`,
  100. `d`.`pay_stages` AS `pay_stages`,
  101. `d`.`inv_fee` AS `inv_fee`,
  102. `d`.`return_img` AS `return_img`,
  103. `d`.`status` AS `dstatus`,
  104. `a`.`status` AS `status`,
  105. `a`.`remark` AS `remark`,
  106. `d`.`addtime` AS `addtime`,
  107. `d`.`companyNo` AS `companyNo`,
  108. `d`.`remark` AS `aremark`,
  109. `a`.`creater` AS `creater`,
  110. `d`.`dzNo` AS `dzNo`,
  111. `a`.`inv_rate` AS `inv_rate`,
  112. ifnull( `p`.`rate`, '' ) AS `rate`,
  113. `d`.`pay_status` AS `pay_status`,
  114. `d`.`pay_remark` AS `pay_remark`,
  115. `a`.`ainv_fee` AS `ainv_fee`,
  116. `a`.`winv_fee` AS `winv_fee` ")->select();
  117. //echo Db::name("pay_ment")->getLastSql();
  118. $data=[];
  119. foreach ($list as $key=>$value){
  120. $value['ordeNum'] = Db::name("pay_info")->where("payNo","=",$value["payNo"])->count();
  121. $user = GetInfoById($token,["id"=>$value['creater']]);
  122. $username = "";
  123. if(isset($user['code'])&&$user['code']==0){
  124. $username = isset($user['data']['nickname']) ? $user['data']['nickname']: "";
  125. }
  126. $value['createrName'] = $username;
  127. $data[]=$value;
  128. }
  129. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  130. }
  131. /**
  132. * 显示创建资源表单页.
  133. *
  134. * @return \think\Response
  135. */
  136. public function PayAdd()
  137. {
  138. $post =$this->request->post();
  139. $token = isset($post['token']) ? trim($post['token']) : "";
  140. if($token==""){
  141. return error_show(101,'token不能为空');
  142. }
  143. $effetc = VerifyTokens($token);
  144. if(!empty($effetc) && $effetc['code']!=0){
  145. return error_show($effetc['code'],$effetc['message']);
  146. }
  147. $guserinfo =GetUserInfo($token);
  148. if(isset($guserinfo['code']) && $guserinfo['code']!=0){
  149. return error_show($guserinfo['code'],$guserinfo['message']);
  150. }
  151. $cids = isset($post['cids'])&&$post['cids']!="" ? trim($post['cids']) :"";
  152. if($cids==""){
  153. return error_show(1003,"参数cids不能为空");
  154. }
  155. $supplierNo = isset($post['supplierNo'])&&$post['supplierNo']!="" ? trim($post['supplierNo']) :"";
  156. if($supplierNo==""){
  157. return error_show(1003,"参数supplierNo不能为空");
  158. }
  159. $pay_method = isset($post['pay_method'])&&$post['pay_method']!="" ? $post['pay_method']:"";
  160. if($pay_method==""){
  161. return error_show(1004,"账期类型不能为空");
  162. }
  163. $pay_rate = isset($post['pay_rate'])&&is_array($post['pay_rate'])? $post['pay_rate']:[100];
  164. $inv_rate = isset($post['inv_rate'])&&$post['inv_rate']!==""? $post['inv_rate']:"";
  165. if($inv_rate==""){
  166. return error_show(1004,"发票税率不能为空");
  167. }
  168. $rate = Db::name("order_rate")->where("id","=",$inv_rate)->find();
  169. if(empty($rate)){
  170. return error_show(1004,"未找到对应发票税率");
  171. }
  172. $cgd = Db::name("cgd_info")->where([["id","in",$cids],["ShortText1617861287265","=",$supplierNo],
  173. ["status","=",0]]
  174. )->field("sequenceNo,`ShortText1619463208482` as inv_status,`ShortText1619463188366` as pay_status,Number1618330470625 as apay,Number1618330472961 as wpay,Number1618330541286 ainv,Number1618330543270 as winv,id")->select();
  175. if(empty($cgd->toArray())){
  176. return error_show(1004,"未找到有关的数据");
  177. }
  178. $payNo = makeNo("PAY");
  179. Db::startTrans();
  180. try {
  181. $data=[];
  182. $paydata=[
  183. "payNo"=>$payNo,
  184. "creater"=>$effetc['data']['user']['id'],
  185. "apply_name"=>$guserinfo['data']['nickname'],
  186. "total_fee"=>0,
  187. "pay_fee"=>0,
  188. "supplierNo"=>$supplierNo,
  189. "invoiceNo"=>"",
  190. "run_fee"=>0,
  191. "pay_method"=>$pay_method,
  192. "pay_rate"=>json_encode($pay_rate),
  193. "wait_fee"=>0,
  194. "ainv_fee"=>0,
  195. "winv_fee"=>0,
  196. "inv_rate"=>$inv_rate,
  197. "remark"=>"",
  198. "status"=>1,
  199. "addtime"=>date("Y-m-d H:i:s"),
  200. "updatetime"=>date("Y-m-d H:i:s"),
  201. ];
  202. $invcom = "";
  203. foreach ($cgd as $key=>$value){
  204. $temp=[];
  205. $coma =Db::name("cgd_inv")->where([['status',"=",2],['cgdNo',"=",$value['sequenceNo']]])->find();
  206. if(empty($coma)){
  207. Db::rollback();
  208. return error_show(1003,"采购单{$value['sequenceNo']}未确认发票开票信息");
  209. }
  210. if($invcom==""){
  211. $invcom = $coma['companyNo'];
  212. }else{
  213. if($invcom!=$coma['companyNo']){
  214. Db::rollback();
  215. return error_show(1003,"采购单{$value['sequenceNo']}发票开票信息不一致");
  216. }
  217. }
  218. $temp['sequenceNo']=$value['sequenceNo'];
  219. $temp['total_fee']=($value['apay']*100+$value['wpay']*100)/100;
  220. $temp['pay_fee']=round($value['apay'],2);
  221. $temp['wait_fee']=round($value['wpay'],2);
  222. $temp['winv_fee']=round($value['winv'],2);
  223. $temp['ainv_fee']=round($value['ainv'],2);
  224. $temp['payNo']=$payNo;
  225. $temp['addtime']=date("Y-m-d H:i:s");
  226. $temp['updatetime']=date("Y-m-d H:i:s");
  227. $data[]=$temp;
  228. $paydata['total_fee']+= $temp['total_fee'];
  229. $paydata['wait_fee']+= $temp['wait_fee'];
  230. $paydata['pay_fee']+= $temp['pay_fee'];
  231. $paydata['ainv_fee']+= $temp['ainv_fee'];
  232. $paydata['winv_fee']+= $temp['winv_fee'];
  233. }
  234. if($invcom==""){
  235. Db::rollback();
  236. return error_show(1003,"采购单未确认发票开票信息");
  237. }
  238. $paydata['total_fee']= round($paydata['total_fee'],2);
  239. $paydata['wait_fee']= round($paydata['wait_fee'],2);
  240. $paydata['pay_fee']= round($paydata['pay_fee'],2);
  241. $paydata['ainv_fee']= round($paydata['ainv_fee'],2);
  242. $paydata['winv_fee']= round($paydata['winv_fee'],2);
  243. if(count($pay_rate)>=1){
  244. $paydata['run_fee'] = round($pay_rate[0],2);
  245. $paydata['wait_fee'] = round($paydata['total_fee']-$paydata['pay_fee'],2);
  246. //$paydata['wait_fee'] = round($paydata['total_fee']-$paydata['pay_fee']- $paydata['run_fee'],2);:ToDo
  247. }
  248. $info = Db::name("pay_info")->insertAll($data);
  249. if($info!=0){
  250. $payDA=Db::name("pay")->insert($paydata);
  251. if($payDA){
  252. $stage=[];
  253. $pay=0;
  254. $tota= round(array_sum($pay_rate),2);
  255. if( $paydata['wait_fee']!= $tota){
  256. return error_show(1005,'剩余待付金额未对账');
  257. }
  258. foreach ($pay_rate as $key=>$value){
  259. $temp=[];
  260. $temp['payNo']=$payNo;
  261. $temp['dzNo']=makeNo("DZ");
  262. if($paydata['wait_fee']>=$value){
  263. $paydata['wait_fee']=round($paydata['wait_fee']-$value,2);
  264. $temp['pay_fee']=round($value,2);
  265. }else{
  266. return error_show(1005,'待付金额不足于分期金额');
  267. }
  268. $temp['pay_rate']=$value;
  269. $temp['pay_stages']=$key+1;
  270. $temp['status']=$paydata['winv_fee']==0?7: 0;
  271. $temp['pay_status']=$temp['pay_fee']==0?4: 0;
  272. $temp['return_img']="";
  273. $temp['companyNo'] = $invcom;
  274. $temp['addtime']=date("Y-m-d H:i:s");
  275. $temp['updatetime']=date("Y-m-d H:i:s");
  276. $pay+=$temp['pay_fee'];
  277. $stage[] =$temp;
  278. }
  279. $pay_stage = Db::name("pay_stages")->insertAll($stage);
  280. if($pay_stage){
  281. Db::name("cgd_info")->where("id","in",$cids)->save(['status'=>1,"check_rate"=>""]);
  282. Db::commit();
  283. return app_show(0,"添加成功");
  284. }
  285. }
  286. }
  287. Db::rollback();
  288. return error_show(1003,"添加失败");
  289. }catch(\Exception $e){
  290. Db::rollback();
  291. return error_show(1003,$e->getMessage());
  292. }
  293. }
  294. /**
  295. * 保存新建的资源
  296. *
  297. * @param \think\Request $request
  298. * @return \think\Response
  299. */
  300. public function PayStatus()
  301. {
  302. $post =$this->request->post();
  303. $token = isset($post['token']) ? trim($post['token']) : "";
  304. if($token==""){
  305. return error_show(101,'token不能为空');
  306. }
  307. $effetc = VerifyTokens($token);
  308. if(!empty($effetc) && $effetc['code']!=0){
  309. return error_show($effetc['code'],$effetc['message']);
  310. }
  311. $payNo = isset($post['payNo']) && $post['payNo']!="" ? trim($post['payNo']):"";
  312. if($payNo==""){
  313. return error_show(1004,"参数payNo不能为空");
  314. }
  315. $status= isset($post['status']) && $post['status']!=="" ? trim($post['status']):"";
  316. if($status===""){
  317. return error_show(1004,"参数status不能为空");
  318. }
  319. $data = Db::name("pay")->where("payNo","=",$payNo)->find();
  320. if($data==false){
  321. return error_show(1004,"未能找到对应数据");
  322. }
  323. Db::startTrans();
  324. try{
  325. $result = Db::name("pay")->where("payNo","=",$payNo)->update(['status'=>$status]);
  326. if($result){
  327. $cgd = Db::name("pay_info")->where([["payNo","=",$payNo],["status","=",1]])->column("sequenceNo");
  328. if(!empty($cgd)){
  329. Db::name("pay_info")->where([["payNo","=",$payNo],["status","=",1]])->save(['status'=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  330. Db::name("pay_stages")->where([["payNo","=",$payNo],["is_del","=",0]])->save(['is_del'=>1, "updatetime"=>date("Y-m-d H:i:s")]);
  331. $update = Db::name("cgd_info")->where([["sequenceNo","in",$cgd],['status',"=",1]])->save(['status'=>0]);
  332. if($update){
  333. Db::commit();
  334. return app_show(0,"状态更新成功");
  335. }
  336. }
  337. }
  338. Db::rollback();
  339. return error_show(1004,"状态更新失败");
  340. }catch (\Exception $e){
  341. Db::rollback();
  342. return error_show(1004,$e->getMessage());
  343. }
  344. // return $result ? app_show(0,"状态更新成功") : error_show(1004,"状态更新失败");
  345. }
  346. /**
  347. * status 1申请付款 2 业务驳回 3发票已上传 4发票确认 5 财务审核发票 6 付款回执 7完成 0 取消申请
  348. *
  349. * @param int $id
  350. * @return \think\Response
  351. */
  352. public function BusAduit()
  353. {
  354. $post =$this->request->post();
  355. $token = isset($post['token']) ? trim($post['token']) : "";
  356. if($token==""){
  357. return error_show(101,'token不能为空');
  358. }
  359. $effetc = VerifyTokens($token);
  360. if(!empty($effetc) && $effetc['code']!=0){
  361. return error_show($effetc['code'],$effetc['message']);
  362. }
  363. $payNo = isset($post['payNo'])&& $post['payNo']!="" ? trim($post['payNo']) :"";
  364. if($payNo==""){
  365. return error_show(1004,"参数payNo 不能为空");
  366. }
  367. $stageid = isset($post['sid'])&& $post['sid']!="" ? intval($post['sid']) :"";
  368. if($stageid==""){
  369. return error_show(1004,"参数sid 不能为空");
  370. }
  371. $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
  372. if($status==""){
  373. return error_show(1004,"参数status 不能为空");
  374. }
  375. if(!in_array($status,[1,2,3,4,5,6,0])){
  376. return error_show(1004,"参数status 错误");
  377. }
  378. $pay = Db::name("pay")->where(['payNo'=>$payNo])->find();
  379. if(empty($pay)){
  380. return error_show(1004,"未找到付款码{$payNo}的数据");
  381. }
  382. if(!in_array($pay['status'],[0,1])){
  383. return error_show(1004,"数据状态异常");
  384. }
  385. $payStage = Db::name("pay_stages")->where([['id',"=",$stageid],['payNo',"=",$payNo],["is_del","=",0]])->find();
  386. if(empty($payStage)){
  387. return error_show(1005,"未找到付款申请信息");
  388. }
  389. if($payStage["pay_stages"]>1){
  390. $prev = $payStage["pay_stages"]-1;
  391. $prevStage = Db::name("pay_stages")->where([['pay_stages',"=",$prev],['payNo',"=",$payNo],["is_del","=",0]])->find();
  392. if(empty($prevStage)){
  393. return error_show(1004,"付款码{$payNo}上一期数据丢失");
  394. }
  395. if($prevStage['status']!=7){
  396. return error_show(1004,"付款码{$payNo}上一期数据流程正在进行中");
  397. }
  398. }
  399. if($pay['winv_fee']==0){
  400. $status=7;
  401. }
  402. $data=[
  403. "status"=>$status,
  404. "updatetime"=>date("Y-m-d H:i:s")
  405. ];
  406. // $companyNo=isset($post['companyNo'])&&$post['companyNo']!="" ? $post['companyNo'] :"";
  407. // if($status==3&& $payStage['status']==2 ){
  408. // if($companyNo==""){
  409. // return error_show(1005,"参数companyNo 不能为空");
  410. // }else{
  411. // $data['companyNo'] = $companyNo;
  412. // }
  413. // }
  414. $remark = isset($post['remark'])&&$post['remark']!="" ? $post['remark'] :"";
  415. if(($status==4 && $payStage['status']==5) || $status==2){
  416. if($remark==""){
  417. return error_show(1005,"参数remark 不能为空");
  418. }else{
  419. $data['remark'] = $remark;
  420. }
  421. }
  422. // $img = isset($post['image'])&&$post['image']!="" ? $post['image'] :"";
  423. // if($status==8 && $payStage['status']==7){
  424. // if($img==""){
  425. // return error_show(1005,"参数img 不能为空");
  426. // }else{
  427. // $data['return_img'] =$img;
  428. // }
  429. // }
  430. Db::startTrans();
  431. try {
  432. $upd=Db::name("pay_stages")->where([['id',"=",$stageid],['payNo',"=",$payNo],["is_del","=",0]])->save($data);
  433. if($upd){
  434. if($status==7){
  435. // if($pay['wait_fee']<$payStage['pay_fee']){
  436. // Db::rollback();
  437. // return error_show(1005,"付款金额与待付款金额不符");
  438. // }
  439. // $pay['wait_fee'] = $pay['wait_fee'] - $payStage['pay_fee'];
  440. // $pay['run_fee'] = $pay['run_fee'] - $payStage['pay_fee'];//::ToDo
  441. // $pay['pay_fee'] = $pay['pay_fee']+ $payStage['pay_fee'];
  442. $num = Db::name("pay_stages")->whereOr([['status',"<>",7],['pay_status',"<>",4]])->where
  443. ([['payNo',"=", $pay['payNo']],["is_del","=",0]])->count();
  444. if($num==0){
  445. $pay['status']=2;
  446. }
  447. $pay['updatetime']=date("Y-m-d H:i:s");
  448. $pay_up = Db::name("pay")->save($pay);
  449. if($pay_up){
  450. Db::commit();
  451. return app_show(0,"状态修改成功");
  452. }else{
  453. Db::rollback();
  454. return error_show(1004,"状态修改失败");
  455. }
  456. }
  457. Db::commit();
  458. return app_show(0,"状态修改成功");
  459. }else{
  460. Db::rollback();
  461. return error_show(1004,"状态修改失败");
  462. }
  463. }catch (\Exception $e){
  464. Db::rollback();
  465. return error_show(1005,$e->getMessage());
  466. }
  467. }
  468. /**
  469. * 显示编辑资源表单页.
  470. * 付款审核状态 0 待付款 1 付款待审核 2 审核通过 3 审核未通过
  471. * @param int $id
  472. * @return \think\Response
  473. */
  474. public function PayExam()
  475. {
  476. $post =$this->request->post();
  477. $token = isset($post['token']) ? trim($post['token']) : "";
  478. if($token==""){
  479. return error_show(101,'token不能为空');
  480. }
  481. $effetc = VerifyTokens($token);
  482. if(!empty($effetc) && $effetc['code']!=0){
  483. return error_show($effetc['code'],$effetc['message']);
  484. }
  485. // $payNo = isset($post['payNo'])&& $post['payNo']!="" ? trim($post['payNo']) :"";
  486. // if($payNo==""){
  487. // return error_show(1004,"参数payNo 不能为空");
  488. // }
  489. $stageid = isset($post['sid'])&& $post['sid']!="" ? intval($post['sid']) :"";
  490. if($stageid==""){
  491. return error_show(1004,"参数sid 不能为空");
  492. }
  493. $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
  494. if($status==""){
  495. return error_show(1004,"参数status 不能为空");
  496. }
  497. if(!in_array($status,[0,1,2,3,4,5])){
  498. return error_show(1004,"参数status 无效值");
  499. }
  500. $payment= Db::name("pay_ment")->where([['sid',"=",$stageid],['status',"<>",0]])->find();
  501. if(empty($payment)){
  502. return error_show(1005,"未找到付款申请信息");
  503. }
  504. if(($status==1 && (!in_array($payment['pay_status'],[0,5,6]))) || ($status==2 && $payment['pay_status']!=1) ||
  505. ($status==3 && $payment['pay_status']!=2) ||($status==4 && $payment['pay_status']!=3)){
  506. return error_show(1005,"数据状态有误");
  507. }
  508. $remark = isset($post['remark'])? trim($post['remark']) :"";
  509. $image = isset($post['image'])? trim($post['image']) :"";
  510. if($status==4 && $image==""){
  511. return error_show(1005,"付款回单图片不能为空");
  512. }
  513. $paytime = isset($post['paytime'])&&$post['paytime']!=""?$post['paytime'] :date("Y-m-d H:i:s");
  514. Db::startTrans();
  515. try{
  516. $status==1&&$payment['dpay_fee']==0 ? $status =4 : "";
  517. $data = [
  518. "pay_status"=>$status,
  519. "pay_remark"=>$remark,
  520. "return_img"=>$image!=""?$image :$payment['return_img']
  521. ];
  522. if($status==4){
  523. $data['paytime']=$paytime;
  524. }
  525. $upd=Db::name("pay_stages")->where([['id',"=",$stageid],["is_del","=",0]])->save($data);
  526. if($upd){
  527. if($status==4){
  528. $num = Db::name("pay_stages")->whereOr([['status',"<>",7],['pay_status',"<>",4]])->where
  529. ([['payNo',"=", $payment['payNo']],["is_del","=",0]])->count();
  530. if($num==0){
  531. $pay_up = Db::name("pay")->where('payNo',"=",$payment['payNo'])->save(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  532. if($pay_up){
  533. Db::commit();
  534. return app_show(0,"状态修改成功");
  535. }else{
  536. Db::rollback();
  537. return error_show(1004,"状态修改失败");
  538. }
  539. }
  540. }
  541. if($status==3){
  542. if($payment['wait_fee']<$payment['dpay_fee']){
  543. Db::rollback();
  544. return error_show(1005,"付款金额与待付款金额不符");
  545. }
  546. $pay=[
  547. "wait_fee"=>$payment['wait_fee']-$payment['dpay_fee'],
  548. "pay_fee"=>$payment['pay_fee']+$payment['dpay_fee'],
  549. "updatetime"=>date("Y-m-d H:i:s")
  550. // "run_fee"=>$payment['run_fee']-$payment['dpay_fee'],
  551. ];
  552. $pay_up = Db::name("pay")->where('payNo',"=",$payment['payNo'])->save($pay);
  553. if($pay_up){
  554. Db::commit();
  555. return app_show(0,"状态修改成功");
  556. }else{
  557. Db::rollback();
  558. return error_show(1004,"状态修改失败");
  559. }
  560. }
  561. Db::commit();
  562. return app_show(0,"状态修改成功");
  563. }else{
  564. Db::rollback();
  565. return error_show(1003,"状态修改失败");
  566. }
  567. }catch (\Exception $e){
  568. Db::rollback();
  569. return error_show(1008,$e->getMessage());
  570. }
  571. }
  572. /**
  573. * 保存更新的资源
  574. *
  575. * @param \think\Request $request
  576. * @param int $id
  577. * @return \think\Response
  578. */
  579. public function CgdList()
  580. {
  581. $post =$this->request->post();
  582. $token = isset($post['token']) ? trim($post['token']) : "";
  583. if($token==""){
  584. return error_show(101,'token不能为空');
  585. }
  586. $effetc = VerifyTokens($token);
  587. if(!empty($effetc) && $effetc['code']!=0){
  588. return error_show($effetc['code'],$effetc['message']);
  589. }
  590. $userinfo = GetUserInfo($token);
  591. if(!isset($userinfo['code'])|| $userinfo['code']!=0){
  592. return error_show(101,'未能获取用户信息');
  593. }
  594. $condition = [["a.status","=",0 ]];
  595. $check = checkRole($userinfo['data']['roleid'],46);
  596. if($check){
  597. $condition []=["a.ownerName","=",$userinfo['data']['nickname']];
  598. }
  599. $name= isset($post['name'])&&$post['name']!="" ? trim($post['name']) :"";
  600. if($name!=""){
  601. $condition[]=["b.name","like","%{$name}%"];
  602. }
  603. $code= isset($post['code'])&&$post['code']!="" ? trim($post['code']) :"";
  604. if($code!=""){
  605. $condition[]=["b.code", "=",$code];
  606. }
  607. $cgdNo = isset($post["sequenceNo"]) && $post["sequenceNo"]!="" ? $post["sequenceNo"] : "";
  608. if($cgdNo!=""){
  609. // $condition[]=[" `a`.`ShortText1618315935182` like '%{$cgdNo}%'";
  610. $condition[]=["a.ShortText1618315935182", "like","%{$cgdNo}%"];
  611. }
  612. $invcom= isset($post["inv_company"]) && $post["inv_company"]!="" ? $post["inv_company"] : "";
  613. if($invcom!=""){
  614. // $condition[]=[" `c`.`companyNo` = '{$invcom}'";
  615. $condition[]=["b.companyNo", "=",$invcom];
  616. }
  617. $invstatus= isset($post["inv_status"]) && $post["inv_status"]!=="" ? $post["inv_status"] : "";
  618. if($invstatus!==""){
  619. $cgdnp = Db::name("cgd_inv")->where(["status"=>$invstatus])->column("cgdNo");
  620. $condition[]=["a.sequenceNo","in",$cgdnp];
  621. }
  622. $send_status= isset($post["send_status"]) && $post["send_status"]!=="" ? $post["send_status"] : "";
  623. if($send_status!==""){
  624. $condition[]=["a.ShortText1618859321070","=",$send_status];
  625. }
  626. $th_status= isset($post["th_status"]) && $post["th_status"]!=="" ? $post["th_status"] : "";
  627. if($th_status!==""){
  628. // $condition[]=["a.th_status = '{$th_status}'";
  629. $condition[]=["a.th_status","=",$th_status];
  630. }
  631. $payinfo= isset($post["payinfo"]) && $post["payinfo"]!=="" ? $post["payinfo"] : "";
  632. if($payinfo!==""){
  633. // $condition[]=[" `a`.`ShortText1619463188366` = '{$payinfo}'";
  634. $condition[]=["a.ShortText1619463188366","=",$payinfo];
  635. }
  636. $invinfo= isset($post["invinfo"]) && $post["invinfo"]!=="" ? $post["invinfo"] : "";
  637. if($invinfo !==""){
  638. $condition[]=["a.ShortText1619463208482", "=", $invinfo];
  639. }
  640. $owner= isset($post['owner'])&&$post['owner']!="" ? trim($post['owner']) :"";
  641. if($owner!=""){
  642. $condition[]=["a.ownerName","like", "%{$owner}%"];
  643. }
  644. $goodNo= isset($post['goodNo'])&&$post['goodNo']!="" ? trim($post['goodNo']) :"";
  645. if($goodNo!=""){
  646. $condition[]=["a.ShortText1617861001482", "like", "%{$goodNo}%"];
  647. }
  648. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :0;
  649. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  650. $count = Db::name("cgd_info")->alias('a')->join('cfp_supplier_info b','a.ShortText1617861287265=b.code','left')
  651. ->where($condition)->count();
  652. // echo Db::name("cgd_info")->getLastSql();
  653. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  654. $page = $page>=$total?intval($total):$page;
  655. $list = Db::name("cgd_info")->alias('a')->join('cfp_supplier_info b','a.ShortText1617861287265=b.code','left')
  656. ->where($condition)->page($page,$size)->order('a.Date1618315953443 desc')->field("a.id,
  657. `a`.`sequenceNo` AS `sequenceNo`,
  658. `a`.`ownerName` AS `ownerName`,
  659. `a`.`department` AS `department`,
  660. b.code as supplierNo,
  661. `a`.`ShortText1618315935182` AS `cgdNO`,
  662. `b`.`name` AS `name`,
  663. `b`.`contector` AS `contector`,
  664. `b`.`mobile` AS `mobile`,
  665. `a`.`ShortText1617861001482` AS `goodNo`,
  666. `a`.`ShortText1617861966146` AS `goodName`,
  667. `a`.`ShortText1617865626160` AS `goodType`,
  668. `a`.`ShortText1617865633070` AS `goodBank`,
  669. `a`.`ShortText1617865675342` AS `goodModel`,
  670. `a`.`ShortText1617865678025` AS `goodMaterial`,
  671. `a`.`ShortText1617865680605` AS `goodColor`,
  672. `a`.`text1618268357055` AS `goodDesc`,
  673. `a`.`ShortText1617865685744` AS `goodUnit`,
  674. `a`.`ShortText1618240134229` AS `goodCat`,
  675. ifnull( `a`.`Number1618240480148`, '0' ) AS `weight`,
  676. ifnull( `a`.`Number1618240458074`, '0' ) AS `price`,
  677. `a`.`ShortText1617866158120` AS `isDiff`,
  678. `a`.`number1618240356104` AS `workDay`,
  679. `a`.`ShortText1617865688485` AS `taxPoint`,
  680. ifnull(`a`.`Number1617865804813`,0) AS `nakedPrice`,
  681. ifnull(`a`.`Number1617865807879`,0) AS `markPrice`,
  682. ifnull(`a`.`Number1617865810822`,0) AS `packPrice`,
  683. ifnull(`a`.`Number1617865813284`,0) AS `certPrice`,
  684. ifnull(`a`.`Number1617865816181`,0) AS `openPrice`,
  685. ifnull(`a`.`number1618240204358`,0) AS `craftPrice`,
  686. ifnull(`a`.`number1618240287778`,0) AS `postPrice`,
  687. ifnull(`a`.`Number1617865818517`,0) AS `totalPrice`,
  688. ifnull(`a`.`Number1618240685904`,0) AS `purchasePrice`,
  689. ifnull(`a`.`Number1618240600907`,0) AS `orderNum`,
  690. ifnull(`a`.`Number1618330470625`,0) AS `pay_fee`,
  691. ifnull(`a`.`Number1618330472961`,0) AS `wait_fee`,
  692. ifnull(`a`.`Number1618330541286`,0) AS `inv_open_fee`,
  693. ifnull(`a`.`Number1618330543270`,0) AS `inv_wait_fee`,
  694. `a`.`ShortText1619463188366` AS `payinfo`,
  695. `a`.`ShortText1619463208482` AS `invinfo`,
  696. a.th_status as th_info,
  697. ifnull(a.Number1619625470651,0) as th_num,
  698. ifnull(a.Number1619625483510,0) as th_fee,
  699. a.ShortText1618859321070 as send_info,
  700. `a`.`Number1619632826654` AS `diff_fee`,
  701. `a`.`Number1619632830397` AS `diff_info`,
  702. createdTime,
  703. a.Date1618315953443 as DownTime,
  704. IF
  705. (( `a`.`sequenceStatus` = 'COMPLETED' ), 2, 1 ) AS `order_status`
  706. ")->select();
  707. $data = [];
  708. foreach ($list as $key=>$value){
  709. $value['pay_price'] = sprintf( "%.2f",$value['totalPrice']*$value['orderNum']);
  710. $value['purchasePrice']-= $value['th_fee'];
  711. $value['orderNum']-= $value['th_num'];
  712. // ifnull( `c`.`companyNo`, '' ) AS `inv_company`,
  713. // ifnull( `c`.`status`, '0' ) AS `inv_status`,
  714. // ifnull( `c`.`id`, '0' ) AS `invid`",
  715. $cgdinvfo = Db::name("cgd_inv")->where(["cgdNo"=>$value['sequenceNo']])->find();
  716. $value['inv_company'] = isset($cgdinvfo['companyNo'])&&$cgdinvfo['companyNo']!="" ? trim($cgdinvfo['companyNo']) :'';
  717. $value['inv_status'] = isset($cgdinvfo['status'])&&$cgdinvfo['status']!=="" ? $cgdinvfo['status'] :'';
  718. $value['invid'] = isset($cgdinvfo['id'])&&$cgdinvfo['id']!="" ? intval($cgdinvfo['id']) :'';
  719. if($value['inv_company']!=""){
  720. $invcom = Db::name("company_info")->where([['companyNo',"=",$value['inv_company']],['status',"=",1]])
  721. ->find();
  722. $value['inv_company_name'] =isset($invcom['company_name']) ? $invcom['company_name']:"";
  723. }else{
  724. $value['inv_company_name'] ="";
  725. }
  726. $data[]=$value;
  727. }
  728. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  729. }
  730. public function PayInfo()
  731. {
  732. $post =$this->request->post();
  733. $token = isset($post['token']) ? trim($post['token']) : "";
  734. if($token==""){
  735. return error_show(101,'token不能为空');
  736. }
  737. $effetc = VerifyTokens($token);
  738. if(!empty($effetc) && $effetc['code']!=0){
  739. return error_show($effetc['code'],$effetc['message']);
  740. }
  741. $sid = isset($post['sid'])&& $post['sid']!="" ? intval($post['sid']) :"";
  742. if($sid==""){
  743. return error_show(1004,"参数sid 不能为空");
  744. }
  745. $payinfo = Db::name("pay_ment")->where("sid","=",$sid)->field("id,payNo,contector,supplierNo,name,pay_method,pay_rate,dpay_fee,inv_rate,rate,dpay_rate,pay_stages,return_img,remark,pay_status,dstatus,pay_remark")->find();
  746. if(empty($payinfo)){
  747. return error_show(1004,"付款申请信息未找到");
  748. }
  749. $payinfo['return_img'] = isset($payinfo['return_img']) && $payinfo['return_img']!="" ? explode(",",$payinfo['return_img']):"";
  750. $paylist = Db::name("pay_info")->where([["payNo","=",$payinfo['payNo']],["status","=",1]])->column("sequenceNo");
  751. $payinfo['list']=[];
  752. if($paylist){
  753. $list = Db::name("cgd_info")->alias('a')->join('cfp_supplier_info b','a.ShortText1617861287265=b.code','left')
  754. ->join("cfp_cgd_inv c",'c.cgdNo=a.sequenceNo','left')
  755. ->where([["a.sequenceNo","in",$paylist],["a.status","=",1]])->field("a.id,
  756. `a`.`sequenceNo` AS `sequenceNo`,
  757. `a`.`ownerName` AS `ownerName`,
  758. `a`.`department` AS `department`,
  759. b.code as supplierNo,
  760. `a`.`ShortText1618315935182` AS `cgdNO`,
  761. `b`.`name` AS `name`,
  762. `b`.`contector` AS `contector`,
  763. `b`.`mobile` AS `mobile`,
  764. `a`.`ShortText1617861001482` AS `goodNo`,
  765. `a`.`ShortText1617861966146` AS `goodName`,
  766. `a`.`ShortText1617865626160` AS `goodType`,
  767. `a`.`ShortText1617865633070` AS `goodBank`,
  768. `a`.`ShortText1617865675342` AS `goodModel`,
  769. `a`.`ShortText1617865678025` AS `goodMaterial`,
  770. `a`.`ShortText1617865680605` AS `goodColor`,
  771. `a`.`text1618268357055` AS `goodDesc`,
  772. `a`.`ShortText1617865685744` AS `goodUnit`,
  773. `a`.`ShortText1618240134229` AS `goodCat`,
  774. ifnull( `a`.`Number1618240480148`, '0' ) AS `weight`,
  775. ifnull( `a`.`Number1618240458074`, '0' ) AS `price`,
  776. `a`.`ShortText1617866158120` AS `isDiff`,
  777. `a`.`number1618240356104` AS `workDay`,
  778. `a`.`ShortText1617865688485` AS `taxPoint`,
  779. `a`.`Number1617865804813` AS `nakedPrice`,
  780. `a`.`Number1617865807879` AS `markPrice`,
  781. `a`.`Number1617865810822` AS `packPrice`,
  782. `a`.`Number1617865813284` AS `certPrice`,
  783. `a`.`Number1617865816181` AS `openPrice`,
  784. `a`.`number1618240204358` AS `craftPrice`,
  785. `a`.`number1618240287778` AS `postPrice`,
  786. `a`.`Number1617865818517` AS `totalPrice`,
  787. `a`.`Number1618240685904` AS `purchasePrice`,
  788. `a`.`Number1618240600907` AS `orderNum`,
  789. `a`.`Number1618330470625` AS `pay_fee`,
  790. `a`.`Number1618330472961` AS `wait_fee`,
  791. `a`.`Number1618330541286` AS `inv_open_fee`,
  792. `a`.`Number1618330543270` AS `inv_wait_fee`,
  793. `a`.`ShortText1619463188366` AS `payinfo`,
  794. `a`.`ShortText1619463208482` AS `invinfo`,
  795. a.th_status as th_info,
  796. a.ShortText1618859321070 as send_info,
  797. `a`.`Number1619632826654` AS `diff_fee`,
  798. `a`.`Number1619632830397` AS `diff_info`,
  799. createdTime,
  800. a.Date1618315953443 as DownTime,
  801. IF
  802. (( `a`.`sequenceStatus` = 'COMPLETED' ), 2, 1 ) AS `order_status`,
  803. ifnull( `c`.`companyNo`, '' ) AS `inv_company`,
  804. ifnull( `c`.`status`, '0' ) AS `inv_status`,
  805. ifnull( `c`.`id`, '0' ) AS `invid`")
  806. ->select();
  807. $data=[];
  808. foreach ($list as $key=>$value){
  809. $value['pay_price'] =sprintf( "%.2f",$value['totalPrice']*$value['orderNum']);
  810. if($value['inv_company']!=""){
  811. $invcom = Db::name("company_info")->where([['companyNo',"=",$value['inv_company']],['status',"=",1]])
  812. ->find();
  813. $value['inv_company_name'] =isset($invcom['company_name']) ? $invcom['company_name']:"";
  814. }else{
  815. $value['inv_company_name'] ="";
  816. }
  817. $data[]=$value;
  818. }
  819. $payinfo['list']=$data;
  820. }
  821. return app_show(0,"获取成功",$payinfo);
  822. }
  823. /**
  824. * @return \think\response\Json|void
  825. * @throws \think\db\exception\DataNotFoundException
  826. * @throws \think\db\exception\DbException
  827. * @throws \think\db\exception\ModelNotFoundException
  828. * @throws \think\exception\DbException
  829. */
  830. public function PaySave(){
  831. $post =$this->request->post();
  832. $token = isset($post['token']) ? trim($post['token']) : "";
  833. if($token==""){
  834. return error_show(101,'token不能为空');
  835. }
  836. $effetc = VerifyTokens($token);
  837. if(!empty($effetc) && $effetc['code']!=0){
  838. return error_show($effetc['code'],$effetc['message']);
  839. }
  840. $payNo = isset($post['payNo'])&&$post['payNo']!="" ? trim($post['payNo']) :"";
  841. if($payNo==""){
  842. return error_show(1003,"参数payNo 不能为空");
  843. }
  844. $pay = Db::name("pay")->where("payNo","=",$payNo)->find();
  845. if(empty($pay)){
  846. return error_show(1003,"对账数据不存在");
  847. }
  848. $payStage= Db::name("pay_stages")->where([["is_del","=",0],["payNo","=",$payNo],["status","<>",0]])
  849. ->count();
  850. $payT= Db::name("pay_stages")->where([["is_del","=",0],["payNo","=",$payNo],["status","=",2]])
  851. ->count();
  852. if($payStage!=0&&$payT==0){
  853. return error_show(1003,"对账已申请付款无法修改");
  854. }
  855. $inv_rate = isset($post['inv_rate'])&&$post['inv_rate']!==""? $post['inv_rate']:"";
  856. if($inv_rate===""){
  857. return error_show(1004,"发票税率不能为空");
  858. }
  859. $rate = Db::name("order_rate")->where("id","=",$inv_rate)->find();
  860. if(empty($rate)){
  861. return error_show(1004,"未找到对应发票税率");
  862. }
  863. $paystatus= Db::name("pay_stages")->where([["is_del","=",0],["payNo","=",$payNo],["pay_status","<>",0]])
  864. ->count();
  865. if($paystatus!=0 || $payT>0){
  866. $pay['inv_rate'] = $inv_rate;
  867. $pay['updatetime'] = date("Y-m-d H:i:s");
  868. $payDAba=Db::name("pay")->save($pay);
  869. if($payDAba){
  870. return app_show(0,"税率修改成功");
  871. }else{
  872. return error_show(1005,"税率修改失败");
  873. }
  874. }
  875. $cids = isset($post['cids'])&&$post['cids']!="" ? trim($post['cids']) :"";
  876. if($cids==""){
  877. return error_show(1003,"参数cids不能为空");
  878. }
  879. $supplierNo = isset($post['supplierNo'])&&$post['supplierNo']!="" ? trim($post['supplierNo']) :"";
  880. if($supplierNo==""){
  881. return error_show(1003,"参数supplierNo不能为空");
  882. }
  883. $pay_method = isset($post['pay_method'])&&$post['pay_method']!="" ? $post['pay_method']:"";
  884. if($pay_method==""){
  885. return error_show(1004,"账期类型不能为空");
  886. }
  887. $pay_rate = isset($post['pay_rate'])&&is_array($post['pay_rate'])? $post['pay_rate']:[100];
  888. Db::startTrans();
  889. $payinfo = Db::name("pay_info")->where([['payNo',"=",$payNo],['status',"=",1]])->column("sequenceNo");
  890. $reset = Db::name("cgd_info")->where([["sequenceNo","in",$payinfo],["ShortText1617861287265","=",$pay['supplierNo']],
  891. ["status","=",1]]
  892. )->save(["status"=>0,"check_rate"=>""]);
  893. if($reset==false){
  894. Db::rollback();
  895. return error_show(1004,"采购单状态修改失败!");
  896. }else{
  897. $delpay = Db::name("pay_info")->where([['payNo',"=",$payNo],['status',"=",1]])->save(["status"=>0]);
  898. if($delpay==false){
  899. Db::rollback();
  900. return error_show(1004,"采购单关联失败!");
  901. }
  902. $delstage = Db::name("pay_stages")->where([['payNo',"=",$payNo],['is_del',"=",0]])->save(["is_del"=>1]);
  903. if($delstage==false){
  904. Db::rollback();
  905. return error_show(1004,"对账单付款修改失败!");
  906. }
  907. }
  908. $cgd = Db::name("cgd_info")->where([["id","in",$cids],["ShortText1617861287265","=",$supplierNo],["status","=",0]]
  909. )->field("sequenceNo,`ShortText1619463208482` as inv_status,`ShortText1619463188366` as pay_status,Number1618330470625 as apay,Number1618330472961 as wpay,Number1618330541286 ainv,Number1618330543270 as winv,id")->select();
  910. if(empty($cgd->toArray())){
  911. return error_show(1004,"未找到有关的数据");
  912. }
  913. try {
  914. $data=[];
  915. $paydata=[
  916. "payNo"=>$payNo,
  917. "id"=>$pay['id'],
  918. "creater"=>$effetc['data']['user']['id'],
  919. "total_fee"=>0,
  920. "pay_fee"=>0,
  921. "supplierNo"=>$supplierNo,
  922. "invoiceNo"=>"",
  923. "run_fee"=>$pay['run_fee'],
  924. "pay_method"=>$pay_method,
  925. "pay_rate"=>json_encode($pay_rate),
  926. "wait_fee"=>0,
  927. "ainv_fee"=>0,
  928. "winv_fee"=>0,
  929. "inv_rate"=>$inv_rate,
  930. //"inv_rate"=>0,
  931. "remark"=>"",
  932. "status"=>1,
  933. "updatetime"=>date("Y-m-d H:i:s"),
  934. ];
  935. $invcom="";
  936. foreach ($cgd as $key=>$value){
  937. $temp=[];
  938. $coma =Db::name("cgd_inv")->where([['status',"=",2],['cgdNo',"=",$value['sequenceNo']]])->find();
  939. if(empty($coma)){
  940. Db::rollback();
  941. return error_show(1003,"采购单{$value['sequenceNo']}未确认发票开票信息");
  942. }
  943. if($invcom==""){
  944. $invcom = $coma['companyNo'];
  945. }else{
  946. if($invcom!=$coma['companyNo']){
  947. Db::rollback();
  948. return error_show(1003,"采购单{$value['sequenceNo']}发票开票信息不一致");
  949. }
  950. }
  951. $temp['sequenceNo']=$value['sequenceNo'];
  952. $temp['total_fee']=($value['apay']*100+$value['wpay']*100)/100;
  953. $temp['pay_fee']=round($value['apay'],2);
  954. $temp['wait_fee']=round($value['wpay'],2);
  955. $temp['winv_fee']=round($value['winv'],2);
  956. $temp['ainv_fee']=round($value['ainv'],2);
  957. $temp['payNo']=$payNo;
  958. $temp['addtime']=date("Y-m-d H:i:s");
  959. $temp['updatetime']=date("Y-m-d H:i:s");
  960. $data[]=$temp;
  961. $paydata['total_fee']+= $temp['total_fee'];
  962. $paydata['wait_fee']+= $temp['wait_fee'];
  963. $paydata['pay_fee']+= $temp['pay_fee'];
  964. $paydata['ainv_fee']+= $temp['ainv_fee'];
  965. $paydata['winv_fee']+= $temp['winv_fee'];
  966. }
  967. $paydata['total_fee']= round($paydata['total_fee'],2);
  968. $paydata['wait_fee']= round($paydata['wait_fee'],2);
  969. $paydata['pay_fee']= round($paydata['pay_fee'],2);
  970. $paydata['ainv_fee']= round($paydata['ainv_fee'],2);
  971. $paydata['winv_fee']= round($paydata['winv_fee'],2);
  972. if(count($pay_rate)>=1){
  973. $paydata['run_fee'] = round($pay_rate[0],2);
  974. $paydata['wait_fee'] = round($paydata['total_fee']-$paydata['pay_fee'],2);
  975. //$paydata['wait_fee'] = round($paydata['total_fee']-$paydata['pay_fee']- $paydata['run_fee'],2);:ToDo
  976. }
  977. $info = Db::name("pay_info")->insertAll($data);
  978. if($info!=0){
  979. $payDA=Db::name("pay")->save($paydata);
  980. if($payDA){
  981. $stage=[];
  982. $pay=0;
  983. $tota= round(array_sum($pay_rate),2);
  984. if( $paydata['wait_fee']!= $tota){
  985. return error_show(1005,'剩余待付金额未对账');
  986. }
  987. foreach ($pay_rate as $key=>$value){
  988. $temp=[];
  989. $temp['payNo']=$payNo;
  990. $temp['dzNo']=makeNo("DZ");
  991. if($paydata['wait_fee']>=$value){
  992. $paydata['wait_fee']=round($paydata['wait_fee']-$value,2);
  993. $temp['pay_fee']=round($value,2);
  994. }else{
  995. return error_show(1005,'待付金额不足于分期金额');
  996. }
  997. $temp['pay_rate']=$value;
  998. $temp['pay_stages']=$key+1;
  999. $temp['return_img']="";
  1000. $temp['status']=$paydata['winv_fee']==0?7: 0;
  1001. $temp['pay_status']=$temp['pay_fee']==0?4: 0;
  1002. $temp['companyNo'] = $invcom;
  1003. $temp['addtime']=date("Y-m-d H:i:s");
  1004. $temp['updatetime']=date("Y-m-d H:i:s");
  1005. $pay+=$temp['pay_fee'];
  1006. $stage[] =$temp;
  1007. }
  1008. $pay_stage = Db::name("pay_stages")->insertAll($stage);
  1009. if($pay_stage){
  1010. Db::name("cgd_info")->where("id","in",$cids)->save(['status'=>1,"check_rate"=>""]);
  1011. Db::commit();
  1012. return app_show(0,"修改成功");
  1013. }
  1014. }
  1015. }
  1016. Db::rollback();
  1017. return error_show(1003,"修改失败");
  1018. }catch(\Exception $e){
  1019. Db::rollback();
  1020. return error_show(1003,$e->getMessage());
  1021. }
  1022. }
  1023. /**
  1024. * @return \think\response\Json|void
  1025. * @throws \think\db\exception\DataNotFoundException
  1026. * @throws \think\db\exception\DbException
  1027. * @throws \think\db\exception\ModelNotFoundException
  1028. * @throws \think\exception\DbException
  1029. */
  1030. public function CgdInv(){
  1031. $post =$this->request->post();
  1032. $token = isset($post['token']) ? trim($post['token']) : "";
  1033. if($token==""){
  1034. return error_show(101,'token不能为空');
  1035. }
  1036. $effetc = VerifyTokens($token);
  1037. if(!empty($effetc) && $effetc['code']!=0){
  1038. return error_show($effetc['code'],$effetc['message']);
  1039. }
  1040. $cgd = isset($post['cgd'])&&$post['cgd']!='' ? $post['cgd']:"";
  1041. if($cgd==""){
  1042. return error_show(1004,"参数cgd 不能为空");
  1043. }
  1044. if(!is_array($cgd)){
  1045. return error_show(1004,"参数cgd 无效数据");
  1046. }
  1047. // $cgd = Db::name("cgd_info")->where([["status","=",0],["sequenceNo","in",$cids]])->field("id,sequenceNo,ShortText1617861287265 as gys")
  1048. // ->select()->toArray();
  1049. // if(empty($cgd)){
  1050. // return error_show(1004,"未找到绑定的数据");
  1051. // }
  1052. // $company = isset($post['companyNo'])&&$post['companyNo']!="" ? $post['companyNo']:"";
  1053. // if($company==""){
  1054. // return error_show(1003,"参数companyNo 不能为空");
  1055. // }
  1056. // $companyinfo = Db::name("company_info")->where([["status","=",1],["companyNo","=",$company]])->find();
  1057. // if(empty($companyinfo)){
  1058. // return error_show(1004,"企业开票信息未找到");
  1059. // }
  1060. Db::startTrans();
  1061. try{
  1062. foreach ($cgd as $value){
  1063. if(!isset($value['cgdNo'])||$value['cgdNo']==""){
  1064. Db::rollback();
  1065. return error_show(1003,"参数cgdNo 不能为空");
  1066. }
  1067. if(!isset($value['companyNo'])||$value['companyNo']==""){
  1068. Db::rollback();
  1069. return error_show(1003,"参数companyNo 不能为空");
  1070. }
  1071. $cgdinfo = Db::name("cgd_info")->where([["sequenceNo","=",$value['cgdNo']]])->field("id,sequenceNo,ShortText1617861287265 as gys,status")->find();
  1072. if(empty($cgdinfo)){
  1073. Db::rollback();
  1074. return error_show(1004,"未找到采购单{$value['cgdNo']}数据");
  1075. }
  1076. if($cgdinfo['status']!=0){
  1077. Db::rollback();
  1078. return error_show(1004,"采购单{$value['cgdNo']}数据正在对账中");
  1079. }
  1080. $companyinfo = Db::name("company_info")->where([["status","=",1],["companyNo","=",$value['companyNo']]])->find();
  1081. if(empty($companyinfo)){
  1082. Db::rollback();
  1083. return error_show(1004,"企业开票信息未找到");
  1084. }
  1085. $inv = Db::name("cgd_inv")->where("cgdNo","=",$value['cgdNo'])->find();
  1086. if($inv){
  1087. $inv['companyNo']=$value['companyNo'];
  1088. $inv['sup_company']=$cgdinfo['gys'];
  1089. $inv['status']=1;
  1090. $inv['updatetime']=date("Y-m-d H:i:s");
  1091. $que = Db::name("cgd_inv")->save($inv);
  1092. }else{
  1093. $inv = [
  1094. "cgdNo"=> $value['cgdNo'],
  1095. "companyNo"=> $value['companyNo'],
  1096. "sup_company"=> $cgdinfo['gys'],
  1097. "status"=> 1,
  1098. "addtime"=> date("Y-m-d H:i:s"),
  1099. "updatetime"=> date("Y-m-d H:i:s")
  1100. ];
  1101. $que = Db::name("cgd_inv")->insert($inv);
  1102. }
  1103. if($que==false){
  1104. Db::rollback();
  1105. return error_show(1004,"采购单{$value['cgdNo']}数据关联失败");
  1106. }
  1107. }
  1108. Db::commit();
  1109. return app_show(0,"开票信息关联成功");
  1110. }catch (\Exception $e){
  1111. Db::rollback();
  1112. return error_show(1004,$e->getMessage());
  1113. }
  1114. }
  1115. /**
  1116. * @return \think\response\Json|void
  1117. * @throws \think\db\exception\DataNotFoundException
  1118. * @throws \think\db\exception\DbException
  1119. * @throws \think\db\exception\ModelNotFoundException
  1120. * @throws \think\exception\DbException
  1121. */
  1122. public function CgdExam(){
  1123. $post =$this->request->post();
  1124. $token = isset($post['token']) ? trim($post['token']) : "";
  1125. if($token==""){
  1126. return error_show(101,'token不能为空');
  1127. }
  1128. $effetc = VerifyTokens($token);
  1129. if(!empty($effetc) && $effetc['code']!=0){
  1130. return error_show($effetc['code'],$effetc['message']);
  1131. }
  1132. $cgdinv = isset($post['cgdinv'])&& $post['cgdinv']!="" ? $post['cgdinv'] :"";
  1133. if($cgdinv==""){
  1134. return error_show(1004,"参数cgdinv 不能为空");
  1135. }
  1136. if(!is_array($cgdinv)){
  1137. return error_show(1004,"参数cgdinv 无效数据");
  1138. }
  1139. Db::startTrans();
  1140. try {
  1141. foreach ($cgdinv as $value){
  1142. $info = Db::name("cgd_inv")->where('cgdNo',"=",$value['invid'])->find();
  1143. if(empty($info)){
  1144. Db::rollback();
  1145. return error_show(1004,"未找到{$value['invid']}对应的关联数据");
  1146. }
  1147. if($info['status']!=1){
  1148. Db::rollback();
  1149. return error_show(1004,"采购单{$info['cgdNo']}状态异常");
  1150. }
  1151. if(!in_array($value['status'],[0,1,2,3])){
  1152. Db::rollback();
  1153. return error_show(1004,"参数status 无效值");
  1154. }
  1155. $inv=["status"=>$value['status'],"remark"=>$value['remark'],"updatetime"=>date("Y-m-d H:i:s")];
  1156. $save= Db::name("cgd_inv")->where('cgdNo',"=",$value['invid'])->save($inv);
  1157. if(!$save){
  1158. Db::rollback();
  1159. return error_show(1004,"采购单{$info['cgdNo']}状态更新失败");
  1160. }
  1161. }
  1162. Db::commit();
  1163. return app_show(0,"采购单关联状态更新成功");
  1164. }catch (\Exception $e){
  1165. Db::rollback();
  1166. return error_show(1004,$e->getMessage());
  1167. }
  1168. }
  1169. /**
  1170. * status 1申请付款 2 业务驳回 3发票已上传 4发票确认 5 财务审核发票 6 付款回执 7完成 0 取消申请
  1171. *
  1172. * @param int $id
  1173. * @return \think\Response
  1174. */
  1175. public function InvReset()
  1176. {
  1177. $post =$this->request->post();
  1178. $token = isset($post['token']) ? trim($post['token']) : "";
  1179. if($token==""){
  1180. return error_show(101,'token不能为空');
  1181. }
  1182. $effetc = VerifyTokens($token);
  1183. if(!empty($effetc) && $effetc['code']!=0){
  1184. return error_show($effetc['code'],$effetc['message']);
  1185. }
  1186. $stageid = isset($post['sid'])&& $post['sid']!="" ? intval($post['sid']) :"";
  1187. if($stageid==""){
  1188. return error_show(1004,"参数sid 不能为空");
  1189. }
  1190. $payStage = Db::name("pay_stages")->where([['id',"=",$stageid],["is_del","=",0]])->find();
  1191. if(empty($payStage)){
  1192. return error_show(1005,"未找到付款申请信息");
  1193. }
  1194. $pay = Db::name("pay")->where(['payNo'=>$payStage['payNo']])->find();
  1195. if(empty($pay)){
  1196. return error_show(1004,"未找到对账的数据");
  1197. }
  1198. if(!in_array($pay['status'],[0,1])){
  1199. return error_show(1004,"数据状态异常");
  1200. }
  1201. if($payStage["pay_stages"]>1){
  1202. $prev = $payStage["pay_stages"]-1;
  1203. $prevStage = Db::name("pay_stages")->where([['pay_stages',"=",$prev],['payNo',"=",$payStage['payNo']],["is_del","=",0]])->find();
  1204. if(empty($prevStage)){
  1205. return error_show(1004,"对账上一期数据丢失");
  1206. }
  1207. if($prevStage['status']!=7){
  1208. return error_show(1004,"对账上一期数据流程正在进行中");
  1209. }
  1210. }
  1211. $data=[
  1212. "status"=>3,
  1213. "updatetime"=>date("Y-m-d H:i:s")
  1214. ];
  1215. Db::startTrans();
  1216. try {
  1217. $upd=Db::name("pay_stages")->where([['id',"=",$stageid],['payNo',"=",$payStage['payNo']],["is_del","=",0]])->save($data);
  1218. if($upd){
  1219. Db::commit();
  1220. return app_show(0,"状态修改成功");
  1221. }else{
  1222. Db::rollback();
  1223. return error_show(1004,"状态修改失败");
  1224. }
  1225. }catch (\Exception $e){
  1226. Db::rollback();
  1227. return error_show(1005,$e->getMessage());
  1228. }
  1229. }
  1230. /**退货详情
  1231. * @return \think\response\Json|void
  1232. * @throws \think\db\exception\DataNotFoundException
  1233. * @throws \think\db\exception\DbException
  1234. * @throws \think\db\exception\ModelNotFoundException
  1235. * @throws \think\exception\DbException
  1236. */
  1237. public function thinfo(){
  1238. $post =$this->request->post();
  1239. $token = isset($post['token']) ? trim($post['token']) : "";
  1240. if($token==""){
  1241. return error_show(101,'token不能为空');
  1242. }
  1243. $effetc = VerifyTokens($token);
  1244. if(!empty($effetc) && $effetc['code']!=0){
  1245. return error_show($effetc['code'],$effetc['message']);
  1246. }
  1247. $type = isset($post['type'])&&$post['type']!=="" ? $post['type'] :1;
  1248. $codeNo = isset($post['codeNo'])&&$post['codeNo']!=="" ? trim($post['codeNo']) :'';
  1249. $condition =[];
  1250. if($type==1&&$codeNo!=""){
  1251. $condition[]=['th_cgdNo',"=",$codeNo];
  1252. }
  1253. if($type==2&&$codeNo!=""){
  1254. $condition[]=['th_qrdNo',"=",$codeNo];
  1255. }
  1256. if($type==3&&$codeNo!=""){
  1257. $condition[]=['th_qrdcpNo',"=",$codeNo];
  1258. }
  1259. $status = isset($post['status'])&&$post['status']!=="" ? $post['status'] :'';
  1260. if($status!=""){
  1261. $condition[]=['th_status',"=",$status];
  1262. }
  1263. $company = isset($post['company'])&&$post['company']!=="" ? trim($post['company']) :'';
  1264. if($company!=""){
  1265. $condition[]=['th_company',"like","%{$company}%"];
  1266. }
  1267. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :1;
  1268. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  1269. $count = Db::name('th_source')->where($condition)->count();
  1270. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  1271. $page = $page>=$total?intval($total):$page;
  1272. $thinfo = Db::name('th_source')->where($condition)->page(intval($page),$size)->order("createtime desc")->select();
  1273. return app_show(0,"获取成功",["list"=>$thinfo,"count"=>$count]);
  1274. }
  1275. public function UnPay(){
  1276. $post =$this->request->post();
  1277. $token = isset($post['token']) ? trim($post['token']) : "";
  1278. if($token==""){
  1279. return error_show(101,'token不能为空');
  1280. }
  1281. $effetc = VerifyTokens($token);
  1282. if(!empty($effetc) && $effetc['code']!=0){
  1283. return error_show($effetc['code'],$effetc['message']);
  1284. }
  1285. $codeNo = isset($post['id'])&&$post['id']!=="" ? trim($post['id']) :'';
  1286. if($codeNo===""){
  1287. return error_show(1004,'参数id 不能为空');
  1288. }
  1289. $update = Db::name('cgd_info')->where("id","in",$codeNo)->update(['status'=>2]);
  1290. // echo Db::name('cgd_info')->getLastSql();
  1291. if($update){
  1292. return app_show(0,'更新成功');
  1293. }else{
  1294. return error_show(1004,'更新失败');
  1295. }
  1296. }
  1297. }