OrderPay.php 59 KB

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