@php use App\CoreFacturalo\Helpers\Template\ReportHelper; @endphp @php $sold_items = $documents->whereIn('record_type', ['sale_note_item', 'document_item'])->groupBy('item_id'); $table_sold_items = $sold_items->map(function ($row, $key) { $last_item = $row->last(); $description = $last_item['item']->description ?? 'Error al obtener nombre de producto.'; $unit_value = $last_item['unit_value']; // $quantity = $row->sum('quantity'); $quantity = $row->reduce(function ($carry, $g_item) { $item = $g_item['item']; $g_quantity = $g_item['quantity']; $qty = 0; $factor_presentation = 1; if (isset($item->presentation)) { $presentation = $item->presentation; // if($presentation != null || ) //si presentacion no es un objeto, entonces es un array if (is_object($presentation)) { $factor_presentation = $presentation->quantity_unit; } } $qty = $g_quantity * $factor_presentation; return $carry + $qty; }, 0); $total = $row->sum('total'); return [ 'item_id' => $last_item['item_id'], 'description' => $description, 'quantity' => $quantity, 'unit_value' => $unit_value, 'total' => $total, ]; }); @endphp @foreach ($table_sold_items as $item) @endforeach
Totales por producto
# Producto Cantidad Ăšltimo V. Unitario Total
{{ $loop->iteration }} {{ $item['description'] }} {{ $item['quantity'] }} {{ ReportHelper::setNumber($item['unit_value'], 2, '.', '') }} {{ ReportHelper::setNumber($item['total'], 2, '.', '') }}
Totales {{ ReportHelper::setNumber($table_sold_items->sum('total'), 2, '.', '') }}