Payment.php 72 KB

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