Trade.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\Admin\controller;
  4. use app\BaseController;
  5. use think\facade\Db;
  6. class Trade extends BaseController
  7. {
  8. /**
  9. * 显示资源列表
  10. *
  11. * @return \think\Response
  12. * @throws \think\db\exception\DataNotFoundException
  13. * @throws \think\db\exception\DbException
  14. * @throws \think\db\exception\ModelNotFoundException
  15. */
  16. public function list()
  17. {
  18. $post = $this->request->post();
  19. $token = isset($post['token']) ? trim($post['token']) : "";
  20. if ($token == "") {
  21. return error_show(101, 'token不能为空');
  22. }
  23. $effetc = VerifyTokens($token);
  24. if (!empty($effetc) && $effetc['code'] != 0) {
  25. return error_show($effetc['code'], $effetc['message']);
  26. }
  27. $condition = [["is_del","=",0]];
  28. $status = isset($post['status']) && $post['status'] !== "" ? intval($post['status']): "";
  29. if ($status != "") {
  30. $condition[] = ["status", "=",$status];
  31. }
  32. $name = isset($post['name']) && $post['name'] !== "" ? trim($post['name']): "";
  33. if ($name != "") {
  34. $condition[] = ["trade_out", "like","%{$name}%"];
  35. }
  36. $logNo = isset($post['logNo']) && $post['logNo'] !== "" ? trim($post['logNo']) : "";
  37. if ($logNo != "") {
  38. $condition[] = ["logNo", "like","%{$logNo}%"];
  39. }
  40. $companyNo = isset($post['companyNo']) && $post['companyNo'] !== "" ? trim($post['companyNo']) : "";
  41. if ($companyNo != "") {
  42. $logall = Db::name("trade_pool")->where([["companyNo","=",$companyNo],['is_del',"=",0]])->column('logNo');
  43. $condition[] = ["logNo", "in",$logall];
  44. }
  45. $traNo = isset($post['traNo']) && $post['traNo'] !== "" ? trim($post['traNo']) : "";
  46. if ($traNo != "") {
  47. $logall = Db::name("trade_pool")->where([["traNo","=",$traNo],['is_del',"=",0]])->column('logNo');
  48. $condition[] = ["logNo", "in",$logall];
  49. }
  50. $total_s = isset($post['total_low']) && $post['total_low'] !== "" ? trim($post['total_low']) : "";
  51. if ($total_s != "") {
  52. $condition[] = ["total_fee", ">=",$total_s];
  53. }
  54. $total_e = isset($post['total_up']) && $post['total_up'] !== "" ? trim($post['total_up']) : "";
  55. if ($total_e != "") {
  56. $condition[] = ["total_fee", "<=",$total_e];
  57. }
  58. $trade_type = isset($post['trade_type']) && $post['trade_type'] !== "" ? trim($post['trade_type']) : "";
  59. if ($trade_type != "") {
  60. $condition[] = ["trade_type", "=",$trade_type];
  61. }
  62. $end = isset($post["end"])&&$post['end']!="" ? $post['end'] : "";
  63. $start = isset($post["start"])&&$post['start']!="" ? $post['start'] : "";
  64. if($start!=""){
  65. $condition[] = ["trade_time", ">=",$start];
  66. }
  67. if($end!=""){
  68. $condition[] = ["trade_time", "<=",$end];
  69. }
  70. $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
  71. $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
  72. $count = Db::name("trade_log")->where($condition)->count();
  73. $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
  74. $page = $page >= $total ? intval($total) : $page;
  75. $list = Db::name("trade_log")->where($condition)->field('*,used_fee+tra_fee as amount')->order("addtime desc")
  76. ->page(intval($page), $size)
  77. ->select();
  78. $data=[];
  79. foreach ($list as $value){
  80. $value['trade'] =Db::name("trade_pool")->alias("a")->leftJoin("cfp_customer_info b","a.companyNo=b.companyNo")->where
  81. ([["logNo","=",$value['logNo']],['a.is_del',"=",0],['refund_status',"=",0]])->field("a.*,b.companyName")->select();
  82. $data[]=$value;
  83. }
  84. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  85. }
  86. /**
  87. * 显示创建资源表单页.
  88. *
  89. * @return \think\Response
  90. */
  91. public function create()
  92. {
  93. $post = $this->request->post();
  94. $token = isset($post['token']) ? trim($post['token']) : "";
  95. if ($token == "") {
  96. return error_show(101, 'token不能为空');
  97. }
  98. $effetc = VerifyTokens($token);
  99. if (!empty($effetc) && $effetc['code'] != 0) {
  100. return error_show($effetc['code'], $effetc['message']);
  101. }
  102. $files = $this->request->file("execl");
  103. $data = upload_ll($files,$files->getOriginalExtension());
  104. if($data['code']!=0){
  105. return error_show(1004,$data['msg']);
  106. }
  107. $trade = $data['data'];
  108. $list = [];
  109. foreach ( $trade as $key => $value) {
  110. if (!isset($value[0]) || $value[0] == '') {
  111. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  112. return error_show( 1003, '交易时间不能为空!');
  113. }
  114. if ((!isset($value[1]) || $value[1] == '') && (!isset($value[2]) || $value[2] == '')) {
  115. //return ['code' => 1003, "msg" => '收入/支出金额不能为空'];
  116. return error_show( 1003, '收入/支出金额不能为空!');
  117. }
  118. if (!isset($value[4]) || $value[4] == '') {
  119. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  120. return error_show( 1003, '交易行名不能为空!');
  121. }
  122. if (!isset($value[6]) || $value[6] == '') {
  123. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  124. return error_show( 1003, '对方账户不能为空!');
  125. }
  126. if (!isset($value[7]) || $value[7] == '') {
  127. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  128. return error_show( 1003, '对方户名不能为空!');
  129. }
  130. if ($key == 0) {
  131. if ($value[0] != '交易时间') {
  132. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  133. return error_show( 1003, '模板第一列为必须为交易时间!');
  134. }
  135. if ($value[1] != '收入金额') {
  136. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  137. return error_show( 1003, '模板第二列为必须为收入金额!');
  138. }
  139. if ($value[2] != '支出金额') {
  140. // return ['code' => 1003, "msg" => '模板第二列为必须为支出金额!'];
  141. return error_show( 1003, '模板第二列为必须为支出金额!');
  142. }
  143. if ($value[4] != '交易行名') {
  144. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  145. return error_show( 1003, '模板第五列为必须为交易行名!');
  146. }
  147. if ($value[6] != '对方账号') {
  148. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  149. return error_show( 1003, '模板第七列为必须为对方账号!');
  150. }
  151. if ($value[7] != '对方户名') {
  152. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  153. return error_show( 1003, '模板第八列为必须为对方户名!');
  154. }
  155. continue;
  156. }
  157. $total_fee = $value[1]==0&&$value[2]!=0 ? $value[2]: $value[1];
  158. $type = $value[1]==0&&$value[2]!=0 ? 1: 0;
  159. $time = explode(" ", $value[0]);
  160. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  161. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  162. $list[$key]["trade_bank"] = $value[4];
  163. $list[$key]["trade_account"] = $value[6];
  164. $list[$key]["trade_out"] = $value[7];
  165. $list[$key]["trade_in"] = '';
  166. $list[$key]["trade_type"] = $type;
  167. $list[$key]["trade_used"] = isset($value[8]) ? $value[8] : "";
  168. $list[$key]["trade_remark"] = '';
  169. }
  170. if(empty($list)){
  171. return error_show( 1003, '导入数据不能为空!');
  172. }
  173. Db::startTrans();
  174. try{
  175. $tra=[];
  176. foreach ($list as $value) {
  177. $temp = [];
  178. $temp["logNo"] = makeNo('S');
  179. $temp['trade_time'] = $value['tradeTime'];
  180. $temp['total_fee'] = $value['trade_fee'];
  181. $temp['trade_bank'] = $value['trade_bank'];
  182. $temp['trade_account'] = $value['trade_account'];
  183. $temp['trade_type'] =$value['trade_type'];
  184. $temp['trade_out'] = $value['trade_out'];
  185. $temp['trade_in'] = $value['trade_in'];
  186. $temp['trade_used'] = $value['trade_used'];
  187. $temp['trade_remark'] = $value['trade_remark'];
  188. $temp['balance'] =$value['trade_fee'];
  189. $temp['addtime'] = date("Y-m-d H:i:s");
  190. $temp['updatetime'] = date("Y-m-d H:i:s");
  191. $tra[]=$temp;
  192. }
  193. $list = Db::name('trade_log')->insertAll($tra);
  194. if($list==count($tra)){
  195. Db::commit();
  196. return app_show(0, "资金导入成功");
  197. }else{
  198. Db::rollback();
  199. return app_show(1004, "资金导入失败");
  200. }
  201. }catch (\Exception $e){
  202. Db::rollback();
  203. return app_show(1004, $e->getMessage());
  204. }
  205. }
  206. /**
  207. * 保存新建的资源
  208. *
  209. * @param \think\Request $request
  210. * @return \think\Response
  211. */
  212. public function save()
  213. {
  214. $post = $this->request->post();
  215. $token = isset($post['token']) ? trim($post['token']) : "";
  216. if ($token == "") {
  217. return error_show(101, 'token不能为空');
  218. }
  219. $effetc = VerifyTokens($token);
  220. if (!empty($effetc) && $effetc['code'] != 0) {
  221. return error_show($effetc['code'], $effetc['message']);
  222. }
  223. $userinfo = GetUserInfo($token);
  224. $username="";
  225. if(isset($userinfo['code'])&&$userinfo['code']==0){
  226. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname']: "";
  227. }
  228. $data = isset($post['id']) && $post['id']!="" ? $post['id']:"";
  229. if($data==""){
  230. return error_show(1004,"参数id 不能为空");
  231. }
  232. $companyNo = isset($post['companyNo']) && $post['companyNo']!="" ? $post['companyNo']:"";
  233. if($companyNo==""){
  234. return error_show(1004,"参数companyNo 不能为空");
  235. }
  236. $trade = Db::name("trade_log")->where([["id","=",$data],["is_del","=",0]])->find();
  237. if(empty($trade)){
  238. return error_show(1005,"未找到资金数据");
  239. }
  240. $total_fee = isset($post['total_fee'])&&$post['total_fee']!="" ? $post['total_fee']:"";
  241. if($total_fee==""){
  242. return error_show(1004,"参数total_fee 不能为空");
  243. }
  244. if($trade['balance']<$total_fee){
  245. return error_show(1005,"资金余额不足");
  246. }
  247. $invid = isset($post['invid']) && $post['invid']!="" ? intval($post['invid']) :"";
  248. if($invid!=""){
  249. $invlog = Db::name("invoice_pool")->where([["id","=",$invid],["type_check","=",2],["is_del","=",0]])
  250. ->find();
  251. if(empty($invlog)){
  252. return error_show(1005,"未找到发票数据");
  253. }
  254. }
  255. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  256. if(empty($customer)){
  257. return error_show(1005,"未找到用户数据");
  258. }
  259. Db::startTrans();
  260. try{
  261. $trade['is_all'] = $total_fee == $trade['total_fee'] ? 1 :0;
  262. $trade['balance'] -= $total_fee;
  263. $trade['tra_fee'] += $total_fee;
  264. $trade['updatetime'] = date("Y-m-d H:i:s");
  265. $tradlog=Db::name("trade_log")->save($trade);
  266. if($tradlog){
  267. $count = Db::name("trade_pool")->where("logNo","=",$trade['logNo'])->count();
  268. $tradepool = [
  269. "traNo" =>$trade["logNo"]."-".sprintf('%03d',$count+1),
  270. "companyNo" => $companyNo,
  271. "logNo" => $trade["logNo"],
  272. "trade_time" => $trade['trade_time'],
  273. "total_fee" => $total_fee,
  274. "order_fee" => 0,
  275. "inv_fee" => 0,
  276. "inv_used" => 0,
  277. "order_used" => 0,
  278. "winv_fee" => $total_fee,
  279. "worder_fee" => $total_fee,
  280. "order_status" => 1,
  281. "inv_status" => 1,
  282. "remark" => "",
  283. "status" => 2,
  284. "addtime" => date("Y-m-d H:i:s"),
  285. "updatetime" => date("Y-m-d H:i:s")
  286. ];
  287. $tra = Db::name("trade_pool")->insert($tradepool,true);
  288. if ($invid != "" && !empty($invlog) && $tra>0) {
  289. $balance = $invlog['wtrade_fee'] > $tradepool['winv_fee'] ? $tradepool['winv_fee'] : $invlog['wtrade_fee'];
  290. if ($invlog['wtrade_fee'] <= 0) {
  291. Db::rollback();
  292. return error_show(1004, "发票可核销余额为0");
  293. }
  294. $assocNo = makeNo("ASC");
  295. $asslog = [
  296. "assocNo" => $assocNo,
  297. "type" => 3,
  298. "apply_id" => $effetc['data']['user']['id'],
  299. "apply_name" => $username,
  300. "companyNo" => $companyNo,
  301. "codeNo" => $tradepool["traNo"],
  302. "rela_form" => 5,
  303. "total_fee" => $tradepool["total_fee"],
  304. "balance" => $tradepool['winv_fee'] - $balance,
  305. "cancel_total" => $balance,
  306. "status" => 1,
  307. "source" => 2,
  308. "addtime" => date("Y-m-d H:i:s"),
  309. "updatetime" => date("Y-m-d H:i:s")
  310. ];
  311. $logassc = Db::name("assoc_key")->insert($asslog);
  312. if ($logassc) {
  313. $assrela = [
  314. "assocNo" => $assocNo,
  315. "viceNo" => $invlog["invNo"],
  316. "vice_total" => $invlog["wtrade_fee"],
  317. "vice_fee" => $invlog['wtrade_fee'] - $balance,
  318. "cancel_fee" => $balance,
  319. "addtime" => date("Y-m-d H:i:s"),
  320. "updatetime" => date("Y-m-d H:i:s")
  321. ];
  322. Db::name("assoc_rela")->insert($assrela);
  323. $invlog['wtrade_fee'] = $invlog['wtrade_fee'] - $balance;
  324. $invlog['fund_used'] = $invlog['fund_used'] + $balance;
  325. $invlog["updatetime"] = date("Y-m-d H:i:s");
  326. Db::name("invoice_pool")->save($invlog);
  327. // $trade['winv_fee'] = $tradepool['winv_fee'] - $balance;
  328. // $trade['inv_used'] = $tradepool['inv_used'] + $balance;
  329. // $trade['updatetime'] = date("Y-m-d H:i:s");
  330. $tard = [
  331. "winv_fee"=> $tradepool['winv_fee'] - $balance,
  332. "inv_used"=> $balance,
  333. "updatetime"=> date("Y-m-d H:i:s")
  334. ];
  335. Db::name("trade_pool")->where('traNo',"=",$tradepool['traNo'])->save($tard);
  336. Db::commit();
  337. return error_show(0, "资金认领成功");
  338. } else {
  339. Db::rollback();
  340. return error_show(0, "关联关系创建失败");
  341. }
  342. }
  343. Db::commit();
  344. return error_show(0, "资金认领成功");
  345. }
  346. Db::rollback();
  347. return error_show(1005, "资金认领失败");
  348. }catch (\Exception $e){
  349. Db::rollback();
  350. return error_show(1005,$e->getMessage());
  351. }
  352. }
  353. // public function save()
  354. // {
  355. // $post = $this->request->post();
  356. //// $token = isset($post['token']) ? trim($post['token']) : "";
  357. //// if ($token == "") {
  358. //// return error_show(101, 'token不能为空');
  359. //// }
  360. //// $effetc = VerifyTokens($token);
  361. //// if (!empty($effetc) && $effetc['code'] != 0) {
  362. //// return error_show($effetc['code'], $effetc['message']);
  363. //// }
  364. //
  365. // $data = isset($post['id']) && $post['id']!="" ? $post['id']:"";
  366. // if($data==""){
  367. // return error_show(1004,"参数id 不能为空");
  368. // }
  369. // $companyNo = isset($post['companyNo']) && $post['companyNo']!="" ? $post['companyNo']:"";
  370. // if($companyNo==""){
  371. // return error_show(1004,"参数companyNo 不能为空");
  372. // }
  373. // $trade = Db::name("trade_list")->where([["id","=",$data],["is_del","=",0]])->find();
  374. // if(empty($trade)){
  375. // return error_show(1005,"未找到资金数据");
  376. // }
  377. // $invid = isset($post['invid']) && $post['invid']!="" ? intval($post['invid']) :"";
  378. // if($invid!=""){
  379. // $invlog = Db::name("inv_list")->where([["id","=",$invid],["type_check","=",2]])->find();
  380. // if(empty($invlog)){
  381. // return error_show(1005,"未找到发票数据");
  382. // }
  383. // }
  384. // $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  385. // if(empty($customer)){
  386. // return error_show(1005,"未找到用户数据");
  387. // }
  388. // Db::startTrans();
  389. // try{
  390. //
  391. // $tradeinfo=[
  392. // "companyNo"=>$companyNo,
  393. // "status"=>1,
  394. // "updatetime"=>date("Y-m-d H:i:s")
  395. // ];
  396. // $tra = Db::name("trade_pool")->where("traNo","=",$trade['traNo'])->save($tradeinfo);
  397. // if($invid!="" && !empty($invlog) && $tra){
  398. // $data=[
  399. // "temp_type"=>2,
  400. // "mainNo"=>$tradeinfo["id"],
  401. // "viceNo"=>$invid,
  402. // "total_fee"=>$invlog['used_fee']>$tradeinfo['used_fee'] ? $tradeinfo['used_fee']:$invlog['used_fee'],
  403. // "status"=>0,
  404. // "exam_remark"=> "",
  405. // "addtime"=>date("Y-m-d H:i:s")
  406. // ];
  407. // $temp = Db::name("temp_exam")->insert($data);
  408. // if($temp){
  409. // Db::commit();
  410. // return error_show(0,"资金认领成功");
  411. // }else{
  412. // Db::rollback();
  413. // return error_show(1003,"资金认领失败");
  414. // }
  415. // }
  416. //
  417. // Db::commit();
  418. // return error_show(0,"资金认领成功");
  419. // }catch (\Exception $e){
  420. // Db::rollback();
  421. // return error_show(1005,$e->getMessage());
  422. // }
  423. // }
  424. /**
  425. * 显示指定的资源
  426. *
  427. * @param int $id
  428. * @return \think\Response
  429. */
  430. public function read()
  431. {
  432. $post = $this->request->post();
  433. $token = isset($post['token']) ? trim($post['token']) : "";
  434. // if ($token == "") {
  435. // return error_show(101, 'token不能为空');
  436. // }
  437. // $effetc = VerifyTokens($token);
  438. // if (!empty($effetc) && $effetc['code'] != 0) {
  439. // return error_show($effetc['code'], $effetc['message']);
  440. // }
  441. $tradeNo = isset($post['traNo']) && $post['traNo']!="" ? trim($post['traNo']):"";
  442. if($tradeNo==""){
  443. return error_show(1004,"参数traNo 不能为空");
  444. }
  445. $info = Db::name("trade_list")->where([["is_del","=",0],["traNo","=",$tradeNo]])->find();
  446. if(empty($info)){
  447. return error_show(1004,"未找到数据");
  448. }
  449. $info['userinfo']=[];
  450. if($info['companyNo']!=''){
  451. $info['userinfo'] = Db::name("customer_info")->where('companyNo',"=",$info['companyNo'])->find();
  452. }
  453. $info['refundinfo']=[];
  454. if($info['trade_type']==1){
  455. $refund=Db::name("refund")->where([["backNo","=",$tradeNo],['is_del',"=",0]])->select()->toArray();
  456. if(!empty($refund)){
  457. foreach ($refund as $value){
  458. $trade=Db::name("trade_list")->where([['traNo',"=",$value['relaNo']]])->find();
  459. $customer = Db::name("customer_info")->where('companyNo',"=",$trade['companyNo'])->find();
  460. $trade['companyName'] = isset($customer['companyName']) ? $customer['companyName']:"";
  461. $info['refundinfo'][]=$trade;
  462. }
  463. };
  464. }
  465. $list = Db::name("assoc_list")->where("(codeNo='{$tradeNo}' or viceNo ='{$tradeNo}' ) and is_del=0")->select();
  466. $info["invNo"]=[];
  467. $info["orderNo"]=[];
  468. foreach ($list as $value){
  469. if($value["rela_form"]==2){
  470. $info["orderNo"][]=$value["codeNo"];
  471. }
  472. if($value["rela_form"]==3){
  473. $info["orderNo"][]=$value["viceNo"];
  474. }
  475. if($value["rela_form"]==4){
  476. $info["invNo"][]=$value["codeNo"];
  477. }
  478. if($value["rela_form"]==5){
  479. $info["invNo"][]=$value["viceNo"];
  480. }
  481. }
  482. $info["invNo"]= implode(",",$info["invNo"]);
  483. $info["orderNo"]= implode(",",$info["orderNo"]);
  484. return app_show(0,"获取成功",$info);
  485. }
  486. public function info(){
  487. $post = $this->request->post();
  488. $token = isset($post['token']) ? trim($post['token']) : "";
  489. if ($token == "") {
  490. return error_show(101, 'token不能为空');
  491. }
  492. $effetc = VerifyTokens($token);
  493. if (!empty($effetc) && $effetc['code'] != 0) {
  494. return error_show($effetc['code'], $effetc['message']);
  495. }
  496. $tradeNo = isset($post['traNo']) && $post['traNo']!="" ? trim($post['traNo']):"";
  497. if($tradeNo==""){
  498. return error_show(1004,"参数traNo 不能为空");
  499. }
  500. $info = Db::name("trade_list")->where([["is_del","=",0],["traNo","=",$tradeNo]])->find();
  501. if(empty($info)){
  502. return error_show(1004,"未找到数据");
  503. }
  504. $info['userinfo']=[];
  505. if($info['companyNo']!=''){
  506. $info['userinfo'] = Db::name("customer_info")->where('companyNo',"=",$info['companyNo'])->find();
  507. }
  508. $list = Db::name("assoc_list")->where("codeNo='{$tradeNo}' and is_del=0 and type=3 and rela_form=5 and source=2")->find();
  509. if($list){
  510. $inv = Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$list['viceNo']]])->find();
  511. if($inv){
  512. $info['inv'] = $inv;
  513. }else{
  514. return error_show(1004,"未找到关联发票");
  515. }
  516. }else{
  517. $info['inv']=[];
  518. }
  519. return app_show(0,"获取成功",$info);
  520. }
  521. /**
  522. * @return \think\response\Json|void
  523. * @throws \think\db\exception\DataNotFoundException
  524. * @throws \think\db\exception\DbException
  525. * @throws \think\db\exception\ModelNotFoundException
  526. * @throws \think\exception\DbException
  527. */
  528. public function TradeInfo(){
  529. $post = $this->request->post();
  530. $token = isset($post['token']) ? trim($post['token']) : "";
  531. if ($token == "") {
  532. return error_show(101, 'token不能为空');
  533. }
  534. $effetc = VerifyTokens($token);
  535. if (!empty($effetc) && $effetc['code'] != 0) {
  536. return error_show($effetc['code'], $effetc['message']);
  537. }
  538. $tradeNo = isset($post['traNo']) && $post['traNo']!="" ? trim($post['traNo']):"";
  539. if($tradeNo==""){
  540. return error_show(1004,"参数traNo 不能为空");
  541. }
  542. $info = Db::name("trade_log")->field('*,used_fee+tra_fee as amount')->where([["is_del","=",0],["logNo","=",$tradeNo]])->find();
  543. if(empty($info)){
  544. return error_show(1004,"未找到数据");
  545. }
  546. $tradelist = Db::name("trade_pool")->where([["is_del","=",0],["logNo","=",$tradeNo]])->select();
  547. $info['tradelist']=$tradelist;
  548. return app_show(0,"获取成功",$info);
  549. }
  550. /**
  551. * 显示编辑资源表单页.
  552. *
  553. * @param int $id
  554. * @return \think\Response
  555. */
  556. // public function Poollist()
  557. // {
  558. // $post = $this->request->post();
  559. // $token = isset($post['token']) ? trim($post['token']) : "";
  560. // if ($token == "") {
  561. // return error_show(101, 'token不能为空');
  562. // }
  563. // $effetc = VerifyTokens($token);
  564. // if (!empty($effetc) && $effetc['code'] != 0) {
  565. // return error_show($effetc['code'], $effetc['message']);
  566. // }
  567. // $condition = [["is_del","=",0],['refund_status',"=",0]];
  568. //
  569. // $status = isset($post['status']) && $post['status'] !== "" ? intval($post['status']): "";
  570. // if ($status != "") {
  571. // $condition[] = ["status", "=",$status];
  572. // }
  573. // $companyNo = isset($post['companyNo']) && $post['companyNo'] !== "" ? trim($post['companyNo']): "";
  574. // if ($companyNo != "") {
  575. // $condition[] = ["companyNo", "=",$companyNo];
  576. // }
  577. // $order_status = isset($post['order_status']) && $post['order_status'] !== "" ? intval($post['order_status']) : "";
  578. // if ($order_status != "") {
  579. // // $condition[] = ["order_status", "=",$order_status];
  580. // if($order_status==4){
  581. // $condition[] = ["order_status", "in", [1,2]];
  582. // }else{
  583. // $condition[] = ["order_status", "=", "{$order_status}"];
  584. // }
  585. // }
  586. //
  587. // $inv_status = isset($post['inv_status']) && $post['inv_status'] !== "" ? intval($post['inv_status']) : "";
  588. // if ($inv_status != "") {
  589. // // $condition[] = ["inv_status", "=",$inv_status];
  590. // if($inv_status==4){
  591. // $condition[] = ["inv_status", "in", [1,2]];
  592. // }else{
  593. // $condition[] = ["inv_status", "=", "{$inv_status}"];
  594. // }
  595. // }
  596. // $tradeNo = isset($post['traNo']) && $post['traNo'] !== "" ? trim($post['traNo']) : "";
  597. // if ($tradeNo != "") {
  598. // $condition[] = ["traNo", "like","%{$tradeNo}%"];
  599. // }
  600. // $total_s = isset($post['total_low']) && $post['total_low'] !== "" ? trim($post['total_low']) : "";
  601. // if ($total_s != "") {
  602. // $condition[] = ["total_fee", ">=",$total_s];
  603. // }
  604. // $total_e = isset($post['total_up']) && $post['total_up'] !== "" ? trim($post['total_up']) : "";
  605. // if ($total_e != "") {
  606. // $condition[] = ["total_fee", ">=",$total_e];
  607. // }
  608. // $end = isset($post["end"])&&$post['end']!="" ? $post['end'] : "";
  609. // $start = isset($post["start"])&&$post['start']!="" ? $post['start'] : "";
  610. // if($start!=""){
  611. // $condition[] = ["trade_time", ">=",$start];
  612. // }
  613. // if($end!=""){
  614. // $condition[] = ["trade_time", "<=",$end];
  615. // }
  616. // $trade_type = isset($post['trade_type']) && $post['trade_type'] !== "" ? trim($post['trade_type']) : 0;
  617. //
  618. // $condition[] = ["trade_type", "=",$trade_type];
  619. //
  620. // $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
  621. // $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
  622. //
  623. // $count = Db::name("trade_list")->where($condition)->count();
  624. // $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
  625. // $page = $page >= $total ? intval($total) : $page;
  626. // $list = Db::name("trade_list")->where($condition)->order("addtime desc")->page(intval($page), $size)->select();
  627. // $data=[];
  628. // foreach ($list as $value){
  629. // $customer = Db::name("customer_info")->where("companyNo","=",$value['companyNo'])->find();
  630. // $value['customer'] = $customer['companyName'];
  631. // $data[]=$value;
  632. // }
  633. //
  634. // return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  635. // }
  636. public function Poollist()
  637. {
  638. $post = $this->request->post();
  639. $token = isset($post['token']) ? trim($post['token']) : "";
  640. if ($token == "") {
  641. return error_show(101, 'token不能为空');
  642. }
  643. $effetc = VerifyTokens($token);
  644. if (!empty($effetc) && $effetc['code'] != 0) {
  645. return error_show($effetc['code'], $effetc['message']);
  646. }
  647. $condition = [["a.is_del","=",0],['a.refund_status',"=",0]];
  648. $status = isset($post['status']) && $post['status'] !== "" ? intval($post['status']): "";
  649. if ($status != "") {
  650. $condition[] = ["a.status", "=",$status];
  651. }
  652. $companyNo = isset($post['companyNo']) && $post['companyNo'] !== "" ? trim($post['companyNo']): "";
  653. if ($companyNo != "") {
  654. $condition[] = ["a.companyNo", "=",$companyNo];
  655. }
  656. $order_status = isset($post['order_status']) && $post['order_status'] !== "" ? intval($post['order_status']) : "";
  657. if ($order_status != "") {
  658. // $condition[] = ["order_status", "=",$order_status];
  659. if($order_status==4){
  660. $condition[] = ["a.order_status", "in", [1,2]];
  661. }else{
  662. $condition[] = ["a.order_status", "=", "{$order_status}"];
  663. }
  664. }
  665. $inv_status = isset($post['inv_status']) && $post['inv_status'] !== "" ? intval($post['inv_status']) : "";
  666. if ($inv_status != "") {
  667. // $condition[] = ["inv_status", "=",$inv_status];
  668. if($inv_status==4){
  669. $condition[] = ["a.inv_status", "in", [1,2]];
  670. }else{
  671. $condition[] = ["a.inv_status", "=", "{$inv_status}"];
  672. }
  673. }
  674. $tradeNo = isset($post['traNo']) && $post['traNo'] !== "" ? trim($post['traNo']) : "";
  675. if ($tradeNo != "") {
  676. $condition[] = ["a.traNo", "like","%{$tradeNo}%"];
  677. }
  678. $total_s = isset($post['total_low']) && $post['total_low'] !== "" ? trim($post['total_low']) : "";
  679. if ($total_s != "") {
  680. $condition[] = ["a.total_fee", ">=",$total_s];
  681. }
  682. $total_e = isset($post['total_up']) && $post['total_up'] !== "" ? trim($post['total_up']) : "";
  683. if ($total_e != "") {
  684. $condition[] = ["a.total_fee", ">=",$total_e];
  685. }
  686. $end = isset($post["end"])&&$post['end']!="" ? $post['end'] : "";
  687. $start = isset($post["start"])&&$post['start']!="" ? $post['start'] : "";
  688. if($start!=""){
  689. $condition[] = ["b.trade_time", ">=",$start];
  690. }
  691. if($end!=""){
  692. $condition[] = ["b.trade_time", "<=",$end];
  693. }
  694. $trade_type = isset($post['trade_type']) && $post['trade_type'] !== "" ? trim($post['trade_type']) : 0;
  695. $condition[] = ["b.trade_type", "=",$trade_type];
  696. $page = isset($post['page']) && $post['page'] != "" ? intval($post['page']) : 1;
  697. $size = isset($post['size']) && $post['size'] != "" ? intval($post['size']) : 10;
  698. $count = Db::name("trade_pool")->alias("a")->leftJoin("trade_log b","a.logNo = b.logNo")->where($condition)->count();
  699. $total = ceil($count / $size) > 1 ? ceil($count / $size) : 1;
  700. $page = $page >= $total ? intval($total) : $page;
  701. $list = Db::name("trade_pool")->alias("a")->leftJoin("trade_log b","a.logNo = b.logNo")->where($condition)->field("`a`.`id` AS `id`,
  702. `a`.`traNo` AS `traNo`,
  703. `a`.`companyNo` AS `companyNo`,
  704. `a`.`logNo` AS `logNo`,
  705. `a`.`total_fee` AS `total_fee`,
  706. `a`.`order_fee` AS `order_fee`,
  707. `a`.`inv_fee` AS `inv_fee`,
  708. `a`.`inv_used` AS `inv_used`,
  709. `a`.`winv_fee` AS `winv_fee`,
  710. `a`.`order_used` AS `order_used`,
  711. `a`.`worder_fee` AS `worder_fee`,
  712. `a`.`order_status` AS `order_status`,
  713. `a`.`inv_status` AS `inv_status`,
  714. `a`.`remark` AS `remark`,
  715. `a`.`is_del` AS `is_del`,
  716. `a`.`status` AS `status`,
  717. `a`.`exam_remark` AS `exam_remark`,
  718. `a`.`is_order` AS `is_order`,
  719. `a`.`addtime` AS `addtime`,
  720. `a`.`updatetime` AS `updatetime`,
  721. `b`.`trade_bank` AS `trade_bank`,
  722. `b`.`trade_account` AS `trade_account`,
  723. `b`.`trade_type` AS `trade_type`,
  724. `b`.`trade_time` AS `trade_time`,
  725. `b`.`trade_used` AS `trade_used`,
  726. `b`.`trade_out` AS `trade_out`,
  727. `b`.`trade_in` AS `trade_in`,
  728. `b`.`trade_remark` AS `trade_remark`,
  729. `b`.`total_fee` AS `btotal_fee`,
  730. `b`.`used_fee` AS `bused_fee`,
  731. `b`.`tra_fee` AS `btra_fee`,
  732. `b`.`balance` AS `bbalance`,
  733. `b`.`status` AS `bstatus`,
  734. `b`.`is_all` AS `is_all`,(
  735. `b`.`used_fee` + `b`.`tra_fee`
  736. ) AS `amount`,
  737. `a`.`refund_status` AS `refund_status`")
  738. ->order("a.addtime desc")->page(intval($page), $size)->select();
  739. $data=[];
  740. foreach ($list as $value){
  741. $customer = Db::name("customer_info")->where("companyNo","=",$value['companyNo'])->find();
  742. $value['customer'] = $customer['companyName'];
  743. $data[]=$value;
  744. }
  745. return app_show(0,"获取成功",['list'=>$data,"count"=>$count]);
  746. }
  747. /**
  748. * 保存更新的资源
  749. *
  750. * @param \think\Request $request
  751. * @param int $id
  752. * @return \think\Response
  753. */
  754. public function update()
  755. {
  756. $post = $this->request->post();
  757. $token = isset($post['token']) ? trim($post['token']) : "";
  758. if ($token == "") {
  759. return error_show(101, 'token不能为空');
  760. }
  761. $effetc = VerifyTokens($token);
  762. if (!empty($effetc) && $effetc['code'] != 0) {
  763. return error_show($effetc['code'], $effetc['message']);
  764. }
  765. $traNo = isset($post['traNo']) &&$post['traNo']!="" ? trim($post['traNo']):"";
  766. if($traNo==""){
  767. return error_show(1005,"参数traNo 不能为空");
  768. }
  769. $status= isset($post["status"])&&$post["status"]!=="" ? $post["status"] :"";
  770. if($status===""){
  771. return error_show(1005,"参数status 不能为空");
  772. }
  773. $remark= isset($post["remark"])&&$post["remark"]!="" ? $post["remark"] :"";
  774. $info = Db::name("trade_pool")->where([["traNo","=",$traNo],['is_del',"=",0]])->find();
  775. if(!$info){
  776. return error_show(1005,"未找到资金数据");
  777. }
  778. if($info['status']==3){
  779. return error_show(1005,"资金已审核通过");
  780. }
  781. $tradeinfo=[
  782. "status"=> $status,
  783. "exam_remark"=>$remark,
  784. "updatetime"=>date("Y-m-d H:i:s")
  785. ];
  786. Db::startTrans();
  787. try{
  788. $tra = Db::name("trade_pool")->where([["traNo","=",$traNo],['is_del',"=",0]])->save($tradeinfo);
  789. if($tra && $status==3){
  790. $tradelog = Db::name("trade_log")->where([["logNo","=",$info['logNo']],["is_del","=",0]])->find();
  791. if($tradelog){
  792. $tradelog['tra_fee'] -=$info['total_fee'];
  793. $tradelog['used_fee'] +=$info['total_fee'];
  794. $tradelog['status'] =$tradelog['used_fee']==$tradelog['total_fee'] ? 3:2;
  795. $tradelog['is_all'] =$tradelog['used_fee']==$tradelog['total_fee'] ? 1:0;
  796. $tradelog['updatetime'] =date("Y-m-d H:i:s");
  797. Db::name("trade_log")->save($tradelog);
  798. }else{
  799. Db::rollback();
  800. return app_show(1004,"资金数据更新失败");
  801. }
  802. $assoc = Db::name("assoc_list")->where([["source","=",2],["is_del","=",0],['rela_form',"=",5],
  803. ['codeNo',"=",$traNo]])->find();
  804. if($assoc){
  805. $tra = Db::name("trade_pool")->where("traNo","=",$traNo)->save
  806. (['inv_fee'=>$info['inv_fee']+$assoc['cancel_total'],'inv_used'=>$info['inv_used']-$assoc['cancel_total'],
  807. 'inv_status'=>$info['winv_fee']==0 ? 3:2]);
  808. $inv = Db::name("invoice_pool")->where([["is_del","=",0],['invNo',"=",$assoc['viceNo']]])->find();
  809. if($inv){
  810. $inv['atrade_fee']+=$assoc['cancel_fee'];
  811. $inv['fund_used']-=$assoc['cancel_fee'];
  812. $inv['updatetime']=date("Y-m-d H:i:s");
  813. $inv['trade_status']=$inv['wtrade_fee']==0?3:2;
  814. Db::name("invoice_pool")->save($inv);
  815. Db::name("assoc_key")->where([["source","=",2],["is_del","=",0],['rela_form',"=",5],
  816. ['codeNo',"=",$traNo]])->save(["status"=>3]);
  817. }else{
  818. Db::rollback();
  819. return app_show(1004,"未找到发票数据");
  820. }
  821. }
  822. Db::commit();
  823. return app_show(0,"审核通过");
  824. }
  825. if($tra && $status==4){
  826. Db::commit();
  827. return app_show(0,"审核未通过");
  828. }
  829. Db::rollback();
  830. return app_show(1004,"审核失败");
  831. }catch (\Exception $e){
  832. Db::rollback();
  833. return error_show(1003,$e->getMessage());
  834. }
  835. }
  836. /**
  837. * 删除指定资源
  838. *
  839. * @param int $id
  840. * @return \think\Response
  841. */
  842. public function delete()
  843. {
  844. $post = $this->request->post();
  845. $token = isset($post['token']) ? trim($post['token']) : "";
  846. if ($token == "") {
  847. return error_show(101, 'token不能为空');
  848. }
  849. $effetc = VerifyTokens($token);
  850. if (!empty($effetc) && $effetc['code'] != 0) {
  851. return error_show($effetc['code'], $effetc['message']);
  852. }
  853. $tradeNo = isset($post['traNo']) && $post['traNo']!="" ? trim($post['traNo']):"";
  854. if($tradeNo==""){
  855. return error_show(1004,"参数traNo 不能为空");
  856. }
  857. $info = Db::name("trade_pool")->where([["is_del","=",0],["traNo","=",$tradeNo]])->find();
  858. if(empty($info)){
  859. return error_show(1004,"未找到数据");
  860. }
  861. if($info['status']==3&&$info['trade_user']!=''){
  862. return error_show(1004,"款项已通过审核");
  863. }
  864. Db::startTrans();
  865. try {
  866. $assck= Db::name("assoc_list")->where([["type","=",3],["codeNo","=",$tradeNo],["is_del","=",0],["rela_form","=",5]])->find();
  867. if( $assck && $assck['status']!=3){
  868. $aa =Db::name("assoc_key")->where([["type","=",3],["codeNo","=",$tradeNo],["is_del","=",0],["rela_form","=",5]])->save(["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  869. if($aa){
  870. $data=[
  871. "is_del"=>1,
  872. "updatetime"=>date("Y-m-d H:i:s")
  873. ];
  874. $trapool = Db::name("trade_pool")->where([["is_del","=",0],["traNo","=",$tradeNo]])->save($data);
  875. if($trapool){
  876. $tralog = Db::name('trade_log')->where('logNo',"=",$info['logNo'])->find();
  877. if($tralog){
  878. $tralog['tra_fee']-=$info['total_fee'];
  879. $tralog['balance']+=$info['total_fee'];
  880. $tralog['status']=$tralog['balance']==$tralog['total_fee'] ?1 :2;
  881. $tralog['updatetime']=date("Y-m-d H:i:s");
  882. Db::name('trade_log')->save($tralog);
  883. }else{
  884. Db::rollback();
  885. return app_show(1004,"资金认领取消失败");
  886. }
  887. }else{
  888. Db::rollback();
  889. return app_show(1004,"资金认领取消失败");
  890. }
  891. $inv = Db::name("invoice_pool")->where([["is_del","=",0],["invNo","=",$assck['viceNo']]])->find();
  892. if($inv){
  893. $inv['wtrade_fee']+=$assck['cancel_fee'];
  894. $inv['fund_used']-=$assck['cancel_fee'];
  895. $inv['updatetime']=date("Y-m-d H:i:s");
  896. Db::name("invoice_pool")->save($inv);
  897. }
  898. Db::commit();
  899. return app_show(0,"取消认领成功");
  900. }else{
  901. Db::rollback();
  902. return error_show(1003,"取消认领失败");
  903. }
  904. }else{
  905. $data=[
  906. "is_del"=>1,
  907. "updatetime"=>date("Y-m-d H:i:s")
  908. ];
  909. $trapool = Db::name("trade_pool")->where([["is_del","=",0],["traNo","=",$tradeNo]])->save($data);
  910. if($trapool){
  911. $tralog = Db::name('trade_log')->where('logNo',"=",$info['logNo'])->find();
  912. if($tralog){
  913. $tralog['tra_fee']-=$info['total_fee'];
  914. $tralog['balance']+=$info['total_fee'];
  915. $tralog['status']=$tralog['balance']==$tralog['total_fee'] ?1 :2;
  916. $tralog['updatetime']=date("Y-m-d H:i:s");
  917. Db::name('trade_log')->save($tralog);
  918. Db::commit();
  919. return app_show(0,"取消认领成功");
  920. }else{
  921. Db::rollback();
  922. return app_show(1004,"资金认领取消失败");
  923. }
  924. }else{
  925. Db::rollback();
  926. return app_show(1004,"资金认领取消失败");
  927. }
  928. }
  929. Db::rollback();
  930. return error_show(1003,"取消认领失败");
  931. }catch (\Exception $e){
  932. Db::rollback();
  933. return error_show(1004,$e->getMessage());
  934. }
  935. }
  936. /**
  937. * @return \think\response\Json|void
  938. * @throws \think\db\exception\DataNotFoundException
  939. * @throws \think\db\exception\DbException
  940. * @throws \think\db\exception\ModelNotFoundException
  941. * @throws \think\exception\DbException
  942. */
  943. public function SaveRefund(){
  944. $post = $this->request->post();
  945. $token = isset($post['token']) ? trim($post['token']) : "";
  946. if ($token == "") {
  947. return error_show(101, 'token不能为空');
  948. }
  949. $effetc = VerifyTokens($token);
  950. if (!empty($effetc) && $effetc['code'] != 0) {
  951. return error_show($effetc['code'], $effetc['message']);
  952. }
  953. $guserinfo =GetUserInfo($token);
  954. if(isset($guserinfo['code']) && $guserinfo['code']!=0){
  955. return error_show($guserinfo['code'],$guserinfo['message']);
  956. }
  957. $userinfo = GetUserInfo($token);
  958. $username = "";
  959. if (isset($userinfo['code']) && $userinfo['code'] == 0) {
  960. $username = isset($userinfo['data']['nickname']) ? $userinfo['data']['nickname'] : "";
  961. }
  962. $data = isset($post['id']) && $post['id']!="" ? $post['id']:"";
  963. if($data==""){
  964. return error_show(1004,"参数id 不能为空");
  965. }
  966. $companyNo = isset($post['companyNo']) && $post['companyNo']!="" ? $post['companyNo']:"";
  967. if($companyNo==""){
  968. return error_show(1004,"参数companyNo 不能为空");
  969. }
  970. $trade = Db::name("trade_log")->where([["id","=",$data],["is_del","=",0]])->find();
  971. if(empty($trade)){
  972. return error_show(1005,"未找到资金数据");
  973. }
  974. // $total_fee = isset($post['total_fee'])&&$post['total_fee']!="" ? $post['total_fee']:"";
  975. // if($total_fee==""){
  976. // return error_show(1004,"参数total_fee 不能为空");
  977. // }
  978. // if($trade['balance']<$total_fee){
  979. // return error_show(1005,"资金余额不足");
  980. // }
  981. $refundid = isset($post['refundid']) && $post['refundid']!="" ? $post['refundid'] :"";
  982. if($refundid==""){
  983. return error_show(1006,"参数refundid 不能为空");
  984. }
  985. $refund =Db::name("refund")->where([["is_del","=",0],['refund_status',"=",3],['refundNo',"in",$refundid]])
  986. ->select()->toArray();
  987. if(empty($refund)){
  988. return error_show(1006,"退款资金数据未找到");
  989. }
  990. $customer = Db::name("customer_info")->where("companyNo","=",$companyNo)->find();
  991. if(empty($customer)){
  992. return error_show(1005,"未找到用户数据");
  993. }
  994. $refund_fee = array_sum(array_column($refund,"refund_fee"));
  995. if($refund_fee>$trade['balance']){
  996. return error_show(1005,"资金退款余额不足");
  997. }
  998. Db::startTrans();
  999. try{
  1000. $trade['is_all'] = $refund_fee == $trade['total_fee'] ? 1 :0;
  1001. $trade['balance'] -= $refund_fee;
  1002. $trade['tra_fee'] += $refund_fee;
  1003. $trade['updatetime'] = date("Y-m-d H:i:s");
  1004. $tradlog=Db::name("trade_log")->save($trade);
  1005. if($tradlog){
  1006. $count = Db::name("trade_pool")->where("logNo","=",$trade['logNo'])->count();
  1007. $tradepool = [
  1008. "traNo" =>$trade["logNo"]."-".sprintf('%03d',$count+1),
  1009. "companyNo" => $companyNo,
  1010. "logNo" => $trade["logNo"],
  1011. "trade_time" => $trade['trade_time'],
  1012. "total_fee" => $refund_fee,
  1013. "order_fee" => 0,
  1014. "inv_fee" => 0,
  1015. "inv_used" => 0,
  1016. "order_used" => 0,
  1017. "winv_fee" => $refund_fee,
  1018. "worder_fee" => $refund_fee,
  1019. "order_status" => 1,
  1020. "inv_status" => 1,
  1021. "remark" => "",
  1022. "status" => 2,
  1023. "addtime" => date("Y-m-d H:i:s"),
  1024. "updatetime" => date("Y-m-d H:i:s")
  1025. ];
  1026. $tra = Db::name("trade_pool")->insert($tradepool,true);
  1027. if($tra>0) {
  1028. $good = [];
  1029. foreach ($refund as $value) {
  1030. $value['backNo'] = $tradepool['traNo'];
  1031. $value['back_fee'] = $value['refund_fee'];
  1032. $value['refund_status'] = 4;
  1033. $value['updatetime'] = date("Y-m-d H:i:s");
  1034. $ref = Db::name("refund")->save($value);
  1035. if ($ref) {
  1036. $tadpool = Db::name("trade_pool")->where([["is_del", "=", 0], ["traNo", "=", $value['relaNo']], ['refund_status', "=", 2]])->save(["refund_status" => 3,
  1037. "updatetime" => date("Y-m-d H:i:s")]);
  1038. if (!$tadpool) {
  1039. Db::rollback();
  1040. return error_show(1007, "资金退款状态更新失败");
  1041. }
  1042. $tarlog = Db::name("trade_list")->where([["is_del", "=", 0], ["traNo", "=",$value['relaNo']]])->find();
  1043. if($tarlog){
  1044. $dat=[
  1045. 'balance'=>$tarlog['bbalance']+$tarlog['total_fee'],
  1046. "used_fee"=>$tarlog['bused_fee']-$tarlog['total_fee'],
  1047. "status"=>$tarlog['bused_fee']==$tarlog['total_fee'] ? 1:2,
  1048. "is_all"=>0
  1049. ];
  1050. Db::name("trade_log")->where("logNo","=",$tarlog['logNo'])->save($dat);
  1051. }
  1052. $rela = Db::name("assoc_list")->where([["codeNo|viceNo", "=", $value['relaNo']], ["is_del", "=", 0]])->find();
  1053. if ($rela) {
  1054. $corderNo= $rela['type']==1 ? $rela['codeNo'] : $rela['viceNo'];
  1055. $orderlist = Db::name("order_info")->where([['orderNo', "=",$corderNo], ["status", "=", 1]])->select();
  1056. $bala = 0;
  1057. foreach ($orderlist as $key => $val) {
  1058. $temp = [];
  1059. if ($rela['cancel_fee'] <= 0) {
  1060. break;
  1061. }
  1062. if ($rela['cancel_fee'] >= $val['afund_fee']) {
  1063. $rela['cancel_fee'] -= $val['afund_fee'];
  1064. $bala = $val['afund_fee'];
  1065. } else {
  1066. $bala = $rela['cancel_fee'];
  1067. $rela['cancel_fee'] = 0;
  1068. }
  1069. $temp['goodno'] = $val['goodNo'];
  1070. $temp['qrdno'] = $val['qrdNo'];
  1071. $temp['fund_fee'] = $bala;
  1072. $temp['num'] = $val['good_num'];
  1073. $good[] = $temp;
  1074. }
  1075. }
  1076. } else {
  1077. Db::rollback();
  1078. return error_show(1007, "资金退款状态更新失败");
  1079. }
  1080. }
  1081. if (!empty($good)) {
  1082. $total=0;
  1083. $orderNo = makeNo('ORD');
  1084. foreach ($good as $value) {
  1085. $temp = [];
  1086. $temp['orderNo'] = $orderNo;
  1087. $temp['goodNo'] = $value['goodno'];
  1088. $goocp = Db::name("qrd_list")->where("sequenceNo", "=", $value['goodno'])->find();
  1089. if (!$goocp) {
  1090. continue;
  1091. }
  1092. $temp['good_name'] = $goocp['goodname'];
  1093. $temp['qrdNo'] = $goocp['qrdNo'];
  1094. $temp['brand'] = $goocp['brand'];
  1095. $temp['unit'] = $goocp['unit'];
  1096. $temp['good_num'] = $value['num'];
  1097. $temp['price'] = $goocp['price'];
  1098. $temp['total_fee'] = $value['fund_fee'];
  1099. $temp['wfund_fee'] = 0;
  1100. $temp['afund_fee'] = $value['fund_fee'];
  1101. $temp['winv_fee'] = $value['fund_fee'];
  1102. $temp['other_fee'] = 0;
  1103. $temp['status'] = 1;
  1104. $temp['remark'] = "";
  1105. $temp['addtime'] = date("Y-m-d H:i:s");
  1106. $temp['updatetime'] = date("Y-m-d H:i:s");
  1107. $total += $value['fund_fee'];
  1108. $orderinfo[] = $temp;
  1109. }
  1110. $orderins = Db::name("order_info")->insertAll($orderinfo);
  1111. if ($orderins) {
  1112. $order = [
  1113. "orderNo" => $orderNo,
  1114. "companyNo" => $companyNo,
  1115. "sales_id" => $effetc['data']['user']['id'],
  1116. "sales_name" => $username,
  1117. "total_amount" => $total,
  1118. "inv_status" => 1,
  1119. "fund_status" => 3,
  1120. "fund_fee" => $total,
  1121. "wfund_fee" => 0,
  1122. "inv_fee" => 0,
  1123. "winv_fee" => $total,
  1124. "status" => 0,
  1125. "type" =>4,
  1126. "addtime" => date("Y-m-d H:i:s"),
  1127. "updatetime" => date("Y-m-d H:i:s")
  1128. ];
  1129. $orderpool = Db::name("order_pool")->insert($order, true);
  1130. if ($orderpool > 0) {
  1131. $rela = [];
  1132. $assNo = makeNo("ASC");
  1133. $cancel = 0;
  1134. $balance = $tradepool["worder_fee"] > $total ? $total : $tradepool["worder_fee"];
  1135. $total -= $balance;
  1136. $sav = [
  1137. "worder_fee" => $tradepool['worder_fee'] - $balance,
  1138. "order_fee" => $balance,
  1139. "is_order" => 1,
  1140. "updatetime" => date("Y-m-d H:i:s")
  1141. ];
  1142. Db::name("trade_pool")->where([["traNo", "=", $tradepool['traNo']]])->save($sav);
  1143. $rela = [
  1144. "assocNo" => $assNo,
  1145. "viceNo" => $orderNo,
  1146. "vice_total" => $order['total_amount'],
  1147. "vice_fee" => $total,
  1148. "cancel_fee" => $balance,
  1149. "status" => 1,
  1150. "addtime" => date("Y-m-d H:i:s"),
  1151. "updatetime" => date("Y-m-d H:i:s")
  1152. ];
  1153. $assoc = Db::name("assoc_rela")->insert($rela);
  1154. if ($assoc) {
  1155. $data = [
  1156. "assocNo" => $assNo,
  1157. "companyNo" => $companyNo,
  1158. "type" => 3,
  1159. "rela_form" => 3,
  1160. "codeNo" => $tradepool["traNo"],
  1161. "total_fee" => $tradepool['total_fee'],
  1162. "balance" => $tradepool['worder_fee'] - $balance,
  1163. "cancel_total" => $balance,
  1164. "status" => 3,
  1165. "source" => 3,
  1166. "addtime" => date("Y-m-d H:i:s"),
  1167. "updatetime" => date("Y-m-d H:i:s")
  1168. ];
  1169. $realkey = Db::name("assoc_key")->insert($data);
  1170. if ($realkey) {
  1171. $qrdup=$this->qrd_sure($orderNo,$balance);
  1172. if(!$qrdup){
  1173. Db::rollback();
  1174. return error_show(1004, "新建关联申请失败");
  1175. }
  1176. Db::commit();
  1177. return app_show(0, "欠款资金认领成功");
  1178. }
  1179. }
  1180. }
  1181. }
  1182. }
  1183. }
  1184. Db::commit();
  1185. return app_show(0,"欠款资金认领成功");
  1186. }
  1187. Db::rollback();
  1188. return error_show(1007,"资金退款认领失败");
  1189. }catch (\Exception $e){
  1190. Db::rollback();
  1191. return error_show(1006,$e->getMessage());
  1192. }
  1193. }
  1194. /**
  1195. * @param $orderNo
  1196. * @param $balance
  1197. * @return bool
  1198. * @throws \think\db\exception\DataNotFoundException
  1199. * @throws \think\db\exception\DbException
  1200. * @throws \think\db\exception\ModelNotFoundException
  1201. */
  1202. private function qrd_sure($orderNo,$balance){
  1203. $orderinfo = Db::name("order_pool")->where([["orderNo", "=", $orderNo], ["is_del", "=", 0]])->find();
  1204. if (empty($orderinfo)) {
  1205. return false;
  1206. }
  1207. $orderlist = Db::name("order_info")->where([["orderNo", "=", $orderNo], ["status", "=", 1]])->select();
  1208. if (empty($orderlist)) {
  1209. return false;
  1210. }
  1211. foreach ($orderlist as $value) {
  1212. $order_fee= $value['afund_fee'];
  1213. $qrdinfo = Db::name("qrd_info")->where([["sequenceNo", "=", $value['goodNo']]])->field("id,ShortText1617650701648,Number1618249202608,Number1618249205231,Number1618249146997,number1618249149738,ShortText1618559043560,number1618249258956,ShortText1618559274859,inving_fee,paying_fee")->find();
  1214. if($orderinfo['type']==4 && $order_fee <= $qrdinfo['Number1618249146997']){
  1215. $qrdinfo['Number1618249146997']-= $order_fee;
  1216. $qrdinfo['number1618249149738'] += $order_fee;
  1217. $qrdinfo['ShortText1618559043560'] = $qrdinfo['Number1618249146997']>0 ? "2" : "1";
  1218. }else{
  1219. if($order_fee<=$qrdinfo['paying_fee']){
  1220. $qrdinfo['paying_fee'] -= $order_fee;
  1221. $qrdinfo['Number1618249146997'] += $order_fee;
  1222. $qrdinfo['ShortText1618559043560'] = $qrdinfo['number1618249149738']>0 ? "2" : "3";
  1223. }else{
  1224. return false;
  1225. }
  1226. }
  1227. $qrdinfo['status']=1;
  1228. $qinfo = Db::name("qrd_info")->save($qrdinfo);
  1229. if ($qinfo) {
  1230. $qrdp = Db::name("qrd")->where([["sequenceNo", "=", $qrdinfo['ShortText1617650701648']]])->find();
  1231. if($orderinfo['type']==4 &&$qrdp['apay_fee'] >= $order_fee ){
  1232. $qrdp['apay_fee'] -= $order_fee;
  1233. $qrdp['wpay_fee'] += $order_fee;
  1234. $qrdp['pay_status'] = $qrdp['apay_fee'] == 0 ? 1 : 2;
  1235. }else{
  1236. if ($qrdp['pay_fee'] >= $order_fee) {
  1237. $qrdp['pay_fee'] -= $order_fee;
  1238. $qrdp['apay_fee'] += $order_fee;
  1239. $qrdp['pay_status'] = $qrdp['wpay_fee'] == 0 ? 3 : 2;
  1240. }else{
  1241. return false;
  1242. }
  1243. }
  1244. $qrdp['status'] =1;
  1245. $qrd = Db::name("qrd")->save($qrdp);
  1246. if(!$qrd){
  1247. return false;
  1248. }
  1249. }else {
  1250. return false;
  1251. }
  1252. }
  1253. return true;
  1254. }
  1255. }