429 lines
16 KiB
PHP
429 lines
16 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Manage;
|
|
|
|
use App\Enums\OwnerVerificationStatus;
|
|
use App\Models\OwnerVerificationRequest;
|
|
use App\Models\Product;
|
|
use App\Models\ProductVariant;
|
|
use App\Models\Purchase;
|
|
use App\Models\RawMaterial;
|
|
use App\Models\Restock;
|
|
use App\Models\User;
|
|
use App\Services\Concerns\CachesQuery;
|
|
use App\Services\Master\ProductService;
|
|
use App\Services\Master\RawMaterialService;
|
|
use App\Services\System\PushNotificationService;
|
|
use App\Services\System\Setting\MarketplaceService;
|
|
use App\Settings\MarketplaceSettings;
|
|
use App\Support\ActivityLog\ModelLabel;
|
|
use App\Support\OwnerVerification\VerificationChangeFormatter;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class OwnerVerificationService
|
|
{
|
|
use CachesQuery;
|
|
|
|
public function __construct(
|
|
private readonly ProductService $productService,
|
|
private readonly RawMaterialService $rawMaterialService,
|
|
private readonly PurchaseService $purchaseService,
|
|
private readonly RestockService $restockService,
|
|
private readonly PushNotificationService $pushNotificationService,
|
|
private readonly MarketplaceService $marketplaceService,
|
|
private readonly RetailStockService $retailStockService,
|
|
) {}
|
|
|
|
public function hasPendingMarketplaceVerification(): bool
|
|
{
|
|
return OwnerVerificationRequest::query()
|
|
->where('subject_type', MarketplaceSettings::class)
|
|
->pending()
|
|
->exists();
|
|
}
|
|
|
|
public function paginateForIndex(
|
|
User $user,
|
|
array $tableQuery,
|
|
string $status = '',
|
|
string $subjectType = '',
|
|
string $action = '',
|
|
): LengthAwarePaginator {
|
|
$perPage = 25;
|
|
|
|
$query = $this->buildVerificationRequestQuery($user, $tableQuery, $status, $subjectType, $action);
|
|
|
|
return $query
|
|
->paginate($perPage)
|
|
->withQueryString()
|
|
->through(fn (OwnerVerificationRequest $request) => $this->presentRequestRow($request));
|
|
}
|
|
|
|
public function pendingCountForUser(User $user): int
|
|
{
|
|
return $this->cacheRemember("verification:pending_count:{$user->id}", 30, function () use ($user) {
|
|
return OwnerVerificationRequest::query()
|
|
->pending()
|
|
->visibleTo($user)
|
|
->count();
|
|
});
|
|
}
|
|
|
|
public function subjectTypeOptions(User $user): array
|
|
{
|
|
return OwnerVerificationRequest::query()
|
|
->visibleTo($user)
|
|
->distinct()
|
|
->pluck('subject_type')
|
|
->filter()
|
|
->map(fn (string $type) => [
|
|
'value' => $type,
|
|
'label' => ModelLabel::for($type),
|
|
])
|
|
->unique('value')
|
|
->sortBy('label')
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
public function approveRequest(
|
|
OwnerVerificationRequest $request,
|
|
User $user,
|
|
?string $approvalNote = null,
|
|
): void {
|
|
if ($request->status !== OwnerVerificationStatus::PENDING) {
|
|
throw ValidationException::withMessages([
|
|
'status' => 'Hanya pengajuan yang menunggu verifikasi yang dapat disetujui.',
|
|
]);
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($request, $user, $approvalNote): void {
|
|
$this->applyVerificationRequest($request);
|
|
|
|
if ($approvalNote !== null && trim($approvalNote) !== '') {
|
|
$request->rejection()->create([
|
|
'reason' => trim($approvalNote),
|
|
'rejected_by_id' => $user->id,
|
|
]);
|
|
}
|
|
|
|
$request->update([
|
|
'status' => OwnerVerificationStatus::APPROVED,
|
|
'verified_by_id' => $user->id,
|
|
'verified_at' => now(),
|
|
]);
|
|
|
|
$this->clearVerificationRequestMedia($request);
|
|
});
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
Log::error('Gagal menyetujui verifikasi owner: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
throw ValidationException::withMessages([
|
|
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
|
]);
|
|
}
|
|
|
|
$subjectLabel = ModelLabel::for($request->subject_type);
|
|
|
|
$this->notifyRequestSubmitter(
|
|
$request,
|
|
'✅ Pengajuan Disetujui',
|
|
"Pengajuan {$request->action->label()} {$subjectLabel} '{$this->requestTitle($request)}' telah disetujui owner.",
|
|
);
|
|
|
|
$this->cacheForgetByPattern('verification:*');
|
|
}
|
|
|
|
public function rejectRequest(
|
|
OwnerVerificationRequest $request,
|
|
User $user,
|
|
string $reason,
|
|
): void {
|
|
if ($request->status !== OwnerVerificationStatus::PENDING) {
|
|
throw ValidationException::withMessages([
|
|
'status' => 'Hanya pengajuan yang menunggu verifikasi yang dapat ditolak.',
|
|
]);
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($request, $user, $reason): void {
|
|
$this->rejectVerificationRequest($request);
|
|
|
|
$request->rejection()->create([
|
|
'reason' => trim($reason),
|
|
'rejected_by_id' => $user->id,
|
|
]);
|
|
|
|
$request->update([
|
|
'status' => OwnerVerificationStatus::REJECTED,
|
|
'verified_by_id' => $user->id,
|
|
'verified_at' => now(),
|
|
]);
|
|
|
|
$this->clearVerificationRequestMedia($request);
|
|
});
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
Log::error('Gagal menolak verifikasi owner: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
throw ValidationException::withMessages([
|
|
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
|
]);
|
|
}
|
|
|
|
$title = $this->requestTitle($request);
|
|
$subjectLabel = ModelLabel::for($request->subject_type);
|
|
|
|
$this->pushNotificationService->sendToRoles(
|
|
'❌ Pengajuan Ditolak',
|
|
"Pengajuan {$request->action->label()} {$subjectLabel} '{$title}' ditolak oleh {$user->profile?->full_name} dengan alasan: '{$reason}'.",
|
|
['owner', 'developer', 'direktur'],
|
|
route('admin.dashboard'),
|
|
);
|
|
|
|
$this->notifyRequestSubmitter(
|
|
$request,
|
|
'❌ Pengajuan Ditolak',
|
|
"Pengajuan {$request->action->label()} {$subjectLabel} '{$title}' ditolak dengan alasan: '{$reason}'.",
|
|
);
|
|
|
|
$this->cacheForgetByPattern('verification:*');
|
|
}
|
|
|
|
private function rejectVerificationRequest(OwnerVerificationRequest $request): void
|
|
{
|
|
match ($request->subject_type) {
|
|
Product::class => $this->productService->rejectVerificationRequest($request),
|
|
RawMaterial::class => $this->rawMaterialService->rejectVerificationRequest($request),
|
|
Purchase::class => $this->purchaseService->rejectVerificationRequest($request),
|
|
Restock::class => $this->restockService->rejectVerificationRequest($request),
|
|
MarketplaceSettings::class => null,
|
|
default => throw ValidationException::withMessages([
|
|
'subject_type' => 'Tipe data verifikasi tidak didukung.',
|
|
]),
|
|
};
|
|
}
|
|
|
|
private function applyVerificationRequest(OwnerVerificationRequest $request): void
|
|
{
|
|
match ($request->subject_type) {
|
|
Product::class => $this->productService->applyVerificationRequest($request),
|
|
RawMaterial::class => $this->rawMaterialService->applyVerificationRequest($request),
|
|
Purchase::class => $this->purchaseService->applyVerificationRequest($request),
|
|
Restock::class => $this->restockService->applyVerificationRequest($request),
|
|
MarketplaceSettings::class => $this->marketplaceService->applyVerificationRequest($request),
|
|
default => throw ValidationException::withMessages([
|
|
'subject_type' => 'Tipe data verifikasi tidak didukung.',
|
|
]),
|
|
};
|
|
}
|
|
|
|
private function clearVerificationRequestMedia(OwnerVerificationRequest $request): void
|
|
{
|
|
match ($request->subject_type) {
|
|
Product::class => $this->productService->clearVerificationRequestMedia($request),
|
|
RawMaterial::class => $this->rawMaterialService->clearVerificationRequestMedia($request),
|
|
Purchase::class => $this->purchaseService->clearVerificationRequestMedia($request),
|
|
Restock::class => $this->restockService->clearVerificationRequestMedia($request),
|
|
default => null,
|
|
};
|
|
}
|
|
|
|
private function presentRequestRow(OwnerVerificationRequest $request): array
|
|
{
|
|
$payload = is_array($request->payload) ? $request->payload : [];
|
|
|
|
return [
|
|
'id' => $request->id,
|
|
'source' => 'request',
|
|
'subject_type' => $request->subject_type,
|
|
'subject_label' => ModelLabel::for($request->subject_type),
|
|
'subject_id' => $request->subject_id,
|
|
'action' => $request->action->value,
|
|
'action_label' => $request->action->label(),
|
|
'status' => $request->status->value,
|
|
'status_label' => $request->status->label(),
|
|
'title' => $this->requestTitle($request),
|
|
'submitted_by_name' => $request->submittedBy?->profile?->full_name
|
|
?? $request->submittedBy?->username
|
|
?? '-',
|
|
'verified_by_name' => $request->verifiedBy?->profile?->full_name
|
|
?? $request->verifiedBy?->username,
|
|
'verified_at_formatted' => $request->verified_at?->translatedFormat('l, d F Y H:i'),
|
|
'created_at' => $request->created_at?->toIso8601String(),
|
|
'created_at_formatted' => $request->created_at?->translatedFormat('l, d F Y H:i'),
|
|
'changes' => VerificationChangeFormatter::format($payload),
|
|
'rejection_reason' => $request->rejection?->reason,
|
|
'is_pending' => $request->status === OwnerVerificationStatus::PENDING,
|
|
];
|
|
}
|
|
|
|
private function buildVerificationRequestQuery(
|
|
User $user,
|
|
array $tableQuery,
|
|
string $status,
|
|
string $subjectType,
|
|
string $action,
|
|
): Builder {
|
|
$query = OwnerVerificationRequest::query()
|
|
->visibleTo($user)
|
|
->with([
|
|
'subject',
|
|
'submittedBy.profile',
|
|
'verifiedBy.profile',
|
|
'rejection',
|
|
])
|
|
->when($tableQuery['search'] !== '', function (Builder $query) use ($tableQuery): void {
|
|
$search = $tableQuery['search'];
|
|
$query->where(function (Builder $query) use ($search): void {
|
|
$query->where('action', 'like', "%{$search}%")
|
|
->orWhere('status', 'like', "%{$search}%")
|
|
->orWhereHas('submittedBy', function (Builder $query) use ($search): void {
|
|
$query->where('username', 'like', "%{$search}%")
|
|
->orWhereHas('profile', fn (Builder $query) => $query->where('full_name', 'like', "%{$search}%"));
|
|
})
|
|
->orWhereHasMorph('subject', [Product::class, RawMaterial::class, Purchase::class, ProductVariant::class], function (Builder $query, string $type) use ($search): void {
|
|
if ($type === Purchase::class) {
|
|
$query->where('notes', 'like', "%{$search}%")
|
|
->orWhereHas('supplier', fn (Builder $query) => $query->where('name', 'like', "%{$search}%"));
|
|
|
|
return;
|
|
}
|
|
|
|
$query->where('name', 'like', "%{$search}%");
|
|
});
|
|
});
|
|
})
|
|
->when($status !== '', fn (Builder $query) => $query->where('status', $status))
|
|
->when($subjectType !== '', fn (Builder $query) => $query->where('subject_type', $subjectType))
|
|
->when($action !== '', fn (Builder $query) => $query->where('action', $action));
|
|
|
|
$this->applySorting($query, $tableQuery['sort'], $tableQuery['direction']);
|
|
|
|
return $query;
|
|
}
|
|
|
|
private function requestTitle(OwnerVerificationRequest $request): string
|
|
{
|
|
if ($request->subject_type === MarketplaceSettings::class) {
|
|
return 'Pengaturan Marketplace';
|
|
}
|
|
|
|
if ($request->subject instanceof Product || $request->subject instanceof RawMaterial) {
|
|
return $request->subject->name;
|
|
}
|
|
|
|
if ($request->subject instanceof Purchase) {
|
|
$request->subject->loadMissing('supplier');
|
|
|
|
return $request->subject->supplier?->name ?? 'Belanja';
|
|
}
|
|
|
|
if ($request->subject instanceof ProductVariant) {
|
|
$payload = is_array($request->payload) ? $request->payload : [];
|
|
$newData = $payload['new'] ?? [];
|
|
|
|
return ($newData['product_name'] ?? 'Produk').' - '.($newData['variant_name'] ?? $request->subject->name);
|
|
}
|
|
|
|
$payload = is_array($request->payload) ? $request->payload : [];
|
|
|
|
foreach (['new', 'old'] as $key) {
|
|
$section = $payload[$key] ?? null;
|
|
|
|
if (is_array($section) && isset($section['supplier_name'])) {
|
|
return (string) $section['supplier_name'];
|
|
}
|
|
}
|
|
|
|
foreach (['new', 'old'] as $key) {
|
|
$section = $payload[$key] ?? null;
|
|
|
|
if (is_array($section) && isset($section['name'])) {
|
|
return (string) $section['name'];
|
|
}
|
|
}
|
|
|
|
if (isset($payload['name'])) {
|
|
return (string) $payload['name'];
|
|
}
|
|
|
|
return 'Item Baru';
|
|
}
|
|
|
|
private function requestSearchTerm(OwnerVerificationRequest $request): ?string
|
|
{
|
|
if ($request->subject_type === MarketplaceSettings::class) {
|
|
return null;
|
|
}
|
|
|
|
if ($request->subject_type === ProductVariant::class) {
|
|
if ($request->subject instanceof ProductVariant) {
|
|
return (string) $request->subject->product_id;
|
|
}
|
|
$payload = is_array($request->payload) ? $request->payload : [];
|
|
$newData = $payload['new'] ?? [];
|
|
if (isset($newData['product_id'])) {
|
|
return (string) $newData['product_id'];
|
|
}
|
|
}
|
|
|
|
if ($request->subject_id !== null) {
|
|
return (string) $request->subject_id;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private function notifyRequestSubmitter(
|
|
OwnerVerificationRequest $request,
|
|
string $title,
|
|
string $body,
|
|
): void {
|
|
if (! $request->submitted_by_id) {
|
|
return;
|
|
}
|
|
|
|
$searchTerm = $this->requestSearchTerm($request);
|
|
$routeParams = $searchTerm !== null ? ['search_id' => $searchTerm] : [];
|
|
|
|
$url = match ($request->subject_type) {
|
|
Product::class, ProductVariant::class => route('admin.master.products.index', $routeParams),
|
|
RawMaterial::class => route('admin.master.raw_materials.index', $routeParams),
|
|
Purchase::class => route('admin.manage.purchases.index', $routeParams),
|
|
MarketplaceSettings::class => route('admin.system.settings.index'),
|
|
default => route('admin.dashboard'),
|
|
};
|
|
|
|
$this->pushNotificationService->sendToUser(
|
|
$title,
|
|
$body,
|
|
$request->submitted_by_id,
|
|
$url,
|
|
);
|
|
}
|
|
|
|
private function applySorting(Builder $query, string $sort, string $direction): void
|
|
{
|
|
if (in_array($sort, ['created_at', 'action', 'status'], true)) {
|
|
$query->orderBy($sort, $direction);
|
|
|
|
return;
|
|
}
|
|
|
|
$query->latest();
|
|
}
|
|
}
|