Skip to content

Commit 36b2c19

Browse files
feat(new-purchase-order-form): integrate product selection and total calculation in purchase order form
1 parent 20402a3 commit 36b2c19

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

resources/js/components/forms/purchase-order/new-purchase-order-form.tsx

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import InputError from '@/components/feedback/input-error';
22
import CalendarDatePicker from '@/components/helpers/calendar-date-picker';
33
import PartnerSelectCombobox from '@/components/helpers/partners/partner-select-combobox';
4+
import ItemsForPurchaseOrder, {
5+
type SelectedProduct,
6+
} from '@/components/helpers/purchase-order/items-for-purchase-order';
47
import { Button } from '@/components/ui/button';
58
import { ButtonGroup } from '@/components/ui/button-group';
69
import { Field, FieldGroup, FieldLabel } from '@/components/ui/field';
710
import { Spinner } from '@/components/ui/spinner';
811
import { Textarea } from '@/components/ui/textarea';
12+
import { formatCurrency } from '@/lib/utils';
913
import erp from '@/routes/erp';
1014
import { Form, Link } from '@inertiajs/react';
1115
import { PlusCircleIcon } from 'lucide-react';
1216
import { Activity, useState } from 'react';
1317

1418
export default function NewPurchaseOrderForm() {
1519
const [supplierId, setSupplierId] = useState<number | null>(null);
16-
const [productIds, setProductIds] = useState<number[]>([]);
20+
const [selectedProducts, setSelectedProducts] = useState<SelectedProduct[]>([]);
1721
const [orderDate, setOrderDate] = useState<Date | null>(null);
1822
const [forecastDate, setForecastDate] = useState<Date | null>(null);
1923

@@ -26,7 +30,10 @@ export default function NewPurchaseOrderForm() {
2630
transform={(data) => ({
2731
...data,
2832
supplier_id: supplierId,
29-
product_ids: productIds,
33+
products: selectedProducts.map((product) => ({
34+
id: product.id,
35+
quantity: product.quantity,
36+
})),
3037
order_date: orderDate ? orderDate.toISOString().split('T')[0] : null,
3138
forecast_date: forecastDate ? forecastDate.toISOString().split('T')[0] : null,
3239
})}
@@ -74,14 +81,31 @@ export default function NewPurchaseOrderForm() {
7481
</Field>
7582
</FieldGroup>
7683

77-
{/* TODO: Insert product selection and item order list here. */}
78-
7984
<Field aria-invalid={!!errors.notes}>
8085
<FieldLabel htmlFor="notes">Notes</FieldLabel>
8186
<Textarea id="notes" name="notes" rows={4} maxLength={255} />
8287
<InputError message={errors.notes} />
8388
</Field>
8489

90+
<ItemsForPurchaseOrder
91+
value={selectedProducts}
92+
setValue={setSelectedProducts}
93+
route={erp.purchaseOrders.create()}
94+
/>
95+
96+
<p className="text-right text-3xl">
97+
Total:{' '}
98+
<span className="font-mono font-semibold">
99+
{formatCurrency(
100+
selectedProducts.reduce(
101+
(total, product) =>
102+
total + Number(product.selling_price) * Number(product.quantity),
103+
0,
104+
),
105+
)}
106+
</span>
107+
</p>
108+
85109
<ButtonGroup>
86110
<Button type="button" variant="outline" disabled={processing} asChild>
87111
<Link href={erp.payables.index()} prefetch="hover" as="button">
@@ -98,7 +122,13 @@ export default function NewPurchaseOrderForm() {
98122
<Button
99123
type="button"
100124
variant="outline"
101-
onClick={() => resetAndClearErrors()}
125+
onClick={() => {
126+
resetAndClearErrors();
127+
setSupplierId(null);
128+
setSelectedProducts([]);
129+
setOrderDate(null);
130+
setForecastDate(null);
131+
}}
102132
disabled={processing}
103133
>
104134
Reset

0 commit comments

Comments
 (0)