예제 #1
0
 public function todayTopSales(Carbon $date, $limit = 10)
 {
     $arr = [];
     $ds_null = false;
     $current_day_zero_sales = false;
     $ds = DailySales::where('date', $date->format('Y-m-d'))->orderBy('sales', 'DESC')->take($limit)->get();
     if (count($ds) == '0') {
         $ds = DailySales::where('date', $date->copy()->subDay()->format('Y-m-d'))->orderBy('sales', 'DESC')->take($limit)->get();
         $ds_null = true;
     } else {
         foreach ($ds as $d) {
             if ($d->sales == '0.00') {
                 $ds = DailySales::where('date', $date->copy()->subDay()->format('Y-m-d'))->orderBy('sales', 'DESC')->take($limit)->get();
                 $ds_null = true;
                 continue;
             }
         }
     }
     foreach ($ds as $d) {
         $branch = Branch::where('id', $d->branchid)->get(['code', 'descriptor', 'id'])->first();
         if ($ds_null) {
             $ds_today = new DailySales();
             $ds_yesteday = $d;
         } else {
             $ds_today = $d;
             $ds_yesteday = DailySales::where('date', $date->copy()->subDay()->format('Y-m-d'))->where('branchid', $d->branchid)->first();
         }
         $ds_otherday = DailySales::where('date', $date->copy()->subDay(2)->format('Y-m-d'))->where('branchid', $d->branchid)->first();
         $s = new StdClass();
         $c = new StdClass();
         $s->branch = $branch;
         $s->today = $ds_today;
         $s->yesterday = $ds_yesteday;
         $s->otherday = $ds_otherday;
         $c->sales = $ds_today->sales - $ds_yesteday->sales;
         $s->today->sign = $this->getSign($ds_today->sales - $ds_yesteday->sales);
         $c->sales1 = $ds_yesteday->sales - $ds_otherday->sales;
         $s->yesterday->sign = $this->getSign($ds_yesteday->sales - $ds_otherday->sales);
         $s->diff = $c;
         array_push($arr, $s);
     }
     return collect($arr);
 }