1042 lines
39 KiB
PHP
1042 lines
39 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Manage;
|
|
|
|
use App\Enums\CuttingStatus;
|
|
use App\Enums\Permission;
|
|
use App\Models\Cutting;
|
|
use App\Models\CuttingMaterial;
|
|
use App\Models\CuttingResult;
|
|
use App\Models\CuttingResultPrice;
|
|
use App\Models\Product;
|
|
use App\Models\ProductVariant;
|
|
use App\Models\RawMaterial;
|
|
use App\Models\RawMaterialPrice;
|
|
use App\Models\User;
|
|
use App\Services\System\PushNotificationService;
|
|
use App\Support\Media\MediaPresenter;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class CuttingService
|
|
{
|
|
public function __construct(
|
|
private readonly PushNotificationService $pushNotificationService,
|
|
) {}
|
|
|
|
public function isEditable(CuttingStatus $status): bool
|
|
{
|
|
return $status->isEditable();
|
|
}
|
|
|
|
public function ensureEditable(Cutting $cutting): void
|
|
{
|
|
if (! $this->isEditable($cutting->status)) {
|
|
throw ValidationException::withMessages([
|
|
'status' => 'Proses cutting tidak dapat diubah.',
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function transitionStatusMessage(CuttingStatus $status): string
|
|
{
|
|
return match ($status) {
|
|
CuttingStatus::COMPLETED => 'Proses cutting berhasil diselesaikan. Menunggu verifikasi admin toko.',
|
|
CuttingStatus::VERIFIED => 'Proses cutting berhasil diverifikasi. Stok produk telah diperbarui.',
|
|
CuttingStatus::REJECTED => 'Proses cutting berhasil ditolak.',
|
|
CuttingStatus::IN_PROGRESS => 'Proses cutting dikembalikan ke proses.',
|
|
default => 'Status proses cutting berhasil diperbarui.',
|
|
};
|
|
}
|
|
|
|
public function canTransitionTo(CuttingStatus $from, CuttingStatus $to): bool
|
|
{
|
|
return $from->canTransitionTo($to);
|
|
}
|
|
|
|
public function transitionPermission(CuttingStatus $status): Permission
|
|
{
|
|
return $status->transitionPermission();
|
|
}
|
|
|
|
/**
|
|
* @return list<array{status: string, label: string, destructive: bool, permission: string, icon_only: bool}>
|
|
*/
|
|
public function availableActions(CuttingStatus $status): array
|
|
{
|
|
return $status->availableActions();
|
|
}
|
|
|
|
/**
|
|
* @param array{search: string, sort: string, direction: 'asc'|'desc'} $tableQuery
|
|
*/
|
|
public function paginateForIndex(array $tableQuery, User $user): LengthAwarePaginator
|
|
{
|
|
$query = Cutting::query()
|
|
->with([
|
|
'createdBy.profile',
|
|
'rejection.rejectedBy.profile',
|
|
'materials.rawMaterialPrice.rawMaterial:id,name,unit',
|
|
'results.productVariant.product:id,name',
|
|
'results.productVariant:id,product_id,name',
|
|
])
|
|
->when($tableQuery['search'] !== '', function (Builder $query) use ($tableQuery): void {
|
|
$search = $tableQuery['search'];
|
|
$query->where(function (Builder $query) use ($search): void {
|
|
$query->where('description', 'like', "%{$search}%")
|
|
->orWhereHas('materials.rawMaterialPrice', function (Builder $query) use ($search): void {
|
|
$query->where('variant', 'like', "%{$search}%")
|
|
->orWhereHas('rawMaterial', fn (Builder $query) => $query->where('name', 'like', "%{$search}%"));
|
|
})
|
|
->orWhereHas('results.productVariant', function (Builder $query) use ($search): void {
|
|
$query->where('name', 'like', "%{$search}%")
|
|
->orWhereHas('product', fn (Builder $query) => $query->where('name', 'like', "%{$search}%"));
|
|
});
|
|
});
|
|
});
|
|
|
|
$this->applySorting($query, $tableQuery['sort'], $tableQuery['direction']);
|
|
|
|
return $query
|
|
->paginate(10)
|
|
->withQueryString()
|
|
->through(function (Cutting $cutting) use ($user) {
|
|
$actions = collect($this->availableActions($cutting->status))
|
|
->filter(fn (array $action) => $user->can($action['permission']))
|
|
->values()
|
|
->all();
|
|
|
|
$cutting->setAttribute('available_actions', $actions);
|
|
$cutting->setAttribute('is_editable', $this->isEditable($cutting->status));
|
|
$this->appendCostPreview($cutting);
|
|
|
|
return $cutting;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Cutting>
|
|
*/
|
|
public function getInProgressCuttings(User $user): Collection
|
|
{
|
|
return Cutting::query()
|
|
->with([
|
|
'createdBy.profile',
|
|
'rejection.rejectedBy.profile',
|
|
'materials.rawMaterialPrice.rawMaterial:id,name,unit',
|
|
'results.productVariant.product:id,name',
|
|
'results.productVariant:id,product_id,name',
|
|
])
|
|
->inProgress()
|
|
->latest()
|
|
->get()
|
|
->each(function (Cutting $cutting) use ($user): void {
|
|
$actions = collect($this->availableActions($cutting->status))
|
|
->filter(fn (array $action) => $user->can($action['permission']))
|
|
->values()
|
|
->all();
|
|
|
|
$cutting->setAttribute('available_actions', $actions);
|
|
$cutting->setAttribute('is_editable', $this->isEditable($cutting->status));
|
|
$this->appendCostPreview($cutting);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Cutting>
|
|
*/
|
|
public function getCompletedCuttings(User $user): Collection
|
|
{
|
|
return Cutting::query()
|
|
->with([
|
|
'createdBy.profile',
|
|
'rejection.rejectedBy.profile',
|
|
'materials.rawMaterialPrice.rawMaterial:id,name,unit',
|
|
'results.productVariant.product:id,name',
|
|
'results.productVariant:id,product_id,name',
|
|
])
|
|
->completed()
|
|
->latest()
|
|
->get()
|
|
->each(function (Cutting $cutting) use ($user): void {
|
|
$actions = collect($this->availableActions($cutting->status))
|
|
->filter(fn (array $action) => $user->can($action['permission']))
|
|
->values()
|
|
->all();
|
|
|
|
$cutting->setAttribute('available_actions', $actions);
|
|
$cutting->setAttribute('is_editable', $this->isEditable($cutting->status));
|
|
$this->appendCostPreview($cutting);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, RawMaterial>
|
|
*/
|
|
public function rawMaterialCatalog(?Cutting $cutting = null, ?User $user = null): Collection
|
|
{
|
|
$selectedPriceIds = $cutting
|
|
? $cutting->materials()->pluck('raw_material_price_id')->all()
|
|
: ($user ? $this->draftMaterialsQuery($user)->pluck('raw_material_price_id')->all() : []);
|
|
|
|
return RawMaterial::query()
|
|
->with([
|
|
'prices' => fn ($query) => $query
|
|
->orderBy('created_at')
|
|
->with(['rawMaterial:id,unit', 'media']),
|
|
])
|
|
->where(function (Builder $query) use ($selectedPriceIds): void {
|
|
$query->active();
|
|
|
|
if ($selectedPriceIds !== []) {
|
|
$query->orWhereHas(
|
|
'prices',
|
|
fn (Builder $query) => $query->whereIn('id', $selectedPriceIds),
|
|
);
|
|
}
|
|
})
|
|
->orderBy('name')
|
|
->get()
|
|
->each(function (RawMaterial $rawMaterial): void {
|
|
$rawMaterial->prices->each(function (RawMaterialPrice $price): void {
|
|
$price->setAttribute(
|
|
'images',
|
|
MediaPresenter::collection($price, 'images'),
|
|
);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Product>
|
|
*/
|
|
public function productCatalog(?Cutting $cutting = null, ?User $user = null): Collection
|
|
{
|
|
$selectedVariantIds = $cutting
|
|
? $cutting->results()->pluck('product_variant_id')->all()
|
|
: ($user ? $this->draftResultsQuery($user)->pluck('product_variant_id')->all() : []);
|
|
|
|
return Product::query()
|
|
->with([
|
|
'variants' => fn ($query) => $query
|
|
->with('media')
|
|
->orderBy('created_at'),
|
|
])
|
|
->where(function (Builder $query) use ($selectedVariantIds): void {
|
|
$query->active();
|
|
|
|
if ($selectedVariantIds !== []) {
|
|
$query->orWhereHas(
|
|
'variants',
|
|
fn (Builder $query) => $query->whereIn('id', $selectedVariantIds),
|
|
);
|
|
}
|
|
})
|
|
->orderBy('name')
|
|
->get()
|
|
->each(function (Product $product): void {
|
|
$product->variants->each(function (ProductVariant $variant): void {
|
|
$variant->setAttribute(
|
|
'images',
|
|
MediaPresenter::collection($variant, 'images'),
|
|
);
|
|
});
|
|
});
|
|
}
|
|
|
|
public function findForEdit(Cutting $cutting): Cutting
|
|
{
|
|
$cutting->load([
|
|
'materials.rawMaterialPrice.rawMaterial:id,name,unit',
|
|
'materials.rawMaterialPrice.media',
|
|
'results.productVariant.product:id,name',
|
|
'results.productVariant.media',
|
|
'rejection.rejectedBy.profile',
|
|
]);
|
|
|
|
$cutting->materials->each(function (CuttingMaterial $material): void {
|
|
$price = $material->rawMaterialPrice;
|
|
|
|
if ($price) {
|
|
$price->setAttribute(
|
|
'images',
|
|
MediaPresenter::collection($price, 'images'),
|
|
);
|
|
}
|
|
});
|
|
|
|
$cutting->results->each(function (CuttingResult $result): void {
|
|
$variant = $result->productVariant;
|
|
|
|
if ($variant) {
|
|
$variant->setAttribute(
|
|
'images',
|
|
MediaPresenter::collection($variant, 'images'),
|
|
);
|
|
}
|
|
});
|
|
|
|
return $cutting;
|
|
}
|
|
|
|
/**
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function draftMaterialsForUser(User $user): array
|
|
{
|
|
return $this->draftMaterialsQuery($user)
|
|
->with([
|
|
'rawMaterialPrice.rawMaterial:id,name,unit',
|
|
'rawMaterialPrice.media',
|
|
])
|
|
->get()
|
|
->map(fn (CuttingMaterial $item) => $this->presentDraftMaterial($item))
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function draftResultsForUser(User $user): array
|
|
{
|
|
return $this->draftResultsQuery($user)
|
|
->with([
|
|
'productVariant.product:id,name',
|
|
'productVariant.media',
|
|
])
|
|
->get()
|
|
->map(fn (CuttingResult $item) => $this->presentDraftResult($item))
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function syncDraftMaterial(array $validated, User $user): array
|
|
{
|
|
$price = RawMaterialPrice::query()
|
|
->with('rawMaterial')
|
|
->findOrFail($validated['raw_material_price_id']);
|
|
|
|
$materialUsage = round((float) $validated['material_usage'], 2);
|
|
|
|
if ($materialUsage <= 0) {
|
|
throw ValidationException::withMessages([
|
|
'material_usage' => 'Pemakaian bahan harus lebih dari 0.',
|
|
]);
|
|
}
|
|
|
|
$item = CuttingMaterial::query()->updateOrCreate(
|
|
[
|
|
'user_id' => $user->id,
|
|
'raw_material_price_id' => $price->id,
|
|
'cutting_id' => null,
|
|
],
|
|
[
|
|
'material_usage' => $materialUsage,
|
|
],
|
|
);
|
|
|
|
$item->load([
|
|
'rawMaterialPrice.rawMaterial:id,name,unit',
|
|
'rawMaterialPrice.media',
|
|
]);
|
|
|
|
return $this->presentDraftMaterial($item);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function syncDraftResult(array $validated, User $user): array
|
|
{
|
|
$variant = ProductVariant::query()
|
|
->with('product:id,name')
|
|
->findOrFail($validated['product_variant_id']);
|
|
|
|
$cuttingResult = (int) $validated['cutting_result'];
|
|
$sampel = (int) $validated['sampel'];
|
|
$hasilCuttingDiluarSampel = (int) ($validated['hasil_cutting_diluar_sampel'] ?? 0);
|
|
|
|
if ($cuttingResult < 1) {
|
|
throw ValidationException::withMessages([
|
|
'cutting_result' => 'Hasil cutting minimal 1 pcs.',
|
|
]);
|
|
}
|
|
|
|
if ($sampel < 0) {
|
|
throw ValidationException::withMessages([
|
|
'sampel' => 'Sampel tidak boleh negatif.',
|
|
]);
|
|
}
|
|
|
|
if ($hasilCuttingDiluarSampel < 0) {
|
|
throw ValidationException::withMessages([
|
|
'hasil_cutting_diluar_sampel' => 'Hasil cutting diluar sampel tidak boleh negatif.',
|
|
]);
|
|
}
|
|
|
|
if (($sampel + $hasilCuttingDiluarSampel) !== $cuttingResult) {
|
|
throw ValidationException::withMessages([
|
|
'cutting_result' => 'Hasil cutting harus sama dengan sampel ditambah hasil cutting diluar sampel.',
|
|
]);
|
|
}
|
|
|
|
$item = CuttingResult::query()->updateOrCreate(
|
|
[
|
|
'user_id' => $user->id,
|
|
'product_variant_id' => $variant->id,
|
|
'cutting_id' => null,
|
|
],
|
|
[
|
|
'cutting_result' => $cuttingResult,
|
|
'sampel' => $sampel,
|
|
'hasil_cutting_diluar_sampel' => $hasilCuttingDiluarSampel,
|
|
],
|
|
);
|
|
|
|
$item->load([
|
|
'productVariant.product:id,name',
|
|
'productVariant.media',
|
|
]);
|
|
|
|
return $this->presentDraftResult($item);
|
|
}
|
|
|
|
public function removeDraftMaterial(User $user, RawMaterialPrice $rawMaterialPrice): void
|
|
{
|
|
CuttingMaterial::query()
|
|
->whereNull('cutting_id')
|
|
->where('user_id', $user->id)
|
|
->where('raw_material_price_id', $rawMaterialPrice->id)
|
|
->delete();
|
|
}
|
|
|
|
public function removeDraftResult(User $user, ProductVariant $productVariant): void
|
|
{
|
|
CuttingResult::query()
|
|
->whereNull('cutting_id')
|
|
->where('user_id', $user->id)
|
|
->where('product_variant_id', $productVariant->id)
|
|
->delete();
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
*/
|
|
public function create(array $validated, User $user): Cutting
|
|
{
|
|
try {
|
|
$cutting = DB::transaction(function () use ($validated, $user): Cutting {
|
|
/** @var EloquentCollection<int, CuttingMaterial> $draftMaterials */
|
|
$draftMaterials = $this->draftMaterialsQuery($user)
|
|
->with('rawMaterialPrice.rawMaterial')
|
|
->lockForUpdate()
|
|
->get();
|
|
|
|
/** @var EloquentCollection<int, CuttingResult> $draftResults */
|
|
$draftResults = $this->draftResultsQuery($user)
|
|
->lockForUpdate()
|
|
->get();
|
|
|
|
if ($draftMaterials->isEmpty()) {
|
|
throw ValidationException::withMessages([
|
|
'materials' => 'Tambahkan minimal satu bahan baku.',
|
|
]);
|
|
}
|
|
|
|
if ($draftResults->isEmpty()) {
|
|
throw ValidationException::withMessages([
|
|
'results' => 'Tambahkan minimal satu hasil produk.',
|
|
]);
|
|
}
|
|
|
|
$cutting = Cutting::create([
|
|
'status' => CuttingStatus::IN_PROGRESS,
|
|
'description' => $validated['description'] ?? null,
|
|
'sewing_cost' => (int) ($validated['sewing_cost'] ?? 0),
|
|
'other_cost' => (int) ($validated['other_cost'] ?? 0),
|
|
'created_by_id' => $user->id,
|
|
]);
|
|
|
|
foreach ($draftMaterials as $material) {
|
|
$material->cutting_id = $cutting->id;
|
|
$material->user_id = null;
|
|
$material->save();
|
|
}
|
|
|
|
foreach ($draftResults as $result) {
|
|
$result->cutting_id = $cutting->id;
|
|
$result->user_id = null;
|
|
$result->save();
|
|
}
|
|
|
|
$cutting->load('materials.rawMaterialPrice.rawMaterial');
|
|
$this->deductMaterialStock($cutting);
|
|
|
|
return $cutting;
|
|
});
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
Log::error('Gagal membuat proses cutting: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
throw ValidationException::withMessages([
|
|
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
|
]);
|
|
}
|
|
|
|
$this->pushNotificationService->sendToRoles(
|
|
'✂️ Proses Cutting Baru',
|
|
"Proses cutting dengan deskripsi ({$cutting->description}) telah dimulai oleh {$user->profile?->full_name}.",
|
|
['owner', 'developer'],
|
|
route('admin.manage.cuttings.index'),
|
|
);
|
|
|
|
return $cutting;
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $validated
|
|
*/
|
|
public function update(Cutting $cutting, array $validated): void
|
|
{
|
|
if (! $this->isEditable($cutting->status)) {
|
|
throw ValidationException::withMessages([
|
|
'status' => 'Proses cutting tidak dapat diubah.',
|
|
]);
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($cutting, $validated): void {
|
|
$cutting->load(['materials.rawMaterialPrice.rawMaterial', 'results']);
|
|
|
|
if ($cutting->status === CuttingStatus::IN_PROGRESS) {
|
|
$this->reverseTotalMaterialStock($cutting);
|
|
} elseif ($cutting->status === CuttingStatus::REJECTED) {
|
|
$this->reverseMaterialStock($cutting);
|
|
}
|
|
|
|
$cutting->materials()->delete();
|
|
$cutting->results()->delete();
|
|
|
|
$materials = $this->buildMaterials($validated['materials']);
|
|
$results = $this->buildResults($validated['results']);
|
|
|
|
$cutting->description = $validated['description'] ?? null;
|
|
$cutting->sewing_cost = (int) ($validated['sewing_cost'] ?? 0);
|
|
$cutting->other_cost = (int) ($validated['other_cost'] ?? 0);
|
|
if ($cutting->status === CuttingStatus::REJECTED) {
|
|
$cutting->status = CuttingStatus::IN_PROGRESS;
|
|
$cutting->rejection()?->delete();
|
|
}
|
|
$cutting->save();
|
|
|
|
foreach ($materials as $materialData) {
|
|
$cutting->materials()->create($materialData);
|
|
}
|
|
|
|
foreach ($results as $resultData) {
|
|
$cutting->results()->create($resultData);
|
|
}
|
|
|
|
if ($cutting->status === CuttingStatus::IN_PROGRESS) {
|
|
$cutting->load('materials.rawMaterialPrice.rawMaterial');
|
|
$this->deductMaterialStock($cutting);
|
|
}
|
|
});
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
Log::error('Gagal memperbarui proses cutting: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
throw ValidationException::withMessages([
|
|
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
|
]);
|
|
}
|
|
|
|
$description = $cutting->description ?? '-';
|
|
$this->pushNotificationService->sendToRoles(
|
|
'✏️ Proses Cutting Diperbarui',
|
|
"Proses cutting dengan deskripsi '{$description}' telah diperbarui.",
|
|
['owner', 'developer'],
|
|
route('admin.manage.cuttings.index'),
|
|
);
|
|
}
|
|
|
|
public function delete(Cutting $cutting): void
|
|
{
|
|
if (! in_array($cutting->status, [CuttingStatus::IN_PROGRESS, CuttingStatus::REJECTED], true)) {
|
|
throw ValidationException::withMessages([
|
|
'status' => 'Proses cutting hanya dapat dihapus saat masih proses atau ditolak.',
|
|
]);
|
|
}
|
|
|
|
$description = $cutting->description ?? '-';
|
|
|
|
try {
|
|
DB::transaction(function () use ($cutting): void {
|
|
$cutting->load(['materials.rawMaterialPrice.rawMaterial']);
|
|
|
|
if ($cutting->status === CuttingStatus::IN_PROGRESS) {
|
|
$this->reverseTotalMaterialStock($cutting);
|
|
} elseif ($cutting->status === CuttingStatus::REJECTED) {
|
|
$this->reverseMaterialStock($cutting);
|
|
}
|
|
|
|
$cutting->materials()->delete();
|
|
$cutting->results()->delete();
|
|
$cutting->delete();
|
|
});
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
Log::error('Gagal menghapus proses cutting: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
throw ValidationException::withMessages([
|
|
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
|
]);
|
|
}
|
|
|
|
$this->pushNotificationService->sendToRoles(
|
|
'🗑️ Proses Cutting Dihapus',
|
|
"Proses cutting dengan deskripsi {$description} telah dihapus.",
|
|
['owner', 'developer'],
|
|
route('admin.manage.cuttings.index'),
|
|
);
|
|
}
|
|
|
|
public function transitionStatus(
|
|
Cutting $cutting,
|
|
CuttingStatus $status,
|
|
User $user,
|
|
?string $reason = null,
|
|
?string $verificationNote = null,
|
|
?array $results = null,
|
|
?array $resultPrices = null,
|
|
): void {
|
|
if (! $this->canTransitionTo($cutting->status, $status)) {
|
|
throw ValidationException::withMessages([
|
|
'status' => 'Status proses cutting tidak dapat diubah.',
|
|
]);
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($cutting, $status, $verificationNote, $results, $resultPrices, $user, $reason): void {
|
|
$cutting->load(['materials.rawMaterialPrice', 'results']);
|
|
|
|
if ($status === CuttingStatus::COMPLETED) {
|
|
$cutting->total_material_cost = $this->calculateTotalMaterialCost($cutting);
|
|
$cutting->cost_per_unit = $this->calculateCostPerUnit($cutting);
|
|
}
|
|
|
|
if ($status === CuttingStatus::IN_PROGRESS) {
|
|
$cutting->rejection()?->delete();
|
|
}
|
|
|
|
if ($status === CuttingStatus::REJECTED) {
|
|
$this->storeRejection($cutting, $reason, $user);
|
|
}
|
|
|
|
if ($status === CuttingStatus::VERIFIED) {
|
|
if ($results !== null) {
|
|
foreach ($results as $item) {
|
|
$cutting->results()
|
|
->where('product_variant_id', $item['product_variant_id'])
|
|
->update([
|
|
'sampel' => $item['sampel'],
|
|
'hasil_cutting_diluar_sampel' => $item['hasil_cutting_diluar_sampel'],
|
|
]);
|
|
}
|
|
$cutting->load('results');
|
|
}
|
|
$this->applyProductStockOnVerify($cutting);
|
|
$this->storeResultPrices($cutting, $resultPrices ?? []);
|
|
|
|
if ($verificationNote !== null && trim($verificationNote) !== '') {
|
|
$cutting->rejection()->create([
|
|
'reason' => trim($verificationNote),
|
|
'rejected_by_id' => $user->id,
|
|
]);
|
|
}
|
|
}
|
|
|
|
$cutting->status = $status;
|
|
$cutting->save();
|
|
});
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (\Throwable $e) {
|
|
Log::error('Gagal mengubah status proses cutting: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
throw ValidationException::withMessages([
|
|
'system' => 'Terjadi kesalahan pada server. Silakan laporkan masalah ini ke pihak terkait.',
|
|
]);
|
|
}
|
|
|
|
$description = $cutting->description ?? '-';
|
|
$message = match ($status) {
|
|
CuttingStatus::COMPLETED => "Proses cutting dengan deskripsi '{$description}' telah selesai dan menunggu verifikasi.",
|
|
CuttingStatus::VERIFIED => "Proses cutting dengan deskripsi '{$description}' telah diverifikasi.",
|
|
CuttingStatus::REJECTED => "Proses cutting dengan deskripsi '{$description}' ditolak".($reason ? " dengan alasan: '{$reason}'" : '').'.',
|
|
CuttingStatus::IN_PROGRESS => "Proses cutting dengan deskripsi '{$description}' dikembalikan ke proses.",
|
|
default => "Status proses cutting dengan deskripsi '{$description}' telah diperbarui ke: {$status->label()}.",
|
|
};
|
|
|
|
$title = match ($status) {
|
|
CuttingStatus::COMPLETED => '✂️ Proses Cutting Selesai',
|
|
CuttingStatus::VERIFIED => '✂️ Proses Cutting Terverifikasi',
|
|
CuttingStatus::REJECTED => '✂️ Proses Cutting Ditolak',
|
|
CuttingStatus::IN_PROGRESS => '✂️ Proses Cutting Dikembalikan',
|
|
default => '✂️ Proses Cutting Diperbarui',
|
|
};
|
|
|
|
$roles = $status === CuttingStatus::COMPLETED
|
|
? ['owner', 'developer', 'admin-toko']
|
|
: ['owner', 'developer'];
|
|
|
|
$this->pushNotificationService->sendToRoles(
|
|
$title,
|
|
$message,
|
|
$roles,
|
|
route('admin.manage.cuttings.index'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param list<array{raw_material_price_id: int, material_usage: float|int|string}> $materials
|
|
* @return list<array{raw_material_price_id: int, material_usage: float}>
|
|
*/
|
|
private function buildMaterials(array $materials): array
|
|
{
|
|
return collect($materials)
|
|
->map(function (array $itemData, int $index) {
|
|
$price = RawMaterialPrice::query()
|
|
->with('rawMaterial')
|
|
->find($itemData['raw_material_price_id']);
|
|
|
|
if ($price === null) {
|
|
throw ValidationException::withMessages([
|
|
"materials.{$index}.raw_material_price_id" => 'Bahan baku tidak ditemukan.',
|
|
]);
|
|
}
|
|
|
|
$materialUsage = round((float) $itemData['material_usage'], 2);
|
|
|
|
if ($materialUsage <= 0) {
|
|
throw ValidationException::withMessages([
|
|
"materials.{$index}.material_usage" => 'Pemakaian bahan harus lebih dari 0.',
|
|
]);
|
|
}
|
|
|
|
return [
|
|
'raw_material_price_id' => $price->id,
|
|
'material_usage' => $materialUsage,
|
|
];
|
|
})
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @param list<array{product_variant_id: int, cutting_result: int|string, sampel: int|string, hasil_cutting_diluar_sampel?: int|string}> $results
|
|
* @return list<array{product_variant_id: int, cutting_result: int, sampel: int, hasil_cutting_diluar_sampel: int}>
|
|
*/
|
|
private function buildResults(array $results): array
|
|
{
|
|
return collect($results)
|
|
->map(function (array $itemData, int $index) {
|
|
$variant = ProductVariant::query()->find($itemData['product_variant_id']);
|
|
|
|
if ($variant === null) {
|
|
throw ValidationException::withMessages([
|
|
"results.{$index}.product_variant_id" => 'Varian produk tidak ditemukan.',
|
|
]);
|
|
}
|
|
|
|
$cuttingResult = (int) $itemData['cutting_result'];
|
|
$sampel = (int) $itemData['sampel'];
|
|
$hasilCuttingDiluarSampel = (int) ($itemData['hasil_cutting_diluar_sampel'] ?? 0);
|
|
|
|
if ($cuttingResult < 1) {
|
|
throw ValidationException::withMessages([
|
|
"results.{$index}.cutting_result" => 'Hasil cutting minimal 1 pcs.',
|
|
]);
|
|
}
|
|
|
|
if ($sampel < 0) {
|
|
throw ValidationException::withMessages([
|
|
"results.{$index}.sampel" => 'Sampel tidak boleh negatif.',
|
|
]);
|
|
}
|
|
|
|
if ($hasilCuttingDiluarSampel < 0) {
|
|
throw ValidationException::withMessages([
|
|
"results.{$index}.hasil_cutting_diluar_sampel" => 'Hasil cutting diluar sampel tidak boleh negatif.',
|
|
]);
|
|
}
|
|
|
|
if (($sampel + $hasilCuttingDiluarSampel) !== $cuttingResult) {
|
|
throw ValidationException::withMessages([
|
|
"results.{$index}.cutting_result" => 'Hasil cutting harus sama dengan sampel ditambah hasil cutting diluar sampel.',
|
|
]);
|
|
}
|
|
|
|
return [
|
|
'product_variant_id' => $variant->id,
|
|
'cutting_result' => $cuttingResult,
|
|
'sampel' => $sampel,
|
|
'hasil_cutting_diluar_sampel' => $hasilCuttingDiluarSampel,
|
|
];
|
|
})
|
|
->all();
|
|
}
|
|
|
|
private function deductMaterialStock(Cutting $cutting): void
|
|
{
|
|
foreach ($cutting->materials as $material) {
|
|
$totalTaken = (float) $material->material_usage;
|
|
$price = RawMaterialPrice::query()->lockForUpdate()->find($material->raw_material_price_id);
|
|
|
|
if ($price === null) {
|
|
throw ValidationException::withMessages([
|
|
'materials' => 'Bahan baku tidak ditemukan.',
|
|
]);
|
|
}
|
|
|
|
if ((float) $price->stock < $totalTaken) {
|
|
throw ValidationException::withMessages([
|
|
'materials' => "Stok {$price->rawMaterial?->name} ({$price->variant}) tidak mencukupi.",
|
|
]);
|
|
}
|
|
|
|
$price->decrement('stock', $totalTaken);
|
|
}
|
|
}
|
|
|
|
private function reverseTotalMaterialStock(Cutting $cutting): void
|
|
{
|
|
foreach ($cutting->materials as $material) {
|
|
RawMaterialPrice::query()
|
|
->whereKey($material->raw_material_price_id)
|
|
->increment('stock', (float) $material->material_usage);
|
|
}
|
|
}
|
|
|
|
private function reverseMaterialStock(Cutting $cutting): void
|
|
{
|
|
foreach ($cutting->materials as $material) {
|
|
RawMaterialPrice::query()
|
|
->whereKey($material->raw_material_price_id)
|
|
->increment('stock', (float) $material->material_usage);
|
|
}
|
|
}
|
|
|
|
private function applyProductStockOnVerify(Cutting $cutting): void
|
|
{
|
|
foreach ($cutting->results as $result) {
|
|
if ($result->sampel > 0) {
|
|
ProductVariant::query()
|
|
->whereKey($result->product_variant_id)
|
|
->increment('stock', $result->sampel);
|
|
}
|
|
|
|
if ($result->hasil_cutting_diluar_sampel > 0) {
|
|
ProductVariant::query()
|
|
->whereKey($result->product_variant_id)
|
|
->increment('reject_stock', $result->hasil_cutting_diluar_sampel);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function reverseProductStock(Cutting $cutting): void
|
|
{
|
|
foreach ($cutting->results as $result) {
|
|
if ($result->sampel > 0) {
|
|
ProductVariant::query()
|
|
->whereKey($result->product_variant_id)
|
|
->decrement('stock', $result->sampel);
|
|
}
|
|
|
|
if ($result->hasil_cutting_diluar_sampel > 0) {
|
|
ProductVariant::query()
|
|
->whereKey($result->product_variant_id)
|
|
->decrement('reject_stock', $result->hasil_cutting_diluar_sampel);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function storeRejection(Cutting $cutting, ?string $reason, User $user): void
|
|
{
|
|
if ($reason === null || trim($reason) === '') {
|
|
throw ValidationException::withMessages([
|
|
'reason' => 'Alasan penolakan wajib diisi.',
|
|
]);
|
|
}
|
|
|
|
$cutting->rejection()?->delete();
|
|
|
|
$cutting->rejection()->create([
|
|
'reason' => trim($reason),
|
|
'rejected_by_id' => $user->id,
|
|
]);
|
|
}
|
|
|
|
private function applySorting(Builder $query, string $sort, string $direction): void
|
|
{
|
|
if (in_array($sort, ['created_at', 'status'], true)) {
|
|
$query->orderBy($sort, $direction);
|
|
|
|
return;
|
|
}
|
|
|
|
$query->latest();
|
|
}
|
|
|
|
/**
|
|
* @return Builder<CuttingMaterial>
|
|
*/
|
|
private function draftMaterialsQuery(User $user): Builder
|
|
{
|
|
return CuttingMaterial::query()
|
|
->whereNull('cutting_id')
|
|
->where('user_id', $user->id);
|
|
}
|
|
|
|
/**
|
|
* @return Builder<CuttingResult>
|
|
*/
|
|
private function draftResultsQuery(User $user): Builder
|
|
{
|
|
return CuttingResult::query()
|
|
->whereNull('cutting_id')
|
|
->where('user_id', $user->id);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function presentDraftMaterial(CuttingMaterial $item): array
|
|
{
|
|
$price = $item->rawMaterialPrice;
|
|
$rawMaterial = $price?->rawMaterial;
|
|
|
|
return [
|
|
'raw_material_price_id' => $item->raw_material_price_id,
|
|
'raw_material_name' => $rawMaterial?->name ?? '',
|
|
'variant' => $price?->variant ?? '',
|
|
'unit' => $rawMaterial?->unit?->value ?? '',
|
|
'unit_abbreviation' => $rawMaterial?->unit?->abbreviation() ?? '',
|
|
'stock_input' => $price?->stock_input ?? '',
|
|
'material_usage' => $this->formatQuantityInput((float) $item->material_usage),
|
|
'images' => $price ? MediaPresenter::collection($price, 'images') : [],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function presentDraftResult(CuttingResult $item): array
|
|
{
|
|
$variant = $item->productVariant;
|
|
|
|
return [
|
|
'product_variant_id' => $item->product_variant_id,
|
|
'product_name' => $variant?->product?->name ?? '',
|
|
'variant_name' => $variant?->name ?? '',
|
|
'stock' => $variant?->stock ?? 0,
|
|
'cutting_result' => (string) $item->cutting_result,
|
|
'sampel' => (string) $item->sampel,
|
|
'hasil_cutting_diluar_sampel' => (string) $item->hasil_cutting_diluar_sampel,
|
|
'images' => $variant ? MediaPresenter::collection($variant, 'images') : [],
|
|
];
|
|
}
|
|
|
|
private function formatQuantityInput(float $value): string
|
|
{
|
|
return rtrim(rtrim(number_format($value, 2, '.', ''), '0'), '.');
|
|
}
|
|
|
|
private function appendCostPreview(Cutting $cutting): void
|
|
{
|
|
$cutting->setAttribute('total_result_pieces', (int) $cutting->results->sum('cutting_result'));
|
|
$cutting->setAttribute('total_material_usage', (float) $cutting->materials->sum(fn (CuttingMaterial $material) => (float) $material->material_usage));
|
|
|
|
$totalMaterialCost = $cutting->total_material_cost ?? $this->calculateTotalMaterialCost($cutting);
|
|
$sewingCost = (int) ($cutting->sewing_cost ?? 0);
|
|
$otherCost = (int) ($cutting->other_cost ?? 0);
|
|
$totalProductionCost = $totalMaterialCost + $sewingCost + $otherCost;
|
|
$costPerUnit = $cutting->cost_per_unit ?? $this->calculateCostPerUnit($cutting);
|
|
|
|
$cutting->setAttribute('total_material_cost', $totalMaterialCost);
|
|
$cutting->setAttribute('total_material_cost_formatted', 'Rp '.number_format($totalMaterialCost, 0, ',', '.'));
|
|
$cutting->setAttribute('sewing_cost', $sewingCost);
|
|
$cutting->setAttribute('sewing_cost_formatted', 'Rp '.number_format($sewingCost, 0, ',', '.'));
|
|
$cutting->setAttribute('other_cost', $otherCost);
|
|
$cutting->setAttribute('other_cost_formatted', 'Rp '.number_format($otherCost, 0, ',', '.'));
|
|
$cutting->setAttribute('total_production_cost', $totalProductionCost);
|
|
$cutting->setAttribute('total_production_cost_formatted', 'Rp '.number_format($totalProductionCost, 0, ',', '.'));
|
|
$cutting->setAttribute('estimated_cost_per_unit', $costPerUnit);
|
|
$cutting->setAttribute('estimated_cost_per_unit_formatted', 'Rp '.number_format($costPerUnit, 0, ',', '.'));
|
|
}
|
|
|
|
public function calculateTotalMaterialCost(Cutting $cutting): int
|
|
{
|
|
return (int) $cutting->materials->sum(fn (CuttingMaterial $material) => $material->materialCost());
|
|
}
|
|
|
|
public function calculateTotalProductionCost(Cutting $cutting): int
|
|
{
|
|
return $this->calculateTotalMaterialCost($cutting)
|
|
+ (int) ($cutting->sewing_cost ?? 0)
|
|
+ (int) ($cutting->other_cost ?? 0);
|
|
}
|
|
|
|
public function calculateCostPerUnit(Cutting $cutting): int
|
|
{
|
|
$totalPieces = (int) $cutting->results->sum('cutting_result');
|
|
|
|
if ($totalPieces <= 0) {
|
|
return 0;
|
|
}
|
|
|
|
return (int) round($this->calculateTotalProductionCost($cutting) / $totalPieces);
|
|
}
|
|
|
|
/**
|
|
* @param list<array{product_variant_id: int, prices: list<array{type: string, price: int}>}> $resultPrices
|
|
*/
|
|
private function storeResultPrices(Cutting $cutting, array $resultPrices): void
|
|
{
|
|
$costPerUnit = (int) ($cutting->cost_per_unit ?? $this->calculateCostPerUnit($cutting));
|
|
|
|
foreach ($resultPrices as $resultData) {
|
|
foreach ($resultData['prices'] as $priceData) {
|
|
CuttingResultPrice::query()->create([
|
|
'cutting_id' => $cutting->id,
|
|
'product_variant_id' => $resultData['product_variant_id'],
|
|
'price_type' => $priceData['type'],
|
|
'price' => (int) $priceData['price'],
|
|
'cost_per_unit' => $costPerUnit,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
}
|