mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-20 05:22:02 +00:00
feat(FE-355,357): Use API summary for weighted averages
This commit is contained in:
@@ -19,7 +19,6 @@ import { formatCurrency, formatNumber } from '@/lib/helper';
|
||||
import { HppPerKandangReport } from '@/types/api/report/hpp-per-kandang';
|
||||
import { isResponseSuccess } from '@/lib/api-helper';
|
||||
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
||||
import Pagination from '@/components/Pagination';
|
||||
import Button from '@/components/Button';
|
||||
import Dropdown from '@/components/dropdown/Dropdown';
|
||||
import MenuItem from '@/components/menu/MenuItem';
|
||||
@@ -210,20 +209,15 @@ const HppPerKandangTab = () => {
|
||||
);
|
||||
|
||||
const summary =
|
||||
isResponseSuccess(hppPerKandang) && 'summary' in hppPerKandang
|
||||
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.summary
|
||||
? hppPerKandang.data.summary
|
||||
: undefined;
|
||||
|
||||
const period =
|
||||
isResponseSuccess(hppPerKandang) && 'period' in hppPerKandang
|
||||
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.period
|
||||
? hppPerKandang.data.period
|
||||
: undefined;
|
||||
|
||||
const meta =
|
||||
isResponseSuccess(hppPerKandang) && 'meta' in hppPerKandang
|
||||
? hppPerKandang.meta
|
||||
: undefined;
|
||||
|
||||
const { data: allDataForExport } = useSWR(
|
||||
isSubmitted
|
||||
? () => {
|
||||
@@ -417,32 +411,6 @@ const HppPerKandangTab = () => {
|
||||
return Array.from(suppliers).join(' | ');
|
||||
}, [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 tableColumns: ColumnDef<HppPerKandangReport['rows'][0]>[] = [
|
||||
{
|
||||
@@ -553,7 +521,13 @@ const HppPerKandangTab = () => {
|
||||
},
|
||||
footer: () => (
|
||||
<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>
|
||||
),
|
||||
},
|
||||
@@ -567,7 +541,13 @@ const HppPerKandangTab = () => {
|
||||
},
|
||||
footer: () => (
|
||||
<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>
|
||||
),
|
||||
},
|
||||
@@ -709,6 +689,8 @@ const HppPerKandangTab = () => {
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className='divider'></div>
|
||||
|
||||
{!isSubmitted ? (
|
||||
<div className='mt-6 text-center text-gray-500'>
|
||||
Silakan pilih filter dan klik tombol Cari untuk menampilkan data.
|
||||
@@ -727,7 +709,7 @@ const HppPerKandangTab = () => {
|
||||
columns={getTableColumns()}
|
||||
renderFooter={data.length > 0}
|
||||
className={{
|
||||
containerClassName: 'w-full',
|
||||
containerClassName: 'w-full mt-6',
|
||||
tableWrapperClassName: 'overflow-x-auto mt-4',
|
||||
tableClassName: 'w-full table-auto text-sm',
|
||||
headerRowClassName: 'border-b border-b-gray-200 bg-gray-50',
|
||||
@@ -742,7 +724,6 @@ const HppPerKandangTab = () => {
|
||||
footerRowClassName: 'border-t-2 border-gray-300',
|
||||
footerColumnClassName:
|
||||
'px-4 py-3 text-xs text-gray-900 whitespace-nowrap',
|
||||
paginationClassName: 'hidden',
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user