Aucune description

DayGrandSalerTotalHistory.php 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use App\CustTotal;
  6. use App\CustDetail;
  7. use App\Order;
  8. class DayGrandSalerTotalHistory extends Command {
  9. protected $signature = 'DayGrandSalerTotalHistory';
  10. /**
  11. * The console command description.
  12. *
  13. * @var string
  14. */
  15. protected $description = '分销售每日汇总投放,成本,毛利等数据 历史处理';
  16. public function handle()
  17. {
  18. $this->dayGrandSalerTotal();
  19. }
  20. public function dayGrandSalerTotal(){
  21. $_start = '2019-09-04';
  22. for($i=79;$i>=1;$i--){
  23. $m = '-'.($i-1).' day';
  24. $n = '-'.$i.' day';
  25. $_end = date('Y-m-d', strtotime($m));
  26. $idate = date('Y-m-d', strtotime($n));
  27. //查看是否已有数据
  28. $if_e = DB::table('day_grand_saler_total')->where('idate', $idate)->first();
  29. if(!empty($if_e)){
  30. echo "\n日期:".$idate." 已存在";
  31. return false;
  32. }
  33. $team_ids = $this->getTeams();
  34. //获取各个团队销售id
  35. $salerIds = $this->getTeamSalers($team_ids);
  36. foreach($salerIds as $team_id=>$saler_ids){
  37. //团队总投入
  38. $team_throw_cost = CustTotal::where('is_del',0)->where('team_id',$team_id)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('total_cost');
  39. //团队销售历史总加粉
  40. $wx_fan_total = CustDetail::whereIn('admin_id', $saler_ids)->where('is_del', 0)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');
  41. foreach($saler_ids as $admin_id){
  42. $data = array();
  43. $data['idate'] = $idate;
  44. $data['admin_id'] = $admin_id;
  45. $data['team_id'] = $team_id;
  46. //总加粉
  47. $data['fan_count'] = CustDetail::where('is_del',0)->where('admin_id', $admin_id)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');
  48. //预估总投入
  49. $data['throw_cost'] = $wx_fan_total>0 ? round($data['fan_count']/$wx_fan_total * $team_throw_cost, 2) : 0;
  50. //订单信息
  51. $order = Order::select(DB::raw('sum(cost) as goods_cost, sum(freight_cost) as freight_cost, sum(aftersale_fee) as aftersale_cost, count(1) as order_count, sum(receivedAmount) as order_amount, count(distinct(receiverMobile)) as cust_count, sum(refund_price) as refund_fee'))->where('is_del',0)->where('admin_id', $admin_id)->where('createTime','<',$_end)->where('createTime','>=',$_start)->first();
  52. $data['goods_cost'] = $order->goods_cost;
  53. $data['freight_cost'] = $order->freight_cost;
  54. $data['aftersale_cost'] = $order->aftersale_cost;
  55. $data['refund_fee'] = $order->refund_fee;
  56. $data['order_count'] = $order->order_count;
  57. $data['order_amount'] = $order->order_amount;
  58. $data['cust_count'] = $order->cust_count;
  59. //毛利 = 总销售额-总商品成本-总运费-总售后
  60. $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
  61. //总新粉单数
  62. $new_order = Order::select(DB::raw('count(1) as order_count'))->leftJoin('customers as cu','cu.phone', '=', 'order.receiverMobile')->whereRaw('left(order.createTime, 10) = cu.fanTime')->where('order.is_del', 0)->where('order.admin_id', $admin_id)->where('order.createTime','<',$_end)->where('order.createTime','>=',$_start)->first();
  63. $data['new_order_count'] = $new_order->order_count;
  64. //总老粉单数
  65. $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
  66. //总复购订单
  67. $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
  68. //7 - 60 roi汇总
  69. $s7 = $idate. ' -7 day';
  70. $date = date('Y-m-d', strtotime($s7));
  71. $data['cost7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('cost');
  72. $data['order_count7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_count');
  73. $data['order_amount7'] = DB::table('roi_saler_total')->where('type', 1)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_amount');
  74. $s15 = $idate. ' -15 day';
  75. $date = date('Y-m-d', strtotime($s15));
  76. $data['cost15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('cost');
  77. $data['order_count15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_count');
  78. $data['order_amount15'] = DB::table('roi_saler_total')->where('type', 2)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_amount');
  79. $s30 = $idate. ' -30 day';
  80. $date = date('Y-m-d', strtotime($s30));
  81. $data['cost30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('cost');
  82. $data['order_count30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_count');
  83. $data['order_amount30'] = DB::table('roi_saler_total')->where('type', 3)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_amount');
  84. $s45 = $idate. ' -45 day';
  85. $date = date('Y-m-d', strtotime($s45));
  86. $data['cost45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('cost');
  87. $data['order_count45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_count');
  88. $data['order_amount45'] = DB::table('roi_saler_total')->where('type', 4)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_amount');
  89. $s60 = $idate. ' -60 day';
  90. $date = date('Y-m-d', strtotime($s60));
  91. $data['cost60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('cost');
  92. $data['order_count60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_count');
  93. $data['order_amount60'] = DB::table('roi_saler_total')->where('type', 5)->where('admin_id', $admin_id)->where('ad_time', '<=', $date)->sum('order_amount');
  94. $res = DB::table('day_grand_saler_total')->insert($data);
  95. echo "\n日期:".$idate." 团队:".$team_id. " 销售:" .$admin_id." 插入结果:".$res;
  96. }
  97. }
  98. }
  99. }
  100. public function getTeamSalers($team_ids){
  101. $data = array();
  102. foreach($team_ids as $team_id){
  103. $data[$team_id] = DB::table('admin')->where('team_id', $team_id)->lists('id');
  104. }
  105. return $data;
  106. }
  107. public function getTeams(){
  108. $data = DB::table('teams')->where('type', 1)->lists('id');
  109. return $data;
  110. }
  111. }