public function index(Request $request) { $sort = Input::get('sort') == null ? 'relation_id' : Input::get('sort'); $client_id_select = Input::get('client_id_select') == null ? 'all' : Input::get('client_id_select'); $countCongestion = Input::get('count_congestion') == null ? 'balance_owed' : Input::get('count_congestion'); $sortDirection = Input::get('sortDirection') == null ? 'DESC' : Input::get('sortDirection'); $clients = Client::all(); $debtors = Debtor::all(); $limits = Limit::whereHas('relation', function ($query) use($client_id_select) { if ($client_id_select !== 'all') { $query->where('client_id', '=', $client_id_select); } })->get(); $usedLimit = array(); foreach ($limits as $key => $limit) { $usedLimitItem = 0; foreach ($limit->relation->deliveries as $value) { if ($value->status == 'Профинансирована') { if ($countCongestion == 'remainder_of_the_debt_first_payment') { $usedLimitItem += $value->remainder_of_the_debt_first_payment; } else { $usedLimitItem += $value->balance_owed; } } } $usedLimit[$key] = $usedLimitItem; } $relations = Relation::select('client_id')->distinct()->get(); if ($request->ajax()) { if ($sort == 'client_id') { return view('limits.indexAjaxClient', ['clients' => $clients, 'countCongestion' => $countCongestion]); } else { if ($sort == 'debtor_id') { return view('limits.indexAjaxDebtor', ['countCongestion' => $countCongestion, 'debtors' => $debtors]); } else { return view('limits.indexAjax', ['limits' => $limits, 'relations' => $relations, 'usedLimit' => $usedLimit]); } } } else { return view('limits.index', ['limits' => $limits, 'relations' => $relations, 'usedLimit' => $usedLimit, 'clients' => $clients]); } }