OrderPay.php 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ReportCode;use think\App;
  4. use think\Exception;
  5. use think\facade\Db;
  6. use think\facade\Validate;
  7. class OrderPay extends Base{
  8. public function __construct(App $app) {parent::__construct($app);}
  9. //新建资金认领数据
  10. public function create(){
  11. $tradNo = isset($this->post['tradNo'])&&$this->post['tradNo']!=""?trim($this->post['tradNo']):"";
  12. if($tradNo==""){
  13. return error_show(1004,"参数 tradNo 不能为空");
  14. }
  15. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr'])?$this->post['orderArr']:[];
  16. if(empty($orderArr)){
  17. return error_show(1004,"参数 orderArr 不能为空");
  18. }
  19. Db::startTrans();
  20. try{
  21. $trade =Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->lock(true)->findOrEmpty();
  22. if(empty($trade)){
  23. Db::rollback();
  24. return error_show(1004,"未找到资金信息");
  25. }
  26. $total_fee =array_sum(array_column($orderArr,"trad_fee"));
  27. if ($trade['balance']<$total_fee){
  28. Db::rollback();
  29. return error_show(1004,"资金余额不足核销");
  30. }
  31. $assoc=[];
  32. foreach ($orderArr as $value){
  33. $logNo=makeNo("TRC");
  34. if(!isset($value['sequenceNo'])||$value['sequenceNo']==""){
  35. Db::rollback();
  36. return error_show(1004,"销售单编号不能为空");
  37. }
  38. if(!isset($value['trad_fee'])||$value['trad_fee']==""){
  39. Db::rollback();
  40. return error_show(1004,"销售单核销金额不能为空");
  41. }
  42. $qrd =Db::name("qrd_info")->where("sequenceNo","=",$value['sequenceNo'])->field("id,customerNo,status,pay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  43. if(empty($qrd)){
  44. Db::rollback();
  45. return error_show(1004,"销售单信息未找到");
  46. }
  47. if($qrd['wpay_fee']<$value['trad_fee']){
  48. Db::rollback();
  49. return error_show(1004,"销售单未付款金额不足核销金额");
  50. }
  51. $updt=[
  52. "pay_fee"=>$qrd['pay_fee']+$value['trad_fee'],
  53. "wpay_fee"=>$qrd['wpay_fee']-$value['trad_fee'],
  54. "pay_status"=>2,
  55. "status"=>1,
  56. "updatetime"=>date("Y-m-d H:i:s")
  57. ];
  58. $qrdup = Db::name("qrd_info")->where($qrd)->update($updt);
  59. if($qrdup==false){
  60. Db::rollback();
  61. return error_show(1004,"销售单核销金额失败");
  62. }
  63. $temp=[
  64. "assocNo"=>makeNo("AS"),
  65. "apply_id"=>$this->uid,
  66. "apply_name"=>$this->uname,
  67. "type"=>2,
  68. "orderCode"=>$value['sequenceNo'],
  69. "customerNo"=>$qrd['customerNo'],
  70. "viceCode"=>$logNo,
  71. "order_total"=>$qrd['totalPrice'],
  72. "vice_total"=>$total_fee,
  73. "cancel_fee"=>$value['trad_fee'],
  74. "status"=>1,
  75. "addtime"=>date("Y-m-d H:i:s"),
  76. "updatetime"=>date("Y-m-d H:i:s")
  77. ];
  78. $assoc[]=$temp;
  79. $create = [
  80. "logNo"=>$logNo,
  81. "tradNo"=>$tradNo,
  82. "platform_type"=>$qrd['platform_type']??0,
  83. "companyNo"=>$trade['companyNo'],
  84. "customerNo"=>$qrd['customerNo'],
  85. "apply_id"=>$this->uid,
  86. "apply_name"=>$this->uname,
  87. "trade_time"=>$trade['trade_time'],
  88. "total_fee"=>$value['trad_fee'],
  89. "status"=>1,
  90. "addtime"=>date("Y-m-d H:i:s"),
  91. "updatetime"=>date("Y-m-d H:i:s")
  92. ];
  93. $tradchild =Db::name("trade_pool")->insert($create);
  94. if($tradchild ==false){
  95. Db::rollback();
  96. return error_show(1004,"资金认领失败");
  97. }
  98. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  99. if($report)$report->setField("logNo",$logNo);
  100. if($report)$report->setField("tradNo",$tradNo);
  101. }
  102. $asscin= Db::name("assoc")->insertAll($assoc);
  103. if($asscin!=count($assoc)){
  104. Db::rollback();
  105. return error_show(1004,"销售单核销金额关联失败");
  106. }
  107. $update=[
  108. "balance"=>$trade['balance']-$total_fee,
  109. "used_fee"=>$trade['used_fee']+$total_fee,
  110. "status"=>($trade['balance']-$total_fee)==0 ? 3:2,
  111. "updatetime"=>date("Y-m-d H:i:s"),
  112. ];
  113. $tradup=Db::name("trade")->where($trade)->update($update);
  114. if($tradup){
  115. Db::commit();
  116. return app_show(0,"资金认领成功");
  117. }
  118. Db::rollback();
  119. return error_show(1004,"资金认领失败");
  120. }catch (\Exception $e){
  121. Db::rollback();
  122. return error_show(1004,$e->getMessage());
  123. }
  124. }
  125. // 1待审批2审批通过3审批驳回4退款5解除认领
  126. public function status(){
  127. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!=""?trim($this->post['logNo']):"";
  128. if($logNo==""){
  129. return error_show(1004,"参数logNo不能为空");
  130. }
  131. $logingo= Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  132. if($logingo==false){
  133. return error_show(1004,"资金认领信息未找到");
  134. }
  135. $status=isset($this->post['status'])&&$this->post['status']!=""?intval($this->post['status']):"";
  136. if($status==""){
  137. return error_show(1004,"参数 status 不能为空");
  138. }
  139. if($status==5 &&$logingo['status']!=1 ){
  140. return error_show(1004,"认领资金状态有误");
  141. }
  142. $remark=isset($this->post['remark'])&&$this->post['remark']!=""?trim($this->post['remark']):"";
  143. $trade =Db::name("trade")->where(["tradNo"=>$logingo['tradNo'],"is_del"=>0])->findOrEmpty();
  144. if(empty($trade)){
  145. return error_show(1005,"资金信息未找到");
  146. }
  147. Db::startTrans();
  148. try {
  149. $up=["status"=>$status,"updatetime"=>date("Y-m-d H:i:s"),"remark"=>$remark];
  150. $logup = Db::name("trade_pool")->where($logingo)->update($up);
  151. if($logup){
  152. if($status==2){
  153. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  154. if(!empty($qrdArr)){
  155. foreach ($qrdArr as $value){
  156. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,is_comon,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,cxCode")->findOrEmpty();
  157. if(empty($qrd)){
  158. Db::rollback();
  159. return error_show(1005,"未找到销售单数据");
  160. }
  161. if($qrd['pay_fee']<$value['cancel_fee']){
  162. Db::rollback();
  163. return error_show(1005,"销售单待付金额不足");
  164. }
  165. $update =[
  166. "apay_fee"=>$qrd['apay_fee']+$value['cancel_fee'],
  167. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  168. "paytime"=>date("Y-m-d H:i:s"),
  169. "pay_status"=>$qrd['wpay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 3:2,
  170. "updatetime"=>date("Y-m-d H:i:s")
  171. ];
  172. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  173. if($qrdup==false){
  174. Db::rollback();
  175. return error_show(1005,"销售单更新失败");
  176. }
  177. if($update['pay_status']==3 && $qrd['is_comon']==0){
  178. (new \app\admin\model\ComonOrder())->where(["cxCode"=>$qrd['cxCode'],"status"=>-1])->update(["status"=>0]);
  179. }
  180. $asscup =[
  181. "status"=>2,
  182. "assoc_time"=>date("Y-m-d H:i:s"),
  183. "updatetime"=>date("Y-m-d H:i:s")
  184. ];
  185. $assc=Db::name("assoc")->where($value)->update($asscup);
  186. if($assc==false){
  187. Db::rollback();
  188. return error_show(1005,"销售单更新失败");
  189. }
  190. }
  191. }
  192. }
  193. if($status==3 || $status==5){
  194. $qrdArr=Db::name("assoc")->where(["viceCode"=>$logNo,"status"=>1,"is_del"=>0])->select()->toArray();
  195. if(!empty($qrdArr)){
  196. foreach ($qrdArr as $value){
  197. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  198. if(empty($qrd)){
  199. Db::rollback();
  200. return error_show(1005,"未找到销售单数据");
  201. }
  202. if($qrd['pay_fee']<$value['cancel_fee']){
  203. Db::rollback();
  204. return error_show(1005,"销售单待付金额不足");
  205. }
  206. $update =[
  207. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  208. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  209. "pay_status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ? 1:2,
  210. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  211. "updatetime"=>date("Y-m-d H:i:s")
  212. ];
  213. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  214. if($qrdup==false){
  215. Db::rollback();
  216. return error_show(1005,"销售单更新失败");
  217. }
  218. $asscup =[
  219. "status"=>3,
  220. "updatetime"=>date("Y-m-d H:i:s")
  221. ];
  222. $assc=Db::name("assoc")->where($value)->update($asscup);
  223. if($assc==false){
  224. Db::rollback();
  225. return error_show(1005,"销售单更新失败");
  226. }
  227. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  228. if($report)$report->rmField("logNo",$logNo);
  229. }
  230. }
  231. if($trade['used_fee']<$logingo['total_fee']){
  232. Db::rollback();
  233. return error_show(1005,"资金信息已认领金额不足");
  234. }
  235. $tradup =[
  236. "used_fee"=>$trade['used_fee']-$logingo['total_fee'],
  237. "balance"=>$trade['balance']+$logingo['total_fee'],
  238. "status"=>($trade['used_fee']-$logingo['total_fee'])==0 ? 1 :2,
  239. "updatetime"=>date("Y-m-d H:i:s")
  240. ];
  241. $updaT=Db::name("trade")->where($trade)->update($tradup);
  242. if($updaT==false){
  243. Db::rollback();
  244. return error_show(1005,"资金信息更新失败");
  245. }
  246. }
  247. Db::commit();
  248. return app_show(0,"审核成功");
  249. }
  250. Db::rollback();
  251. return error_show(1004,"审核失败");
  252. }catch (\Exception $e){
  253. Db::rollback();
  254. return error_show(1004,$e->getMessage());
  255. }
  256. }
  257. //资金信息列表
  258. public function list(){
  259. $condition=[];
  260. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  261. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  262. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  263. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  264. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  265. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  266. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  267. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  268. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  269. $used_lower =isset($this->post['userd_lower'])&&$this->post['userd_lower']!==""? floor($this->post['userd_lower']) :"0";
  270. $used_upper =isset($this->post['used_upper'])&&$this->post['used_upper']!==""? floor($this->post['used_upper']):"";
  271. if($used_lower!=="")$condition[]=["used_fee",">=",$used_lower];
  272. if($used_upper!=="")$condition[]=["used_fee","<=",$used_upper];
  273. $total_lower =isset($this->post['total_lower'])&&$this->post['total_lower']!==""? floor($this->post['total_lower']) :"0";
  274. $total_upper =isset($this->post['total_upper'])&&$this->post['total_upper']!==""? floor($this->post['total_upper']):"";
  275. if($total_lower!=="")$condition[]=["total_fee",">=",$total_lower];
  276. if($total_upper!=="")$condition[]=["total_fee","<=",$total_upper];
  277. if($companyNo!==""){
  278. $condition[]=["companyNo","=",$companyNo];
  279. }
  280. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  281. if($relaComNo!=""){
  282. $condition[]=["companyNo","=",$relaComNo];
  283. }
  284. if($tradNo!==""){
  285. $condition[]=["tradNo","like","%$tradNo%"];
  286. }
  287. if($bank!=""){
  288. $condition[]=["trade_bank","like","%$bank%"];
  289. }
  290. if($name!=""){
  291. $condition[]=["trade_out","like","%$name%"];
  292. }
  293. if($start!=""){
  294. $condition[]=["trade_time",">=",$start." 00:00:00"];
  295. }
  296. if($end!=""){
  297. $condition[]=["trade_time","<=",$end." 23:59:59"];
  298. }
  299. if($status!==""){
  300. $condition[]=["status","=",$status];
  301. }
  302. $count=Db::name("trade")->where($condition)->count();
  303. $total=ceil($count/$size);
  304. $page=$page>$total? intval($total):$page;
  305. $list = Db::name("trade")
  306. ->where($condition)
  307. ->page($page, $size)
  308. ->order(['addtime' => 'desc', 'id' => 'desc'])
  309. ->select()
  310. ->toArray();
  311. foreach ($list as &$value) {
  312. $data = Db::name("trade_pool")
  313. ->alias("a")
  314. ->leftJoin("assoc b", "a.logNo=b.viceCode")
  315. ->leftJoin("qrd_info c", "b.orderCode=c.sequenceNo")
  316. ->where(["a.tradNo" => $value['tradNo'], "a.is_del" => 0])
  317. ->order(['a.addtime' => 'desc', 'a.id' => 'desc'])
  318. ->field("a.*,b.orderCode,c.cxCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.customerName,c.customerNo")
  319. ->select()
  320. ->toArray();
  321. $value['child'] = $data;
  322. $value['companyName'] = Db::name('company_info')->where(['companyNo' => $value['companyNo']])->value('company_name', '');
  323. }
  324. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  325. }
  326. // 认领资金列表明细
  327. public function tradeList(){
  328. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  329. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  330. $condition =[["a.is_del","=",0],["b.is_del","=",0],["c.is_del","=",0],["d.is_del","=",0]];
  331. $roleid = $this->roleid;
  332. $check = checkRole($roleid,'87');
  333. if($check){
  334. $condition[]=["a.apply_id","=",$this->uid];
  335. }
  336. $platform_type =isset($this->post['platform_type'])&&$this->post['platform_type']!==""? intval($this->post['platform_type']):"";
  337. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  338. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  339. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  340. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  341. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  342. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  343. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  344. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  345. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  346. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  347. if($companyNo!==""){
  348. $condition[]=["a.companyNo","=",$companyNo];
  349. }
  350. if($platform_type!==""){
  351. $condition[]=["a.platform_type","=",$platform_type];
  352. }
  353. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  354. if($relaComNo!=""){
  355. $condition[]=["a.companyNo","=",$relaComNo];
  356. }
  357. if($tradNo!==""){
  358. $condition[]=["a.tradNo","like","%$tradNo%"];
  359. }
  360. if($orderCode!==""){
  361. $condition[]=["d.orderCode","like","%$orderCode%"];
  362. }
  363. $cxCode= isset($this->post['cxCode'])&&$this->post['cxCode']!="" ? trim($this->post['cxCode']) :"";
  364. if($cxCode!=""){
  365. $condition[]=["c.cxCode","like","%$cxCode%"];
  366. }
  367. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  368. if($logNo!==""){
  369. $condition[]=["a.logNo","like","%$logNo%"];
  370. }
  371. if($bank!=""){
  372. $condition[]=["b.trade_bank","like","%$bank%"];
  373. }
  374. if($name!=""){
  375. $condition[]=["b.trade_out","like","%$name%"];
  376. }
  377. if($start!=""){
  378. $condition[]=["a.addtime",">=",date("Y-m-d 00:00:00",strtotime($start))];
  379. }
  380. if($end!=""){
  381. $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($start))];
  382. }
  383. if($status!==""){
  384. $condition[]=["a.status","=",$status];
  385. }
  386. if($apply_id!==""){
  387. $condition[]=["a.apply_id","=",$apply_id];
  388. }
  389. if($apply_name!==""){
  390. $condition[]=["a.apply_name","like","%$apply_name%"];
  391. }
  392. $count=Db::name("trade_pool")->alias("a")
  393. ->leftJoin("trade b","a.tradNo=b.tradNo")
  394. ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
  395. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  396. ->where($condition)->count();
  397. $total=ceil($count/$size);
  398. $page=$page>$total? intval($total):$page;
  399. $list =Db::name("trade_pool")->alias("a")
  400. ->leftJoin("trade b","a.tradNo=b.tradNo")
  401. ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
  402. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  403. ->where($condition)
  404. ->page($page,$size)
  405. ->field("a.*,b.trade_bank,b.trade_account,b.trade_out,b.total_fee as btotal_fee,b.used_fee,b.balance,c.customerName,c.customerNo,
  406. d.orderCode,c.qrdSource,c.cxCode,c.goodNo,c.goodName,c.qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,c.totalPrice,d.cancel_fee")
  407. ->order("a.addtime desc")->select();
  408. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  409. }
  410. // 获取资金下面的认领信息
  411. public function tradeQuery(){
  412. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  413. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  414. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  415. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  416. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  417. $platform_type =isset($this->post['platform_type'])&&$this->post['platform_type']!==""? intval($this->post['platform_type']):"";
  418. if($companyNo!==""){
  419. $condition[]=["companyNo","=",$companyNo];
  420. }
  421. if($platform_type!==""){
  422. $condition[]=["platform_type","=",$platform_type];
  423. }
  424. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  425. if($relaComNo!=""){
  426. $condition[]=["companyNo","=",$relaComNo];
  427. }
  428. $condition=[["is_del","=",0]];
  429. if($apply_id!==""){
  430. $condition[]=["apply_id","=",$apply_id];
  431. }
  432. if($apply_name!==""){
  433. $condition[]=["apply_name","like","%$apply_name%"];
  434. }
  435. if($status!==""){
  436. $condition[]=["status","=",$status];
  437. }
  438. if($tradNo!==""){
  439. $condition[]=["tradNo","like","%$tradNo%"];
  440. }
  441. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  442. return app_show(0,"获取成功",$list);
  443. }
  444. //资金详情
  445. public function tradeInfo(){
  446. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  447. if($tradNo==""){
  448. return error_show(1004,"参数 tradNo 不能为空");
  449. }
  450. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  451. if($tradinfo==false){
  452. return error_show(1004,"资金信息未找到");
  453. }
  454. return app_show(0,"获取成功",$tradinfo);
  455. }
  456. //资金认领详情
  457. public function logInfo(){
  458. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  459. if($logNo==""){
  460. return error_show(1004,"参数 logNo 不能为空");
  461. }
  462. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  463. if($tradinfo==false){
  464. return error_show(1004,"资金信息未找到");
  465. }
  466. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  467. $tradinfo['trade_out']= $trade['trade_out']??"";
  468. $tradinfo['trade_in']= $trade['trade_in']??"";
  469. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  470. $tradinfo['customerName']= Db::name("customer_info")->where(["companyNo"=>$tradinfo['customerNo']])->value("companyName","");
  471. $tradinfo['companyNo']= $trade['companyNo']??"";
  472. $tradinfo['total_fee']= $trade['total_fee']??"";
  473. $tradinfo['balance']= $trade['balance']??"";
  474. $tradinfo['used_fee']= $trade['used_fee']??"";
  475. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  476. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2,3]])
  477. ->order("a.addtime desc")
  478. ->field("c.*,a.cancel_fee")
  479. ->findOrEmpty();
  480. $tradinfo['orderinfo']=$orderinfo;
  481. return app_show(0,"获取成功",$tradinfo);
  482. }
  483. //认领资金退回或退款 type 1 退款 2 解除资金认领
  484. public function ReturnPay(){
  485. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  486. if($logNo==''){
  487. return error_show(1004,"参数 logNo 不能为空");
  488. }
  489. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  490. if($type==""){
  491. return error_show(1004,"参数 type 不能为空");
  492. }
  493. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  494. if($loginfo==false){
  495. return error_show(1004,"认领资金信息未找到");
  496. }
  497. if($loginfo['status']==2 && $type==1)return error_show(1004,"认领资金信息审核已通过");
  498. if($loginfo['status']!=2 && $type==2)return error_show(1004,"认领资金信息未审核通过");
  499. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  500. if($tradinfo==false){
  501. return error_show(1004,"资金信息未找到");
  502. }
  503. $isT= Db::name("trade_return")->where(["logNo"=>$logNo,"tradNo"=>$loginfo['tradNo'],"status"=>[0,1]])
  504. ->findOrEmpty();
  505. if(!empty($isT)) return error_show(1004,"资金退回流程已存在");
  506. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  507. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  508. $returnCode =makeNo("RTA");
  509. $data=[
  510. "returnCode"=>$returnCode,
  511. "logNo"=>$logNo,
  512. "companyNo"=>$tradinfo['companyNo'],
  513. "tradNo"=>$loginfo['tradNo'],
  514. "return_img"=>'',
  515. "type"=>$type,
  516. "apply_id"=>$this->uid,
  517. "apply_name"=>$this->uname,
  518. "return_reason"=>$reason,
  519. "remark"=>$remark,
  520. "addtime"=>date("Y-m-d H:i:s"),
  521. "updatetime"=>date("Y-m-d H:i:s")
  522. ];
  523. $in = Db::name("trade_return")->insert($data);
  524. if($in){
  525. return app_show(0,"申请新建成功");
  526. }else{
  527. return error_show(1004,"申请新建失败");
  528. }
  529. }
  530. /**退款列表
  531. * //page size tradNo logNo apply_id apply_name type status returnCode
  532. * @return \think\response\Json|void
  533. * @throws \think\db\exception\DataNotFoundException
  534. * @throws \think\db\exception\DbException
  535. * @throws \think\db\exception\ModelNotFoundException
  536. */
  537. public function returnList(){
  538. $condition=[["a.is_del","=",0]];
  539. $roleid = $this->roleid;
  540. $check = checkRole($roleid,'90');
  541. if($check){
  542. $condition[]=["a.apply_id","=",$this->uid];
  543. }
  544. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  545. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  546. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  547. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  548. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  549. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  550. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  551. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  552. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  553. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  554. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  555. if($companyNo!==""){
  556. $condition[]=["a.companyNo","=",$companyNo];
  557. }
  558. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  559. if($relaComNo!=""){
  560. $condition[]=["a.companyNo","=",$relaComNo];
  561. }
  562. $condition=[["a.is_del","=",0]];
  563. if($tradNo!=''){
  564. $condition[]=["a.tradNo","like","%$tradNo%"];
  565. }
  566. if($orderCode!=''){
  567. $condition[]=["b.orderCode","like","%$orderCode%"];
  568. }
  569. if($logNo!=''){
  570. $condition[]=["a.logNo","like","%$logNo%"];
  571. }
  572. if($apply_id!=''){
  573. $condition[]=["a.apply_id","=",$apply_id];
  574. }
  575. if($apply_name!=''){
  576. $condition[]=["a.apply_name","like","%$apply_name%"];
  577. }
  578. if($returnCode!=''){
  579. $condition[]=["a.returnCode","like","%$returnCode%"];
  580. }
  581. if($type!=''){
  582. $condition[]=["a.type","=",$type];
  583. }
  584. if($status!==''){
  585. $condition[]=["a.status","=",$status];
  586. }
  587. $count =Db::name("trade_return")->alias("a")
  588. ->leftJoin("assoc b","a.logNo=b.viceCode")
  589. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  590. ->where($condition)->count();
  591. $total=ceil($count/$size);
  592. $page = $page>$total ? intval($total):$page;
  593. $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode")
  594. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode")
  595. ->field("a.*,b.orderCode,c.qrdSource,goodNo,goodName,qrdType,c.ownerName,c.ownerid,c.department,c.poCode,c.platName,b.cancel_fee,c.customerName,c.customerNo")
  596. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  597. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  598. }
  599. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  600. public function returnStatus(){
  601. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  602. if($returnCode==''){
  603. return error_show(1004,"参数 returnCode 不能为空");
  604. }
  605. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  606. if($returninfo==false){
  607. return error_show(1004,"退款申请数据未找到");
  608. }
  609. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  610. if($status==''){
  611. return error_show(1004,"参数 status 不能为空");
  612. }
  613. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  614. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  615. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  616. if($loginfo==false){
  617. return error_show(1004,"认领资金信息未找到");
  618. }
  619. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  620. Db::startTrans();
  621. try{
  622. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  623. $up =Db::name("trade_return")->where($returninfo)->update($update);
  624. if($up){
  625. if($status==1){
  626. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>[1,2],"is_del"=>0])
  627. ->select()->toArray();
  628. if(!empty($qrdArr)){
  629. foreach ($qrdArr as $value){
  630. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice")->findOrEmpty();
  631. if(empty($qrd)){
  632. Db::rollback();
  633. return error_show(1005,"未找到销售单数据");
  634. }
  635. if($returninfo['type']==2){
  636. if($qrd['apay_fee']<$value['cancel_fee']){
  637. Db::rollback();
  638. return error_show(1005,"销售单已付金额不足");
  639. }
  640. $update =[
  641. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  642. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  643. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  644. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  645. "updatetime"=>date("Y-m-d H:i:s")
  646. ];
  647. }else{
  648. if($qrd['pay_fee']<$value['cancel_fee']){
  649. Db::rollback();
  650. return error_show(1005,"销售单付款中金额不足");
  651. }
  652. $update =[
  653. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  654. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  655. "pay_status"=>($qrd['pay_fee']-$value['cancel_fee'])==0 && $qrd['apay_fee']==0 ? 1:2,
  656. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  657. "updatetime"=>date("Y-m-d H:i:s")
  658. ];
  659. }
  660. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  661. if($qrdup==false){
  662. Db::rollback();
  663. return error_show(1005,"销售单更新失败");
  664. }
  665. $asscup =[
  666. "status"=>3,
  667. "updatetime"=>date("Y-m-d H:i:s")
  668. ];
  669. $assc=Db::name("assoc")->where($value)->update($asscup);
  670. if($assc==false){
  671. Db::rollback();
  672. return error_show(1005,"销售单更新失败");
  673. }
  674. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  675. if($report)$report->setField("returnTrad",$returnCode);
  676. }
  677. }
  678. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  679. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  680. if($upde==false){
  681. Db::rollback();
  682. return error_show(1005,"资金认领信息更新失败");
  683. }
  684. // if($returninfo['type']==1){
  685. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  686. if($trade==false){
  687. Db::rollback();
  688. return error_show(1005,"资金信息未找到");
  689. }
  690. if($trade['used_fee']<$loginfo['total_fee']){
  691. Db::rollback();
  692. return error_show(1005,"资金信息已认领金额不足");
  693. }
  694. $tradup =[
  695. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  696. "balance"=>$trade['balance']+$loginfo['total_fee'],
  697. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ?1 :2,
  698. "updatetime"=>date("Y-m-d H:i:s")
  699. ];
  700. $updaT=Db::name("trade")->where($trade)->update($tradup);
  701. if($updaT==false){
  702. Db::rollback();
  703. return error_show(1005,"资金信息更新失败");
  704. }
  705. // }
  706. }
  707. Db::commit();
  708. return app_show(0,"退款申请审核成功");
  709. }
  710. Db::rollback();
  711. return error_show(1004,'审核状态失败');
  712. }catch (\Exception $e){
  713. Db::rollback();
  714. return error_show(1004,$e->getMessage());
  715. }
  716. }
  717. //退款申请详情
  718. public function returnInfo(){
  719. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  720. if($returnCode==''){
  721. return error_show(1004,"参数 returnCode 不能为空");
  722. }
  723. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  724. if($returninfo==false){
  725. return error_show(1004,"退款申请数据未找到");
  726. }
  727. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  728. $returninfo['trade_out']= $trade['trade_out']??"";
  729. $returninfo['trade_in']= $trade['trade_in']??"";
  730. $returninfo['trade_bank']= $trade['trade_bank']??"";
  731. $returninfo['trade_time']= $trade['trade_time']??"";
  732. $returninfo['customerNo']= $trade['customerNo']??"";
  733. $returninfo['companyNo']= $trade['companyNo']??"";
  734. $returninfo['total_fee']= $trade['total_fee']??"";
  735. $returninfo['balance']= $trade['balance']??"";
  736. $returninfo['used_fee']= $trade['used_fee']??"";
  737. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  738. $returninfo['log_total_fee']= $pool['total_fee']??"";
  739. $returninfo['log_apply_id']= $pool['apply_id']??"";
  740. $returninfo['log_apply_name']= $pool['apply_name']??"";
  741. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  742. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  743. ->order("a.addtime desc")
  744. ->field("c.*")
  745. ->findOrEmpty();
  746. $returninfo['orderinfo'] = $orderinfo;
  747. return app_show(0,"获取成功",$returninfo);
  748. }
  749. /**
  750. * 显示创建资源表单页.
  751. *
  752. * @return \think\Response
  753. */
  754. public function importTrade()
  755. {
  756. $files = $this->request->file("excel");
  757. $data = upload($files,$files->getOriginalExtension());
  758. if($data['code']!=0){
  759. return error_show(1004,$data['msg']);
  760. }
  761. $trade = $data['data'];
  762. $list = [];
  763. foreach ( $trade as $key => $value) {
  764. if (!isset($value[0]) || $value[0] == '') {
  765. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  766. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  767. }
  768. if (!isset($value[4]) || $value[4] == '') {
  769. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  770. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  771. }
  772. if (!isset($value[8]) || $value[8] == '') {
  773. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  774. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  775. }
  776. if (!isset($value[9]) || $value[9] == '') {
  777. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  778. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  779. }
  780. if ($key == 0) {
  781. if ($value[0] != '收款方公司编号') {
  782. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  783. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  784. }
  785. if ($value[4] != '收入金额') {
  786. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  787. return error_show( 1003, '模板第五列为必须为收入金额!');
  788. }
  789. if ($value[6] != '交易行名') {
  790. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  791. return error_show( 1003, '模板第七列为必须为交易行名!');
  792. }
  793. if ($value[8] != '对方账号') {
  794. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  795. return error_show( 1003, '模板第九列为必须为对方账号!');
  796. }
  797. if ($value[9] != '对方户名') {
  798. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  799. return error_show( 1003, '模板第十列为必须为对方户名!');
  800. }
  801. continue;
  802. }
  803. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  804. if(empty($company)){
  805. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  806. }
  807. $total_fee = $value[4];
  808. $type = 0;
  809. $time = explode(" ", $value[3]);
  810. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  811. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  812. $list[$key]["trade_bank"] = $value[6];
  813. $list[$key]["trade_account"] = $value[8];
  814. $list[$key]["trade_out"] = $value[9];
  815. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  816. $list[$key]["trade_type"] = $type;
  817. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  818. $list[$key]["trade_remark"] = '';
  819. $list[$key]["companyNo"] = $value[0];
  820. $list[$key]["trade_in_account"] = $value[2]??"";
  821. }
  822. if(empty($list)){
  823. return error_show( 1003, '导入数据不能为空!');
  824. }
  825. Db::startTrans();
  826. try{
  827. $tra=[];
  828. foreach ($list as $value) {
  829. $temp = [];
  830. $temp["tradNo"] = makeStr('S');
  831. $temp['trade_time'] = $value['tradeTime'];
  832. $temp['total_fee'] = $value['trade_fee'];
  833. $temp['trade_bank'] = $value['trade_bank'];
  834. $temp['trade_account'] = $value['trade_account'];
  835. $temp['trade_type'] =$value['trade_type'];
  836. $temp['trade_out'] = $value['trade_out'];
  837. $temp['trade_in'] = $value['trade_in'];
  838. $temp['trade_in_account'] = $value['trade_in_account'];
  839. $temp['companyNo'] = $value['companyNo'];
  840. $temp['trade_used'] = $value['trade_used'];
  841. $temp['trade_remark'] = $value['trade_remark'];
  842. $temp['balance'] =$value['trade_fee'];
  843. $temp['addtime'] = date("Y-m-d H:i:s");
  844. $temp['updatetime'] = date("Y-m-d H:i:s");
  845. $tra[]=$temp;
  846. }
  847. $list = Db::name('trade')->insertAll($tra);
  848. if($list==count($tra)){
  849. Db::commit();
  850. return app_show(0, "资金导入成功");
  851. }else{
  852. Db::rollback();
  853. return app_show(1004, "资金导入失败");
  854. }
  855. }catch (\Exception $e){
  856. Db::rollback();
  857. return app_show(1004, $e->getMessage());
  858. }
  859. }
  860. /**
  861. * 资金导入
  862. * @return \think\response\Json|void
  863. */
  864. public function importTradeByArr()
  865. {
  866. $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
  867. if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
  868. $list = [];
  869. foreach ( $trade as $key => $value) {
  870. if (!isset($value[0]) || $value[0] == '') {
  871. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  872. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  873. }
  874. if (!isset($value[2]) || $value[2] == '') {
  875. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  876. return error_show( 1003, '第'.($key+1).'行收款账户不能为空!');
  877. }
  878. if (!isset($value[3]) || $value[3] == '') {
  879. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  880. return error_show( 1003, '第'.($key+1).'行交易时间不能为空!');
  881. }
  882. if (!isset($value[4]) || $value[4] == '') {
  883. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  884. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  885. }
  886. if (!isset($value[6]) || $value[6] == '') {
  887. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  888. return error_show( 1003, '第'.($key+1).'行交易银行名称不能为空!');
  889. }
  890. if (!isset($value[8]) || $value[8] == '') {
  891. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  892. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  893. }
  894. if (!isset($value[9]) || $value[9] == '') {
  895. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  896. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  897. }
  898. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  899. if(empty($company)){
  900. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  901. }
  902. $total_fee = $value[4];
  903. $type = 0;
  904. $time = explode(" ", $value[3]);
  905. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  906. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  907. $list[$key]["trade_bank"] = $value[6];
  908. $list[$key]["trade_account"] = $value[8];
  909. $list[$key]["trade_out"] = $value[9];
  910. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  911. $list[$key]["trade_type"] = $type;
  912. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  913. $list[$key]["trade_remark"] = '';
  914. $list[$key]["companyNo"] = $value[0];
  915. $list[$key]["trade_in_account"] = $value[2]??"";
  916. }
  917. if(empty($list)){
  918. return error_show( 1003, '导入数据不能为空!');
  919. }
  920. Db::startTrans();
  921. try{
  922. $tra=[];
  923. $i=0;
  924. foreach ($list as $value) {
  925. $temp = [];
  926. $i++;
  927. $temp["tradNo"] = makeStr('S').str_pad($i,3,'0',STR_PAD_LEFT);
  928. $temp['trade_time'] = $value['tradeTime'];
  929. $temp['total_fee'] = $value['trade_fee'];
  930. $temp['trade_bank'] = $value['trade_bank'];
  931. $temp['trade_account'] = $value['trade_account'];
  932. $temp['trade_type'] =$value['trade_type'];
  933. $temp['trade_out'] = $value['trade_out'];
  934. $temp['trade_in'] = $value['trade_in'];
  935. $temp['trade_in_account'] = $value['trade_in_account'];
  936. $temp['companyNo'] = $value['companyNo'];
  937. $temp['trade_used'] = $value['trade_used'];
  938. $temp['trade_remark'] = $value['trade_remark'];
  939. $temp['balance'] =$value['trade_fee'];
  940. $temp['addtime'] = date("Y-m-d H:i:s");
  941. $temp['updatetime'] = date("Y-m-d H:i:s");
  942. $tra[]=$temp;
  943. }
  944. $list = Db::name('trade')->insertAll($tra);
  945. if($list==count($tra)){
  946. Db::commit();
  947. return app_show(0, "资金导入成功");
  948. }else{
  949. Db::rollback();
  950. return app_show(1004, "资金导入失败");
  951. }
  952. }catch (\Exception $e){
  953. Db::rollback();
  954. return app_show(1004, $e->getMessage());
  955. }
  956. }
  957. //批量认领资金,跟订单相关
  958. public function importTradeByBatchOrderCode()
  959. {
  960. $list = $this->request->post('list', [], 'trim');
  961. $val = Validate::rule(['list|导入数据' => 'require|array|max:100']);
  962. if (!$val->check(['list' => $list])) return error_show(1004, $val->getError());
  963. $company = $tradNos = $orderCodes = $assoc_insert_data = $OrderCodeToAssocNo = $OrderCodeToLogNo = [];
  964. $val_item = Validate::rule([
  965. 'companyNo|卖出方公司编号' => 'require|max:255',
  966. 'tradNo|资金编号' => 'require|max:255',
  967. 'orderCode|订单编号' => 'require|max:255',
  968. 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
  969. ]);
  970. foreach ($list as $key => $value) {
  971. if (!$val_item->check($value)) return error_show(1004, $val_item->getError());
  972. if (!isset($company[$value['companyNo']])) $company[$value['companyNo']] = $value['companyNo'];
  973. }
  974. //判断绑定公司
  975. $companyNo = Db::name('user_role')
  976. ->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])
  977. ->whereIn('companyNo', $company)
  978. ->column('companyNo');
  979. $tmp = array_diff($company, $companyNo);//二者取差集
  980. if(!empty($tmp)) return error_show(1004,'不能操作以下公司数据:'.implode(',',$tmp));
  981. Db::startTrans();
  982. try {
  983. $i=1;
  984. //批量资金认领
  985. foreach ($list as $key=>$value){
  986. //资金明细判断
  987. $i++;
  988. $all_trade = Db::name('trade')
  989. ->where('is_del', 0)
  990. ->whereIn('status', [1, 2])
  991. ->where('tradNo', $value['tradNo'])
  992. ->field('id,balance,used_fee,companyNo,trade_time')
  993. ->findOrEmpty();
  994. if(empty($all_trade))throw new Exception("{$value['tradNo']}资金未找到数据");
  995. if($all_trade['balance']<$value['trad_fee'])throw new Exception("{$value['tradNo']}资金余额不足");
  996. //订单明细判断
  997. $all_order=Db::name('qrd_info')
  998. ->where('is_del', 0)
  999. ->whereIn('sequenceNo',$value['orderCode'])
  1000. ->field('id,customerNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode')
  1001. ->findOrEmpty();
  1002. if(empty($all_order))throw new Exception("{$value['orderCode']}销售单未找到数据");
  1003. if($all_order['wpay_fee']<$value['trad_fee'])throw new Exception("{$value['orderCode']}销售单未付款金额不足核销金额");
  1004. $OrderCodeToAssocNo = substr(makeNo('AS'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1005. $OrderCodeToLogNo = substr(makeNo('TRC'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1006. //资金数据更新处理
  1007. $balance = bcsub($all_trade['balance'], $value['trad_fee'], 2);
  1008. $tradeUp=Db::name('trade')
  1009. ->where($all_trade)
  1010. ->update([
  1011. 'balance' => $balance,
  1012. 'used_fee' => bcadd($all_trade['used_fee'], $value['trad_fee'], 2),
  1013. 'status' => $balance == 0 ? 3 : 2,
  1014. 'updatetime' =>date("Y-m-d H:i:s"),
  1015. ]);
  1016. if($tradeUp==false ) throw new Exception("{$value['tradNo']}资金更新失败");
  1017. //订单数据更新处理
  1018. $update=[
  1019. 'apay_fee' => bcadd($all_order['apay_fee'], $value['trad_fee'], 2),
  1020. 'wpay_fee' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2),
  1021. 'pay_status' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2)==0 &&$all_order['pay_fee']==0? 3:2,
  1022. 'status' => 1,
  1023. 'updatetime' => date('Y-m-d H:i:s')
  1024. ];
  1025. $orderUp=Db::name('qrd_info')
  1026. ->where($all_order)
  1027. ->update($update);
  1028. if($orderUp==false ) throw new Exception("{$value['orderCode']}销售单更新失败");
  1029. if($update['pay_status']==3){
  1030. (new \app\admin\model\ComonOrder())->where(['cxCode'=>$all_order['cxCode'],'status'=>-1])->update(['status'=>0]);
  1031. }
  1032. //资金订单关联数据
  1033. $assoc_insert_data[]=[
  1034. 'assocNo' => $OrderCodeToAssocNo,
  1035. 'apply_id' => $this->uid,
  1036. 'apply_name' => $this->uname,
  1037. 'type' => 2,
  1038. 'orderCode' => $value['orderCode'],
  1039. 'customerNo' => $all_order['customerNo'],
  1040. 'viceCode' => $OrderCodeToLogNo,
  1041. 'order_total' => $all_order['totalPrice'],
  1042. 'vice_total' => $value['trad_fee'],
  1043. 'cancel_fee' => $value['trad_fee'],
  1044. 'status' => 2,//认领通过
  1045. 'addtime' => date("Y-m-d H:i:s"),
  1046. 'updatetime' => date("Y-m-d H:i:s"),
  1047. ];
  1048. //资金认领数据
  1049. $trade_pool_insert_data[]=[
  1050. 'logNo' => $OrderCodeToLogNo,
  1051. 'tradNo' => $value['tradNo'],
  1052. 'platform_type' => $all_order['platform_type'],
  1053. 'companyNo' => $value['companyNo'],
  1054. 'customerNo' => $all_order['customerNo'],
  1055. 'apply_id' => $this->uid,
  1056. 'apply_name' => $this->uname,
  1057. 'trade_time' => $all_trade['trade_time'],
  1058. 'total_fee' => $value['trad_fee'],
  1059. 'status' => 2,//2审核通过
  1060. 'addtime' => date("Y-m-d H:i:s"),
  1061. 'updatetime' => date("Y-m-d H:i:s"),
  1062. ];
  1063. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  1064. if($report){
  1065. $report->setField("logNo",$OrderCodeToLogNo);
  1066. $report->setField("tradNo",$value['tradNo']);
  1067. }
  1068. }
  1069. if ($assoc_insert_data) Db::name('assoc')->insertAll($assoc_insert_data);
  1070. if ($trade_pool_insert_data) Db::name('trade_pool')->insertAll($trade_pool_insert_data);
  1071. Db::commit();
  1072. return app_show(0, '批量认领资金成功');
  1073. } catch (Exception $exception) {
  1074. Db::rollback();
  1075. return error_show(1004, '批量认领资金失败,' . $exception->getMessage());
  1076. }
  1077. }
  1078. public function exportTrade(){
  1079. $param = $this->request->param(["name"=>"","bank"=>'',"start"=>'',"end"=>'',"status"=>'',"tradNo"=>'',
  1080. "companyNo"=>'',"total_lower"=>'','total_upper'=>''],"post","trim");
  1081. $condition=[["is_del","=",0]];
  1082. $param['name']==""?: $condition[]=["trade_out","like","%{$param['name']}%"];
  1083. $param['bank']==""?: $condition[]=['trade_bank','like',"%{$param['bank']}%"];;
  1084. $param['start']==""?: $condition[]=['trade_time','>=',startTime($param['start'])];
  1085. $param['end']==""?: $condition[]=['trade_time','<=',endTime($param['end'])];
  1086. $param['status']===""?: $condition[]=["status","=",$param['status']];
  1087. $param['tradNo']==""?: $condition[]=["tradNo","like","%{$param['tradNo']}%"];
  1088. $param['companyNo']==""?: $condition[]=['companyNo','=',$param['companyNo']];
  1089. $param['total_lower']==""?: $condition[]=['total_fee','>=',$param['total_lower']];
  1090. $param['total_upper']==""?: $condition[]=['total_fee','<=',$param['total_upper']];
  1091. $list= \app\admin\model\Trade::where($condition)
  1092. ->field(["tradNo","companyNo","trade_in",'trade_in_account','trade_time','total_fee','trade_bank',
  1093. 'trade_account','trade_out','trade_used'])
  1094. ->select();
  1095. $header = ["资金编号","收款方公司编码",'收款方公司名称','收款方账户','交易时间','收入金额','交易行名','对方账号','对方户名','交易用途'];
  1096. if($list->isEmpty()){
  1097. $header=["暂无数据"];
  1098. }
  1099. excelExport("资金管理导出",$header,$list->toArray());
  1100. }
  1101. }