start_date = now()->startOfMonth()->format('Y-m-d'); $this->end_date = now()->endOfMonth()->format('Y-m-d'); $this->productionArray = ['no_production', 'production']; $this->article_type = 'article'; $this->getIngredients(); // Load ingredients on mount } public function getIngredients() { $subquery = OrderLineItemArticles::select( 'production_date', 'article_id', 'menu_id', DB::raw('SUM(qty_to_send) as total_qty') ) ->where('menu_article_type', '!=', 'bundle-child') ->whereBetween('production_date', [$this->start_date, $this->end_date]) ->groupBy('production_date', 'article_id', 'menu_id'); // Main query $query = OrderLineItemArticles::select('order_line_item_articles.*', 'agg.total_qty') ->joinSub($subquery, 'agg', function ($join) { $join->on('order_line_item_articles.article_id', '=', 'agg.article_id') ->on('order_line_item_articles.menu_id', '=', 'agg.menu_id') ->on('order_line_item_articles.production_date', '=', 'agg.production_date'); }); if (!empty($this->productionArray)) { $query->whereIn('no_production', $this->productionArray); } if ($this->article_type) { $query->where('article_type', $this->article_type); } $orders = $query->get(); // Populate the full_final_ing array foreach ($orders as $article) { $article_id = $article->article_id; $article_data = Article::find($article_id); $total_weight = $receipe_weight = $article_data->receipe_weight; if ($article_data->total_weight_unit === 'KG') { $total_weight *= 1000; } $ing_arr = []; $ingredients = $article_data->ingredients; foreach ($ingredients as $ingredient) { $main_ingredient = Ingredients::with(['suppliers'])->find($ingredient->ingredients_id); $supplier_name = $main_ingredient->suppliers->first()->name ?? ' - '; $final_weight = ($total_weight != 0) ? ($total_weight * $ingredient->product_quantity) / $total_weight : 0; if (!in_array($ingredient->id, $ing_arr)) { $ing_arr[] = $main_ingredient->id; $this->full_final_ing[$main_ingredient->id] = [ 'ingredient_id' => $main_ingredient->id, 'ingredient_name' => $main_ingredient->name, 'supplier' => $supplier_name, 'total_weight' => $final_weight, 'weight_unit' => $ingredient->product_unit, 'barcode' => $main_ingredient->barcode, ]; } else { $this->full_final_ing[$main_ingredient->id]['total_weight'] += $final_weight; } } } } public function query(): Collection { return collect($this->full_final_ing); } public function table(Table $table): Table { return $table ->columns([ TextColumn::make('ingredient_name')->label('Ingredient'), TextColumn::make('supplier')->label('Supplier'), TextColumn::make('total_weight')->label('Total Weight'), TextColumn::make('weight_unit')->label('Weight Unit'), TextColumn::make('barcode')->label('Barcode'), ]) ->filters([]) ->actions([]) ->emptyStateIcon('heroicon-o-x-circle') ->emptyStateHeading('No Ingredients Found') ->emptyStateDescription('There are no ingredients available for the selected criteria.'); } public function updated($propertyName) { if (in_array($propertyName, ['start_date', 'end_date', 'productionArray', 'article_type'])) { $this->getIngredients(); } } }