public function update($id, Request $request) { $Units = Units::findOrFail($id); $item = Items::findOrFail($Units->item_id); $this->validate($request, ['title' => 'required|unique:units|max:100']); $input = $request->all(); Helper::add($id, 'updated unit ' . $Units->title . ' for item ID ' . $input['item_id']); if (array_key_exists('default', $input)) { ItemUnits::where(['default' => 1, 'item_id' => $input['item_id']])->where('id', '!=', $id)->update(['default' => 0]); ItemUnits::where(['item_id' => $input['item_id']])->update(['factor' => DB::raw('factor/' . $input['factor'])]); Helper::add($id, 'changed item ' . $item->title . '(ID ' . $input['item_id'] . ') default unit to ' . $Units->title); StockItem::where(['item_id' => $input['item_id']])->update(['stock' => DB::raw('stock/' . $input['factor'])]); RecipeItems::where(['item_id' => $input['item_id']])->update(['value' => DB::raw('value/' . $input['factor'])]); Menu::where(['item_id' => $input['item_id']])->update(['value' => DB::raw('value/' . $input['factor'])]); ItemPurchases::where(['item_id' => $input['item_id']])->update(['value' => DB::raw('value/' . $input['factor'])]); StockCheck::where(['item_id' => $input['item_id']])->update(['before' => DB::raw('`before` / ' . $input['factor']), 'after' => DB::raw('`after` / ' . $input['factor'])]); $input['default'] = 1; } $Units->fill($input)->save(); Session::flash('flash_message', $this->title . ' successfully added!'); return Redirect::action('UnitsController@index'); }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { $RecipeItems = RecipeItems::findOrFail($id); $recipe = Recipes::findOrFail($RecipeItems->recipe_id); if ($RecipeItems->type == 'recipe') { $item = Recipes::findOrFail($RecipeItems->sub_recipe); } else { $item = Items::findOrFail($RecipeItems->item_id); } $RecipeItems->delete(); Helper::add($id, 'deleted ' . $RecipeItems->type . ' ' . $item->title . ' (ID ' . $RecipeItems->id . ') from recipe ' . $recipe->title . ' (ID ' . $recipe->id . ')'); Session::flash('flash_message', $this->title . ' successfully deleted!'); return Redirect::action('RecipeItemsController@index', ['recipe_id' => $recipe->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)); }
public function updatePrice($id, Request $request) { $item = Items::findOrFail($id); $input = $request->all(); $item->fill($input)->save(); Helper::add($item->id, 'set price for item ' . $item->title); Session::flash('flash_message', $this->title . ' price added!'); return Redirect::action('ItemsController@prices'); }