OrderPay.php 57 KB

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