diff --git a/app/Http/Requests/Admin/Manage/TransactionRequest.php b/app/Http/Requests/Admin/Manage/TransactionRequest.php index 86b33b1..ad3b924 100644 --- a/app/Http/Requests/Admin/Manage/TransactionRequest.php +++ b/app/Http/Requests/Admin/Manage/TransactionRequest.php @@ -35,7 +35,7 @@ public function rules(): array 'customer_id' => ['nullable', 'integer', Rule::exists('customers', 'id')], 'marketing_id' => ['nullable', 'integer', Rule::exists('users', 'id')], 'discount' => ['nullable', 'integer', 'min:0'], - 'nego_price' => ['nullable', 'integer'], + 'nego_price' => ['nullable', 'integer', 'min:0'], 'is_completed' => ['sometimes', 'boolean'], 'tiktok_order_id' => ['nullable', 'string', 'max:100'], 'shopee_order_id' => ['nullable', 'string', 'max:100'], diff --git a/app/Services/Admin/Manage/TransactionService.php b/app/Services/Admin/Manage/TransactionService.php index 31b5bff..cfc5a43 100644 --- a/app/Services/Admin/Manage/TransactionService.php +++ b/app/Services/Admin/Manage/TransactionService.php @@ -210,8 +210,10 @@ public function store(array $data): Order $itemRows = $this->buildItemRows($data['items'], $stockType, $priceType, $now, $subtotal, $totalCost); $discount = (int) ($data['discount'] ?? 0); - $negoPrice = ! empty($data['nego_price']) ? (int) $data['nego_price'] : null; - $totalAmount = $subtotal - $discount - ($negoPrice ?? 0); + $priceBeforeNego = $subtotal - $discount; + $negoFinalPrice = ! empty($data['nego_price']) ? (int) $data['nego_price'] : null; + $totalAmount = $negoFinalPrice ?? $priceBeforeNego; + $negoPrice = $negoFinalPrice !== null ? $priceBeforeNego - $negoFinalPrice : null; if ($totalAmount <= 0) { throw ValidationException::withMessages([ @@ -219,6 +221,12 @@ public function store(array $data): Order ]); } + if ($negoPrice !== null && $negoPrice < 0) { + throw ValidationException::withMessages([ + 'nego_price' => 'Harga nego tidak boleh lebih besar dari harga sebelum nego.', + ]); + } + $order = Order::create([ 'created_by_id' => auth()->id(), 'customer_id' => $data['customer_id'] ?? null, @@ -294,8 +302,10 @@ public function update(Order $order, array $data): Order $itemRows = $this->buildItemRows($data['items'], $stockType, $priceType, $now, $subtotal, $totalCost); $discount = (int) ($data['discount'] ?? 0); - $negoPrice = ! empty($data['nego_price']) ? (int) $data['nego_price'] : null; - $totalAmount = $subtotal - $discount - ($negoPrice ?? 0); + $priceBeforeNego = $subtotal - $discount; + $negoFinalPrice = ! empty($data['nego_price']) ? (int) $data['nego_price'] : null; + $totalAmount = $negoFinalPrice ?? $priceBeforeNego; + $negoPrice = $negoFinalPrice !== null ? $priceBeforeNego - $negoFinalPrice : null; if ($totalAmount <= 0) { throw ValidationException::withMessages([ @@ -303,6 +313,12 @@ public function update(Order $order, array $data): Order ]); } + if ($negoPrice !== null && $negoPrice < 0) { + throw ValidationException::withMessages([ + 'nego_price' => 'Harga nego tidak boleh lebih besar dari harga sebelum nego.', + ]); + } + foreach ($itemRows as &$row) { $row['order_id'] = $order->id; } diff --git a/resources/js/pages/admin/manage/transaction/create.tsx b/resources/js/pages/admin/manage/transaction/create.tsx index f8a2c22..f80c56f 100644 --- a/resources/js/pages/admin/manage/transaction/create.tsx +++ b/resources/js/pages/admin/manage/transaction/create.tsx @@ -291,7 +291,7 @@ export default function TransactionCreate({ 0, ); - const total = subtotal - discount - (negoPrice ?? 0); + const total = negoPrice ?? (subtotal - discount); const updateQuantity = useCallback((variantId: number, value: number) => { setQuantities((prev) => ({ @@ -980,7 +980,7 @@ export default function TransactionCreate({ placeholder="0" /> - Jumlah potongan harga yang dinego customer. + Harga akhir setelah dinego dengan customer. (transaction.customer_id); const [marketingId, setMarketingId] = useState(transaction.marketing_id); const [discount, setDiscount] = useState(transaction.discount); - const [negoPrice, setNegoPrice] = useState(transaction.nego_price); + const initialSubtotal = (transaction.items ?? []).reduce( + (sum, item) => sum + item.unit_price * item.quantity, + 0, + ); + const [negoPrice, setNegoPrice] = useState( + transaction.nego_price != null + ? initialSubtotal - transaction.discount - transaction.nego_price + : null, + ); const [isCompleted, setIsCompleted] = useState(transaction.is_completed); const [notes, setNotes] = useState(transaction.notes ?? ''); const [photo, setPhoto] = useState(transaction.photo_key); @@ -268,7 +276,7 @@ export default function TransactionEdit({ 0, ); - const total = subtotal - discount - (negoPrice ?? 0); + const total = negoPrice ?? (subtotal - discount); const updateQuantity = useCallback((variantId: number, value: number) => { setQuantities((prev) => ({ @@ -889,7 +897,7 @@ export default function TransactionEdit({ placeholder="0" /> - Jumlah potongan harga yang dinego customer. + Harga akhir setelah dinego dengan customer.