feat: update CashTransactionType enum and improve transaction labels; refactor EmployeeAdvanceService and PayrollPeriodService for consistency

This commit is contained in:
Yoga Pangestu 2026-08-06 15:26:46 +07:00
parent 6ebcbc24c0
commit 42e04673c6
8 changed files with 50 additions and 55 deletions

View File

@ -10,16 +10,16 @@ enum CashTransactionType: string
case DEPOSIT = 'deposit'; case DEPOSIT = 'deposit';
case EXPENSE = 'expense'; case EXPENSE = 'expense';
case TRANSFER = 'transfer';
case WITHDRAWAL = 'withdrawal'; case WITHDRAWAL = 'withdrawal';
case EMPLOYEE_ADVANCE = 'employee_advance';
public function label(): string public function label(): string
{ {
return match ($this) { return match ($this) {
self::DEPOSIT => 'Setoran', self::DEPOSIT => 'Deposit',
self::EXPENSE => 'Pengeluaran', self::EXPENSE => 'Pengeluaran',
self::TRANSFER => 'Transfer', self::WITHDRAWAL => 'Withdrawal',
self::WITHDRAWAL => 'Penarikan', self::EMPLOYEE_ADVANCE => 'Kasbon',
}; };
} }
} }

View File

@ -26,14 +26,14 @@ protected function casts(): array
protected function formattedAmount(): Attribute protected function formattedAmount(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn() => 'Rp ' . number_format($this->amount, 0, ',', '.'), get: fn () => 'Rp '.number_format($this->amount, 0, ',', '.'),
); );
} }
protected function formattedPaidAt(): Attribute protected function formattedPaidAt(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn() => $this->paid_at?->translatedFormat('l, d F Y'), get: fn () => $this->paid_at?->translatedFormat('l, d F Y'),
); );
} }

View File

@ -34,14 +34,14 @@ protected function casts(): array
protected function formattedName(): Attribute protected function formattedName(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn() => ucfirst($this->name), get: fn () => ucfirst($this->name),
); );
} }
protected function formattedStock(): Attribute protected function formattedStock(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn() => number_format($this->stock, 0, ',', '.'), get: fn () => number_format($this->stock, 0, ',', '.'),
); );
} }

View File

@ -2,6 +2,7 @@
namespace App\Services\Admin\Finance; namespace App\Services\Admin\Finance;
use App\Enums\CashTransactionType;
use App\Enums\EmployeeAdvanceStatus; use App\Enums\EmployeeAdvanceStatus;
use App\Models\EmployeeAdvance; use App\Models\EmployeeAdvance;
use App\Models\EmployeeAdvancePayment; use App\Models\EmployeeAdvancePayment;
@ -97,8 +98,9 @@ public function delete(EmployeeAdvance $employeeAdvance): bool
public function approve(EmployeeAdvance $employeeAdvance): EmployeeAdvance public function approve(EmployeeAdvance $employeeAdvance): EmployeeAdvance
{ {
$cashTransaction = $this->debitCash( $cashTransaction = $this->debitCash(
amount: $employeeAdvance->amount, $employeeAdvance->amount,
description: 'Kasbon: '.$employeeAdvance->description, 'Kasbon: '.$employeeAdvance->description,
CashTransactionType::EMPLOYEE_ADVANCE
); );
$employeeAdvance->update([ $employeeAdvance->update([
@ -131,8 +133,9 @@ public function pay(EmployeeAdvance $employeeAdvance, int $amount): EmployeeAdva
$employeeAdvance = DB::transaction(function () use ($employeeAdvance, $amount) { $employeeAdvance = DB::transaction(function () use ($employeeAdvance, $amount) {
$cashTransaction = $this->creditCash( $cashTransaction = $this->creditCash(
amount: $amount, $amount,
description: 'Pembayaran kasbon: '.$employeeAdvance->description, 'Pembayaran kasbon: '.$employeeAdvance->description,
CashTransactionType::EMPLOYEE_ADVANCE
); );
EmployeeAdvancePayment::create([ EmployeeAdvancePayment::create([

View File

@ -6,7 +6,6 @@
use App\Enums\PayrollStatus; use App\Enums\PayrollStatus;
use App\Models\Payroll; use App\Models\Payroll;
use App\Models\PayrollPeriod; use App\Models\PayrollPeriod;
use App\Services\Concerns\HandlesCashTransactions;
use App\Services\NotificationService; use App\Services\NotificationService;
use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@ -15,8 +14,6 @@
class PayrollPeriodService class PayrollPeriodService
{ {
use HandlesCashTransactions;
private function canViewAll(): bool private function canViewAll(): bool
{ {
return auth()->user()->hasAnyRole(['developer', 'owner', 'direktur', 'admin-toko']); return auth()->user()->hasAnyRole(['developer', 'owner', 'direktur', 'admin-toko']);

View File

@ -30,14 +30,14 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
->with([ ->with([
'createdBy:id', 'createdBy:id',
'createdBy.userProfile:id,user_id,full_name', 'createdBy.userProfile:id,user_id,full_name',
'restockItems' => fn($q) => $q 'restockItems' => fn ($q) => $q
->select(['id', 'restock_id', 'product_variant_id', 'quantity', 'unit_price', 'subtotal']) ->select(['id', 'restock_id', 'product_variant_id', 'quantity', 'unit_price', 'subtotal'])
->orderByRaw('(SELECT name FROM product_variants WHERE product_variants.id = restock_items.product_variant_id)'), ->orderByRaw('(SELECT name FROM product_variants WHERE product_variants.id = restock_items.product_variant_id)'),
'restockItems.productVariant:id,product_id,name,stock,reject_stock,retail_stock', 'restockItems.productVariant:id,product_id,name,stock,reject_stock,retail_stock',
'restockItems.productVariant.product:id,name', 'restockItems.productVariant.product:id,name',
]) ])
->when($search, function ($q) use ($search) { ->when($search, function ($q) use ($search) {
$q->whereHas('restockItems.productVariant.product', fn($sq) => $sq->where('name', 'like', "%{$search}%")) $q->whereHas('restockItems.productVariant.product', fn ($sq) => $sq->where('name', 'like', "%{$search}%"))
->orWhere('notes', 'like', "%{$search}%"); ->orWhere('notes', 'like', "%{$search}%");
}) })
->orderBy($sort, $direction) ->orderBy($sort, $direction)
@ -79,11 +79,11 @@ public function getForCreate(): array
: null; : null;
$capitalPrice = $variant->productPrices $capitalPrice = $variant->productPrices
->first(fn($price) => $price->type === PriceType::CAPITAL); ->first(fn ($price) => $price->type === PriceType::CAPITAL);
$variant->capital_price = $capitalPrice?->price ?? 0; $variant->capital_price = $capitalPrice?->price ?? 0;
$rejectPrice = $variant->productPrices $rejectPrice = $variant->productPrices
->first(fn($price) => $price->type === PriceType::REJECT); ->first(fn ($price) => $price->type === PriceType::REJECT);
$variant->reject_price = $rejectPrice?->price ?? 0; $variant->reject_price = $rejectPrice?->price ?? 0;
}); });
}), }),
@ -93,7 +93,7 @@ public function getForCreate(): array
public function getForEdit(Restock $restock): array public function getForEdit(Restock $restock): array
{ {
$restock->load([ $restock->load([
'restockItems' => fn($q) => $q 'restockItems' => fn ($q) => $q
->orderByRaw('(SELECT name FROM product_variants WHERE product_variants.id = restock_items.product_variant_id)'), ->orderByRaw('(SELECT name FROM product_variants WHERE product_variants.id = restock_items.product_variant_id)'),
'restockItems.productVariant.product', 'restockItems.productVariant.product',
]); ]);
@ -108,7 +108,7 @@ public function getForEdit(Restock $restock): array
'photo_url' => $media 'photo_url' => $media
? $this->s3Service->getTemporaryUrl($media->file_name) ? $this->s3Service->getTemporaryUrl($media->file_name)
: null, : null,
'items' => $restock->restockItems->map(fn(RestockItem $item) => [ 'items' => $restock->restockItems->map(fn (RestockItem $item) => [
'id' => $item->id, 'id' => $item->id,
'product_variant_id' => $item->product_variant_id, 'product_variant_id' => $item->product_variant_id,
'quantity' => $item->quantity, 'quantity' => $item->quantity,
@ -145,7 +145,7 @@ public function create(array $data): Restock
NotificationService::notify( NotificationService::notify(
roles: ['Owner', 'Developer', 'Admin Toko'], roles: ['Owner', 'Developer', 'Admin Toko'],
title: 'Restock Baru', title: 'Restock Baru',
body: 'Restock ' . ($stockType === ProductStockQuality::GOOD->value ? 'produk' : 'reject') . ' sebesar Rp ' . number_format($subtotal, 0, ',', '.') . ' berhasil dicatat oleh ' . auth()->user()->full_name . '.', body: 'Restock '.($stockType === ProductStockQuality::GOOD->value ? 'produk' : 'reject').' sebesar Rp '.number_format($subtotal, 0, ',', '.').' berhasil dicatat oleh '.auth()->user()->full_name.'.',
url: route('admin.manage.restocks.index'), url: route('admin.manage.restocks.index'),
); );
@ -219,7 +219,7 @@ private function buildItemRows(array $items, string $stockType, $now, int &$subt
->get() ->get()
->mapWithKeys(function (ProductVariant $variant) use ($priceType) { ->mapWithKeys(function (ProductVariant $variant) use ($priceType) {
$price = $variant->productPrices $price = $variant->productPrices
->first(fn($p) => $p->type === $priceType); ->first(fn ($p) => $p->type === $priceType);
return [$variant->id => $price?->price ?? 0]; return [$variant->id => $price?->price ?? 0];
}); });

View File

@ -53,18 +53,18 @@ public function paginated(int $perPage = 25, string $search = '', string $sort =
'orderItems.productVariant.product:id,name', 'orderItems.productVariant.product:id,name',
]) ])
->when($search, function ($q) use ($search) { ->when($search, function ($q) use ($search) {
$q->whereHas('orderItems.productVariant.product', fn($sq) => $sq->where('name', 'like', "%{$search}%")) $q->whereHas('orderItems.productVariant.product', fn ($sq) => $sq->where('name', 'like', "%{$search}%"))
->orWhere('order_number', 'like', "%{$search}%") ->orWhere('order_number', 'like', "%{$search}%")
->orWhere('notes', 'like', "%{$search}%"); ->orWhere('notes', 'like', "%{$search}%");
}) })
->when($filters['status'] ?? null, fn($q, $status) => $q->where('status', $status)) ->when($filters['status'] ?? null, fn ($q, $status) => $q->where('status', $status))
->when($filters['channel'] ?? null, fn($q, $channel) => $q->where('channel', $channel)) ->when($filters['channel'] ?? null, fn ($q, $channel) => $q->where('channel', $channel))
->when($filters['payment_type'] ?? null, fn($q, $paymentType) => $q->where('payment_type', $paymentType)) ->when($filters['payment_type'] ?? null, fn ($q, $paymentType) => $q->where('payment_type', $paymentType))
->when($filters['customer_id'] ?? null, fn($q, $customerId) => $q->where('customer_id', $customerId)) ->when($filters['customer_id'] ?? null, fn ($q, $customerId) => $q->where('customer_id', $customerId))
->when($filters['marketing_id'] ?? null, fn($q, $marketingId) => $q->where('marketing_id', $marketingId)) ->when($filters['marketing_id'] ?? null, fn ($q, $marketingId) => $q->where('marketing_id', $marketingId))
->when($filters['created_by_id'] ?? null, fn($q, $createdById) => $q->where('created_by_id', $createdById)) ->when($filters['created_by_id'] ?? null, fn ($q, $createdById) => $q->where('created_by_id', $createdById))
->when($filters['date_from'] ?? null, fn($q, $dateFrom) => $q->whereDate('created_at', '>=', $dateFrom)) ->when($filters['date_from'] ?? null, fn ($q, $dateFrom) => $q->whereDate('created_at', '>=', $dateFrom))
->when($filters['date_to'] ?? null, fn($q, $dateTo) => $q->whereDate('created_at', '<=', $dateTo)) ->when($filters['date_to'] ?? null, fn ($q, $dateTo) => $q->whereDate('created_at', '<=', $dateTo))
->orderBy($sort, $direction) ->orderBy($sort, $direction)
->paginate($perPage); ->paginate($perPage);
@ -99,14 +99,14 @@ public function getSummary(array $filters = []): array
->selectRaw('COALESCE(SUM(subtotal) - SUM(COALESCE(nego_price, subtotal)), 0) as total_discount') ->selectRaw('COALESCE(SUM(subtotal) - SUM(COALESCE(nego_price, subtotal)), 0) as total_discount')
->selectRaw('COALESCE(SUM(total_amount), 0) as total_amount') ->selectRaw('COALESCE(SUM(total_amount), 0) as total_amount')
->selectRaw('COALESCE(SUM(cogs), 0) as total_cogs') ->selectRaw('COALESCE(SUM(cogs), 0) as total_cogs')
->when($filters['status'] ?? null, fn($q, $status) => $q->where('status', $status)) ->when($filters['status'] ?? null, fn ($q, $status) => $q->where('status', $status))
->when($filters['channel'] ?? null, fn($q, $channel) => $q->where('channel', $channel)) ->when($filters['channel'] ?? null, fn ($q, $channel) => $q->where('channel', $channel))
->when($filters['payment_type'] ?? null, fn($q, $paymentType) => $q->where('payment_type', $paymentType)) ->when($filters['payment_type'] ?? null, fn ($q, $paymentType) => $q->where('payment_type', $paymentType))
->when($filters['customer_id'] ?? null, fn($q, $customerId) => $q->where('customer_id', $customerId)) ->when($filters['customer_id'] ?? null, fn ($q, $customerId) => $q->where('customer_id', $customerId))
->when($filters['marketing_id'] ?? null, fn($q, $marketingId) => $q->where('marketing_id', $marketingId)) ->when($filters['marketing_id'] ?? null, fn ($q, $marketingId) => $q->where('marketing_id', $marketingId))
->when($filters['created_by_id'] ?? null, fn($q, $createdById) => $q->where('created_by_id', $createdById)) ->when($filters['created_by_id'] ?? null, fn ($q, $createdById) => $q->where('created_by_id', $createdById))
->when($filters['date_from'] ?? null, fn($q, $dateFrom) => $q->whereDate('created_at', '>=', $dateFrom)) ->when($filters['date_from'] ?? null, fn ($q, $dateFrom) => $q->whereDate('created_at', '>=', $dateFrom))
->when($filters['date_to'] ?? null, fn($q, $dateTo) => $q->whereDate('created_at', '<=', $dateTo)) ->when($filters['date_to'] ?? null, fn ($q, $dateTo) => $q->whereDate('created_at', '<=', $dateTo))
->first(); ->first();
return [ return [
@ -134,9 +134,9 @@ public function getFilterOptions(): array
->with('userProfile:id,user_id,full_name') ->with('userProfile:id,user_id,full_name')
->orderBy('id') ->orderBy('id')
->get() ->get()
->filter(fn(User $user) => $user->userProfile?->full_name) ->filter(fn (User $user) => $user->userProfile?->full_name)
->values() ->values()
->map(fn(User $user) => [ ->map(fn (User $user) => [
'id' => $user->id, 'id' => $user->id,
'name' => $user->userProfile->full_name, 'name' => $user->userProfile->full_name,
]), ]),
@ -162,7 +162,7 @@ public function getForCreate(): array
? $this->s3Service->getTemporaryUrl($media->file_name) ? $this->s3Service->getTemporaryUrl($media->file_name)
: null; : null;
$prices = $variant->productPrices->mapWithKeys(fn($p) => [$p->type->value => $p->price]); $prices = $variant->productPrices->mapWithKeys(fn ($p) => [$p->type->value => $p->price]);
$variant->prices = $prices; $variant->prices = $prices;
}); });
}), }),
@ -176,11 +176,11 @@ public function getForCreate(): array
->with('userProfile:id,user_id,full_name') ->with('userProfile:id,user_id,full_name')
->orderBy('id') ->orderBy('id')
->get() ->get()
->filter(fn(User $user) => $user->userProfile?->full_name) ->filter(fn (User $user) => $user->userProfile?->full_name)
->values(), ->values(),
'channelOptions' => OrderChannel::toSelect(), 'channelOptions' => OrderChannel::toSelect(),
'paymentTypeOptions' => PaymentType::toSelect(), 'paymentTypeOptions' => PaymentType::toSelect(),
'priceTypeOptions' => PriceType::toSelect()->filter(fn($p) => $p['value'] !== PriceType::CAPITAL->value)->values(), 'priceTypeOptions' => PriceType::toSelect()->filter(fn ($p) => $p['value'] !== PriceType::CAPITAL->value)->values(),
]; ];
} }
@ -210,7 +210,7 @@ public function getForEdit(Order $order): array
'photo_url' => $media 'photo_url' => $media
? $this->s3Service->getTemporaryUrl($media->file_name) ? $this->s3Service->getTemporaryUrl($media->file_name)
: null, : null,
'items' => $order->orderItems->map(fn(OrderItem $item) => [ 'items' => $order->orderItems->map(fn (OrderItem $item) => [
'id' => $item->id, 'id' => $item->id,
'product_variant_id' => $item->product_variant_id, 'product_variant_id' => $item->product_variant_id,
'quantity' => $item->quantity, 'quantity' => $item->quantity,
@ -270,7 +270,7 @@ public function create(array $data): Order
NotificationService::notify( NotificationService::notify(
roles: ['Owner', 'Developer', 'Admin Toko'], roles: ['Owner', 'Developer', 'Admin Toko'],
title: 'Transaksi Baru', title: 'Transaksi Baru',
body: 'Transaksi ' . $order->order_number . ' sebesar Rp ' . number_format($totalAmount, 0, ',', '.') . ' berhasil dicatat oleh ' . auth()->user()->full_name . '.', body: 'Transaksi '.$order->order_number.' sebesar Rp '.number_format($totalAmount, 0, ',', '.').' berhasil dicatat oleh '.auth()->user()->full_name.'.',
url: route('admin.manage.transactions.index'), url: route('admin.manage.transactions.index'),
); );
@ -379,14 +379,14 @@ private function buildItemRows(array $items, string $stockType, string $priceTyp
$prices = $variants->mapWithKeys(function (ProductVariant $variant) use ($resolvedPriceType) { $prices = $variants->mapWithKeys(function (ProductVariant $variant) use ($resolvedPriceType) {
$price = $variant->productPrices $price = $variant->productPrices
->first(fn($p) => $p->type === $resolvedPriceType); ->first(fn ($p) => $p->type === $resolvedPriceType);
return [$variant->id => $price?->price ?? 0]; return [$variant->id => $price?->price ?? 0];
}); });
$capitalPrices = $variants->mapWithKeys(function (ProductVariant $variant) { $capitalPrices = $variants->mapWithKeys(function (ProductVariant $variant) {
$price = $variant->productPrices $price = $variant->productPrices
->first(fn($p) => $p->type === PriceType::CAPITAL); ->first(fn ($p) => $p->type === PriceType::CAPITAL);
return [$variant->id => $price?->price ?? 0]; return [$variant->id => $price?->price ?? 0];
}); });
@ -429,6 +429,6 @@ private function generateOrderNumber(): string
$sequence = 1; $sequence = 1;
} }
return $prefix . $date . str_pad($sequence, 4, '0', STR_PAD_LEFT); return $prefix.$date.str_pad($sequence, 4, '0', STR_PAD_LEFT);
} }
} }

View File

@ -77,11 +77,6 @@ export function createTransactionColumns(
<span className="font-medium"> <span className="font-medium">
{getTypeLabel(transaction.type)} {getTypeLabel(transaction.type)}
</span> </span>
<span className="text-xs text-muted-foreground">
{getReferenceLabel(
transaction.reference?.type ?? '',
)}
</span>
</div> </div>
); );
}, },