feat(FE-355,357): Use API summary for weighted averages

This commit is contained in:
rstubryan
2025-12-17 11:11:44 +07:00
parent 730b7903a7
commit 4cc41c0167
@@ -19,7 +19,6 @@ import { formatCurrency, formatNumber } from '@/lib/helper';
import { HppPerKandangReport } from '@/types/api/report/hpp-per-kandang'; import { HppPerKandangReport } from '@/types/api/report/hpp-per-kandang';
import { isResponseSuccess } from '@/lib/api-helper'; import { isResponseSuccess } from '@/lib/api-helper';
import { useTableFilter } from '@/services/hooks/useTableFilter'; import { useTableFilter } from '@/services/hooks/useTableFilter';
import Pagination from '@/components/Pagination';
import Button from '@/components/Button'; import Button from '@/components/Button';
import Dropdown from '@/components/dropdown/Dropdown'; import Dropdown from '@/components/dropdown/Dropdown';
import MenuItem from '@/components/menu/MenuItem'; import MenuItem from '@/components/menu/MenuItem';
@@ -210,20 +209,15 @@ const HppPerKandangTab = () => {
); );
const summary = const summary =
isResponseSuccess(hppPerKandang) && 'summary' in hppPerKandang isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.summary
? hppPerKandang.data.summary ? hppPerKandang.data.summary
: undefined; : undefined;
const period = const period =
isResponseSuccess(hppPerKandang) && 'period' in hppPerKandang isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.period
? hppPerKandang.data.period ? hppPerKandang.data.period
: undefined; : undefined;
const meta =
isResponseSuccess(hppPerKandang) && 'meta' in hppPerKandang
? hppPerKandang.meta
: undefined;
const { data: allDataForExport } = useSWR( const { data: allDataForExport } = useSWR(
isSubmitted isSubmitted
? () => { ? () => {
@@ -417,32 +411,6 @@ const HppPerKandangTab = () => {
return Array.from(suppliers).join(' | '); return Array.from(suppliers).join(' | ');
}, [data]); }, [data]);
const weightedAverageDocPrice = useMemo(() => {
if (data.length === 0) return 0;
const totalValue = data.reduce(
(sum, item) => sum + (item.hpp_rp * item.remaining_chicken_birds || 0),
0
);
const totalChickens = data.reduce(
(sum, item) => sum + (item.remaining_chicken_birds || 0),
0
);
return totalChickens > 0 ? totalValue / totalChickens : 0;
}, [data]);
const weightedAverageHpp = useMemo(() => {
if (data.length === 0) return 0;
const totalValue = data.reduce(
(sum, item) => sum + (item.hpp_rp * item.remaining_chicken_birds || 0),
0
);
const totalChickens = data.reduce(
(sum, item) => sum + (item.remaining_chicken_birds || 0),
0
);
return totalChickens > 0 ? totalValue / totalChickens : 0;
}, [data]);
const getTableColumns = (): ColumnDef<HppPerKandangReport['rows'][0]>[] => { const getTableColumns = (): ColumnDef<HppPerKandangReport['rows'][0]>[] => {
const tableColumns: ColumnDef<HppPerKandangReport['rows'][0]>[] = [ const tableColumns: ColumnDef<HppPerKandangReport['rows'][0]>[] = [
{ {
@@ -553,7 +521,13 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatCurrency(weightedAverageDocPrice)} {formatCurrency(
summary?.total_remaining_value_rp &&
summary?.total_remaining_chicken_birds
? summary.total_remaining_value_rp /
summary.total_remaining_chicken_birds
: 0
)}
</div> </div>
), ),
}, },
@@ -567,7 +541,13 @@ const HppPerKandangTab = () => {
}, },
footer: () => ( footer: () => (
<div className='text-right font-semibold text-gray-900'> <div className='text-right font-semibold text-gray-900'>
{formatCurrency(weightedAverageHpp)} {formatCurrency(
summary?.total_remaining_value_rp &&
summary?.total_remaining_chicken_birds
? summary.total_remaining_value_rp /
summary.total_remaining_chicken_birds
: 0
)}
</div> </div>
), ),
}, },
@@ -709,6 +689,8 @@ const HppPerKandangTab = () => {
/> />
</div> </div>
<div className='divider'></div>
{!isSubmitted ? ( {!isSubmitted ? (
<div className='mt-6 text-center text-gray-500'> <div className='mt-6 text-center text-gray-500'>
Silakan pilih filter dan klik tombol Cari untuk menampilkan data. Silakan pilih filter dan klik tombol Cari untuk menampilkan data.
@@ -727,7 +709,7 @@ const HppPerKandangTab = () => {
columns={getTableColumns()} columns={getTableColumns()}
renderFooter={data.length > 0} renderFooter={data.length > 0}
className={{ className={{
containerClassName: 'w-full', containerClassName: 'w-full mt-6',
tableWrapperClassName: 'overflow-x-auto mt-4', tableWrapperClassName: 'overflow-x-auto mt-4',
tableClassName: 'w-full table-auto text-sm', tableClassName: 'w-full table-auto text-sm',
headerRowClassName: 'border-b border-b-gray-200 bg-gray-50', headerRowClassName: 'border-b border-b-gray-200 bg-gray-50',
@@ -742,7 +724,6 @@ const HppPerKandangTab = () => {
footerRowClassName: 'border-t-2 border-gray-300', footerRowClassName: 'border-t-2 border-gray-300',
footerColumnClassName: footerColumnClassName:
'px-4 py-3 text-xs text-gray-900 whitespace-nowrap', 'px-4 py-3 text-xs text-gray-900 whitespace-nowrap',
paginationClassName: 'hidden',
}} }}
/> />
)} )}