From 7dec701770d66ee18a1f209785d33f548c91519b Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Wed, 29 Jul 2026 08:59:02 +0700 Subject: [PATCH] feat: add total quantity formatting to Purchase model and update related components - Implemented totalQuantityFormatted method in Purchase model to format total quantities by unit. - Updated PurchaseGroupedTable.vue to display total quantity formatted in the purchase details. - Modified TypeScript type definition to include total_quantity_formatted property. --- app/Models/Purchase.php | 23 +++++++++++++++++++ .../purchases/table/PurchaseGroupedTable.vue | 2 ++ resources/js/types/purchase.ts | 1 + 3 files changed, 26 insertions(+) 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) {

+ Total Jumlah + {{ purchase.total_quantity_formatted }} Subtotal {{ purchase.subtotal_formatted }} Diskon {{ purchase.discount_formatted diff --git a/resources/js/types/purchase.ts b/resources/js/types/purchase.ts index 74a5cbd..c80704e 100644 --- a/resources/js/types/purchase.ts +++ b/resources/js/types/purchase.ts @@ -41,6 +41,7 @@ export type PurchaseListItem = { shipping_cost: number; shipping_cost_formatted: string; total_formatted: string; + total_quantity_formatted?: string; notes: string | null; created_at_formatted: string; created_by_name?: string;