Receipt.php 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\Admin\controller;
  4. use app\BaseController;
  5. use think\facade\Db;
  6. use think\Request;
  7. class Receipt extends BaseController
  8. {
  9. /**
  10. * 显示资源列表
  11. *
  12. * @return \think\Response
  13. */
  14. public function list()
  15. {
  16. $post = $this->request->post();
  17. $token = isset($post['token']) ? trim($post['token']) : "";
  18. if ($token == "") {
  19. return error_show(101, 'token不能为空');
  20. }
  21. $effetc = VerifyTokens($token);
  22. if (!empty($effetc) && $effetc['code'] != 0) {
  23. return error_show($effetc['code'], $effetc['message']);
  24. }
  25. $condition = [['is_del',"=",0],['refund_status',"=",0]];
  26. $guserinfo =GetUserInfo($token);
  27. if(isset($guserinfo['code']) && $guserinfo['code']!=0){
  28. return error_show($guserinfo['code'],$guserinfo['message']);
  29. }
  30. $check = checkRole($guserinfo['data']['roleid'],52);
  31. if($check){
  32. $condition[]=["apply_id","=", $guserinfo['data']['id']];
  33. }
  34. $no = isset($post['companyNo']) && $post['companyNo'] != "" ? trim($post['companyNo']) : "";
  35. if ($no != "") {
  36. $condition[] = ["inv_in", "=", "{$no}"];
  37. }
  38. $status = isset($post['status']) && $post['status'] !== "" ? intval($post['status']) : "";
  39. if ($status !== "") {
  40. $condition[] = ["status", "=", "{$status}"];
  41. }
  42. $type_check = isset($post['type_check']) && $post['type_check'] != "" ? $post['type_check'] : "";
  43. if ($type_check != "") {
  44. $condition[] = ["type_check", "=", "{$type_check}"];
  45. }
  46. $starttime = isset($post['starttime']) && $post['starttime'] != "" ? $post['starttime'] : "";
  47. if ($starttime != "") {
  48. $starttime = $starttime." 00:00:00";
  49. $condition[] = ["addtime", ">=", "{$starttime}"];
  50. }
  51. $endtime = isset($post['endtime']) && $post['endtime'] != "" ? $post['endtime'] : "";
  52. if ($endtime != "") {
  53. $endtime = $endtime." 23:59:59";
  54. $condition[] = ["addtime", "<=", "{$endtime}"];
  55. }
  56. $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
  57. $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
  58. $count = Db::name("invoice_pool")->where($condition)->count();
  59. $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
  60. $page = $page >= $total ? intval($total) : $page;
  61. $list = Db::name("invoice_pool")->where($condition)->order("addtime desc")->page(intval($page), $size)->select();
  62. $data=[];
  63. foreach ($list as $value){
  64. $customer= Db::name("customer_info")->where("companyNo","=",$value['inv_in'])->field("companyName,contactor")
  65. ->find();
  66. $value['company'] = isset($customer['companyName']) ? $customer['companyName']:"";
  67. $value['contactor'] = isset($customer['contactor']) ? $customer['contactor']:"";
  68. $out = Db::name('company_info')->where('companyNo',"=",$value['inv_out'])->find();
  69. $value['kp_company'] =isset($out['company_name']) ? $out['company_name']:"";
  70. $post = Db::name("inv_post")->where("invNo","=",$value['invNo'])->find();
  71. $value['post_company']=isset($post['post_company'])&&$post['post_company']!="" ? $post['post_company']:"";
  72. $value['post_code']=isset($post['postNo'])&&$post['postNo']!="" ? $post['postNo']:"";
  73. $data[]=$value;
  74. }
  75. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  76. }
  77. /**
  78. * 显示创建资源表单页.
  79. *
  80. * @return \think\Response
  81. */
  82. public function create()
  83. {
  84. $post = $this->request->post();
  85. $token = isset($post['token']) ? trim($post['token']) : "";
  86. if ($token == "") {
  87. return error_show(101, 'token不能为空');
  88. }
  89. $effetc = VerifyTokens($token);
  90. if (!empty($effetc) && $effetc['code'] != 0) {
  91. return error_show($effetc['code'], $effetc['message']);
  92. }
  93. // $companyNo = isset($post['tradeNo'])&&$post['tradeNo']!=""?trim($post['tradeNo']):"";
  94. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
  95. $type = isset($post['type'])&&$post['type']!=""?intval($post['type']):1;
  96. $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  97. $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):"1";
  98. $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
  99. $userinfo = GetUserInfo($token);
  100. $username="";
  101. if($companyNo==""){
  102. return error_show(1004,"参数companyNo 不能为空");
  103. }
  104. $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
  105. if($inv_out==""){
  106. return error_show(1004,"参数inv_out 不能为空");
  107. }
  108. $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
  109. if(empty($out)){
  110. return error_show(1004,"未找到开票企业信息");
  111. }
  112. if(isset($userinfo['code'])&&$userinfo['code']==0){
  113. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  114. }
  115. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  116. if(empty($customer)){
  117. return error_show(1004,"未找到客户企业信息");
  118. }
  119. $good = isset($post['good'])&&is_array($post['good'])?$post['good']:"";
  120. if($good==""&& empty($good)){
  121. return error_show(1004,"商品信息不能为空");
  122. }
  123. $invoice=[];
  124. isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
  125. isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
  126. isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
  127. $post['invoice_bankNo']:"";
  128. isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
  129. isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
  130. $post['invoice_mobile']:"";
  131. isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
  132. $post['invoice_title']:"";
  133. $orderinfo=[];
  134. $orderNo=makeNo('ORD');
  135. $total=0;
  136. $fund_fee=0;
  137. $wfund_fee=0;
  138. $inv_fee=0;
  139. $winv_fee=0;
  140. // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
  141. // return error_show(1004,"企业信息联系电话格式错误");
  142. // }
  143. Db::startTrans();
  144. try{
  145. foreach ($good as $value){
  146. $temp=[];
  147. if(!isset($value['num'])||$value['num']==0){
  148. return error_show(1004,"商品数量不能为空");
  149. }
  150. if(!isset($value['total_fee'])||$value['total_fee']==0){
  151. return error_show(1004,"商品总金额不能为空");
  152. }
  153. $temp['orderNo'] = $orderNo;
  154. $temp['goodNo']= $value['sequenceNo'];
  155. $goocp = Db::name("qrd_list")->where("sequenceNo","=",$value['sequenceNo'])->find();
  156. $temp['good_name']= $goocp['goodname'];
  157. $temp['qrdNo'] = $goocp['qrdNo'];
  158. $temp['brand']= $goocp['brand'];
  159. $temp['unit']= $value['unit'];
  160. $temp['good_num']= $value['num'];
  161. $temp['price']= $goocp['price'];
  162. $temp['total_fee']= $value['total_fee'];
  163. $temp['wfund_fee'] =$value['total_fee'];
  164. $temp['winv_fee'] = $value['total_fee'];
  165. $temp['other_fee']= 0;
  166. $temp['status']= 1;
  167. $temp['remark']= isset($value['remark']) ? $value['remark'] : "";
  168. $temp['addtime']= date("Y-m-d H:i:s");
  169. $temp['updatetime']= date("Y-m-d H:i:s");
  170. $total+= $value['total_fee'];
  171. $fund_fee+= $goocp['apay_fee'];
  172. $winv_fee+= $goocp['winv_fee'];
  173. $inv_fee+= $goocp['ainv_fee'];
  174. $wfund_fee+= $goocp['wpay_fee'];
  175. $orderinfo[]=$temp;
  176. }
  177. if(!empty($invoice) && $invoice!=''){
  178. $invoice['updatetime'] = date("Y-m-d H:i:s");
  179. $invoice['checkNo'] = $companyNo;
  180. if($inv_add==1){
  181. $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
  182. if($cust){
  183. Db::rollback();
  184. return error_show(1005,"企业财务信息已存在");
  185. }
  186. $invoice['addtime'] = date("Y-m-d H:i:s");
  187. Db::name('customer_invoice')->insert($invoice);
  188. }else{
  189. Db::name('customer_invoice')->where('invoice_code',"=",$invoice['invoice_code'])->save($invoice);
  190. }
  191. }
  192. $orderins = Db::name("order_info")->insertAll($orderinfo);
  193. //if($total<0){
  194. // Db::rollback();
  195. // return error_show(1005,"发票核销数据为不能为负值");
  196. // }
  197. if($orderins) {
  198. $order = [
  199. "orderNo" => $orderNo,
  200. "companyNo" => $companyNo,
  201. "sales_id" => $effetc['data']['user']['id'],
  202. "sales_name" => $username,
  203. "total_amount" => $total,
  204. "inv_status" => 1,
  205. "fund_status" => 1,
  206. "fund_fee"=>0,
  207. "wfund_fee"=>$total,
  208. "inv_fee"=>0,
  209. "inv_used"=>$total,
  210. "winv_fee"=>0,
  211. "status" => 0,
  212. "type" => $type,
  213. "addtime" => date("Y-m-d H:i:s"),
  214. "updatetime" => date("Y-m-d H:i:s")
  215. ];
  216. $orderpool = Db::name("order_pool")->insert($order, true);
  217. if ($orderpool > 0) {
  218. $invNo = makeNo("INV");
  219. $inv = [
  220. "invNo" => $invNo,
  221. "inv_value" => $total,
  222. "inv_out" => $inv_out,
  223. "inv_in" => $companyNo,
  224. "apply_id" =>$effetc['data']['user']['id'],
  225. "apply_name" => $username,
  226. "status" => 0,
  227. "worder_fee"=>0,
  228. "order_used"=>$total,
  229. "wtrade_fee"=>$total,
  230. "remark" => $remark,
  231. "invoice_title" => $invoice['invoice_title'],
  232. "invoice_addr" => $invoice['invoice_addr'],
  233. "invoice_mobile" => $invoice['invoice_mobile'],
  234. "invoice_code" => $invoice['invoice_code'],
  235. "invoice_bank" => $invoice['invoice_bank'],
  236. "invoice_bankNo" => $invoice['invoice_bankNo'],
  237. "inv_add" => $inv_add,
  238. "is_order" =>1,
  239. "inv_type"=>$inv_type,
  240. "type_check"=>$type,
  241. "addtime" => date("Y-m-d H:i:s"),
  242. "updatetime" => date("Y-m-d H:i:s")
  243. ];
  244. $invinsert = Db::name("invoice_pool")->insert($inv);
  245. if ($invinsert) {
  246. if($type==2){
  247. Db::commit();
  248. return app_show(0,"发票新建成功");
  249. }
  250. $assocNo = makeNo("ASC");
  251. $asslog = [
  252. "assocNo"=>$assocNo,
  253. "type"=>1,
  254. "apply_id" =>$effetc['data']['user']['id'],
  255. "apply_name" => $username,
  256. "companyNo"=>$companyNo,
  257. "codeNo"=>$orderNo,
  258. "rela_form"=>1,
  259. "total_fee"=>$total,
  260. "balance"=>0,
  261. "cancel_total"=>$total,
  262. "status"=>1,
  263. "source"=>2,
  264. "addtime"=>date("Y-m-d H:i:s"),
  265. "updatetime"=>date("Y-m-d H:i:s")
  266. ];
  267. $logassc = Db::name("assoc_key")->insert($asslog);
  268. if($logassc){
  269. $assrela =[
  270. "assocNo"=>$assocNo,
  271. "viceNo"=>$invNo,
  272. "vice_total"=>$total,
  273. "vice_fee"=>0,
  274. "cancel_fee"=>$total,
  275. "addtime"=>date("Y-m-d H:i:s"),
  276. "updatetime"=>date("Y-m-d H:i:s")
  277. ];
  278. $relas = Db::name("assoc_rela")->insert($assrela);
  279. if($relas){
  280. $resula= $this->updateQrd($orderNo,$total);
  281. if($resula){
  282. Db::commit();
  283. return app_show(0,"发票新建申请成功");
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. Db::rollback();
  291. return error_show(1004,"发票新建申请失败");
  292. }catch (\Exception $e) {
  293. Db::rollback();
  294. return error_show(1004, $e->getMessage());
  295. }
  296. }
  297. /**
  298. * 保存新建的资源
  299. * @param \think\Request $request
  300. * @return \think\Response
  301. */
  302. public function save()
  303. {
  304. $post = $this->request->post();
  305. $token = isset($post['token']) ? trim($post['token']) : "";
  306. if ($token == "") {
  307. return error_show(101, 'token不能为空');
  308. }
  309. $effetc = VerifyTokens($token);
  310. if (!empty($effetc) && $effetc['code'] != 0) {
  311. return error_show($effetc['code'], $effetc['message']);
  312. }
  313. // $customer_w = isset($post['customer'])&& $post['customer']!='' ? $post['customer'] : "";
  314. $invNo = isset($post['invNo'])&&$post['invNo']!=""?trim($post['invNo']):"";
  315. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
  316. $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):1;
  317. $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
  318. $type = isset($post['type'])&&$post['type']!=""?intval($post['type']):1;
  319. $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  320. $userinfo = GetUserInfo($token);
  321. $username="";
  322. if($companyNo==""){
  323. return error_show(1004,"参数companyNo 不能为空");
  324. }
  325. if($invNo==""){
  326. return error_show(1004,"参数invNo 不能为空");
  327. }
  328. if(isset($userinfo['code'])&&$userinfo['code']==0){
  329. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  330. }
  331. $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
  332. if($inv_out==""){
  333. return error_show(1004,"参数inv_out 不能为空");
  334. }
  335. $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
  336. if(empty($out)){
  337. return error_show(1004,"未找到开票企业信息");
  338. }
  339. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  340. if(empty($customer)){
  341. return error_show(1004,"未找到客户企业信息");
  342. }
  343. $invinfo =Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$invNo]])->find();
  344. if(empty($invinfo)){
  345. return error_show(1004,"发票信息未找到");
  346. }
  347. if($invinfo['status']>=3){
  348. return error_show(1004,"发票信息已通过财务审核,无法修改!");
  349. }
  350. $good = isset($post['good'])&&is_array($post['good'])?$post['good']:"";
  351. if($good==""&& empty($good)){
  352. return error_show(1004,"商品信息不能为空");
  353. }
  354. $assc = Db::name("assoc_list")->where([["viceNo","=",$invNo],["is_del","=",0],["type","=",1]])->find();
  355. if(empty($assc)){
  356. return error_show(1005,"未找到对应的关联数据");
  357. }
  358. $invoice=[];
  359. isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
  360. isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
  361. isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
  362. $post['invoice_bankNo']:"";
  363. isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
  364. isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
  365. $post['invoice_mobile']:"";
  366. isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
  367. $post['invoice_title']:"";
  368. // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
  369. // return error_show(1004,"企业信息联系电话格式错误");
  370. // }
  371. $total=0;
  372. $fund_fee=0;
  373. $wfund_fee=0;
  374. $inv_fee=0;
  375. $winv_fee=0;
  376. Db::startTrans();
  377. try{
  378. if(!empty($invoice) && $invoice!='') {
  379. $invoice['updatetime'] = date("Y-m-d H:i:s");
  380. $invoice['checkNo'] = $companyNo;
  381. if ($inv_add == 1) {
  382. $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
  383. if($cust){
  384. Db::rollback();
  385. return error_show(1005,"企业财务信息已存在");
  386. }
  387. $invoice['addtime'] = date("Y-m-d H:i:s");
  388. Db::name('customer_invoice')->insert($invoice);
  389. } else {
  390. Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
  391. }
  392. }
  393. // $reset = $this->qrd_delet($assc['codeNo'],$assc['cancel_total']);
  394. // if($reset){
  395. // Db::name("order_info")->where("orderNo","=",$assc['codeNo'])->save(["status"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  396. // }else{
  397. // Db::rollback();
  398. // return error_show(1004,"确认单数据编辑失败");
  399. // }
  400. foreach ($good as $value){
  401. // $temp['goodNo']= $value['sequenceNo'];
  402. // $temp['orderNo']= $assc['codeNo'];
  403. //$goocp = Db::name("qrd_list")->where("sequenceNo","=",$value['sequenceNo'])->find();
  404. $goocp = Db::name("order_info")->where([["orderNo","=",$assc['codeNo']],["goodNo","=",$value['sequenceNo']]])
  405. ->find();
  406. // $goocp['good_name']= $value['remark'];
  407. // $temp['qrdNo'] = $goocp['qrdNo'];
  408. // $temp['brand']= $goocp['brand'];
  409. // $temp['unit']= $goocp['unit'];
  410. // $temp['good_num']= $value['num'];
  411. // $temp['price']= $goocp['price'];
  412. // $temp['total_fee']= $value['total_fee'];
  413. // $temp['wfund_fee'] = $value['total_fee'];
  414. // $temp['winv_fee'] = $value['total_fee'];
  415. // $temp['other_fee']= 0;
  416. // $temp['status']=1;
  417. $goocp['remark']= isset($value['remark']) ? $value['remark'] : "";
  418. $goocp['updatetime']= date("Y-m-d H:i:s");
  419. // $temp['addtime']= date("Y-m-d H:i:s");
  420. // $fund_fee+= $goocp['apay_fee'];
  421. // $winv_fee+= $goocp['winv_fee'];
  422. // $inv_fee+= $goocp['ainv_fee'];
  423. // $wfund_fee+= $goocp['wpay_fee'];
  424. Db::name("order_info")->save($goocp);
  425. $total+= $value['total_fee'];
  426. }
  427. $order = [
  428. "companyNo" => $companyNo,
  429. "total_amount" => $total,
  430. "fund_fee"=>0,
  431. "wfund_fee"=>$total,
  432. "inv_fee"=>0,
  433. "inv_used"=>$total,
  434. "winv_fee"=>0,
  435. "updatetime" => date("Y-m-d H:i:s")
  436. ];
  437. // if($total<0){
  438. // Db::rollback();
  439. // return error_show(1005,"发票核销数据为不能为负值");
  440. // }
  441. $orderpool = Db::name("order_pool")->where("orderNo","=",$assc['codeNo'])->save($order);
  442. if ($orderpool) {
  443. $invinfo['inv_value'] = $total;
  444. $invinfo['inv_in'] = $companyNo;
  445. $invinfo['inv_out'] = $inv_out;
  446. $invinfo['worder_fee'] = 0;
  447. $invinfo['inv_type'] = $inv_type;
  448. $invinfo['type_check'] = $type;
  449. $invinfo['wtrade_fee'] = $total;
  450. $invinfo['order_used'] = $total;
  451. $invinfo['invoice_title'] = $invoice['invoice_title'];
  452. $invinfo['invoice_addr'] = $invoice['invoice_addr'];
  453. $invinfo['invoice_mobile'] = $invoice['invoice_mobile'];
  454. $invinfo['invoice_code'] = $invoice['invoice_code'];
  455. $invinfo['invoice_bank'] = $invoice['invoice_bank'];
  456. $invinfo['invoice_bankNo'] = $invoice['invoice_bankNo'];
  457. $invinfo['inv_add'] = $inv_add;
  458. $invinfo['remark']=$remark;
  459. $invinfo['updatetime'] =date("Y-m-d H:i:s");
  460. $invinsert = Db::name("invoice_pool")->save($invinfo);
  461. if($invinsert){
  462. $asslog = [
  463. "cancel_fee" => $total,
  464. "vice_fee" => 0,
  465. "vice_total" => 0,
  466. "updatetime" => date("Y-m-d H:i:s")
  467. ];
  468. $logassc = Db::name("assoc_rela")->where("id","=", $assc['id'])->save($asslog);
  469. if ($logassc) {
  470. $keyas=[
  471. "total_fee"=>$total,
  472. "balance"=>0,
  473. "cancel_total"=>$total,
  474. "updatetime"=>date("Y-m-d H:i:s")
  475. ];
  476. $relas = Db::name("assoc_key")->where("id","=",$assc['aid'])->save($keyas);
  477. if($relas){
  478. // $resula= $this->updateQrd($assc['codeNo'],$total);
  479. // if($resula){
  480. Db::commit();
  481. return app_show(0,"发票编辑成功");
  482. // }
  483. }
  484. }
  485. }
  486. }
  487. Db::rollback();
  488. return error_show(1004,"发票编辑失败");
  489. }catch (\Exception $e) {
  490. Db::rollback();
  491. return error_show(1004, $e->getMessage());
  492. }
  493. }
  494. /**
  495. * @return \think\response\Json|void
  496. * @throws \think\db\exception\DataNotFoundException
  497. * @throws \think\db\exception\DbException
  498. * @throws \think\db\exception\ModelNotFoundException
  499. * @throws \think\exception\DbException
  500. */
  501. public function CreateByTrade(){
  502. $post = $this->request->post();
  503. $token = isset($post['token']) ? trim($post['token']) : "";
  504. if ($token == "") {
  505. return error_show(101, 'token不能为空');
  506. }
  507. $effetc = VerifyTokens($token);
  508. if (!empty($effetc) && $effetc['code'] != 0) {
  509. return error_show($effetc['code'], $effetc['message']);
  510. }
  511. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
  512. $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  513. $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):"1";
  514. $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
  515. $userinfo = GetUserInfo($token);
  516. $username="";
  517. if($companyNo==""){
  518. return error_show(1004,"参数companyNo 不能为空");
  519. }
  520. if(isset($userinfo['code'])&&$userinfo['code']==0){
  521. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  522. }
  523. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  524. if(empty($customer)){
  525. return error_show(1004,"未找到客户企业信息");
  526. }
  527. $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
  528. if($inv_out==""){
  529. return error_show(1004,"参数inv_out 不能为空");
  530. }
  531. $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
  532. if(empty($out)){
  533. return error_show(1004,"未找到开票企业信息");
  534. }
  535. $traNo = isset($post['traNo'])&&$post['traNo']!=""?$post['traNo']:"";
  536. if($traNo==""){
  537. return error_show(1004,"参数traNo 不能为空");
  538. }
  539. $trade = Db::name("trade_pool")->where([["traNo","in",$traNo],["is_del","=",0],['status',"=",3]])->select()
  540. ->toArray();
  541. if(empty($trade)){
  542. return error_show(1004,"未找到资金信息");
  543. }
  544. $invoice=[];
  545. isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
  546. isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
  547. isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
  548. $post['invoice_bankNo']:"";
  549. isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
  550. isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
  551. $post['invoice_mobile']:"";
  552. isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
  553. $post['invoice_title']:"";
  554. // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
  555. // return error_show(1004,"企业信息联系电话格式错误");
  556. // }
  557. Db::startTrans();
  558. try{
  559. if(!empty($invoice) && $invoice!='') {
  560. $invoice['updatetime'] = date("Y-m-d H:i:s");
  561. $invoice['checkNo'] = $companyNo;
  562. if ($inv_add == 1) {
  563. $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
  564. if($cust){
  565. Db::rollback();
  566. return error_show(1005,"企业财务信息已存在");
  567. }
  568. $invoice['addtime'] = date("Y-m-d H:i:s");
  569. Db::name('customer_invoice')->insert($invoice);
  570. } else {
  571. Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
  572. }
  573. }
  574. $total=0;
  575. $relas=[];
  576. $assocNo = makeNo("ASC");
  577. $invNo = makeNo("INV");
  578. foreach ($trade as $value){
  579. if($value['winv_fee']<=0){
  580. continue;
  581. }
  582. $relas[] =[
  583. "assocNo"=>$assocNo,
  584. "viceNo"=>$value['traNo'],
  585. "vice_total"=>$value['total_fee'],
  586. "vice_fee"=>0,
  587. "cancel_fee"=>$value['winv_fee'],
  588. "addtime"=>date("Y-m-d H:i:s"),
  589. "updatetime"=>date("Y-m-d H:i:s")
  590. ];
  591. $total+=$value['winv_fee'];
  592. $value['inv_used']+=$value['winv_fee'];
  593. $value['winv_fee']=0;
  594. $value['updatetime']=date("Y-m-d H:i:s");
  595. Db::name("trade_pool")->save($value);
  596. }
  597. $inv = [
  598. "invNo" => $invNo,
  599. "inv_value" => $total,
  600. "inv_out" => $inv_out,
  601. "inv_in" => $companyNo,
  602. "apply_id" =>$effetc['data']['user']['id'],
  603. "apply_name" => $username,
  604. "status" => 0,
  605. "worder_fee"=>$total,
  606. "order_used"=>0,
  607. "fund_used"=>$total,
  608. "wtrade_fee"=>0,
  609. "remark" => $remark,
  610. "invoice_title" => $invoice['invoice_title'],
  611. "invoice_addr" => $invoice['invoice_addr'],
  612. "invoice_mobile" => $invoice['invoice_mobile'],
  613. "invoice_code" => $invoice['invoice_code'],
  614. "invoice_bank" => $invoice['invoice_bank'],
  615. "invoice_bankNo" => $invoice['invoice_bankNo'],
  616. "inv_add" => $inv_add,
  617. "is_order" =>0,
  618. "inv_type"=>$inv_type,
  619. "type_check"=>3,
  620. "addtime" => date("Y-m-d H:i:s"),
  621. "updatetime" => date("Y-m-d H:i:s")
  622. ];
  623. $invinsert = Db::name("invoice_pool")->insert($inv);
  624. if ($invinsert) {
  625. $asslog = [
  626. "assocNo"=>$assocNo,
  627. "type"=>2,
  628. "apply_id" =>$effetc['data']['user']['id'],
  629. "apply_name" => $username,
  630. "companyNo"=>$companyNo,
  631. "codeNo"=>$invNo,
  632. "rela_form"=>4,
  633. "total_fee"=>$total,
  634. "balance"=>0,
  635. "cancel_total"=>$total,
  636. "status"=>1,
  637. "source"=>2,
  638. "addtime"=>date("Y-m-d H:i:s"),
  639. "updatetime"=>date("Y-m-d H:i:s")
  640. ];
  641. $logassc = Db::name("assoc_key")->insert($asslog);
  642. if($logassc){
  643. $relas = Db::name("assoc_rela")->insertAll($relas);
  644. if($relas){
  645. Db::commit();
  646. return app_show(0,"发票新建申请成功");
  647. }
  648. }
  649. }
  650. Db::rollback();
  651. return error_show(1005,"发票新建申请失败");
  652. }catch (\Exception $e){
  653. Db::rollback();
  654. return error_show(1005,$e->getMessage());
  655. }
  656. }
  657. /**
  658. * @return \think\response\Json|void
  659. * @throws \think\db\exception\DataNotFoundException
  660. * @throws \think\db\exception\DbException
  661. * @throws \think\db\exception\ModelNotFoundException
  662. * @throws \think\exception\DbException
  663. */
  664. public function EditByTrain(){
  665. $post = $this->request->post();
  666. $token = isset($post['token']) ? trim($post['token']) : "";
  667. if ($token == "") {
  668. return error_show(101, 'token不能为空');
  669. }
  670. $effetc = VerifyTokens($token);
  671. if (!empty($effetc) && $effetc['code'] != 0) {
  672. return error_show($effetc['code'], $effetc['message']);
  673. }
  674. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
  675. $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  676. $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):"1";
  677. $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
  678. $invNo = isset($post['invNo'])&&$post['invNo']!=""?trim($post['invNo']):"";
  679. $userinfo = GetUserInfo($token);
  680. $username="";
  681. if($companyNo==""){
  682. return error_show(1004,"参数companyNo 不能为空");
  683. }
  684. if($invNo==""){
  685. return error_show(1004,"参数invNo 不能为空");
  686. }
  687. if(isset($userinfo['code'])&&$userinfo['code']==0){
  688. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  689. }
  690. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  691. if(empty($customer)){
  692. return error_show(1004,"未找到客户企业信息");
  693. }
  694. $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
  695. if($inv_out==""){
  696. return error_show(1004,"参数inv_out 不能为空");
  697. }
  698. $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
  699. if(empty($out)){
  700. return error_show(1004,"未找到开票企业信息");
  701. }
  702. $invinfo =Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$invNo]])->find();
  703. if(empty($invinfo)){
  704. return error_show(1004,"发票信息未找到");
  705. }
  706. if($invinfo['status']>=3){
  707. return error_show(1004,"发票信息已通过财务审核,无法修改!");
  708. }
  709. $invoice=[];
  710. isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
  711. isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
  712. isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
  713. $post['invoice_bankNo']:"";
  714. isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
  715. isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
  716. $post['invoice_mobile']:"";
  717. isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
  718. $post['invoice_title']:"";
  719. // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
  720. // return error_show(1004,"企业信息联系电话格式错误");
  721. // }
  722. Db::startTrans();
  723. try{
  724. if(!empty($invoice) && $invoice!='') {
  725. $invoice['updatetime'] = date("Y-m-d H:i:s");
  726. $invoice['checkNo'] = $companyNo;
  727. if ($inv_add == 1) {
  728. $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
  729. if($cust){
  730. Db::rollback();
  731. return error_show(1005,"企业财务信息已存在");
  732. }
  733. $invoice['addtime'] = date("Y-m-d H:i:s");
  734. Db::name('customer_invoice')->insert($invoice);
  735. } else {
  736. Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
  737. }
  738. }
  739. $invpool=[
  740. "invoice_title" => $invoice['invoice_title'],
  741. "invoice_addr" => $invoice['invoice_addr'],
  742. "invoice_mobile" => $invoice['invoice_mobile'],
  743. "invoice_code" => $invoice['invoice_code'],
  744. "invoice_bank" => $invoice['invoice_bank'],
  745. "invoice_bankNo" => $invoice['invoice_bankNo'],
  746. "inv_add" => $inv_add,
  747. "inv_out" => $inv_out,
  748. "remark"=>$remark,
  749. "updatetime"=>date("Y-m-d H:i:s")
  750. ];
  751. $up= Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$invNo]])->save($invpool);
  752. if($up){
  753. Db::commit();
  754. return app_show(0,"更新成功");
  755. }else{
  756. Db::rollback();
  757. return error_show(1005,"更新失败");
  758. }
  759. }catch(\Exception $e){
  760. Db::rollback();
  761. return error_show(1005,$e->getMessage());
  762. }
  763. }
  764. /**
  765. * 显示指定的资源
  766. *
  767. * @param int $id
  768. * @return \think\Response
  769. */
  770. public function read()
  771. {
  772. $post = $this->request->post();
  773. $token = isset($post['token']) ? trim($post['token']) : "";
  774. if ($token == "") {
  775. return error_show(101, 'token不能为空');
  776. }
  777. $effetc = VerifyTokens($token);
  778. if (!empty($effetc) && $effetc['code'] != 0) {
  779. return error_show($effetc['code'], $effetc['message']);
  780. }
  781. $invid = isset($post['id'])&&$post['id']!=""?intval($post['id']):"";
  782. if($invid==""){
  783. return error_show(1004,"参数id 不能为空");
  784. }
  785. $inv = Db::name("invoice_pool")->where("id","=",$invid)->find();
  786. if(empty($inv)){
  787. return error_show(1005,"未找到发票信息");
  788. }
  789. $customer = Db::name("customer_info")->where("companyNo","=",$inv['inv_in'])->find();
  790. $inv['customer']=empty($customer)? []:$customer;
  791. $kp_company = Db::name("company_info")->where("companyNo","=",$inv['inv_out'])->find();
  792. $inv['kp_company']=empty($kp_company)? []:$kp_company;
  793. $inv['order']=[];
  794. $assoc = Db::name("assoc_list")->where([["type","=",1],['viceNo',"=",$inv["invNo"]],["is_del","=",0]])
  795. ->find();
  796. if(!empty($assoc)){
  797. $order= Db::name("order_pool")->where([['orderNo',"=",$assoc['codeNo']]])->find();
  798. $list=[];
  799. if($order['type']==1){
  800. $good = Db::name('order_info')->where([['orderNo',"=",$assoc['codeNo']],["status","=",1]])->select();
  801. foreach ($good as $key=>$value){
  802. $info = Db::name("qrd_list")->where("sequenceNo","=",$value['goodNo'])->find();
  803. $value['num'] = intval($info['num']);
  804. $value['ainv_fee'] =sprintf("%.2f", $info['ainv_fee']);
  805. $value['winv_fee'] = sprintf("%.2f",$info['winv_fee']);
  806. $value['total'] = sprintf("%.2f",$info['total_fee']);
  807. $value['inv_num'] = intval($info['inv_num']);
  808. $list[]=$value;
  809. }
  810. }
  811. $order['good'] = $list;
  812. $inv['order']=$order;
  813. }
  814. $inv['trade']=[];
  815. $assoc= Db::name("assoc_list")->where([["type","=",2],["source","=",2],["is_del","=",0],["rela_form","=",4],["codeNo","=",$inv["invNo"]]])->select()->toArray();
  816. if(!empty($assoc)) {
  817. foreach ($assoc as $value) {
  818. $temp = Db::name("trade_list")->alias("a")->leftJoin("cfp_customer_info b","a.companyNo=b.companyNo")
  819. ->where([["a.is_del", "=", 0], ["a.traNo", "=", $value["viceNo"]]])->field("a.*,b.companyName")
  820. ->find();
  821. $inv['trade'][]=$temp;
  822. }
  823. }
  824. return app_show(0,"获取成功",$inv);
  825. }
  826. /**
  827. * 显示编辑资源表单页.
  828. *
  829. * @param int $id
  830. * @return \think\Response
  831. */
  832. public function add()
  833. {
  834. $post = $this->request->post();
  835. $token = isset($post['token']) ? trim($post['token']) : "";
  836. if ($token == "") {
  837. return error_show(101, 'token不能为空');
  838. }
  839. $effetc = VerifyTokens($token);
  840. if (!empty($effetc) && $effetc['code'] != 0) {
  841. return error_show($effetc['code'], $effetc['message']);
  842. }
  843. // $customer_w = isset($post['customer'])&& $post['customer']!='' ? $post['customer'] : "";
  844. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
  845. $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):1;
  846. $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
  847. $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  848. $userinfo = GetUserInfo($token);
  849. $username="";
  850. if($companyNo==""){
  851. return error_show(1004,"参数companyNo 不能为空");
  852. }
  853. if(isset($userinfo['code'])&&$userinfo['code']==0){
  854. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  855. }
  856. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  857. if(empty($customer)){
  858. return error_show(1004,"未找到客户企业信息");
  859. }
  860. $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
  861. if($inv_out==""){
  862. return error_show(1004,"参数inv_out 不能为空");
  863. }
  864. $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
  865. if(empty($out)){
  866. return error_show(1004,"未找到开票企业信息");
  867. }
  868. $invoice=[];
  869. isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
  870. isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
  871. isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
  872. $post['invoice_bankNo']:"";
  873. isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
  874. isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
  875. $post['invoice_mobile']:"";
  876. isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
  877. $post['invoice_title']:"";
  878. // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
  879. // return error_show(1004,"企业信息联系电话格式错误");
  880. // }
  881. $total = isset($post['total'])&&$post['total']!=""?$post['total']:"";
  882. if($total==""){
  883. return error_show(1004,"参数total 不能为空");
  884. }
  885. //if($total<0){
  886. // Db::rollback();
  887. // return error_show(1005,"发票核销数据为不能为负值");
  888. // }
  889. Db::startTrans();
  890. try{
  891. if(!empty($invoice) && $invoice!='') {
  892. $invoice['updatetime'] = date("Y-m-d H:i:s");
  893. $invoice['checkNo'] = $companyNo;
  894. if ($inv_add == 1) {
  895. $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
  896. if($cust){
  897. Db::rollback();
  898. return error_show(1005,"企业财务信息已存在");
  899. }
  900. $invoice['addtime'] = date("Y-m-d H:i:s");
  901. Db::name('customer_invoice')->insert($invoice);
  902. } else {
  903. Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
  904. }
  905. }
  906. $invNo = makeNo("INV");
  907. $inv = [
  908. "invNo" => $invNo,
  909. "inv_value" => $total,
  910. "inv_out" => $inv_out,
  911. "inv_in" => $companyNo,
  912. "apply_id" =>$effetc['data']['user']['id'],
  913. "apply_name" => $username,
  914. "status" => 0,
  915. "trade_status" => 1,
  916. "order_status" => 1,
  917. "worder_fee"=>$total,
  918. "wtrade_fee"=>$total,
  919. "invoice_title" => $invoice['invoice_title'],
  920. "invoice_addr" => $invoice['invoice_addr'],
  921. "invoice_mobile" => $invoice['invoice_mobile'],
  922. "invoice_code" => $invoice['invoice_code'],
  923. "invoice_bank" => $invoice['invoice_bank'],
  924. "invoice_bankNo" => $invoice['invoice_bankNo'],
  925. "inv_add" => $inv_add,
  926. "inv_type"=>$inv_type,
  927. "type_check"=>2,
  928. "is_order"=>0,
  929. "remark"=>$remark,
  930. "addtime" => date("Y-m-d H:i:s"),
  931. "updatetime" => date("Y-m-d H:i:s")
  932. ];
  933. $invinsert = Db::name("invoice_pool")->insert($inv);
  934. if($invinsert){
  935. Db::commit();
  936. return app_show(0,"发票创建成功");
  937. }
  938. Db::rollback();
  939. return error_show(1004,"发票新建失败");
  940. }catch (\Exception $e){
  941. Db::rollback();
  942. return error_show(1004,$e->getMessage());
  943. }
  944. }
  945. /**
  946. * 显示编辑资源表单页.
  947. *
  948. * @param int $id
  949. * @return \think\Response
  950. */
  951. public function edit()
  952. {
  953. $post = $this->request->post();
  954. $token = isset($post['token']) ? trim($post['token']) : "";
  955. if ($token == "") {
  956. return error_show(101, 'token不能为空');
  957. }
  958. $effetc = VerifyTokens($token);
  959. if (!empty($effetc) && $effetc['code'] != 0) {
  960. return error_show($effetc['code'], $effetc['message']);
  961. }
  962. // $companyNo = isset($post['tradeNo'])&&$post['tradeNo']!=""?trim($post['tradeNo']):"";
  963. $invNo = isset($post['invNo'])&&$post['invNo']!=""?trim($post['invNo']):"";
  964. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
  965. $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):1;
  966. $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
  967. $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  968. // $customer_w = isset($post['customer'])&& $post['customer']!='' ? $post['customer'] : "";
  969. $userinfo = GetUserInfo($token);
  970. $username="";
  971. if($companyNo==""){
  972. return error_show(1004,"参数companyNo 不能为空");
  973. }
  974. if($invNo==""){
  975. return error_show(1004,"参数companyNo 不能为空");
  976. }
  977. if(isset($userinfo['code'])&&$userinfo['code']==0){
  978. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  979. }
  980. $inv_out = isset($post['inv_out'])&&$post['inv_out']!=""?trim($post['inv_out']):"";
  981. if($inv_out==""){
  982. return error_show(1004,"参数inv_out 不能为空");
  983. }
  984. $out = Db::name('company_info')->where([['companyNo',"=",$inv_out],['status',"=",1]])->find();
  985. if(empty($out)){
  986. return error_show(1004,"未找到开票企业信息");
  987. }
  988. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  989. if(empty($customer)){
  990. return error_show(1004,"未找到客户企业信息");
  991. }
  992. $invinfo =Db::name("invoice_pool")->where("invNo","=",$invNo)->find();
  993. if(empty($invinfo)){
  994. return error_show(1004,"发票信息未找到");
  995. }
  996. if($invinfo['status']>=3){
  997. return error_show(1004,"发票信息已通过财务审核,无法修改!");
  998. }
  999. $total = isset($post['total'])&&$post['total']!=""?$post['total']:"";
  1000. if($total==""){
  1001. return error_show(1004,"参数total 不能为空");
  1002. }
  1003. //if($total<0){
  1004. // Db::rollback();
  1005. // return error_show(1005,"发票核销数据为不能为负值");
  1006. // }
  1007. isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $customer['invoice_addr'] = $post['invoice_addr']:"";
  1008. $invoice=[];
  1009. isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
  1010. isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
  1011. isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
  1012. $post['invoice_bankNo']:"";
  1013. isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
  1014. isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
  1015. $post['invoice_mobile']:"";
  1016. isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
  1017. $post['invoice_title']:"";
  1018. // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
  1019. // return error_show(1004,"企业信息联系电话格式错误");
  1020. // }
  1021. $invinfo['inv_value']=$total;
  1022. $invinfo['worder_fee']=$total;
  1023. $invinfo['wtrade_fee']=$total;
  1024. $invinfo['inv_in']=$companyNo;
  1025. $invinfo['inv_out']=$inv_out;
  1026. $invinfo['invoice_title'] = $invoice['invoice_title'];
  1027. $invinfo['invoice_addr'] = $invoice['invoice_addr'];
  1028. $invinfo['invoice_mobile'] = $invoice['invoice_mobile'];
  1029. $invinfo['invoice_code'] = $invoice['invoice_code'];
  1030. $invinfo['invoice_bank'] = $invoice['invoice_bank'];
  1031. $invinfo['invoice_bankNo'] = $invoice['invoice_bankNo'];
  1032. $invinfo['inv_type']=$inv_type;
  1033. $invinfo['inv_add']=$inv_add;
  1034. $invinfo['remark']=$remark;
  1035. $invinfo['updatetime']= date("Y-m-d H:i:s");
  1036. Db::startTrans();
  1037. try{
  1038. if(!empty($invoice) && $invoice!='') {
  1039. $invoice['updatetime'] = date("Y-m-d H:i:s");
  1040. $invoice['checkNo'] = $companyNo;
  1041. if ($inv_add == 1) {
  1042. $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
  1043. if($cust){
  1044. Db::rollback();
  1045. return error_show(1005,"企业财务信息已存在");
  1046. }
  1047. $invoice['addtime'] = date("Y-m-d H:i:s");
  1048. Db::name('customer_invoice')->insert($invoice);
  1049. } else {
  1050. Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
  1051. }
  1052. }
  1053. $invinsert = Db::name("invoice_pool")->save($invinfo);
  1054. if($invinsert){
  1055. Db::commit();
  1056. return app_show(0,"发票编辑成功");
  1057. }
  1058. Db::rollback();
  1059. return error_show(1004,"发票编辑失败");
  1060. }catch (\Exception $e){
  1061. Db::rollback();
  1062. return error_show(1004,$e->getMessage());
  1063. }
  1064. }
  1065. /**
  1066. * 保存更新的资源
  1067. *
  1068. * @param \think\Request $request
  1069. * @param int $id
  1070. * @return \think\Response
  1071. */
  1072. public function status( )
  1073. {
  1074. $post = $this->request->post();
  1075. $token = isset($post['token']) ? trim($post['token']) : "";
  1076. if ($token == "") {
  1077. return error_show(101, 'token不能为空');
  1078. }
  1079. $effetc = VerifyTokens($token);
  1080. if (!empty($effetc) && $effetc['code'] != 0) {
  1081. return error_show($effetc['code'], $effetc['message']);
  1082. }
  1083. $invNo=isset($post['invNo'])&& $post['invNo']!=""? trim($post['invNo']):"";
  1084. if($invNo==""){
  1085. return error_show(1004,"参数invNo 不能为空");
  1086. }
  1087. $invinfo = Db::name("invoice_pool")->where("invNo","=",$invNo)->find();
  1088. if(empty($invinfo)){
  1089. return error_show(1003,"未找到发票数据");
  1090. }
  1091. $status = isset($post['status'])&&$post['status']!==""? intval($post['status']):"";
  1092. $remark = isset($post['remark'])&&$post['remark']!==""? trim($post['remark']):"";
  1093. if($status===""){
  1094. return error_show(1005,"参数status 不能为空");
  1095. }
  1096. if($status==$invinfo['status']){
  1097. return error_show(1005,"状态已修改");
  1098. }
  1099. if(!in_array($status,[0,1,2,3,4,5])){
  1100. return error_show(1005,"参数status 无效");
  1101. }
  1102. if($status===0&&$invinfo['status']>=3){
  1103. return error_show(1005,"发票已通过审核无法取消申请!");
  1104. }
  1105. // if($status===1&&$invinfo['status']>=3){
  1106. // return error_show(1005,"发票已通过审核无法取消申请!");
  1107. // }
  1108. Db::startTrans();
  1109. if($status==4){
  1110. $invlist = isset($post['inv_info'])&&$post['inv_info']!="" ? $post['inv_info'] :"";
  1111. if($invlist==""){
  1112. Db::rollback();
  1113. return error_show(1004,"参数inv_info 不能为空");
  1114. }
  1115. }
  1116. $invinfo['status']=$status;
  1117. $invinfo['exam_remark']=$remark;
  1118. $invinfo['updatetime']=date("Y-m-d H:i:s");
  1119. $update =Db::name("invoice_pool")->save($invinfo);
  1120. if($update){
  1121. if($status==5) {
  1122. if ($invinfo['type_check'] == 1) {
  1123. $assinfo = Db::name("assoc_list")->where([["viceNo", "=", $invNo], ["status", "=", 1], ["source",
  1124. "=", 2], ["is_del", "=", "0"]])->find();
  1125. $assoc = Db::name("assoc_key")->where("assocNo", "=", $assinfo['assocNo'])->save
  1126. (["status" => 3, "updatetime" => date("Y-m-d H:i:s")]);
  1127. if ($assoc) {
  1128. $invinfo['aorder_fee'] += $assinfo['cancel_fee'];
  1129. $invinfo['order_used'] -= $assinfo['cancel_fee'];
  1130. $invinfo['is_order'] = 1;
  1131. $invinfo['order_status'] = $invinfo['worder_fee'] == 0 ? 3 : 2;
  1132. Db::name("invoice_pool")->save($invinfo);
  1133. $orderpool = Db::name("order_pool")->where([["orderNo", "=", $assinfo['codeNo']],
  1134. ["status", "=", 0], ["is_del", "=", 0]])->find();
  1135. $orderpool['status'] = 1;
  1136. $orderpool['inv_fee'] += $assinfo['cancel_total'];
  1137. $orderpool['inv_used'] -= $assinfo['cancel_total'];
  1138. $orderpool['inv_status'] = $orderpool['winv_fee'] == 0 ? 3 : 2;
  1139. $orderpool['updatetime'] = date("Y-m-d H:i:s");
  1140. $pool = Db::name("order_pool")->save($orderpool);
  1141. if ($pool) {
  1142. $qrd = $this->qrd_sure($assinfo['codeNo'], $assinfo['cancel_fee']);
  1143. if ($qrd) {
  1144. Db::commit();
  1145. return app_show(0, "状态修改成功");
  1146. }
  1147. }
  1148. }
  1149. Db::rollback();
  1150. return error_show(1005, "状态修改失败");
  1151. }
  1152. if ($invinfo['type_check'] == 3) {
  1153. $assoc = Db::name("assoc_list")->where([["type", "=", 2], ["source", "=", 2], ["is_del", "=", 0], ["rela_form", "=", 4], ["codeNo", "=", $invNo]])->select()->toArray();
  1154. if (!empty($assoc)) {
  1155. $taotal = 0;
  1156. foreach ($assoc as $value) {
  1157. $temp = Db::name("trade_pool")->where([["is_del", "=", 0], ["traNo", "=", $value["viceNo"]]])->find();
  1158. $temp['inv_fee'] += $value['cancel_fee'];
  1159. $temp['inv_used']-= $value['cancel_fee'];
  1160. $temp['inv_status']= $temp['winv_fee']==0 ? 3 :2;
  1161. $temp['updatetime'] = date("Y-m-d H:i:s");
  1162. $taotal = $value['cancel_total'];
  1163. Db::name("trade_pool")->save($temp);
  1164. }
  1165. $invinfo['atrade_fee'] += $taotal;
  1166. $invinfo['fund_used'] -= $taotal;
  1167. $invinfo['trade_status'] = $invinfo['wtrade_fee'] == 0 ? 3 : 2;
  1168. Db::name("invoice_pool")->save($invinfo);
  1169. $ass_up = Db::name("assoc_key")->where([["type", "=", 2], ["source", "=", 2], ["is_del", "=", 0], ["rela_form", "=", 4], ["codeNo", "=", $invNo]])->save(["status" => 3, "updatetime" => date("Y-m-d H:i:s")]);
  1170. if ($ass_up) {
  1171. Db::commit();
  1172. return app_show(0, "状态修改成功");
  1173. }
  1174. }
  1175. Db::rollback();
  1176. return error_show(1005, "状态修改失败");
  1177. }
  1178. }
  1179. Db::commit();
  1180. return app_show(0,"状态修改成功");
  1181. }else{
  1182. Db::rollback();
  1183. return error_show(1005,"状态修改失败");
  1184. }
  1185. }
  1186. /**
  1187. * 删除指定资源
  1188. *
  1189. * @param int $id
  1190. * @return \think\Response
  1191. */
  1192. public function delete()
  1193. {
  1194. $post = $this->request->post();
  1195. $token = isset($post['token']) ? trim($post['token']) : "";
  1196. if ($token == "") {
  1197. return error_show(101, 'token不能为空');
  1198. }
  1199. $effetc = VerifyTokens($token);
  1200. if (!empty($effetc) && $effetc['code'] != 0) {
  1201. return error_show($effetc['code'], $effetc['message']);
  1202. }
  1203. $invNo =isset($post['invNo']) && $post['invNo']!="" ? trim($post['invNo']):"";
  1204. if($invNo==""){
  1205. return error_show(1003,"参数invNo 不能为空");
  1206. }
  1207. $inv = Db::name("invoice_pool")->where("invNo","=",$invNo)->find();
  1208. if(empty($inv)){
  1209. return error_show(1004,"未找到发票数据");
  1210. }
  1211. if($inv['status']>2){
  1212. return error_show(1004,"财务审核已通过无法取消");
  1213. }
  1214. Db::startTrans();
  1215. if($inv['type_check']==1){
  1216. $assoc= Db::name("assoc_list")->where([["type","=",1],["is_del","=",0],["rela_form","=",1],["viceNo","=",
  1217. $invNo]])
  1218. ->find();
  1219. if(!empty($assoc)) {
  1220. $update = Db::name("order_pool")->where('orderNo', "=", $assoc['codeNo'])->save(['is_del' => 1,
  1221. "updatetime" => date("Y-m-d H:i:s")]);
  1222. if (!$update) {
  1223. Db::rollback();
  1224. return error_show(1004, "订单删除失败");
  1225. }
  1226. $ass_up = Db::name("assoc_key")->where("id", "=", $assoc['aid'])->save(["is_del" => 1, "updatetime" => date("Y-m-d H:i:s")]);
  1227. if (!$ass_up) {
  1228. Db::rollback();
  1229. return error_show(1004, "订单发票关联表删除失败");
  1230. }
  1231. $del= $this->qrd_delet($assoc['codeNo'],$assoc['cancel_total']);
  1232. if(!$del){
  1233. Db::rollback();
  1234. return error_show(1006,"确认单数据修改失败");
  1235. }
  1236. }
  1237. }
  1238. if($inv['type_check']==3){
  1239. $assoc= Db::name("assoc_list")->where([["type","=",2],["source","=",2],["is_del","=",0],["rela_form","=",4],["codeNo","=",$invNo]])->select()->toArray();
  1240. if(!empty($assoc)) {
  1241. foreach ($assoc as $value){
  1242. $temp = Db::name("trade_pool")->where([["is_del", "=", 0], ["traNo", "=",$value["viceNo"]]])->find();
  1243. $temp['winv_fee'] += $value['cancel_fee'];
  1244. $temp['inv_used'] -= $value['cancel_fee'];
  1245. $temp['updatetime'] = date("Y-m-d H:i:s");
  1246. Db::name("trade_pool")->save($temp);
  1247. }
  1248. $ass_up = Db::name("assoc_key")->where([["type","=",2],["source","=",2],["is_del","=",0],["rela_form","=",4],["codeNo","=",$invNo]])->save(["is_del" => 1, "updatetime" => date("Y-m-d H:i:s")]);
  1249. if (!$ass_up) {
  1250. Db::rollback();
  1251. return error_show(1004, "订单发票关联表删除失败");
  1252. }
  1253. }
  1254. }
  1255. $inv['is_del']=1;
  1256. $inv['updatetime']=date("Y-m-d H:i:s");
  1257. $update = Db::name("invoice_pool")->save($inv);
  1258. if($update){
  1259. Db::commit();
  1260. return app_show(0,"取消申请成功");
  1261. }else{
  1262. Db::rollback();
  1263. return error_show(1003,"取消申请失败");
  1264. }
  1265. // return $update? app_show(0,"取消申请成功"):error_show(1003,"取消申请失败");
  1266. }
  1267. /**
  1268. * @return \think\response\Json|void
  1269. * @throws \think\db\exception\DataNotFoundException
  1270. * @throws \think\db\exception\DbException
  1271. * @throws \think\db\exception\ModelNotFoundException
  1272. * @throws \think\exception\DbException
  1273. */
  1274. public function InvoPost(){
  1275. $post = $this->request->post();
  1276. $token = isset($post['token']) ? trim($post['token']) : "";
  1277. if ($token == "") {
  1278. return error_show(101, 'token不能为空');
  1279. }
  1280. $effetc = VerifyTokens($token);
  1281. if (!empty($effetc) && $effetc['code'] != 0) {
  1282. return error_show($effetc['code'], $effetc['message']);
  1283. }
  1284. $invNo=isset($post['invNo'])&& $post['invNo']!=""? trim($post['invNo']):"";
  1285. if($invNo==""){
  1286. return error_show(1004,"参数invNo 不能为空");
  1287. }
  1288. $invinfo = Db::name("invoice_pool")->where("invNo","=",$invNo)->find();
  1289. if(empty($invinfo)){
  1290. return error_show(1003,"未找到发票数据");
  1291. }
  1292. $postcode = isset($post["code"])&&$post['code']!="" ? $post["code"] :"";
  1293. $postname = isset($post['company']) && $post['company']!="" ? $post['company']:"";
  1294. if ($postcode==""){
  1295. return error_show(1005,"参数code 不能为空");
  1296. }
  1297. if ($postname==""){
  1298. return error_show(1005,"参数company 不能为空");
  1299. }
  1300. // if($invinfo['status']!=4){
  1301. // return error_show(1005,"发票数据还未上传");
  1302. // }
  1303. $data=[
  1304. "postNo"=>$postcode,
  1305. "post_company"=>$postname,
  1306. "invNo"=>$invNo,
  1307. "contactor"=>"",
  1308. "mobile"=>"",
  1309. "status"=>1,
  1310. "addtime"=>date("y-m-d H:i:s"),
  1311. "updatetime"=>date("y-m-d H:i:s"),
  1312. ];
  1313. Db::startTrans();
  1314. $post= Db::name("inv_post")->insert($data);
  1315. if($post){
  1316. $invinfo['status']=5;
  1317. $invinfo["updatetime"]=date("Y-m-d H:i:s");
  1318. $sav= Db::name("invoice_pool")->save($invinfo);
  1319. if($sav){
  1320. if ($invinfo['type_check'] == 1) {
  1321. $assinfo = Db::name("assoc_list")->where([["viceNo", "=", $invNo], ["status", "=", 1], ["source",
  1322. "=", 2], ["is_del", "=", "0"]])->find();
  1323. $assoc = Db::name("assoc_key")->where("assocNo", "=", $assinfo['assocNo'])->save
  1324. (["status" => 3, "updatetime" => date("Y-m-d H:i:s")]);
  1325. if ($assoc) {
  1326. $invinfo['aorder_fee'] += $assinfo['cancel_fee'];
  1327. $invinfo['order_used'] -= $assinfo['cancel_fee'];
  1328. $invinfo['is_order'] = 1;
  1329. $invinfo['order_status'] = $invinfo['worder_fee'] == 0 ? 3 : 2;
  1330. Db::name("invoice_pool")->save($invinfo);
  1331. $orderpool = Db::name("order_pool")->where([["orderNo", "=", $assinfo['codeNo']],
  1332. ["status", "=", 0], ["is_del", "=", 0]])->find();
  1333. $orderpool['status'] = 1;
  1334. $orderpool['inv_fee'] += $assinfo['cancel_total'];
  1335. $orderpool['inv_used'] -= $assinfo['cancel_total'];
  1336. $orderpool['inv_status'] = $orderpool['winv_fee'] == 0 ? 3 : 2;
  1337. $orderpool['updatetime'] = date("Y-m-d H:i:s");
  1338. $pool = Db::name("order_pool")->save($orderpool);
  1339. if ($pool) {
  1340. $qrd = $this->qrd_sure($assinfo['codeNo'], $assinfo['cancel_fee']);
  1341. if ($qrd) {
  1342. Db::commit();
  1343. return app_show(0, "状态修改成功");
  1344. }
  1345. }
  1346. }
  1347. Db::rollback();
  1348. return error_show(1005, "状态修改失败");
  1349. }
  1350. if ($invinfo['type_check'] == 3) {
  1351. $assoc = Db::name("assoc_list")->where([["type", "=", 2], ["source", "=", 2], ["is_del", "=", 0], ["rela_form", "=", 4], ["codeNo", "=", $invNo]])->select()->toArray();
  1352. if (!empty($assoc)) {
  1353. $taotal = 0;
  1354. foreach ($assoc as $value) {
  1355. $temp = Db::name("trade_pool")->where([["is_del", "=", 0], ["traNo", "=", $value["viceNo"]]])->find();
  1356. $temp['inv_fee'] += $value['cancel_fee'];
  1357. $temp['inv_used']-= $value['cancel_fee'];
  1358. $temp['inv_status']= $temp['winv_fee']==0 ? 3 :2;
  1359. $temp['updatetime'] = date("Y-m-d H:i:s");
  1360. $taotal = $value['cancel_total'];
  1361. Db::name("trade_pool")->save($temp);
  1362. }
  1363. $invinfo['atrade_fee'] += $taotal;
  1364. $invinfo['fund_used'] -= $taotal;
  1365. $invinfo['trade_status'] = $invinfo['wtrade_fee'] == 0 ? 3 : 2;
  1366. Db::name("invoice_pool")->save($invinfo);
  1367. $ass_up = Db::name("assoc_key")->where([["type", "=", 2], ["source", "=", 2], ["is_del", "=", 0], ["rela_form", "=", 4], ["codeNo", "=", $invNo]])->save(["status" => 3, "updatetime" => date("Y-m-d H:i:s")]);
  1368. if ($ass_up) {
  1369. Db::commit();
  1370. return app_show(0, "状态修改成功");
  1371. }
  1372. }
  1373. Db::rollback();
  1374. return error_show(1005, "状态修改失败");
  1375. }
  1376. Db::commit();
  1377. return app_show(0,"添加成功");
  1378. }else{
  1379. Db::rollback();
  1380. return error_show(1003,"添加失败");
  1381. }
  1382. }
  1383. Db::rollback();
  1384. return error_show(1003,"添加失败");
  1385. }
  1386. /**
  1387. * @return \think\response\Json|void
  1388. */
  1389. public function GetReceipt(){
  1390. $post = $this->request->post();
  1391. $token = isset($post['token']) ? trim($post['token']) : "";
  1392. if ($token == "") {
  1393. return error_show(101, 'token不能为空');
  1394. }
  1395. $effetc = VerifyTokens($token);
  1396. if (!empty($effetc) && $effetc['code'] != 0) {
  1397. return error_show($effetc['code'], $effetc['message']);
  1398. }
  1399. $invNo=isset($post['inv_in'])&& $post['inv_in']!=""? trim($post['inv_in']):"";
  1400. if($invNo==""){
  1401. return error_show(1004,"参数inv_in 不能为空");
  1402. }
  1403. $type = isset($post['type_check'])&& $post['type_check']!=""? trim($post['type_check']) : 2;
  1404. $condition = [["inv_in","=",$invNo],["type_check","=",$type],["lstatus","=",1]];
  1405. $list = Db::name("inv_list")->where($condition)->select();
  1406. return app_show(0,"获取成功",$list);
  1407. }
  1408. /**
  1409. * 显示资源列表
  1410. *
  1411. * @return \think\Response
  1412. */
  1413. public function recepitlist()
  1414. {
  1415. $post = $this->request->post();
  1416. $token = isset($post['token']) ? trim($post['token']) : "";
  1417. if ($token == "") {
  1418. return error_show(101, 'token不能为空');
  1419. }
  1420. $effetc = VerifyTokens($token);
  1421. if (!empty($effetc) && $effetc['code'] != 0) {
  1422. return error_show($effetc['code'], $effetc['message']);
  1423. }
  1424. $condition = [['is_del',"=",0],['refund_status',"=",0]];
  1425. $guserinfo =GetUserInfo($token);
  1426. if(isset($guserinfo['code']) && $guserinfo['code']!=0){
  1427. return error_show($guserinfo['code'],$guserinfo['message']);
  1428. }
  1429. $check = checkRole($guserinfo['data']['roleid'],55);
  1430. if($check){
  1431. $condition[]=["apply_id","=", $guserinfo['data']['id']];
  1432. }
  1433. $no = isset($post['companyNo']) && $post['companyNo'] != "" ? trim($post['companyNo']) : "";
  1434. if ($no != "") {
  1435. $condition[] = ["inv_in", "=", "{$no}"];
  1436. }
  1437. $invNo = isset($post['invNo']) && $post['invNo'] != "" ? trim($post['invNo']) : "";
  1438. if ($invNo != "") {
  1439. $condition[] = ["invNo", "like", "%{$invNo}%"];
  1440. }
  1441. $type_check = isset($post['type_check']) && $post['type_check'] != "" ? $post['type_check'] : "";
  1442. if ($type_check != "") {
  1443. $condition[] = ["type_check", "=", "{$type_check}"];
  1444. }
  1445. $status = isset($post['status']) && $post['status'] != "" ? $post['status'] : "";
  1446. if ($status != "") {
  1447. $condition[] = ["status", "=", "{$status}"];
  1448. }
  1449. $order_status = isset($post['order_status']) && $post['order_status'] != "" ? intval($post['order_status']) :
  1450. "";
  1451. if ($order_status != "") {
  1452. if($order_status==4){
  1453. $condition[] = ["order_status", "in", [1,2]];
  1454. }else{
  1455. $condition[] = ["order_status", "=", "{$order_status}"];
  1456. }
  1457. // $condition[] = ["order_status", "=", "{$order_status}"];
  1458. }
  1459. $trade_status = isset($post['trade_status']) && $post['trade_status'] != "" ? intval($post['trade_status']) : "";
  1460. if ($trade_status != "") {
  1461. if($trade_status==4){
  1462. $condition[] = ["trade_status", "in", [1,2]];
  1463. }else{
  1464. $condition[] = ["trade_status", "=", "{$trade_status}"];
  1465. }
  1466. }
  1467. $total_low = isset($post['total_low']) && $post['total_low'] != "" ? $post['total_low'] : "";
  1468. if ($total_low != "") {
  1469. $condition[] = ["inv_value", ">=", "{$total_low}"];
  1470. }
  1471. $total_up = isset($post['total_up']) && $post['total_up'] != "" ? $post['total_up'] : "";
  1472. if ($total_up != "") {
  1473. $condition[] = ["inv_value", "<=", "{$total_up}"];
  1474. }
  1475. $starttime = isset($post['starttime']) && $post['starttime'] != "" ? $post['starttime'] : "";
  1476. if ($starttime != "") {
  1477. $condition[] = ["addtime", ">=", "{$starttime}"];
  1478. }
  1479. $endtime = isset($post['endtime']) && $post['endtime'] != "" ? $post['endtime'] : "";
  1480. if ($endtime != "") {
  1481. $condition[] = ["addtime", "<=", "{$endtime}"];
  1482. }
  1483. $type = isset($post['type_check'])&& $post['type_check']!=""? $post['type_check'] : "";
  1484. if ($type != "") {
  1485. $condition[] = ["type_check", "=", "{$type}"];
  1486. }
  1487. $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
  1488. $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
  1489. $count = Db::name("invoice_pool")->where($condition)->count();
  1490. $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
  1491. $page = $page >= $total ? intval($total) : $page;
  1492. $list = Db::name("invoice_pool")->where($condition)->order("addtime desc")->page(intval($page), $size)->select();
  1493. $data=[];
  1494. foreach ($list as $value){
  1495. $customer= Db::name("customer_info")->where("companyNo","=",$value['inv_in'])->find();
  1496. $value['company'] = isset($customer['companyName']) ? $customer['companyName']:"";
  1497. $value['contactor'] = isset($customer['contactor']) ? $customer['contactor']:"";
  1498. // $value['invoice_bankNo'] = isset($customer['invoice_bankNo']) ? $customer['invoice_bankNo']:"";
  1499. $value['mobile'] = isset($customer['mobile']) ? $customer['mobile']:"";
  1500. $out = Db::name('company_info')->where('companyNo',"=",$value['inv_out'])->find();
  1501. $value['kp_company'] =isset($out['company_name']) ? $out['company_name']:"";
  1502. $data[]=$value;
  1503. }
  1504. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  1505. }
  1506. /**
  1507. * @param $orderNo
  1508. * @param $balance
  1509. * @return bool
  1510. * @throws \think\db\exception\DataNotFoundException
  1511. * @throws \think\db\exception\DbException
  1512. * @throws \think\db\exception\ModelNotFoundException
  1513. */
  1514. private function updateQrd($orderNo,$balance)
  1515. {
  1516. $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=", 0]])->find();
  1517. if (empty($orderinfo)) {
  1518. return false;
  1519. }
  1520. $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
  1521. if (empty($orderlist)) {
  1522. return false;
  1523. }
  1524. foreach ($orderlist as $value) {
  1525. if($balance==0){
  1526. break;
  1527. }
  1528. if($value['winv_fee'] == 0){
  1529. continue;
  1530. }
  1531. $order_fee=0;
  1532. if($value['winv_fee']<=$balance){
  1533. $order_fee = $value['winv_fee'];
  1534. $balance -=$value['winv_fee'];
  1535. $value['inv_fee'] += $value['winv_fee'];
  1536. $value['winv_fee'] = 0;
  1537. }else{
  1538. $value['inv_fee'] += $balance;
  1539. $value['winv_fee']-= $balance;
  1540. $order_fee = $balance;
  1541. $balance=0;
  1542. }
  1543. $value['updatetime'] = date("Y-m-d H:i:s");
  1544. $orde = Db::name("order_info")->save($value);
  1545. if($orde===false){
  1546. return false;
  1547. }
  1548. $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
  1549. if (($order_fee <= $qrdinfo['Number1618249205231'])||($order_fee <=
  1550. $qrdinfo['Number1618249202608']&&$orderinfo['type']==4)) {
  1551. $qrdinfo['inving_fee'] += $order_fee;
  1552. if($orderinfo['type']==4){
  1553. $qrdinfo['Number1618249202608'] -= $order_fee;
  1554. }else{
  1555. $qrdinfo['Number1618249205231'] -= $order_fee;
  1556. $qrdinfo['number1618249258956'] += $value['good_num'];
  1557. }
  1558. $qrdinfo['status'] =$qrdinfo['ShortText1618559274859']==1 &&$qrdinfo['ShortText1618559043560']==1 ? 0:1 ;
  1559. $qinfo = Db::name("qrd_info")->save($qrdinfo);
  1560. if ($qinfo) {
  1561. $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
  1562. if(($qrdp['winv_fee'] >= $order_fee)|| ($qrdp['ainv_fee'] >= $order_fee &&$orderinfo['type']==4
  1563. )) {
  1564. if($orderinfo['type']==4){
  1565. $qrdp['ainv_fee'] -= $order_fee;
  1566. $qrdp['inv_fee'] += $order_fee;
  1567. }else{
  1568. $qrdp['winv_fee'] -= $order_fee;
  1569. $qrdp['inv_fee'] += $order_fee;
  1570. }
  1571. // $qrdp['inv_status'] = $qrdp['winv_fee'] == 0 ? 3 : 2;
  1572. $qrd = Db::name("qrd")->save($qrdp);
  1573. if(!$qrd){
  1574. return false;
  1575. }
  1576. }else {
  1577. return false;
  1578. }
  1579. }else {
  1580. return false;
  1581. }
  1582. } else {
  1583. return false;
  1584. }
  1585. }
  1586. return true;
  1587. }
  1588. /**
  1589. * @param $orderNo
  1590. * @param $balance
  1591. * @return bool
  1592. * @throws \think\db\exception\DataNotFoundException
  1593. * @throws \think\db\exception\DbException
  1594. * @throws \think\db\exception\ModelNotFoundException
  1595. */
  1596. private function qrd_sure($orderNo,$balance){
  1597. $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=", 0]])->find();
  1598. if (empty($orderinfo)) {
  1599. return false;
  1600. }
  1601. $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
  1602. if (empty($orderlist)) {
  1603. return false;
  1604. }
  1605. foreach ($orderlist as $value) {
  1606. if($balance==0){
  1607. break;
  1608. }
  1609. if($value['inv_fee'] == 0){
  1610. continue;
  1611. }
  1612. $order_fee=0;
  1613. if($value['inv_fee']<=$balance){
  1614. $order_fee = $value['inv_fee'];
  1615. $balance -=$value['inv_fee'];
  1616. $value['ainv_fee'] += $value['inv_fee'];
  1617. $value['inv_fee'] = 0;
  1618. }else{
  1619. $value['ainv_fee'] += $balance;
  1620. $value['inv_fee']-= $balance;
  1621. $order_fee = $balance;
  1622. $balance=0;
  1623. }
  1624. $value['updatetime'] = date("Y-m-d H:i:s");
  1625. $orde = Db::name("order_info")->save($value);
  1626. if($orde===false){
  1627. return false;
  1628. }
  1629. $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
  1630. if (($order_fee <= $qrdinfo['inving_fee'])||($order_fee <= $qrdinfo['inving_fee']&&$orderinfo['type']==4
  1631. )) {
  1632. if($orderinfo['type']==4 ){
  1633. $qrdinfo['inving_fee'] -= $order_fee;
  1634. //$qrdinfo['number1618249149738'] -= $order_fee;
  1635. $qrdinfo['Number1618249205231'] += $order_fee;
  1636. //$qrdinfo['number1618249258956'] += $value['good_num'];
  1637. $qrdinfo['ShortText1618559274859'] = $qrdinfo['Number1618249202608']>0 ? "2" : "1";
  1638. }else{
  1639. $qrdinfo['inving_fee'] -= $order_fee;
  1640. //$qrdinfo['number1618249149738'] -= $order_fee;
  1641. $qrdinfo['Number1618249202608'] += $order_fee;
  1642. //$qrdinfo['number1618249258956'] += $value['good_num'];
  1643. $qrdinfo['ShortText1618559274859'] = $qrdinfo['Number1618249205231']>0 ? "2" : "3";
  1644. }
  1645. $qrdinfo['status'] =$qrdinfo['ShortText1618559274859']==1 &&$qrdinfo['ShortText1618559043560']==1 ? 0:1 ;
  1646. $qinfo = Db::name("qrd_info")->save($qrdinfo);
  1647. if ($qinfo) {
  1648. $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
  1649. if ($qrdp['inv_fee'] >= $order_fee || ($qrdp['inv_fee'] >= $order_fee &&$orderinfo['type']==4
  1650. )) {
  1651. if($orderinfo['type']==4){
  1652. $qrdp['inv_fee'] -= $order_fee;
  1653. $qrdp['winv_fee'] += $order_fee;
  1654. $qrdp['inv_status'] = $qrdp['ainv_fee'] == 0 ? 1 : 2;
  1655. }else{
  1656. $qrdp['inv_fee'] -= $order_fee;
  1657. $qrdp['ainv_fee'] += $order_fee;
  1658. $qrdp['inv_status'] = $qrdp['winv_fee'] == 0 ? 3 : 2;
  1659. }
  1660. $qrd = Db::name("qrd")->save($qrdp);
  1661. if(!$qrd){
  1662. return false;
  1663. }
  1664. }else {
  1665. return false;
  1666. }
  1667. }else {
  1668. return false;
  1669. }
  1670. } else {
  1671. return false;
  1672. }
  1673. }
  1674. return true;
  1675. }
  1676. /**
  1677. * @param $orderNo
  1678. * @param $balance
  1679. * @return bool
  1680. * @throws \think\db\exception\DataNotFoundException
  1681. * @throws \think\db\exception\DbException
  1682. * @throws \think\db\exception\ModelNotFoundException
  1683. */
  1684. private function qrd_delet($orderNo,$balance){
  1685. $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=", 1]])->find();
  1686. if (empty($orderinfo)) {
  1687. return false;
  1688. }
  1689. $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
  1690. if (empty($orderlist)) {
  1691. return false;
  1692. }
  1693. foreach ($orderlist as $value) {
  1694. if($balance==0){
  1695. break;
  1696. }
  1697. if($value['inv_fee'] == 0){
  1698. continue;
  1699. }
  1700. $order_fee=0;
  1701. if($value['inv_fee']<=$balance){
  1702. $order_fee = $value['inv_fee'];
  1703. $balance -=$value['inv_fee'];
  1704. $value['winv_fee'] += $value['inv_fee'];
  1705. $value['inv_fee'] = 0;
  1706. }else{
  1707. $value['inv_fee'] -= $balance;
  1708. $value['winv_fee']+= $balance;
  1709. $order_fee = $balance;
  1710. $balance=0;
  1711. }
  1712. $value['updatetime'] = date("Y-m-d H:i:s");
  1713. $orde = Db::name("order_info")->save($value);
  1714. if(!$orde){
  1715. return false;
  1716. }
  1717. $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
  1718. if ($order_fee <= $qrdinfo['inving_fee']) {
  1719. $qrdinfo['inving_fee'] -= $order_fee;
  1720. if($orderinfo['type']==4){
  1721. $qrdinfo['Number1618249202608'] += $order_fee;
  1722. // $qrdinfo['number1618249258956'] += $value['good_num'];
  1723. }else{
  1724. $qrdinfo['Number1618249205231'] += $order_fee;
  1725. $qrdinfo['number1618249258956'] -= $value['good_num'];
  1726. }
  1727. $qrdinfo['status'] =$qrdinfo['ShortText1618559274859']==1 &&$qrdinfo['ShortText1618559043560']==1 ? 0:1 ;
  1728. // $qrdinfo['ShortText1618559043560'] = $value['total_fee'] < $qrdinfo['number1618249149738'] ? "2" : "3";
  1729. $qinfo = Db::name("qrd_info")->save($qrdinfo);
  1730. if ($qinfo) {
  1731. $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
  1732. if ($qrdp['inv_fee'] >= $order_fee) {
  1733. if($orderinfo['type']==4){
  1734. $qrdp['ainv_fee'] += $order_fee;
  1735. }else{
  1736. $qrdp['winv_fee'] += $order_fee;
  1737. }
  1738. $qrdp['inv_fee'] -= $order_fee;
  1739. // $qrdp['inv_status'] = $qrdp['winv_fee'] == 0 ? 3 : 2;
  1740. $qrd = Db::name("qrd")->save($qrdp);
  1741. if(!$qrd){
  1742. return false;
  1743. }
  1744. }else {
  1745. return false;
  1746. }
  1747. }else {
  1748. return false;
  1749. }
  1750. } else {
  1751. return false;
  1752. }
  1753. }
  1754. return true;
  1755. }
  1756. /**
  1757. * @return \think\response\Json|void
  1758. * @throws \think\db\exception\DataNotFoundException
  1759. * @throws \think\db\exception\DbException
  1760. * @throws \think\db\exception\ModelNotFoundException
  1761. * @throws \think\exception\DbException
  1762. */
  1763. public function DownReceipt(){
  1764. $post = $this->request->post();
  1765. $token = isset($post['token']) ? trim($post['token']) : "";
  1766. if ($token == "") {
  1767. return error_show(101, 'token不能为空');
  1768. }
  1769. $effetc = VerifyTokens($token);
  1770. if (!empty($effetc) && $effetc['code'] != 0) {
  1771. return error_show($effetc['code'], $effetc['message']);
  1772. }
  1773. $condition=[["type_check","=",1],["is_del","=",0],["status","=",3]];
  1774. $inid = isset($post['invid'])&& $post['invid']!="" ? $post['invid'] :"";
  1775. if($inid!=""){
  1776. $condition[]=['id',"in",$inid];
  1777. }
  1778. $invice = Db::name("invoice_pool")->where($condition)->select()->toArray();
  1779. if (empty($invice)){
  1780. return error_show(1003,"未找到数据");
  1781. }
  1782. $data=[];
  1783. foreach ($invice as $value){
  1784. $temp=[];
  1785. $orderNo = Db::name("assoc_list")->where([["rela_form","=",1],["is_del","=",0],['viceNo',"=",$value['invNo']]])->find();
  1786. if(!$orderNo){
  1787. continue;
  1788. }
  1789. $good = Db::name("order_info")->where([['orderNo',"=",$orderNo['codeNo']],["status","=",1]])->select()->toArray();
  1790. if(empty($good)){
  1791. continue;
  1792. }
  1793. if(isset($value['inv_out'])&& $value['inv_out']!=''){
  1794. $invout = Db::name("company_info")->where([['companyNo',"=",$value['inv_out']]])->find();
  1795. }
  1796. foreach ($good as $val){
  1797. $qrd = Db::name("qrd_list")->where("sequenceNo","=",$val['goodNo'])->find();
  1798. $temp['invNo'] = $value['invNo'];
  1799. $temp['qrdNo'] = $val['qrdNo'];
  1800. $temp['depart'] = $qrd['bumen'];
  1801. $temp['apply_name'] =$value['apply_name'];
  1802. $temp['code'] ="";
  1803. $temp['invoice_title'] =$value['invoice_title'];
  1804. $temp['invoice_code'] =$value['invoice_code']." ";
  1805. $temp['invoice_addr'] =$value['invoice_addr'];
  1806. $temp['invoice_mobile'] =$value['invoice_mobile'];
  1807. $temp['invoice_bank'] =$value['invoice_bank'];
  1808. $temp['invoice_bankNo'] =$value['invoice_bankNo']." ";
  1809. $temp['company'] =$qrd['kehu'];
  1810. $temp['goodname'] =$qrd['goodname'];
  1811. $temp['tax'] =$qrd['taxPoint'];
  1812. $temp['inv_name'] =$val['remark'];
  1813. $temp['diff_weight'] =$qrd['diff_weight'];
  1814. $temp['diff_price'] =$qrd['diff_price'];
  1815. $temp['unit'] =$val['unit'];
  1816. $temp['num'] =$val['good_num'];
  1817. $temp['price'] =$val['price'];
  1818. $temp['total_fee'] =$val['total_fee'];
  1819. switch ($value['inv_type']){
  1820. case 1:
  1821. $temp['inv_type'] = "增值税专用发票";
  1822. break;
  1823. case 2:
  1824. $temp['inv_type'] = "增值税普通发票";
  1825. break;
  1826. case 3:
  1827. $temp['inv_type'] = "增值税电子专用发票";
  1828. break;
  1829. case 4:
  1830. $temp['inv_type'] = "增值税电子普通发票";
  1831. break;
  1832. default:
  1833. $temp['inv_type'] = "-";
  1834. break;
  1835. }
  1836. // $temp['inv_type'] = $value['inv_type']==1?"增值税专用发票": "增值税普通发票";
  1837. // $temp['inv_value'] =$value['inv_value'];
  1838. $temp['remark'] =$value['remark'];
  1839. $temp['kp_companyName'] = isset($invout['company_name']) ? $invout['company_name']:"";
  1840. $temp['kp_company_address'] = isset($invout['company_address']) ? $invout['company_address']:"";
  1841. $temp['kp_company_license'] = isset($invout['company_license']) ? $invout['company_license'].' ':"";
  1842. $temp['kp_bank_name'] = isset($invout['bank_name']) ? $invout['bank_name']:"";
  1843. $temp['kp_bankNo'] = isset($invout['bankNo']) ? $invout['bankNo'].' ':"";
  1844. $temp['kp_contector'] = isset($invout['contector']) ? $invout['contector']:"";
  1845. $temp['kp_mobile'] = isset($invout['mobile']) ? $invout['mobile'].' ':"";
  1846. $data[]=$temp;
  1847. }
  1848. }
  1849. if (empty($data)){
  1850. return error_show(1003,"未找到数据");
  1851. }
  1852. $header=[
  1853. "申请编号",
  1854. "公司订单号",
  1855. "申请部门(所属平台)",
  1856. "申请人",
  1857. "代码",
  1858. "发票开具全称",
  1859. "发票开具企业纳税识别号",
  1860. "发票开具企业地址",
  1861. "发票开具企业地址联系方式",
  1862. "发票开具企业收款银行",
  1863. "发票开具企业收款账户",
  1864. "分公司",
  1865. "采购商品名",
  1866. "税率",
  1867. "发票商品名",
  1868. "工差重量",
  1869. "工差金额",
  1870. "单位",
  1871. "数量",
  1872. "单价",
  1873. "总价",
  1874. "开票种类",
  1875. "备注说明",
  1876. '开票企业名称',
  1877. '开票企业地址',
  1878. '开票企业纳税号',
  1879. '开票企业银行名称',
  1880. '开票企业银行账户',
  1881. '开票企业法人',
  1882. '开票企业联系方式'
  1883. ];
  1884. $li = excelExport("待开票",$header,$data);
  1885. }
  1886. /**
  1887. * @return \think\response\Json|void
  1888. * @throws \think\db\exception\DataNotFoundException
  1889. * @throws \think\db\exception\DbException
  1890. * @throws \think\db\exception\ModelNotFoundException
  1891. * @throws \think\exception\DbException
  1892. */
  1893. public function ImportReceipt(){
  1894. $post = $this->request->post();
  1895. $token = isset($post['token']) ? trim($post['token']) : "";
  1896. if ($token == "") {
  1897. return error_show(101, 'token不能为空');
  1898. }
  1899. $effetc = VerifyTokens($token);
  1900. if (!empty($effetc) && $effetc['code'] != 0) {
  1901. return error_show($effetc['code'], $effetc['message']);
  1902. }
  1903. $invlist = isset($post['data'])&& $post['data']!="" ? $post['data'] :"";
  1904. if($invlist==""){
  1905. return error_show(1005, '参数data 不能为空');
  1906. }
  1907. $list=[];
  1908. $total =[];
  1909. foreach ($invlist as $key=>$value){
  1910. if($value['invNo']==""){
  1911. return error_show(1005,"第".($key+1)."行缺少发票申请编号数据");
  1912. }
  1913. if($value["inv_type"]===""){
  1914. return error_show(1005,"第".($key+1)."行缺少发票类型数据");
  1915. }
  1916. if($value["inv_code"]==""){
  1917. return error_show(1005,"第".($key+1)."行缺少发票代码数据");
  1918. }
  1919. if($value["inv_number"]==""){
  1920. return error_show(1005,"第".($key+1)."行缺少发票号码数据");
  1921. }
  1922. if($value["inv_total"]==""){
  1923. return error_show(1005,"第".($key+1)."行缺少发票金额数据");
  1924. }
  1925. if($value["createtime"]==""){
  1926. return error_show(1005,"第".($key+1)."行缺少发票开票日期数据");
  1927. }
  1928. !isset($total[$value['invNo']]) ? $total[$value['invNo']]['total']=0:"";
  1929. // $inv=Db::name("invoice_list")->where([["inv_code","=",$value['inv_code']],["inv_number","=",$value['inv_number']],["is_del","=",0]])->find();
  1930. // if($inv){
  1931. // return error_show(1005,"第".($key+1)."行发票已存在");
  1932. // }
  1933. $temp=[];
  1934. $temp['invNo'] = $value["invNo"];
  1935. $temp['inv_type'] = $value['inv_type'];
  1936. $temp['inv_code'] = $value['inv_code'];
  1937. $temp['inv_number'] = $value['inv_number'];
  1938. $temp['inv_total'] = $value['inv_total'];
  1939. $temp['createtime'] = $value['createtime'];
  1940. $temp['status'] = 1;
  1941. $temp['is_del'] = 0;
  1942. $temp['addtime'] = date("Y-m-d H:i:s");
  1943. $temp['updatetime'] = date("Y-m-d H:i:s");
  1944. $total[$value["invNo"]]['total']+=$value['inv_total'];
  1945. $list[]=$temp;
  1946. }
  1947. Db::startTrans();
  1948. try{
  1949. foreach ($total as $key=>$value){
  1950. $inpool = Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->find();
  1951. $invtotal=Db::name("invoice_list")->where([["invNo","=",$key],["is_del","=",0]])->sum('inv_total');
  1952. if($inpool['inv_value']!=$value['total']+$invtotal){
  1953. Db::rollback();
  1954. if($inpool['inv_value']>$value['total']+$invtotal){
  1955. return error_show(1005,"发票上传数据{$key}不足票面总额");
  1956. }else{
  1957. return error_show(1005,"发票上传数据{$key}超出票面总额");
  1958. }
  1959. }
  1960. $up =Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->save(["status"=>4,"updatetime"=>date("Y-m-d H:i:s")]);
  1961. if(!$up){
  1962. Db::rollback();
  1963. return error_show(1005,"发票状态更新失败");
  1964. }
  1965. }
  1966. $insert = Db::name("invoice_list")->insertAll($list);
  1967. if(!$insert){
  1968. Db::rollback();
  1969. return error_show(1005,"发票数据录入失败");
  1970. }else{
  1971. Db::commit();
  1972. return app_show(0,"发票数据录入成功");
  1973. }
  1974. }catch (\Exception $e){
  1975. Db::rollback();
  1976. return error_show(1005,$e->getMessage());
  1977. }
  1978. }
  1979. public function RefundAdd()
  1980. {
  1981. $post = $this->request->post();
  1982. $token = isset($post['token']) ? trim($post['token']) : "";
  1983. if ($token == "") {
  1984. return error_show(101, 'token不能为空');
  1985. }
  1986. $effetc = VerifyTokens($token);
  1987. if (!empty($effetc) && $effetc['code'] != 0) {
  1988. return error_show($effetc['code'], $effetc['message']);
  1989. }
  1990. // $customer_w = isset($post['customer'])&& $post['customer']!='' ? $post['customer'] : "";
  1991. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=""?trim($post['companyNo']):"";
  1992. $inv_type = isset($post['inv_type'])&&$post['inv_type']!=""?trim($post['inv_type']):1;
  1993. // $inv_add = isset($post['inv_add'])&&$post['inv_add']!=""?trim($post['inv_add']):"0";
  1994. $remark = isset($post['remark'])&&$post['remark']!=""?trim($post['remark']):"";
  1995. $userinfo = GetUserInfo($token);
  1996. $username="";
  1997. if($companyNo==""){
  1998. return error_show(1004,"参数companyNo 不能为空");
  1999. }
  2000. if(isset($userinfo['code'])&&$userinfo['code']==0){
  2001. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  2002. }
  2003. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  2004. if(empty($customer)){
  2005. return error_show(1004,"未找到客户企业信息");
  2006. }
  2007. $invoice=[];
  2008. // isset($post['invoice_addr'])&& $post['invoice_addr']!='' ? $invoice['invoice_addr'] = $post['invoice_addr']:"";
  2009. // isset($post['invoice_bank'])&& $post['invoice_bank']!='' ? $invoice['invoice_bank'] = $post['invoice_bank']:"";
  2010. // isset($post['invoice_bankNo'])&& $post['invoice_bankNo']!='' ? $invoice['invoice_bankNo'] =
  2011. // $post['invoice_bankNo']:"";
  2012. // isset($post['invoice_code'])&& $post['invoice_code']!='' ? $invoice['invoice_code'] = $post['invoice_code']:"";
  2013. // isset($post['invoice_mobile'])&& $post['invoice_mobile']!='' ? $invoice['invoice_mobile'] =
  2014. // $post['invoice_mobile']:"";
  2015. // isset($post['invoice_title'])&& $post['invoice_title']!='' ? $invoice['invoice_title'] =
  2016. // $post['invoice_title']:"";
  2017. // if(!checkTel($invoice['invoice_mobile'])&&!checkMobile($invoice['invoice_mobile'])){
  2018. // return error_show(1004,"企业信息联系电话格式错误");
  2019. // }
  2020. $total = isset($post['total'])&&$post['total']!=""?$post['total']:"";
  2021. if($total==""){
  2022. return error_show(1004,"参数total 不能为空");
  2023. }
  2024. Db::startTrans();
  2025. try{
  2026. // if(!empty($invoice) && $invoice!='') {
  2027. // $invoice['updatetime'] = date("Y-m-d H:i:s");
  2028. // $invoice['checkNo'] = $companyNo;
  2029. // if ($inv_add == 1) {
  2030. // $cust = Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->find();
  2031. // if($cust){
  2032. // Db::rollback();
  2033. // return error_show(1005,"企业财务信息已存在");
  2034. // }
  2035. // $invoice['addtime'] = date("Y-m-d H:i:s");
  2036. // Db::name('customer_invoice')->insert($invoice);
  2037. // } else {
  2038. // Db::name('customer_invoice')->where('invoice_code', "=", $invoice['invoice_code'])->save($invoice);
  2039. // }
  2040. // }
  2041. $invNo = makeNo("INV");
  2042. $inv = [
  2043. "invNo" => $invNo,
  2044. "inv_value" => $total,
  2045. "inv_out" => "",
  2046. "inv_in" => $companyNo,
  2047. "apply_id" =>$effetc['data']['user']['id'],
  2048. "apply_name" => $username,
  2049. "status" => 0,
  2050. "trade_status" => 1,
  2051. "order_status" => 1,
  2052. "worder_fee"=>$total,
  2053. "wtrade_fee"=>$total,
  2054. "invoice_title" => "",
  2055. "invoice_addr" => "",
  2056. "invoice_mobile" => "",
  2057. "invoice_code" => "",
  2058. "invoice_bank" => "",
  2059. "invoice_bankNo" => "",
  2060. "inv_add" => 0,
  2061. "inv_type"=>$inv_type,
  2062. "type_check"=>5,
  2063. "is_order"=>0,
  2064. "remark"=>$remark,
  2065. "addtime" => date("Y-m-d H:i:s"),
  2066. "updatetime" => date("Y-m-d H:i:s")
  2067. ];
  2068. $invinsert = Db::name("invoice_pool")->insert($inv);
  2069. if($invinsert){
  2070. Db::commit();
  2071. return app_show(0,"发票创建成功");
  2072. }
  2073. Db::rollback();
  2074. return error_show(1004,"发票新建失败");
  2075. }catch (\Exception $e){
  2076. Db::rollback();
  2077. return error_show(1004,$e->getMessage());
  2078. }
  2079. }
  2080. /**
  2081. * @return \think\response\Json|void
  2082. * @throws \think\db\exception\DataNotFoundException
  2083. * @throws \think\db\exception\DbException
  2084. * @throws \think\db\exception\ModelNotFoundException
  2085. * @throws \think\exception\DbException
  2086. */
  2087. public function SaveRefundInv(){
  2088. $post = $this->request->post();
  2089. $token = isset($post['token']) ? trim($post['token']) : "";
  2090. if ($token == "") {
  2091. return error_show(101, 'token不能为空');
  2092. }
  2093. $effetc = VerifyTokens($token);
  2094. if (!empty($effetc) && $effetc['code'] != 0) {
  2095. return error_show($effetc['code'], $effetc['message']);
  2096. }
  2097. $invNo = isset($post['invNo']) && $post['invNo']!="" ? $post['invNo']:"";
  2098. if($invNo==""){
  2099. return error_show(1004,"参数invNo 不能为空");
  2100. }
  2101. $companyNo = isset($post['companyNo']) && $post['companyNo']!="" ? $post['companyNo']:"";
  2102. if($companyNo==""){
  2103. return error_show(1004,"参数companyNo 不能为空");
  2104. }
  2105. $invfo = Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->find();
  2106. if(empty($invfo)){
  2107. return error_show(1005,"未找到发票数据");
  2108. }
  2109. // $total_fee = isset($post['total_fee'])&&$post['total_fee']!="" ? $post['total_fee']:"";
  2110. // if($total_fee==""){
  2111. // return error_show(1004,"参数total_fee 不能为空");
  2112. // }
  2113. // if($trade['balance']<$total_fee){
  2114. // return error_show(1005,"资金余额不足");
  2115. // }
  2116. $refundid = isset($post['refundid']) && $post['refundid']!="" ? $post['refundid'] :"";
  2117. if($refundid==""){
  2118. return error_show(1006,"参数refundid 不能为空");
  2119. }
  2120. $refund =Db::name("refund")->where([["is_del","=",0],['refund_status',"=",3],['id',"in",$refundid]])->select()->toArray();
  2121. if(empty($refund)){
  2122. return error_show(1006,"退款资金数据未找到");
  2123. }
  2124. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  2125. if(empty($customer)){
  2126. return error_show(1005,"未找到用户数据");
  2127. }
  2128. $refund_fee = array_sum(array_column($refund,"refund_fee"));
  2129. if($refund_fee!=$invfo['inv_value']){
  2130. return error_show(1005,"发票额度和退票额度不符");
  2131. }
  2132. // if($refund_fee<$trade['balance']){
  2133. // return error_show(1005,"资金退款余额不足");
  2134. // }
  2135. Db::startTrans();
  2136. try{
  2137. $invfo['inv_in'] = $companyNo;
  2138. $invfo['updatetime'] = date("Y-m-d H:i:s");
  2139. $tra = Db::name("invoice_pool")->save($invfo);
  2140. if($tra){
  2141. foreach ($refund as $value){
  2142. $value['backNo']= $invfo['invNo'];
  2143. $value['back_fee']= $value['refund_fee'];
  2144. $value['refund_status']= 4;
  2145. $value['updatetime']= date("Y-m-d H:i:s");
  2146. $ref=Db::name("refund")->save($value);
  2147. if($ref){
  2148. $tadpool = Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",
  2149. $value['relaNo']],['refund_status',"=",2]])->save(["refund_status"=>3,
  2150. "updatetime"=>date("Y-m-d H:i:s")]);
  2151. if(!$tadpool){
  2152. Db::rollback();
  2153. return error_show(1007,"资金退款状态更新失败");
  2154. }
  2155. }else{
  2156. Db::rollback();
  2157. return error_show(1007,"资金退款状态更新失败");
  2158. }
  2159. }
  2160. Db::commit();
  2161. return app_show(0,"欠款资金认领成功");
  2162. }
  2163. Db::rollback();
  2164. return error_show(1007,"资金退款认领失败");
  2165. }catch (\Exception $e){
  2166. Db::rollback();
  2167. return error_show(1006,$e->getMessage());
  2168. }
  2169. }
  2170. /**
  2171. * 退票关联订单
  2172. * @return \think\response\Json|void
  2173. * @throws \think\db\exception\DataNotFoundException
  2174. * @throws \think\db\exception\DbException
  2175. * @throws \think\db\exception\ModelNotFoundException
  2176. * @throws \think\exception\DbException
  2177. */
  2178. public function InvRefundCreate(){
  2179. $post = $this->request->post();
  2180. $token = isset($post['token']) ? trim($post['token']) : "";
  2181. if ($token == "") {
  2182. return error_show(101, 'token不能为空');
  2183. }
  2184. $effetc = VerifyTokens($token);
  2185. if (!empty($effetc) && $effetc['code'] != 0) {
  2186. return error_show($effetc['code'], $effetc['message']);
  2187. }
  2188. $guserinfo =GetUserInfo($token);
  2189. if(isset($guserinfo['code']) && $guserinfo['code']!=0){
  2190. return error_show($guserinfo['code'],$guserinfo['message']);
  2191. }
  2192. $invNo = isset($post['invNo'])&&$post['invNo']!="" ? trim($post['invNo']) : "";
  2193. if($invNo==""){
  2194. return error_show(1005,"参数invNo 不能为空");
  2195. }
  2196. $invinfo = Db::name("invoice_pool")->where([['invNo',"=",$invNo],['is_del',"=",0]])->find();
  2197. if(empty($invinfo)){
  2198. return error_show(1005,"未找到发票数据");
  2199. }
  2200. if($invinfo['type_check']!=5){
  2201. return error_show(1005,"数据不是退票数据");
  2202. }
  2203. $type = isset($post['type']) && $post['type']!=="" ? $post['type'] :1;
  2204. $trade = isset($post['trade'])&& $post['trade']!=="" ? $post['trade'] :"";
  2205. $good = isset($post['good'])&& $post['good']!=="" ? $post['good'] :"";
  2206. if($type==1 &&$trade==""){
  2207. return error_show(1005,"参数trade 不能为空");
  2208. }
  2209. if($type!=1 &&$good==""){
  2210. return error_show(1005,"参数good 不能为空");
  2211. }
  2212. $companyNo = isset($post['companyNo']) && $post['companyNo'] != "" ? trim($post['companyNo']) : "";
  2213. $userinfo = GetUserInfo($token);
  2214. $username = "";
  2215. if ($companyNo == "") {
  2216. return error_show(1004, "参数companyNo 不能为空");
  2217. }
  2218. if (isset($userinfo['code']) && $userinfo['code'] == 0) {
  2219. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : "";
  2220. }
  2221. $customer = Db::name("customer_info")->where("companyNo", "=", $companyNo)->find();
  2222. if (empty($customer)) {
  2223. return error_show(1004, "未找到客户企业信息");
  2224. }
  2225. Db::startTrans();
  2226. try{
  2227. if($type!=1) {
  2228. $orderinfo = [];
  2229. $orderNo = makeNo('ORD');
  2230. $total = 0;
  2231. $fund_fee = 0;
  2232. $wfund_fee = 0;
  2233. $inv_fee = 0;
  2234. $winv_fee = 0;
  2235. foreach ($good as $value) {
  2236. $temp = [];
  2237. $temp['orderNo'] = $orderNo;
  2238. $temp['goodNo'] = $value['goodNo'];
  2239. $goocp = Db::name("qrd_list")->where("sequenceNo", "=", $value['goodNo'])->find();
  2240. if (!$goocp) {
  2241. continue;
  2242. }
  2243. $temp['good_name'] = $goocp['goodname'];
  2244. $temp['qrdNo'] = $goocp['qrdNo'];
  2245. $temp['brand'] = $goocp['brand'];
  2246. $temp['unit'] = $goocp['unit'];
  2247. $temp['good_num'] = $value['num'];
  2248. $temp['price'] = $goocp['price'];
  2249. $temp['total_fee'] = $value['total_fee'];
  2250. $temp['wfund_fee'] = $value['total_fee'];
  2251. $temp['winv_fee'] = $value['total_fee'];
  2252. $temp['other_fee'] = 0;
  2253. $temp['status'] = 1;
  2254. $temp['remark'] = "";
  2255. $temp['addtime'] = date("Y-m-d H:i:s");
  2256. $temp['updatetime'] = date("Y-m-d H:i:s");
  2257. $total += $value['total_fee'];
  2258. $fund_fee += $value['apay_fee'];
  2259. $winv_fee += $value['winv_fee'];
  2260. $inv_fee += $value['ainv_fee'];
  2261. $wfund_fee += $value['wpay_fee'];
  2262. $orderinfo[] = $temp;
  2263. }
  2264. if ($total != $invinfo['worder_fee']) {
  2265. Db::rollback();
  2266. return error_show(1004, "商品总额于发票面额不符");
  2267. }
  2268. $orderins = Db::name("order_info")->insertAll($orderinfo);
  2269. if ($orderins) {
  2270. $order = [
  2271. "orderNo" => $orderNo,
  2272. "companyNo" => $companyNo,
  2273. "sales_id" => $effetc['data']['user']['id'],
  2274. "sales_name" => $username,
  2275. "total_amount" => $total,
  2276. "inv_status" => 1,
  2277. "fund_status" => 1,
  2278. "fund_fee" => 0,
  2279. "wfund_fee" => $total,
  2280. "inv_fee" => 0,
  2281. "winv_fee" => $total,
  2282. "status" => 0,
  2283. "type" => 3,
  2284. "addtime" => date("Y-m-d H:i:s"),
  2285. "updatetime" => date("Y-m-d H:i:s")
  2286. ];
  2287. $orderpool = Db::name("order_pool")->insert($order, true);
  2288. if ($orderpool > 0) {
  2289. $rela = [];
  2290. $assNo = makeNo("ASC");
  2291. $cancel = 0;
  2292. $balance = $invinfo["worder_fee"] > $total ? $total : $invinfo["worder_fee"];
  2293. $total -= $balance;
  2294. $sav = [
  2295. "worder_fee" => $invinfo['worder_fee'] - $balance,
  2296. "order_used" => $invinfo['order_used'] + $balance,
  2297. "is_order" => 1,
  2298. "updatetime" => date("Y-m-d H:i:s")
  2299. ];
  2300. Db::name("invoice_pool")->where([["invNo", "=", $invinfo['invNo']]])->save($sav);
  2301. $rela = [
  2302. "assocNo" => $assNo,
  2303. "viceNo" => $orderNo,
  2304. "vice_total" => $order['total_amount'],
  2305. "vice_fee" => $total,
  2306. "cancel_fee" => $balance,
  2307. "status" => 1,
  2308. "addtime" => date("Y-m-d H:i:s"),
  2309. "updatetime" => date("Y-m-d H:i:s")
  2310. ];
  2311. $assoc = Db::name("assoc_rela")->insert($rela);
  2312. if ($assoc) {
  2313. $data = [
  2314. "assocNo" => $assNo,
  2315. "companyNo" => $companyNo,
  2316. "type" => 1,
  2317. "rela_form" => 1,
  2318. "codeNo" => $invinfo["invNo"],
  2319. "total_fee" => $invinfo['inv_value'],
  2320. "balance" => $invinfo['worder_fee'] - $balance,
  2321. "cancel_total" => $balance,
  2322. "status" => 1,
  2323. "addtime" => date("Y-m-d H:i:s"),
  2324. "updatetime" => date("Y-m-d H:i:s")
  2325. ];
  2326. $realkey = Db::name("assoc_key")->insert($data);
  2327. if ($realkey) {
  2328. Db::name("invoice_pool")->where("invNo", "=", $invNo)->save(["wfund_fee" => $total, 'fund_used' => $balance, "updatetime" => date("Y-m-d H:i:s")]);
  2329. $qrdup = $this->updateQrd($orderNo, $balance);
  2330. if (!$qrdup) {
  2331. Db::rollback();
  2332. return error_show(1004, "新建关联申请失败");
  2333. }
  2334. Db::commit();
  2335. return app_show(0, "新建关联成功");
  2336. }
  2337. }
  2338. Db::rollback();
  2339. return error_show(1004, "新建关联申请失败");
  2340. }
  2341. }
  2342. Db::rollback();
  2343. return error_show(1004, "新建关联申请失败");
  2344. }else{
  2345. $rela = [];
  2346. $trade = 0;
  2347. $assNo = makeNo("ASC");
  2348. foreach ($trade as $value) {
  2349. $balance = $invinfo['wtrade_fee'] > $value['cancel'] ? $value['cancel'] : $invinfo['wtrade_fee'];
  2350. if ($invinfo['wtrade_fee'] <= 0) {
  2351. Db::rollback();
  2352. return error_show(1005, "资金总额大于发票额度");
  2353. }
  2354. if ($value['inv_fee'] <= 0) {
  2355. Db::rollback();
  2356. return error_show(1005, "发票核销金额为0");
  2357. }
  2358. $invinfo['wtrade_fee'] -= $balance;
  2359. $invinfo['fund_used'] += $balance;
  2360. $trade += $balance;
  2361. $rela[] = [
  2362. "assocNo" => $assNo,
  2363. "viceNo" => $value['traNo'],
  2364. "vice_total" => $value['total_fee'],
  2365. "vice_fee" => $value['inv_fee'] - $balance,
  2366. "cancel_fee" => $balance,
  2367. "status" => 1,
  2368. "addtime" => date("Y-m-d H:i:s"),
  2369. "updatetime" => date("Y-m-d H:i:s")
  2370. ];
  2371. $value['inv_fee'] -= $balance;
  2372. $value['inv_used'] += $balance;
  2373. $value['updatetime'] = date("Y-m-d H:i:s");
  2374. Db::name("trade_pool")->save($value);
  2375. }
  2376. $assoc = Db::name("assoc_rela")->insertAll($rela);
  2377. if ($assoc) {
  2378. $data = [
  2379. "assocNo" => $assNo,
  2380. "companyNo" => $companyNo,
  2381. "type" => 2,
  2382. "rela_form" => $type,
  2383. "codeNo" => $invinfo["invNo"],
  2384. "total_fee" => $invinfo['inv_value'],
  2385. "balance" => $invinfo['wtrade_fee'],
  2386. "cancel_total" => $trade,
  2387. "status" => 1,
  2388. "addtime" => date("Y-m-d H:i:s"),
  2389. "updatetime" => date("Y-m-d H:i:s")
  2390. ];
  2391. $realkey = Db::name("assoc_key")->insert($data);
  2392. if ($realkey) {
  2393. $invinfo['updatetime'] = date("Y-m-d H:i:s");
  2394. $order_up = Db::name("invoice_pool")->save($invinfo);
  2395. Db::commit();
  2396. return app_show(0, "新建关联成功");
  2397. }
  2398. }
  2399. Db::rollback();
  2400. return error_show(1004, "新建关联申请失败");
  2401. }
  2402. }catch (\Exception $e){
  2403. Db::rollback();
  2404. return error_show(1004,$e->getMessage());
  2405. }
  2406. }
  2407. }