Payment.php 59 KB

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