public function name($separator = ' - ') { // Gather Attributtes & Groups $combination = Combination::with('options')->with('options.optiongroup')->findOrFail($this->id); $name = []; foreach ($combination->relations['options'] as $att) { $name[] = $att->optiongroup->name . ': ' . $att->name; } return implode($separator, $name); }
public function fulfill() { // Update Product $product = \App\Product::find($this->product_id); $quantity_onhand = $product->quantity_onhand + $this->quantity; // Average price stuff... (ToDo) $product->quantity_onhand = $quantity_onhand; $product->save(); if ($this->combination_id > 0) { $combination = \App\Combination::find($this->combination_id); $quantity_onhand = $combination->quantity_onhand + $this->quantity; $combination->quantity_onhand = $quantity_onhand; $combination->save(); } // Update Product-Warehouse relationship (quantity) $whs = $product->warehouses; if ($whs->contains($this->warehouse_id)) { $wh = $product->warehouses()->get(); $wh = $wh->find($this->warehouse_id); $quantity = $wh->pivot->quantity + $this->quantity; if ($quantity != 0) { $wh->pivot->quantity = $quantity; $wh->pivot->save(); } else { // Delete record ($quantity = 0) $product->warehouses()->detach($this->warehouse_id); } } else { $product->warehouses()->attach($this->warehouse_id, array('quantity' => $this->quantity)); } if ($this->combination_id > 0) { $whs = $combination->warehouses; if ($whs->contains($this->warehouse_id)) { $wh = $combination->warehouses()->get(); $wh = $wh->find($this->warehouse_id); $quantity = $wh->pivot->quantity + $this->quantity; if ($quantity != 0) { $wh->pivot->quantity = $quantity; $wh->pivot->save(); } else { // Delete record ($quantity = 0) $combination->warehouses()->detach($this->warehouse_id); } } else { $combination->warehouses()->attach($this->warehouse_id, array('quantity' => $this->quantity)); } } }
/** * Make Combinations for specified resource. * * @param int $id * @return Response */ public function combine($id, Request $request) { $groups = $request->input('groups'); // Validate: $groups ahold not be empty, and values should match options table // http://laravel.io/forum/11-12-2014-how-to-validate-array-input // https://www.google.es/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8&client=ubuntu#q=laravel%20validate%20array%20input $product = $this->product->findOrFail($id); // Start Combinator machime! $data = array(); foreach ($groups as $group) { $data[] = \App\Option::where('option_group_id', '=', $group)->orderby('position', 'asc')->lists('id'); } $combos = combos($data); foreach ($combos as $combo) { $combination = \App\Combination::create(array()); $product->combinations()->save($combination); $combination->options()->attach($combo); } // ToDo: Combinations are created with stock = 0. So, set main product stock = 0 too! // El stock del producto principal es la suma de los stocks de las combinaciones! return redirect('products')->with('success', l('This record has been successfully combined :: (:id) ', ['id' => $id], 'layouts')); }