OrderPay.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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=[['is_del',"=",0]];
  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. public function tradeDelete(){
  330. $id =isset($this->post['id'])&&$this->post['id']!=""? intval($this->post['id']):"";
  331. if($id==""){
  332. return error_show(1004,"参数错误");
  333. }
  334. $trade = Db::name("trade")->where(["id"=>$id])->find();
  335. if($trade){
  336. $updaT=Db::name("trade")->where(["id"=>$id])->update(["is_del"=>1]);
  337. if($updaT) return app_show(0,"删除成功");
  338. }
  339. return error_show(1004,"删除失败");
  340. }
  341. // 认领资金列表明细
  342. public function tradeList(){
  343. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  344. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  345. $condition =[["a.is_del","=",0],["b.is_del","=",0],["c.is_del","=",0],["d.is_del","=",0]];
  346. $roleid = $this->roleid;
  347. $check = checkRole($roleid,'87');
  348. if($check){
  349. $condition[]=["a.apply_id","=",$this->uid];
  350. }
  351. $platform_type =isset($this->post['platform_type'])&&$this->post['platform_type']!==""? intval($this->post['platform_type']):"";
  352. $name =isset($this->post['name'])&&$this->post['name']!=""? trim($this->post['name']):"";
  353. $bank =isset($this->post['bank'])&&$this->post['bank']!=""? trim($this->post['bank']):"";
  354. $start =isset($this->post['start'])&&$this->post['start']!=""? trim($this->post['start']):"";
  355. $end =isset($this->post['end'])&&$this->post['end']!=""? trim($this->post['end']):"";
  356. $status =isset($this->post['status'])&&$this->post['status']!==""? intval($this->post['status']):"";
  357. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  358. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  359. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  360. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  361. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  362. if($companyNo!==""){
  363. $condition[]=["a.companyNo","=",$companyNo];
  364. }
  365. if($platform_type!==""){
  366. $condition[]=["a.platform_type","=",$platform_type];
  367. }
  368. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  369. if($relaComNo!=""){
  370. $condition[]=["a.companyNo","=",$relaComNo];
  371. }
  372. if($tradNo!==""){
  373. $condition[]=["a.tradNo","like","%$tradNo%"];
  374. }
  375. if($orderCode!==""){
  376. $condition[]=["d.orderCode","like","%$orderCode%"];
  377. }
  378. $cxCode= isset($this->post['cxCode'])&&$this->post['cxCode']!="" ? trim($this->post['cxCode']) :"";
  379. if($cxCode!=""){
  380. $condition[]=["c.cxCode","like","%$cxCode%"];
  381. }
  382. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  383. if($logNo!==""){
  384. $condition[]=["a.logNo","like","%$logNo%"];
  385. }
  386. if($bank!=""){
  387. $condition[]=["b.trade_bank","like","%$bank%"];
  388. }
  389. if($name!=""){
  390. $condition[]=["b.trade_out","like","%$name%"];
  391. }
  392. if($start!=""){
  393. $condition[]=["a.addtime",">=",date("Y-m-d 00:00:00",strtotime($start))];
  394. }
  395. if($end!=""){
  396. $condition[]=["a.addtime","<=",date("Y-m-d 23:59:59",strtotime($start))];
  397. }
  398. if($status!==""){
  399. $condition[]=["a.status","=",$status];
  400. }
  401. if($apply_id!==""){
  402. $condition[]=["a.apply_id","=",$apply_id];
  403. }
  404. if($apply_name!==""){
  405. $condition[]=["a.apply_name","like","%$apply_name%"];
  406. }
  407. $count=Db::name("trade_pool")->alias("a")
  408. ->leftJoin("trade b","a.tradNo=b.tradNo")
  409. ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
  410. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  411. ->where($condition)->count();
  412. $total=ceil($count/$size);
  413. $page=$page>$total? intval($total):$page;
  414. $list =Db::name("trade_pool")->alias("a")
  415. ->leftJoin("trade b","a.tradNo=b.tradNo")
  416. ->leftJoin("assoc d","a.logNo=d.viceCode AND d.type = 2")
  417. ->leftJoin("qrd_info c","d.orderCode=c.sequenceNo")
  418. ->where($condition)
  419. ->page($page,$size)
  420. ->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,
  421. 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")
  422. ->order("a.addtime desc")->select();
  423. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  424. }
  425. // 获取资金下面的认领信息
  426. public function tradeQuery(){
  427. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  428. $status =isset($this->post['status'])&&$this->post['status']!=""? intval($this->post['status']):"";
  429. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=""? intval($this->post['apply_id']):"";
  430. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=""? trim($this->post['apply_name']):"";
  431. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  432. $platform_type =isset($this->post['platform_type'])&&$this->post['platform_type']!==""? intval($this->post['platform_type']):"";
  433. if($companyNo!==""){
  434. $condition[]=["companyNo","=",$companyNo];
  435. }
  436. if($platform_type!==""){
  437. $condition[]=["platform_type","=",$platform_type];
  438. }
  439. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  440. if($relaComNo!=""){
  441. $condition[]=["companyNo","=",$relaComNo];
  442. }
  443. $condition=[["is_del","=",0]];
  444. if($apply_id!==""){
  445. $condition[]=["apply_id","=",$apply_id];
  446. }
  447. if($apply_name!==""){
  448. $condition[]=["apply_name","like","%$apply_name%"];
  449. }
  450. if($status!==""){
  451. $condition[]=["status","=",$status];
  452. }
  453. if($tradNo!==""){
  454. $condition[]=["tradNo","like","%$tradNo%"];
  455. }
  456. $list =Db::name("trade_pool")->where($condition)->order("addtime desc")->select();
  457. return app_show(0,"获取成功",$list);
  458. }
  459. //资金详情
  460. public function tradeInfo(){
  461. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=""? trim($this->post['tradNo']):"";
  462. if($tradNo==""){
  463. return error_show(1004,"参数 tradNo 不能为空");
  464. }
  465. $tradinfo = Db::name("trade")->where(["tradNo"=>$tradNo,"is_del"=>0])->find();
  466. if($tradinfo==false){
  467. return error_show(1004,"资金信息未找到");
  468. }
  469. return app_show(0,"获取成功",$tradinfo);
  470. }
  471. //资金认领详情
  472. public function logInfo(){
  473. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=""? trim($this->post['logNo']):"";
  474. if($logNo==""){
  475. return error_show(1004,"参数 logNo 不能为空");
  476. }
  477. $tradinfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  478. if($tradinfo==false){
  479. return error_show(1004,"资金信息未找到");
  480. }
  481. $trade = Db::name("trade")->where(["tradNo"=>$tradinfo['tradNo'],"is_del"=>0])->find();
  482. $tradinfo['trade_out']= $trade['trade_out']??"";
  483. $tradinfo['trade_in']= $trade['trade_in']??"";
  484. $tradinfo['trade_bank']= $trade['trade_bank']??"";
  485. $tradinfo['customerName']= Db::name("customer_info")->where(["companyNo"=>$tradinfo['customerNo']])->value("companyName","");
  486. $tradinfo['companyNo']= $trade['companyNo']??"";
  487. $tradinfo['total_fee']= $trade['total_fee']??"";
  488. $tradinfo['balance']= $trade['balance']??"";
  489. $tradinfo['used_fee']= $trade['used_fee']??"";
  490. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  491. ->where(["a.viceCode"=>$logNo,"a.is_del"=>0,"a.status"=>[1,2,3]])
  492. ->order("a.addtime desc")
  493. ->field("c.*,a.cancel_fee")
  494. ->findOrEmpty();
  495. $tradinfo['orderinfo']=$orderinfo;
  496. return app_show(0,"获取成功",$tradinfo);
  497. }
  498. //认领资金退回或退款 type 1 退款 2 解除资金认领
  499. public function ReturnPay(){
  500. $logNo = isset($this->post['logNo'])&&$this->post['logNo']!="" ? trim($this->post['logNo']):"";
  501. if($logNo==''){
  502. return error_show(1004,"参数 logNo 不能为空");
  503. }
  504. $type = isset($this->post['type'])&&$this->post['type']!="" ? intval($this->post['type']):"";
  505. if($type==""){
  506. return error_show(1004,"参数 type 不能为空");
  507. }
  508. $loginfo = Db::name("trade_pool")->where(["logNo"=>$logNo,"is_del"=>0])->find();
  509. if($loginfo==false){
  510. return error_show(1004,"认领资金信息未找到");
  511. }
  512. if($loginfo['status']==2 && $type==1)return error_show(1004,"认领资金信息审核已通过");
  513. if($loginfo['status']!=2 && $type==2)return error_show(1004,"认领资金信息未审核通过");
  514. $tradinfo = Db::name("trade")->where(["tradNo"=>$loginfo['tradNo'],"is_del"=>0])->find();
  515. if($tradinfo==false){
  516. return error_show(1004,"资金信息未找到");
  517. }
  518. $isT= Db::name("trade_return")->where(["logNo"=>$logNo,"tradNo"=>$loginfo['tradNo'],"status"=>[0,1]])
  519. ->findOrEmpty();
  520. if(!empty($isT)) return error_show(1004,"资金退回流程已存在");
  521. $reason = isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  522. $remark = isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  523. $returnCode =makeNo("RTA");
  524. $data=[
  525. "returnCode"=>$returnCode,
  526. "logNo"=>$logNo,
  527. "companyNo"=>$tradinfo['companyNo'],
  528. "tradNo"=>$loginfo['tradNo'],
  529. "return_img"=>'',
  530. "type"=>$type,
  531. "apply_id"=>$this->uid,
  532. "apply_name"=>$this->uname,
  533. "return_reason"=>$reason,
  534. "remark"=>$remark,
  535. "addtime"=>date("Y-m-d H:i:s"),
  536. "updatetime"=>date("Y-m-d H:i:s")
  537. ];
  538. $in = Db::name("trade_return")->insert($data);
  539. if($in){
  540. return app_show(0,"申请新建成功");
  541. }else{
  542. return error_show(1004,"申请新建失败");
  543. }
  544. }
  545. /**退款列表
  546. * //page size tradNo logNo apply_id apply_name type status returnCode
  547. * @return \think\response\Json|void
  548. * @throws \think\db\exception\DataNotFoundException
  549. * @throws \think\db\exception\DbException
  550. * @throws \think\db\exception\ModelNotFoundException
  551. */
  552. public function returnList(){
  553. $condition=[["a.is_del","=",0]];
  554. $roleid = $this->roleid;
  555. $check = checkRole($roleid,'90');
  556. if($check){
  557. $condition[]=["a.apply_id","=",$this->uid];
  558. }
  559. $page=isset($this->post['page'])&&$this->post['page']!='' ? intval($this->post['page']):1;
  560. $size=isset($this->post['size'])&&$this->post['size']!='' ? intval($this->post['size']):15;
  561. $tradNo =isset($this->post['tradNo'])&&$this->post['tradNo']!=''?trim($this->post['tradNo']):"";
  562. $logNo =isset($this->post['logNo'])&&$this->post['logNo']!=''?trim($this->post['logNo']):"";
  563. $apply_id =isset($this->post['apply_id'])&&$this->post['apply_id']!=''?intval($this->post['apply_id']):"";
  564. $apply_name =isset($this->post['apply_name'])&&$this->post['apply_name']!=''?trim($this->post['apply_name']):"";
  565. $type =isset($this->post['type'])&&$this->post['type']!=''?intval($this->post['type']):"";
  566. $status =isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  567. $returnCode =isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  568. $orderCode =isset($this->post['orderCode'])&&$this->post['orderCode']!=''?trim($this->post['orderCode']):"";
  569. $companyNo =isset($this->post['companyNo'])&&$this->post['companyNo']!=""? trim($this->post['companyNo']):"";
  570. if($companyNo!==""){
  571. $condition[]=["a.companyNo","=",$companyNo];
  572. }
  573. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  574. if($relaComNo!=""){
  575. $condition[]=["a.companyNo","=",$relaComNo];
  576. }
  577. if($tradNo!=''){
  578. $condition[]=["a.tradNo","like","%$tradNo%"];
  579. }
  580. if($orderCode!=''){
  581. $condition[]=["b.orderCode","like","%$orderCode%"];
  582. }
  583. if($logNo!=''){
  584. $condition[]=["a.logNo","like","%$logNo%"];
  585. }
  586. if($apply_id!=''){
  587. $condition[]=["a.apply_id","=",$apply_id];
  588. }
  589. if($apply_name!=''){
  590. $condition[]=["a.apply_name","like","%$apply_name%"];
  591. }
  592. if($returnCode!=''){
  593. $condition[]=["a.returnCode","like","%$returnCode%"];
  594. }
  595. if($type!=''){
  596. $condition[]=["a.type","=",$type];
  597. }
  598. if($status!==''){
  599. $condition[]=["a.status","=",$status];
  600. }
  601. $count =Db::name("trade_return")->alias("a")
  602. ->leftJoin("assoc b","a.logNo=b.viceCode and b.type=2")
  603. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode and c.is_del=0")
  604. ->where($condition)->count();
  605. $total=ceil($count/$size);
  606. $page = $page>$total ? intval($total):$page;
  607. $list=Db::name("trade_return")->alias("a")->leftJoin("assoc b","a.logNo=b.viceCode and b.type=2")
  608. ->leftJoin("qrd_info c","c.sequenceNo=b.orderCode and c.is_del=0")
  609. ->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")
  610. ->where($condition)->page($page,$size)->order("a.addtime desc")->select();
  611. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  612. }
  613. //退款状态审核 0 待审核 1 财务审核 2 财务驳回
  614. public function returnStatus(){
  615. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  616. if($returnCode==''){
  617. return error_show(1004,"参数 returnCode 不能为空");
  618. }
  619. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  620. if($returninfo==false){
  621. return error_show(1004,"退款申请数据未找到");
  622. }
  623. $status=isset($this->post['status']) &&$this->post['status']!=''?intval($this->post['status']) : '';
  624. if($status==''){
  625. return error_show(1004,"参数 status 不能为空");
  626. }
  627. $return_img = isset($this->post['return_img'])&&$this->post['return_img']!=''?trim($this->post['return_img']):"";
  628. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  629. $loginfo = Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  630. if($loginfo==false){
  631. return error_show(1004,"认领资金信息未找到");
  632. }
  633. if($loginfo['status']!=2)return error_show(1004,"认领资金信息审核未通过");
  634. Db::startTrans();
  635. try{
  636. $update=["status"=>$status,"return_img"=>$return_img,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  637. $up =Db::name("trade_return")->where($returninfo)->update($update);
  638. if($up){
  639. if($status==1){
  640. $qrdArr=Db::name("assoc")->where(["viceCode"=>$returninfo['logNo'],"status"=>[1,2],"is_del"=>0])
  641. ->select()->toArray();
  642. if(!empty($qrdArr)){
  643. $cxCodeArr=[];
  644. foreach ($qrdArr as $value){
  645. $qrd =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->field("id,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,cxCode")->findOrEmpty();
  646. if(empty($qrd)){
  647. Db::rollback();
  648. return error_show(1005,"未找到销售单数据");
  649. }
  650. if($returninfo['type']==2){
  651. if($qrd['apay_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. "apay_fee"=>$qrd['apay_fee']-$value['cancel_fee'],
  658. "pay_status"=>($qrd['apay_fee']-$value['cancel_fee'])==0 && $qrd['pay_fee']==0 ? 1:2,
  659. "status"=>$qrd['pay_fee']==0 && ($qrd['apay_fee']-$value['cancel_fee'])==0 ?0:1,
  660. "updatetime"=>date("Y-m-d H:i:s")
  661. ];
  662. }else{
  663. if($qrd['pay_fee']<$value['cancel_fee']){
  664. Db::rollback();
  665. return error_show(1005,"销售单付款中金额不足");
  666. }
  667. $update =[
  668. "wpay_fee"=>$qrd['wpay_fee']+$value['cancel_fee'],
  669. "pay_fee"=>$qrd['pay_fee']-$value['cancel_fee'],
  670. "pay_status"=>($qrd['pay_fee']-$value['cancel_fee'])==0 && $qrd['apay_fee']==0 ? 1:2,
  671. "status"=>$qrd['apay_fee']==0 && ($qrd['pay_fee']-$value['cancel_fee'])==0 ?0:1,
  672. "updatetime"=>date("Y-m-d H:i:s")
  673. ];
  674. }
  675. $qrdup =Db::name("qrd_info")->where($qrd)->update($update);
  676. if($qrdup==false){
  677. Db::rollback();
  678. return error_show(1005,"销售单更新失败");
  679. }
  680. $asscup =[
  681. "status"=>3,
  682. "updatetime"=>date("Y-m-d H:i:s")
  683. ];
  684. $assc=Db::name("assoc")->where($value)->update($asscup);
  685. if($assc==false){
  686. Db::rollback();
  687. return error_show(1005,"销售单更新失败");
  688. }
  689. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  690. if($report)$report->setField("returnTrad",$returnCode);
  691. if($qrd['cxCode']!='' && $update['pay_status']==1){
  692. $cxCodeArr[]=$qrd['cxCode'];
  693. }
  694. }
  695. if(!empty($cxCodeArr)){
  696. \app\admin\model\ComonOrder::CheckCxCode($cxCodeArr);
  697. }
  698. }
  699. $logup =["status"=>$returninfo['type']==1?5:4,"updatetime"=>date("Y-m-d H:i:s")];
  700. $upde =Db::name("trade_pool")->where($loginfo)->update($logup);
  701. if($upde==false){
  702. Db::rollback();
  703. return error_show(1005,"资金认领信息更新失败");
  704. }
  705. // if($returninfo['type']==1){
  706. $trade =Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  707. if($trade==false){
  708. Db::rollback();
  709. return error_show(1005,"资金信息未找到");
  710. }
  711. if($trade['used_fee']<$loginfo['total_fee']){
  712. Db::rollback();
  713. return error_show(1005,"资金信息已认领金额不足");
  714. }
  715. $tradup =[
  716. "used_fee"=>$trade['used_fee']-$loginfo['total_fee'],
  717. "balance"=>$trade['balance']+$loginfo['total_fee'],
  718. "status"=>($trade['used_fee']-$loginfo['total_fee'])==0 ?1 :2,
  719. "updatetime"=>date("Y-m-d H:i:s")
  720. ];
  721. $updaT=Db::name("trade")->where($trade)->update($tradup);
  722. if($updaT==false){
  723. Db::rollback();
  724. return error_show(1005,"资金信息更新失败");
  725. }
  726. // }
  727. }
  728. Db::commit();
  729. return app_show(0,"退款申请审核成功");
  730. }
  731. Db::rollback();
  732. return error_show(1004,'审核状态失败');
  733. }catch (\Exception $e){
  734. Db::rollback();
  735. return error_show(1004,$e->getMessage());
  736. }
  737. }
  738. //退款申请详情
  739. public function returnInfo(){
  740. $returnCode=isset($this->post['returnCode'])&&$this->post['returnCode']!=''?trim($this->post['returnCode']):"";
  741. if($returnCode==''){
  742. return error_show(1004,"参数 returnCode 不能为空");
  743. }
  744. $returninfo =Db::name("trade_return")->where(["returnCode"=>$returnCode,"is_del"=>0])->find();
  745. if($returninfo==false){
  746. return error_show(1004,"退款申请数据未找到");
  747. }
  748. $trade = Db::name("trade")->where(["tradNo"=>$returninfo['tradNo'],"is_del"=>0])->find();
  749. $returninfo['trade_out']= $trade['trade_out']??"";
  750. $returninfo['trade_in']= $trade['trade_in']??"";
  751. $returninfo['trade_bank']= $trade['trade_bank']??"";
  752. $returninfo['trade_time']= $trade['trade_time']??"";
  753. $returninfo['customerNo']= $trade['customerNo']??"";
  754. $returninfo['companyNo']= $trade['companyNo']??"";
  755. $returninfo['total_fee']= $trade['total_fee']??"";
  756. $returninfo['balance']= $trade['balance']??"";
  757. $returninfo['used_fee']= $trade['used_fee']??"";
  758. $pool =Db::name("trade_pool")->where(["logNo"=>$returninfo['logNo'],"is_del"=>0])->find();
  759. $returninfo['log_total_fee']= $pool['total_fee']??"";
  760. $returninfo['log_apply_id']= $pool['apply_id']??"";
  761. $returninfo['log_apply_name']= $pool['apply_name']??"";
  762. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  763. ->where(["a.viceCode"=>$returninfo['logNo'],"a.is_del"=>0,"a.status"=>[1,2]])
  764. ->order("a.addtime desc")
  765. ->field("c.*")
  766. ->findOrEmpty();
  767. $returninfo['orderinfo'] = $orderinfo;
  768. return app_show(0,"获取成功",$returninfo);
  769. }
  770. /**
  771. * 显示创建资源表单页.
  772. *
  773. * @return \think\Response
  774. */
  775. public function importTrade()
  776. {
  777. $files = $this->request->file("excel");
  778. $data = upload($files,$files->getOriginalExtension());
  779. if($data['code']!=0){
  780. return error_show(1004,$data['msg']);
  781. }
  782. $trade = $data['data'];
  783. $list = [];
  784. foreach ( $trade as $key => $value) {
  785. if (!isset($value[0]) || $value[0] == '') {
  786. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  787. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  788. }
  789. if (!isset($value[4]) || $value[4] == '') {
  790. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  791. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  792. }
  793. if (!isset($value[8]) || $value[8] == '') {
  794. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  795. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  796. }
  797. if (!isset($value[9]) || $value[9] == '') {
  798. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  799. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  800. }
  801. if ($key == 0) {
  802. if ($value[0] != '收款方公司编号') {
  803. // return ['code' => 1003, "msg" => '模板第一列为必须为交易时间!'];
  804. return error_show( 1003, '模板第一列为必须为收款方公司编号!');
  805. }
  806. if ($value[4] != '收入金额') {
  807. // return ['code' => 1003, "msg" => '模板第二列为必须为收入金额!'];
  808. return error_show( 1003, '模板第五列为必须为收入金额!');
  809. }
  810. if ($value[6] != '交易行名') {
  811. //return ['code' => 1003, "msg" => '模板第五列为必须为交易行名!'];
  812. return error_show( 1003, '模板第七列为必须为交易行名!');
  813. }
  814. if ($value[8] != '对方账号') {
  815. // return ['code' => 1003, "msg" => '模板第七列为必须为对方账号!'];
  816. return error_show( 1003, '模板第九列为必须为对方账号!');
  817. }
  818. if ($value[9] != '对方户名') {
  819. // return ['code' => 1003, "msg" => '模板第八列为必须为对方户名!'];
  820. return error_show( 1003, '模板第十列为必须为对方户名!');
  821. }
  822. continue;
  823. }
  824. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  825. if(empty($company)){
  826. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  827. }
  828. $total_fee = $value[4];
  829. $type = 0;
  830. $time = explode(" ", $value[3]);
  831. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  832. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  833. $list[$key]["trade_bank"] = $value[6];
  834. $list[$key]["trade_account"] = $value[8];
  835. $list[$key]["trade_out"] = $value[9];
  836. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  837. $list[$key]["trade_type"] = $type;
  838. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  839. $list[$key]["trade_remark"] = '';
  840. $list[$key]["companyNo"] = $value[0];
  841. $list[$key]["trade_in_account"] = $value[2]??"";
  842. }
  843. if(empty($list)){
  844. return error_show( 1003, '导入数据不能为空!');
  845. }
  846. Db::startTrans();
  847. try{
  848. $tra=[];
  849. foreach ($list as $value) {
  850. $temp = [];
  851. $temp["tradNo"] = makeStr('S');
  852. $temp['trade_time'] = $value['tradeTime'];
  853. $temp['total_fee'] = $value['trade_fee'];
  854. $temp['trade_bank'] = $value['trade_bank'];
  855. $temp['trade_account'] = $value['trade_account'];
  856. $temp['trade_type'] =$value['trade_type'];
  857. $temp['trade_out'] = $value['trade_out'];
  858. $temp['trade_in'] = $value['trade_in'];
  859. $temp['trade_in_account'] = $value['trade_in_account'];
  860. $temp['companyNo'] = $value['companyNo'];
  861. $temp['trade_used'] = $value['trade_used'];
  862. $temp['trade_remark'] = $value['trade_remark'];
  863. $temp['balance'] =$value['trade_fee'];
  864. $temp['addtime'] = date("Y-m-d H:i:s");
  865. $temp['updatetime'] = date("Y-m-d H:i:s");
  866. $tra[]=$temp;
  867. }
  868. $list = Db::name('trade')->insertAll($tra);
  869. if($list==count($tra)){
  870. Db::commit();
  871. return app_show(0, "资金导入成功");
  872. }else{
  873. Db::rollback();
  874. return app_show(1004, "资金导入失败");
  875. }
  876. }catch (\Exception $e){
  877. Db::rollback();
  878. return app_show(1004, $e->getMessage());
  879. }
  880. }
  881. /**
  882. * 资金导入
  883. * @return \think\response\Json|void
  884. */
  885. public function importTradeByArr()
  886. {
  887. $trade =isset($this->post['data']) &&!empty($this->post['data'])? $this->post['data']:[];
  888. if(!is_array($trade) || empty($trade) ) return error_show( 1003,"参数 data 不能为空");
  889. $list = [];
  890. foreach ( $trade as $key => $value) {
  891. if (!isset($value[0]) || $value[0] == '') {
  892. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  893. return error_show( 1003, '第'.($key+1).'行收款方公司编号不能为空!');
  894. }
  895. if (!isset($value[2]) || $value[2] == '') {
  896. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  897. return error_show( 1003, '第'.($key+1).'行收款账户不能为空!');
  898. }
  899. if (!isset($value[3]) || $value[3] == '') {
  900. //return ['code' => 1003, "msg" => '交易时间不能为空'];
  901. return error_show( 1003, '第'.($key+1).'行交易时间不能为空!');
  902. }
  903. if (!isset($value[4]) || $value[4] == '') {
  904. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  905. return error_show( 1003, '第'.($key+1).'行收入金额不能为空!');
  906. }
  907. if (!isset($value[6]) || $value[6] == '') {
  908. // return ['code' => 1003, "msg" => '交易行名不能为空'];
  909. return error_show( 1003, '第'.($key+1).'行交易银行名称不能为空!');
  910. }
  911. if (!isset($value[8]) || $value[8] == '') {
  912. // return ['code' => 1003, "msg" => '对方账户不能为空'];
  913. return error_show( 1003, '第'.($key+1).'行对方账户不能为空!');
  914. }
  915. if (!isset($value[9]) || $value[9] == '') {
  916. // return ['code' => 1003, "msg" => '对方户名不能为空'];
  917. return error_show( 1003, '第'.($key+1).'行对方户名不能为空!');
  918. }
  919. $company =Db::name("company_info")->where(["companyNo"=>$value[0]])->findOrEmpty();
  920. if(empty($company)){
  921. return error_show( 1003, "业务公司编号不存在{$value[0]}");
  922. }
  923. $total_fee = $value[4];
  924. $type = 0;
  925. $time = explode(" ", $value[3]);
  926. $list[$key]["tradeTime"] = date("Y-m-d", strtotime($time[0])) . " " . (isset($time[1]) ? $time[1] : "00:00:00");
  927. $list[$key]["trade_fee"] = str_replace(",","",$total_fee);
  928. $list[$key]["trade_bank"] = $value[6];
  929. $list[$key]["trade_account"] = $value[8];
  930. $list[$key]["trade_out"] = $value[9];
  931. $list[$key]["trade_in"] = $value[1]??$company['company_name'];
  932. $list[$key]["trade_type"] = $type;
  933. $list[$key]["trade_used"] = isset($value[10]) ? $value[10] : "";
  934. $list[$key]["trade_remark"] = '';
  935. $list[$key]["companyNo"] = $value[0];
  936. $list[$key]["trade_in_account"] = $value[2]??"";
  937. }
  938. if(empty($list)){
  939. return error_show( 1003, '导入数据不能为空!');
  940. }
  941. Db::startTrans();
  942. try{
  943. $tra=[];
  944. $i=0;
  945. foreach ($list as $value) {
  946. $temp = [];
  947. $i++;
  948. $temp["tradNo"] = makeStr('S').str_pad($i,3,'0',STR_PAD_LEFT);
  949. $temp['trade_time'] = $value['tradeTime'];
  950. $temp['total_fee'] = $value['trade_fee'];
  951. $temp['trade_bank'] = $value['trade_bank'];
  952. $temp['trade_account'] = $value['trade_account'];
  953. $temp['trade_type'] =$value['trade_type'];
  954. $temp['trade_out'] = $value['trade_out'];
  955. $temp['trade_in'] = $value['trade_in'];
  956. $temp['trade_in_account'] = $value['trade_in_account'];
  957. $temp['companyNo'] = $value['companyNo'];
  958. $temp['trade_used'] = $value['trade_used'];
  959. $temp['trade_remark'] = $value['trade_remark'];
  960. $temp['balance'] =$value['trade_fee'];
  961. $temp['addtime'] = date("Y-m-d H:i:s");
  962. $temp['updatetime'] = date("Y-m-d H:i:s");
  963. $tra[]=$temp;
  964. }
  965. $list = Db::name('trade')->insertAll($tra);
  966. if($list==count($tra)){
  967. Db::commit();
  968. return app_show(0, "资金导入成功");
  969. }else{
  970. Db::rollback();
  971. return app_show(1004, "资金导入失败");
  972. }
  973. }catch (\Exception $e){
  974. Db::rollback();
  975. return app_show(1004, $e->getMessage());
  976. }
  977. }
  978. //批量认领资金,跟订单相关
  979. public function importTradeByBatchOrderCode()
  980. {
  981. $list = $this->request->post('list', [], 'trim');
  982. $val = Validate::rule(['list|导入数据' => 'require|array|max:100']);
  983. if (!$val->check(['list' => $list])) return error_show(1004, $val->getError());
  984. $company = $tradNos = $orderCodes = $assoc_insert_data = $OrderCodeToAssocNo = $OrderCodeToLogNo = [];
  985. $val_item = Validate::rule([
  986. 'companyNo|卖出方公司编号' => 'require|max:255',
  987. 'tradNo|资金编号' => 'require|max:255',
  988. 'orderCode|订单编号' => 'require|max:255',
  989. 'trad_fee|认领资金' => 'require|float|gt:0|max:999999999.99',
  990. ]);
  991. foreach ($list as $key => $value) {
  992. if (!$val_item->check($value)) return error_show(1004, $val_item->getError());
  993. if (!isset($company[$value['companyNo']])) $company[$value['companyNo']] = $value['companyNo'];
  994. }
  995. //判断绑定公司
  996. $companyNo = Db::name('user_role')
  997. ->where(['is_del' => 0, 'status' => 1, 'uid' => $this->uid])
  998. ->whereIn('companyNo', $company)
  999. ->column('companyNo');
  1000. $tmp = array_diff($company, $companyNo);//二者取差集
  1001. if(!empty($tmp)) return error_show(1004,'不能操作以下公司数据:'.implode(',',$tmp));
  1002. Db::startTrans();
  1003. try {
  1004. $i=1;
  1005. //批量资金认领
  1006. foreach ($list as $key=>$value){
  1007. //资金明细判断
  1008. $i++;
  1009. $all_trade = Db::name('trade')
  1010. ->where('is_del', 0)
  1011. ->whereIn('status', [1, 2])
  1012. ->where('tradNo', $value['tradNo'])
  1013. ->field('id,balance,used_fee,companyNo,trade_time')
  1014. ->findOrEmpty();
  1015. if(empty($all_trade))throw new Exception("{$value['tradNo']}资金未找到数据");
  1016. if($all_trade['balance']<$value['trad_fee'])throw new Exception("{$value['tradNo']}资金余额不足");
  1017. if($all_trade['companyNo']!=$value['companyNo'])throw new Exception("{$value['tradNo']}资金与卖出方公司不一致");
  1018. //订单明细判断
  1019. $all_order=Db::name('qrd_info')
  1020. ->where('is_del', 0)
  1021. ->whereIn('sequenceNo',$value['orderCode'])
  1022. ->field('id,customerNo,companyNo,status,pay_fee,apay_fee,wpay_fee,pay_status,totalPrice,platform_type,cxCode')
  1023. ->findOrEmpty();
  1024. if(empty($all_order))throw new Exception("{$value['orderCode']}销售单未找到数据");
  1025. if($all_order['wpay_fee']<$value['trad_fee'])throw new Exception("{$value['orderCode']}销售单未付款金额不足核销金额");
  1026. if($all_order['companyNo']!=$value['companyNo'])throw new Exception("{$value['orderCode']}资金与卖出方公司不一致");
  1027. $OrderCodeToAssocNo = substr(makeNo('AS'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1028. $OrderCodeToLogNo = substr(makeNo('TRC'), 0, -3).str_pad($i,3,'0',STR_PAD_LEFT);
  1029. //资金数据更新处理
  1030. $balance = bcsub($all_trade['balance'], $value['trad_fee'], 2);
  1031. $tradeUp=Db::name('trade')
  1032. ->where($all_trade)
  1033. ->update([
  1034. 'balance' => $balance,
  1035. 'used_fee' => bcadd($all_trade['used_fee'], $value['trad_fee'], 2),
  1036. 'status' => $balance == 0 ? 3 : 2,
  1037. 'updatetime' =>date("Y-m-d H:i:s"),
  1038. ]);
  1039. if($tradeUp==false ) throw new Exception("{$value['tradNo']}资金更新失败");
  1040. //订单数据更新处理
  1041. $update=[
  1042. 'apay_fee' => bcadd($all_order['apay_fee'], $value['trad_fee'], 2),
  1043. 'wpay_fee' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2),
  1044. 'pay_status' => bcsub($all_order['wpay_fee'], $value['trad_fee'], 2)==0 &&$all_order['pay_fee']==0? 3:2,
  1045. 'status' => 1,
  1046. 'updatetime' => date('Y-m-d H:i:s')
  1047. ];
  1048. $orderUp=Db::name('qrd_info')
  1049. ->where($all_order)
  1050. ->update($update);
  1051. if($orderUp==false ) throw new Exception("{$value['orderCode']}销售单更新失败");
  1052. if($update['pay_status']==3){
  1053. (new \app\admin\model\ComonOrder())->where(['cxCode'=>$all_order['cxCode'],'status'=>-1])->update(['status'=>0]);
  1054. }
  1055. //资金订单关联数据
  1056. $assoc_insert_data[]=[
  1057. 'assocNo' => $OrderCodeToAssocNo,
  1058. 'apply_id' => $this->uid,
  1059. 'apply_name' => $this->uname,
  1060. 'type' => 2,
  1061. 'orderCode' => $value['orderCode'],
  1062. 'customerNo' => $all_order['customerNo'],
  1063. 'viceCode' => $OrderCodeToLogNo,
  1064. 'order_total' => $all_order['totalPrice'],
  1065. 'vice_total' => $value['trad_fee'],
  1066. 'cancel_fee' => $value['trad_fee'],
  1067. 'status' => 2,//认领通过
  1068. 'addtime' => date("Y-m-d H:i:s"),
  1069. 'updatetime' => date("Y-m-d H:i:s"),
  1070. ];
  1071. //资金认领数据
  1072. $trade_pool_insert_data[]=[
  1073. 'logNo' => $OrderCodeToLogNo,
  1074. 'tradNo' => $value['tradNo'],
  1075. 'platform_type' => $all_order['platform_type'],
  1076. 'companyNo' => $value['companyNo'],
  1077. 'customerNo' => $all_order['customerNo'],
  1078. 'apply_id' => $this->uid,
  1079. 'apply_name' => $this->uname,
  1080. 'trade_time' => $all_trade['trade_time'],
  1081. 'total_fee' => $value['trad_fee'],
  1082. 'status' => 2,//2审核通过
  1083. 'addtime' => date("Y-m-d H:i:s"),
  1084. 'updatetime' => date("Y-m-d H:i:s"),
  1085. ];
  1086. $report=ReportCode::where(["qrdNo"=>$value['orderCode']])->find();
  1087. if($report){
  1088. $report->setField("logNo",$OrderCodeToLogNo);
  1089. $report->setField("tradNo",$value['tradNo']);
  1090. }
  1091. }
  1092. if ($assoc_insert_data) Db::name('assoc')->insertAll($assoc_insert_data);
  1093. if ($trade_pool_insert_data) Db::name('trade_pool')->insertAll($trade_pool_insert_data);
  1094. Db::commit();
  1095. return app_show(0, '批量认领资金成功');
  1096. } catch (Exception $exception) {
  1097. Db::rollback();
  1098. return error_show(1004, '批量认领资金失败,' . $exception->getMessage());
  1099. }
  1100. }
  1101. public function exportTrade(){
  1102. $param = $this->request->param(["name"=>"","bank"=>'',"start"=>'',"end"=>'',"status"=>'',"tradNo"=>'',
  1103. "companyNo"=>'',"total_lower"=>'','total_upper'=>''],"post","trim");
  1104. $condition=[["is_del","=",0]];
  1105. $param['name']==""?: $condition[]=["trade_out","like","%{$param['name']}%"];
  1106. $param['bank']==""?: $condition[]=['trade_bank','like',"%{$param['bank']}%"];;
  1107. $param['start']==""?: $condition[]=['trade_time','>=',startTime($param['start'])];
  1108. $param['end']==""?: $condition[]=['trade_time','<=',endTime($param['end'])];
  1109. $param['status']===""?: $condition[]=["status","=",$param['status']];
  1110. $param['tradNo']==""?: $condition[]=["tradNo","like","%{$param['tradNo']}%"];
  1111. $param['companyNo']==""?: $condition[]=['companyNo','=',$param['companyNo']];
  1112. $param['total_lower']==""?: $condition[]=['total_fee','>=',$param['total_lower']];
  1113. $param['total_upper']==""?: $condition[]=['total_fee','<=',$param['total_upper']];
  1114. $list= \app\admin\model\Trade::where($condition)
  1115. ->field(["tradNo","companyNo","trade_in",'trade_in_account','trade_time','total_fee','trade_bank',
  1116. 'trade_account','trade_out','trade_used'])
  1117. ->select();
  1118. $header = ["资金编号","收款方公司编码",'收款方公司名称','收款方账户','交易时间','收入金额','交易行名','对方账号','对方户名','交易用途'];
  1119. if($list->isEmpty()){
  1120. $header=["暂无数据"];
  1121. }
  1122. excelExport("资金管理导出",$header,$list->toArray());
  1123. }
  1124. }