diff --git a/src/components/pages/closing/ClosingDetail.tsx b/src/components/pages/closing/ClosingDetail.tsx index 07745878..c3c91a5a 100644 --- a/src/components/pages/closing/ClosingDetail.tsx +++ b/src/components/pages/closing/ClosingDetail.tsx @@ -66,7 +66,13 @@ const ClosingDetail: React.FC = ({ { id: 'overhead', label: 'Overhead', - content: , + content: ( + + ), }, { id: 'hppEkspedisi', diff --git a/src/components/pages/closing/ClosingGeneralInformationTable.tsx b/src/components/pages/closing/ClosingGeneralInformationTable.tsx index 0105beb6..a3aa0f49 100644 --- a/src/components/pages/closing/ClosingGeneralInformationTable.tsx +++ b/src/components/pages/closing/ClosingGeneralInformationTable.tsx @@ -1,3 +1,4 @@ +import { formatNumber } from '@/lib/helper'; import { ClosingGeneralInformation } from '@/types/api/closing'; import { ProjectFlock } from '@/types/api/production/project-flock'; import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang'; @@ -56,8 +57,8 @@ const ClosingGeneralInformationTable = ({ : {!kandangData - ? (initialValue?.population ?? 0) - : (chickinPopulation ?? 0)}{' '} + ? formatNumber(initialValue?.population || 0) + : formatNumber(chickinPopulation || 0)}{' '} Ekor diff --git a/src/components/pages/closing/ClosingOverheadTabContent.tsx b/src/components/pages/closing/ClosingOverheadTabContent.tsx index 458cff0f..e6b0cb5a 100644 --- a/src/components/pages/closing/ClosingOverheadTabContent.tsx +++ b/src/components/pages/closing/ClosingOverheadTabContent.tsx @@ -1,16 +1,26 @@ import ClosingOverheadTable from '@/components/pages/closing/ClosingOverheadTable'; +import { ClosingGeneralInformation } from '@/types/api/closing'; +import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang'; interface ClosingOverheadTabContentProps { projectFlockId: number; + generalInformation?: ClosingGeneralInformation; + kandangData?: ProjectFlockKandang; } const ClosingOverheadTabContent = ({ projectFlockId, + generalInformation, + kandangData, }: ClosingOverheadTabContentProps) => { return (
{projectFlockId && ( - + )}
); diff --git a/src/components/pages/closing/ClosingOverheadTable.tsx b/src/components/pages/closing/ClosingOverheadTable.tsx index 9ef6694f..0d51ae3b 100644 --- a/src/components/pages/closing/ClosingOverheadTable.tsx +++ b/src/components/pages/closing/ClosingOverheadTable.tsx @@ -3,7 +3,13 @@ import Table, { TABLE_DEFAULT_STYLING } from '@/components/Table'; import { isResponseSuccess } from '@/lib/api-helper'; import { cn, formatCurrency, formatDate, formatNumber } from '@/lib/helper'; import { ClosingApi } from '@/services/api/closing'; -import { Overhead, OverheadTotal } from '@/types/api/closing'; +import { + ClosingGeneralInformation, + Overhead, + OverheadTotal, +} from '@/types/api/closing'; +import { ProjectFlockKandang } from '@/types/api/production/project-flock-kandang'; +import { Icon } from '@iconify/react'; import { ColumnDef } from '@tanstack/react-table'; import { useSearchParams } from 'next/navigation'; import { useMemo } from 'react'; @@ -11,16 +17,30 @@ import useSWR from 'swr'; interface ClosingOverheadTableProps { projectFlockId: number; + generalInformation?: ClosingGeneralInformation; + kandangData?: ProjectFlockKandang; } const ClosingOverheadTable = ({ projectFlockId, + generalInformation, + kandangData, }: ClosingOverheadTableProps) => { const searchParams = useSearchParams(); const kandangId = searchParams.get('kandangId'); const { data: overhead, isLoading: isLoadingOverhead } = useSWR( - `${ClosingApi.basePath}/${projectFlockId}${kandangId ? `/${kandangId}` : ''}/overhead`, + `${ClosingApi.basePath}/${projectFlockId}/overhead`, + () => ClosingApi.getOverhead(projectFlockId), + { + keepPreviousData: true, + } + ); + + const { data: overheadKandang, isLoading: isLoadingOverheadKandang } = useSWR( + kandangId + ? `${ClosingApi.basePath}/${projectFlockId}/${kandangId}/overhead` + : undefined, () => ClosingApi.getOverhead( projectFlockId, @@ -31,6 +51,26 @@ const ClosingOverheadTable = ({ } ); + const chickinPopulation = useMemo(() => { + if (kandangData) { + return kandangData?.chickins?.reduce( + (acc, chickin) => acc + chickin.usage_qty, + 0 + ); + } + return 0; + }, [kandangData]); + + const kandangTotal = useMemo(() => { + if (!isResponseSuccess(overhead)) { + return 0; + } + const total = + ((chickinPopulation ?? 0) * overhead.data.total.actual_total_amount) / + (generalInformation?.population ?? 0); + return total; + }, [overhead, chickinPopulation, generalInformation]); + // Helper function to create columns with footer support const createColumns = ( total?: OverheadTotal, @@ -44,29 +84,22 @@ const ClosingOverheadTable = ({ { id: 'budget_quantity', header: 'Jumlah', - accessorFn: (props) => - props.budget_quantity ? formatNumber(props.budget_quantity) : '-', + accessorFn: (props) => formatNumber(props.budget_quantity), footer: total ? () => formatNumber(total.budget_quantity) : '', }, { id: 'budget_unit_price', header: 'Harga Satuan', - accessorFn: (props) => - props.budget_unit_price - ? formatCurrency(props.budget_unit_price) - : '-', + accessorFn: (props) => formatCurrency(props.budget_unit_price), footer: '', }, { id: 'budget_total_amount', header: 'Total', - accessorFn: (props) => - props.budget_total_amount - ? formatCurrency(props.budget_total_amount) - : '-', + accessorFn: (props) => formatCurrency(props.budget_total_amount), footer: total ? () => formatCurrency(total.budget_total_amount) - : '', + : '0', }, ], }, @@ -78,37 +111,28 @@ const ClosingOverheadTable = ({ id: 'actual_date', header: 'Tanggal', accessorFn: (props) => - props.actual_date - ? formatDate(props.actual_date, 'DD MMM, YYYY') - : '-', + formatDate(props.actual_date, 'DD MMM, YYYY'), footer: '', }, { id: 'actual_quantity', header: 'Jumlah', - accessorFn: (props) => - props.actual_quantity ? formatNumber(props.actual_quantity) : '-', - footer: total ? () => formatNumber(total.actual_quantity) : '', + accessorFn: (props) => formatNumber(props.actual_quantity), + footer: total ? () => formatNumber(total.actual_quantity) : '0', }, { id: 'actual_unit_price', header: 'Harga Satuan', - accessorFn: (props) => - props.actual_unit_price - ? formatCurrency(props.actual_unit_price) - : '-', + accessorFn: (props) => formatCurrency(props.actual_unit_price), footer: '', }, { id: 'actual_total_amount', header: 'Total', - accessorFn: (props) => - props.actual_total_amount - ? formatCurrency(props.actual_total_amount) - : '-', + accessorFn: (props) => formatCurrency(props.actual_total_amount), footer: total ? () => formatCurrency(total.actual_total_amount) - : '', + : '0', }, ], }, @@ -118,35 +142,25 @@ const ClosingOverheadTable = ({ { id: 'actual_date', header: 'Tanggal', - accessorFn: (props) => - props.actual_date - ? formatDate(props.actual_date, 'DD MMM, YYYY') - : '-', + accessorFn: (props) => formatDate(props.actual_date, 'DD MMM, YYYY'), footer: '', }, { id: 'actual_quantity', header: 'Jumlah', - accessorFn: (props) => - props.actual_quantity ? formatNumber(props.actual_quantity) : '-', + accessorFn: (props) => formatNumber(props.actual_quantity), footer: total ? () => formatNumber(total.actual_quantity) : '', }, { id: 'actual_unit_price', header: 'Harga Satuan', - accessorFn: (props) => - props.actual_unit_price - ? formatCurrency(props.actual_unit_price) - : '-', + accessorFn: (props) => formatCurrency(props.actual_unit_price), footer: '', }, { id: 'actual_total_amount', header: 'Total', - accessorFn: (props) => - props.actual_total_amount - ? formatCurrency(props.actual_total_amount) - : '-', + accessorFn: (props) => formatCurrency(props.actual_total_amount), footer: total ? () => formatCurrency(total.actual_total_amount) : '', }, ]; @@ -171,8 +185,7 @@ const ClosingOverheadTable = ({ { id: 'cost_per_bird', header: 'Rp/Ekor', - accessorFn: (props) => - props.cost_per_bird ? formatCurrency(props.cost_per_bird) : '-', + accessorFn: (props) => formatCurrency(props.cost_per_bird), footer: total ? () => formatCurrency(total.cost_per_bird) : '', }, ]; @@ -183,11 +196,15 @@ const ClosingOverheadTable = ({ () => isResponseSuccess(overhead) ? createColumns( - overhead.data?.total, + kandangId + ? isResponseSuccess(overheadKandang) + ? overheadKandang.data?.total + : undefined + : overhead.data?.total, kandangId ? Number(kandangId) : undefined ) : createColumns(), - [overhead] + [overhead, kandangId, overheadKandang] ); return ( @@ -203,7 +220,13 @@ const ClosingOverheadTable = ({ > data={ - isResponseSuccess(overhead) ? (overhead.data?.overheads ?? []) : [] + kandangId + ? isResponseSuccess(overheadKandang) + ? (overheadKandang.data?.overheads ?? []) + : [] + : isResponseSuccess(overhead) + ? (overhead.data?.overheads ?? []) + : [] } columns={columns} className={{ @@ -220,6 +243,60 @@ const ClosingOverheadTable = ({ : false } /> + {kandangId && ( + +
+
+

Pembelian Kandang

+
+
+ +
+
+
+ Populasi Akhir KANDANG{' '} + Pemakaian + Di FARM +
+
+
+ Populasi Akhir Proyek +
+
+
+ +
+
+
+ {formatNumber(chickinPopulation ?? 0)} + + {formatCurrency( + isResponseSuccess(overhead) + ? overhead.data?.total.actual_total_amount + : 0 + )} +
+
+
+ {formatNumber(generalInformation?.population ?? 0)} +
+
+
+ +
+
+

+ {formatNumber(kandangTotal || 0)} +

+
+
+
+ )} );