Assoc.php 121 KB

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