diff --git a/src/components/pages/marketing/form/MarketingForm.schema.ts b/src/components/pages/marketing/form/MarketingForm.schema.ts index 144ec6ff..3d7eac5c 100644 --- a/src/components/pages/marketing/form/MarketingForm.schema.ts +++ b/src/components/pages/marketing/form/MarketingForm.schema.ts @@ -194,6 +194,19 @@ export const mergeSOwithDO = ( const delivery = deliveryOrders.find( (d) => d?.marketing_product_id === so.id ); + const isTelurQty = + so.marketing_type?.value?.toLowerCase() === 'telur' && + so.convertion_unit?.value?.toLowerCase() === 'qty'; + const salesOrderUnitPrice = + isTelurQty && Number(so.total_price || 0) > 0 && Number(so.qty || 0) > 0 + ? Number(so.total_price) / Number(so.qty) + : so.unit_price; + const salesOrderPricePerQty = + isTelurQty && + Number(so.total_price || 0) > 0 && + Number(so.total_weight || 0) > 0 + ? Number(so.total_price) / Number(so.total_weight) + : so.price_per_qty; return { ...so, // nilai dasar dari sales order @@ -201,7 +214,7 @@ export const mergeSOwithDO = ( delivery_date: delivery?.delivery_date || undefined, do_number: delivery?.do_number || undefined, vehicle_number: delivery?.vehicle_number || so.vehicle_number, - unit_price: autofill ? so.unit_price : delivery?.unit_price, + unit_price: autofill ? salesOrderUnitPrice : delivery?.unit_price, total_weight: autofill ? so.total_weight : delivery?.total_weight, qty: autofill ? so.qty : delivery?.qty, avg_weight: autofill ? so.avg_weight : delivery?.avg_weight, @@ -219,7 +232,7 @@ export const mergeSOwithDO = ( : delivery?.convertion_unit, marketing_type: autofill ? so.marketing_type : delivery?.marketing_type, total_peti: autofill ? so.total_peti : delivery?.total_peti, - price_per_qty: autofill ? so.price_per_qty : delivery?.price_per_qty, + price_per_qty: autofill ? salesOrderPricePerQty : delivery?.price_per_qty, sisa_berat: autofill ? so.sisa_berat : delivery?.sisa_berat, price_sisa_berat: autofill ? so.price_sisa_berat diff --git a/src/components/pages/marketing/form/repeater/delivery-order/DeliverOrderProduct.tsx b/src/components/pages/marketing/form/repeater/delivery-order/DeliverOrderProduct.tsx index 66b50600..781ebf26 100644 --- a/src/components/pages/marketing/form/repeater/delivery-order/DeliverOrderProduct.tsx +++ b/src/components/pages/marketing/form/repeater/delivery-order/DeliverOrderProduct.tsx @@ -32,6 +32,63 @@ import Dropdown from '@/components/Dropdown'; import { Icon } from '@iconify/react'; import { handleMarketingCalculation } from '@/lib/marketing-calculation'; +type PricingOption = + | string + | { + value: string; + label: string; + } + | null + | undefined; + +type PricingSource = + | { + marketing_type?: PricingOption; + convertion_unit?: PricingOption; + total_price?: string | number | null; + qty?: string | number | null; + total_weight?: string | number | null; + unit_price?: string | number | null; + price_per_qty?: number | null; + } + | null + | undefined; + +const getOptionValue = (value?: PricingOption) => { + if (!value) return undefined; + if (typeof value === 'string') return value.toLowerCase(); + + return value.value?.toLowerCase(); +}; + +const isTelurQtyProduct = (value?: PricingSource) => + getOptionValue(value?.marketing_type) === 'telur' && + getOptionValue(value?.convertion_unit) === 'qty'; + +const getDisplayedUnitPrice = (value?: PricingSource) => { + if ( + isTelurQtyProduct(value) && + Number(value?.total_price || 0) > 0 && + Number(value?.qty || 0) > 0 + ) { + return Number(value?.total_price) / Number(value?.qty); + } + + return value?.unit_price ?? undefined; +}; + +const getDisplayedPricePerQty = (value?: PricingSource) => { + if ( + isTelurQtyProduct(value) && + Number(value?.total_price || 0) > 0 && + Number(value?.total_weight || 0) > 0 + ) { + return Number(value?.total_price) / Number(value?.total_weight); + } + + return value?.price_per_qty ?? null; +}; + const DeliveryOrderProductForm = ({ formState, salesOrders, @@ -69,14 +126,18 @@ const DeliveryOrderProductForm = ({ Number(initialValues.total_peti) : 0; - const initialPricePerConvertion = - initialValues?.total_price && - initialValues?.total_peti && - Number(initialValues.total_peti) !== 0 - ? (Number(initialValues.total_price) - - initialSisaBerat * Number(initialValues.unit_price || 0)) / - Number(initialValues.total_peti) - : 0; + // const initialPricePerConvertion = + // initialValues?.total_price && + // initialValues?.total_peti && + // Number(initialValues.total_peti) !== 0 + // ? (Number(initialValues.total_price) - + // initialSisaBerat * Number(initialValues.unit_price || 0)) / + // Number(initialValues.total_peti) + // : 0; + + const initialPricePerConvertion = initialValues?.unit_price + ? Number(initialValues?.unit_price) + : 0; const initialPriceSisaBerat = initialValues?.total_price && initialValues?.total_peti @@ -154,6 +215,27 @@ const DeliveryOrderProductForm = ({ (item) => item.id === initialValues?.marketing_product_id ); + const defaultPricingSource: PricingSource = { + marketing_type: + initialValues?.marketing_type ?? salesOrder?.marketing_type ?? null, + convertion_unit: + initialValues?.convertion_unit ?? salesOrder?.convertion_unit ?? null, + total_price: + deliveryOrder?.total_price ?? + initialValues?.total_price ?? + salesOrder?.total_price, + qty: deliveryOrder?.qty ?? initialValues?.qty ?? salesOrder?.qty, + total_weight: + deliveryOrder?.total_weight ?? + initialValues?.total_weight ?? + salesOrder?.total_weight, + unit_price: + deliveryOrder?.unit_price ?? + initialValues?.unit_price ?? + salesOrder?.unit_price, + price_per_qty: initialValues?.price_per_qty ?? null, + }; + const formik = useFormik({ enableReinitialize: true, initialValues: { @@ -167,8 +249,7 @@ const DeliveryOrderProductForm = ({ undefined, marketing_product_id: salesOrder?.id || initialValues?.marketing_product_id || undefined, - unit_price: - deliveryOrder?.unit_price ?? initialValues?.unit_price ?? undefined, + unit_price: getDisplayedUnitPrice(defaultPricingSource), total_weight: deliveryOrder?.total_weight ?? initialValues?.total_weight ?? undefined, qty: deliveryOrder?.qty ?? initialValues?.qty ?? undefined, @@ -186,7 +267,7 @@ const DeliveryOrderProductForm = ({ convertion_unit: initialValues?.convertion_unit || null, marketing_type: initialValues?.marketing_type || null, total_peti: initialValues?.total_peti ?? null, - price_per_qty: initialValues?.price_per_qty ?? null, + price_per_qty: getDisplayedPricePerQty(defaultPricingSource), sisa_berat: initialSisaBerat, price_sisa_berat: initialPriceSisaBerat, week: initialValues?.week ?? null, @@ -329,7 +410,11 @@ const DeliveryOrderProductForm = ({ if (!Boolean(initialValues.qty)) { handleResetForm(); } else { - setFormikValues(initialValues); + setFormikValues({ + ...initialValues, + unit_price: getDisplayedUnitPrice(initialValues), + price_per_qty: getDisplayedPricePerQty(initialValues), + }); if (initialValues?.marketing_product_id) { setSelectedProduct({ value: initialValues?.id, @@ -458,10 +543,11 @@ const DeliveryOrderProductForm = ({ marketing_product_id: selected.value as number, marketing_product: soFieldValues, qty: so.qty, - unit_price: so.unit_price, + unit_price: getDisplayedUnitPrice(so), total_price: so.total_price, avg_weight: so.avg_weight, total_weight: so.total_weight, + price_per_qty: getDisplayedPricePerQty(so), vehicle_number: so.vehicle_number, week: soFieldValues.week ?? null, }); @@ -761,12 +847,32 @@ const DeliveryOrderProductForm = ({ /> )} - {/* Harga per butir untuk TELUR + QTY */} + {/* Harga Satuan */} + {formik.values.convertion_unit?.value.toLowerCase() !== 'peti' && + formik.values.convertion_unit?.value.toLowerCase() !== 'kg' && ( + { + const value = Number(e.target.value); + handleFieldChange('unit_price', value, () => + setCurrentInput(e.target.name) + ); + }} + isError={Boolean(formik.errors.unit_price)} + errorMessage={formik.errors.unit_price} + placeholder='Masukan Harga Satuan' + /> + )} + + {/* Harga per kg untuk TELUR + QTY */} {formik.values.marketing_type?.value.toLowerCase() === 'telur' && formik.values.convertion_unit?.value.toLowerCase() === 'qty' && ( { @@ -780,27 +886,7 @@ const DeliveryOrderProductForm = ({ Boolean(formik.errors.price_per_qty) } errorMessage={formik.errors.price_per_qty} - placeholder='Masukan Harga per Butir' - /> - )} - - {/* Harga Satuan */} - {formik.values.convertion_unit?.value.toLowerCase() !== 'peti' && - formik.values.convertion_unit?.value.toLowerCase() !== 'kg' && ( - { - const value = Number(e.target.value); - handleFieldChange('unit_price', value, () => - setCurrentInput(e.target.name) - ); - }} - isError={Boolean(formik.errors.unit_price)} - errorMessage={formik.errors.unit_price} - placeholder='Masukan Harga Satuan' + placeholder='Masukan Harga per Kg' /> )}