feat: enforce minimum value for nego_price and update total calculation logic
This commit is contained in:
parent
c162ccab04
commit
dc97b9bc61
@ -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'],
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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"
|
||||
/>
|
||||
<FieldDescription>
|
||||
Jumlah potongan harga yang dinego customer.
|
||||
Harga akhir setelah dinego dengan customer.
|
||||
</FieldDescription>
|
||||
<InputError
|
||||
message={errors.nego_price}
|
||||
|
||||
@ -92,7 +92,15 @@ export default function TransactionEdit({
|
||||
const [customerId, setCustomerId] = useState<number | null>(transaction.customer_id);
|
||||
const [marketingId, setMarketingId] = useState<number | null>(transaction.marketing_id);
|
||||
const [discount, setDiscount] = useState(transaction.discount);
|
||||
const [negoPrice, setNegoPrice] = useState<number | null>(transaction.nego_price);
|
||||
const initialSubtotal = (transaction.items ?? []).reduce(
|
||||
(sum, item) => sum + item.unit_price * item.quantity,
|
||||
0,
|
||||
);
|
||||
const [negoPrice, setNegoPrice] = useState<number | null>(
|
||||
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<string | null>(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"
|
||||
/>
|
||||
<FieldDescription>
|
||||
Jumlah potongan harga yang dinego customer.
|
||||
Harga akhir setelah dinego dengan customer.
|
||||
</FieldDescription>
|
||||
<InputError
|
||||
message={errors.nego_price}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user