Payment.php 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\model\CgdInfo;
  5. use app\admin\model\FhdChild;
  6. use app\admin\model\GoodCombind;
  7. use app\admin\model\InvoiceOrder;
  8. use app\admin\model\InvoicePay;
  9. use app\admin\model\InvoicePool;
  10. use app\admin\model\OrderCategory;
  11. use app\admin\model\Pay;
  12. use app\admin\model\InvoiceInfo;
  13. use app\admin\model\PayInvoice;
  14. use app\admin\model\ReportCode;
  15. use think\App;use think\Exception;
  16. use think\facade\Config;
  17. use think\facade\Db;
  18. use think\facade\Validate;
  19. class Payment extends Base
  20. {
  21. protected $invoiceType;
  22. protected $invName;
  23. // 发票状态,取值范围:
  24. //
  25. // 正常:valid
  26. // 已作废:invalidated
  27. // 已红冲:reversed
  28. protected $invStatus=[0=>"待验证","1"=>'正常',"2"=>'已作废',"3"=>'已红冲'];
  29. public function __construct(App $app) {
  30. parent::__construct($app);
  31. $invoice =Config::get("invoiceType");
  32. $this->invoiceType = $invoice['invoiceName'];
  33. $this->invName = $invoice['invoiceType'];
  34. }
  35. /**
  36. *付款详细列表
  37. * @return \think\Response
  38. */
  39. public function paymentList()
  40. {
  41. $post = $this->post;
  42. $condition = [["a.is_del","=",0],['pay_type','=',$post['pay_type']??1]];
  43. $check = checkRole($this->roleid,[79,119]);
  44. if($check){
  45. $condition[]=["b.apply_id","=",$this->uid];
  46. }
  47. $startTime= isset($post['startTime'])&&$post['startTime']!="" ? date("Y-m-d 00:00:00",strtotime($post['startTime']))
  48. :"";
  49. $endTime= isset($post['endTime'])&&$post['endTime']!="" ? date("Y-m-d 23:59:59",strtotime($post['endTime']))
  50. :"";
  51. $supplierNo= isset($post['supplierNo'])&&$post['supplierNo']!="" ? trim($post['supplierNo']) :"";
  52. $supplierName= isset($post['supplierName'])&&$post['supplierName']!="" ? trim($post['supplierName']) :"";
  53. $companyNo= isset($post['companyNo'])&&$post['companyNo']!="" ? trim($post['companyNo']) :"";
  54. $payNo= isset($post['payNo'])&&$post['payNo']!="" ? trim($post['payNo']) :"";
  55. $apply= isset($post['apply_name'])&&$post['apply_name']!="" ? trim($post['apply_name']) :"";
  56. $paystatus= isset($post['status'])&&$post['status']!="" ? $post['status'] :"";
  57. $dzNo= isset($post['dzNo'])&&$post['dzNo']!="" ? trim($post['dzNo']) :"";
  58. if($startTime!=""){
  59. $condition[]=['b.addtime','>=',$startTime];
  60. }
  61. if($endTime!=""){
  62. $condition[]=['b.addtime','<=',$endTime];
  63. }
  64. if($supplierNo!=""){
  65. $condition[]=['a.supplierNo','=',$supplierNo];
  66. }
  67. if($supplierName!=""){
  68. $condition[]=['a.supplierName','like',"%$supplierName%"];
  69. }
  70. if($companyNo!=""){
  71. $condition[]=['a.companyNo','=',$companyNo];
  72. }
  73. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  74. if($relaComNo!=""){
  75. $condition[]=['a.companyNo|a.supplierNo','=',$relaComNo];
  76. }
  77. if($payNo!=""){
  78. $condition[]=['a.payNo','like',"%$payNo%"];
  79. }
  80. if($dzNo!=""){
  81. $condition[]=['b.dzNo','like',"%$dzNo%"];
  82. }
  83. if($apply!=""){
  84. $condition[]=['b.apply_name','like',"%$apply%"];
  85. }
  86. if($paystatus!=""){
  87. $condition[]=['b.status','=',$paystatus];
  88. }
  89. if(isset($post['is_comon'])&&$post['is_comon']!=='') $condition []=['is_comon','=',$post['is_comon']];
  90. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :1;
  91. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  92. $count = Db::name("pay_payment")->alias('b')
  93. ->join("pay a","`a`.`payNo` = `b`.`payNo` AND b.is_del = 0 ",'left')
  94. ->where($condition)->count();
  95. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  96. $page = $page>=$total?intval($total):$page;
  97. $list = Db::name("pay_payment")->alias('b')
  98. ->join("pay a","`a`.`payNo` = `b`.`payNo` AND ( `b`.`is_del` = 0 ) ",'left')
  99. ->where($condition)->page(intval($page),$size)->order("b.addtime desc")
  100. ->field(" `b`.`id` AS `id`,
  101. `a`.`payNo` AS `payNo`,
  102. `a`.`supplierNo`,
  103. `a`.`supplierName`,
  104. `a`.`apay_fee` ,
  105. `a`.`total_fee` AS `total_fee`,
  106. `b`.`apply_name`,
  107. `b`.`apply_id`,
  108. `a`.`is_comon`,
  109. `a`.`winv_fee` ,
  110. `a`.`ainv_fee` ,
  111. `a`.`wpay_fee` ,
  112. `a`.`pay_status` ,
  113. `a`.`inv_status` ,
  114. `b`.`pay_fee` AS `dpay_fee`,
  115. `b`.`return_img`,
  116. `b`.`return_time`,
  117. `b`.`status` AS `dstatus`,
  118. `a`.`status`,
  119. `a`.`remark` AS `remark`,
  120. `b`.`addtime` ,
  121. `a`.`companyNo` ,
  122. `a`.`companyName` ,
  123. `b`.`dzNo` AS `dzNo`
  124. ")->select()->toArray();
  125. //校验是否开通了供应商账号
  126. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list,'supplierNo')));
  127. $data=[];
  128. foreach ($list as $key=>$value){
  129. $value['ordeNum'] = Db::name("pay_info")->where([["payNo","=",$value["payNo"]],["is_del","=",0]])->count();
  130. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  131. $data[]=$value;
  132. }
  133. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  134. }
  135. /**
  136. *对账单列表
  137. * @return \think\Response
  138. */
  139. public function payList()
  140. {
  141. $post = $this->post;
  142. $condition = [["is_del","=",0],['pay_type','=',$post['pay_type']??1]];
  143. $check = checkRole($this->roleid,[77,117]);
  144. if($check){
  145. $condition[]=["apply_id","=",$this->uid];
  146. }
  147. $startTime= isset($post['startTime'])&&$post['startTime']!="" ? date("Y-m-d 00:00:00",strtotime($post['startTime'])) :"";
  148. $endTime= isset($post['endTime'])&&$post['endTime']!="" ? date("Y-m-d 23:59:59",strtotime($post['endTime'])) :"";
  149. $supplierNo= isset($post['supplierNo'])&&$post['supplierNo']!="" ? trim($post['supplierNo']) :"";
  150. $supplierName= isset($post['supplierName'])&&$post['supplierName']!="" ? trim($post['supplierName']) :"";
  151. $payNo= isset($post['payNo'])&&$post['payNo']!="" ? trim($post['payNo']) :"";
  152. $paystatus= isset($post['pay_status'])&&$post['pay_status']!="" ? $post['pay_status'] :"";
  153. $inv_status= isset($post['inv_status'])&&$post['inv_status']!="" ? $post['inv_status'] :"";
  154. $status= isset($post['status'])&&$post['status']!="" ? $post['status'] :"";
  155. $tag_id= isset($post['tag_id'])&&$post['tag_id']!=="" ? intval($post['tag_id']) :"";
  156. if($tag_id!==''){
  157. if($tag_id==1) $condition[]=["inv_tag","=",1];
  158. if($tag_id==2) $condition[]=["pay_tag","=",1];
  159. if($tag_id==3) $condition[]=["pay_tag|inv_tag","=",1];
  160. if($tag_id==4) $condition[]=["pay_tag&inv_tag","=",1];
  161. }
  162. if($startTime!=""){
  163. $condition[]=["addtime",">=",$startTime];
  164. }
  165. if($endTime!=""){
  166. $condition[]=["addtime","<=",$endTime];
  167. }
  168. if($supplierNo!=""){
  169. $condition[]=["supplierNo","like","%$supplierNo%"];
  170. }
  171. if($supplierName!=""){
  172. $condition[]=["supplierName","like","%$supplierName%"];
  173. }
  174. if($payNo!=""){
  175. $condition[]=["payNo","like","%$payNo%"];
  176. }
  177. if($inv_status!==''){
  178. if($inv_status==0){
  179. $condition[]=["inv_status","<>",3];
  180. }else{
  181. $condition[]=["inv_status","=",$inv_status];
  182. }
  183. }
  184. if($paystatus!==''){
  185. if($paystatus==0){
  186. $condition[]=["pay_status","<>",3];
  187. }else{
  188. $condition[]=["pay_status","=",$paystatus];
  189. }
  190. }
  191. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=''?trim($post['companyNo']):'';
  192. if($companyNo!==''){
  193. $condition[]=["companyNo","=",$companyNo];
  194. }
  195. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  196. if($relaComNo!=""){
  197. $condition[]=["companyNo|supplierNo","=",$relaComNo];
  198. }
  199. $companyName = isset($post['companyName'])&&$post['companyName']!=''?trim($post['companyName']):'';
  200. if($companyName !=''){
  201. $condition[]=["companyName","like","%$companyName%"];
  202. }
  203. if($status!=""){
  204. $condition[]=["status","=",$status];
  205. }
  206. if(isset($post['is_comon'])&&$post['is_comon']!=='') $condition []=['is_comon','=',$post['is_comon']];
  207. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :1;
  208. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  209. $count = Db::name("pay")->where($condition)->count();
  210. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  211. $page = $page>=$total?intval($total):$page;
  212. $list = Db::name("pay")->where($condition)->page(intval($page),$size)->order("addtime desc")->select()->toArray();
  213. //校验是否开通了供应商账号
  214. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list,'supplierNo')));
  215. $data=[];
  216. foreach ($list as $key=>$value){
  217. $value['ordeNum'] = Db::name("pay_info")->where([["payNo","=",$value["payNo"]],["is_del","=",0],['status','=',1]])->count();
  218. $value['inv_tag_name']='';
  219. $value['pay_tag_name']='';
  220. $value['pay_tag_img']='';
  221. $value['inv_tag_id']='0';
  222. $value['pay_tag_id']='0';
  223. $value['pay_tag_remark']='';
  224. $value['inv_tag_img']='';
  225. $value['inv_tag_remark']='';
  226. if($value['pay_tag']==1 || $value['inv_tag']==1){
  227. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  228. (['code'=>$value['payNo'],"a.status"=>[1,2]])->order('a.id desc')->column('a.id,b.tag_name,a.tag_img,a
  229. .tag_remark','b.type');
  230. if($value['inv_tag']==1){
  231. $value['inv_tag_name']=$tag[2]['tag_name']??'';
  232. $value['inv_tag_id']=$tag[2]['id']??'0';
  233. $value['inv_tag_img']=$tag[2]['tag_img']??'';
  234. $value['inv_tag_remark']=$tag[2]['tag_remark']??'';
  235. }
  236. if($value['pay_tag']==1){
  237. $value['pay_tag_id']=$tag[1]['id']??'0';
  238. $value['pay_tag_name']=$tag[1]['tag_name']??'';
  239. $value['pay_tag_img']=$tag[1]['tag_img']??'';
  240. $value['pay_tag_remark']=$tag[1]['tag_remark']??'';
  241. }
  242. }
  243. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  244. $data[]=$value;
  245. }
  246. return app_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  247. }
  248. /**
  249. * 新建对账付款数据
  250. * @return \think\Response
  251. */
  252. public function PayAdd()
  253. {
  254. $post = $this->post;
  255. $cids = isset($post['cids'])&&$post['cids']!="" ? trim($post['cids']) :"";
  256. if($cids==""){
  257. return error_show(1003,"参数cids不能为空");
  258. }
  259. $cgdall =Db::name("cgd_info")->where(["id"=>explode(",",$cids)])->select()->toArray();
  260. if(empty($cgdall)){
  261. return error_show(1003,"采购单数据不能为空");
  262. }
  263. $statusAll = array_column($cgdall,"status");
  264. $isComonAll = array_unique(array_column($cgdall,'is_comon')) ;
  265. if(count($isComonAll)>1){
  266. return error_show(1003,"采购单业务类型不一致");
  267. }
  268. if(in_array(1,$statusAll)){
  269. return error_show(1003,'采购单存在已对账数据');
  270. }
  271. if(in_array(2,$statusAll)){
  272. return error_show(1003,"采购单存在不核算数据");
  273. }
  274. $supplierNo= array_column($cgdall,"supplierNo");
  275. $supplierName= array_column($cgdall,"supplierName");
  276. if(count(array_unique($supplierNo))>1){
  277. return error_show(1003,"采购单数据供应商不一致");
  278. }
  279. $companyNo= array_column($cgdall,"companyNo");
  280. $companyName= array_column($cgdall,"companyName");
  281. if(count(array_unique($companyNo))>1){
  282. return error_show(1003,"采购单数据业务公司不一致");
  283. }
  284. if ($this->level == 2) {
  285. //判断是否开通供应商账号
  286. $temp = check_has_account_by_supplierNos([$supplierNo[0]]);
  287. if (isset($temp['data'][$supplierNo[0]])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  288. }
  289. $goodNo= array_column($cgdall,'goodNo');
  290. $good_info = \app\admin\model\Good::where(['spuCode'=>$goodNo])->column("status","spuCode");
  291. $payNo = makeNo("PAY");
  292. Db::startTrans();
  293. try {
  294. $data=[];
  295. $paydata=[
  296. "payNo"=>$payNo,
  297. "apply_id"=>$this->uid,
  298. "apply_name"=>$this->uname,
  299. "total_fee"=>0,
  300. "is_comon"=>$isComonAll[0],
  301. "supplierNo"=>$supplierNo[0],
  302. "supplierName"=>$supplierName[0],
  303. "companyNo"=>$companyNo[0],
  304. "companyName"=>$companyName[0],
  305. "wpay_fee"=>0,
  306. "apay_fee"=>0,
  307. "ainv_fee"=>0,
  308. "winv_fee"=>0,
  309. "remark"=>"",
  310. "status"=>1,
  311. "addtime"=>date("Y-m-d H:i:s"),
  312. "updatetime"=>date("Y-m-d H:i:s"),
  313. ];
  314. foreach ($cgdall as $key=>$value){
  315. if(!isset($good_info[$value['goodNo']])|| $good_info[$value['goodNo']]==0){
  316. Db::rollback();
  317. return error_show(1004,"采购单商品{$value['goodNo']}商品未确认开票类目");
  318. }
  319. $temp=[];
  320. $temp['cgdNo']=$value['sequenceNo'];
  321. $temp['total_fee']=$value['totalPrice'];
  322. $temp['apay_fee']=round($value['apay_fee'],2);
  323. $temp['wpay_fee']=round($value['wpay_fee'],2);
  324. $temp['winv_fee']=round($value['winv_fee'],2);
  325. $temp['ainv_fee']=round($value['ainv_fee'],2);
  326. $temp['payNo']=$payNo;
  327. $temp['addtime']=date("Y-m-d H:i:s");
  328. $temp['updatetime']=date("Y-m-d H:i:s");
  329. $data[]=$temp;
  330. $paydata['total_fee']+= $temp['total_fee'];
  331. $paydata['apay_fee']+= $temp['apay_fee'];
  332. $paydata['wpay_fee']+= $temp['wpay_fee'];
  333. $paydata['ainv_fee']+= $temp['ainv_fee'];
  334. $paydata['winv_fee']+= $temp['winv_fee'];
  335. }
  336. ReportCode::where(['cgdNo'=>array_column($cgdall,"sequenceNo")])->update(['payNo'=>$payNo]);
  337. $paydata['total_fee']= round($paydata['total_fee'],2);
  338. $paydata['apay_fee']= round($paydata['apay_fee'],2);
  339. $paydata['wpay_fee']= round($paydata['wpay_fee'],2);
  340. $paydata['ainv_fee']= round($paydata['ainv_fee'],2);
  341. $paydata['winv_fee']= round($paydata['winv_fee'],2);
  342. $info = Db::name("pay_info")->insertAll($data);
  343. if($info!=0){
  344. $payDA=Db::name("pay")->insert($paydata);
  345. if($payDA){
  346. $cgddup= Db::name("cgd_info")->where("id","in",$cids)->save(['status'=>1,"updatetime"=>date("Y-m-d
  347. H:i:s")]);
  348. if($cgddup){
  349. Db::commit();
  350. return app_show(0,"添加成功",["payNo"=>$payNo]);
  351. }
  352. }
  353. }
  354. Db::rollback();
  355. return error_show(1003,"添加失败");
  356. }catch(\Exception $e){
  357. Db::rollback();
  358. return error_show(1003,$e->getMessage());
  359. }
  360. }
  361. /**对账单未审核前可需改编辑
  362. * @return \think\response\Json|void
  363. * @throws \think\db\exception\DataNotFoundException
  364. * @throws \think\db\exception\DbException
  365. * @throws \think\db\exception\ModelNotFoundException
  366. * @throws \think\exception\DbException
  367. */
  368. public function PaySave()
  369. {
  370. $post = $this->post;
  371. $cids = isset($post['cids'])&&$post['cids']!="" ? trim($post['cids']) :"";
  372. if($cids==""){
  373. return error_show(1003,"参数cids不能为空");
  374. }
  375. $payNo = isset($post['payNo'])&&$post['payNo']!="" ? trim($post['payNo']) :"";
  376. if($payNo==""){
  377. return error_show(1003,"参数 payNo 不能为空");
  378. }
  379. $payinfo = Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  380. if($payinfo==false){
  381. return error_show(1003,"未找到对账单数据");
  382. }
  383. if($payinfo['status']!=1){
  384. return error_show(1003,"对账单已提交审核");
  385. }
  386. $cgdall =Db::name("cgd_info")->where(["id"=>explode(",",$cids)])->select()->toArray();
  387. if(empty($cgdall)){
  388. return error_show(1003,"采购单数据不能为空");
  389. }
  390. $supplierNo= array_column($cgdall,"supplierNo");
  391. $supplierName= array_column($cgdall,"supplierName");
  392. if(count(array_unique($supplierNo))>1){
  393. return error_show(1003,"采购单数据供应商不一致");
  394. }
  395. if ($this->level == 2) {
  396. //判断是否开通供应商账号
  397. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  398. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  399. }
  400. $companyNo= array_column($cgdall,"companyNo");
  401. $companyName= array_column($cgdall,"companyName");
  402. if(count(array_unique($companyNo))>1){
  403. return error_show(1003,"采购单数据业务公司不一致");
  404. }
  405. $cgdArr=array_column($cgdall,"sequenceNo");
  406. $cgdNo = Db::name("pay_info")->where([["payNo","=",$payNo],["is_del","=",0]])->column("cgdNo");
  407. $add=[];
  408. $remove=[];
  409. if(!empty($cgdNo)){
  410. $remove = array_diff($cgdNo,$cgdArr);
  411. $add = array_diff($cgdArr,$cgdNo);
  412. }
  413. Db::startTrans();
  414. try {
  415. $data=[];
  416. $paydata=[
  417. "supplierNo"=>$supplierNo[0],
  418. "supplierName"=>$supplierName[0],
  419. "companyNo"=>$companyNo[0],
  420. "companyName"=>$companyName[0],
  421. "wpay_fee"=>0,
  422. "apay_fee"=>0,
  423. "ainv_fee"=>0,
  424. "winv_fee"=>0,
  425. "total_fee"=>0,
  426. "updatetime"=>date("Y-m-d H:i:s")
  427. ];
  428. foreach ($cgdall as $key=>$value){
  429. if(!empty($add)&& in_array($value['sequenceNo'],$add)){
  430. $temp=[];
  431. $temp['cgdNo']=$value['sequenceNo'];
  432. $temp['total_fee']=$value['totalPrice'];
  433. $temp['apay_fee']=round($value['apay_fee'],2);
  434. $temp['wpay_fee']=round($value['wpay_fee'],2);
  435. $temp['winv_fee']=round($value['winv_fee'],2);
  436. $temp['ainv_fee']=round($value['ainv_fee'],2);
  437. $temp['payNo']=$payNo;
  438. $temp['addtime']=date("Y-m-d H:i:s");
  439. $temp['updatetime']=date("Y-m-d H:i:s");
  440. $data[]=$temp;
  441. $report=ReportCode::where(["cgdNo"=>$value['sequenceNo']])->find();
  442. if($report)$report->setField("payNo",$payNo);
  443. }
  444. $paydata['total_fee']+= $value['totalPrice'];
  445. $paydata['apay_fee']+= $value['apay_fee'];
  446. $paydata['wpay_fee']+= $value['wpay_fee'];
  447. $paydata['ainv_fee']+= $value['ainv_fee'];
  448. $paydata['winv_fee']+= $value['winv_fee'];
  449. }
  450. $paydata['total_fee']= round($paydata['total_fee'],2);
  451. $paydata['apay_fee']= round($paydata['apay_fee'],2);
  452. $paydata['wpay_fee']= round($paydata['wpay_fee'],2);
  453. $paydata['ainv_fee']= round($paydata['ainv_fee'],2);
  454. $paydata['winv_fee']= round($paydata['winv_fee'],2);
  455. $payDA=Db::name("pay")->where($payinfo)->update($paydata);
  456. if($payDA){
  457. if(!empty($remove)){
  458. foreach ($remove as $value){
  459. $report=ReportCode::where(["cgdNo"=>$value])->find();
  460. if($report)$report->rmField("payNo",$payNo);
  461. }
  462. $payrm = Db::name("pay_info")->where(["cgdNo"=>$remove,"is_del"=>0])->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  463. if($payrm==false){
  464. Db::rollback();
  465. return error_show(1004,"修改失败");
  466. }
  467. $cgdrm= Db::name("cgd_info")->where([["sequenceNo","in",$remove],["status","=",1]])->save(['status'=>0, "updatetime"=>date("Y-m-d H:i:s")]);
  468. if($cgdrm==false){
  469. Db::rollback();
  470. return error_show(1005,"修改失败");
  471. }
  472. }
  473. if(!empty($add)){
  474. $cgddup= Db::name("cgd_info")->where([["sequenceNo","in",$add],["status","=",0]])->save(['status'=>1, "updatetime"=>date("Y-m-d H:i:s")]);
  475. if($cgddup==false){
  476. Db::rollback();
  477. return error_show(1006,"修改失败");
  478. }
  479. }
  480. if(!empty($data)){
  481. $info = Db::name("pay_info")->insertAll($data);
  482. if($info==false){
  483. Db::rollback();
  484. return error_show(1003,"修改失败");
  485. }
  486. }
  487. Db::commit();
  488. return app_show(0,"修改成功",["payNo"=>$payNo]);
  489. }
  490. Db::rollback();
  491. return error_show(1007,"添加失败");
  492. }catch(\Exception $e){
  493. Db::rollback();
  494. return error_show(1003,$e->getMessage());
  495. }
  496. }
  497. /**
  498. * 对账付款状态审核 status 0 待提交 1 待买方审核 2 审核成功 3驳回 4 对账单撤销,释放采购单数据
  499. * @param \think\Request $request
  500. * @return \think\Response
  501. */
  502. public function PayStatus()
  503. {
  504. $post = $this->post;
  505. $payNo = isset($post['payNo']) && $post['payNo']!="" ? trim($post['payNo']):"";
  506. if($payNo==""){
  507. return error_show(1004,"参数payNo不能为空");
  508. }
  509. $status= isset($post['status']) && $post['status']!=="" ? trim($post['status']):"";
  510. if($status===""){
  511. return error_show(1004,"参数status不能为空");
  512. }
  513. $data = Db::name("pay")->where([["payNo","=",$payNo],["is_del","=",0]])->find();
  514. if($data==false){
  515. return error_show(1004,"未能找到对应数据");
  516. }
  517. $remark = isset($post['remark']) && $post['remark']!=""? trim($post['remark']):"";
  518. $cgdNo = Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  519. if(empty($cgdNo)){
  520. return error_show(1004,"未能找到对应对账数据");
  521. }
  522. if($status==4){
  523. $stages = Db::name("pay_payment")->where(['status'=>[0,1,2,3,4],"payNo"=>$payNo,"is_del"=>0])
  524. ->findOrEmpty();
  525. if(!empty($stages)) return error_show(1004,"对账单存在未解除的付款申请单");
  526. $hpinv = Db::name("pay_invoice")->where(['status'=>[0,1,2,3,4,9,11,12],"payNo"=>$payNo,"is_del"=>0])->findOrEmpty();
  527. if(!empty($hpinv)) return error_show(1004,"对账单存在未解除的回票申请单");
  528. if($data['inv_tag']==1)$this->error("存在回票标签未解除");
  529. if($data['pay_tag']==1)$this->error("存在付款标签未解除");
  530. if($data['is_comon']==1){
  531. $invoceAr=InvoicePay::where(["payNo"=>$payNo])->column("invNo");
  532. if(!empty($invoceAr)){
  533. $ist= InvoicePool::where([["invNo","in",$invoceAr],["status","in",[0,1,2,3,4]]])->select()
  534. ->isEmpty();
  535. if($ist==false)$this->error('通用订单存在开票申请未解除');
  536. }
  537. }
  538. }
  539. Db::startTrans();
  540. try{
  541. $update=[
  542. "status"=>$status,
  543. "remark"=>$remark,
  544. "updatetime"=>date("Y-m-d H:i:s")
  545. ];
  546. $result = Db::name("pay")->where("payNo","=",$payNo)->update($update);
  547. if($result){
  548. if($status==3 || $status==4){
  549. if($data['pay_type']==1){
  550. CgdInfo::where(['sequenceNo'=>$cgdNo,'status'=>1])->update(['status'=>0,
  551. 'updatetime'=>date('Y-m-d H:i:s')]);
  552. }else{
  553. \app\admin\model\Track::where(["TrackCode"=>$cgdNo,"status"=>1])->update(["status"=>0]);
  554. }
  555. $payinfoup =Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->update(['status'=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  556. if($payinfoup==false){
  557. Db::rollback();
  558. return error_show(1004,"对账驳回失败");
  559. }
  560. foreach ($cgdNo as $value){
  561. $report=ReportCode::where(["cgdNo"=>$value])->find();
  562. if($report)$report->rmField("payNo",$payNo);
  563. }
  564. }
  565. Db::commit();
  566. if(($status==3 || $status==4)&&$data['is_comon']==1){
  567. event("comonOrder",["payNo"=>$payNo,"type"=>"check"]);
  568. }
  569. return app_show(0,"状态更新成功");
  570. }
  571. Db::rollback();
  572. return error_show(1004,"状态更新失败");
  573. }catch (\Exception $e){
  574. Db::rollback();
  575. return error_show(1004,$e->getMessage());
  576. }
  577. }
  578. /**删除未审核完成的对账单
  579. * @return \think\response\Json|void
  580. * @throws \think\db\exception\DataNotFoundException
  581. * @throws \think\db\exception\DbException
  582. * @throws \think\db\exception\ModelNotFoundException
  583. */
  584. public function payDel(){
  585. $post = $this->post;
  586. $payNo = isset($post['payNo']) && $post['payNo']!="" ? trim($post['payNo']):"";
  587. if($payNo==""){
  588. return error_show(1004,"参数payNo不能为空");
  589. }
  590. $data = Db::name("pay")->where([["payNo","=",$payNo],["is_del","=",0]])->find();
  591. if($data==false){
  592. return error_show(1004,"未能找到对应数据");
  593. }
  594. if($data['status']==2){
  595. return error_show(1004,"对账单已审核通过");
  596. }
  597. if ($this->level == 2) {
  598. //判断是否开通供应商账号
  599. $temp = check_has_account_by_supplierNos([$data['supplierNo']]);
  600. if (isset($temp['data'][$data['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  601. }
  602. $cgdNo = Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  603. if(empty($cgdNo)){
  604. return error_show(1004,"未能找到对应对账数据");
  605. }
  606. Db::startTrans();
  607. try{
  608. $payDel = ["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
  609. $pay =Db::name("pay")->where($data)->update($payDel);
  610. if($pay){
  611. $infoup =Db::name("pay_info")->where(["cgdNo"=>$cgdNo,"payNo"=>$payNo,"is_del"=>0])->update($payDel);
  612. if($infoup==false){
  613. Db::rollback();
  614. return error_show(1004,"对账删除失败");
  615. }
  616. if(in_array($data['status'],[0,1,2])){
  617. if($data['pay_type']==1){
  618. CgdInfo::where(['sequenceNo'=>$cgdNo,'status'=>1])->update(['status'=>0,
  619. 'updatetime'=>date('Y-m-d H:i:s')]);
  620. }else{
  621. \app\admin\model\Track::where(['TrackCode'=>$cgdNo,'status'=>1])->update(['status'=>0]);
  622. }
  623. }
  624. foreach ($cgdNo as $value){
  625. $report=ReportCode::where(["cgdNo"=>$value])->find();
  626. if($report)$report->rmField("payNo",$payNo);
  627. }
  628. Db::commit();
  629. return error_show(0,"对账删除成功");
  630. }
  631. Db::rollback();
  632. return error_show(1004,"对账删除失败");
  633. }catch (\Exception $e){
  634. Db::rollback();
  635. return error_show(1004,$e->getMessage());
  636. }
  637. }
  638. /**
  639. *
  640. * @param int $id
  641. * @return \think\Response
  642. */
  643. public function stageAdd()
  644. {
  645. $post = $this->post;
  646. $payNo = isset($post['payNo'])&& $post['payNo']!="" ? trim($post['payNo']) :"";
  647. if($payNo==""){
  648. return error_show(1004,"参数payNo 不能为空");
  649. }
  650. $payinfo =Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  651. if($payinfo==false){
  652. return error_show(1004,"未找到对账信息");
  653. }
  654. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  655. if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  656. if($payinfo['status']!=2){
  657. return error_show(1004,"对账未审核完成");
  658. }
  659. $pay_fee = isset($post['pay_fee'])&& $post['pay_fee']!="" ? floatval($post['pay_fee']) :"";
  660. if($pay_fee==""){
  661. return error_show(1004,"参数 pay_fee 不能为空或零");
  662. }
  663. if($payinfo['wpay_fee']< $pay_fee){
  664. return error_show(1004,"对账单未付金额不足");
  665. }
  666. $dzno=makeNo("DZ");
  667. Db::startTrans();
  668. try {
  669. $ment =[
  670. "payNo"=>$payNo,
  671. "dzNo"=>$dzno,
  672. "apply_id"=>$this->uid,
  673. "apply_name"=>$this->uname,
  674. "pay_fee"=>$pay_fee,
  675. "return_img"=>'',
  676. "status"=>1,
  677. "addtime"=>date("Y-m-d H:i:s"),
  678. "updatetime"=>date("Y-m-d H:i:s")
  679. ];
  680. $payment =Db::name("pay_payment")->insert($ment);
  681. if($payment){
  682. $update=[
  683. "pay_fee"=> round($payinfo['pay_fee']+$pay_fee,2),
  684. "wpay_fee"=> round($payinfo['wpay_fee']-$pay_fee,2),
  685. "updatetime"=>date("Y-m-d H:i:s")
  686. ];
  687. $payup = Db::name("pay")->where($payinfo)->update($update);
  688. if($payup){
  689. foreach ($cgdNo as $value){
  690. $report=ReportCode::where(["cgdNo"=>$value])->find();
  691. if($report)$report->setField("DzNo",$dzno);
  692. }
  693. Db::commit();
  694. return app_show(0,"付款申请添加成功",["dzNo"=>$dzno]);
  695. }
  696. }
  697. Db::rollback();
  698. return error_show(1004,"付款申请添加失败");
  699. }catch (\Exception $e){
  700. Db::rollback();
  701. return error_show(1005,$e->getMessage());
  702. }
  703. }
  704. /**
  705. * 付款申请审核状态
  706. * 付款审核状态 0待发起 1待业务审核 2带财务审核 3待回执 4付款完成 5 付款已退 6 业务驳回 7 财务驳回
  707. * @param int $id
  708. * @return \think\Response
  709. */
  710. public function stageStatus()
  711. {
  712. $post = $this->post;
  713. $dzNo = isset($post['dzNo'])&& $post['dzNo']!="" ? trim($post['dzNo']) :"";
  714. if($dzNo==""){
  715. return error_show(1004,"参数 dzNo 不能为空");
  716. }
  717. $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
  718. if($status==""){
  719. return error_show(1004,"参数status 不能为空");
  720. }
  721. if(!in_array($status,[0,1,2,3,4,5,6,7])){
  722. return error_show(1004,"参数status 无效值");
  723. }
  724. $payment= Db::name("pay_payment")->where([['dzNo',"=",$dzNo],['is_del',"=",0]])->find();
  725. if($payment==false){
  726. return error_show(1005,"未找到付款申请信息");
  727. }
  728. $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
  729. if($payinfo==false){
  730. return error_show(1005,"未找到对账信息");
  731. }
  732. if($payinfo['status']!=2){
  733. return error_show(1005,"对账信息未完成审核");
  734. }
  735. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
  736. if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  737. $remark = isset($post['remark'])? trim($post['remark']) :"";
  738. $image = isset($post['return_image'])? trim($post['return_image']) :"";
  739. $return_time = isset($post['return_time'])? trim($post['return_time']) :date("Y-m-d H:i:s");
  740. if($status==4){
  741. if($image=="")return error_show(1005,"付款回单图片不能为空");
  742. if($return_time=="")return error_show(1005,"付款回单时间不能为空");
  743. if($payinfo['pay_fee']< $payment['pay_fee'])return error_show(1005,"对账付款申请金额有误,请确认对账申请金额");
  744. }
  745. Db::startTrans();
  746. try{
  747. $payment= Db::name('pay_payment')->where([['dzNo','=',$dzNo],['is_del','=',0]])->lock(true)->find();
  748. $mentupdate =["status"=>$status,"remark"=>$remark,"return_img"=>$image,"updatetime"=>date("Y-m-d H:i:s")];
  749. if($status==3) $mentupdate['return_time'] = $return_time;
  750. $payup=Db::name("pay_payment")->where($payment)->update($mentupdate);
  751. if($payup){
  752. if (in_array($status,[4,5,6,7])){
  753. if($payinfo['pay_fee']<$payment['pay_fee']) throw new Exception("对账单付款中金额不足");
  754. if($status==4){ //审核通过 扣减对账付款中的金额 添加到已付金额
  755. $payupdate =[
  756. "apay_fee"=>$payinfo['apay_fee']+$payment['pay_fee'],
  757. "pay_fee"=>$payinfo['pay_fee']-$payment['pay_fee'],
  758. "pay_status" => ($payinfo['pay_fee']-$payment['pay_fee'])==0 && $payinfo['wpay_fee']==0 ?3:2,
  759. "updatetime" => date("Y-m-d H:i:s")
  760. ];
  761. }
  762. if(in_array($status,[5,6,7])){//审核驳回 扣减对账付款中的金额 返回到待付金额中
  763. $payupdate =[
  764. "wpay_fee"=>$payinfo['wpay_fee']+$payment['pay_fee'],
  765. "pay_fee"=>$payinfo['pay_fee']-$payment['pay_fee'],
  766. "pay_status" => $payinfo['apay_fee']==0 ?1:2,
  767. "updatetime" => date("Y-m-d H:i:s")
  768. ];
  769. foreach ($cgdNo as $value){
  770. $report=ReportCode::where(["cgdNo"=>$value])->find();
  771. if($report)$report->rmField("DzNo",$dzNo);
  772. }
  773. }
  774. $pay = Db::name("pay")->where($payinfo)->update($payupdate);
  775. if($pay==false){
  776. Db::rollback();
  777. return error_show(1003,"对账状态修改失败");
  778. }
  779. }
  780. Db::commit();
  781. if($payinfo['is_comon']==1 && $status==4){
  782. event("comonOrder",["payNo"=>$payinfo['payNo'],"pay_fee"=>$payment['pay_fee'],"type"=>"pay"]);
  783. }
  784. return app_show(0,"状态修改成功");
  785. }else{
  786. Db::rollback();
  787. return error_show(1003,"状态修改失败");
  788. }
  789. }catch (\Exception $e){
  790. Db::rollback();
  791. return error_show(1008,$e->getMessage());
  792. }
  793. }
  794. /**对账申请删除
  795. * @return \think\response\Json|void
  796. * @throws \think\db\exception\DataNotFoundException
  797. * @throws \think\db\exception\DbException
  798. * @throws \think\db\exception\ModelNotFoundException
  799. */
  800. public function stageDel(){
  801. $post = $this->post;
  802. $dzNo = isset($post['dzNo'])&& $post['dzNo']!="" ? trim($post['dzNo']) :"";
  803. if($dzNo==""){
  804. return error_show(1004,"参数 dzNo 不能为空");
  805. }
  806. $payment= Db::name("pay_payment")->where([['dzNo',"=",$dzNo],['is_del',"=",0]])->find();
  807. if($payment==false){
  808. return error_show(1005,"未找到付款申请信息");
  809. }
  810. $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
  811. if($payinfo==false){
  812. return error_show(1005,"未找到付款信息");
  813. }
  814. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
  815. if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  816. Db::startTrans();
  817. try{
  818. $paym = Db::name("pay_payment")->where($payment)->update(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  819. if($paym){
  820. if(in_array($payment['status'],[1,2,3,4])){
  821. if(in_array($payment['status'],[1,2,3])){
  822. $payinfo['wpay_fee']+=$payment['pay_fee'];
  823. $payinfo['pay_fee']-=$payment['pay_fee'];
  824. $payinfo['updatetime']=date("Y-m-d H:i:s");
  825. }
  826. if($payment['status']==4){
  827. $payinfo['wpay_fee']+=$payment['pay_fee'];
  828. $payinfo['apay_fee']-=$payment['pay_fee'];
  829. $payinfo['pay_status']=$payinfo['apay_fee']==0? 1 : 2 ;
  830. $payinfo['updatetime']=date("Y-m-d H:i:s");
  831. foreach ($cgdNo as $value){
  832. $report=ReportCode::where(["cgdNo"=>$value])->find();
  833. if($report)$report->rmField("DzNo",$dzNo);
  834. }
  835. }
  836. $pay = Db::name("pay")->save($payinfo);
  837. if($pay==false){
  838. Db::rollback();
  839. return error_show(1003,"对账状态修改失败");
  840. }
  841. }
  842. Db::commit();
  843. return app_show(0,"对账申请删除成功");
  844. }
  845. }catch (\Exception $e){
  846. Db::rollback();
  847. return error_show(1003,$e->getMessage());
  848. }
  849. }
  850. /**
  851. * 采购单列表
  852. * @param \think\Request $request
  853. * @param int $id
  854. * @return \think\Response
  855. */
  856. public function CgdList()
  857. {
  858. $post = $this->post;
  859. $condition = [["cgdSource|cgdType","<>",1],['a.is_del',"=",0]];
  860. $check = checkRole($this->roleid,[76,116]);
  861. if($check){
  862. $condition []=["ownerid","=",$this->uid];
  863. }
  864. $sequenceNo = isset($post['sequenceNo']) && $post['sequenceNo']!='' ? trim($post['sequenceNo']) :"";
  865. if($sequenceNo!=""){
  866. $condition []=["a.sequenceNo","like","%$sequenceNo%"];
  867. }
  868. $Total = isset($post['total']) && $post['total']!='' ? trim($post['total']) :'';
  869. if($Total!=''){
  870. $condition []=['totalPrice',$Total==1?"=":'>',0];
  871. }
  872. $orderCode = isset($post['qrdCode']) && $post['qrdCode']!='' ? trim($post['qrdCode']) :"";
  873. if($orderCode!=""){
  874. $condition []=["qrdCode","like","%$orderCode%"];
  875. }
  876. $status = isset($post['status']) && $post['status']!=='' ? intval($post['status']) :"";
  877. $cgd_status = isset($post['cgd_status']) && $post['cgd_status']!=='' ? intval($post['cgd_status']) :"";
  878. if($cgd_status!==''){
  879. $condition[]=['cgd_status','=',$cgd_status];
  880. }
  881. if($status!==""){
  882. if($status!==''){
  883. if($status==3){
  884. $condition[]=["a.status","<>",2];
  885. }else{
  886. $condition[]=["a.status","=",$status];
  887. }
  888. }
  889. }
  890. $companyNo = isset($post['companyNo']) && $post['companyNo']!='' ? trim($post['companyNo']) :"";
  891. if($companyNo!=""){
  892. $condition []=["companyNo","=",$companyNo];
  893. }
  894. $payNo=isset($post['payNo']) && $post['payNo'] != '' ? trim($post['payNo']) : '';
  895. if ($payNo != '') {
  896. $condition []=[
  897. 'payNo' ,'like' , "%$payNo%"
  898. ];
  899. }
  900. $cxCode = isset($post['cxCode'])&&$post['cxCode']!==''?trim($post['cxCode']):'';
  901. if($cxCode!==''){
  902. $condition[]=["cxCode","=",$cxCode];
  903. }
  904. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  905. if($relaComNo!=""){
  906. $condition[]=["companyNo|supplierNo","=",$relaComNo];
  907. }
  908. $companyName = isset($post['companyName']) && $post['companyName']!='' ? trim($post['companyName']) :"";
  909. if($companyName!=""){
  910. $condition []=["companyName","like","%$companyName%"];
  911. }
  912. $bkCode = isset($post['bkCode']) && $post['bkCode']!='' ? trim($post['bkCode']) :"";
  913. if($bkCode!=""){
  914. $condition []=["bkCode","like","%$bkCode%"];
  915. }
  916. $goodNo = isset($post['goodNo']) && $post['goodNo']!='' ? trim($post['goodNo']) :"";
  917. if($goodNo!=""){
  918. $condition []=["goodNo","like","%$goodNo%"];
  919. }
  920. $goodName = isset($post['goodName']) && $post['goodName']!='' ? trim($post['goodName']) :"";
  921. if($goodName!=""){
  922. $condition []=["goodName","like","%$goodName%"];
  923. }
  924. $supplierName = isset($post['supplierName']) && $post['supplierName']!='' ? trim($post['supplierName']) :"";
  925. if($supplierName!=""){
  926. $condition []=["supplierName","like","%$supplierName%"];
  927. }
  928. $supplierNo = isset($post['supplierNo']) && $post['supplierNo']!='' ? trim($post['supplierNo']) :"";
  929. if($supplierNo!=""){
  930. $condition []=["supplierNo","=",$supplierNo];
  931. }
  932. $start=isset($post['start'])&& $post['start']!=''?trim($post['start']):"";
  933. $end=isset($post['end'])&& $post['end']!=''?trim($post['end']):"";
  934. if($start!='') $condition []=["cgdTime",">=",date("Y-m-d 00:00:00",strtotime($start))];
  935. if($end!='') $condition []=["cgdTime","<=",date("Y-m-d 23:59:59",strtotime($end))];
  936. $cgder= isset($post['cgder']) && $post['cgder']!='' ? trim($post['cgder']) :"";
  937. if($cgder!='') $condition []=["ownerName","like","%$cgder%"];
  938. $cgderid= isset($post['cgderid']) && $post['cgderid']!='' ? trim($post['cgderid']) :"";
  939. if($cgderid!='') $condition []=["ownerid","=",$cgderid];
  940. $cgdtype= isset($post['cgdtype']) && $post['cgdtype']!='' ? intval($post['cgdtype']) :"";
  941. if($cgdtype!='') $condition []=["cgdType","=",$cgdtype];
  942. $cgdsource= isset($post['cgdsource']) && $post['cgdsource']!=='' ? intval($post['cgdsource']) :"";
  943. if($cgdsource!=='') $condition []=["cgdSource","=",$cgdsource];
  944. $sendstatus= isset($post['sendstatus']) && $post['sendstatus']!='' ? intval($post['sendstatus']) :"";
  945. if($sendstatus!='') $condition []=["qrdSend","=",$sendstatus];
  946. if(isset($post['is_comon'])&&$post['is_comon']!=='') $condition []=["is_comon","=",$post['is_comon']];
  947. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']) :0;
  948. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']) :10;
  949. $inv_status = isset($post['inv_status'])&&$post['inv_status']!==''?intval($post['inv_status']):'';
  950. if($inv_status!==''){
  951. if($inv_status==0){
  952. $condition[]=['a.inv_status','<>',3];
  953. }else{
  954. $condition[]=['a.inv_status','=',$inv_status];
  955. }
  956. }
  957. $invIs = isset($post['inv_is'])&&$post['inv_is']!==''?intval($post['inv_is']):'';
  958. if($invIs!==''){
  959. if($invIs==1){
  960. $condition[]=['a.winv_fee','<>',0];
  961. }else{
  962. $condition[]=['a.winv_fee','=',0];
  963. }
  964. }
  965. $payIs = isset($post['pay_is'])&&$post['pay_is']!==''?intval($post['pay_is']):'';
  966. if($payIs!==''){
  967. if($payIs==1){
  968. $condition[]=['a.wpay_fee','<>',0];
  969. }else{
  970. $condition[]=['a.wpay_fee','=',0];
  971. }
  972. }
  973. $pay_status = isset($post['pay_status'])&&$post['pay_status']!==''?intval($post['pay_status']):'';
  974. if($pay_status!==''){
  975. if($pay_status==0){
  976. $condition[]=['a.pay_status','<>',3];
  977. }else{
  978. $condition[]=['a.pay_status','=',$pay_status];
  979. }
  980. }
  981. $list = CgdInfo::alias("a")
  982. ->leftJoin("pay_info b","a.sequenceNo=b.cgdNo and b.is_del=0 and b.status=1")
  983. ->where($condition)
  984. ->order("a.id","desc")
  985. ->field("a.*,b.payNo")
  986. ->paginate (["list_rows"=>$size,"page"=>$page])->each(function(&$item){
  987. $item['orderTax'] = OrderCategory::where(['code' => $item['sequenceNo'], 'spuCode' => $item['goodNo']])
  988. ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
  989. $item['catInfo'] = json_decode($item['catInfo'],true);
  990. });
  991. //校验是否开通了供应商账号
  992. $supplierAr=array_unique(array_column($list->items(),'supplierNo'));
  993. $supp_account = check_has_account_by_supplierNos($supplierAr);
  994. $payArr=Db::name('supplier_info')->whereIn('code',$supplierAr)->column('pay_method','code');
  995. foreach ($list->items() as &$value){
  996. $value["pay_type"]=$payArr[$value['supplierNo']]??"";
  997. $value['goodNum'] =$value['goodNum']- $value['thNum'];
  998. $value['sendNum'] =$value['sendNum']- $value['thNum'];
  999. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  1000. $value['inv_tag_name']='';
  1001. $value['pay_tag_name']='';
  1002. $value['pay_tag_img']='';
  1003. $value['inv_tag_id']='0';
  1004. $value['pay_tag_id']='0';
  1005. $value['pay_tag_remark']='';
  1006. $value['inv_tag_img']='';
  1007. $value['inv_tag_remark']='';
  1008. if($value['pay_tag']==1 || $value['inv_tag']==1){
  1009. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  1010. (['code'=>$value['sequenceNo'],'a.status'=>[5,6]])->order('a.id desc')->column('a.id,b.tag_name,a.tag_img,a
  1011. .tag_remark','b.type');
  1012. if($value['inv_tag']==1){
  1013. $value['inv_tag_name']=$tag[6]['tag_name']??'';
  1014. $value['inv_tag_id']=$tag[6]['id']??'0';
  1015. $value['inv_tag_img']=$tag[6]['tag_img']??'';
  1016. $value['inv_tag_remark']=$tag[6]['tag_remark']??'';
  1017. }
  1018. if($value['pay_tag']==1){
  1019. $value['pay_tag_id']=$tag[5]['id']??'0';
  1020. $value['pay_tag_name']=$tag[5]['tag_name']??'';
  1021. $value['pay_tag_img']=$tag[5]['tag_img']??'';
  1022. $value['pay_tag_remark']=$tag[5]['tag_remark']??'';
  1023. }
  1024. }
  1025. }
  1026. return app_show(0,"获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  1027. }
  1028. public function cgdQuery(){
  1029. $post = $this->request->only(["cgdNoArr"=>[],"qrdNoArr"=>[],"status"=>"","cgd_status"=>"","is_comon"=>"","relaComNo"=>'',"companyNo"=>"","supplierNo"=>""],"post");
  1030. $valid=Validate::rule([
  1031. "cgdNoArr|采购单编号集合"=>"requireWithout:qrdNoArr|array",
  1032. "qrdNoArr|销售单编号集合"=>"requireWithout:cgdNoArr|array",
  1033. "status|采购单对账状态"=>"number|in:0,1,2,3",
  1034. "cgd_status|采购单状态"=>"number|in:0,1,2,3,4",
  1035. "is_comon|是否通用订单"=>"number|in:0,1",
  1036. ]);
  1037. if($valid->check($post)==false) return error_show(1004,$valid->getError());
  1038. if(!empty($post['qrdNoArr'])){
  1039. $cgdNo = Db::name("qrd_info")->where(["sequenceNo"=>$post['qrdNoArr']])->column("cgdNo");
  1040. $post["cgdNoArr"]=array_merge($post["cgdNoArr"],$cgdNo);
  1041. }
  1042. $condition = [["cgdSource|cgdType","<>",1],['is_del',"=",0],["sequenceNo","in",$post["cgdNoArr"]]];
  1043. $companyNo = isset($post['companyNo']) && $post['companyNo']!='' ? trim($post['companyNo']) :"";
  1044. if($companyNo!=""){
  1045. $condition []=["companyNo","=",$companyNo];
  1046. }
  1047. $Total=isset($post['total']) && $post['total'] != '' ? trim($post['total']) : '';
  1048. if ($Total != '') {
  1049. $condition []=[
  1050. 'totalPrice' ,
  1051. $Total == 1 ? '=' : '>' ,
  1052. 0
  1053. ];
  1054. }
  1055. if($post['cgd_status']!==''){
  1056. $condition []=[
  1057. 'cgd_status' ,
  1058. '=' ,
  1059. $post['cgd_status']
  1060. ];
  1061. }
  1062. if(isset($post['is_comon'])&& $post['is_comon']!=='') $condition []=['is_comon','=',$post['is_comon']];
  1063. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1064. if($relaComNo!=""){
  1065. $condition[]=["companyNo|supplierNo","=",$relaComNo];
  1066. }
  1067. $supplierNo = isset($post['supplierNo']) && $post['supplierNo']!='' ? trim($post['supplierNo']) :"";
  1068. if($supplierNo!=""){
  1069. $condition []=["supplierNo","=",$supplierNo];
  1070. }
  1071. $status = isset($post['status']) && $post['status']!=='' ? intval($post['status']) :"";
  1072. if($status!==''){
  1073. if($status==3){
  1074. $condition[]=["status","<>",2];
  1075. }else{
  1076. $condition[]=["status","=",$status];
  1077. }
  1078. }
  1079. $list = Db::name("cgd_info")->where($condition)->order('createdTime desc')->select()->toArray();
  1080. $payArr=Db::name('supplier_info')->whereIn('code',array_unique(array_column($list,'supplierNo')))->column('pay_method','code');
  1081. foreach ($list as &$value){
  1082. $value['pay_type']=$payArr[$value['supplierNo']]??'';
  1083. $value['goodNum'] =$value['goodNum']- $value['thNum'];
  1084. $value['sendNum'] =$value['sendNum']- $value['thNum'];
  1085. $value['inv_tag_name']='';
  1086. $value['pay_tag_name']='';
  1087. $value['pay_tag_img']='';
  1088. $value['inv_tag_id']='0';
  1089. $value['pay_tag_id']='0';
  1090. $value['pay_tag_remark']='';
  1091. $value['inv_tag_img']='';
  1092. $value['inv_tag_remark']='';
  1093. if($value['pay_tag']==1 || $value['inv_tag']==1){
  1094. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  1095. (['code'=>$value['sequenceNo'],'a.status'=>[5,6]])->order('a.id desc')->column('a.id,b.tag_name,a.tag_img,a
  1096. .tag_remark','b.type');
  1097. if($value['inv_tag']==1){
  1098. $value['inv_tag_name']=$tag[6]['tag_name']??'';
  1099. $value['inv_tag_id']=$tag[6]['id']??'0';
  1100. $value['inv_tag_img']=$tag[6]['tag_img']??'';
  1101. $value['inv_tag_remark']=$tag[6]['tag_remark']??'';
  1102. }
  1103. if($value['pay_tag']==1){
  1104. $value['pay_tag_id']=$tag[5]['id']??'0';
  1105. $value['pay_tag_name']=$tag[5]['tag_name']??'';
  1106. $value['pay_tag_img']=$tag[5]['tag_img']??'';
  1107. $value['pay_tag_remark']=$tag[5]['tag_remark']??'';
  1108. }
  1109. }
  1110. }
  1111. return app_show(0,"获取成功",$list);
  1112. }
  1113. //采购单信息
  1114. public function cgdinfo(){
  1115. $post=$this->post;
  1116. $sequenceNo = isset($post['sequenceNo']) && $post['sequenceNo']!='' ? trim($post['sequenceNo']) :"";
  1117. if($sequenceNo==''){
  1118. return error_show(1004,"参数 sequenceNo 不能为空");
  1119. }
  1120. $cgdinfo = Db::name("cgd_info")->where(["sequenceNo"=>$sequenceNo])->findOrEmpty();
  1121. if(empty($cgdinfo)){
  1122. return error_show(1004,"采购单信息未找到");
  1123. }
  1124. $cgdinfo['catInfo'] = json_decode($cgdinfo['catInfo'],true);
  1125. $payinfo =Db::name("pay_info")->alias("a")->leftJoin("pay b","a.payNo=b.payNo")
  1126. ->where(["a.is_del"=>0,"b.is_del"=>0,"a.cgdNo"=>$sequenceNo])->field("b.inv_status,b.pay_status,b.status")
  1127. ->find();
  1128. $cgdinfo['pay_type']=Db::name('supplier_info')->where('code',$cgdinfo['supplierNo'])->value('pay_method','');
  1129. $cgdinfo["cgd_pay_status"]=$payinfo['pay_status']??1;
  1130. $cgdinfo["cgd_inv_status"]=$payinfo['inv_status']??1;
  1131. $cgdinfo["pay_status"]=$payinfo['status']??0;
  1132. $cgdinfo['goodNum'] =$cgdinfo['goodNum']- $cgdinfo['thNum'];
  1133. $cgdinfo['sendNum'] =$cgdinfo['sendNum']- $cgdinfo['thNum'];
  1134. $cgdinfo['child']=[];
  1135. $cgdinfo['orderTax']= OrderCategory::where(['code' => $cgdinfo['sequenceNo'], 'spuCode' => $cgdinfo['goodNo']])
  1136. ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
  1137. $list=(new GoodCombind())->where("spuCode",$cgdinfo['goodNo'])->visible(["child"])->with(["child"])
  1138. ->select();
  1139. if($list->isEmpty()==false){
  1140. $cgdinfo['child']=array_column($list->toArray(),'child');
  1141. foreach ($cgdinfo['child'] as &$value){
  1142. $value['orderTax'] = OrderCategory::where(['code' => $cgdinfo['sequenceNo'], 'spuCode' => $value['spuCode']])
  1143. ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
  1144. }
  1145. }
  1146. $cgdinfo['inv_tag_name']='';
  1147. $cgdinfo['pay_tag_name']='';
  1148. $cgdinfo['pay_tag_img']='';
  1149. $cgdinfo['inv_tag_id']='0';
  1150. $cgdinfo['pay_tag_id']='0';
  1151. $cgdinfo['pay_tag_remark']='';
  1152. $cgdinfo['inv_tag_img']='';
  1153. $cgdinfo['inv_tag_remark']='';
  1154. if($cgdinfo['pay_tag']==1 || $cgdinfo['inv_tag']==1){
  1155. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  1156. (['code'=>$cgdinfo['sequenceNo'],'a.status'=>[5,6]])->order('a.id desc')->column('a.id,b.tag_name,a.tag_img,a
  1157. .tag_remark','b.type');
  1158. if($cgdinfo['inv_tag']==1){
  1159. $cgdinfo['inv_tag_name']=$tag[6]['tag_name']??'';
  1160. $cgdinfo['inv_tag_id']=$tag[6]['id']??'0';
  1161. $cgdinfo['inv_tag_img']=$tag[6]['tag_img']??'';
  1162. $cgdinfo['inv_tag_remark']=$tag[6]['tag_remark']??'';
  1163. }
  1164. if($cgdinfo['pay_tag']==1){
  1165. $cgdinfo['pay_tag_id']=$tag[5]['id']??'0';
  1166. $cgdinfo['pay_tag_name']=$tag[5]['tag_name']??'';
  1167. $cgdinfo['pay_tag_img']=$tag[5]['tag_img']??'';
  1168. $cgdinfo['pay_tag_remark']=$tag[5]['tag_remark']??'';
  1169. }
  1170. }
  1171. return app_show(0,"获取成功",$cgdinfo);
  1172. }
  1173. //todo
  1174. public function PayInfo()
  1175. {
  1176. $post = $this->post;
  1177. $payNo =isset($post['payNo'])&&$post['payNo']!='' ? trim($post['payNo']):"";
  1178. if($payNo==""){
  1179. return error_show(1004,"参数 payNo 不能为空");
  1180. }
  1181. $payinfo =Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  1182. if($payinfo==false){
  1183. return error_show(1004,"对账单数据未找到");
  1184. }
  1185. if($payinfo['pay_type']==1){
  1186. $pay =Db::name("pay_info")->alias("a")->leftJoin("cgd_info b","a.cgdNo=b.sequenceNo")
  1187. ->where(["a.is_del"=>0,"b.is_del"=>0,"a.payNo"=>$payNo])->field("b.*")
  1188. ->select()->toArray();
  1189. foreach ($pay as &$value){
  1190. $value['orderTax'] = OrderCategory::where(["code"=>$value['sequenceNo'],"spuCode"=>$value['goodNo']])
  1191. ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
  1192. $value['catInfo'] = json_decode($value['catInfo'],true);
  1193. $value['goodNum'] =$value['goodNum']- $value['thNum'];
  1194. $value['sendNum'] =$value['sendNum']- $value['thNum'];
  1195. if($payinfo['is_comon']==1){
  1196. $value['qrdCode'] = (new \app\admin\model\ComonOrder())->where(['cgdNo'=>$value['sequenceNo']])->value("orderCode",'');
  1197. }
  1198. }
  1199. }else{
  1200. $pay =Db::name('pay_info')->alias('a')
  1201. ->leftJoin('track b','a.cgdNo=b.TrackCode')
  1202. ->leftJoin('fhd_child c','b.outChildCode=c.outChildCode')
  1203. ->where(['a.is_del'=>0,"a.status"=>1,'a.payNo'=>$payNo])
  1204. ->field('b.*,c.outCode,c.good_name,c.spuCode,c.post_name,c.post_code')
  1205. ->select()->toArray();
  1206. }
  1207. $payinfo['pay_type']=Db::name('supplier_info')->where('code',$payinfo['supplierNo'])->value('pay_method','');
  1208. $payinfo['inv_tag_name']='';
  1209. $payinfo['pay_tag_name']='';
  1210. $payinfo['pay_tag_img']='';
  1211. $payinfo['inv_tag_id']='0';
  1212. $payinfo['pay_tag_id']='0';
  1213. $payinfo['pay_tag_remark']='';
  1214. $payinfo['inv_tag_img']='';
  1215. $payinfo['inv_tag_remark']='';
  1216. if($payinfo['pay_tag']==1 || $payinfo['inv_tag']==1){
  1217. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  1218. (['code'=>$payNo,"a.status"=>[1,2]])->order('id desc')->column('a.id,b.tag_name,a.tag_img,a
  1219. .tag_remark','b.type');
  1220. if($payinfo['inv_tag']==1){
  1221. $payinfo['inv_tag_name']=$tag[2]['tag_name']??'';
  1222. $payinfo['inv_tag_id']=$tag[2]['id']??'0';
  1223. $payinfo['inv_tag_img']=$tag[2]['tag_img']??'';
  1224. $payinfo['inv_tag_remark']=$tag[2]['tag_remark']??'';
  1225. }
  1226. if($payinfo['pay_tag']==1){
  1227. $payinfo['pay_tag_id']=$tag[1]['id']??'0';
  1228. $payinfo['pay_tag_name']=$tag[1]['tag_name']??'';
  1229. $payinfo['pay_tag_img']=$tag[1]['tag_img']??'';
  1230. $payinfo['pay_tag_remark']=$tag[1]['tag_remark']??'';
  1231. }
  1232. }
  1233. $payinfo['cgdlist'] = $pay;
  1234. return app_show(0,"获取成功",$payinfo);
  1235. }
  1236. public function DzInfo()
  1237. {
  1238. $post = $this->post;
  1239. $DzNo =isset($post['DzNo'])&&$post['DzNo']!='' ? trim($post['DzNo']):"";
  1240. if($DzNo==""){
  1241. return error_show(1004,"参数 DzNo 不能为空");
  1242. }
  1243. $payinfo =Db::name("pay_payment")->where(["dzNo"=>$DzNo,"is_del"=>0])->find();
  1244. if($payinfo==false){
  1245. return error_show(1004,"对账单付款申请数据未找到");
  1246. }
  1247. $pay =Db::name("pay")->where(["payNo"=>$payinfo['payNo'],"is_del"=>0])->find();
  1248. if($pay==false){
  1249. return error_show(1004,"对账单数据未找到");
  1250. }
  1251. $payinfo['pay_apply_id'] = $pay['apply_id']??'';
  1252. $payinfo['pay_apply_name'] = $pay['apply_name']??'';
  1253. $payinfo['supplierNo'] = $pay['supplierNo']??'';
  1254. $payinfo['supplierName'] = $pay['supplierName']??'';
  1255. $payinfo['companyNo'] = $pay['companyNo']??'';
  1256. $payinfo['companyName'] = $pay['companyName']??'';
  1257. $payinfo['total_fee'] = $pay['total_fee']??'';
  1258. return app_show(0,"获取成功",$payinfo);
  1259. }
  1260. /**发票新建添加
  1261. * @return \think\response\Json|void
  1262. * @throws \think\db\exception\DataNotFoundException
  1263. * @throws \think\db\exception\DbException
  1264. * @throws \think\db\exception\ModelNotFoundException
  1265. * @throws \think\exception\DbException
  1266. */
  1267. public function invAdd(){
  1268. $post = $this->post;
  1269. $payNo = isset($post['payNo'])&&$post['payNo']!='' ? $post['payNo']:"";
  1270. if($payNo==""){
  1271. return error_show(1004,"参数 payNo 不能为空");
  1272. }
  1273. $payinfo =Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  1274. if($payinfo==false){
  1275. return error_show(1004,"对账单数据未找到");
  1276. }
  1277. if($payinfo['winv_fee']<=0) return error_show(1004,"对账单数据未开票金额为0");
  1278. if ($this->level == 2) {
  1279. //判断是否开通供应商账号
  1280. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  1281. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  1282. }
  1283. // $result=$this->checkSupplier($payinfo['supplierNo']);
  1284. // if($result['code']!=0){
  1285. // return error_show($result['code'],$result['message']);
  1286. // }
  1287. if($payinfo['status']!=2){
  1288. return error_show(1004,"对账单数据未完成审核");
  1289. }
  1290. $invArr=isset($post['invArr']) && !empty($post['invArr']) ? $post['invArr'] :'';
  1291. if($invArr==''|| empty($invArr)){
  1292. return error_show(1004,"参数 invArr 不能为空");
  1293. }
  1294. $daList=[];
  1295. foreach ($invArr as $item){
  1296. if(!isset($item['invType'])||$item['invType']=='') return error_show(1004,"参数 invType 不能为空");
  1297. // if(in_array($item['invoiceType'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic']) && $item['invType']!=1){
  1298. // return error_show(1004,"全电发票暂不支持图片识别");
  1299. // }
  1300. if($item['invType']==1){
  1301. if(!isset($item['invoiceType'])||$item['invoiceType']=='') return error_show(1004,"参数 invoiceType 不能为空");
  1302. if (!in_array($item['invoiceType'],$this->invName)) return error_show(1004,"发票类型有误");
  1303. //全电票没有发票代码
  1304. if(!in_array($item['invoiceType'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1305. if(!isset($item['invCode'])||$item['invCode']=='') return error_show(1004,"参数 invCode 不能为空");
  1306. }
  1307. if(!isset($item['invNumber'])||$item['invNumber']=='') return error_show(1004,"参数 invNumber 不能为空");
  1308. if(!isset($item['invoiceType'])||$item['invoiceType']=='') return error_show(1004,"参数 invoiceType 不能为空");
  1309. if(!isset($item['open_time'])||$item['open_time']=='') return error_show(1004,"参数 open_time 不能为空");
  1310. if(!isset($item['subtotal_amount'])||$item['subtotal_amount']=='') return error_show(1004,"参数 subtotal_amount 不能为空");
  1311. $invamount=0;
  1312. if(in_array($item['invoiceType'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1313. $invamount = $item['subtotal_amount']??0;
  1314. $item['subtotal_amount']=0;
  1315. }
  1316. //普票需要校验码
  1317. if(in_array($item['invoiceType'],['normal','roll',"toll",'electronic'])){
  1318. if(!isset($item['checkNumber'])||$item['checkNumber']=='') return error_show(1004,"参数 checkNumber 不能为空");
  1319. }
  1320. }
  1321. if($item['invType']==2){
  1322. if(!isset($item['inv_img'])||$item['inv_img']=='') return error_show(1004,"参数 inv_img 不能为空");
  1323. if(!isset($item['invName'])||$item['invName']=='') return error_show(1004,"参数 invName 不能为空");
  1324. }
  1325. if($item['invType']==3){
  1326. if(!isset($item['inv_img'])||$item['inv_img']=='') return error_show(1004,"参数 inv_img 不能为空");
  1327. if(!isset($item['invName'])||$item['invName']=='') return error_show(1004,"参数 invName 不能为空");
  1328. }
  1329. $hpNo=makeNo("hp");
  1330. $data=[
  1331. "payNo"=>$payNo,
  1332. "hpNo"=>$hpNo,
  1333. "apply_id"=>$this->uid,
  1334. "apply_name"=>$this->uname,
  1335. "inv_fee"=> 0,//核算金额
  1336. "inv_subtotal_amount"=> $item['subtotal_amount']??0, //票面金额
  1337. "inv_amount"=> $invamount, //票面金额
  1338. "invType"=> $item['invType'],
  1339. "invoiceType"=>$item['invoiceType']??"",
  1340. "inv_img"=>$item['inv_img']??'',
  1341. "invName"=>$item['invName']??'',
  1342. "invoiceNumber"=>$item['invNumber']??'',
  1343. "invoiceCode"=>$item['invCode']??'',
  1344. "status"=>$item['invType']==2 ?0:1,
  1345. "checkNumber"=>$item['checkNumber']??'',
  1346. "open_time"=>isset($item['open_time'])&&$item['open_time']!=''? date("Y-m-d",strtotime($item['open_time'])):'',
  1347. "addtime"=>date("Y-m-d H:i:s"),
  1348. "updatetime"=>date("Y-m-d H:i:s")
  1349. ];
  1350. $daList[]=$data;
  1351. }
  1352. if(empty($daList)) return error_show(1004,"录入数据不能为空");
  1353. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  1354. if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  1355. Db::startTrans();
  1356. try{
  1357. $inadd =Db::name("pay_invoice")->insertAll($daList);
  1358. if ($inadd){
  1359. foreach ($cgdNo as $value){
  1360. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1361. if($report)$report->setField("hpNo",$hpNo);
  1362. }
  1363. Db::commit();
  1364. return app_show(0,"回票新建成功");
  1365. }
  1366. Db::rollback();
  1367. return error_show(1004,"回票申请新建失败");
  1368. }catch (\Exception $e){
  1369. Db::rollback();
  1370. return error_show(1004,$e->getMessage());
  1371. }
  1372. }
  1373. //批量添加发票信息
  1374. public function invAddBatchByImport()
  1375. {
  1376. try {
  1377. $param = $this->request->only(['list', 'companyNo', 'is_comon'], 'post', 'trim');
  1378. $validate = Validate::rule([
  1379. 'list' => 'require|array|max:100',
  1380. 'companyNo|卖出方公司编号' => 'require|max:255|checkCompanyNo:',
  1381. 'is_comon|是否通用对账' => 'require|number|in:0,1',
  1382. ]);
  1383. $validate->extend('checkCompanyNo', function ($val, $rule, $da) {
  1384. $tmp = Db::name('user_role')
  1385. ->field('id,status')
  1386. ->where(['is_del' => 0, 'uid' => $this->uid, 'companyNo' => $val])
  1387. ->findOrEmpty();
  1388. if (empty($tmp)) return '账号尚未绑定当前公司';
  1389. if ($tmp['status'] == 0) return '当前公司已禁用';
  1390. return true;
  1391. });
  1392. if (!$validate->check($param)) throw new Exception($validate->getError());
  1393. $val = Validate::rule([
  1394. 'payNo|对账编号' => 'require|max:255',
  1395. 'invoiceType|发票类型' => 'require|in:' . implode(',', array_values($this->invName)),
  1396. 'invoiceCode|发票代码' => 'max:255|checkInvoiceCode:',
  1397. 'invoiceNumber|发票号码' => 'require|max:255',
  1398. 'inv_subtotal_amount|发票税前金额' => 'require|float|max:9999999.99',
  1399. 'open_time|开票日期' => 'require|date',
  1400. 'checkNumber|校验码' => 'checkNumberCallback:',
  1401. ]);
  1402. $val->extend('checkNumberCallback', function ($val, $rule, $da) {
  1403. if (in_array($da['invoiceType'], ['special', 'normal', 'roll', 'special_electronic', 'electronic', 'toll'])) {
  1404. if (empty($val)) return '电子票时校验码必填';
  1405. }
  1406. return true;
  1407. });
  1408. $val->extend('checkInvoiceCode', function ($val, $rule, $da) {
  1409. if (in_array($da['invoiceType'], ['special', 'normal', 'roll', 'special_electronic', 'electronic', 'toll'])) {
  1410. if (empty($val)) return '发票代码必填';
  1411. }
  1412. return true;
  1413. });
  1414. //所有的payNo(对账编码)
  1415. $payNo = array_column($param['list'], 'payNo');
  1416. $payinfo = Db::name('pay')
  1417. ->where('is_del', 0)
  1418. ->whereIn('payNo', $payNo)
  1419. ->column('id,payNo,supplierNo,supplierName,status,is_comon', 'payNo');
  1420. $Supp=[];
  1421. if ($this->level == 2) {
  1422. //判断是否开通供应商账号
  1423. $suppArr=array_unique(array_column($payinfo,'supplierNo'));
  1424. $temp = check_has_account_by_supplierNos(array_unique(array_column($payinfo,"supplierNo")));
  1425. $Supp = $temp['data']??[];
  1426. }
  1427. //$da_insert 批量新增的数据,$payNo_hpNo对账编码=>回票编码
  1428. $da_insert = $payNo_hpNo = [];
  1429. $date = date('Y-m-d H:i:s');
  1430. $unique=[];
  1431. $diesa=[];
  1432. foreach ($param['list'] as $item) {
  1433. if (!$val->check($item)) throw new Exception($val->getError());
  1434. if(in_array($item['invoiceNumber'],$unique))throw new Exception("{$item['invoiceNumber']} 发票重复提交");
  1435. $unique[]=$item['invoiceNumber'];
  1436. if (!isset($payinfo[$item['payNo']])) throw new Exception('对账单数据未找到');
  1437. if ($payinfo[$item['payNo']]['status'] != 2) throw new Exception('对账单数据未完成审核');
  1438. if($payinfo[$item['payNo']]['is_comon'] != $param['is_comon']){
  1439. $diesa[]=$item['payNo'];
  1440. }
  1441. if ($this->level == 2) {
  1442. if ( isset($Supp[$payinfo[$item['payNo']]['supplierNo']])) throw new Exception("该供应商{$payinfo[$item['payNo']]['supplierName']}已经开通账号,不允许当前账号操作");
  1443. }
  1444. $invamount=0;
  1445. if(in_array($item['invoiceType'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1446. $invamount = $item['inv_subtotal_amount']??0;
  1447. $item['inv_subtotal_amount']=0;
  1448. }
  1449. $hpNo = makeNo('hp');
  1450. $da_insert[] = [
  1451. 'payNo' => $item['payNo'],
  1452. 'hpNo' => $hpNo,
  1453. 'apply_id' => $this->uid,
  1454. 'apply_name' => $this->uname,
  1455. 'inv_fee' => 0,//核算金额
  1456. 'inv_subtotal_amount' => $item['inv_subtotal_amount'] ?? 0, //票面金额
  1457. 'inv_amount' => $invamount, //票面金额
  1458. 'invType' => 1,
  1459. 'invoiceType' => $item['invoiceType'],
  1460. 'inv_img' => '',
  1461. 'invName' => '',
  1462. 'invoiceNumber' => $item['invoiceNumber'],
  1463. 'invoiceCode' => $item['invoiceCode'],
  1464. 'status' => 1,
  1465. 'checkNumber' => $item['checkNumber'],
  1466. 'open_time' => date('Y-m-d', strtotime($item['open_time'])),
  1467. 'addtime' => $date,
  1468. 'updatetime' => $date
  1469. ];
  1470. $payNo_hpNo[$item['payNo']] = $hpNo;
  1471. }
  1472. if(empty($diesa)==false){
  1473. $this->error("对账单不符合对账要求",10004,$diesa);
  1474. }
  1475. $cgdNo = Db::name('pay_info')
  1476. ->where('is_del', 0)
  1477. ->whereIn('payNo', $payNo)
  1478. ->column('cgdNo', 'payNo');
  1479. if (empty($cgdNo)) throw new Exception('未找到对账数据信息');
  1480. Db::startTrans();
  1481. try {
  1482. Db::name('pay_invoice')->insertAll($da_insert);
  1483. foreach ($cgdNo as $payNoTmp => $cgdNoTmp) {
  1484. Db::name('report_code')
  1485. ->where('cgdNo', $cgdNoTmp)
  1486. ->update(['hpNo' => $payNo_hpNo[$payNoTmp]]);
  1487. }
  1488. Db::commit();
  1489. return app_show(0, '回票新建成功');
  1490. } catch (Exception $e) {
  1491. Db::rollback();
  1492. return error_show(1004, '回票新建失败,' . $e->getMessage());
  1493. }
  1494. } catch (Exception $exception) {
  1495. return error_show(1005, $exception->getMessage());
  1496. }
  1497. }
  1498. /**
  1499. * 发票审核状态 //发票验证通过 才会扣减待开票金额 失败的开票申请不用处理
  1500. * // 1待系统验证 2 买方公司审核 3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数 10 回票已退
  1501. * 11 待关联订单 12 订单关联完毕待财务审核 13 订单关联完毕财务驳回
  1502. * @return \think\response\Json|void
  1503. * @throws \think\db\exception\DataNotFoundException
  1504. * @throws \think\db\exception\DbException
  1505. * @throws \think\db\exception\ModelNotFoundException
  1506. * @throws \think\exception\DbException
  1507. */
  1508. public function invStatus(){
  1509. $param = $this->request->param(["hpNo"=>"","status"=>"","remark"=>"","ItemRemark"=>[]],"post","trim");
  1510. $valid = Validate::rule(["hpNo|回票申请编号"=>"require|max:255","status"=>"require|number|in:1,2,3,4,6,7,8,13"]);
  1511. if(!$valid->check($param))$this->error($valid->getError());
  1512. $info = PayInvoice::where(["hpNo"=>$param['hpNo'],"is_del"=>0])->findOrEmpty();
  1513. if ($info->isEmpty())$this->error("未找到回票申请信息");
  1514. $payinfo = Pay::where(['payNo'=>$info['payNo'],"is_del"=>0])->findOrEmpty();
  1515. if ($payinfo->isEmpty())$this->error("未找到对账单信息");
  1516. if($payinfo['status']!=2&&$param['status']==3)$this->error("对账信息未完成审核");
  1517. if($payinfo['inv_fee']< $info['inv_fee']) $this->error("回票金额不能大于对账单回票金额");
  1518. $invup =['remark'=>$param['remark'],'updatetime'=>date('Y-m-d H:i:s')];
  1519. if($param['status']==3) $invup['check_time']=date("Y-m-d H:i:s");
  1520. if(in_array($info['invoiceType'],['normal','electronic','toll','roll']) && $param['status']==3){
  1521. $param['status']=4; //普票无需认证
  1522. }
  1523. $invup['status']=$param['status'];
  1524. Db::startTrans();
  1525. try {
  1526. $save=$info->save($invup);
  1527. if($save){
  1528. if($param['status']==4){
  1529. Pay::addAinv($info['payNo'],$info->inv_fee);
  1530. }
  1531. if($param['status']==3){
  1532. if (!empty($param['ItemRemark'])){
  1533. (new InvoiceOrder)->saveAll($param['ItemRemark']);
  1534. };
  1535. }
  1536. if(in_array($param['status'],[5,6,7,8,13])){
  1537. Pay::subInv($info['payNo'],$info->inv_fee);
  1538. if($param['status']==13||$param['status']==7)\app\admin\model\InvoiceItem::rmInvoice($param['hpNo']);
  1539. event("reportCode",["param"=>["key"=>"hpNo","value"=>$param['hpNo']],"type"=>"sub"]);
  1540. }
  1541. }else throw new \Exception("回票审核失败");
  1542. Db::commit();
  1543. }catch (\Exception $e){
  1544. Db::rollback();
  1545. $this->error($e->getMessage());
  1546. }
  1547. $this->success("回票审核成功");
  1548. }
  1549. // public function invStatus(){
  1550. // $post = $this->post;
  1551. // $hpNo = isset($post['hpNo'])&& $post['hpNo']!="" ? trim($post['hpNo']) :"";
  1552. // if($hpNo==""){
  1553. // return error_show(1004,"参数 hpNo 不能为空");
  1554. // }
  1555. // $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
  1556. // if($status==""){
  1557. // return error_show(1004,"参数status 不能为空");
  1558. // }
  1559. //// if(!in_array($status,[0,1,2,3,4,5,6,7,8,9,10,11,12])){
  1560. //// return error_show(1004,"参数status 无效值");
  1561. //// }
  1562. // if(!in_array($status,[0,1,2,3,4,5,6,7])){
  1563. // return error_show(1004,"参数status 无效值");
  1564. // }
  1565. // $payment= Db::name("pay_invoice")->where([['hpNo',"=",$hpNo],['is_del',"=",0]])->find();
  1566. //
  1567. // if($payment==false){
  1568. // return error_show(1005,"未找到回票申请信息");
  1569. // }
  1570. // $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
  1571. //
  1572. // if($payinfo==false){
  1573. // return error_show(1005,"未找到对账信息");
  1574. // }
  1575. // if($payinfo['status']!=2){
  1576. // return error_show(1005,"对账信息未完成审核");
  1577. // }
  1578. // $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
  1579. // if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  1580. // if($payinfo['inv_fee']< $payment['inv_fee'])return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1581. // $remark = $post['remark']??'';
  1582. // Db::startTrans();
  1583. // try {
  1584. // Db::name('pay_invoice')->where([['hpNo','=',$hpNo],['is_del','=',0]])->lock(true)->find();
  1585. // $invup =["remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  1586. // if($status==3) $invup['check_time']=date("Y-m-d H:i:s");
  1587. // if(in_array($payment['invoiceType'],['normal','electronic','toll','roll']) && $status==3){
  1588. // $status=4; //普票无需认证
  1589. // }
  1590. // $invup['status']=$status;
  1591. // $inv =Db::name("pay_invoice")->where($payment)->update($invup);
  1592. // if($inv){
  1593. // if($status==4){
  1594. // $payupdate =[
  1595. // "ainv_fee"=>$payinfo['ainv_fee']+$payment['inv_fee'],
  1596. // "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
  1597. // "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['winv_fee']==0?3:2,
  1598. // "updatetime" => date("Y-m-d H:i:s")
  1599. // ];
  1600. // $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1601. // if($pay==false){
  1602. // Db::rollback();
  1603. // return error_show(1003,"回票申请状态更新失败");
  1604. // }
  1605. // }
  1606. // if($status==6 || $status==5|| $status==7){
  1607. // $payupdate =[
  1608. // "winv_fee"=>$payinfo['winv_fee']+$payment['inv_fee'],
  1609. // "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
  1610. // "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['ainv_fee']==0?1:2,
  1611. // "updatetime" => date("Y-m-d H:i:s")
  1612. // ];
  1613. // $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1614. // if($pay==false){
  1615. // Db::rollback();
  1616. // return error_show(1003,"回票申请状态更新失败");
  1617. // }
  1618. // foreach ($cgdNo as $value){
  1619. // $report=ReportCode::where(["cgdNo"=>$value])->find();
  1620. // if($report)$report->rmField("hpNo",$hpNo);
  1621. // }
  1622. // }
  1623. // Db::commit();
  1624. // return app_show(0,"回票申请更新成功");
  1625. // }throw new \Exception("回票申请状态更新失败");
  1626. // }catch (\Exception $e){
  1627. // Db::rollback();
  1628. // return error_show(1004,$e->getMessage());
  1629. // }
  1630. //
  1631. // }
  1632. /**发票列表识别
  1633. * @param int $id
  1634. * @return \think\Response
  1635. */
  1636. public function InvList()
  1637. {
  1638. $post = $this->post;
  1639. $condition = [["a.is_del","=",0 ],['a.invType','<>',4 ],["b.is_del","=",0 ],['pay_type','=',$post['pay_type']??1]];
  1640. $invtype = isset($post['invType'])&&$post['invType']!='' ? intval($post['invType']):"";
  1641. $check = checkRole($this->roleid,[82,120]);
  1642. if($check){
  1643. $condition []=["a.apply_id","=",$this->uid];
  1644. }
  1645. if ($invtype!=''){
  1646. $condition[]=["a.invType","=",$invtype];
  1647. }
  1648. $companyNo = isset($post['companyNo'])&&$post['companyNo']!='' ? trim($post['companyNo']):"";
  1649. if($companyNo!=""){
  1650. $condition[]=["b.companyNo","=",$companyNo];
  1651. }
  1652. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1653. if($relaComNo!=""){
  1654. $condition[]=["b.companyNo|b.supplierNo","=",$relaComNo];
  1655. }
  1656. $supplierNo = isset($post['supplierNo'])&&$post['supplierNo']!='' ? trim($post['supplierNo']):"";
  1657. if($supplierNo!=""){
  1658. $condition[]=["b.supplierNo","=",$supplierNo];
  1659. }
  1660. $hpNo = isset($post['hpNo'])&&$post['hpNo']!='' ? trim($post['hpNo']):"";
  1661. if ($hpNo!=''){
  1662. $condition[]=["a.hpNo","like","%$hpNo%"];
  1663. }
  1664. $payNo = isset($post['payNo'])&&$post['payNo']!='' ? trim($post['payNo']):"";
  1665. if ($payNo!=''){
  1666. $condition[]=["a.payNo","like","%$payNo%"];
  1667. }
  1668. $invoiceType = isset($post['invoiceType'])&&$post['invoiceType']!='' ? intval($post['invoiceType']):"";
  1669. if ($invoiceType!=''){
  1670. $condition[]=["a.invoiceType","=",$invoiceType];
  1671. }
  1672. $status = isset($post['status'])&&$post['status']!=="" ? intval($post['status']):"";
  1673. if($status!==""){
  1674. $condition[]=["a.status","=",$status];
  1675. }
  1676. $invNumber = isset($post['invNumber'])&&$post['invNumber']!="" ? trim($post['invNumber']):"";
  1677. if($invNumber!=''){
  1678. $condition[]=["invoiceNumber","=",$invNumber];
  1679. }
  1680. $checkApi = isset($post['checkApi'])&&$post['checkApi']!="" ? intval($post['checkApi']):"";
  1681. if($checkApi!==''){
  1682. $condition[]=["checkApi","=",$checkApi];
  1683. }
  1684. $invCode = isset($post['invCode'])&&$post['invCode']!="" ? trim($post['invCode']):"";
  1685. if($invCode!=''){
  1686. $condition[]=["invoiceCode","=",$invCode];
  1687. }
  1688. $start = isset($post['start']) && $post['start']!="" ? $post['start'] :"";
  1689. if($start!=""){
  1690. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1691. }
  1692. $end = isset($post['end']) && $post['end']!="" ? $post['end'] :"";
  1693. if($end!=""){
  1694. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1695. }
  1696. $open_start = isset($post['open_start']) && $post['open_start']!="" ? $post['open_start'] :"";
  1697. if($open_start!=""){
  1698. $condition[]=["a.open_time",">=",$open_start];
  1699. }
  1700. $open_end = isset($post['open_end']) && $post['open_end']!="" ? $post['open_end'] :"";
  1701. if($open_end!=""){
  1702. $condition[]=["a.open_time","<=",$open_end];
  1703. }
  1704. $apply_id = isset($post['apply_id']) && $post['apply_id']!="" ? $post['apply_id'] :"";
  1705. if($apply_id!=""){
  1706. $condition[]=["a.apply_id","=",$apply_id];
  1707. }
  1708. $apply_name = isset($post['apply_name']) && $post['apply_name']!="" ? trim($post['apply_name']):"";
  1709. if($apply_name!=""){
  1710. $condition[]=["a.apply_name","like","%$apply_name%"];
  1711. }
  1712. if(isset($post['is_comon'])&&$post['is_comon']!=='') $condition []=['is_comon','=',$post['is_comon']];
  1713. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']):1;
  1714. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']):10;
  1715. $count =Db::name("pay_invoice")
  1716. ->alias("a")
  1717. ->leftJoin("pay b","a.payNo=b.payNo")
  1718. ->where($condition)
  1719. ->count();
  1720. $total = ceil($count/$size);
  1721. $page = $page>$total? intval($total):$page;
  1722. $list = Db::name("pay_invoice")
  1723. ->alias("a")
  1724. ->leftJoin("pay b", "a.payNo=b.payNo")
  1725. ->field("a.*,b.supplierNo,b.supplierName,b.companyName,b.is_comon,b.companyNo")
  1726. ->where($condition)
  1727. ->page($page,$size)
  1728. ->order("addtime desc")
  1729. ->select()
  1730. ->toArray();
  1731. //校验是否开通了供应商账号
  1732. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list,'supplierNo')));
  1733. $inv_fee=$inv_subtotal_amount = $total = 0;
  1734. foreach ($list as &$value){
  1735. $invoinfo =InvoiceInfo::where(["hpNo"=>$value['hpNo'],"status"=>1])->find();
  1736. $value['buyer_name'] = $invoinfo['buyer_name']??"";
  1737. $value['buyer_id'] = $invoinfo['buyer_id']??"";
  1738. $value['buyer_address'] = $invoinfo['buyer_address']??"";
  1739. $value['buyer_bank'] = $invoinfo['buyer_bank']??"";
  1740. $value['seller_name'] = $invoinfo['seller_name']??"";
  1741. $value['seller_id'] = $invoinfo['seller_id']??"";
  1742. $value['seller_address'] = $invoinfo['seller_address']??"";
  1743. $value['seller_bank'] = $invoinfo['seller_bank']??"";
  1744. $value['subtotal_amount'] = $invoinfo['subtotal_amount']??"";
  1745. $value['subtotal_tax'] = $invoinfo['subtotal_tax']??"";
  1746. $value['total'] = $invoinfo['total']??"";
  1747. $value['receiver'] = $invoinfo['receiver']??"";
  1748. $value['issuer'] = $invoinfo['issuer']??"";
  1749. $value['reivewer'] = $invoinfo['reivewer']??"";
  1750. $value['remarks'] = $invoinfo['remarks']??"";
  1751. $value['invoiceType_cn'] = $this->invoiceType[$value['invoiceType']]??'';
  1752. $value['invStatus_cn'] = $this->invStatus[$value['invStatus']]??'';
  1753. $value['item_list'] = $invoinfo['item_list']??[];
  1754. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  1755. if($value['status']==4){
  1756. $inv_fee += $value['inv_fee'];
  1757. $inv_subtotal_amount += intval($value['inv_subtotal_amount']??0);
  1758. $total += intval($value['total']??0);
  1759. }
  1760. }
  1761. //结算invlist这个接口当用payNo筛选的时候,返回列表里需要加一项统计筛选后列表的inv_subtotal_amount、total字段的和
  1762. // $inv_fee=$inv_subtotal_amount = $total = 0;
  1763. // if ($payNo != '') {
  1764. // $inv_subtotal_amount = round(array_sum(array_column($list, 'inv_subtotal_amount')), 2);
  1765. // $total = round(array_sum(array_column($list, 'total')), 2);
  1766. // }
  1767. return app_show(0, "获取成功", [
  1768. "count" => $count,
  1769. "list" => $list,
  1770. 'inv_subtotal_amount' => $inv_subtotal_amount,
  1771. 'total' => $total,
  1772. "inv_fee"=>$inv_fee
  1773. ]);
  1774. }
  1775. /**发票删除
  1776. * @return \think\response\Json|void
  1777. * @throws \think\db\exception\DataNotFoundException
  1778. * @throws \think\db\exception\DbException
  1779. * @throws \think\db\exception\ModelNotFoundException
  1780. */
  1781. public function invDel(){
  1782. $post = $this->post;
  1783. $hpNo = isset($post['hpNo'])&&$post['hpNo']!="" ? trim($post['hpNo']) :"";
  1784. if ($hpNo==""){
  1785. return error_show(1004,"参数 hpNo 不能为空");
  1786. }
  1787. $payinv =Db::name("pay_invoice")->where(["hpNo"=>$hpNo,"is_del"=>0])->find();
  1788. if($payinv==false){
  1789. return error_show(1004,"对账单回票申请未找到数据");
  1790. }
  1791. $payinfo= Db::name("pay")->where([['payNo',"=",$payinv['payNo']],['is_del',"=",0]])->find();
  1792. if($payinfo==false){
  1793. return error_show(1005,"未找到对账信息");
  1794. }
  1795. if ($this->level == 2) {
  1796. //判断是否开通供应商账号
  1797. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  1798. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  1799. }
  1800. if($payinfo['status']!=2){
  1801. return error_show(1005,"对账信息未完成审核");
  1802. }
  1803. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payinv['payNo'],"is_del"=>0])->column("cgdNo");
  1804. if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  1805. Db::startTrans();
  1806. try{
  1807. $update=["status"=>8,"updatetime"=>date("Y-m-d H:i:s")]; //删除状态 流程终止
  1808. $invup =Db::name("pay_invoice")->where($payinv)->update($update);
  1809. if($invup){
  1810. if($payinv['status']==4){
  1811. if($payinfo['ainv_fee']< $payinv['inv_fee']){
  1812. Db::rollback();
  1813. return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1814. }
  1815. $payupdate =[
  1816. "ainv_fee"=>$payinfo['ainv_fee']-$payinv['inv_fee'],
  1817. "winv_fee"=>$payinfo['winv_fee']+$payinv['inv_fee'],
  1818. "inv_status" => ($payinfo['ainv_fee']-$payinv['inv_fee'])==0?1:2,
  1819. "updatetime" => date("Y-m-d H:i:s")
  1820. ];
  1821. $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1822. if($pay==false){
  1823. Db::rollback();
  1824. return error_show(1003,"删除失败");
  1825. }
  1826. foreach ($cgdNo as $value){
  1827. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1828. if($report)$report->rmField("hpNo",$hpNo);
  1829. }
  1830. }
  1831. if(in_array($payinv['status'], [1,2,3,11,12])){
  1832. if($payinfo['inv_fee']< $payinv['inv_fee']){
  1833. Db::rollback();
  1834. return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1835. }
  1836. Pay::subInv($payinfo['payNo'],$payinv['inv_fee']);
  1837. if(in_array($payinv['status'], [2,3,12])) \app\admin\model\InvoiceItem::rmInvoice($param['hpNo']);
  1838. foreach ($cgdNo as $value){
  1839. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1840. if($report)$report->rmField("hpNo",$hpNo);
  1841. }
  1842. }
  1843. Db::commit();
  1844. return app_show(0,"删除成功");
  1845. }
  1846. }catch (\Exception $e){
  1847. Db::rollback();
  1848. return error_show(1003,$e->getMessage());
  1849. }
  1850. }
  1851. public function HpInfo(){
  1852. $post = $this->post;
  1853. $hpNo = isset($post['hpNo'])&&$post['hpNo']!="" ? trim($post['hpNo']) :"";
  1854. if ($hpNo==""){
  1855. return error_show(1004,"参数 hpNo 不能为空");
  1856. }
  1857. $payinv =Db::name("pay_invoice")->where(["hpNo"=>$hpNo,"is_del"=>0])->find();
  1858. if($payinv==false){
  1859. return error_show(1004,"对账单回票申请未找到数据");
  1860. }
  1861. $payinfo= Db::name("pay")->where([['payNo',"=",$payinv['payNo']],['is_del',"=",0]])->find();
  1862. if($payinfo==false){
  1863. return error_show(1005,"未找到对账信息");
  1864. }
  1865. $payinv['pay_apply_id'] = $payinfo['apply_id']??'';
  1866. $payinv['pay_apply_name'] = $payinfo['apply_name']??'';
  1867. $payinv['supplierNo'] = $payinfo['supplierNo']??'';
  1868. $payinv['supplierName'] = $payinfo['supplierName']??'';
  1869. $payinv['companyNo'] = $payinfo['companyNo']??'';
  1870. $payinv['companyName'] = $payinfo['companyName']??'';
  1871. $payinv['total_fee'] = $payinfo['total_fee']??'';
  1872. $invinfo =InvoiceInfo::where(["hpNo"=>$hpNo])->findOrEmpty();
  1873. $payinv['invoiceType_cn'] = $this->invoiceType[$payinv['invoiceType']]??'';
  1874. $payinv['invStatus_cn'] = $this->invStatus[$payinv['invStatus']]??'';
  1875. $payinv["info"]=$invinfo;
  1876. return app_show(0,'获取成功',$payinv);
  1877. }
  1878. /**
  1879. * @return \think\response\Json|void
  1880. * @throws \think\db\exception\DbException
  1881. */
  1882. public function UnPay(){
  1883. $post = $this->post;
  1884. $codeNo = isset($post['id'])&&$post['id']!=="" ? trim($post['id']) :'';
  1885. if($codeNo===""){
  1886. return error_show(1004,'参数 id 不能为空');
  1887. }
  1888. $update = Db::name('cgd_info')->where("id","in",$codeNo)->update(['status'=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  1889. if($update){
  1890. return app_show(0,'更新成功');
  1891. }else{
  1892. return error_show(1004,'更新失败');
  1893. }
  1894. }
  1895. /**
  1896. * @return \think\|\think\response\Json|void
  1897. */
  1898. public function ticketInfo(){
  1899. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  1900. if($num==''){
  1901. return error_show(1004,'参数 number 不能为空');
  1902. }
  1903. $condition=[];
  1904. $condition[]=["number","=",$num];
  1905. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1906. if($invNo!=='')$condition[]=["hpNo","=",$invNo];
  1907. $info =InvoiceInfo::where($condition)->findOrEmpty();
  1908. return empty($info)?error_show(1005,"为找到数据"):app_show(0,"获取成功",$info);
  1909. }
  1910. /**
  1911. * @return \think\response\Json|void
  1912. * @throws \think\db\exception\DataNotFoundException
  1913. * @throws \think\db\exception\DbException
  1914. * @throws \think\db\exception\ModelNotFoundException
  1915. */
  1916. public function hpinvList(){
  1917. $post=$this->request->only(["hpNo"=>"","page"=>1,"size"=>15],"post","trim");
  1918. $where=[];
  1919. if($post["hpNo"]!="") $where[]=["hpNo","=",$post['hpNo']];
  1920. $count=InvoiceInfo::where($where)->count();
  1921. $total =ceil($count/$post['size']);
  1922. $page= $post['page']>$total ? intval($total):intval($post['page']);
  1923. $list = InvoiceInfo::where($where)
  1924. ->page($page,intval($post['size']))
  1925. ->order("id desc")
  1926. ->select()->toArray();
  1927. foreach ($list as &$value){
  1928. $value['type_name'] = $this->invoiceType[$value['type']]??"";
  1929. }
  1930. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1931. }
  1932. public function hpinvReturn(){
  1933. $param =$this->request->only(["hpNo"=>"","reason"=>""],"post","trim");
  1934. $valide=Validate::rule([
  1935. "hpNo|回票编号"=>"require|max:255",
  1936. "reason|申请原因"=>"require|max:255"
  1937. ]);
  1938. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  1939. $hpinfo =Db::name("pay_invoice")->where([["hpNo","=",$param['hpNo']],["is_del","=",0]])->findOrEmpty();
  1940. if(empty($hpinfo)) return error_show(1004,"回票信息未找到");
  1941. $payinfo =Db::name("pay")->where(["payNo"=>$hpinfo['payNo'],"is_del"=>0])->find();
  1942. if($payinfo==false){
  1943. return error_show(1004,"对账单数据未找到");
  1944. }
  1945. if ($this->level == 2) {
  1946. //判断是否开通供应商账号
  1947. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  1948. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  1949. }
  1950. if($hpinfo['status']!=4)return error_show(1004,"回票信息流程未完成");
  1951. $isR=Db::name("pay_return")->where(["orderCode"=>$param['hpNo'],"status"=>[1,2],"is_del"=>0])->findOrEmpty();
  1952. if(!empty($isR))return error_show(1004,"回票信息退票流程已存在");
  1953. $returnCode =makeNo("RP");
  1954. $data = [
  1955. "returnCode"=>$returnCode,
  1956. "returnType"=>1,
  1957. "payNo"=>$hpinfo['payNo'],
  1958. "orderCode"=>$param['hpNo'],
  1959. "reason"=>$param['reason'],
  1960. "returnImg"=>'',
  1961. "remark"=>'',
  1962. "status"=>1,
  1963. "apply_id"=>$this->uid,
  1964. "apply_name"=>$this->uname,
  1965. "is_del"=>0,
  1966. "addtime"=>date("Y-m-d H:i:s"),
  1967. "updatetime"=>date("Y-m-d H:i:s"),
  1968. ];
  1969. $insert=Db::name("pay_return")->insert($data);
  1970. if($insert){
  1971. return app_show(0,"退票申请新建成功",$returnCode);
  1972. }else{
  1973. return error_show(1004,"退票申请新建失败");
  1974. }
  1975. }
  1976. public function hpinvReturnList(){
  1977. $param =$this->request->only(["relaComNo"=>"","companyNo"=>"","supplierNo"=>"","start"=>"","end"=>"","returnCode"=>"",
  1978. "status"=>"","hpNo"=>"","is_comon"=>"","payNo"=>"","pay_type"=>"","page"=>1,"size"=>15],"post","trim");
  1979. $condition=[["a.is_del","=",0],['a.returnType',"=",1],['pay_type','=',$param['pay_type']?:1]];
  1980. $check = checkRole($this->roleid,[132,130]);
  1981. if($check){
  1982. $condition []=["a.apply_id","=",$this->uid];
  1983. }
  1984. if($param['relaComNo']!="") $condition[]=["b.companyNo|b.supplierNo","=",$param['relaComNo']];
  1985. if($param['companyNo']!="") $condition[]=["b.companyNo","=",$param['companyNo']];
  1986. if($param['supplierNo']!="") $condition[]=["b.supplierNo","=",$param['supplierNo']];
  1987. if($param['start']!="") $condition[]=["a.addtime",">=",date("Y-m-d H:i:s",strtotime($param['start']))];
  1988. if($param['end']!="") $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($param['end']))];
  1989. if($param['status']!="") $condition[]=["a.status","=",$param['status']];
  1990. if($param['is_comon']!="") $condition[]=["b.is_comon","=",$param['is_comon']];
  1991. if($param['hpNo']!="") $condition[]=["a.orderCode","like","%{$param['hpNo']}%"];
  1992. if($param['returnCode']!="") $condition[]=["a.returnCode","like","%{$param['returnCode']}%"];
  1993. if($param['payNo']!="") $condition[]=["a.payNo","like","%{$param['payNo']}%"];
  1994. if(isset($param['is_comon'])&&$param['is_comon']!=='') $condition []=['is_comon','=',$param['is_comon']];
  1995. $count = Db::name("pay_return")
  1996. ->alias("a")
  1997. ->leftJoin("pay b", "a.payNo=b.payNo and b.is_del=0")
  1998. ->leftJoin("pay_invoice c", "a.orderCode=c.hpNo")
  1999. ->where($condition)
  2000. ->count();
  2001. $total = ceil($count / $param['size']);
  2002. $page = $param['page'] >= $total ? intval($total) : intval($param['page']);
  2003. $list = Db::name("pay_return")
  2004. ->alias("a")
  2005. ->leftJoin("pay b", "a.payNo=b.payNo and b.is_del=0")
  2006. ->leftJoin("pay_invoice c", "a.orderCode=c.hpNo")
  2007. ->where($condition)
  2008. ->page($page, intval($param['size']))
  2009. ->order('id desc')
  2010. ->field("a.*,b.is_comon,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")
  2011. ->select()
  2012. ->toArray();
  2013. //校验是否开通了供应商账号
  2014. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list, 'supplierNo')));
  2015. foreach ($list as &$value) {
  2016. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  2017. $value['inv_number']= \app\admin\model\InvoiceInfo::where(["hpNo"=>$value['orderCode']])->value("number",'');
  2018. }
  2019. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2020. }
  2021. public function hpinvReturnStatus(){
  2022. $param =$this->request->only(["returnCode"=>"","status"=>"","remark"=>"","returnImg"=>""],"post","trim");
  2023. $valide=Validate::rule([
  2024. "returnCode|退票申请编号"=>"require|max:255",
  2025. "status|状态"=>"require|number|in:1,2,3",
  2026. ]);
  2027. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2028. $returninfo =Db::name("pay_return")->where(["is_del"=>0,"returnCode"=>$param['returnCode']])->findOrEmpty();
  2029. if(empty($returninfo))return error_show(1004,"退票详情未找到");
  2030. Db::startTrans();
  2031. try{
  2032. $update=[
  2033. "status"=>$param['status'],
  2034. "remark"=>$param['remark'],
  2035. "returnImg"=>$param['returnImg'],
  2036. "updatetime"=>date("Y-m-d H:i:s")
  2037. ];
  2038. $up =Db::name("pay_return")->where($returninfo)->update($update);
  2039. if($up==false)throw new Exception("退票申请审核失败");
  2040. if($param['status']==2){
  2041. $invinfo =Db::name('pay_invoice')->where(['hpNo'=>$returninfo['orderCode'],'is_del'=>0])->findOrEmpty();
  2042. if(empty($invinfo))throw new Exception('回票详情未找到');
  2043. $pay =Db::name('pay')->where(['payNo'=>$returninfo['payNo'],'is_del'=>0])->findOrEmpty();
  2044. if(empty($pay))throw new Exception('对账详情未找到');
  2045. $invup =Db::name('pay_invoice')->where($invinfo)->update(['status'=>10,'updatetime'=>date('Y-m-d H:i:s')]);
  2046. if($invup==false)throw new Exception('回票更新失败');
  2047. Pay::subAinv($returninfo['payNo'],$invinfo['inv_fee']);
  2048. \app\admin\model\InvoiceItem::rmInvoice($returninfo['orderCode'],2);
  2049. }
  2050. Db::commit();
  2051. }catch (\Exception $e){
  2052. Db::rollback();
  2053. return error_show(1005,$e->getMessage());
  2054. }
  2055. $this->success("退票申请审核成功");
  2056. }
  2057. public function hpinvReturnInfo(){
  2058. $param = $this->request->only(["returnCode"=>""],"post","trim");
  2059. $valide=Validate::rule([
  2060. "returnCode|退票申请编号"=>"require|max:255",
  2061. ]);
  2062. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2063. $info = Db::name("pay_return")->alias("a")
  2064. ->leftJoin("pay b","a.payNo=b.payNo and b.is_del=0")
  2065. ->leftJoin("pay_invoice c","a.orderCode=c.hpNo")
  2066. ->where(['returnCode'=>$param['returnCode']])
  2067. ->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")
  2068. ->findOrEmpty();
  2069. return app_show(0,"退票申请获取成功",$info);
  2070. }
  2071. //付款退回
  2072. public function stageReturn(){
  2073. $param =$this->request->only(["dzNo"=>"","reason"=>""],"post","trim");
  2074. $valide=Validate::rule([
  2075. "dzNo|付款编号"=>"require|max:255",
  2076. "reason|申请原因"=>"require|max:255"
  2077. ]);
  2078. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2079. $hpinfo =Db::name("pay_payment")->where([["dzNo","=",$param['dzNo']],["is_del","=",0]])->findOrEmpty();
  2080. if(empty($hpinfo)) return error_show(1004,"付款信息未找到");
  2081. $payinfo =Db::name("pay")->where(["payNo"=>$hpinfo['payNo'],"is_del"=>0])->find();
  2082. if($payinfo==false){
  2083. return error_show(1004,"对账单数据未找到");
  2084. }
  2085. if ($this->level == 2 && $payinfo['is_comon']==0) {
  2086. //判断是否开通供应商账号
  2087. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  2088. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  2089. }
  2090. if(!in_array($hpinfo['status'],[3,4]))return error_show(1004,"付款信息流程未完成");
  2091. $isR=Db::name("pay_return")->where(["orderCode"=>$param['dzNo'],"status"=>[1,2],"is_del"=>0])->findOrEmpty();
  2092. if(!empty($isR))return error_show(1004,"付款信息退款流程已存在");
  2093. $returnCode =makeNo("RP");
  2094. $data = [
  2095. "returnCode"=>$returnCode,
  2096. "returnType"=>2,
  2097. "payNo"=>$hpinfo['payNo'],
  2098. "orderCode"=>$param['dzNo'],
  2099. "reason"=>$param['reason'],
  2100. "returnImg"=>'',
  2101. "remark"=>'',
  2102. "status"=>1,
  2103. "apply_id"=>$this->uid,
  2104. "apply_name"=>$this->uname,
  2105. "is_del"=>0,
  2106. "addtime"=>date("Y-m-d H:i:s"),
  2107. "updatetime"=>date("Y-m-d H:i:s"),
  2108. ];
  2109. $insert=Db::name("pay_return")->insert($data);
  2110. if($insert){
  2111. return app_show(0,"退款申请新建成功",$returnCode);
  2112. }else{
  2113. return error_show(1004,"退款申请新建失败");
  2114. }
  2115. }
  2116. public function stageReturnList(){
  2117. $param =$this->request->only(["relaComNo"=>"","companyNo"=>"","supplierNo"=>"","start"=>"","end"=>"","returnCode"=>"","payNo"=>"",
  2118. "status"=>"","dzNo"=>"","pay_type"=>"","is_comon"=>"","page"=>1,"size"=>15],"post","trim");
  2119. $condition=[["a.is_del","=",0],['a.returnType',"=",2],['pay_type','=',$param['pay_type']?:1]];
  2120. $check = checkRole($this->roleid,[131,129]);
  2121. if($check){
  2122. $condition []=["a.apply_id","=",$this->uid];
  2123. }
  2124. if($param['relaComNo']!="") $condition[]=["b.companyNo|b.supplierNo","=",$param['relaComNo']];
  2125. if($param['companyNo']!="") $condition[]=["b.companyNo","=",$param['companyNo']];
  2126. if($param['supplierNo']!="") $condition[]=["b.supplierNo","=",$param['supplierNo']];
  2127. if($param['start']!="") $condition[]=["a.addtime",">=",date("Y-m-d H:i:s",strtotime($param['start']))];
  2128. if($param['end']!="") $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($param['end']))];
  2129. if($param['status']!="") $condition[]=["a.status","=",$param['status']];
  2130. if($param['dzNo']!="") $condition[]=["a.orderCode","like","%{$param['dzNo']}%"];
  2131. if($param['returnCode']!="") $condition[]=["a.returnCode","like","%{$param['returnCode']}%"];
  2132. if($param['payNo']!="") $condition[]=["a.payNo","like","%{$param['payNo']}%"];
  2133. if(isset($param['is_comon'])&&$param['is_comon']!=='') $condition []=['is_comon','=',$param['is_comon']];
  2134. $count = Db::name("pay_return")
  2135. ->alias("a")
  2136. ->leftJoin("pay b", "a.payNo=b.payNo and b.is_del=0")
  2137. ->leftJoin("pay_payment c", "a.orderCode=c.dzNo")
  2138. ->where($condition)
  2139. ->count();
  2140. $total = ceil($count / $param['size']);
  2141. $page = $param['page'] >= $total ? intval($total) : intval($param['page']);
  2142. $list = Db::name("pay_return")
  2143. ->alias("a")
  2144. ->leftJoin("pay b", "a.payNo=b.payNo and b.is_del=0")
  2145. ->leftJoin("pay_payment c", "a.orderCode=c.dzNo")
  2146. ->where($condition)
  2147. ->page($page, intval($param['size']))
  2148. ->order('id desc')
  2149. ->field("a.*,b.supplierNo,b.is_comon,b.supplierName,b.companyNo,b.companyName,b.total_fee,b.apay_fee,b.wpay_fee,b.pay_fee,c.pay_fee pay_pay_fee")
  2150. ->select()
  2151. ->toArray();
  2152. //校验是否开通了供应商账号
  2153. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list, 'supplierNo')));
  2154. foreach ($list as &$value) {
  2155. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  2156. }
  2157. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2158. }
  2159. public function stageReturnStatus(){
  2160. $param =$this->request->only(["returnCode"=>"","status"=>"","remark"=>"","returnImg"=>""],"post","trim");
  2161. $valide=Validate::rule([
  2162. "returnCode|退票申请编号"=>"require|max:255",
  2163. "status|状态"=>"require|number|in:1,2,3",
  2164. ]);
  2165. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2166. $returninfo =Db::name("pay_return")->where(["is_del"=>0,"returnCode"=>$param['returnCode']])->findOrEmpty();
  2167. if(empty($returninfo))return error_show(1004,"退票详情未找到");
  2168. Db::startTrans();
  2169. try{
  2170. if($param['status']==2){
  2171. $invinfo =Db::name("pay_payment")->where(["dzNo"=>$returninfo['orderCode'],"is_del"=>0])->findOrEmpty();
  2172. if(empty($invinfo))throw new Exception("付款详情未找到");
  2173. $pay =Db::name("pay")->where(["payNo"=>$returninfo['payNo'],"is_del"=>0])->findOrEmpty();
  2174. if(empty($pay))throw new Exception("对账详情未找到");
  2175. $invup =Db::name("pay_payment")->where($invinfo)->update(["status"=>5,"updatetime"=>date("Y-m-d H:i:s")]);
  2176. if($invup==false)throw new Exception("付款更新失败");
  2177. if($pay['apay_fee']<$invinfo['pay_fee'])throw new Exception('已付款金额不足');
  2178. $paydata=[
  2179. "apay_fee"=>$pay['apay_fee']-$invinfo['pay_fee'],
  2180. "wpay_fee"=>$pay['wpay_fee']+$invinfo['pay_fee'],
  2181. "pay_status" => $pay['pay_fee']==0 &&($pay['apay_fee']-$invinfo['pay_fee'])==0?1:2,
  2182. "updatetime" => date("Y-m-d H:i:s")
  2183. ];
  2184. $oayup =Db::name("pay")->where($pay)->update($paydata);
  2185. if($oayup==false)throw new Exception("对账更新失败");
  2186. }
  2187. $update=[
  2188. "status"=>$param['status'],
  2189. "remark"=>$param['remark'],
  2190. "returnImg"=>$param['returnImg'],
  2191. "updatetime"=>date("Y-m-d H:i:s")
  2192. ];
  2193. $up =Db::name("pay_return")->where($returninfo)->update($update);
  2194. if($up==false)throw new Exception("退款申请审核失败");
  2195. Db::commit();
  2196. if($param['status']==2 && $pay['is_comon']==1){
  2197. event('comonOrder', ['payNo' => $pay['payNo'], 'pay_fee' => $invinfo['pay_fee'], 'type' => 'repay']);
  2198. }
  2199. return app_show(0,"退款申请审核成功");
  2200. }catch (\Exception $e){
  2201. Db::rollback();
  2202. return error_show(1005,$e->getMessage());
  2203. }
  2204. }
  2205. public function stageReturnInfo(){
  2206. $param = $this->request->only(["returnCode"=>""],"post","trim");
  2207. $valide=Validate::rule([
  2208. "returnCode|退票申请编号"=>"require|max:255",
  2209. ]);
  2210. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2211. $info = Db::name("pay_return")->alias("a")
  2212. ->leftJoin("pay b","a.payNo=b.payNo and b.is_del=0")
  2213. ->leftJoin("pay_payment c","a.orderCode=c.dzNo")
  2214. ->where(['returnCode'=>$param['returnCode']])
  2215. ->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")
  2216. ->findOrEmpty();
  2217. return app_show(0,"退款申请获取成功",$info);
  2218. }
  2219. /**批量上传待认证发票
  2220. * @param hpNo array 回票申请编号集合 必传
  2221. * @param relaComNo string 业务公司编号 企业账户
  2222. * @param companyNo string 业务公司编号 超管账户
  2223. * @return \think\response\Json|void
  2224. */
  2225. //回票申请批量认证
  2226. public function hpBatchImport()
  2227. {
  2228. $post = $this->request->only(["list" => [], "relaComNo" => "", "companyNo" => ""], "post");
  2229. $valid = Validate::rule([
  2230. "list|回票申请编号集合" => "require|array|max:100",
  2231. "relaComNo|关联公司" => "requireWithout:companyNo|max:255",
  2232. "companyNo|公司编号" => "requireWithout:relaComNo|max:255",
  2233. ]);
  2234. if ($valid->check($post) == false) return error_show(1004, $valid->getError());
  2235. $companyNo = $post["relaComNo"] ?: $post['companyNo'];
  2236. $payArr = Db::name("pay_invoice")
  2237. ->alias("a")
  2238. ->leftJoin("pay b", "a.payNo=b.payNo")
  2239. ->where(['a.is_del' => 0, 'b.is_del' => 0])
  2240. ->whereIn('a.hpNo', array_column($post['list'], 'hpNo'))
  2241. ->column("a.id,a.payNo,a.hpNo,a.invoiceType,a.inv_fee as invoice_fee,a.status,b.inv_status,b.inv_fee,b.companyNo,b.ainv_fee,b.winv_fee,a.hpNo", "a.hpNo");
  2242. $val_hpNo = Validate::rule([
  2243. 'hpNo|回票申请编号' => 'require',
  2244. 'status|审核状态' => 'require|number|in:4,7',
  2245. 'remark|审核备注' => 'requireIf:status,7|max:255'
  2246. ]);
  2247. $unique=[];
  2248. foreach ($post['list'] as $item) {
  2249. if (!$val_hpNo->check($item)) return error_show(1004, $val_hpNo->getError());
  2250. if(in_array($item['hpNo'],$unique))return error_show(1004, "{$item['hpNo']} 发票重复提交");
  2251. $unique[]=$item['hpNo'];
  2252. }
  2253. Db::startTrans();
  2254. try {
  2255. $pay_invoice_allow = [];
  2256. foreach ($post['list'] as $val) {
  2257. if (!$val_hpNo->check($val)) throw new \Exception($val_hpNo->getError());
  2258. if (!isset($payArr[$val['hpNo']])) throw new \Exception("{$val['hpNo']}记录不存在");
  2259. $value = $payArr[$val['hpNo']];
  2260. $payinfo = Db::name("pay")->where(["payNo"=>$payArr[$val['hpNo']]['payNo'],"status"=>2,"is_del"=>0])->findOrEmpty();
  2261. if(empty($payinfo)) throw new Exception("{$value['hpNo']}对账信息有误");
  2262. if ($payinfo['companyNo'] == '') throw new Exception("{$value['hpNo']}对账信息有误");
  2263. if ($payinfo['companyNo'] != $companyNo) throw new Exception("{$value['hpNo']}不属于当前业务公司发票");
  2264. if ($value['status'] != '3') throw new Exception("{$value['hpNo']}状态有误");
  2265. if ($value['invoice_fee'] > $payinfo['inv_fee']) throw new Exception("{$value['hpNo']}对账单开票中金额不足");
  2266. if ($val['status'] == 4) {
  2267. Pay::addAinv($payArr[$val['hpNo']]['payNo'],$value['invoice_fee']);
  2268. $pay_invoice_allow[] = $value['id'];
  2269. } else {
  2270. Pay::subInv($payArr[$val['hpNo']]['payNo'],$value['invoice_fee']);
  2271. Db::name("pay_invoice")
  2272. ->where(["id" => $value['id'], "status" => 3, "is_del" => 0])
  2273. ->update(["status" => $val['status'], "updatetime" => date("Y-m-d H:i:s"), 'remark' =>$val['remark']]);
  2274. }
  2275. }
  2276. 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")]);
  2277. // if ($update == false) throw new Exception("回票申请状态更新失败");
  2278. Db::commit();
  2279. return app_show(0, "回票申请认证成功");
  2280. } catch (\Exception $e) {
  2281. Db::rollback();
  2282. return error_show(1004, $e->getMessage());
  2283. }
  2284. }
  2285. /**
  2286. * 导出对账单采购单信息
  2287. */
  2288. public function payCgdExport(){
  2289. $param = $this->request->only(["payNo"=>""],"post","trim");
  2290. $valide=Validate::rule([
  2291. "payNo|对账申请编号"=>"require|max:255",
  2292. ]);
  2293. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2294. $payinfo =Db::name("pay")->where(["payNo"=>$param['payNo'],"is_del"=>0,"pay_type"=>1])->findOrEmpty();
  2295. if(empty($payinfo)) return error_show(1005,'对账单信息未找到');
  2296. $cgdlist =Db::name("pay_info")->alias("a")
  2297. ->leftJoin("cgd_info b","a.cgdNo=b.sequenceNo")
  2298. ->leftJoin("order_category c","a.cgdNo=c.code and order_type=2")
  2299. ->leftJoin("good d","c.spuCode =d.spuCode")
  2300. ->where(["a.payNo"=>$param['payNo'],"a.status"=>1,"a.is_del"=>0])
  2301. ->field("a.payNo'对账单号',b.sequenceNo '采购单编号',b.qrdCode '销售单编号',b.supplierName '供货商',b.companyName '业务公司',
  2302. b.goodUnit '单位',(b.goodNum-b.thNum) '商品数量',b.goodPrice'商品单价',b.totalPrice'总货款',
  2303. if(b.cxCode='',b.qrdCode,b.cxCode) 销售主单号,b.goodNo 商品编码,b.goodName 商品名称,c.cat_name 货物和劳务名称,c.merge_code 商品开票类目,c.short_name 商品和服务分类简称,
  2304. c.tax/100 开票税率,c.inv_good_name 开票商品名称,if(b.open_type =1,'原商品开票',if(b.open_type =2,'子商品开票','')) 开票类型")
  2305. ->select()->toArray();
  2306. if(empty($cgdlist))$cgdlist=["暂无数据"=>''];
  2307. else{
  2308. array_walk($cgdlist, function (&$item) {
  2309. $item['采购单可关联金额']= bcsub($item['总货款'],InvoiceOrder::getInvoiceOrderTotalFee($item['采购单编号']),2);
  2310. });
  2311. }
  2312. excelExport("{$param['payNo']}对账采购单详情",array_keys($cgdlist[0]),$cgdlist);
  2313. }
  2314. /** 导出对账单采购单信息
  2315. */
  2316. public function payTrackExport(){
  2317. $param = $this->request->only(['payNo'=>''],'post','trim');
  2318. $valide=Validate::rule([
  2319. 'payNo|对账申请编号'=>'require|max:255',
  2320. ]);
  2321. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2322. $payinfo =Db::name('pay')->where(['payNo'=>$param['payNo'],'is_del'=>0,'pay_type'=>2])->findOrEmpty();
  2323. if(empty($payinfo)) return error_show(1005,'对账单信息未找到');
  2324. $cgdlist =Pay::alias("e")
  2325. ->leftJoin("pay_info a","a.payNo=e.payNo and a.status=1 and a.is_del=0")
  2326. ->leftJoin("track b","a.cgdNo=b.TrackCode")
  2327. ->leftJoin("fhd_child c","c.outChildCode=b.outChildCode")
  2328. ->where(['e.payNo'=>$param['payNo']])
  2329. ->field("a.payNo 对账单号,
  2330. e.total_fee 对账单总金额,
  2331. e.status 对账单状态,
  2332. e.apply_name 对账单创建人,
  2333. e.addtime 对账单创建时间,
  2334. b.TrackCode 发货运单号,
  2335. b.outChildCode 发货工单号,
  2336. c.outCode 发货单号,
  2337. c.num 发货数量,
  2338. b.orderCode 销售单号,
  2339. b.cgdNo 采购单号,
  2340. c.order_source 订单来源,
  2341. c.order_type 商品类型,
  2342. c.good_name 商品名称,
  2343. b.supplierNo 物流供应商编号,
  2344. b.supplierName 物流供应商名称,
  2345. b.companyNo 业务公司编号,
  2346. b.companyName 业务公司名称,
  2347. b.post_fee 物流费,
  2348. b.apply_name 运费单创建人,
  2349. b.create_time 运费单创建时间")
  2350. ->withAttr("对账单状态",function ($v){
  2351. return Pay::$status[$v]??"";
  2352. })
  2353. ->withAttr('订单来源',function ($v){
  2354. return FhdChild::$order_source[$v]??'';
  2355. })
  2356. ->withAttr('商品类型',function ($v){
  2357. return FhdChild::$order_type[$v]??'';
  2358. })
  2359. ->select()->toArray();
  2360. // $this->success("",$cgdlist);
  2361. if(empty($cgdlist))$cgdlist=['暂无数据'=>''];
  2362. excelExport("{$param['payNo']}对账运费单详情",array_keys($cgdlist[0]),$cgdlist);
  2363. }
  2364. // /**校验供应商下面是否有操作账户
  2365. // * @param $supplierNo 供应商账户
  2366. // * @return array
  2367. // */
  2368. // private function checkSupplier($supplierNo){
  2369. // $companyinfo = UserHandle("/userCompanyBasicList", ["companyNo" =>$supplierNo]);
  2370. // if ($companyinfo['code'] != 0) return ["code"=>$companyinfo['code'],"message"=> $companyinfo['message']];
  2371. // if (!empty($companyinfo['data']) && $companyinfo['data']['count'] > 0) {
  2372. // return ["code"=>1004,"message"=> "供应商公司存在操作账户,请用供应商账户操作"];
  2373. // }
  2374. // else{
  2375. // return ["code"=>0,"message"=> "业务账户可操作"];
  2376. // }
  2377. // }
  2378. }