public function setDefault($id)
 {
     $itemUnit = ItemUnits::findOrFail($id);
     Helper::add($id, 'changed item ' . $itemUnit->item()->first()->title . '(ID ' . $itemUnit->item()->first()->id . ') default unit to ' . $itemUnit->unit()->first()->title);
     ItemUnits::where('item_id', $itemUnit->item_id)->update(['default' => 0]);
     ItemUnits::where('id', $id)->update(['default' => 1]);
     ItemUnits::where(['item_id' => $itemUnit->item_id])->update(['factor' => DB::raw('factor/' . $itemUnit->factor)]);
     StockItem::where(['item_id' => $itemUnit->item_id])->update(['stock' => DB::raw('stock/' . $itemUnit->factor)]);
     RecipeItems::where(['item_id' => $itemUnit->item_id])->update(['value' => DB::raw('value/' . $itemUnit->factor)]);
     Menu::where(['item_id' => $itemUnit->item_id])->update(['value' => DB::raw('value/' . $itemUnit->factor)]);
     ItemPurchases::where(['item_id' => $itemUnit->item_id])->update(['value' => DB::raw('value/' . $itemUnit->factor)]);
     StockCheck::where(['item_id' => $itemUnit->item_id])->update(['before' => DB::raw('`before` / ' . $itemUnit->factor), 'after' => DB::raw('`after` / ' . $itemUnit->factor)]);
     return Redirect::action('ItemUnitsController@index', array('item_id' => $itemUnit->item_id));
 }
 public function edit($id)
 {
     $item = Items::findOrFail($id);
     $other = $item->units()->where(['default' => 0])->first();
     $actions = ['add' => 'Add', 'reduce' => 'Reduce by', 'change' => 'Change to'];
     $other_units = [];
     if ($other) {
         foreach ($item->units()->where(['default' => 0])->get() as $unit) {
             $other_units[$unit->id] = $unit->unit()->first()->title;
         }
     }
     $currentPeriodId = Helper::periodAfterId(Helper::defaultPeriodId());
     $periods = StockPeriods::all();
     $period_list = array();
     foreach ($periods as $period) {
         $period_list[$period->id] = 'Stock #' . $period->number . ' (' . $period->date_from . ' - ' . ($period->id == $currentPeriodId ? 'NOW' : $period->date_to) . ')';
     }
     if (Input::has('stock_period')) {
         $currentPeriodId = Input::get('stock_period');
     }
     return view('StockCheck.edit')->with(array('title' => $this->title, 'item' => $item, 'default' => $item->units()->where(['default' => 1])->first(), 'other' => $other, 'actions' => $actions, 'other_units' => $other_units, 'period' => $currentPeriodId, 'stock' => StockCheck::select(['stock_items.*', 'stock_checks.*'])->join('stock_items', 'stock_checks.stock_item_id', '=', 'stock_items.id')->where(['stock_checks.item_id' => $id, 'stock_items.stock_period_id' => $currentPeriodId])->orderBy('stock_checks.created_at', 'DESC')->get(), 'stocks_list' => $period_list));
 }