Payment.php 89 KB

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