RewardController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Orderdetail;
  4. use App\Models\Ordertest;
  5. use App\Models\Rewards;
  6. use App\Models\System;
  7. use App\Models\Store;
  8. use App\Models\Goods;
  9. use App\Models\GoodsSku;
  10. use App\Models\GoodsSpec;
  11. use App\Models\StoreUser;
  12. use App\Models\SubsidiesInfo;
  13. use App\Models\ExpStoreAgent;
  14. use Illuminate\Http\Request;
  15. use Illuminate\Support\Facades\DB;
  16. use Illuminate\Support\Facades\Log;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Database\Eloquent\Builder;
  19. use \Carbon\Carbon;
  20. use \Exception;
  21. class RewardController extends Controller
  22. {
  23. public function getRewardList(Request $request){
  24. $input=$request->all();
  25. $page_size=$input['page_size'];
  26. $page_index=$input['page_index'];
  27. $num = ($page_index - 1) * $page_size;
  28. $search_name=$input['search_name'];
  29. $where=[];
  30. $where['type']=$input['type'];
  31. $count=Rewards::with('recom:id,realname','agent:id,realname')
  32. ->where($where)
  33. ->count();
  34. if($count==0){
  35. $this->error('400001','没有数据');
  36. }
  37. $list=Rewards::with('recom:id,realname','agent:id,realname')
  38. ->where($where)
  39. ->orderBy('id','desc')
  40. ->skip($num)->take($page_size)
  41. ->get();
  42. if(empty($list)){
  43. return $this->error('400002','没有获取到数据');
  44. }
  45. return $this->success_list($list,'success',$count);
  46. }
  47. //代理业绩奖励结算
  48. public function uploadAgentSalesResult(Request $request){
  49. $input=$request->all();
  50. $userId=$input['userids'];
  51. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  52. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  53. $time = [$starttime, $endtime];
  54. $sales_area_reward_str=System::where('keys','sales_area_reward')->value('values');
  55. $sales_area_reward_arr=explode('/',$sales_area_reward_str);
  56. foreach($sales_area_reward_arr as $key =>$val){
  57. $sales_area_reward[$key]=explode('*',$val);
  58. }
  59. $result = array();
  60. foreach($sales_area_reward as $k=>$v){
  61. $result[$k] = $v[0];
  62. }
  63. $minValue=min($result);
  64. DB::beginTransaction();
  65. try {
  66. foreach($userId as $key=>$val){
  67. $rewards=Rewards::where('recom_id',$val)->where('type',2)->where('month',$input['time'])->first();
  68. if($rewards){
  69. DB::rollBack();
  70. return $this->error('400111','用户id=>'.$val.'当月已结算');
  71. }
  72. $list=DB::table('users')
  73. ->select('users.id','users.nickname','users.level','warea.name as warea','uu.nickname as agent',DB::raw('SUM(order_goods.totalprice) as account'))
  74. ->leftJoin('order','order.user_id','=','users.id')
  75. ->leftJoin('order_goods','order.id','=','order_goods.order_id')
  76. ->leftJoin('warea','warea.id','=','users.warea_id')
  77. ->leftJoin('users as uu','uu.id','=','users.agent_id')
  78. ->where('order.status',2)
  79. ->where('users.id',$val)
  80. ->whereBetween('order.created_at',$time)
  81. ->get()->toArray();
  82. if($list){
  83. $list[0]->rewards=0;
  84. foreach($sales_area_reward as $k=>$v){
  85. if($list[0]->account>=$v[0] && $list[0]->account<$v[1]){
  86. $list[0]->rewards=round($list[0]->account*$v[2]/100,2);
  87. }
  88. }
  89. if($list[0]->rewards>0){
  90. $data['agent_id']=1;
  91. $data['recom_id']=$list[0]->id;
  92. $data['type']=2;
  93. $data['bonus']=$list[0]->rewards;
  94. $data['spec']='1';
  95. $data['totalprice']=$list[0]->account;
  96. $data['month']=$input['time'];
  97. Rewards::create($data);
  98. }else{
  99. DB::rollBack();
  100. return $this->error('400111','用户'.$list[0]->nickname.'结算失败');
  101. }
  102. }else{
  103. DB::rollBack();
  104. return $this->error('400111','用户结算失败');
  105. }
  106. }
  107. DB::commit();
  108. Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')结算代理业绩奖励,代理id:'.json_encode($input['userids']));
  109. return $this->success([]);
  110. }catch(Exception $e){
  111. DB::rollBack();
  112. return $this->error();
  113. }
  114. }
  115. //门店业绩奖励结算钱
  116. public function uploadAgentSalesResultMoney(Request $request){
  117. $input=$request->all();
  118. $storeId=$input['storeids'];
  119. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  120. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  121. $time = [$starttime, $endtime];
  122. $type=$input['type'];
  123. if($type==2){
  124. $rate=System::where('keys','store_reward_money_enjoy')->value('values');
  125. }elseif($type==3){
  126. $rate=System::where('keys','store_reward_money_experience')->value('values');
  127. }
  128. $rates=explode('/',$rate);
  129. DB::beginTransaction();
  130. try {
  131. foreach($storeId as $key=>$val){
  132. $list=DB::table('store')->where('id',$val)->get();
  133. $list[0]->account=0;
  134. $rewards=Rewards::where('recom_id',$list[0]->man_id)->where('type',3)->where('month',$input['time'])->first();
  135. if($rewards){
  136. DB::rollBack();
  137. return $this->error('400111','门店'.$list[0]->name.'当月已结算');
  138. }
  139. $info=DB::table('store')
  140. ->select('goods.type','goods.name', DB::raw('SUM(order_goods.num) as total'))
  141. ->leftJoin('users', function ($join) {
  142. $join->on('users.store_id', '=', 'store.id');
  143. })
  144. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  145. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  146. ->Join('goods', 'goods.id', '=', 'order_goods.goods_id')
  147. ->where('store.id',$val)
  148. ->where('order.status', 2)
  149. ->whereBetween('order.created_at',$time)
  150. ->groupBy('goods.type')
  151. ->get();
  152. if(!$info){
  153. DB::rollBack();
  154. return $this->error('400111','门店奖励结算失败');
  155. }
  156. foreach($info as $k =>$v){
  157. if($v->type==1){
  158. $info[$k]->price=$rates[0];
  159. $info[$k]->totalprice=$v->total*$rates[0];
  160. $list[0]->account+=$v->total*$rates[0];
  161. }
  162. if($v->type==2){
  163. $info[$k]->price=$rates[1];
  164. $info[$k]->totalprice=$v->total*$rates[1];
  165. $list[0]->account+=$v->total*$rates[1];
  166. }
  167. if($v->type==3){
  168. $info[$k]->price=$rates[2];
  169. $info[$k]->totalprice=$v->total*$rates[2];
  170. $list[0]->account+=$v->total*$rates[2];
  171. }
  172. }
  173. $list[0]->info=$info;
  174. if(!$list[0]->account){
  175. DB::rollBack();
  176. return $this->error('400111','门店'.$list[0]->name.'奖励结算失败');
  177. }
  178. $data['agent_id']=1;
  179. $data['recom_id']=$list[0]->man_id;
  180. $data['store_id']=$list[0]->id;
  181. $data['type']=3;
  182. $data['bonus']=$list[0]->account;
  183. $data['spec']='1';
  184. $data['totalprice']='';
  185. $data['month']=$input['time'];
  186. Rewards::create($data);
  187. }
  188. DB::commit();
  189. Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')结算门店业绩奖励,代理id:'.json_encode($input['storeids']));
  190. return $this->success([]);
  191. }catch(Exception $e){
  192. DB::rollBack();
  193. return $this->error();
  194. }
  195. }
  196. //门店业绩奖励 物
  197. public function uploadAgentSalesResultGoods(Request $request){
  198. $input=$request->all();
  199. $storeId=$input['storeids'];
  200. $starttime=date("Y-m-01 00:00:00",strtotime($input['time']));
  201. $endtime= date("Y-m-01 00:00:00",strtotime($input['time'] ."+1 months"));
  202. $time = [$starttime, $endtime];
  203. // $goodsInfo=$input['goodsinfo'];
  204. $type=$input['type'];
  205. if($type==1){
  206. $rate=System::where('keys','store_reward_goods_job')->value('values');
  207. }elseif($type==2){
  208. $rate=System::where('keys','store_reward_goods_enjoy')->value('values');
  209. }
  210. DB::beginTransaction();
  211. try {
  212. foreach($storeId as $key=>$val){
  213. $list=DB::table('store')
  214. ->select('store.name','store.man_id','store.id', DB::raw('SUM(order_goods.totalprice) as account'))
  215. ->leftJoin('users', function ($join) {
  216. $join->on('users.store_id', '=', 'store.id');
  217. })
  218. ->leftJoin('order', 'order.user_id', '=', 'users.id')
  219. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  220. ->where('store.id',$val)
  221. ->where('order.status', 2)
  222. ->whereBetween('order.created_at',$time)
  223. ->groupBy('store.id')
  224. ->get();
  225. if(!$list){
  226. DB::rollBack();
  227. return $this->error('400111','门店奖励结算失败');
  228. }
  229. $rewards=Rewards::where('recom_id',$list[0]->man_id)->where('type',3)->where('month',$input['time'])->first();
  230. if($rewards){
  231. DB::rollBack();
  232. return $this->error('400111','门店'.$list[0]->name.'当月已结算');
  233. }
  234. foreach($list as $key =>$val){
  235. $list[$key]->rewards=round($val->account*$rate/100,2);
  236. }
  237. if(!$list[0]->rewards){
  238. DB::rollBack();
  239. return $this->error('400111','门店'.$list[0]->name.'奖励结算失败');
  240. }
  241. $data['agent_id']=1;
  242. $data['recom_id']=$list[0]->man_id;
  243. $data['store_id']=$list[0]->id;
  244. $data['type']=3;
  245. $data['bonus']='';
  246. $data['spec']='2';
  247. $data['totalprice']=$list[0]->account;
  248. $data['goods_id']=$input['goods_id'];
  249. $data['goods_price']=$input['goods_price'];
  250. $data['goods_num']=$input['goods_num'];
  251. $data['month']=$input['time'];
  252. $row=Rewards::create($data);
  253. // foreach($goodsInfo as $k=>$v){
  254. // $dd['reward_id']=$row;
  255. // $dd['goods_id']=$v['id'];
  256. // $dd['sku_id']=$v['sku_id'];
  257. // $dd['price']=$v['price'];
  258. // $dd['num']=$v['num'];
  259. // $dd['totalprice']=$v['price']*$v['num'];
  260. // OrderGoods::create($dd);
  261. // }
  262. }
  263. DB::commit();
  264. Log::info('管理员:'.Auth::user()->name.'(id='.Auth::user()->id.')结算门店业绩奖励,代理id:'.json_encode($input['storeids']));
  265. return $this->success([]);
  266. }catch(Exception $e){
  267. DB::rollBack();
  268. return $this->error();
  269. }
  270. }
  271. public function getRewardGoodsList(){
  272. $list = Goods::where('status',1)->select('id','name','type','tjprice','total')->get();
  273. if(!$list){
  274. return $this->error('400133','没有可用商品信息');
  275. }
  276. foreach($list as $key=>$val){
  277. $sku=GoodsSku::where('goods_id',$val->id)->where('is_show',1)->select('id','sku_path')->get();
  278. foreach($sku as $k=>$v){
  279. $sku[$k]->info=$this->getSku($v->id);
  280. }
  281. $list[$key]->sku=$sku;
  282. }
  283. return $this->success($list);
  284. }
  285. //获取商品规格数据
  286. public function getSku($id){
  287. $path=GoodsSku::withTrashed()->find($id)->sku_path;
  288. $arr=explode('/',$path);
  289. $goodsSku=[];
  290. foreach($arr as $k=>$v){
  291. $upSpec=GoodsSpec::withTrashed()->where('id',$v)->first();
  292. $upSpec['uptitle']=GoodsSpec::where('id',$upSpec->pid)->value('title');
  293. $goodsSku[$upSpec['uptitle']]=$upSpec->title;
  294. }
  295. return $goodsSku;
  296. }
  297. //获取代理与体验店
  298. public function getAgentExperience(){
  299. $gg=Ordertest::query()->whereNotNUll('store_id')->groupBy('user_id')->get();
  300. foreach($gg as $key=>$val){
  301. ExpStoreAgent::create([
  302. 'user_id'=>$val->user_id,
  303. 'store_id'=>$val->store_id,
  304. ]);
  305. }
  306. }
  307. //计算店铺区间销量
  308. public function getStoreSaleNum(Request $request){
  309. $input=$request->all();
  310. $store_id=$input['store_id'];
  311. $month=$input['month'];
  312. if($month){
  313. $start_time=Carbon::parse($month)->startOfMonth();
  314. $end_time= Carbon::parse($month)->endOfMonth();
  315. }else{
  316. $start_time=$input['start_time'];
  317. $end_time=$input['end_time'];
  318. $month= Carbon::parse($start_time)->format('Y-m');
  319. $month2= Carbon::parse($end_time)->format('Y-m');
  320. if($month != $month2){
  321. return $this->error('450001','所选时间段必须在同一个月内');
  322. }
  323. }
  324. $sub=SubsidiesInfo::where('month',$month)->where('store_id',$store_id)->first();
  325. if($sub){
  326. return $this->error('450001','当月已完成结算');
  327. }
  328. $store=Store::where('id',$store_id)->select('id','cost','amount','name','type')->first();
  329. if(empty($store)){
  330. return $this->error('450001','店铺不存在');
  331. }
  332. if(empty($store->cost)){
  333. return $this->error('450001','未设置店铺装修花费');
  334. }
  335. $type=$input['type'];
  336. if($type==3){//优享店
  337. $rate=System::where('keys','store_reward_money_enjoy')->value('values');
  338. $user_id=StoreUser::where('store_id',$store_id)->value('user_id');
  339. $order_ids=Ordertest::query()->where('user_id',$user_id)->where('is_deleted','=',0)
  340. ->whereBetween('created_at',[$start_time,$end_time])
  341. ->pluck('id');
  342. }elseif($type==2){//体验店
  343. $rate=System::where('keys','store_reward_money_experience')->value('values');
  344. $order_ids=Ordertest::query()->where('store_id',$store_id)->where('is_deleted','=',0)
  345. ->whereBetween('created_at',[$start_time,$end_time])
  346. ->pluck('id');
  347. }
  348. $orders=Orderdetail::whereIn('order_id',$order_ids)->groupBy('goods_name')
  349. ->select('goods_name',DB::raw('sum(num) as count'),DB::raw('sum(money) as amount'))->get();
  350. $rate=json_decode($rate,true);
  351. foreach($orders as $key=>$val){
  352. $n=0;
  353. foreach($rate as $k=>$v){
  354. if($val->goods_name == $v['name'] || substr_count($val->goods_name,$v['name'])>=1){
  355. $orders[$key]->rate = $v['num'];
  356. $orders[$key]->unit = $v['unit'];
  357. $n++;
  358. }
  359. }
  360. if($n==0){
  361. return $this->error('450001',$val->goods_name.'没有设置补助标准');
  362. }
  363. }
  364. $user=StoreUser::with('user:id,nickname')
  365. ->select('id','store_id','user_id','user_mobile','equity_rate')
  366. ->where('store_id',$store_id)->get();
  367. $data['user']=$user;
  368. $data['rate']=$rate;
  369. $data['order']=$orders;
  370. $data['store']=$store;
  371. return $this->success($data);
  372. }
  373. //结算店铺补助
  374. public function settleStoreSubsidies(Request $request){
  375. $month=$request->input('month');
  376. if($month){
  377. $start_time= Carbon::parse($month)->startOfMonth();
  378. $end_time= Carbon::parse($month)->endOfMonth();
  379. $complete_month=1;
  380. }else{
  381. $start_time=$request->input('start_time');
  382. $end_time=$request->input('end_time');
  383. $month= Carbon::parse($start_time)->format('Y-m');
  384. $month2= Carbon::parse($end_time)->format('Y-m');
  385. if($month != $month2){
  386. return $this->error('450001','所选时间段必须在同一个月内');
  387. }
  388. if(Carbon::parse($month)->startOfMonth()==$start_time && Carbon::parse($month)->endOfMonth()==$end_time){
  389. $complete_month=1;
  390. }else{
  391. $complete_month=0;
  392. }
  393. }
  394. $store_id=$request->input('store_id');
  395. $sub=SubsidiesInfo::where('month',$month)->where('store_id',$store_id)->first();
  396. if($sub){
  397. return $this->error('450001','当月已完成结算');
  398. }
  399. $is_break=$request->input('is_break');
  400. $user_info=$request->input('user_info');
  401. $sale_info=$request->input('sale_info');
  402. $rate_info=$request->input('rate_info');
  403. $sale_amount=$request->input('sale_amount');
  404. $subsidies_amount=$request->input('subsidies_amount');
  405. $send_type=$request->input('send_type');
  406. $data=[
  407. 'month'=>$month,
  408. 'start_time'=>$start_time,
  409. 'end_time'=>$end_time,
  410. 'complete_month'=>$complete_month,
  411. 'store_id'=>$store_id,
  412. 'is_break'=>$is_break,
  413. 'user_info'=>json_encode($user_info,JSON_UNESCAPED_UNICODE),
  414. 'sale_info'=>json_encode($sale_info,JSON_UNESCAPED_UNICODE),
  415. 'rate_info'=>$rate_info,
  416. 'sale_amount'=>$sale_amount,
  417. 'subsidies_amount'=>$subsidies_amount,
  418. 'send_time'=>Carbon::now(),
  419. 'send_type'=>$send_type
  420. ];
  421. try{
  422. DB::transaction(function()use($data,$store_id){
  423. SubsidiesInfo::query()->create($data);
  424. $store=Store::query()->where('id',$store_id)->lockForUpdate()->first();
  425. $store->amount+=$data['subsidies_amount'];
  426. $store->save();
  427. },5);
  428. return $this->success([]);
  429. }catch(Exception $e){
  430. return $this->error('450001',$e->getMessage());
  431. }
  432. }
  433. public function getSubsidiesInfo(Request $request){
  434. $store_id=$request->input('store_id');
  435. $page_index=$request->input('page_index');
  436. $page_size=$request->input('page_size');
  437. $num=$page_size*($page_index-1);
  438. $count=SubsidiesInfo::where('store_id',$store_id)->count();
  439. $list=SubsidiesInfo::with('store:id,name,type,cost,amount')
  440. ->where('store_id',$store_id)
  441. ->orderBy('month','desc')->skip($num)->take($page_size)->get();
  442. return $this->success_list($list,'',$count);
  443. }
  444. //取消结算
  445. public function cancelSubsidies(Request $request){
  446. $id=$request->input('id');
  447. $sub=SubsidiesInfo::query()->where('id',$id)->first();
  448. $store=Store::query()->where('id',$sub->store_id)->lockForUpdate()->first();
  449. $store->amount-=$sub->subsidies_amount;
  450. try{
  451. DB::transaction(function()use($sub,$store){
  452. $sub->delete();
  453. $store->save();
  454. });
  455. return $this->success([]);
  456. }catch(Exception $e){
  457. return $this->error('450001',$e->getMessage());
  458. }
  459. }
  460. }