public static function getDefaultPeriod() { if (\Illuminate\Support\Facades\Session::has('period')) { $p = \App\Models\StockPeriods::where(['id' => \Illuminate\Support\Facades\Session::get('period')])->get(); $period = count($p) > 0 ? $p->first() : 0; } else { $current = \App\Models\StockPeriods::whereNull('date_to')->get(); if (count($current) > 0) { $number = $current->first()->number - 1; $p = \App\Models\StockPeriods::where(['number' => $number])->get(); } $period = count($p) > 0 ? $p->first() : 0; } return $period; }
public function index() { $currentPeriodId = Helper::currentPeriodId(); if ($currentPeriodId) { $search = Input::get('q'); $filter = Input::get('filter'); $not_updated = Items::select('count(*)')->whereRaw('not exists (select 1 from stock_items where stock_items.stock_period_id = ' . $currentPeriodId . ' and stock_items.item_id = items.id)')->count(); $items = $search ? Items::where('title', 'LIKE', '%' . $search . '%')->get() : ($filter == 'without_stock' ? Items::select('items.*')->whereRaw('not exists (select 1 from stock_items where stock_items.stock_period_id = ' . $currentPeriodId . ' and stock_items.item_id = items.id)')->get() : Items::all()); if (count($items) == 1 && $search) { return redirect()->action('StockCheckController@edit', ['item_id' => $items->first()->id]); } $last_period = StockPeriods::where(['number' => StockPeriods::findOrFail($currentPeriodId)->number - 1])->get(); return view('StockCheck.index')->with(array('title' => $this->title, 'items' => $search ? Items::where('title', 'LIKE', '%' . $search . '%')->take(10)->get() : ($filter == 'without_stock' ? Items::select('items.*')->whereRaw('not exists (select 1 from stock_items where stock_items.stock_period_id = ' . $currentPeriodId . ' and stock_items.item_id = items.id)')->orderBy("updated_at", 'DESC')->get() : Items::orderBy("updated_at", 'DESC')->take(10)->get()), 'item_list' => Items::lists('title'), 'search' => $search, 'period' => $currentPeriodId, 'previous' => count($last_period) > 0 ? $last_period->first()->id : 0, 'filter' => $filter, 'not_updated' => $not_updated)); } else { Session::flash('flash_message', 'You need to start a period first!'); return Redirect::action('StockPeriodsController@index'); } }