feat: add CuttingCompletedSection and implement rejection and reset workflow for cutting status transitions
This commit is contained in:
parent
1de9d32136
commit
2dab1efeae
@ -33,7 +33,8 @@ public function canTransitionTo(self $status): bool
|
||||
{
|
||||
return match ($this) {
|
||||
self::IN_PROGRESS => $status === self::COMPLETED,
|
||||
self::COMPLETED => $status === self::VERIFIED,
|
||||
self::COMPLETED => in_array($status, [self::VERIFIED, self::REJECTED, self::IN_PROGRESS], true),
|
||||
self::REJECTED => $status === self::IN_PROGRESS,
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
@ -72,6 +73,29 @@ public function availableActions(): array
|
||||
'permission' => Permission::CUTTINGS_VERIFY->value,
|
||||
'icon_only' => false,
|
||||
],
|
||||
[
|
||||
'status' => self::REJECTED->value,
|
||||
'label' => 'Tolak',
|
||||
'destructive' => true,
|
||||
'permission' => Permission::CUTTINGS_REJECT->value,
|
||||
'icon_only' => false,
|
||||
],
|
||||
[
|
||||
'status' => self::IN_PROGRESS->value,
|
||||
'label' => 'Kembalikan ke Proses',
|
||||
'destructive' => true,
|
||||
'permission' => Permission::CUTTINGS_REJECT->value,
|
||||
'icon_only' => false,
|
||||
],
|
||||
],
|
||||
self::REJECTED => [
|
||||
[
|
||||
'status' => self::IN_PROGRESS->value,
|
||||
'label' => 'Kembalikan ke Proses',
|
||||
'destructive' => false,
|
||||
'permission' => Permission::CUTTINGS_UPDATE->value,
|
||||
'icon_only' => false,
|
||||
],
|
||||
],
|
||||
default => [],
|
||||
};
|
||||
|
||||
@ -31,6 +31,7 @@ public function index(Request $request): Response
|
||||
return Inertia::render('admin/manage/cuttings/Index', [
|
||||
'cuttings' => $this->cuttingService->paginateForIndex($tableQuery, $request->user()),
|
||||
'inProgressCuttings' => $this->cuttingService->getInProgressCuttings($request->user()),
|
||||
'completedCuttings' => $this->cuttingService->getCompletedCuttings($request->user()),
|
||||
'filters' => $this->dataTableFilters($tableQuery),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
namespace App\Http\Requests\Admin\Manage;
|
||||
|
||||
use App\Enums\CuttingStatus;
|
||||
use App\Enums\Permission;
|
||||
use App\Models\Cutting;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
@ -18,6 +19,12 @@ public function authorize(): bool
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($status === CuttingStatus::IN_PROGRESS) {
|
||||
return $this->user()?->can(Permission::CUTTINGS_REJECT->value)
|
||||
|| $this->user()?->can(Permission::CUTTINGS_UPDATE->value)
|
||||
|| false;
|
||||
}
|
||||
|
||||
return $this->user()?->can($status->transitionPermission()->value) ?? false;
|
||||
}
|
||||
|
||||
|
||||
@ -98,6 +98,33 @@ public function getInProgressCuttings(User $user): Collection
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @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',
|
||||
])
|
||||
->where('status', CuttingStatus::COMPLETED)
|
||||
->latest()
|
||||
->get()
|
||||
->each(function (Cutting $cutting) use ($user): void {
|
||||
$actions = collect($cutting->status->availableActions())
|
||||
->filter(fn (array $action) => $user->can($action['permission']))
|
||||
->values()
|
||||
->all();
|
||||
|
||||
$cutting->setAttribute('available_actions', $actions);
|
||||
$cutting->setAttribute('is_editable', $cutting->status->isEditable());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection<int, RawMaterial>
|
||||
*/
|
||||
@ -262,6 +289,8 @@ public function update(Cutting $cutting, array $validated): void
|
||||
|
||||
if ($cutting->status === CuttingStatus::IN_PROGRESS) {
|
||||
$this->reverseTotalMaterialStock($cutting);
|
||||
} elseif ($cutting->status === CuttingStatus::REJECTED) {
|
||||
$this->reverseMaterialStock($cutting);
|
||||
}
|
||||
|
||||
$cutting->materials()->delete();
|
||||
@ -271,6 +300,10 @@ public function update(Cutting $cutting, array $validated): void
|
||||
$results = $this->buildResults($validated['results']);
|
||||
|
||||
$cutting->description = $validated['description'] ?? null;
|
||||
if ($cutting->status === CuttingStatus::REJECTED) {
|
||||
$cutting->status = CuttingStatus::IN_PROGRESS;
|
||||
$cutting->rejection()?->delete();
|
||||
}
|
||||
$cutting->save();
|
||||
|
||||
foreach ($materials as $materialData) {
|
||||
@ -285,18 +318,14 @@ public function update(Cutting $cutting, array $validated): void
|
||||
$cutting->load('materials.rawMaterialPrice.rawMaterial');
|
||||
$this->deductMaterialStock($cutting);
|
||||
}
|
||||
|
||||
if ($cutting->status === CuttingStatus::REJECTED) {
|
||||
$cutting->rejection()?->delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function delete(Cutting $cutting): void
|
||||
{
|
||||
if ($cutting->status !== CuttingStatus::IN_PROGRESS) {
|
||||
if (! in_array($cutting->status, [CuttingStatus::IN_PROGRESS, CuttingStatus::REJECTED], true)) {
|
||||
throw ValidationException::withMessages([
|
||||
'status' => 'Proses potong hanya dapat dihapus saat masih proses.',
|
||||
'status' => 'Proses potong hanya dapat dihapus saat masih proses atau ditolak.',
|
||||
]);
|
||||
}
|
||||
|
||||
@ -304,7 +333,12 @@ public function delete(Cutting $cutting): void
|
||||
|
||||
DB::transaction(function () use ($cutting): void {
|
||||
$cutting->load(['materials.rawMaterialPrice.rawMaterial']);
|
||||
$this->reverseTotalMaterialStock($cutting);
|
||||
|
||||
if ($cutting->status === CuttingStatus::IN_PROGRESS) {
|
||||
$this->reverseTotalMaterialStock($cutting);
|
||||
} elseif ($cutting->status === CuttingStatus::REJECTED) {
|
||||
$this->reverseMaterialStock($cutting);
|
||||
}
|
||||
|
||||
$cutting->materials()->delete();
|
||||
$cutting->results()->delete();
|
||||
@ -333,13 +367,22 @@ public function transitionStatus(
|
||||
]);
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($cutting, $status, $verificationNote, $results, $user): void {
|
||||
DB::transaction(function () use ($cutting, $status, $verificationNote, $results, $user, $reason): void {
|
||||
$cutting->load(['materials', 'results']);
|
||||
|
||||
if ($status === CuttingStatus::COMPLETED) {
|
||||
$this->applyRemainingMaterialStock($cutting);
|
||||
}
|
||||
|
||||
if ($status === CuttingStatus::IN_PROGRESS) {
|
||||
$this->deductRemainingMaterialStock($cutting);
|
||||
$cutting->rejection()?->delete();
|
||||
}
|
||||
|
||||
if ($status === CuttingStatus::REJECTED) {
|
||||
$this->storeRejection($cutting, $reason, $user);
|
||||
}
|
||||
|
||||
if ($status === CuttingStatus::VERIFIED) {
|
||||
if ($results !== null) {
|
||||
foreach ($results as $item) {
|
||||
@ -528,6 +571,17 @@ private function applyRemainingMaterialStock(Cutting $cutting): void
|
||||
}
|
||||
}
|
||||
|
||||
private function deductRemainingMaterialStock(Cutting $cutting): void
|
||||
{
|
||||
foreach ($cutting->materials as $material) {
|
||||
if ((float) $material->remaining_material > 0) {
|
||||
RawMaterialPrice::query()
|
||||
->whereKey($material->raw_material_price_id)
|
||||
->decrement('stock', $material->remaining_material);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function reverseMaterialStock(Cutting $cutting): void
|
||||
{
|
||||
foreach ($cutting->materials as $material) {
|
||||
|
||||
@ -0,0 +1,118 @@
|
||||
<script setup lang="ts">
|
||||
import { Check, ChevronDown } from '@lucide/vue';
|
||||
import { computed, ref } from 'vue';
|
||||
import DataTableActions from '@/components/admin/manage/cuttings/data-table-actions.vue';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import {
|
||||
Collapsible,
|
||||
CollapsibleContent,
|
||||
CollapsibleTrigger,
|
||||
} from '@/components/ui/collapsible';
|
||||
import {
|
||||
Empty,
|
||||
EmptyDescription,
|
||||
EmptyHeader,
|
||||
EmptyTitle,
|
||||
} from '@/components/ui/empty';
|
||||
import type { CuttingListItem } from '@/types/cutting';
|
||||
|
||||
const props = defineProps<{
|
||||
cuttings: CuttingListItem[];
|
||||
}>();
|
||||
|
||||
const open = ref(false);
|
||||
|
||||
const totalCompleted = computed(() => props.cuttings.length);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Collapsible v-model:open="open">
|
||||
<Card class="!py-0">
|
||||
<CollapsibleTrigger
|
||||
class="flex w-full items-center justify-between gap-3 px-6 py-4 text-left transition-colors hover:bg-muted/30">
|
||||
<div class="min-w-0 space-y-1">
|
||||
<div class="flex items-center gap-2">
|
||||
<Check class="size-4 shrink-0 text-green-600 dark:text-green-400" />
|
||||
<h3 class="font-semibold leading-tight">
|
||||
Cutting Selesai (Menunggu Verifikasi)
|
||||
</h3>
|
||||
<Badge v-if="totalCompleted > 0" variant="outline" class="border-green-600/30 text-green-600 dark:text-green-400">
|
||||
{{ totalCompleted }}
|
||||
</Badge>
|
||||
</div>
|
||||
<p class="text-xs text-muted-foreground">
|
||||
Daftar cutting yang telah selesai dikerjakan. Gunakan tombol aksi untuk memverifikasi atau menolak.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<ChevronDown class="size-4 shrink-0 text-muted-foreground transition-transform duration-200"
|
||||
:class="open ? 'rotate-180' : ''" />
|
||||
</CollapsibleTrigger>
|
||||
|
||||
<CollapsibleContent>
|
||||
<CardContent class="pt-0 pb-6">
|
||||
<div v-if="cuttings.length" class="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3">
|
||||
<Card v-for="cutting in cuttings" :key="cutting.id" class="flex flex-col justify-between overflow-hidden border bg-card/50 py-0 gap-0">
|
||||
<div class="border-b bg-muted/20 px-4 py-3 flex items-center justify-between gap-2">
|
||||
<div class="min-w-0">
|
||||
<h4 class="font-medium text-sm truncate">
|
||||
Cutting #{{ cutting.id }}
|
||||
</h4>
|
||||
<span class="text-[10px] text-muted-foreground block truncate">
|
||||
{{ cutting.created_at_formatted }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex shrink-0 items-center">
|
||||
<DataTableActions :cutting="cutting" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="p-4 space-y-3 flex-1 text-xs">
|
||||
<div v-if="cutting.description" class="text-muted-foreground pb-2 border-b">
|
||||
<span class="font-medium text-foreground">Catatan:</span> {{ cutting.description }}
|
||||
</div>
|
||||
|
||||
<div class="space-y-1">
|
||||
<span class="font-medium text-foreground">Bahan Baku:</span>
|
||||
<ul class="list-disc pl-4 space-y-0.5 text-muted-foreground">
|
||||
<li v-for="mat in cutting.materials" :key="mat.id">
|
||||
{{ mat.raw_material_price?.raw_material?.name }} ({{ mat.raw_material_price?.variant }}) - {{ mat.material_usage_formatted }}
|
||||
</li>
|
||||
<li v-if="!cutting.materials.length">Belum ada bahan baku</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="space-y-1">
|
||||
<span class="font-medium text-foreground">Hasil Produk:</span>
|
||||
<ul class="list-disc pl-4 space-y-0.5 text-muted-foreground">
|
||||
<li v-for="res in cutting.results" :key="res.id">
|
||||
{{ res.product_variant?.product?.name }} ({{ res.product_variant?.name }}) - {{ res.cutting_result }} pcs
|
||||
</li>
|
||||
<li v-if="!cutting.results.length">Belum ada hasil produk</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="text-[11px] text-muted-foreground pt-2 border-t flex items-center justify-between">
|
||||
<span>Pembuat:</span>
|
||||
<span class="font-medium text-foreground">
|
||||
{{ cutting.created_by?.profile?.full_name ?? cutting.created_by?.username }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<Empty v-else class="py-8">
|
||||
<EmptyHeader>
|
||||
<EmptyTitle>Tidak ada cutting selesai</EmptyTitle>
|
||||
<EmptyDescription>
|
||||
Semua cutting selesai telah diverifikasi atau belum selesai diproses.
|
||||
</EmptyDescription>
|
||||
</EmptyHeader>
|
||||
</Empty>
|
||||
</CardContent>
|
||||
</CollapsibleContent>
|
||||
</Card>
|
||||
</Collapsible>
|
||||
</template>
|
||||
@ -82,7 +82,7 @@ const canEdit = computed(
|
||||
() => props.cutting.is_editable && can('cuttings.update'),
|
||||
);
|
||||
const canDelete = computed(
|
||||
() => can('cuttings.delete') && props.cutting.status === 'in_progress',
|
||||
() => can('cuttings.delete') && ['in_progress', 'rejected'].includes(props.cutting.status),
|
||||
);
|
||||
|
||||
function canPerformAction(action: CuttingStatusAction): boolean {
|
||||
|
||||
@ -4,6 +4,7 @@ import { Plus } from '@lucide/vue';
|
||||
import { computed, ref, watch } from 'vue';
|
||||
import CuttingGroupedTable from '@/components/admin/manage/cuttings/CuttingGroupedTable.vue';
|
||||
import CuttingInProgressSection from '@/components/admin/manage/cuttings/CuttingInProgressSection.vue';
|
||||
import CuttingCompletedSection from '@/components/admin/manage/cuttings/CuttingCompletedSection.vue';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent } from '@/components/ui/card';
|
||||
import { useCan } from '@/composables/useCan';
|
||||
@ -14,6 +15,7 @@ import type { PaginatedCuttings, CuttingListItem } from '@/types/cutting';
|
||||
const props = defineProps<{
|
||||
cuttings: PaginatedCuttings;
|
||||
inProgressCuttings: CuttingListItem[];
|
||||
completedCuttings: CuttingListItem[];
|
||||
filters: {
|
||||
search: string;
|
||||
sort?: string;
|
||||
@ -21,7 +23,7 @@ const props = defineProps<{
|
||||
};
|
||||
}>();
|
||||
|
||||
const { can } = useCan();
|
||||
const { can, hasAnyRole } = useCan();
|
||||
const search = ref(props.filters.search ?? '');
|
||||
|
||||
const { setSearch, resetFilters, syncFromServer } = useDataTableQuery({
|
||||
@ -75,7 +77,15 @@ watch(
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<CuttingInProgressSection :cuttings="inProgressCuttings" />
|
||||
<CuttingInProgressSection
|
||||
v-if="hasAnyRole(['admin-bahan-baku', 'owner', 'developer'])"
|
||||
:cuttings="inProgressCuttings"
|
||||
/>
|
||||
|
||||
<CuttingCompletedSection
|
||||
v-if="hasAnyRole(['admin-toko', 'owner', 'developer'])"
|
||||
:cuttings="completedCuttings"
|
||||
/>
|
||||
|
||||
<Card class="min-w-0">
|
||||
<CardContent class="min-w-0">
|
||||
|
||||
Loading…
Reference in New Issue
Block a user