diff --git a/app/Models/Purchase.php b/app/Models/Purchase.php index 8bd4e91..64d8f65 100644 --- a/app/Models/Purchase.php +++ b/app/Models/Purchase.php @@ -25,6 +25,7 @@ 'shipping_cost_formatted', 'subtotal_formatted', 'total_formatted', + 'total_quantity_formatted', ])] class Purchase extends Model implements HasMedia { @@ -78,6 +79,28 @@ public function totalFormatted(): Attribute ); } + public function totalQuantityFormatted(): Attribute + { + return Attribute::make( + get: function () { + $groups = []; + + foreach ($this->items as $item) { + $unit = $item->unit_abbreviation ?? $item->rawMaterialPrice?->rawMaterial?->unit?->abbreviation() ?? ''; + $groups[$unit] = ($groups[$unit] ?? 0) + (float) $item->quantity; + } + + return collect($groups) + ->filter(fn (float $total) => $total > 0) + ->map(function (float $total, string $unit) { + $formatted = rtrim(rtrim(number_format($total, 2, ',', '.'), '0'), ','); + return "{$formatted} {$unit}"; + }) + ->join(', '); + }, + ); + } + // 4. Other Methods public static function mediaModuleName(): string { diff --git a/resources/js/pages/admin/manage/purchases/table/PurchaseGroupedTable.vue b/resources/js/pages/admin/manage/purchases/table/PurchaseGroupedTable.vue index 4e4c0b7..3a6c961 100644 --- a/resources/js/pages/admin/manage/purchases/table/PurchaseGroupedTable.vue +++ b/resources/js/pages/admin/manage/purchases/table/PurchaseGroupedTable.vue @@ -145,6 +145,8 @@ function openVerificationDetail(requestId: number | undefined) {