private function getData($stock, $category) { $last_period = $stock; $current_period = Helper::periodAfterId($last_period); $item_purchases = []; $last_stock = []; $current_stock = []; $item_sales = []; $item_wastes = []; $items = []; $wastage = []; $sales_chart = []; $items_without_price = 0; $period = null; if ($last_period) { $purchases = Purchases::orderBy('date_created', 'ASC')->where(['stock_period_id' => $last_period])->get(); foreach ($purchases as $purchase) { foreach ($purchase->purchases()->get() as $itemPurchase) { if (array_key_exists($itemPurchase->item_id, $item_purchases)) { $item_purchases[$itemPurchase->item_id]['value'] += $itemPurchase->value; $item_purchases[$itemPurchase->item_id]['occurrences']++; $item_purchases[$itemPurchase->item_id]['price'] += $itemPurchase->value == 0 ? 0 : $itemPurchase->price / $itemPurchase->value; } else { $item_purchases[$itemPurchase->item_id]['value'] = $itemPurchase->value; $item_purchases[$itemPurchase->item_id]['price'] = $itemPurchase->value == 0 ? 0 : $itemPurchase->price / $itemPurchase->value; $item_purchases[$itemPurchase->item_id]['occurrences'] = 1; } } } foreach ($item_purchases as $key => $purchase) { $item_purchases[$key]['price'] = $purchase['price'] / $purchase['occurrences']; } $last_stock_items = StockItem::where(['stock_period_id' => $last_period])->get(); foreach ($last_stock_items as $si) { if (array_key_exists($si->item_id, $last_stock)) { $last_stock[$si->item_id] += $si->stock; } else { $last_stock[$si->item_id] = $si->stock; } } $current_stock_items = StockItem::where(['stock_period_id' => $current_period])->get(); foreach ($current_stock_items as $si) { if (array_key_exists($si->item_id, $current_stock)) { $current_stock[$si->item_id] += $si->stock; } else { $current_stock[$si->item_id] = $si->stock; } } $sales = Sales::where(['stock_period_id' => $last_period])->get(); foreach ($sales as $sale) { foreach ($sale->sales()->orderBy('quantity', 'DESC')->get() as $sale_item) { $menu = $sale_item->menu()->first(); if ($menu) { $color = $this->rand_color(); $sales_chart[$menu->id] = ['value' => $sale_item->quantity, 'color' => $color, 'highlight' => $this->alter_brightness($color, 20), 'label' => $menu->title]; if ($menu->type == 'item') { if (array_key_exists($menu->item_id, $item_sales)) { $item_sales[$menu->item_id] += $menu->value * $sale_item->quantity; } else { $item_sales[$menu->item_id] = $menu->value * $sale_item->quantity; } } elseif ($menu->type == 'recipe') { $recipe = $menu->recipe()->first(); if ($recipe) { $usage = $this->countUsageFromRecipe($recipe); foreach ($usage as $key => $use) { if (array_key_exists($key, $item_sales)) { $item_sales[$key] += $sale_item->quantity * $use; } else { $item_sales[$key] = $sale_item->quantity * $use; } } } } } } } $wastes = Wastes::where(['stock_period_id' => $last_period])->get(); foreach ($wastes as $waste) { if (array_key_exists($waste->reason()->first()->id, $wastage)) { $wastage[$waste->reason()->first()->id]['value'] = $wastage[$waste->reason()->first()->id]['value'] + 1; } else { $color = $this->rand_color(); $wastage[$waste->reason()->first()->id] = ['value' => 1, 'color' => $color, 'highlight' => $this->alter_brightness($color, 20), 'label' => $waste->reason()->first()->reason]; } if ($waste->type == 'item') { if (array_key_exists($waste->item_id, $item_wastes)) { $item_wastes[$waste->item_id] += $waste->value; } else { $item_wastes[$waste->item_id] = $waste->value; } } elseif ($waste->type == 'recipe') { $recipe = $waste->recipe()->first(); if ($recipe) { $usage = $this->countUsageFromRecipe($recipe); foreach ($usage as $key => $use) { if (array_key_exists($key, $item_wastes)) { $item_wastes[$key] += $waste->recipe_count * $use; } else { $item_wastes[$key] = $waste->recipe_count * $use; } } } } elseif ($waste->type == 'menu') { $menu = $waste->menu()->first(); if ($menu) { if ($menu->type == 'item') { if (array_key_exists($menu->item_id, $item_wastes)) { $item_wastes[$menu->item_id] += $waste->menu_count * $menu->value; } else { $item_wastes[$menu->item_id] = $waste->menu_count * $menu->value; } } elseif ($menu->type == 'recipe') { $recipe = $menu->recipe()->first(); if ($recipe) { $usage = $this->countUsageFromRecipe($recipe); foreach ($usage as $key => $use) { if (array_key_exists($key, $item_wastes)) { $item_wastes[$key] += $waste->menu_count * $use; } else { $item_wastes[$key] = $waste->menu_count * $use; } } } } } } } } $all_items = Items::all(); $variance = 0; $count = 0; foreach ($all_items as $item) { if (!array_key_exists($item->category()->first()->id, $items)) { $items[$item->category()->first()->id]['category'] = $item->category()->first()->title; $items[$item->category()->first()->id]['variance'] = 0; $items[$item->category()->first()->id]['items'] = []; } $current_item = []; //$items[$item->id]['object'] = $item; $current_item['title'] = $item->title; $current_item['units'] = $item->units()->where(['default' => 1])->first()->unit()->first()->title; $current_item['wastage'] = array_key_exists($item->id, $item_wastes) ? $item_wastes[$item->id] : 0; $current_item['last_stock'] = array_key_exists($item->id, $last_stock) ? $last_stock[$item->id] : 0; $current_item['current_stock'] = array_key_exists($item->id, $current_stock) ? $current_stock[$item->id] : 0; if (array_key_exists($item->id, $item_purchases)) { $current_item['purchases'] = $item_purchases[$item->id]; } else { $price = 0; $item_price = ItemPurchases::where(['item_id' => $item->id])->orderBy('created_at', 'DESC')->first(); if ($item_price) { $value = $item_price->value; if ($value == 0) { $value = 1; } $price = $item_price->price / $value; } else { if ($item->price) { $price = $item->price; } else { $items_without_price++; } } $current_item['purchases'] = ['value' => 0, 'price' => $price, 'occurrences' => 0]; } $current_item['sales'] = array_key_exists($item->id, $item_sales) ? $item_sales[$item->id] : 0; $current_item['must_stock'] = $current_item['last_stock'] + $current_item['purchases']['value'] - $current_item['sales'] - $current_item['wastage']; $current_item['stock_difference'] = $current_item['current_stock'] - $current_item['must_stock']; $current_item['variance'] = round($current_item['stock_difference'] * $current_item['purchases']['price'], 2); $variance += $current_item['variance']; $items[$item->category()->first()->id]['variance'] += $current_item['variance']; $items[$item->category()->first()->id]['items'][$item->id] = $current_item; $count++; } return $category && array_key_exists($category, $items) ? $items[$category] : $items; }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id, Request $request) { $waste = Wastes::findOrFail($id); $waste->delete(); Helper::add(DB::getPdo()->lastInsertId(), 'deleted waste (ID: ' . DB::getPdo()->lastInsertId() . ')'); Session::flash('flash_message', $this->title . ' successfully deleted!'); $input = $request->all(); $variables = []; if (array_key_exists('stock_period', $input)) { $variables['stock_period'] = $input['stock_period']; } return Redirect::action('WastesController@index', $variables); }