Ei kuvausta

DayGrandTotal.php 4.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 DayGrandTotal extends Command {
  9. protected $signature = 'DayGrandTotal';
  10. /**
  11. * The console command description.
  12. *
  13. * @var string
  14. */
  15. protected $description = '每日汇总投放,成本,毛利等数据';
  16. public function handle()
  17. {
  18. $this->dayGrandTotal();
  19. }
  20. public function dayGrandTotal(){
  21. $_start = '2019-09-04';
  22. $_end = date('Y-m-d');
  23. $data = array();
  24. $idate = date('Y-m-d', strtotime('-1 day'));
  25. //查看是否已有数据
  26. $if_e = DB::table('day_grand_total')->where('idate', $idate)->first();
  27. if(!empty($if_e)){
  28. echo "\n日期:".$idate." 已存在";
  29. return false;
  30. }
  31. $data['idate'] = $idate;
  32. //总投入
  33. $data['throw_cost'] = CustTotal::where('is_del',0)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('total_cost');
  34. //订单信息
  35. $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('createTime','<',$_end)->where('createTime','>=',$_start)->first();
  36. $data['goods_cost'] = $order->goods_cost;
  37. $data['freight_cost'] = $order->freight_cost;
  38. $data['aftersale_cost'] = $order->aftersale_cost;
  39. $data['refund_fee'] = $order->refund_fee;
  40. $data['order_count'] = $order->order_count;
  41. $data['order_amount'] = $order->order_amount;
  42. $data['cust_count'] = $order->cust_count;
  43. //加粉
  44. $data['fan_count'] = CustDetail::where('is_del',0)->where('dtime','<',$_end)->where('dtime','>=',$_start)->sum('fan_add');
  45. //毛利 = 总销售额-总商品成本-总运费-总售后
  46. $data['profit'] = $order->order_amount - $order->goods_cost - $order->freight_cost - $order->aftersale_cost - $data['throw_cost'] + $order->refund_fee;
  47. //总新粉单数
  48. $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.createTime','<',$_end)->where('order.createTime','>=',$_start)->first();
  49. $data['new_order_count'] = $new_order->order_count;
  50. //总老粉单数
  51. $data['old_order_count'] = $data['order_count'] - $data['new_order_count'];
  52. //总复购订单
  53. $data['fugou_order_count'] = $data['order_count'] - $data['cust_count'];
  54. //7 - 60 roi汇总
  55. $date = date('Y-m-d', strtotime('-7 day'));
  56. $data['cost7'] = DB::table('roi_total')->where('type', 1)->where('ad_time', '<', $date)->sum('cost');
  57. $data['order_count7'] = DB::table('roi_total')->where('type', 1)->where('ad_time', '<', $date)->sum('order_count');
  58. $data['order_amount7'] = DB::table('roi_total')->where('type', 1)->where('ad_time', '<', $date)->sum('order_amount');
  59. $date = date('Y-m-d', strtotime('-15 day'));
  60. $data['cost15'] = DB::table('roi_total')->where('type', 2)->where('ad_time', '<', $date)->sum('cost');
  61. $data['order_count15'] = DB::table('roi_total')->where('type', 2)->where('ad_time', '<', $date)->sum('order_count');
  62. $data['order_amount15'] = DB::table('roi_total')->where('type', 2)->where('ad_time', '<', $date)->sum('order_amount');
  63. $date = date('Y-m-d', strtotime('-30 day'));
  64. $data['cost30'] = DB::table('roi_total')->where('type', 3)->where('ad_time', '<', $date)->sum('cost');
  65. $data['order_count30'] = DB::table('roi_total')->where('type', 3)->where('ad_time', '<', $date)->sum('order_count');
  66. $data['order_amount30'] = DB::table('roi_total')->where('type', 3)->where('ad_time', '<', $date)->sum('order_amount');
  67. $date = date('Y-m-d', strtotime('-45 day'));
  68. $data['cost45'] = DB::table('roi_total')->where('type', 4)->where('ad_time', '<', $date)->sum('cost');
  69. $data['order_count45'] = DB::table('roi_total')->where('type', 4)->where('ad_time', '<', $date)->sum('order_count');
  70. $data['order_amount45'] = DB::table('roi_total')->where('type', 4)->where('ad_time', '<', $date)->sum('order_amount');
  71. $date = date('Y-m-d', strtotime('-60 day'));
  72. $data['cost60'] = DB::table('roi_total')->where('type', 5)->where('ad_time', '<', $date)->sum('cost');
  73. $data['order_count60'] = DB::table('roi_total')->where('type', 5)->where('ad_time', '<', $date)->sum('order_count');
  74. $data['order_amount60'] = DB::table('roi_total')->where('type', 5)->where('ad_time', '<', $date)->sum('order_amount');
  75. $res = DB::table('day_grand_total')->insert($data);
  76. echo "\n日期:".$idate." 插入结果:".$res;
  77. }
  78. }