Payment.php 73 KB

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