feat: add predis/predis dependency and implement caching in various services for improved performance
This commit is contained in:
parent
632ca69c3a
commit
00ef124fb3
@ -3,7 +3,9 @@
|
||||
namespace App\Http\Requests\Admin\Manage;
|
||||
|
||||
use App\Enums\Permission;
|
||||
use App\Enums\ProductStockQuality;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StokOpnameAutoSaveRequest extends FormRequest
|
||||
{
|
||||
@ -25,7 +27,7 @@ public function rules(): array
|
||||
'notes' => ['nullable', 'string', 'max:1000'],
|
||||
'items' => ['nullable', 'array'],
|
||||
'items.*.product_variant_id' => ['required', 'integer', 'exists:product_variants,id'],
|
||||
'items.*.stock_quality' => ['required', \Illuminate\Validation\Rule::enum(\App\Enums\ProductStockQuality::class)],
|
||||
'items.*.stock_quality' => ['required', Rule::enum(ProductStockQuality::class)],
|
||||
'items.*.physical_stock' => ['nullable', 'integer', 'min:0'],
|
||||
'items.*.notes' => ['nullable', 'string', 'max:500'],
|
||||
];
|
||||
|
||||
@ -3,7 +3,9 @@
|
||||
namespace App\Http\Requests\Admin\Manage;
|
||||
|
||||
use App\Enums\Permission;
|
||||
use App\Enums\ProductStockQuality;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StokOpnameRequest extends FormRequest
|
||||
{
|
||||
@ -23,7 +25,7 @@ public function rules(): array
|
||||
'notes' => ['nullable', 'string', 'max:1000'],
|
||||
'items' => ['required', 'array', 'min:1'],
|
||||
'items.*.product_variant_id' => ['required', 'integer', 'exists:product_variants,id'],
|
||||
'items.*.stock_quality' => ['required', \Illuminate\Validation\Rule::enum(\App\Enums\ProductStockQuality::class)],
|
||||
'items.*.stock_quality' => ['required', Rule::enum(ProductStockQuality::class)],
|
||||
'items.*.physical_stock' => ['required', 'integer', 'min:0'],
|
||||
'items.*.notes' => ['nullable', 'string', 'max:500'],
|
||||
];
|
||||
|
||||
@ -36,6 +36,7 @@ public function getProductNameAttribute(): string
|
||||
public function getVariantNameAttribute(): string
|
||||
{
|
||||
$qualityLabel = $this->stock_quality ? ' ('.$this->stock_quality->label().')' : '';
|
||||
|
||||
return $this->productVariant->name.$qualityLabel;
|
||||
}
|
||||
|
||||
|
||||
49
app/Services/Concerns/CachesQuery.php
Normal file
49
app/Services/Concerns/CachesQuery.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Concerns;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
|
||||
trait CachesQuery
|
||||
{
|
||||
protected function cacheRemember(string $key, int $ttl, callable $callback)
|
||||
{
|
||||
return Cache::remember($key, $ttl, $callback);
|
||||
}
|
||||
|
||||
protected function cacheForever(string $key, callable $callback)
|
||||
{
|
||||
return Cache::rememberForever($key, $callback);
|
||||
}
|
||||
|
||||
protected function cacheForget(string $key): void
|
||||
{
|
||||
Cache::forget($key);
|
||||
}
|
||||
|
||||
protected function cacheForgetByPattern(string $pattern): void
|
||||
{
|
||||
if (config('cache.default') !== 'redis') {
|
||||
Cache::flush();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$redis = Redis::connection('cache');
|
||||
|
||||
$prefix = config('cache.prefix', 'laravel_cache');
|
||||
|
||||
$fullPattern = "{$prefix}:{$pattern}";
|
||||
|
||||
$cursor = null;
|
||||
|
||||
do {
|
||||
[$cursor, $keys] = $redis->scan($cursor ?? 0, ['match' => $fullPattern, 'count' => 100]);
|
||||
|
||||
if (! empty($keys)) {
|
||||
$redis->del($keys);
|
||||
}
|
||||
} while ($cursor);
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,7 @@
|
||||
use App\Models\CashAccount;
|
||||
use App\Models\CashTransaction;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Concerns\SyncsPhotos;
|
||||
use App\Services\Media\MediaService;
|
||||
@ -18,7 +19,7 @@
|
||||
|
||||
class CashService
|
||||
{
|
||||
use RunsInTransaction, SyncsPhotos;
|
||||
use CachesQuery, RunsInTransaction, SyncsPhotos;
|
||||
|
||||
private const MAX_PHOTOS = 1;
|
||||
|
||||
@ -29,7 +30,9 @@ public function __construct(
|
||||
|
||||
public function getDefaultAccount(): CashAccount
|
||||
{
|
||||
return $this->cacheRemember('finance:default_account', 86400, function (): CashAccount {
|
||||
return CashAccount::query()->firstOrFail();
|
||||
});
|
||||
}
|
||||
|
||||
public function paginateForIndex(CashAccount $cashAccount, array $tableQuery, string $referenceType = ''): LengthAwarePaginator
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
use App\Models\PayrollAdjustment;
|
||||
use App\Models\PayrollPeriod;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\System\PushNotificationService;
|
||||
use Carbon\Carbon;
|
||||
@ -22,7 +23,7 @@
|
||||
|
||||
class PayrollService
|
||||
{
|
||||
use RunsInTransaction;
|
||||
use CachesQuery, RunsInTransaction;
|
||||
|
||||
public function __construct(
|
||||
private readonly CashService $cashService,
|
||||
@ -31,10 +32,12 @@ public function __construct(
|
||||
|
||||
public function listPeriods(): Collection
|
||||
{
|
||||
return $this->cacheRemember('payroll:periods', 3600, function (): Collection {
|
||||
return PayrollPeriod::query()
|
||||
->orderByDesc('year')
|
||||
->orderByDesc('month')
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
public function resolvePeriod(?int $periodId): ?PayrollPeriod
|
||||
@ -43,6 +46,7 @@ public function resolvePeriod(?int $periodId): ?PayrollPeriod
|
||||
return PayrollPeriod::query()->find($periodId);
|
||||
}
|
||||
|
||||
return $this->cacheRemember('payroll:current_period', 3600, function (): ?PayrollPeriod {
|
||||
return PayrollPeriod::query()
|
||||
->where('status', PayrollPeriodStatus::OPEN)
|
||||
->orderByDesc('year')
|
||||
@ -52,6 +56,7 @@ public function resolvePeriod(?int $periodId): ?PayrollPeriod
|
||||
->orderByDesc('year')
|
||||
->orderByDesc('month')
|
||||
->first();
|
||||
});
|
||||
}
|
||||
|
||||
public function periodSummary(PayrollPeriod $period, User $user): array
|
||||
@ -161,6 +166,8 @@ function () use ($user): PayrollPeriod {
|
||||
'Gagal membuka periode payroll',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('payroll:*');
|
||||
|
||||
return $period;
|
||||
}
|
||||
|
||||
@ -212,6 +219,8 @@ function () use ($payroll, $validated, $user): void {
|
||||
'Gagal menambahkan penyesuaian gaji',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('payroll:*');
|
||||
|
||||
if ($payroll->employee?->user_id) {
|
||||
$typeLabel = PayrollAdjustmentType::from($validated['type'])->label();
|
||||
$formattedAmount = 'Rp '.number_format($validated['amount'], 0, ',', '.');
|
||||
@ -243,6 +252,8 @@ function () use ($payroll, $adjustment, $validated): void {
|
||||
'Gagal memperbarui penyesuaian gaji',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('payroll:*');
|
||||
|
||||
if ($payroll->employee?->user_id) {
|
||||
$typeLabel = PayrollAdjustmentType::from($validated['type'])->label();
|
||||
$formattedAmount = 'Rp '.number_format($validated['amount'], 0, ',', '.');
|
||||
@ -271,6 +282,8 @@ function () use ($payroll, $adjustment): void {
|
||||
'Gagal menghapus penyesuaian gaji',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('payroll:*');
|
||||
|
||||
if ($payroll->employee?->user_id) {
|
||||
$this->pushNotificationService->sendToUser(
|
||||
'📊 Penyesuaian Gaji Dihapus',
|
||||
@ -298,6 +311,8 @@ function () use ($payroll, $user): void {
|
||||
'Gagal membayar gaji (total 0)',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('payroll:*');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -327,6 +342,8 @@ function () use ($payroll, $user): void {
|
||||
'Gagal membayar gaji',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('payroll:*');
|
||||
|
||||
if ($payroll->employee?->user_id) {
|
||||
$this->pushNotificationService->sendToUser(
|
||||
'💸 Gaji Dibayarkan',
|
||||
|
||||
@ -6,6 +6,7 @@
|
||||
use App\Models\Employee;
|
||||
use App\Models\User;
|
||||
use App\Models\UserProfile;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Concerns\SyncsPhotos;
|
||||
use App\Services\Media\MediaService;
|
||||
@ -17,7 +18,7 @@
|
||||
|
||||
class EmployeeService
|
||||
{
|
||||
use RunsInTransaction, SyncsPhotos;
|
||||
use CachesQuery, RunsInTransaction, SyncsPhotos;
|
||||
|
||||
public function __construct(
|
||||
private readonly MediaService $mediaService,
|
||||
@ -60,7 +61,9 @@ public function paginateForIndex(
|
||||
|
||||
public function assignableRoleOptions(): array
|
||||
{
|
||||
return $this->cacheRemember('system:assignable_roles', 86400, function (): array {
|
||||
return Role::assignableSelectOptions();
|
||||
});
|
||||
}
|
||||
|
||||
public function findForEdit(User $user): array
|
||||
@ -109,6 +112,8 @@ function () use ($validated): void {
|
||||
},
|
||||
'Gagal membuat karyawan',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('hr:employees:*');
|
||||
}
|
||||
|
||||
public function update(User $user, array $validated): void
|
||||
@ -162,6 +167,8 @@ function () use ($validated, $user, $employee): void {
|
||||
},
|
||||
'Gagal memperbarui karyawan',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('hr:employees:*');
|
||||
}
|
||||
|
||||
public function toggleStatus(User $user, array $validated): void
|
||||
@ -178,6 +185,8 @@ function () use ($user, $validated): void {
|
||||
},
|
||||
'Gagal memperbarui status karyawan',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('hr:employees:*');
|
||||
}
|
||||
|
||||
public function resetPassword(User $user): void
|
||||
@ -192,6 +201,8 @@ function () use ($user): void {
|
||||
},
|
||||
'Gagal mereset kata sandi karyawan',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('hr:employees:*');
|
||||
}
|
||||
|
||||
public function delete(User $user): void
|
||||
@ -204,6 +215,8 @@ function () use ($user): void {
|
||||
},
|
||||
'Gagal menghapus karyawan',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('hr:employees:*');
|
||||
}
|
||||
|
||||
private function syncProfilePhoto(UserProfile $profile, array $validated): void
|
||||
|
||||
@ -5,10 +5,13 @@
|
||||
use App\Enums\PriceType;
|
||||
use App\Models\CuttingResultPrice;
|
||||
use App\Models\ProductPrice;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class CuttingResultPriceResolver
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function resolve(int $productVariantId, PriceType $priceType): ?CuttingResultPrice
|
||||
{
|
||||
$price = CuttingResultPrice::query()
|
||||
@ -43,6 +46,7 @@ public function resolve(int $productVariantId, PriceType $priceType): ?CuttingRe
|
||||
|
||||
public function latestPricesForVariant(int $productVariantId): array
|
||||
{
|
||||
return $this->cacheRemember("prices:variant:{$productVariantId}", 900, function () use ($productVariantId) {
|
||||
$prices = [];
|
||||
|
||||
foreach (PriceType::cases() as $priceType) {
|
||||
@ -54,10 +58,12 @@ public function latestPricesForVariant(int $productVariantId): array
|
||||
}
|
||||
|
||||
return $prices;
|
||||
});
|
||||
}
|
||||
|
||||
public function latestPricesForVariants(array $variantIds): Collection
|
||||
{
|
||||
return $this->cacheRemember('prices:variants:'.md5(implode(',', $variantIds)), 900, function () use ($variantIds) {
|
||||
if (empty($variantIds)) {
|
||||
return collect();
|
||||
}
|
||||
@ -95,5 +101,6 @@ public function latestPricesForVariants(array $variantIds): Collection
|
||||
}
|
||||
|
||||
return $results->groupBy('product_variant_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@
|
||||
use App\Models\RawMaterial;
|
||||
use App\Models\RawMaterialPrice;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Media\MediaService;
|
||||
use App\Services\System\PushNotificationService;
|
||||
@ -26,7 +27,7 @@
|
||||
|
||||
class CuttingService
|
||||
{
|
||||
use RunsInTransaction;
|
||||
use CachesQuery, RunsInTransaction;
|
||||
|
||||
public function __construct(
|
||||
private readonly PushNotificationService $pushNotificationService,
|
||||
@ -555,6 +556,8 @@ function () use ($validated, $user): Cutting {
|
||||
route('admin.manage.cuttings.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:cuttings:*');
|
||||
|
||||
return $cutting;
|
||||
}
|
||||
|
||||
@ -650,6 +653,8 @@ function () use ($cutting, $validated): void {
|
||||
['owner', 'developer', 'direktur'],
|
||||
route('admin.manage.cuttings.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:cuttings:*');
|
||||
}
|
||||
|
||||
public function delete(Cutting $cutting): void
|
||||
@ -686,6 +691,8 @@ function () use ($cutting): void {
|
||||
['owner', 'developer', 'direktur'],
|
||||
route('admin.manage.cuttings.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:cuttings:*');
|
||||
}
|
||||
|
||||
public function transitionStatus(
|
||||
@ -785,6 +792,9 @@ function () use ($cutting, $status, $verificationNote, $results, $resultPrices,
|
||||
route('admin.manage.stocks.index'),
|
||||
);
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('manage:cuttings:*');
|
||||
$this->cacheForgetByPattern('prices:*');
|
||||
}
|
||||
|
||||
private function buildMaterials(array $materials): array
|
||||
@ -1217,6 +1227,8 @@ public function quickCreateRawMaterial(array $validated): array
|
||||
];
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('manage:cuttings:*');
|
||||
|
||||
return [
|
||||
'id' => $rawMaterial->id,
|
||||
'name' => $rawMaterial->name,
|
||||
@ -1283,6 +1295,8 @@ public function quickCreateProduct(array $validated): array
|
||||
];
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('manage:cuttings:*');
|
||||
|
||||
return [
|
||||
'id' => $product->id,
|
||||
'name' => $product->name,
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductVariant;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Finance\CashService;
|
||||
use App\Services\Media\MediaService;
|
||||
@ -27,7 +28,7 @@
|
||||
|
||||
class OrderService
|
||||
{
|
||||
use RunsInTransaction;
|
||||
use CachesQuery, RunsInTransaction;
|
||||
|
||||
private const MAX_PHOTOS = 1;
|
||||
|
||||
@ -496,6 +497,8 @@ function () use ($validated, $user): Order {
|
||||
route('admin.manage.orders.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:orders:*');
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
@ -584,6 +587,8 @@ function () use ($order, $validated): void {
|
||||
['owner', 'developer', 'direktur'],
|
||||
route('admin.manage.orders.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:orders:*');
|
||||
}
|
||||
|
||||
public function delete(Order $order): void
|
||||
@ -617,6 +622,8 @@ function () use ($order): void {
|
||||
['owner', 'developer', 'direktur'],
|
||||
route('admin.manage.orders.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:orders:*');
|
||||
}
|
||||
|
||||
public function transitionStatus(Order $order, OrderStatus $status): void
|
||||
@ -654,6 +661,8 @@ function () use ($order, $status): void {
|
||||
['owner', 'developer', 'direktur'],
|
||||
route('admin.manage.orders.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:orders:*');
|
||||
}
|
||||
|
||||
private function buildLineItems(array $items, PriceType $priceType): array
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
use App\Models\Purchase;
|
||||
use App\Models\RawMaterial;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Master\ProductService;
|
||||
use App\Services\Master\RawMaterialService;
|
||||
use App\Services\System\PushNotificationService;
|
||||
@ -29,6 +30,8 @@
|
||||
|
||||
class OwnerVerificationService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function __construct(
|
||||
private readonly StockService $stockService,
|
||||
private readonly ProductService $productService,
|
||||
@ -129,6 +132,7 @@ private function pendingCuttingRows(User $user, array $tableQuery): Collection
|
||||
|
||||
public function pendingCountForUser(User $user): int
|
||||
{
|
||||
return $this->cacheRemember("verification:pending_count:{$user->id}", 30, function () use ($user) {
|
||||
$requestCount = OwnerVerificationRequest::query()
|
||||
->pending()
|
||||
->visibleTo($user)
|
||||
@ -139,6 +143,7 @@ public function pendingCountForUser(User $user): int
|
||||
}
|
||||
|
||||
return $requestCount + Cutting::query()->pendingVerification()->count();
|
||||
});
|
||||
}
|
||||
|
||||
public function subjectTypeOptions(User $user): array
|
||||
@ -217,6 +222,8 @@ public function approveRequest(
|
||||
'✅ Pengajuan Disetujui',
|
||||
"Pengajuan {$request->action->label()} {$subjectLabel} '{$this->requestTitle($request)}' telah disetujui owner.",
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('verification:*');
|
||||
}
|
||||
|
||||
public function rejectRequest(
|
||||
@ -274,6 +281,8 @@ public function rejectRequest(
|
||||
'❌ Pengajuan Ditolak',
|
||||
"Pengajuan {$request->action->label()} {$subjectLabel} '{$title}' ditolak dengan alasan: '{$reason}'.",
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('verification:*');
|
||||
}
|
||||
|
||||
private function rejectVerificationRequest(OwnerVerificationRequest $request): void
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
use App\Models\RawMaterialPrice;
|
||||
use App\Models\Supplier;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Media\MediaService;
|
||||
use App\Services\System\PushNotificationService;
|
||||
@ -23,7 +24,7 @@
|
||||
|
||||
class PurchaseService
|
||||
{
|
||||
use RunsInTransaction;
|
||||
use CachesQuery, RunsInTransaction;
|
||||
|
||||
private const MAX_PHOTOS = 1;
|
||||
|
||||
@ -324,6 +325,8 @@ function () use ($validated, $user, $isOwner): Purchase {
|
||||
);
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('manage:purchases:*');
|
||||
|
||||
return $purchase;
|
||||
}
|
||||
|
||||
@ -373,6 +376,8 @@ function () use ($purchase, $validated, $user, $isOwner): void {
|
||||
$purchase->supplier->name,
|
||||
);
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('manage:purchases:*');
|
||||
}
|
||||
|
||||
public function delete(Purchase $purchase, User $user): void
|
||||
@ -381,6 +386,7 @@ public function delete(Purchase $purchase, User $user): void
|
||||
|
||||
if ($isOwner) {
|
||||
$this->executeDelete($purchase);
|
||||
$this->cacheForgetByPattern('manage:purchases:*');
|
||||
|
||||
return;
|
||||
}
|
||||
@ -425,6 +431,8 @@ public function applyVerificationRequest(OwnerVerificationRequest $verificationR
|
||||
'action' => 'Aksi verifikasi belanja tidak didukung.',
|
||||
]),
|
||||
};
|
||||
|
||||
$this->cacheForgetByPattern('manage:purchases:*');
|
||||
}
|
||||
|
||||
public function rejectVerificationRequest(OwnerVerificationRequest $verificationRequest): void
|
||||
@ -436,6 +444,8 @@ public function rejectVerificationRequest(OwnerVerificationRequest $verification
|
||||
'action' => 'Aksi verifikasi belanja tidak didukung.',
|
||||
]),
|
||||
};
|
||||
|
||||
$this->cacheForgetByPattern('manage:purchases:*');
|
||||
}
|
||||
|
||||
public function clearVerificationRequestMedia(OwnerVerificationRequest $verificationRequest): void
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
use App\Models\ProductPrice;
|
||||
use App\Models\ProductVariant;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\System\PushNotificationService;
|
||||
use App\Support\Media\MediaPresenter;
|
||||
use Illuminate\Support\Collection;
|
||||
@ -19,6 +20,8 @@
|
||||
|
||||
class StockService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function __construct(
|
||||
private readonly PushNotificationService $pushNotificationService,
|
||||
) {}
|
||||
@ -131,6 +134,8 @@ public function submitVerification(
|
||||
['owner', 'developer', 'direktur'],
|
||||
route('admin.manage.stocks.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:stocks:*');
|
||||
}
|
||||
|
||||
public function approveVerification(
|
||||
@ -181,6 +186,9 @@ public function approveVerification(
|
||||
['owner', 'developer', 'direktur'],
|
||||
route('admin.manage.stocks.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:stocks:*');
|
||||
$this->cacheForgetByPattern('prices:*');
|
||||
}
|
||||
|
||||
public function rejectVerification(
|
||||
@ -232,6 +240,8 @@ public function rejectVerification(
|
||||
route('admin.manage.stocks.index'),
|
||||
);
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('manage:stocks:*');
|
||||
}
|
||||
|
||||
private function breakMaterialCircularReference(CuttingMaterial $material): void
|
||||
|
||||
@ -3,11 +3,13 @@
|
||||
namespace App\Services\Manage;
|
||||
|
||||
use App\Enums\Permission;
|
||||
use App\Enums\ProductStockQuality;
|
||||
use App\Enums\StokOpnameStatus;
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductVariant;
|
||||
use App\Models\StokOpname;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\System\PushNotificationService;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@ -17,6 +19,8 @@
|
||||
|
||||
class StokOpnameService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function __construct(
|
||||
private readonly PushNotificationService $pushNotificationService,
|
||||
) {}
|
||||
@ -106,7 +110,7 @@ public function findForEdit(StokOpname $stokOpname): array
|
||||
public function create(array $validated, User $user): StokOpname
|
||||
{
|
||||
try {
|
||||
return DB::transaction(function () use ($validated, $user): StokOpname {
|
||||
$stokOpname = DB::transaction(function () use ($validated, $user): StokOpname {
|
||||
$stokOpname = StokOpname::create([
|
||||
'opname_date' => $validated['opname_date'],
|
||||
'notes' => $validated['notes'],
|
||||
@ -118,6 +122,10 @@ public function create(array $validated, User $user): StokOpname
|
||||
|
||||
return $stokOpname;
|
||||
});
|
||||
|
||||
$this->cacheForgetByPattern('manage:stok_opname:*');
|
||||
|
||||
return $stokOpname;
|
||||
} catch (ValidationException $e) {
|
||||
throw $e;
|
||||
} catch (\Throwable $e) {
|
||||
@ -160,6 +168,8 @@ public function update(StokOpname $stokOpname, array $validated): void
|
||||
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('manage:stok_opname:*');
|
||||
}
|
||||
|
||||
public function delete(StokOpname $stokOpname): void
|
||||
@ -186,6 +196,8 @@ public function delete(StokOpname $stokOpname): void
|
||||
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('manage:stok_opname:*');
|
||||
}
|
||||
|
||||
public function submit(StokOpname $stokOpname, User $user): void
|
||||
@ -226,6 +238,8 @@ public function submit(StokOpname $stokOpname, User $user): void
|
||||
['owner', 'developer', 'admin-toko'],
|
||||
route('admin.manage.stok-opnames.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:stok_opname:*');
|
||||
}
|
||||
|
||||
public function verify(StokOpname $stokOpname, User $user, ?string $verificationNotes = null): void
|
||||
@ -241,9 +255,9 @@ public function verify(StokOpname $stokOpname, User $user, ?string $verification
|
||||
foreach ($stokOpname->items as $item) {
|
||||
if ($item->difference !== 0) {
|
||||
$column = match ($item->stock_quality) {
|
||||
\App\Enums\ProductStockQuality::GOOD => 'stock',
|
||||
\App\Enums\ProductStockQuality::RETAIL => 'retail_stock',
|
||||
\App\Enums\ProductStockQuality::REJECT => 'reject_stock',
|
||||
ProductStockQuality::GOOD => 'stock',
|
||||
ProductStockQuality::RETAIL => 'retail_stock',
|
||||
ProductStockQuality::REJECT => 'reject_stock',
|
||||
};
|
||||
$item->productVariant()->update([
|
||||
$column => $item->physical_stock,
|
||||
@ -275,6 +289,8 @@ public function verify(StokOpname $stokOpname, User $user, ?string $verification
|
||||
$stokOpname->created_by_id,
|
||||
route('admin.manage.stok-opnames.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:stok_opname:*');
|
||||
}
|
||||
|
||||
public function reject(StokOpname $stokOpname, User $user, string $reason): void
|
||||
@ -311,12 +327,14 @@ public function reject(StokOpname $stokOpname, User $user, string $reason): void
|
||||
$stokOpname->created_by_id,
|
||||
route('admin.manage.stok-opnames.index'),
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('manage:stok_opname:*');
|
||||
}
|
||||
|
||||
public function autoSave(array $validated, User $user): StokOpname
|
||||
{
|
||||
try {
|
||||
return DB::transaction(function () use ($validated, $user): StokOpname {
|
||||
$stokOpname = DB::transaction(function () use ($validated, $user): StokOpname {
|
||||
$stokOpname = null;
|
||||
|
||||
if (! empty($validated['stok_opname_id'])) {
|
||||
@ -342,6 +360,10 @@ public function autoSave(array $validated, User $user): StokOpname
|
||||
|
||||
return $stokOpname;
|
||||
});
|
||||
|
||||
$this->cacheForgetByPattern('manage:stok_opname:*');
|
||||
|
||||
return $stokOpname;
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Gagal auto-save stok opname: '.$e->getMessage(), [
|
||||
'trace' => $e->getTraceAsString(),
|
||||
@ -361,11 +383,11 @@ private function syncItems(StokOpname $stokOpname, array $items): void
|
||||
continue;
|
||||
}
|
||||
|
||||
$quality = \App\Enums\ProductStockQuality::from($item['stock_quality']);
|
||||
$quality = ProductStockQuality::from($item['stock_quality']);
|
||||
$column = match ($quality) {
|
||||
\App\Enums\ProductStockQuality::GOOD => 'stock',
|
||||
\App\Enums\ProductStockQuality::RETAIL => 'retail_stock',
|
||||
\App\Enums\ProductStockQuality::REJECT => 'reject_stock',
|
||||
ProductStockQuality::GOOD => 'stock',
|
||||
ProductStockQuality::RETAIL => 'retail_stock',
|
||||
ProductStockQuality::REJECT => 'reject_stock',
|
||||
};
|
||||
|
||||
$systemStock = $variant->$column ?? 0;
|
||||
|
||||
@ -3,11 +3,14 @@
|
||||
namespace App\Services\Master;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CategoryService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
||||
{
|
||||
$query = Category::query()
|
||||
@ -28,20 +31,27 @@ public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
||||
public function create(array $validated): void
|
||||
{
|
||||
Category::create($validated);
|
||||
$this->cacheForget('master:categories:options');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
}
|
||||
|
||||
public function update(Category $category, array $validated): void
|
||||
{
|
||||
$category->update($validated);
|
||||
$this->cacheForget('master:categories:options');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
}
|
||||
|
||||
public function delete(Category $category): void
|
||||
{
|
||||
$category->delete();
|
||||
$this->cacheForget('master:categories:options');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
}
|
||||
|
||||
public function getSelectOptions(): array
|
||||
{
|
||||
return $this->cacheRemember('master:categories:options', 3600, function () {
|
||||
return Category::query()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name'])
|
||||
@ -50,6 +60,7 @@ public function getSelectOptions(): array
|
||||
'label' => $category->name,
|
||||
])
|
||||
->all();
|
||||
});
|
||||
}
|
||||
|
||||
private function applySorting(Builder $query, string $sort, string $direction): void
|
||||
|
||||
@ -3,11 +3,14 @@
|
||||
namespace App\Services\Master;
|
||||
|
||||
use App\Models\Customer;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class CustomerService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
||||
{
|
||||
$query = Customer::query()
|
||||
@ -30,25 +33,32 @@ public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
||||
public function create(array $validated): void
|
||||
{
|
||||
Customer::create($validated);
|
||||
$this->cacheForget('master:customers:options');
|
||||
}
|
||||
|
||||
public function createAndReturn(array $validated): Customer
|
||||
{
|
||||
return Customer::create($validated);
|
||||
$customer = Customer::create($validated);
|
||||
$this->cacheForget('master:customers:options');
|
||||
|
||||
return $customer;
|
||||
}
|
||||
|
||||
public function update(Customer $customer, array $validated): void
|
||||
{
|
||||
$customer->update($validated);
|
||||
$this->cacheForget('master:customers:options');
|
||||
}
|
||||
|
||||
public function delete(Customer $customer): void
|
||||
{
|
||||
$customer->delete();
|
||||
$this->cacheForget('master:customers:options');
|
||||
}
|
||||
|
||||
public function getSelectOptions(): array
|
||||
{
|
||||
return $this->cacheRemember('master:customers:options', 3600, function () {
|
||||
return Customer::query()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name'])
|
||||
@ -57,6 +67,7 @@ public function getSelectOptions(): array
|
||||
'label' => $customer->name,
|
||||
])
|
||||
->all();
|
||||
});
|
||||
}
|
||||
|
||||
private function applySorting(Builder $query, string $sort, string $direction): void
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductVariant;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Media\MediaService;
|
||||
use App\Services\System\PushNotificationService;
|
||||
@ -20,7 +21,7 @@
|
||||
|
||||
class ProductService
|
||||
{
|
||||
use RunsInTransaction;
|
||||
use CachesQuery, RunsInTransaction;
|
||||
|
||||
private const MAX_VARIANT_IMAGES = 5;
|
||||
|
||||
@ -166,6 +167,10 @@ function () use ($validated, $user, $isOwner): void {
|
||||
'Gagal membuat produk',
|
||||
);
|
||||
|
||||
if ($isOwner) {
|
||||
$this->cacheForgetByPattern('master:products:*');
|
||||
}
|
||||
|
||||
if (! $isOwner) {
|
||||
$this->notifyForPendingRequest(
|
||||
$user,
|
||||
@ -240,6 +245,9 @@ function () use ($validated, $product, $user, $isOwner): void {
|
||||
'Gagal memperbarui produk',
|
||||
);
|
||||
|
||||
$this->cacheForgetByPattern('master:products:*');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
|
||||
if (! $isOwner) {
|
||||
$this->notifyForPendingRequest(
|
||||
$user,
|
||||
@ -257,6 +265,8 @@ public function delete(Product $product, User $user): void
|
||||
|
||||
if ($isOwner) {
|
||||
$this->applyDeleteSubject($product);
|
||||
$this->cacheForgetByPattern('master:products:*');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
|
||||
return;
|
||||
}
|
||||
@ -296,6 +306,9 @@ public function toggleStatus(Product $product, array $validated, User $user): vo
|
||||
'is_active' => (bool) $validated['is_active'],
|
||||
]);
|
||||
|
||||
$this->cacheForgetByPattern('master:products:*');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -344,6 +357,8 @@ public function applyVerificationRequest(OwnerVerificationRequest $verificationR
|
||||
'action' => 'Aksi verifikasi produk tidak didukung.',
|
||||
]),
|
||||
};
|
||||
|
||||
$this->cacheForgetByPattern('master:products:*');
|
||||
}
|
||||
|
||||
public function rejectVerificationRequest(OwnerVerificationRequest $verificationRequest): void
|
||||
@ -356,6 +371,9 @@ public function rejectVerificationRequest(OwnerVerificationRequest $verification
|
||||
'action' => 'Aksi verifikasi produk tidak didukung.',
|
||||
]),
|
||||
};
|
||||
|
||||
$this->cacheForgetByPattern('master:products:*');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
}
|
||||
|
||||
public function clearVerificationRequestMedia(OwnerVerificationRequest $verificationRequest): void
|
||||
|
||||
@ -10,6 +10,7 @@
|
||||
use App\Models\RawMaterial;
|
||||
use App\Models\RawMaterialPrice;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Media\MediaService;
|
||||
use App\Services\System\PushNotificationService;
|
||||
@ -20,7 +21,7 @@
|
||||
|
||||
class RawMaterialService
|
||||
{
|
||||
use RunsInTransaction;
|
||||
use CachesQuery, RunsInTransaction;
|
||||
|
||||
private const MAX_VARIANT_IMAGES = 5;
|
||||
|
||||
@ -144,6 +145,10 @@ function () use ($validated, $user, $isOwner): void {
|
||||
'Gagal membuat bahan baku',
|
||||
);
|
||||
|
||||
if ($isOwner) {
|
||||
$this->cacheForgetByPattern('master:raw_materials:*');
|
||||
}
|
||||
|
||||
if (! $isOwner) {
|
||||
$this->notifyForPendingRequest(
|
||||
$user,
|
||||
@ -197,6 +202,10 @@ function () use ($validated, $rawMaterial, $user, $isOwner): void {
|
||||
'Gagal memperbarui bahan baku',
|
||||
);
|
||||
|
||||
if ($isOwner) {
|
||||
$this->cacheForgetByPattern('master:raw_materials:*');
|
||||
}
|
||||
|
||||
if (! $isOwner) {
|
||||
$this->notifyForPendingRequest(
|
||||
$user,
|
||||
@ -214,6 +223,7 @@ public function delete(RawMaterial $rawMaterial, User $user): void
|
||||
|
||||
if ($isOwner) {
|
||||
$this->applyDeleteSubject($rawMaterial);
|
||||
$this->cacheForgetByPattern('master:raw_materials:*');
|
||||
|
||||
return;
|
||||
}
|
||||
@ -253,6 +263,8 @@ public function toggleStatus(RawMaterial $rawMaterial, array $validated, User $u
|
||||
'is_active' => (bool) $validated['is_active'],
|
||||
]);
|
||||
|
||||
$this->cacheForgetByPattern('master:raw_materials:*');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -301,6 +313,8 @@ public function applyVerificationRequest(OwnerVerificationRequest $verificationR
|
||||
'action' => 'Aksi verifikasi bahan baku tidak didukung.',
|
||||
]),
|
||||
};
|
||||
|
||||
$this->cacheForgetByPattern('master:raw_materials:*');
|
||||
}
|
||||
|
||||
public function rejectVerificationRequest(OwnerVerificationRequest $verificationRequest): void
|
||||
@ -313,6 +327,8 @@ public function rejectVerificationRequest(OwnerVerificationRequest $verification
|
||||
'action' => 'Aksi verifikasi bahan baku tidak didukung.',
|
||||
]),
|
||||
};
|
||||
|
||||
$this->cacheForgetByPattern('master:raw_materials:*');
|
||||
}
|
||||
|
||||
public function clearVerificationRequestMedia(OwnerVerificationRequest $verificationRequest): void
|
||||
|
||||
@ -3,11 +3,14 @@
|
||||
namespace App\Services\Master;
|
||||
|
||||
use App\Models\Supplier;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class SupplierService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
||||
{
|
||||
$query = Supplier::query()
|
||||
@ -30,20 +33,24 @@ public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
||||
public function create(array $validated): void
|
||||
{
|
||||
Supplier::create($validated);
|
||||
$this->cacheForget('master:suppliers:options');
|
||||
}
|
||||
|
||||
public function update(Supplier $supplier, array $validated): void
|
||||
{
|
||||
$supplier->update($validated);
|
||||
$this->cacheForget('master:suppliers:options');
|
||||
}
|
||||
|
||||
public function delete(Supplier $supplier): void
|
||||
{
|
||||
$supplier->delete();
|
||||
$this->cacheForget('master:suppliers:options');
|
||||
}
|
||||
|
||||
public function getSelectOptions(): array
|
||||
{
|
||||
return $this->cacheRemember('master:suppliers:options', 3600, function () {
|
||||
return Supplier::query()
|
||||
->orderBy('name')
|
||||
->get(['id', 'name'])
|
||||
@ -52,6 +59,7 @@ public function getSelectOptions(): array
|
||||
'label' => $supplier->name,
|
||||
])
|
||||
->all();
|
||||
});
|
||||
}
|
||||
|
||||
private function applySorting(Builder $query, string $sort, string $direction): void
|
||||
|
||||
@ -20,13 +20,17 @@
|
||||
use App\Models\Purchase;
|
||||
use App\Models\RawMaterialPrice;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AnalysisService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function getAttendance(): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_attendance', 900, function () {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -57,10 +61,12 @@ public function getAttendance(): array
|
||||
'absent' => $absent,
|
||||
'on_leave' => $onLeave,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function getMyAttendance(User $user, ?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_my_attendance', 900, function () use ($user, $startDate, $endDate) {
|
||||
$employee = $user->employee;
|
||||
|
||||
if ($employee === null) {
|
||||
@ -120,10 +126,12 @@ public function getMyAttendance(User $user, ?Carbon $startDate = null, ?Carbon $
|
||||
'leave_days' => $leaveDays,
|
||||
'percentage' => $percentage,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function getCashOverview(): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_cash_overview', 900, function () {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -151,10 +159,12 @@ public function getCashOverview(): array
|
||||
'total_deposit' => (int) ($summary->total_deposit ?? 0),
|
||||
'total_withdrawal' => (int) ($summary->total_withdrawal ?? 0),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function getTopSuppliers(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_top_suppliers', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -174,10 +184,12 @@ public function getTopSuppliers(?Carbon $startDate = null, ?Carbon $endDate = nu
|
||||
'purchase_count' => (int) $item->purchase_count,
|
||||
])
|
||||
->toArray();
|
||||
});
|
||||
}
|
||||
|
||||
public function getTopCustomers(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_top_customers', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -200,10 +212,12 @@ public function getTopCustomers(?Carbon $startDate = null, ?Carbon $endDate = nu
|
||||
'order_count' => (int) $item->order_count,
|
||||
])
|
||||
->toArray();
|
||||
});
|
||||
}
|
||||
|
||||
public function getRevenueSummary(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_revenue_summary', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -248,10 +262,12 @@ public function getRevenueSummary(?Carbon $startDate = null, ?Carbon $endDate =
|
||||
'total_orders' => (int) ($revenueSummary->total_orders ?? 0),
|
||||
'avg_order' => (int) ($revenueSummary->avg_order ?? 0),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function getMonthlyRevenue(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_monthly_revenue', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -365,10 +381,12 @@ public function getMonthlyRevenue(?Carbon $startDate = null, ?Carbon $endDate =
|
||||
}
|
||||
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
public function getExpenseSummary(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_expense_summary', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -405,10 +423,12 @@ public function getExpenseSummary(?Carbon $startDate = null, ?Carbon $endDate =
|
||||
'expense_total' => $expenseTotal,
|
||||
'advance_total' => $advanceTotal,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function getMonthlyExpense(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_monthly_expense', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -478,10 +498,12 @@ public function getMonthlyExpense(?Carbon $startDate = null, ?Carbon $endDate =
|
||||
}
|
||||
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_profit_metrics', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -583,10 +605,12 @@ public function getProfitMetrics(?Carbon $startDate = null, ?Carbon $endDate = n
|
||||
'aov' => $aov,
|
||||
'items_per_transaction' => $itemsPerTransaction,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function getRawMaterialStock(): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_raw_material_stock', 900, function () {
|
||||
$prices = RawMaterialPrice::query()
|
||||
->join('raw_materials', 'raw_material_prices.raw_material_id', '=', 'raw_materials.id')
|
||||
->selectRaw('
|
||||
@ -610,10 +634,12 @@ public function getRawMaterialStock(): array
|
||||
'kilogram' => round((float) ($prices->get('kilogram')->total_stock ?? 0), 2),
|
||||
],
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function getProductStock(): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_product_stock', 900, function () {
|
||||
$variants = ProductVariant::query()
|
||||
->join('products', 'product_variants.product_id', '=', 'products.id')
|
||||
->selectRaw('
|
||||
@ -646,10 +672,12 @@ public function getProductStock(): array
|
||||
'total_variants' => (int) ($variants->total_variants ?? 0),
|
||||
'total_categories' => (int) $totalCategories,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function getBusyHours(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_busy_hours', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -674,10 +702,12 @@ public function getBusyHours(?Carbon $startDate = null, ?Carbon $endDate = null)
|
||||
}
|
||||
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
public function getTopProducts(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_top_products', 900, function () use ($startDate, $endDate) {
|
||||
/** @var User|null $user */
|
||||
$user = auth()->user();
|
||||
$isSuper = $user?->hasAnyRole(['developer', 'owner', 'direktur']) ?? false;
|
||||
@ -702,10 +732,12 @@ public function getTopProducts(?Carbon $startDate = null, ?Carbon $endDate = nul
|
||||
'total_revenue' => (int) $item->total_revenue,
|
||||
])
|
||||
->toArray();
|
||||
});
|
||||
}
|
||||
|
||||
public function getMarketingSales(?Carbon $startDate = null, ?Carbon $endDate = null): array
|
||||
{
|
||||
return $this->cacheRemember('analysis:get_marketing_sales', 900, function () use ($startDate, $endDate) {
|
||||
$qtySubquery = DB::table('order_items')
|
||||
->select('order_id', DB::raw('SUM(quantity) as total_qty'))
|
||||
->whereNull('deleted_at')
|
||||
@ -743,6 +775,7 @@ public function getMarketingSales(?Carbon $startDate = null, ?Carbon $endDate =
|
||||
'avg_order' => (int) $item->avg_order,
|
||||
])
|
||||
->toArray();
|
||||
});
|
||||
}
|
||||
|
||||
public function isManager(?User $user): bool
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
use App\Models\Category;
|
||||
use App\Models\Product;
|
||||
use App\Models\SystemConfiguration;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Manage\CuttingResultPriceResolver;
|
||||
use App\Services\System\Setting\HomepageSettingService;
|
||||
use App\Settings\SocialMediaSettings;
|
||||
@ -13,6 +14,8 @@
|
||||
|
||||
class HomepageService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function __construct(
|
||||
private readonly CuttingResultPriceResolver $cuttingResultPriceResolver,
|
||||
private readonly HomepageSettingService $homepageSettingService,
|
||||
@ -20,6 +23,7 @@ public function __construct(
|
||||
|
||||
public function pageData(): array
|
||||
{
|
||||
return $this->cacheRemember('homepage:page_data', 900, function () {
|
||||
$categories = Category::getActiveWithProducts();
|
||||
$products = $this->getProducts();
|
||||
|
||||
@ -44,6 +48,7 @@ public function pageData(): array
|
||||
'tiktokUrl' => $socialSettings->tiktok_url ?? null,
|
||||
'homepage' => $this->homepageSettingService->homepageData(),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
private function getProducts()
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Enums\Permission as PermissionEnum;
|
||||
use App\Enums\Role as EnumsRole;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@ -14,6 +15,8 @@
|
||||
|
||||
class RoleService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
||||
{
|
||||
$query = Role::query()
|
||||
@ -34,6 +37,7 @@ public function paginateForIndex(array $tableQuery): LengthAwarePaginator
|
||||
|
||||
public function permissionOptions(): array
|
||||
{
|
||||
return $this->cacheRemember('system:permissions', 86400, function (): array {
|
||||
return collect(PermissionEnum::cases())
|
||||
->map(fn (PermissionEnum $permission) => [
|
||||
'value' => $permission->value,
|
||||
@ -42,6 +46,7 @@ public function permissionOptions(): array
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
});
|
||||
}
|
||||
|
||||
public function findForEdit(Role $role): array
|
||||
@ -81,6 +86,8 @@ public function create(array $validated): void
|
||||
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('system:roles:*');
|
||||
}
|
||||
|
||||
public function update(Role $role, array $validated): void
|
||||
@ -106,11 +113,15 @@ public function update(Role $role, array $validated): void
|
||||
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
||||
]);
|
||||
}
|
||||
|
||||
$this->cacheForgetByPattern('system:roles:*');
|
||||
}
|
||||
|
||||
public function delete(Role $role): void
|
||||
{
|
||||
$role->delete();
|
||||
|
||||
$this->cacheForgetByPattern('system:roles:*');
|
||||
}
|
||||
|
||||
private function applySorting(Builder $query, string $sort, string $direction): void
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Services\System\Setting;
|
||||
|
||||
use App\Models\HomepageConfiguration;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Concerns\SyncsPhotos;
|
||||
use App\Services\Media\MediaService;
|
||||
@ -10,7 +11,7 @@
|
||||
|
||||
class HomepageSettingService
|
||||
{
|
||||
use RunsInTransaction, SyncsPhotos;
|
||||
use CachesQuery, RunsInTransaction, SyncsPhotos;
|
||||
|
||||
public function __construct(
|
||||
private readonly MediaService $mediaService,
|
||||
@ -18,6 +19,7 @@ public function __construct(
|
||||
|
||||
public function homepageData(): array
|
||||
{
|
||||
return $this->cacheRemember('homepage:settings', 86400, function () {
|
||||
$configuration = HomepageConfiguration::instance();
|
||||
$configuration->load('media');
|
||||
|
||||
@ -30,6 +32,7 @@ public function homepageData(): array
|
||||
'about_image_url' => $aboutImage['url'] ?? null,
|
||||
'gallery_images' => $galleryImages,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function updateHomepage(array $validated): void
|
||||
@ -74,5 +77,8 @@ function () use ($validated): void {
|
||||
},
|
||||
'Gagal memperbarui pengaturan homepage',
|
||||
);
|
||||
|
||||
$this->cacheForget('homepage:settings');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,12 +2,16 @@
|
||||
|
||||
namespace App\Services\System\Setting;
|
||||
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Settings\HrSettings;
|
||||
|
||||
class HrSettingService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function hrData(): array
|
||||
{
|
||||
return $this->cacheRemember('system:hr_settings', 86400, function () {
|
||||
$settings = app(HrSettings::class);
|
||||
|
||||
return [
|
||||
@ -16,6 +20,7 @@ public function hrData(): array
|
||||
'late_penalty_amount' => $settings->late_penalty_amount,
|
||||
'absent_penalty_amount' => $settings->absent_penalty_amount,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function updateHr(array $validated): void
|
||||
@ -27,5 +32,7 @@ public function updateHr(array $validated): void
|
||||
$settings->late_penalty_amount = (int) $validated['late_penalty_amount'];
|
||||
$settings->absent_penalty_amount = (int) $validated['absent_penalty_amount'];
|
||||
$settings->save();
|
||||
|
||||
$this->cacheForget('system:hr_settings');
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
use App\Enums\Permission;
|
||||
use App\Models\OwnerVerificationRequest;
|
||||
use App\Models\User;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\System\PushNotificationService;
|
||||
use App\Settings\MarketplaceSettings;
|
||||
@ -17,7 +18,7 @@
|
||||
|
||||
class MarketplaceService
|
||||
{
|
||||
use RunsInTransaction;
|
||||
use CachesQuery, RunsInTransaction;
|
||||
|
||||
public function __construct(
|
||||
private readonly PushNotificationService $pushNotificationService,
|
||||
@ -52,6 +53,7 @@ private function shopeeFeeKeys(): array
|
||||
|
||||
public function marketplaceData(): array
|
||||
{
|
||||
return $this->cacheRemember('system:marketplace', 86400, function () {
|
||||
$settings = app(MarketplaceSettings::class);
|
||||
|
||||
return [
|
||||
@ -71,6 +73,7 @@ public function marketplaceData(): array
|
||||
'shopee_pre_order' => $this->presentFeeRule($settings->shopee_pre_order),
|
||||
'shopee_live_extra' => $this->presentFeeRule($settings->shopee_live_extra),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function updateMarketplace(array $validated, User $user): void
|
||||
@ -151,6 +154,8 @@ public function saveSettings(array $validated): void
|
||||
}
|
||||
|
||||
$settings->save();
|
||||
|
||||
$this->cacheForget('system:marketplace');
|
||||
}
|
||||
|
||||
public function applyVerificationRequest(OwnerVerificationRequest $request): void
|
||||
@ -159,6 +164,7 @@ public function applyVerificationRequest(OwnerVerificationRequest $request): voi
|
||||
function () use ($request): void {
|
||||
$newPayload = $request->payload['new'] ?? [];
|
||||
$this->saveSettings($newPayload);
|
||||
$this->cacheForget('system:marketplace');
|
||||
},
|
||||
'Gagal menerapkan pengajuan verifikasi owner',
|
||||
);
|
||||
|
||||
@ -2,12 +2,16 @@
|
||||
|
||||
namespace App\Services\System\Setting;
|
||||
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Settings\SocialMediaSettings;
|
||||
|
||||
class SocialMediaService
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function socialMediaData(): array
|
||||
{
|
||||
return $this->cacheRemember('system:social_media', 86400, function () {
|
||||
$settings = app(SocialMediaSettings::class);
|
||||
|
||||
return [
|
||||
@ -15,6 +19,7 @@ public function socialMediaData(): array
|
||||
'facebook_url' => $settings->facebook_url,
|
||||
'tiktok_url' => $settings->tiktok_url,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function updateSocialMedia(array $validated): void
|
||||
@ -26,5 +31,8 @@ public function updateSocialMedia(array $validated): void
|
||||
$settings->tiktok_url = $validated['tiktok_url'] ?? null;
|
||||
|
||||
$settings->save();
|
||||
|
||||
$this->cacheForget('system:social_media');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Services\System\Setting;
|
||||
|
||||
use App\Models\SystemConfiguration;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Concerns\RunsInTransaction;
|
||||
use App\Services\Concerns\SyncsPhotos;
|
||||
use App\Services\Media\MediaService;
|
||||
@ -12,7 +13,7 @@
|
||||
|
||||
class SystemService
|
||||
{
|
||||
use RunsInTransaction, SyncsPhotos;
|
||||
use CachesQuery, RunsInTransaction, SyncsPhotos;
|
||||
|
||||
public function __construct(
|
||||
private readonly MediaService $mediaService,
|
||||
@ -20,6 +21,7 @@ public function __construct(
|
||||
|
||||
public function systemData(): array
|
||||
{
|
||||
return $this->cacheRemember('system:settings', 86400, function () {
|
||||
$settings = app(SystemSettings::class);
|
||||
$configuration = SystemConfiguration::instance();
|
||||
$configuration->load('media');
|
||||
@ -38,6 +40,7 @@ public function systemData(): array
|
||||
'favicon_url' => $favicon['url'] ?? null,
|
||||
'login_cover_url' => $loginCover['url'] ?? null,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
public function updateSystem(array $validated): void
|
||||
@ -107,5 +110,8 @@ function () use ($validated): void {
|
||||
},
|
||||
'Gagal memperbarui pengaturan sistem',
|
||||
);
|
||||
|
||||
$this->cacheForget('system:settings');
|
||||
$this->cacheForget('homepage:page_data');
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,6 +17,7 @@
|
||||
"league/flysystem-aws-s3-v3": "^3.0",
|
||||
"league/flysystem-ftp": "^3.0",
|
||||
"minishlink/web-push": "^9.0",
|
||||
"predis/predis": "^3.5",
|
||||
"spatie/laravel-activitylog": "^5.0",
|
||||
"spatie/laravel-medialibrary": "^11.23",
|
||||
"spatie/laravel-permission": "^8.0",
|
||||
|
||||
65
composer.lock
generated
65
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "ea578c35bcf9ab3ac130534272e48937",
|
||||
"content-hash": "af3de95989c6c9eabd05600486ed22da",
|
||||
"packages": [
|
||||
{
|
||||
"name": "aws/aws-crt-php",
|
||||
@ -3502,6 +3502,69 @@
|
||||
},
|
||||
"time": "2026-01-25T14:56:51+00:00"
|
||||
},
|
||||
{
|
||||
"name": "predis/predis",
|
||||
"version": "v3.5.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/predis/predis.git",
|
||||
"reference": "5c996db191ee2d9bafe651f454b1fca16754271b"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/predis/predis/zipball/5c996db191ee2d9bafe651f454b1fca16754271b",
|
||||
"reference": "5c996db191ee2d9bafe651f454b1fca16754271b",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": "^7.2 || ^8.0",
|
||||
"psr/http-message": "^1.0|^2.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^3.3",
|
||||
"phpstan/phpstan": "^1.9",
|
||||
"phpunit/phpcov": "^6.0 || ^8.0",
|
||||
"phpunit/phpunit": "^8.0 || ~9.4.4"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-relay": "Faster connection with in-memory caching (>=0.6.2)"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Predis\\": "src/"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Till Krüss",
|
||||
"homepage": "https://till.im",
|
||||
"role": "Maintainer"
|
||||
}
|
||||
],
|
||||
"description": "A flexible and feature-complete Redis/Valkey client for PHP.",
|
||||
"homepage": "http://github.com/predis/predis",
|
||||
"keywords": [
|
||||
"nosql",
|
||||
"predis",
|
||||
"redis"
|
||||
],
|
||||
"support": {
|
||||
"issues": "https://github.com/predis/predis/issues",
|
||||
"source": "https://github.com/predis/predis/tree/v3.5.1"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://github.com/sponsors/tillkruss",
|
||||
"type": "github"
|
||||
}
|
||||
],
|
||||
"time": "2026-06-11T16:56:53+00:00"
|
||||
},
|
||||
{
|
||||
"name": "psr/clock",
|
||||
"version": "1.0.0",
|
||||
|
||||
@ -338,7 +338,6 @@
|
||||
->middleware('permission:'.Permission::CUTTINGS_COMPLETE->value.'|'.Permission::CUTTINGS_VERIFY->value.'|'.Permission::CUTTINGS_REJECT->value)
|
||||
->name('transition_status');
|
||||
|
||||
|
||||
Route::post('draft-materials', [CuttingDraftItemController::class, 'storeMaterial'])
|
||||
->middleware('permission:'.Permission::CUTTINGS_CREATE->value)
|
||||
->name('draft_materials.store');
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
use App\Enums\CuttingStatus;
|
||||
use App\Enums\Permission as PermissionEnum;
|
||||
use App\Models\Category;
|
||||
use App\Models\Cutting;
|
||||
use App\Models\CuttingMaterial;
|
||||
use App\Models\CuttingMaterialCombination;
|
||||
@ -761,7 +762,7 @@ function setupDraftItems(User $user): array
|
||||
test('quick create product with categories', function () {
|
||||
$user = createCuttingUserWithPermission(PermissionEnum::CUTTINGS_VIEW, PermissionEnum::CUTTINGS_CREATE);
|
||||
|
||||
$category = \App\Models\Category::factory()->create();
|
||||
$category = Category::factory()->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->postJson('/admin/manage/cuttings/quick-create-product', [
|
||||
@ -777,7 +778,7 @@ function setupDraftItems(User $user): array
|
||||
])
|
||||
->assertOk();
|
||||
|
||||
$product = \App\Models\Product::where('name', 'Product With Category')->first();
|
||||
$product = Product::where('name', 'Product With Category')->first();
|
||||
expect($product)->not->toBeNull();
|
||||
expect($product->categories)->toHaveCount(1);
|
||||
});
|
||||
|
||||
@ -102,7 +102,7 @@ function createCompletedCuttingSetup(): array
|
||||
'product_variant_id' => $setup['variant']->id,
|
||||
'good' => 6, // changed from 5 to 6
|
||||
'reject' => 1, // changed from 2 to 1
|
||||
]
|
||||
],
|
||||
],
|
||||
'result_prices' => [
|
||||
[
|
||||
@ -115,9 +115,9 @@ function createCompletedCuttingSetup(): array
|
||||
[
|
||||
'type' => 'retail',
|
||||
'price' => 75000,
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
@ -105,7 +105,7 @@ function createCatalogSetup(): array
|
||||
'stock_quality' => ProductStockQuality::REJECT->value,
|
||||
'physical_stock' => 3,
|
||||
'notes' => 'Kelebihan 1 reject',
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@ -150,7 +150,7 @@ function createCatalogSetup(): array
|
||||
'stock_quality' => ProductStockQuality::RETAIL->value,
|
||||
'physical_stock' => 6,
|
||||
'notes' => 'Tambah eceran',
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@ -198,7 +198,7 @@ function createCatalogSetup(): array
|
||||
'stock_quality' => ProductStockQuality::GOOD->value,
|
||||
'physical_stock' => 9,
|
||||
'notes' => 'Diubah jadi 9',
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
294
tests/Feature/CacheTest.php
Normal file
294
tests/Feature/CacheTest.php
Normal file
@ -0,0 +1,294 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Supplier;
|
||||
use App\Services\Concerns\CachesQuery;
|
||||
use App\Services\Master\CategoryService;
|
||||
use App\Services\Master\CustomerService;
|
||||
use App\Services\Master\SupplierService;
|
||||
use Database\Seeders\RolePermissionSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->seed(RolePermissionSeeder::class);
|
||||
});
|
||||
|
||||
// ─── CachesQuery Trait ─────────────────────────────────────
|
||||
|
||||
describe('CachesQuery Trait', function () {
|
||||
test('cacheRemember stores and retrieves value', function () {
|
||||
$service = new class
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function callRemember(): array
|
||||
{
|
||||
return $this->cacheRemember('test:key', 60, function () {
|
||||
return ['foo' => 'bar'];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$result = $service->callRemember();
|
||||
expect($result)->toBe(['foo' => 'bar']);
|
||||
expect(Cache::has('test:key'))->toBeTrue();
|
||||
});
|
||||
|
||||
test('cacheRemember returns cached value on second call', function () {
|
||||
$service = new class
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public int $callCount = 0;
|
||||
|
||||
public function callRemember(): array
|
||||
{
|
||||
return $this->cacheRemember('test:counter', 60, function () {
|
||||
$this->callCount++;
|
||||
|
||||
return ['count' => $this->callCount];
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$first = $service->callRemember();
|
||||
$second = $service->callRemember();
|
||||
|
||||
expect($first)->toBe(['count' => 1]);
|
||||
expect($second)->toBe(['count' => 1]);
|
||||
expect($service->callCount)->toBe(1);
|
||||
});
|
||||
|
||||
test('cacheForget removes cached value', function () {
|
||||
$service = new class
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function callRemember(): array
|
||||
{
|
||||
return $this->cacheRemember('test:forget', 60, function () {
|
||||
return ['data' => true];
|
||||
});
|
||||
}
|
||||
|
||||
public function callForget(): void
|
||||
{
|
||||
$this->cacheForget('test:forget');
|
||||
}
|
||||
};
|
||||
|
||||
$service->callRemember();
|
||||
expect(Cache::has('test:forget'))->toBeTrue();
|
||||
|
||||
$service->callForget();
|
||||
expect(Cache::has('test:forget'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('cacheForever stores value without expiration', function () {
|
||||
$service = new class
|
||||
{
|
||||
use CachesQuery;
|
||||
|
||||
public function callForever(): string
|
||||
{
|
||||
return $this->cacheForever('test:forever', function () {
|
||||
return 'permanent';
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
$result = $service->callForever();
|
||||
expect($result)->toBe('permanent');
|
||||
expect(Cache::has('test:forever'))->toBeTrue();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Category Cache ────────────────────────────────────────
|
||||
|
||||
describe('Category Caching', function () {
|
||||
test('getSelectOptions caches results', function () {
|
||||
Category::factory()->count(3)->create();
|
||||
|
||||
$service = app(CategoryService::class);
|
||||
|
||||
$first = $service->getSelectOptions();
|
||||
expect($first)->toHaveCount(3);
|
||||
expect(Cache::has('master:categories:options'))->toBeTrue();
|
||||
|
||||
Category::create(['name' => 'New Category']);
|
||||
|
||||
$second = $service->getSelectOptions();
|
||||
expect($second)->toHaveCount(3);
|
||||
|
||||
Cache::forget('master:categories:options');
|
||||
$third = $service->getSelectOptions();
|
||||
expect($third)->toHaveCount(4);
|
||||
});
|
||||
|
||||
test('create invalidates category cache', function () {
|
||||
$service = app(CategoryService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:categories:options'))->toBeTrue();
|
||||
|
||||
$service->create(['name' => 'Test']);
|
||||
expect(Cache::has('master:categories:options'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('update invalidates category cache', function () {
|
||||
$category = Category::factory()->create();
|
||||
$service = app(CategoryService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:categories:options'))->toBeTrue();
|
||||
|
||||
$service->update($category, ['name' => 'Updated']);
|
||||
expect(Cache::has('master:categories:options'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('delete invalidates category cache', function () {
|
||||
$category = Category::factory()->create();
|
||||
$service = app(CategoryService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:categories:options'))->toBeTrue();
|
||||
|
||||
$service->delete($category);
|
||||
expect(Cache::has('master:categories:options'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Supplier Cache ────────────────────────────────────────
|
||||
|
||||
describe('Supplier Caching', function () {
|
||||
test('getSelectOptions caches results', function () {
|
||||
Supplier::factory()->count(3)->create();
|
||||
|
||||
$service = app(SupplierService::class);
|
||||
|
||||
$first = $service->getSelectOptions();
|
||||
expect($first)->toHaveCount(3);
|
||||
expect(Cache::has('master:suppliers:options'))->toBeTrue();
|
||||
|
||||
Cache::forget('master:suppliers:options');
|
||||
$second = $service->getSelectOptions();
|
||||
expect($second)->toHaveCount(3);
|
||||
});
|
||||
|
||||
test('create invalidates supplier cache', function () {
|
||||
$service = app(SupplierService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:suppliers:options'))->toBeTrue();
|
||||
|
||||
$service->create(['name' => 'Test Supplier', 'phone_number' => '08123', 'address' => 'Jl. Test']);
|
||||
expect(Cache::has('master:suppliers:options'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('update invalidates supplier cache', function () {
|
||||
$supplier = Supplier::factory()->create();
|
||||
$service = app(SupplierService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:suppliers:options'))->toBeTrue();
|
||||
|
||||
$service->update($supplier, ['name' => 'Updated']);
|
||||
expect(Cache::has('master:suppliers:options'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('delete invalidates supplier cache', function () {
|
||||
$supplier = Supplier::factory()->create();
|
||||
$service = app(SupplierService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:suppliers:options'))->toBeTrue();
|
||||
|
||||
$service->delete($supplier);
|
||||
expect(Cache::has('master:suppliers:options'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Customer Cache ────────────────────────────────────────
|
||||
|
||||
describe('Customer Caching', function () {
|
||||
test('getSelectOptions caches results', function () {
|
||||
Customer::factory()->count(3)->create();
|
||||
|
||||
$service = app(CustomerService::class);
|
||||
|
||||
$first = $service->getSelectOptions();
|
||||
expect($first)->toHaveCount(3);
|
||||
expect(Cache::has('master:customers:options'))->toBeTrue();
|
||||
|
||||
Cache::forget('master:customers:options');
|
||||
$second = $service->getSelectOptions();
|
||||
expect($second)->toHaveCount(3);
|
||||
});
|
||||
|
||||
test('create invalidates customer cache', function () {
|
||||
$service = app(CustomerService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:customers:options'))->toBeTrue();
|
||||
|
||||
$service->create(['name' => 'Test Customer', 'phone_number' => '08123', 'address' => 'Jl. Test']);
|
||||
expect(Cache::has('master:customers:options'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('createAndReturn invalidates customer cache', function () {
|
||||
$service = app(CustomerService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:customers:options'))->toBeTrue();
|
||||
|
||||
$customer = $service->createAndReturn(['name' => 'Test', 'phone_number' => '08123', 'address' => 'Jl. Test']);
|
||||
expect($customer)->toBeInstanceOf(Customer::class);
|
||||
expect(Cache::has('master:customers:options'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('update invalidates customer cache', function () {
|
||||
$customer = Customer::factory()->create();
|
||||
$service = app(CustomerService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:customers:options'))->toBeTrue();
|
||||
|
||||
$service->update($customer, ['name' => 'Updated']);
|
||||
expect(Cache::has('master:customers:options'))->toBeFalse();
|
||||
});
|
||||
|
||||
test('delete invalidates customer cache', function () {
|
||||
$customer = Customer::factory()->create();
|
||||
$service = app(CustomerService::class);
|
||||
|
||||
$service->getSelectOptions();
|
||||
expect(Cache::has('master:customers:options'))->toBeTrue();
|
||||
|
||||
$service->delete($customer);
|
||||
expect(Cache::has('master:customers:options'))->toBeFalse();
|
||||
});
|
||||
});
|
||||
|
||||
// ─── Cache Key Isolation ───────────────────────────────────
|
||||
|
||||
describe('Cache Key Isolation', function () {
|
||||
test('different services use different cache keys', function () {
|
||||
Category::factory()->count(2)->create();
|
||||
Supplier::factory()->count(3)->create();
|
||||
|
||||
app(CategoryService::class)->getSelectOptions();
|
||||
app(SupplierService::class)->getSelectOptions();
|
||||
|
||||
expect(Cache::has('master:categories:options'))->toBeTrue();
|
||||
expect(Cache::has('master:suppliers:options'))->toBeTrue();
|
||||
|
||||
Cache::forget('master:categories:options');
|
||||
|
||||
expect(Cache::has('master:categories:options'))->toBeFalse();
|
||||
expect(Cache::has('master:suppliers:options'))->toBeTrue();
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user