Payment.php 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470
  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. $list = CgdInfo::alias("a")
  950. ->leftJoin("pay_info b","a.sequenceNo=b.cgdNo and b.is_del=0 and b.status=1")
  951. ->where($condition)
  952. ->order("a.id","desc")
  953. ->field("a.*,b.payNo")
  954. ->paginate (["list_rows"=>$size,"page"=>$page])->each(function(&$item){
  955. $item['orderTax'] = OrderCategory::where(['code' => $item['sequenceNo'], 'spuCode' => $item['goodNo']])
  956. ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
  957. $item['catInfo'] = json_decode($item['catInfo'],true);
  958. });
  959. //校验是否开通了供应商账号
  960. $supplierAr=array_unique(array_column($list->items(),'supplierNo'));
  961. $supp_account = check_has_account_by_supplierNos($supplierAr);
  962. $payArr=Db::name('supplier_info')->whereIn('code',$supplierAr)->column('pay_method','code');
  963. foreach ($list->items() as &$value){
  964. $value["pay_type"]=$payArr[$value['supplierNo']]??"";
  965. $value['goodNum'] =$value['goodNum']- $value['thNum'];
  966. $value['sendNum'] =$value['sendNum']- $value['thNum'];
  967. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  968. $value['inv_tag_name']='';
  969. $value['pay_tag_name']='';
  970. $value['pay_tag_img']='';
  971. $value['inv_tag_id']='0';
  972. $value['pay_tag_id']='0';
  973. $value['pay_tag_remark']='';
  974. $value['inv_tag_img']='';
  975. $value['inv_tag_remark']='';
  976. if($value['pay_tag']==1 || $value['inv_tag']==1){
  977. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  978. (['code'=>$value['sequenceNo'],'a.status'=>[5,6]])->order('a.id desc')->column('a.id,b.tag_name,a.tag_img,a
  979. .tag_remark','b.type');
  980. if($value['inv_tag']==1){
  981. $value['inv_tag_name']=$tag[6]['tag_name']??'';
  982. $value['inv_tag_id']=$tag[6]['id']??'0';
  983. $value['inv_tag_img']=$tag[6]['tag_img']??'';
  984. $value['inv_tag_remark']=$tag[6]['tag_remark']??'';
  985. }
  986. if($value['pay_tag']==1){
  987. $value['pay_tag_id']=$tag[5]['id']??'0';
  988. $value['pay_tag_name']=$tag[5]['tag_name']??'';
  989. $value['pay_tag_img']=$tag[5]['tag_img']??'';
  990. $value['pay_tag_remark']=$tag[5]['tag_remark']??'';
  991. }
  992. }
  993. }
  994. return app_show(0,"获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  995. }
  996. public function cgdQuery(){
  997. $post = $this->request->only(["cgdNoArr"=>[],"qrdNoArr"=>[],"status"=>"","cgd_status"=>"","is_comon"=>"","relaComNo"=>'',"companyNo"=>"","supplierNo"=>""],"post");
  998. $valid=Validate::rule([
  999. "cgdNoArr|采购单编号集合"=>"requireWithout:qrdNoArr|array",
  1000. "qrdNoArr|销售单编号集合"=>"requireWithout:cgdNoArr|array",
  1001. "status|采购单对账状态"=>"number|in:0,1,2,3",
  1002. "cgd_status|采购单状态"=>"number|in:0,1,2,3,4",
  1003. "is_comon|是否通用订单"=>"number|in:0,1",
  1004. ]);
  1005. if($valid->check($post)==false) return error_show(1004,$valid->getError());
  1006. if(!empty($post['qrdNoArr'])){
  1007. $cgdNo = Db::name("qrd_info")->where(["sequenceNo"=>$post['qrdNoArr']])->column("cgdNo");
  1008. $post["cgdNoArr"]=array_merge($post["cgdNoArr"],$cgdNo);
  1009. }
  1010. $condition = [["cgdSource|cgdType","<>",1],['is_del',"=",0],["sequenceNo","in",$post["cgdNoArr"]]];
  1011. $companyNo = isset($post['companyNo']) && $post['companyNo']!='' ? trim($post['companyNo']) :"";
  1012. if($companyNo!=""){
  1013. $condition []=["companyNo","=",$companyNo];
  1014. }
  1015. $Total=isset($post['total']) && $post['total'] != '' ? trim($post['total']) : '';
  1016. if ($Total != '') {
  1017. $condition []=[
  1018. 'totalPrice' ,
  1019. $Total == 1 ? '=' : '>' ,
  1020. 0
  1021. ];
  1022. }
  1023. if($post['cgd_status']!==''){
  1024. $condition []=[
  1025. 'cgd_status' ,
  1026. '=' ,
  1027. $post['cgd_status']
  1028. ];
  1029. }
  1030. if(isset($post['is_comon'])&& $post['is_comon']!=='') $condition []=['is_comon','=',$post['is_comon']];
  1031. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1032. if($relaComNo!=""){
  1033. $condition[]=["companyNo|supplierNo","=",$relaComNo];
  1034. }
  1035. $supplierNo = isset($post['supplierNo']) && $post['supplierNo']!='' ? trim($post['supplierNo']) :"";
  1036. if($supplierNo!=""){
  1037. $condition []=["supplierNo","=",$supplierNo];
  1038. }
  1039. $status = isset($post['status']) && $post['status']!=='' ? intval($post['status']) :"";
  1040. if($status!==''){
  1041. if($status==3){
  1042. $condition[]=["status","<>",2];
  1043. }else{
  1044. $condition[]=["status","=",$status];
  1045. }
  1046. }
  1047. $list = Db::name("cgd_info")->where($condition)->order('createdTime desc')->select()->toArray();
  1048. $payArr=Db::name('supplier_info')->whereIn('code',array_unique(array_column($list,'supplierNo')))->column('pay_method','code');
  1049. foreach ($list as &$value){
  1050. $value['pay_type']=$payArr[$value['supplierNo']]??'';
  1051. $value['goodNum'] =$value['goodNum']- $value['thNum'];
  1052. $value['sendNum'] =$value['sendNum']- $value['thNum'];
  1053. $value['inv_tag_name']='';
  1054. $value['pay_tag_name']='';
  1055. $value['pay_tag_img']='';
  1056. $value['inv_tag_id']='0';
  1057. $value['pay_tag_id']='0';
  1058. $value['pay_tag_remark']='';
  1059. $value['inv_tag_img']='';
  1060. $value['inv_tag_remark']='';
  1061. if($value['pay_tag']==1 || $value['inv_tag']==1){
  1062. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  1063. (['code'=>$value['sequenceNo'],'a.status'=>[5,6]])->order('a.id desc')->column('a.id,b.tag_name,a.tag_img,a
  1064. .tag_remark','b.type');
  1065. if($value['inv_tag']==1){
  1066. $value['inv_tag_name']=$tag[6]['tag_name']??'';
  1067. $value['inv_tag_id']=$tag[6]['id']??'0';
  1068. $value['inv_tag_img']=$tag[6]['tag_img']??'';
  1069. $value['inv_tag_remark']=$tag[6]['tag_remark']??'';
  1070. }
  1071. if($value['pay_tag']==1){
  1072. $value['pay_tag_id']=$tag[5]['id']??'0';
  1073. $value['pay_tag_name']=$tag[5]['tag_name']??'';
  1074. $value['pay_tag_img']=$tag[5]['tag_img']??'';
  1075. $value['pay_tag_remark']=$tag[5]['tag_remark']??'';
  1076. }
  1077. }
  1078. }
  1079. return app_show(0,"获取成功",$list);
  1080. }
  1081. //采购单信息
  1082. public function cgdinfo(){
  1083. $post=$this->post;
  1084. $sequenceNo = isset($post['sequenceNo']) && $post['sequenceNo']!='' ? trim($post['sequenceNo']) :"";
  1085. if($sequenceNo==''){
  1086. return error_show(1004,"参数 sequenceNo 不能为空");
  1087. }
  1088. $cgdinfo = Db::name("cgd_info")->where(["sequenceNo"=>$sequenceNo])->findOrEmpty();
  1089. if(empty($cgdinfo)){
  1090. return error_show(1004,"采购单信息未找到");
  1091. }
  1092. $cgdinfo['catInfo'] = json_decode($cgdinfo['catInfo'],true);
  1093. $payinfo =Db::name("pay_info")->alias("a")->leftJoin("pay b","a.payNo=b.payNo")
  1094. ->where(["a.is_del"=>0,"b.is_del"=>0,"a.cgdNo"=>$sequenceNo])->field("b.inv_status,b.pay_status,b.status")
  1095. ->find();
  1096. $cgdinfo['pay_type']=Db::name('supplier_info')->where('code',$cgdinfo['supplierNo'])->value('pay_method','');
  1097. $cgdinfo["cgd_pay_status"]=$payinfo['pay_status']??1;
  1098. $cgdinfo["cgd_inv_status"]=$payinfo['inv_status']??1;
  1099. $cgdinfo["pay_status"]=$payinfo['status']??0;
  1100. $cgdinfo['goodNum'] =$cgdinfo['goodNum']- $cgdinfo['thNum'];
  1101. $cgdinfo['sendNum'] =$cgdinfo['sendNum']- $cgdinfo['thNum'];
  1102. $cgdinfo['child']=[];
  1103. $cgdinfo['orderTax']= OrderCategory::where(['code' => $cgdinfo['sequenceNo'], 'spuCode' => $cgdinfo['goodNo']])
  1104. ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
  1105. $list=(new GoodCombind())->where("spuCode",$cgdinfo['goodNo'])->visible(["child"])->with(["child"])
  1106. ->select();
  1107. if($list->isEmpty()==false){
  1108. $cgdinfo['child']=array_column($list->toArray(),'child');
  1109. foreach ($cgdinfo['child'] as &$value){
  1110. $value['orderTax'] = OrderCategory::where(['code' => $cgdinfo['sequenceNo'], 'spuCode' => $value['spuCode']])
  1111. ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
  1112. }
  1113. }
  1114. $cgdinfo['inv_tag_name']='';
  1115. $cgdinfo['pay_tag_name']='';
  1116. $cgdinfo['pay_tag_img']='';
  1117. $cgdinfo['inv_tag_id']='0';
  1118. $cgdinfo['pay_tag_id']='0';
  1119. $cgdinfo['pay_tag_remark']='';
  1120. $cgdinfo['inv_tag_img']='';
  1121. $cgdinfo['inv_tag_remark']='';
  1122. if($cgdinfo['pay_tag']==1 || $cgdinfo['inv_tag']==1){
  1123. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  1124. (['code'=>$cgdinfo['sequenceNo'],'a.status'=>[5,6]])->order('a.id desc')->column('a.id,b.tag_name,a.tag_img,a
  1125. .tag_remark','b.type');
  1126. if($cgdinfo['inv_tag']==1){
  1127. $cgdinfo['inv_tag_name']=$tag[6]['tag_name']??'';
  1128. $cgdinfo['inv_tag_id']=$tag[6]['id']??'0';
  1129. $cgdinfo['inv_tag_img']=$tag[6]['tag_img']??'';
  1130. $cgdinfo['inv_tag_remark']=$tag[6]['tag_remark']??'';
  1131. }
  1132. if($cgdinfo['pay_tag']==1){
  1133. $cgdinfo['pay_tag_id']=$tag[5]['id']??'0';
  1134. $cgdinfo['pay_tag_name']=$tag[5]['tag_name']??'';
  1135. $cgdinfo['pay_tag_img']=$tag[5]['tag_img']??'';
  1136. $cgdinfo['pay_tag_remark']=$tag[5]['tag_remark']??'';
  1137. }
  1138. }
  1139. return app_show(0,"获取成功",$cgdinfo);
  1140. }
  1141. //todo
  1142. public function PayInfo()
  1143. {
  1144. $post = $this->post;
  1145. $payNo =isset($post['payNo'])&&$post['payNo']!='' ? trim($post['payNo']):"";
  1146. if($payNo==""){
  1147. return error_show(1004,"参数 payNo 不能为空");
  1148. }
  1149. $payinfo =Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  1150. if($payinfo==false){
  1151. return error_show(1004,"对账单数据未找到");
  1152. }
  1153. if($payinfo['pay_type']==1){
  1154. $pay =Db::name("pay_info")->alias("a")->leftJoin("cgd_info b","a.cgdNo=b.sequenceNo")
  1155. ->where(["a.is_del"=>0,"b.is_del"=>0,"a.payNo"=>$payNo])->field("b.*")
  1156. ->select()->toArray();
  1157. foreach ($pay as &$value){
  1158. $value['orderTax'] = OrderCategory::where(["code"=>$value['sequenceNo'],"spuCode"=>$value['goodNo']])
  1159. ->field(['cat_code', 'cat_name', 'merge_code', 'short_name', 'tax', 'inv_good_name'])->findOrEmpty();
  1160. $value['catInfo'] = json_decode($value['catInfo'],true);
  1161. $value['goodNum'] =$value['goodNum']- $value['thNum'];
  1162. $value['sendNum'] =$value['sendNum']- $value['thNum'];
  1163. if($payinfo['is_comon']==1){
  1164. $value['qrdCode'] = (new \app\admin\model\ComonOrder())->where(['cgdNo'=>$value['sequenceNo']])->value("orderCode",'');
  1165. }
  1166. }
  1167. }else{
  1168. $pay =Db::name('pay_info')->alias('a')
  1169. ->leftJoin('track b','a.cgdNo=b.TrackCode')
  1170. ->leftJoin('fhd_child c','b.outChildCode=c.outChildCode')
  1171. ->where(['a.is_del'=>0,"a.status"=>1,'a.payNo'=>$payNo])
  1172. ->field('b.*,c.outCode,c.good_name,c.spuCode,c.post_name,c.post_code')
  1173. ->select()->toArray();
  1174. }
  1175. $payinfo['pay_type']=Db::name('supplier_info')->where('code',$payinfo['supplierNo'])->value('pay_method','');
  1176. $payinfo['inv_tag_name']='';
  1177. $payinfo['pay_tag_name']='';
  1178. $payinfo['pay_tag_img']='';
  1179. $payinfo['inv_tag_id']='0';
  1180. $payinfo['pay_tag_id']='0';
  1181. $payinfo['pay_tag_remark']='';
  1182. $payinfo['inv_tag_img']='';
  1183. $payinfo['inv_tag_remark']='';
  1184. if($payinfo['pay_tag']==1 || $payinfo['inv_tag']==1){
  1185. $tag =Db::name('tag_log')->alias('a')->leftJoin('order_tag b','a.tag_id=b.id')->where
  1186. (['code'=>$payNo,"a.status"=>[1,2]])->order('id desc')->column('a.id,b.tag_name,a.tag_img,a
  1187. .tag_remark','b.type');
  1188. if($payinfo['inv_tag']==1){
  1189. $payinfo['inv_tag_name']=$tag[2]['tag_name']??'';
  1190. $payinfo['inv_tag_id']=$tag[2]['id']??'0';
  1191. $payinfo['inv_tag_img']=$tag[2]['tag_img']??'';
  1192. $payinfo['inv_tag_remark']=$tag[2]['tag_remark']??'';
  1193. }
  1194. if($payinfo['pay_tag']==1){
  1195. $payinfo['pay_tag_id']=$tag[1]['id']??'0';
  1196. $payinfo['pay_tag_name']=$tag[1]['tag_name']??'';
  1197. $payinfo['pay_tag_img']=$tag[1]['tag_img']??'';
  1198. $payinfo['pay_tag_remark']=$tag[1]['tag_remark']??'';
  1199. }
  1200. }
  1201. $payinfo['cgdlist'] = $pay;
  1202. return app_show(0,"获取成功",$payinfo);
  1203. }
  1204. public function DzInfo()
  1205. {
  1206. $post = $this->post;
  1207. $DzNo =isset($post['DzNo'])&&$post['DzNo']!='' ? trim($post['DzNo']):"";
  1208. if($DzNo==""){
  1209. return error_show(1004,"参数 DzNo 不能为空");
  1210. }
  1211. $payinfo =Db::name("pay_payment")->where(["dzNo"=>$DzNo,"is_del"=>0])->find();
  1212. if($payinfo==false){
  1213. return error_show(1004,"对账单付款申请数据未找到");
  1214. }
  1215. $pay =Db::name("pay")->where(["payNo"=>$payinfo['payNo'],"is_del"=>0])->find();
  1216. if($pay==false){
  1217. return error_show(1004,"对账单数据未找到");
  1218. }
  1219. $payinfo['pay_apply_id'] = $pay['apply_id']??'';
  1220. $payinfo['pay_apply_name'] = $pay['apply_name']??'';
  1221. $payinfo['supplierNo'] = $pay['supplierNo']??'';
  1222. $payinfo['supplierName'] = $pay['supplierName']??'';
  1223. $payinfo['companyNo'] = $pay['companyNo']??'';
  1224. $payinfo['companyName'] = $pay['companyName']??'';
  1225. $payinfo['total_fee'] = $pay['total_fee']??'';
  1226. return app_show(0,"获取成功",$payinfo);
  1227. }
  1228. /**发票新建添加
  1229. * @return \think\response\Json|void
  1230. * @throws \think\db\exception\DataNotFoundException
  1231. * @throws \think\db\exception\DbException
  1232. * @throws \think\db\exception\ModelNotFoundException
  1233. * @throws \think\exception\DbException
  1234. */
  1235. public function invAdd(){
  1236. $post = $this->post;
  1237. $payNo = isset($post['payNo'])&&$post['payNo']!='' ? $post['payNo']:"";
  1238. if($payNo==""){
  1239. return error_show(1004,"参数 payNo 不能为空");
  1240. }
  1241. $payinfo =Db::name("pay")->where(["payNo"=>$payNo,"is_del"=>0])->find();
  1242. if($payinfo==false){
  1243. return error_show(1004,"对账单数据未找到");
  1244. }
  1245. if($payinfo['winv_fee']<=0) return error_show(1004,"对账单数据未开票金额为0");
  1246. if ($this->level == 2) {
  1247. //判断是否开通供应商账号
  1248. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  1249. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  1250. }
  1251. // $result=$this->checkSupplier($payinfo['supplierNo']);
  1252. // if($result['code']!=0){
  1253. // return error_show($result['code'],$result['message']);
  1254. // }
  1255. if($payinfo['status']!=2){
  1256. return error_show(1004,"对账单数据未完成审核");
  1257. }
  1258. $invArr=isset($post['invArr']) && !empty($post['invArr']) ? $post['invArr'] :'';
  1259. if($invArr==''|| empty($invArr)){
  1260. return error_show(1004,"参数 invArr 不能为空");
  1261. }
  1262. $daList=[];
  1263. foreach ($invArr as $item){
  1264. if(!isset($item['invType'])||$item['invType']=='') return error_show(1004,"参数 invType 不能为空");
  1265. // if(in_array($item['invoiceType'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic']) && $item['invType']!=1){
  1266. // return error_show(1004,"全电发票暂不支持图片识别");
  1267. // }
  1268. if($item['invType']==1){
  1269. if(!isset($item['invoiceType'])||$item['invoiceType']=='') return error_show(1004,"参数 invoiceType 不能为空");
  1270. if (!in_array($item['invoiceType'],$this->invName)) return error_show(1004,"发票类型有误");
  1271. //全电票没有发票代码
  1272. if(!in_array($item['invoiceType'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1273. if(!isset($item['invCode'])||$item['invCode']=='') return error_show(1004,"参数 invCode 不能为空");
  1274. }
  1275. if(!isset($item['invNumber'])||$item['invNumber']=='') return error_show(1004,"参数 invNumber 不能为空");
  1276. if(!isset($item['invoiceType'])||$item['invoiceType']=='') return error_show(1004,"参数 invoiceType 不能为空");
  1277. if(!isset($item['open_time'])||$item['open_time']=='') return error_show(1004,"参数 open_time 不能为空");
  1278. if(!isset($item['subtotal_amount'])||$item['subtotal_amount']=='') return error_show(1004,"参数 subtotal_amount 不能为空");
  1279. $invamount=0;
  1280. if(in_array($item['invoiceType'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1281. $invamount = $item['subtotal_amount']??0;
  1282. $item['subtotal_amount']=0;
  1283. }
  1284. //普票需要校验码
  1285. if(in_array($item['invoiceType'],['normal','roll',"toll",'electronic'])){
  1286. if(!isset($item['checkNumber'])||$item['checkNumber']=='') return error_show(1004,"参数 checkNumber 不能为空");
  1287. }
  1288. }
  1289. if($item['invType']==2){
  1290. if(!isset($item['inv_img'])||$item['inv_img']=='') return error_show(1004,"参数 inv_img 不能为空");
  1291. if(!isset($item['invName'])||$item['invName']=='') return error_show(1004,"参数 invName 不能为空");
  1292. }
  1293. if($item['invType']==3){
  1294. if(!isset($item['inv_img'])||$item['inv_img']=='') return error_show(1004,"参数 inv_img 不能为空");
  1295. if(!isset($item['invName'])||$item['invName']=='') return error_show(1004,"参数 invName 不能为空");
  1296. }
  1297. $hpNo=makeNo("hp");
  1298. $data=[
  1299. "payNo"=>$payNo,
  1300. "hpNo"=>$hpNo,
  1301. "apply_id"=>$this->uid,
  1302. "apply_name"=>$this->uname,
  1303. "inv_fee"=> 0,//核算金额
  1304. "inv_subtotal_amount"=> $item['subtotal_amount']??0, //票面金额
  1305. "inv_amount"=> $invamount, //票面金额
  1306. "invType"=> $item['invType'],
  1307. "invoiceType"=>$item['invoiceType']??"",
  1308. "inv_img"=>$item['inv_img']??'',
  1309. "invName"=>$item['invName']??'',
  1310. "invoiceNumber"=>$item['invNumber']??'',
  1311. "invoiceCode"=>$item['invCode']??'',
  1312. "status"=>$item['invType']==2 ?0:1,
  1313. "checkNumber"=>$item['checkNumber']??'',
  1314. "open_time"=>isset($item['open_time'])&&$item['open_time']!=''? date("Y-m-d",strtotime($item['open_time'])):'',
  1315. "addtime"=>date("Y-m-d H:i:s"),
  1316. "updatetime"=>date("Y-m-d H:i:s")
  1317. ];
  1318. $daList[]=$data;
  1319. }
  1320. if(empty($daList)) return error_show(1004,"录入数据不能为空");
  1321. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payNo,"is_del"=>0])->column("cgdNo");
  1322. if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  1323. Db::startTrans();
  1324. try{
  1325. $inadd =Db::name("pay_invoice")->insertAll($daList);
  1326. if ($inadd){
  1327. foreach ($cgdNo as $value){
  1328. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1329. if($report)$report->setField("hpNo",$hpNo);
  1330. }
  1331. Db::commit();
  1332. return app_show(0,"回票新建成功");
  1333. }
  1334. Db::rollback();
  1335. return error_show(1004,"回票申请新建失败");
  1336. }catch (\Exception $e){
  1337. Db::rollback();
  1338. return error_show(1004,$e->getMessage());
  1339. }
  1340. }
  1341. //批量添加发票信息
  1342. public function invAddBatchByImport()
  1343. {
  1344. try {
  1345. $param = $this->request->only(['list', 'companyNo', 'is_comon'], 'post', 'trim');
  1346. $validate = Validate::rule([
  1347. 'list' => 'require|array|max:100',
  1348. 'companyNo|卖出方公司编号' => 'require|max:255|checkCompanyNo:',
  1349. 'is_comon|是否通用对账' => 'require|number|in:0,1',
  1350. ]);
  1351. $validate->extend('checkCompanyNo', function ($val, $rule, $da) {
  1352. $tmp = Db::name('user_role')
  1353. ->field('id,status')
  1354. ->where(['is_del' => 0, 'uid' => $this->uid, 'companyNo' => $val])
  1355. ->findOrEmpty();
  1356. if (empty($tmp)) return '账号尚未绑定当前公司';
  1357. if ($tmp['status'] == 0) return '当前公司已禁用';
  1358. return true;
  1359. });
  1360. if (!$validate->check($param)) throw new Exception($validate->getError());
  1361. $val = Validate::rule([
  1362. 'payNo|对账编号' => 'require|max:255',
  1363. 'invoiceType|发票类型' => 'require|in:' . implode(',', array_values($this->invName)),
  1364. 'invoiceCode|发票代码' => 'max:255|checkInvoiceCode:',
  1365. 'invoiceNumber|发票号码' => 'require|max:255',
  1366. 'inv_subtotal_amount|发票税前金额' => 'require|float|max:9999999.99',
  1367. 'open_time|开票日期' => 'require|date',
  1368. 'checkNumber|校验码' => 'checkNumberCallback:',
  1369. ]);
  1370. $val->extend('checkNumberCallback', function ($val, $rule, $da) {
  1371. if (in_array($da['invoiceType'], ['special', 'normal', 'roll', 'special_electronic', 'electronic', 'toll'])) {
  1372. if (empty($val)) return '电子票时校验码必填';
  1373. }
  1374. return true;
  1375. });
  1376. $val->extend('checkInvoiceCode', function ($val, $rule, $da) {
  1377. if (in_array($da['invoiceType'], ['special', 'normal', 'roll', 'special_electronic', 'electronic', 'toll'])) {
  1378. if (empty($val)) return '发票代码必填';
  1379. }
  1380. return true;
  1381. });
  1382. //所有的payNo(对账编码)
  1383. $payNo = array_column($param['list'], 'payNo');
  1384. $payinfo = Db::name('pay')
  1385. ->where('is_del', 0)
  1386. ->whereIn('payNo', $payNo)
  1387. ->column('id,payNo,supplierNo,supplierName,status,is_comon', 'payNo');
  1388. $Supp=[];
  1389. if ($this->level == 2) {
  1390. //判断是否开通供应商账号
  1391. $suppArr=array_unique(array_column($payinfo,'supplierNo'));
  1392. $temp = check_has_account_by_supplierNos(array_unique(array_column($payinfo,"supplierNo")));
  1393. $Supp = $temp['data']??[];
  1394. }
  1395. //$da_insert 批量新增的数据,$payNo_hpNo对账编码=>回票编码
  1396. $da_insert = $payNo_hpNo = [];
  1397. $date = date('Y-m-d H:i:s');
  1398. $unique=[];
  1399. $diesa=[];
  1400. foreach ($param['list'] as $item) {
  1401. if (!$val->check($item)) throw new Exception($val->getError());
  1402. if(in_array($item['invoiceNumber'],$unique))throw new Exception("{$item['invoiceNumber']} 发票重复提交");
  1403. $unique[]=$item['invoiceNumber'];
  1404. if (!isset($payinfo[$item['payNo']])) throw new Exception('对账单数据未找到');
  1405. if ($payinfo[$item['payNo']]['status'] != 2) throw new Exception('对账单数据未完成审核');
  1406. if($payinfo[$item['payNo']]['is_comon'] != $param['is_comon']){
  1407. $diesa[]=$item['payNo'];
  1408. }
  1409. if ($this->level == 2) {
  1410. if ( isset($Supp[$payinfo[$item['payNo']]['supplierNo']])) throw new Exception("该供应商{$payinfo[$item['payNo']]['supplierName']}已经开通账号,不允许当前账号操作");
  1411. }
  1412. $invamount=0;
  1413. if(in_array($item['invoiceType'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1414. $invamount = $item['inv_subtotal_amount']??0;
  1415. $item['inv_subtotal_amount']=0;
  1416. }
  1417. $hpNo = makeNo('hp');
  1418. $da_insert[] = [
  1419. 'payNo' => $item['payNo'],
  1420. 'hpNo' => $hpNo,
  1421. 'apply_id' => $this->uid,
  1422. 'apply_name' => $this->uname,
  1423. 'inv_fee' => 0,//核算金额
  1424. 'inv_subtotal_amount' => $item['inv_subtotal_amount'] ?? 0, //票面金额
  1425. 'inv_amount' => $invamount, //票面金额
  1426. 'invType' => 1,
  1427. 'invoiceType' => $item['invoiceType'],
  1428. 'inv_img' => '',
  1429. 'invName' => '',
  1430. 'invoiceNumber' => $item['invoiceNumber'],
  1431. 'invoiceCode' => $item['invoiceCode'],
  1432. 'status' => 1,
  1433. 'checkNumber' => $item['checkNumber'],
  1434. 'open_time' => date('Y-m-d', strtotime($item['open_time'])),
  1435. 'addtime' => $date,
  1436. 'updatetime' => $date
  1437. ];
  1438. $payNo_hpNo[$item['payNo']] = $hpNo;
  1439. }
  1440. if(empty($diesa)==false){
  1441. $this->error("对账单不符合对账要求",10004,$diesa);
  1442. }
  1443. $cgdNo = Db::name('pay_info')
  1444. ->where('is_del', 0)
  1445. ->whereIn('payNo', $payNo)
  1446. ->column('cgdNo', 'payNo');
  1447. if (empty($cgdNo)) throw new Exception('未找到对账数据信息');
  1448. Db::startTrans();
  1449. try {
  1450. Db::name('pay_invoice')->insertAll($da_insert);
  1451. foreach ($cgdNo as $payNoTmp => $cgdNoTmp) {
  1452. Db::name('report_code')
  1453. ->where('cgdNo', $cgdNoTmp)
  1454. ->update(['hpNo' => $payNo_hpNo[$payNoTmp]]);
  1455. }
  1456. Db::commit();
  1457. return app_show(0, '回票新建成功');
  1458. } catch (Exception $e) {
  1459. Db::rollback();
  1460. return error_show(1004, '回票新建失败,' . $e->getMessage());
  1461. }
  1462. } catch (Exception $exception) {
  1463. return error_show(1005, $exception->getMessage());
  1464. }
  1465. }
  1466. /**
  1467. * 发票审核状态 //发票验证通过 才会扣减待开票金额 失败的开票申请不用处理
  1468. * // 1待系统验证 2 买方公司审核 3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数 10 回票已退
  1469. * 11 待关联订单 12 订单关联完毕待财务审核 13 订单关联完毕财务驳回
  1470. * @return \think\response\Json|void
  1471. * @throws \think\db\exception\DataNotFoundException
  1472. * @throws \think\db\exception\DbException
  1473. * @throws \think\db\exception\ModelNotFoundException
  1474. * @throws \think\exception\DbException
  1475. */
  1476. public function invStatus(){
  1477. $param = $this->request->param(["hpNo"=>"","status"=>"","remark"=>"","ItemRemark"=>[]],"post","trim");
  1478. $valid = Validate::rule(["hpNo|回票申请编号"=>"require|max:255","status"=>"require|number|in:1,2,3,4,6,7,8,13"]);
  1479. if(!$valid->check($param))$this->error($valid->getError());
  1480. $info = PayInvoice::where(["hpNo"=>$param['hpNo'],"is_del"=>0])->findOrEmpty();
  1481. if ($info->isEmpty())$this->error("未找到回票申请信息");
  1482. $payinfo = Pay::where(['payNo'=>$info['payNo'],"is_del"=>0])->findOrEmpty();
  1483. if ($payinfo->isEmpty())$this->error("未找到对账单信息");
  1484. if($payinfo['status']!=2&&$param['status']==3)$this->error("对账信息未完成审核");
  1485. if($payinfo['inv_fee']< $info['inv_fee']) $this->error("回票金额不能大于对账单回票金额");
  1486. $invup =['remark'=>$param['remark'],'updatetime'=>date('Y-m-d H:i:s')];
  1487. if($param['status']==3) $invup['check_time']=date("Y-m-d H:i:s");
  1488. if(in_array($info['invoiceType'],['normal','electronic','toll','roll']) && $param['status']==3){
  1489. $param['status']=4; //普票无需认证
  1490. }
  1491. $invup['status']=$param['status'];
  1492. Db::startTrans();
  1493. try {
  1494. $save=$info->save($invup);
  1495. if($save){
  1496. if($param['status']==4){
  1497. Pay::addAinv($info['payNo'],$info->inv_fee);
  1498. }
  1499. if($param['status']==3){
  1500. if (!empty($param['ItemRemark'])){
  1501. (new InvoiceOrder)->saveAll($param['ItemRemark']);
  1502. };
  1503. }
  1504. if(in_array($param['status'],[5,6,7,8,13])){
  1505. Pay::subInv($info['payNo'],$info->inv_fee);
  1506. if($param['status']==13||$param['status']==7)\app\admin\model\InvoiceItem::rmInvoice($param['hpNo']);
  1507. event("reportCode",["param"=>["key"=>"hpNo","value"=>$param['hpNo']],"type"=>"sub"]);
  1508. }
  1509. }else throw new \Exception("回票审核失败");
  1510. Db::commit();
  1511. }catch (\Exception $e){
  1512. Db::rollback();
  1513. $this->error($e->getMessage());
  1514. }
  1515. $this->success("回票审核成功");
  1516. }
  1517. // public function invStatus(){
  1518. // $post = $this->post;
  1519. // $hpNo = isset($post['hpNo'])&& $post['hpNo']!="" ? trim($post['hpNo']) :"";
  1520. // if($hpNo==""){
  1521. // return error_show(1004,"参数 hpNo 不能为空");
  1522. // }
  1523. // $status = isset($post['status'])&& $post['status']!="" ? intval($post['status']) :"";
  1524. // if($status==""){
  1525. // return error_show(1004,"参数status 不能为空");
  1526. // }
  1527. //// if(!in_array($status,[0,1,2,3,4,5,6,7,8,9,10,11,12])){
  1528. //// return error_show(1004,"参数status 无效值");
  1529. //// }
  1530. // if(!in_array($status,[0,1,2,3,4,5,6,7])){
  1531. // return error_show(1004,"参数status 无效值");
  1532. // }
  1533. // $payment= Db::name("pay_invoice")->where([['hpNo',"=",$hpNo],['is_del',"=",0]])->find();
  1534. //
  1535. // if($payment==false){
  1536. // return error_show(1005,"未找到回票申请信息");
  1537. // }
  1538. // $payinfo= Db::name("pay")->where([['payNo',"=",$payment['payNo']],['is_del',"=",0]])->find();
  1539. //
  1540. // if($payinfo==false){
  1541. // return error_show(1005,"未找到对账信息");
  1542. // }
  1543. // if($payinfo['status']!=2){
  1544. // return error_show(1005,"对账信息未完成审核");
  1545. // }
  1546. // $cgdNo =Db::name("pay_info")->where(["payNo"=>$payment['payNo'],"is_del"=>0])->column("cgdNo");
  1547. // if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  1548. // if($payinfo['inv_fee']< $payment['inv_fee'])return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1549. // $remark = $post['remark']??'';
  1550. // Db::startTrans();
  1551. // try {
  1552. // Db::name('pay_invoice')->where([['hpNo','=',$hpNo],['is_del','=',0]])->lock(true)->find();
  1553. // $invup =["remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  1554. // if($status==3) $invup['check_time']=date("Y-m-d H:i:s");
  1555. // if(in_array($payment['invoiceType'],['normal','electronic','toll','roll']) && $status==3){
  1556. // $status=4; //普票无需认证
  1557. // }
  1558. // $invup['status']=$status;
  1559. // $inv =Db::name("pay_invoice")->where($payment)->update($invup);
  1560. // if($inv){
  1561. // if($status==4){
  1562. // $payupdate =[
  1563. // "ainv_fee"=>$payinfo['ainv_fee']+$payment['inv_fee'],
  1564. // "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
  1565. // "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['winv_fee']==0?3:2,
  1566. // "updatetime" => date("Y-m-d H:i:s")
  1567. // ];
  1568. // $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1569. // if($pay==false){
  1570. // Db::rollback();
  1571. // return error_show(1003,"回票申请状态更新失败");
  1572. // }
  1573. // }
  1574. // if($status==6 || $status==5|| $status==7){
  1575. // $payupdate =[
  1576. // "winv_fee"=>$payinfo['winv_fee']+$payment['inv_fee'],
  1577. // "inv_fee"=>$payinfo['inv_fee']-$payment['inv_fee'],
  1578. // "inv_status" => ($payinfo['inv_fee']-$payment['inv_fee'])==0 && $payinfo['ainv_fee']==0?1:2,
  1579. // "updatetime" => date("Y-m-d H:i:s")
  1580. // ];
  1581. // $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1582. // if($pay==false){
  1583. // Db::rollback();
  1584. // return error_show(1003,"回票申请状态更新失败");
  1585. // }
  1586. // foreach ($cgdNo as $value){
  1587. // $report=ReportCode::where(["cgdNo"=>$value])->find();
  1588. // if($report)$report->rmField("hpNo",$hpNo);
  1589. // }
  1590. // }
  1591. // Db::commit();
  1592. // return app_show(0,"回票申请更新成功");
  1593. // }throw new \Exception("回票申请状态更新失败");
  1594. // }catch (\Exception $e){
  1595. // Db::rollback();
  1596. // return error_show(1004,$e->getMessage());
  1597. // }
  1598. //
  1599. // }
  1600. /**发票列表识别
  1601. * @param int $id
  1602. * @return \think\Response
  1603. */
  1604. public function InvList()
  1605. {
  1606. $post = $this->post;
  1607. $condition = [["a.is_del","=",0 ],['a.invType','<>',4 ],["b.is_del","=",0 ],['pay_type','=',$post['pay_type']??1]];
  1608. $invtype = isset($post['invType'])&&$post['invType']!='' ? intval($post['invType']):"";
  1609. $check = checkRole($this->roleid,[82,120]);
  1610. if($check){
  1611. $condition []=["a.apply_id","=",$this->uid];
  1612. }
  1613. if ($invtype!=''){
  1614. $condition[]=["a.invType","=",$invtype];
  1615. }
  1616. $companyNo = isset($post['companyNo'])&&$post['companyNo']!='' ? trim($post['companyNo']):"";
  1617. if($companyNo!=""){
  1618. $condition[]=["b.companyNo","=",$companyNo];
  1619. }
  1620. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1621. if($relaComNo!=""){
  1622. $condition[]=["b.companyNo|b.supplierNo","=",$relaComNo];
  1623. }
  1624. $supplierNo = isset($post['supplierNo'])&&$post['supplierNo']!='' ? trim($post['supplierNo']):"";
  1625. if($supplierNo!=""){
  1626. $condition[]=["b.supplierNo","=",$supplierNo];
  1627. }
  1628. $hpNo = isset($post['hpNo'])&&$post['hpNo']!='' ? trim($post['hpNo']):"";
  1629. if ($hpNo!=''){
  1630. $condition[]=["a.hpNo","like","%$hpNo%"];
  1631. }
  1632. $payNo = isset($post['payNo'])&&$post['payNo']!='' ? trim($post['payNo']):"";
  1633. if ($payNo!=''){
  1634. $condition[]=["a.payNo","like","%$payNo%"];
  1635. }
  1636. $invoiceType = isset($post['invoiceType'])&&$post['invoiceType']!='' ? intval($post['invoiceType']):"";
  1637. if ($invoiceType!=''){
  1638. $condition[]=["a.invoiceType","=",$invoiceType];
  1639. }
  1640. $status = isset($post['status'])&&$post['status']!=="" ? intval($post['status']):"";
  1641. if($status!==""){
  1642. $condition[]=["a.status","=",$status];
  1643. }
  1644. $invNumber = isset($post['invNumber'])&&$post['invNumber']!="" ? trim($post['invNumber']):"";
  1645. if($invNumber!=''){
  1646. $condition[]=["invoiceNumber","=",$invNumber];
  1647. }
  1648. $checkApi = isset($post['checkApi'])&&$post['checkApi']!="" ? intval($post['checkApi']):"";
  1649. if($checkApi!==''){
  1650. $condition[]=["checkApi","=",$checkApi];
  1651. }
  1652. $invCode = isset($post['invCode'])&&$post['invCode']!="" ? trim($post['invCode']):"";
  1653. if($invCode!=''){
  1654. $condition[]=["invoiceCode","=",$invCode];
  1655. }
  1656. $start = isset($post['start']) && $post['start']!="" ? $post['start'] :"";
  1657. if($start!=""){
  1658. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1659. }
  1660. $end = isset($post['end']) && $post['end']!="" ? $post['end'] :"";
  1661. if($end!=""){
  1662. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1663. }
  1664. $open_start = isset($post['open_start']) && $post['open_start']!="" ? $post['open_start'] :"";
  1665. if($open_start!=""){
  1666. $condition[]=["a.open_time",">=",$open_start];
  1667. }
  1668. $open_end = isset($post['open_end']) && $post['open_end']!="" ? $post['open_end'] :"";
  1669. if($open_end!=""){
  1670. $condition[]=["a.open_time","<=",$open_end];
  1671. }
  1672. $apply_id = isset($post['apply_id']) && $post['apply_id']!="" ? $post['apply_id'] :"";
  1673. if($apply_id!=""){
  1674. $condition[]=["a.apply_id","=",$apply_id];
  1675. }
  1676. $apply_name = isset($post['apply_name']) && $post['apply_name']!="" ? trim($post['apply_name']):"";
  1677. if($apply_name!=""){
  1678. $condition[]=["a.apply_name","like","%$apply_name%"];
  1679. }
  1680. if(isset($post['is_comon'])&&$post['is_comon']!=='') $condition []=['is_comon','=',$post['is_comon']];
  1681. $page = isset($post['page'])&&$post['page']!="" ? intval($post['page']):1;
  1682. $size = isset($post['size'])&&$post['size']!="" ? intval($post['size']):10;
  1683. $count =Db::name("pay_invoice")
  1684. ->alias("a")
  1685. ->leftJoin("pay b","a.payNo=b.payNo")
  1686. ->where($condition)
  1687. ->count();
  1688. $total = ceil($count/$size);
  1689. $page = $page>$total? intval($total):$page;
  1690. $list = Db::name("pay_invoice")
  1691. ->alias("a")
  1692. ->leftJoin("pay b", "a.payNo=b.payNo")
  1693. ->field("a.*,b.supplierNo,b.supplierName,b.companyName,b.is_comon,b.companyNo")
  1694. ->where($condition)
  1695. ->page($page,$size)
  1696. ->order("addtime desc")
  1697. ->select()
  1698. ->toArray();
  1699. //校验是否开通了供应商账号
  1700. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list,'supplierNo')));
  1701. $inv_fee=$inv_subtotal_amount = $total = 0;
  1702. foreach ($list as &$value){
  1703. $invoinfo =InvoiceInfo::where(["hpNo"=>$value['hpNo'],"status"=>1])->find();
  1704. $value['buyer_name'] = $invoinfo['buyer_name']??"";
  1705. $value['buyer_id'] = $invoinfo['buyer_id']??"";
  1706. $value['buyer_address'] = $invoinfo['buyer_address']??"";
  1707. $value['buyer_bank'] = $invoinfo['buyer_bank']??"";
  1708. $value['seller_name'] = $invoinfo['seller_name']??"";
  1709. $value['seller_id'] = $invoinfo['seller_id']??"";
  1710. $value['seller_address'] = $invoinfo['seller_address']??"";
  1711. $value['seller_bank'] = $invoinfo['seller_bank']??"";
  1712. $value['subtotal_amount'] = $invoinfo['subtotal_amount']??"";
  1713. $value['subtotal_tax'] = $invoinfo['subtotal_tax']??"";
  1714. $value['total'] = $invoinfo['total']??"";
  1715. $value['receiver'] = $invoinfo['receiver']??"";
  1716. $value['issuer'] = $invoinfo['issuer']??"";
  1717. $value['reivewer'] = $invoinfo['reivewer']??"";
  1718. $value['remarks'] = $invoinfo['remarks']??"";
  1719. $value['invoiceType_cn'] = $this->invoiceType[$value['invoiceType']]??'';
  1720. $value['invStatus_cn'] = $this->invStatus[$value['invStatus']]??'';
  1721. $value['item_list'] = $invoinfo['item_list']??[];
  1722. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  1723. if($value['status']==4){
  1724. $inv_fee += $value['inv_fee'];
  1725. $inv_subtotal_amount += intval($value['inv_subtotal_amount']??0);
  1726. $total += intval($value['total']??0);
  1727. }
  1728. }
  1729. //结算invlist这个接口当用payNo筛选的时候,返回列表里需要加一项统计筛选后列表的inv_subtotal_amount、total字段的和
  1730. // $inv_fee=$inv_subtotal_amount = $total = 0;
  1731. // if ($payNo != '') {
  1732. // $inv_subtotal_amount = round(array_sum(array_column($list, 'inv_subtotal_amount')), 2);
  1733. // $total = round(array_sum(array_column($list, 'total')), 2);
  1734. // }
  1735. return app_show(0, "获取成功", [
  1736. "count" => $count,
  1737. "list" => $list,
  1738. 'inv_subtotal_amount' => $inv_subtotal_amount,
  1739. 'total' => $total,
  1740. "inv_fee"=>$inv_fee
  1741. ]);
  1742. }
  1743. /**发票删除
  1744. * @return \think\response\Json|void
  1745. * @throws \think\db\exception\DataNotFoundException
  1746. * @throws \think\db\exception\DbException
  1747. * @throws \think\db\exception\ModelNotFoundException
  1748. */
  1749. public function invDel(){
  1750. $post = $this->post;
  1751. $hpNo = isset($post['hpNo'])&&$post['hpNo']!="" ? trim($post['hpNo']) :"";
  1752. if ($hpNo==""){
  1753. return error_show(1004,"参数 hpNo 不能为空");
  1754. }
  1755. $payinv =Db::name("pay_invoice")->where(["hpNo"=>$hpNo,"is_del"=>0])->find();
  1756. if($payinv==false){
  1757. return error_show(1004,"对账单回票申请未找到数据");
  1758. }
  1759. $payinfo= Db::name("pay")->where([['payNo',"=",$payinv['payNo']],['is_del',"=",0]])->find();
  1760. if($payinfo==false){
  1761. return error_show(1005,"未找到对账信息");
  1762. }
  1763. if ($this->level == 2) {
  1764. //判断是否开通供应商账号
  1765. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  1766. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  1767. }
  1768. if($payinfo['status']!=2){
  1769. return error_show(1005,"对账信息未完成审核");
  1770. }
  1771. $cgdNo =Db::name("pay_info")->where(["payNo"=>$payinv['payNo'],"is_del"=>0])->column("cgdNo");
  1772. if(empty($cgdNo)) return error_show(1004,"未找到对账数据信息");
  1773. Db::startTrans();
  1774. try{
  1775. $update=["status"=>8,"updatetime"=>date("Y-m-d H:i:s")]; //删除状态 流程终止
  1776. $invup =Db::name("pay_invoice")->where($payinv)->update($update);
  1777. if($invup){
  1778. if($payinv['status']==4){
  1779. if($payinfo['ainv_fee']< $payinv['inv_fee']){
  1780. Db::rollback();
  1781. return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1782. }
  1783. $payupdate =[
  1784. "ainv_fee"=>$payinfo['ainv_fee']-$payinv['inv_fee'],
  1785. "winv_fee"=>$payinfo['winv_fee']+$payinv['inv_fee'],
  1786. "inv_status" => ($payinfo['ainv_fee']-$payinv['inv_fee'])==0?1:2,
  1787. "updatetime" => date("Y-m-d H:i:s")
  1788. ];
  1789. $pay =Db::name("pay")->where($payinfo)->update($payupdate);
  1790. if($pay==false){
  1791. Db::rollback();
  1792. return error_show(1003,"删除失败");
  1793. }
  1794. foreach ($cgdNo as $value){
  1795. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1796. if($report)$report->rmField("hpNo",$hpNo);
  1797. }
  1798. }
  1799. if(in_array($payinv['status'], [1,2,3,11,12])){
  1800. if($payinfo['inv_fee']< $payinv['inv_fee']){
  1801. Db::rollback();
  1802. return error_show(1005,"对账回票申请金额有误,请确认回票申请金额");
  1803. }
  1804. Pay::subInv($payinfo['payNo'],$payinv['inv_fee']);
  1805. if(in_array($payinv['status'], [2,3,12])) \app\admin\model\InvoiceItem::rmInvoice($param['hpNo']);
  1806. foreach ($cgdNo as $value){
  1807. $report=ReportCode::where(["cgdNo"=>$value])->find();
  1808. if($report)$report->rmField("hpNo",$hpNo);
  1809. }
  1810. }
  1811. Db::commit();
  1812. return app_show(0,"删除成功");
  1813. }
  1814. }catch (\Exception $e){
  1815. Db::rollback();
  1816. return error_show(1003,$e->getMessage());
  1817. }
  1818. }
  1819. public function HpInfo(){
  1820. $post = $this->post;
  1821. $hpNo = isset($post['hpNo'])&&$post['hpNo']!="" ? trim($post['hpNo']) :"";
  1822. if ($hpNo==""){
  1823. return error_show(1004,"参数 hpNo 不能为空");
  1824. }
  1825. $payinv =Db::name("pay_invoice")->where(["hpNo"=>$hpNo,"is_del"=>0])->find();
  1826. if($payinv==false){
  1827. return error_show(1004,"对账单回票申请未找到数据");
  1828. }
  1829. $payinfo= Db::name("pay")->where([['payNo',"=",$payinv['payNo']],['is_del',"=",0]])->find();
  1830. if($payinfo==false){
  1831. return error_show(1005,"未找到对账信息");
  1832. }
  1833. $payinv['pay_apply_id'] = $payinfo['apply_id']??'';
  1834. $payinv['pay_apply_name'] = $payinfo['apply_name']??'';
  1835. $payinv['supplierNo'] = $payinfo['supplierNo']??'';
  1836. $payinv['supplierName'] = $payinfo['supplierName']??'';
  1837. $payinv['companyNo'] = $payinfo['companyNo']??'';
  1838. $payinv['companyName'] = $payinfo['companyName']??'';
  1839. $payinv['total_fee'] = $payinfo['total_fee']??'';
  1840. $invinfo =InvoiceInfo::where(["hpNo"=>$hpNo])->findOrEmpty();
  1841. $payinv['invoiceType_cn'] = $this->invoiceType[$payinv['invoiceType']]??'';
  1842. $payinv['invStatus_cn'] = $this->invStatus[$payinv['invStatus']]??'';
  1843. $payinv["info"]=$invinfo;
  1844. return app_show(0,'获取成功',$payinv);
  1845. }
  1846. /**
  1847. * @return \think\response\Json|void
  1848. * @throws \think\db\exception\DbException
  1849. */
  1850. public function UnPay(){
  1851. $post = $this->post;
  1852. $codeNo = isset($post['id'])&&$post['id']!=="" ? trim($post['id']) :'';
  1853. if($codeNo===""){
  1854. return error_show(1004,'参数 id 不能为空');
  1855. }
  1856. $update = Db::name('cgd_info')->where("id","in",$codeNo)->update(['status'=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  1857. if($update){
  1858. return app_show(0,'更新成功');
  1859. }else{
  1860. return error_show(1004,'更新失败');
  1861. }
  1862. }
  1863. /**
  1864. * @return \think\|\think\response\Json|void
  1865. */
  1866. public function ticketInfo(){
  1867. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  1868. if($num==''){
  1869. return error_show(1004,'参数 number 不能为空');
  1870. }
  1871. $condition=[];
  1872. $condition[]=["number","=",$num];
  1873. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1874. if($invNo!=='')$condition[]=["hpNo","=",$invNo];
  1875. $info =InvoiceInfo::where($condition)->findOrEmpty();
  1876. return empty($info)?error_show(1005,"为找到数据"):app_show(0,"获取成功",$info);
  1877. }
  1878. /**
  1879. * @return \think\response\Json|void
  1880. * @throws \think\db\exception\DataNotFoundException
  1881. * @throws \think\db\exception\DbException
  1882. * @throws \think\db\exception\ModelNotFoundException
  1883. */
  1884. public function hpinvList(){
  1885. $post=$this->request->only(["hpNo"=>"","page"=>1,"size"=>15],"post","trim");
  1886. $where=[];
  1887. if($post["hpNo"]!="") $where[]=["hpNo","=",$post['hpNo']];
  1888. $count=InvoiceInfo::where($where)->count();
  1889. $total =ceil($count/$post['size']);
  1890. $page= $post['page']>$total ? intval($total):intval($post['page']);
  1891. $list = InvoiceInfo::where($where)
  1892. ->page($page,intval($post['size']))
  1893. ->order("id desc")
  1894. ->select()->toArray();
  1895. foreach ($list as &$value){
  1896. $value['type_name'] = $this->invoiceType[$value['type']]??"";
  1897. }
  1898. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1899. }
  1900. public function hpinvReturn(){
  1901. $param =$this->request->only(["hpNo"=>"","reason"=>""],"post","trim");
  1902. $valide=Validate::rule([
  1903. "hpNo|回票编号"=>"require|max:255",
  1904. "reason|申请原因"=>"require|max:255"
  1905. ]);
  1906. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  1907. $hpinfo =Db::name("pay_invoice")->where([["hpNo","=",$param['hpNo']],["is_del","=",0]])->findOrEmpty();
  1908. if(empty($hpinfo)) return error_show(1004,"回票信息未找到");
  1909. $payinfo =Db::name("pay")->where(["payNo"=>$hpinfo['payNo'],"is_del"=>0])->find();
  1910. if($payinfo==false){
  1911. return error_show(1004,"对账单数据未找到");
  1912. }
  1913. if ($this->level == 2) {
  1914. //判断是否开通供应商账号
  1915. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  1916. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  1917. }
  1918. if($hpinfo['status']!=4)return error_show(1004,"回票信息流程未完成");
  1919. $isR=Db::name("pay_return")->where(["orderCode"=>$param['hpNo'],"status"=>[1,2],"is_del"=>0])->findOrEmpty();
  1920. if(!empty($isR))return error_show(1004,"回票信息退票流程已存在");
  1921. $returnCode =makeNo("RP");
  1922. $data = [
  1923. "returnCode"=>$returnCode,
  1924. "returnType"=>1,
  1925. "payNo"=>$hpinfo['payNo'],
  1926. "orderCode"=>$param['hpNo'],
  1927. "reason"=>$param['reason'],
  1928. "returnImg"=>'',
  1929. "remark"=>'',
  1930. "status"=>1,
  1931. "apply_id"=>$this->uid,
  1932. "apply_name"=>$this->uname,
  1933. "is_del"=>0,
  1934. "addtime"=>date("Y-m-d H:i:s"),
  1935. "updatetime"=>date("Y-m-d H:i:s"),
  1936. ];
  1937. $insert=Db::name("pay_return")->insert($data);
  1938. if($insert){
  1939. return app_show(0,"退票申请新建成功",$returnCode);
  1940. }else{
  1941. return error_show(1004,"退票申请新建失败");
  1942. }
  1943. }
  1944. public function hpinvReturnList(){
  1945. $param =$this->request->only(["relaComNo"=>"","companyNo"=>"","supplierNo"=>"","start"=>"","end"=>"","returnCode"=>"",
  1946. "status"=>"","hpNo"=>"","is_comon"=>"","payNo"=>"","pay_type"=>"","page"=>1,"size"=>15],"post","trim");
  1947. $condition=[["a.is_del","=",0],['a.returnType',"=",1],['pay_type','=',$param['pay_type']?:1]];
  1948. $check = checkRole($this->roleid,[132,130]);
  1949. if($check){
  1950. $condition []=["a.apply_id","=",$this->uid];
  1951. }
  1952. if($param['relaComNo']!="") $condition[]=["b.companyNo|b.supplierNo","=",$param['relaComNo']];
  1953. if($param['companyNo']!="") $condition[]=["b.companyNo","=",$param['companyNo']];
  1954. if($param['supplierNo']!="") $condition[]=["b.supplierNo","=",$param['supplierNo']];
  1955. if($param['start']!="") $condition[]=["a.addtime",">=",date("Y-m-d H:i:s",strtotime($param['start']))];
  1956. if($param['end']!="") $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($param['end']))];
  1957. if($param['status']!="") $condition[]=["a.status","=",$param['status']];
  1958. if($param['is_comon']!="") $condition[]=["b.is_comon","=",$param['is_comon']];
  1959. if($param['hpNo']!="") $condition[]=["a.orderCode","like","%{$param['hpNo']}%"];
  1960. if($param['returnCode']!="") $condition[]=["a.returnCode","like","%{$param['returnCode']}%"];
  1961. if($param['payNo']!="") $condition[]=["a.payNo","like","%{$param['payNo']}%"];
  1962. if(isset($param['is_comon'])&&$param['is_comon']!=='') $condition []=['is_comon','=',$param['is_comon']];
  1963. $count = Db::name("pay_return")
  1964. ->alias("a")
  1965. ->leftJoin("pay b", "a.payNo=b.payNo and b.is_del=0")
  1966. ->leftJoin("pay_invoice c", "a.orderCode=c.hpNo")
  1967. ->where($condition)
  1968. ->count();
  1969. $total = ceil($count / $param['size']);
  1970. $page = $param['page'] >= $total ? intval($total) : intval($param['page']);
  1971. $list = Db::name("pay_return")
  1972. ->alias("a")
  1973. ->leftJoin("pay b", "a.payNo=b.payNo and b.is_del=0")
  1974. ->leftJoin("pay_invoice c", "a.orderCode=c.hpNo")
  1975. ->where($condition)
  1976. ->page($page, intval($param['size']))
  1977. ->order('id desc')
  1978. ->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")
  1979. ->select()
  1980. ->toArray();
  1981. //校验是否开通了供应商账号
  1982. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list, 'supplierNo')));
  1983. foreach ($list as &$value) {
  1984. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  1985. $value['inv_number']= \app\admin\model\InvoiceInfo::where(["hpNo"=>$value['orderCode']])->value("number",'');
  1986. }
  1987. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1988. }
  1989. public function hpinvReturnStatus(){
  1990. $param =$this->request->only(["returnCode"=>"","status"=>"","remark"=>"","returnImg"=>""],"post","trim");
  1991. $valide=Validate::rule([
  1992. "returnCode|退票申请编号"=>"require|max:255",
  1993. "status|状态"=>"require|number|in:1,2,3",
  1994. ]);
  1995. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  1996. $returninfo =Db::name("pay_return")->where(["is_del"=>0,"returnCode"=>$param['returnCode']])->findOrEmpty();
  1997. if(empty($returninfo))return error_show(1004,"退票详情未找到");
  1998. Db::startTrans();
  1999. try{
  2000. $update=[
  2001. "status"=>$param['status'],
  2002. "remark"=>$param['remark'],
  2003. "returnImg"=>$param['returnImg'],
  2004. "updatetime"=>date("Y-m-d H:i:s")
  2005. ];
  2006. $up =Db::name("pay_return")->where($returninfo)->update($update);
  2007. if($up==false)throw new Exception("退票申请审核失败");
  2008. if($param['status']==2){
  2009. $invinfo =Db::name('pay_invoice')->where(['hpNo'=>$returninfo['orderCode'],'is_del'=>0])->findOrEmpty();
  2010. if(empty($invinfo))throw new Exception('回票详情未找到');
  2011. $pay =Db::name('pay')->where(['payNo'=>$returninfo['payNo'],'is_del'=>0])->findOrEmpty();
  2012. if(empty($pay))throw new Exception('对账详情未找到');
  2013. $invup =Db::name('pay_invoice')->where($invinfo)->update(['status'=>10,'updatetime'=>date('Y-m-d H:i:s')]);
  2014. if($invup==false)throw new Exception('回票更新失败');
  2015. Pay::subAinv($returninfo['payNo'],$invinfo['inv_fee']);
  2016. \app\admin\model\InvoiceItem::rmInvoice($returninfo['orderCode'],2);
  2017. }
  2018. Db::commit();
  2019. }catch (\Exception $e){
  2020. Db::rollback();
  2021. return error_show(1005,$e->getMessage());
  2022. }
  2023. $this->success("退票申请审核成功");
  2024. }
  2025. public function hpinvReturnInfo(){
  2026. $param = $this->request->only(["returnCode"=>""],"post","trim");
  2027. $valide=Validate::rule([
  2028. "returnCode|退票申请编号"=>"require|max:255",
  2029. ]);
  2030. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2031. $info = Db::name("pay_return")->alias("a")
  2032. ->leftJoin("pay b","a.payNo=b.payNo and b.is_del=0")
  2033. ->leftJoin("pay_invoice c","a.orderCode=c.hpNo")
  2034. ->where(['returnCode'=>$param['returnCode']])
  2035. ->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")
  2036. ->findOrEmpty();
  2037. return app_show(0,"退票申请获取成功",$info);
  2038. }
  2039. //付款退回
  2040. public function stageReturn(){
  2041. $param =$this->request->only(["dzNo"=>"","reason"=>""],"post","trim");
  2042. $valide=Validate::rule([
  2043. "dzNo|付款编号"=>"require|max:255",
  2044. "reason|申请原因"=>"require|max:255"
  2045. ]);
  2046. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2047. $hpinfo =Db::name("pay_payment")->where([["dzNo","=",$param['dzNo']],["is_del","=",0]])->findOrEmpty();
  2048. if(empty($hpinfo)) return error_show(1004,"付款信息未找到");
  2049. $payinfo =Db::name("pay")->where(["payNo"=>$hpinfo['payNo'],"is_del"=>0])->find();
  2050. if($payinfo==false){
  2051. return error_show(1004,"对账单数据未找到");
  2052. }
  2053. if ($this->level == 2 && $payinfo['is_comon']==0) {
  2054. //判断是否开通供应商账号
  2055. $temp = check_has_account_by_supplierNos([$payinfo['supplierNo']]);
  2056. if (isset($temp['data'][$payinfo['supplierNo']])) return error_show(1004, '该供应商已经开通账号,不允许当前账号操作');
  2057. }
  2058. if(!in_array($hpinfo['status'],[3,4]))return error_show(1004,"付款信息流程未完成");
  2059. $isR=Db::name("pay_return")->where(["orderCode"=>$param['dzNo'],"status"=>[1,2],"is_del"=>0])->findOrEmpty();
  2060. if(!empty($isR))return error_show(1004,"付款信息退款流程已存在");
  2061. $returnCode =makeNo("RP");
  2062. $data = [
  2063. "returnCode"=>$returnCode,
  2064. "returnType"=>2,
  2065. "payNo"=>$hpinfo['payNo'],
  2066. "orderCode"=>$param['dzNo'],
  2067. "reason"=>$param['reason'],
  2068. "returnImg"=>'',
  2069. "remark"=>'',
  2070. "status"=>1,
  2071. "apply_id"=>$this->uid,
  2072. "apply_name"=>$this->uname,
  2073. "is_del"=>0,
  2074. "addtime"=>date("Y-m-d H:i:s"),
  2075. "updatetime"=>date("Y-m-d H:i:s"),
  2076. ];
  2077. $insert=Db::name("pay_return")->insert($data);
  2078. if($insert){
  2079. return app_show(0,"退款申请新建成功",$returnCode);
  2080. }else{
  2081. return error_show(1004,"退款申请新建失败");
  2082. }
  2083. }
  2084. public function stageReturnList(){
  2085. $param =$this->request->only(["relaComNo"=>"","companyNo"=>"","supplierNo"=>"","start"=>"","end"=>"","returnCode"=>"","payNo"=>"",
  2086. "status"=>"","dzNo"=>"","pay_type"=>"","is_comon"=>"","page"=>1,"size"=>15],"post","trim");
  2087. $condition=[["a.is_del","=",0],['a.returnType',"=",2],['pay_type','=',$param['pay_type']?:1]];
  2088. $check = checkRole($this->roleid,[131,129]);
  2089. if($check){
  2090. $condition []=["a.apply_id","=",$this->uid];
  2091. }
  2092. if($param['relaComNo']!="") $condition[]=["b.companyNo|b.supplierNo","=",$param['relaComNo']];
  2093. if($param['companyNo']!="") $condition[]=["b.companyNo","=",$param['companyNo']];
  2094. if($param['supplierNo']!="") $condition[]=["b.supplierNo","=",$param['supplierNo']];
  2095. if($param['start']!="") $condition[]=["a.addtime",">=",date("Y-m-d H:i:s",strtotime($param['start']))];
  2096. if($param['end']!="") $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($param['end']))];
  2097. if($param['status']!="") $condition[]=["a.status","=",$param['status']];
  2098. if($param['dzNo']!="") $condition[]=["a.orderCode","like","%{$param['dzNo']}%"];
  2099. if($param['returnCode']!="") $condition[]=["a.returnCode","like","%{$param['returnCode']}%"];
  2100. if($param['payNo']!="") $condition[]=["a.payNo","like","%{$param['payNo']}%"];
  2101. if(isset($param['is_comon'])&&$param['is_comon']!=='') $condition []=['is_comon','=',$param['is_comon']];
  2102. $count = Db::name("pay_return")
  2103. ->alias("a")
  2104. ->leftJoin("pay b", "a.payNo=b.payNo and b.is_del=0")
  2105. ->leftJoin("pay_payment c", "a.orderCode=c.dzNo")
  2106. ->where($condition)
  2107. ->count();
  2108. $total = ceil($count / $param['size']);
  2109. $page = $param['page'] >= $total ? intval($total) : intval($param['page']);
  2110. $list = Db::name("pay_return")
  2111. ->alias("a")
  2112. ->leftJoin("pay b", "a.payNo=b.payNo and b.is_del=0")
  2113. ->leftJoin("pay_payment c", "a.orderCode=c.dzNo")
  2114. ->where($condition)
  2115. ->page($page, intval($param['size']))
  2116. ->order('id desc')
  2117. ->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")
  2118. ->select()
  2119. ->toArray();
  2120. //校验是否开通了供应商账号
  2121. $supp_account = check_has_account_by_supplierNos(array_unique(array_column($list, 'supplierNo')));
  2122. foreach ($list as &$value) {
  2123. $value['has_account'] = (int)isset($supp_account['data'][$value['supplierNo']]);
  2124. }
  2125. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2126. }
  2127. public function stageReturnStatus(){
  2128. $param =$this->request->only(["returnCode"=>"","status"=>"","remark"=>"","returnImg"=>""],"post","trim");
  2129. $valide=Validate::rule([
  2130. "returnCode|退票申请编号"=>"require|max:255",
  2131. "status|状态"=>"require|number|in:1,2,3",
  2132. ]);
  2133. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2134. $returninfo =Db::name("pay_return")->where(["is_del"=>0,"returnCode"=>$param['returnCode']])->findOrEmpty();
  2135. if(empty($returninfo))return error_show(1004,"退票详情未找到");
  2136. Db::startTrans();
  2137. try{
  2138. if($param['status']==2){
  2139. $invinfo =Db::name("pay_payment")->where(["dzNo"=>$returninfo['orderCode'],"is_del"=>0])->findOrEmpty();
  2140. if(empty($invinfo))throw new Exception("付款详情未找到");
  2141. $pay =Db::name("pay")->where(["payNo"=>$returninfo['payNo'],"is_del"=>0])->findOrEmpty();
  2142. if(empty($pay))throw new Exception("对账详情未找到");
  2143. $invup =Db::name("pay_payment")->where($invinfo)->update(["status"=>5,"updatetime"=>date("Y-m-d H:i:s")]);
  2144. if($invup==false)throw new Exception("付款更新失败");
  2145. if($pay['apay_fee']<$invinfo['pay_fee'])throw new Exception('已付款金额不足');
  2146. $paydata=[
  2147. "apay_fee"=>$pay['apay_fee']-$invinfo['pay_fee'],
  2148. "wpay_fee"=>$pay['wpay_fee']+$invinfo['pay_fee'],
  2149. "pay_status" => $pay['pay_fee']==0 &&($pay['apay_fee']-$invinfo['pay_fee'])==0?1:2,
  2150. "updatetime" => date("Y-m-d H:i:s")
  2151. ];
  2152. $oayup =Db::name("pay")->where($pay)->update($paydata);
  2153. if($oayup==false)throw new Exception("对账更新失败");
  2154. }
  2155. $update=[
  2156. "status"=>$param['status'],
  2157. "remark"=>$param['remark'],
  2158. "returnImg"=>$param['returnImg'],
  2159. "updatetime"=>date("Y-m-d H:i:s")
  2160. ];
  2161. $up =Db::name("pay_return")->where($returninfo)->update($update);
  2162. if($up==false)throw new Exception("退款申请审核失败");
  2163. Db::commit();
  2164. if($param['status']==2 && $pay['is_comon']==1){
  2165. event('comonOrder', ['payNo' => $pay['payNo'], 'pay_fee' => $invinfo['pay_fee'], 'type' => 'repay']);
  2166. }
  2167. return app_show(0,"退款申请审核成功");
  2168. }catch (\Exception $e){
  2169. Db::rollback();
  2170. return error_show(1005,$e->getMessage());
  2171. }
  2172. }
  2173. public function stageReturnInfo(){
  2174. $param = $this->request->only(["returnCode"=>""],"post","trim");
  2175. $valide=Validate::rule([
  2176. "returnCode|退票申请编号"=>"require|max:255",
  2177. ]);
  2178. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2179. $info = Db::name("pay_return")->alias("a")
  2180. ->leftJoin("pay b","a.payNo=b.payNo and b.is_del=0")
  2181. ->leftJoin("pay_payment c","a.orderCode=c.dzNo")
  2182. ->where(['returnCode'=>$param['returnCode']])
  2183. ->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")
  2184. ->findOrEmpty();
  2185. return app_show(0,"退款申请获取成功",$info);
  2186. }
  2187. /**批量上传待认证发票
  2188. * @param hpNo array 回票申请编号集合 必传
  2189. * @param relaComNo string 业务公司编号 企业账户
  2190. * @param companyNo string 业务公司编号 超管账户
  2191. * @return \think\response\Json|void
  2192. */
  2193. //回票申请批量认证
  2194. public function hpBatchImport()
  2195. {
  2196. $post = $this->request->only(["list" => [], "relaComNo" => "", "companyNo" => ""], "post");
  2197. $valid = Validate::rule([
  2198. "list|回票申请编号集合" => "require|array|max:100",
  2199. "relaComNo|关联公司" => "requireWithout:companyNo|max:255",
  2200. "companyNo|公司编号" => "requireWithout:relaComNo|max:255",
  2201. ]);
  2202. if ($valid->check($post) == false) return error_show(1004, $valid->getError());
  2203. $companyNo = $post["relaComNo"] ?: $post['companyNo'];
  2204. $payArr = Db::name("pay_invoice")
  2205. ->alias("a")
  2206. ->leftJoin("pay b", "a.payNo=b.payNo")
  2207. ->where(['a.is_del' => 0, 'b.is_del' => 0])
  2208. ->whereIn('a.hpNo', array_column($post['list'], 'hpNo'))
  2209. ->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");
  2210. $val_hpNo = Validate::rule([
  2211. 'hpNo|回票申请编号' => 'require',
  2212. 'status|审核状态' => 'require|number|in:4,7',
  2213. 'remark|审核备注' => 'requireIf:status,7|max:255'
  2214. ]);
  2215. $unique=[];
  2216. foreach ($post['list'] as $item) {
  2217. if (!$val_hpNo->check($item)) return error_show(1004, $val_hpNo->getError());
  2218. if(in_array($item['hpNo'],$unique))return error_show(1004, "{$item['hpNo']} 发票重复提交");
  2219. $unique[]=$item['hpNo'];
  2220. }
  2221. Db::startTrans();
  2222. try {
  2223. $pay_invoice_allow = [];
  2224. foreach ($post['list'] as $val) {
  2225. if (!$val_hpNo->check($val)) throw new \Exception($val_hpNo->getError());
  2226. if (!isset($payArr[$val['hpNo']])) throw new \Exception("{$val['hpNo']}记录不存在");
  2227. $value = $payArr[$val['hpNo']];
  2228. $payinfo = Db::name("pay")->where(["payNo"=>$payArr[$val['hpNo']]['payNo'],"status"=>2,"is_del"=>0])->findOrEmpty();
  2229. if(empty($payinfo)) throw new Exception("{$value['hpNo']}对账信息有误");
  2230. if ($payinfo['companyNo'] == '') throw new Exception("{$value['hpNo']}对账信息有误");
  2231. if ($payinfo['companyNo'] != $companyNo) throw new Exception("{$value['hpNo']}不属于当前业务公司发票");
  2232. if ($value['status'] != '3') throw new Exception("{$value['hpNo']}状态有误");
  2233. if ($value['invoice_fee'] > $payinfo['inv_fee']) throw new Exception("{$value['hpNo']}对账单开票中金额不足");
  2234. if ($val['status'] == 4) {
  2235. Pay::addAinv($payArr[$val['hpNo']]['payNo'],$value['invoice_fee']);
  2236. $pay_invoice_allow[] = $value['id'];
  2237. } else {
  2238. Pay::subInv($payArr[$val['hpNo']]['payNo'],$value['invoice_fee']);
  2239. Db::name("pay_invoice")
  2240. ->where(["id" => $value['id'], "status" => 3, "is_del" => 0])
  2241. ->update(["status" => $val['status'], "updatetime" => date("Y-m-d H:i:s"), 'remark' =>$val['remark']]);
  2242. }
  2243. }
  2244. 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")]);
  2245. // if ($update == false) throw new Exception("回票申请状态更新失败");
  2246. Db::commit();
  2247. return app_show(0, "回票申请认证成功");
  2248. } catch (\Exception $e) {
  2249. Db::rollback();
  2250. return error_show(1004, $e->getMessage());
  2251. }
  2252. }
  2253. /**
  2254. * 导出对账单采购单信息
  2255. */
  2256. public function payCgdExport(){
  2257. $param = $this->request->only(["payNo"=>""],"post","trim");
  2258. $valide=Validate::rule([
  2259. "payNo|对账申请编号"=>"require|max:255",
  2260. ]);
  2261. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2262. $payinfo =Db::name("pay")->where(["payNo"=>$param['payNo'],"is_del"=>0,"pay_type"=>1])->findOrEmpty();
  2263. if(empty($payinfo)) return error_show(1005,'对账单信息未找到');
  2264. $cgdlist =Db::name("pay_info")->alias("a")
  2265. ->leftJoin("cgd_info b","a.cgdNo=b.sequenceNo")
  2266. ->leftJoin("order_category c","a.cgdNo=c.code and order_type=2")
  2267. ->leftJoin("good d","c.spuCode =d.spuCode")
  2268. ->where(["a.payNo"=>$param['payNo'],"a.status"=>1,"a.is_del"=>0])
  2269. ->field("a.payNo'对账单号',b.sequenceNo '采购单编号',b.qrdCode '销售单编号',b.supplierName '供货商',b.companyName '业务公司',
  2270. b.goodUnit '单位',(b.goodNum-b.thNum) '商品数量',b.goodPrice'商品单价',b.totalPrice'总货款',
  2271. if(b.cxCode='',b.qrdCode,b.cxCode) 销售主单号,b.goodNo 商品编码,b.goodName 商品名称,c.cat_name 货物和劳务名称,c.merge_code 商品开票类目,c.short_name 商品和服务分类简称,
  2272. c.tax/100 开票税率,c.inv_good_name 开票商品名称,if(b.open_type =1,'原商品开票',if(b.open_type =2,'子商品开票','')) 开票类型")
  2273. ->select()->toArray();
  2274. if(empty($cgdlist))$cgdlist=["暂无数据"=>''];
  2275. else{
  2276. array_walk($cgdlist, function (&$item) {
  2277. $item['采购单可关联金额']= bcsub($item['总货款'],InvoiceOrder::getInvoiceOrderTotalFee($item['采购单编号']),2);
  2278. });
  2279. }
  2280. excelExport("{$param['payNo']}对账采购单详情",array_keys($cgdlist[0]),$cgdlist);
  2281. }
  2282. /** 导出对账单采购单信息
  2283. */
  2284. public function payTrackExport(){
  2285. $param = $this->request->only(['payNo'=>''],'post','trim');
  2286. $valide=Validate::rule([
  2287. 'payNo|对账申请编号'=>'require|max:255',
  2288. ]);
  2289. if($valide->check($param)==false)return error_show(1004,$valide->getError());
  2290. $payinfo =Db::name('pay')->where(['payNo'=>$param['payNo'],'is_del'=>0,'pay_type'=>2])->findOrEmpty();
  2291. if(empty($payinfo)) return error_show(1005,'对账单信息未找到');
  2292. $cgdlist =Pay::alias("e")
  2293. ->leftJoin("pay_info a","a.payNo=e.payNo and a.status=1 and a.is_del=0")
  2294. ->leftJoin("track b","a.cgdNo=b.TrackCode")
  2295. ->leftJoin("fhd_child c","c.outChildCode=b.outChildCode")
  2296. ->where(['e.payNo'=>$param['payNo']])
  2297. ->field("a.payNo 对账单号,
  2298. e.total_fee 对账单总金额,
  2299. e.status 对账单状态,
  2300. e.apply_name 对账单创建人,
  2301. e.addtime 对账单创建时间,
  2302. b.TrackCode 发货运单号,
  2303. b.outChildCode 发货工单号,
  2304. c.outCode 发货单号,
  2305. c.num 发货数量,
  2306. b.orderCode 销售单号,
  2307. b.cgdNo 采购单号,
  2308. c.order_source 订单来源,
  2309. c.order_type 商品类型,
  2310. c.good_name 商品名称,
  2311. b.supplierNo 物流供应商编号,
  2312. b.supplierName 物流供应商名称,
  2313. b.companyNo 业务公司编号,
  2314. b.companyName 业务公司名称,
  2315. b.post_fee 物流费,
  2316. b.apply_name 运费单创建人,
  2317. b.create_time 运费单创建时间")
  2318. ->withAttr("对账单状态",function ($v){
  2319. return Pay::$status[$v]??"";
  2320. })
  2321. ->withAttr('订单来源',function ($v){
  2322. return FhdChild::$order_source[$v]??'';
  2323. })
  2324. ->withAttr('商品类型',function ($v){
  2325. return FhdChild::$order_type[$v]??'';
  2326. })
  2327. ->select()->toArray();
  2328. // $this->success("",$cgdlist);
  2329. if(empty($cgdlist))$cgdlist=['暂无数据'=>''];
  2330. excelExport("{$param['payNo']}对账运费单详情",array_keys($cgdlist[0]),$cgdlist);
  2331. }
  2332. // /**校验供应商下面是否有操作账户
  2333. // * @param $supplierNo 供应商账户
  2334. // * @return array
  2335. // */
  2336. // private function checkSupplier($supplierNo){
  2337. // $companyinfo = UserHandle("/userCompanyBasicList", ["companyNo" =>$supplierNo]);
  2338. // if ($companyinfo['code'] != 0) return ["code"=>$companyinfo['code'],"message"=> $companyinfo['message']];
  2339. // if (!empty($companyinfo['data']) && $companyinfo['data']['count'] > 0) {
  2340. // return ["code"=>1004,"message"=> "供应商公司存在操作账户,请用供应商账户操作"];
  2341. // }
  2342. // else{
  2343. // return ["code"=>0,"message"=> "业务账户可操作"];
  2344. // }
  2345. // }
  2346. }