Example #1
0
 public function adminGraph()
 {
     $result = ['newcomers' => [], 'orga' => ['wei' => 0, 'sandwitch' => 0, 'guarantee' => 0], 'ce' => ['wei' => 0, 'sandwitch' => 0, 'guarantee' => 0], 'vieux' => ['wei' => 0, 'sandwitch' => 0, 'guarantee' => 0]];
     $newscomers = Newcomer::with('weiPayment', 'sandwichPayment', 'guaranteePayment')->get();
     $students = Student::with('weiPayment', 'sandwichPayment', 'guaranteePayment')->get();
     foreach ($newscomers as $newcomer) {
         if (!isset($result['newcomers'][$newcomer->branch])) {
             $result['newcomers'][$newcomer->branch] = [];
             $result['newcomers'][$newcomer->branch]['wei'] = 0;
             $result['newcomers'][$newcomer->branch]['sandwitch'] = 0;
             $result['newcomers'][$newcomer->branch]['guarantee'] = 0;
         }
         if (isset($newcomer->weiPayment) && $newcomer->weiPayment->state == 'paid') {
             $result['newcomers'][$newcomer->branch]['wei'] += 1;
         }
         if (isset($newcomer->sandwichPayment) && $newcomer->sandwichPayment->state == 'paid') {
             $result['newcomers'][$newcomer->branch]['sandwitch'] += 1;
         }
         if (isset($newcomer->guaranteePayment) && $newcomer->guaranteePayment->state == 'paid') {
             $result['newcomers'][$newcomer->branch]['guarantee'] += 1;
         }
     }
     foreach ($students as $student) {
         $ret =& $result['vieux'];
         if ($student->ce && $student->team_accepted && $student->team_id) {
             $ret =& $result['ce'];
         } elseif ($student->orga) {
             $ret =& $result['orga'];
         }
         if (isset($student->weiPayment) && $student->weiPayment->state == 'paid') {
             $ret['wei'] += 1;
         }
         if (isset($student->sandwichPayment) && $student->sandwichPayment->state == 'paid') {
             $ret['sandwitch'] += 1;
         }
         if (isset($student->guaranteePayment) && $student->guaranteePayment->state == 'paid') {
             $ret['guarantee'] += 1;
         }
     }
     $graphPaid = Payment::select(DB::raw('DATE_FORMAT(created_at,\'%d-%m-%Y\') as day'), DB::raw('COUNT(id) as sum'))->where('type', 'payment')->where('state', 'paid')->where('amount', '>', 325)->orderBy('created_at')->groupBy(DB::raw('DATE_FORMAT(created_at,\'%d-%m-%Y\')'))->get();
     $graphCaution = Payment::select(DB::raw('DATE_FORMAT(created_at,\'%d-%m-%Y\') as day'), DB::raw('COUNT(id) as sum'))->where('type', 'guarantee')->where('state', 'paid')->orderBy('created_at')->groupBy(DB::raw('DATE_FORMAT(created_at,\'%d-%m-%Y\')'))->get();
     $graphFood = Payment::select(DB::raw('DATE_FORMAT(created_at,\'%d-%m-%Y\') as day'), DB::raw('COUNT(id) as sum'))->where('type', 'payment')->where(DB::raw('`amount`%500'), 325)->where('state', 'paid')->orderBy('created_at')->groupBy(DB::raw('DATE_FORMAT(created_at,\'%d-%m-%Y\')'))->get();
     $sum = ['paid' => array_sum(array_column($graphPaid->toArray(), 'sum')), 'caution' => array_sum(array_column($graphCaution->toArray(), 'sum')), 'food' => array_sum(array_column($graphFood->toArray(), 'sum'))];
     return View::make('dashboard.wei.graph', ['graphPaid' => $graphPaid, 'graphCaution' => $graphCaution, 'graphFood' => $graphFood, 'sum' => $sum, 'global' => $result]);
 }