mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 15:55:48 +00:00
refactor(FE-355,356,257): Use API summary per_weight_range for HPP
reports
This commit is contained in:
@@ -15,7 +15,11 @@ import { SaleReportApi } from '@/services/api/report/marketing-sale';
|
||||
import Table from '@/components/Table';
|
||||
import { ColumnDef, Row } from '@tanstack/react-table';
|
||||
import { formatCurrency, formatNumber } from '@/lib/helper';
|
||||
import { HppPerKandangReport } from '@/types/api/report/hpp-per-kandang';
|
||||
import {
|
||||
HppPerKandangReport,
|
||||
HppPerKandangRow,
|
||||
HppPerKandangPerWeightRange,
|
||||
} from '@/types/api/report/hpp-per-kandang';
|
||||
import { isResponseSuccess } from '@/lib/api-helper';
|
||||
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
||||
import Button from '@/components/Button';
|
||||
@@ -225,6 +229,17 @@ const HppPerKandangTab = () => {
|
||||
? hppPerKandang.data.summary
|
||||
: undefined;
|
||||
|
||||
const summaryTotal =
|
||||
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.summary?.total
|
||||
? hppPerKandang.data.summary.total
|
||||
: undefined;
|
||||
|
||||
const perWeightRangeSummary =
|
||||
isResponseSuccess(hppPerKandang) &&
|
||||
hppPerKandang?.data?.summary?.per_weight_range
|
||||
? hppPerKandang.data.summary.per_weight_range
|
||||
: [];
|
||||
|
||||
const period =
|
||||
isResponseSuccess(hppPerKandang) && hppPerKandang?.data?.period
|
||||
? hppPerKandang.data.period
|
||||
@@ -271,25 +286,49 @@ const HppPerKandangTab = () => {
|
||||
|
||||
// ===== TABLE COLUMNS DEFINITION =====
|
||||
const totals: Totals = useMemo(() => {
|
||||
if (summaryTotal && perWeightRangeSummary.length > 0) {
|
||||
const totalAverageDocPrice =
|
||||
perWeightRangeSummary.reduce(
|
||||
(acc: number, item: HppPerKandangPerWeightRange) =>
|
||||
acc + (item.average_doc_price_rp || 0),
|
||||
0
|
||||
) / perWeightRangeSummary.length;
|
||||
|
||||
return {
|
||||
total_hpp_rp: perWeightRangeSummary.reduce(
|
||||
(acc: number, item: HppPerKandangPerWeightRange) =>
|
||||
acc + (item.hpp_rp || 0),
|
||||
0
|
||||
),
|
||||
total_average_doc_price_rp: totalAverageDocPrice,
|
||||
};
|
||||
}
|
||||
|
||||
if (data.length > 0) {
|
||||
return {
|
||||
total_hpp_rp: data.reduce(
|
||||
(acc: number, item: HppPerKandangRow) => acc + (item.hpp_rp || 0),
|
||||
0
|
||||
),
|
||||
total_average_doc_price_rp:
|
||||
data.reduce(
|
||||
(acc: number, item: HppPerKandangRow) =>
|
||||
acc + (item.average_doc_price_rp || 0),
|
||||
0
|
||||
) / data.length,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
total_hpp_rp:
|
||||
data.length > 0
|
||||
? data.reduce((acc, item) => acc + (item.hpp_rp || 0), 0)
|
||||
: 0,
|
||||
total_average_doc_price_rp:
|
||||
data.length > 0
|
||||
? data.reduce(
|
||||
(acc, item) => acc + (item.average_doc_price_rp || 0),
|
||||
0
|
||||
) / data.length
|
||||
: 0,
|
||||
total_hpp_rp: 0,
|
||||
total_average_doc_price_rp: 0,
|
||||
};
|
||||
}, [summary]);
|
||||
}, [summaryTotal, perWeightRangeSummary, data]);
|
||||
|
||||
const allFeedSuppliers = useMemo(() => {
|
||||
const suppliers = new Set<string>();
|
||||
data.forEach((item) => {
|
||||
item.feed_suppliers?.forEach((s) => {
|
||||
data.forEach((item: HppPerKandangRow) => {
|
||||
item.feed_suppliers?.forEach((s: { alias?: string; name: string }) => {
|
||||
suppliers.add(s.alias || s.name);
|
||||
});
|
||||
});
|
||||
@@ -298,8 +337,8 @@ const HppPerKandangTab = () => {
|
||||
|
||||
const allDocSuppliers = useMemo(() => {
|
||||
const suppliers = new Set<string>();
|
||||
data.forEach((item) => {
|
||||
item.doc_suppliers?.forEach((s) => {
|
||||
data.forEach((item: HppPerKandangRow) => {
|
||||
item.doc_suppliers?.forEach((s: { alias?: string; name: string }) => {
|
||||
suppliers.add(s.alias || s.name);
|
||||
});
|
||||
});
|
||||
@@ -327,7 +366,7 @@ const HppPerKandangTab = () => {
|
||||
const summary = allDataForExport.summary;
|
||||
|
||||
const excelData: { [key: string]: string | number }[] = allExportData.map(
|
||||
(item, index) => ({
|
||||
(item: HppPerKandangRow, index: number) => ({
|
||||
No: index + 1,
|
||||
Kandang: item.kandang?.name || '',
|
||||
'Rentang Bobot': item.weight_range
|
||||
@@ -339,10 +378,13 @@ const HppPerKandangTab = () => {
|
||||
'Produksi Telur (Butir)': item.egg_production_pieces || 0,
|
||||
'Produksi Telur (KG)': item.egg_production_kg || 0,
|
||||
'Feed (Supplier)':
|
||||
item.feed_suppliers?.map((s) => s.alias || s.name).join(' | ') ||
|
||||
'',
|
||||
item.feed_suppliers
|
||||
?.map((s: { alias?: string; name: string }) => s.alias || s.name)
|
||||
.join(' | ') || '',
|
||||
'DOC (Supplier)':
|
||||
item.doc_suppliers?.map((s) => s.alias || s.name).join(' | ') || '',
|
||||
item.doc_suppliers
|
||||
?.map((s: { alias?: string; name: string }) => s.alias || s.name)
|
||||
.join(' | ') || '',
|
||||
'Rata-Rata Harga DOC (RP)': item.average_doc_price_rp || 0,
|
||||
'Nilai Nominal Telur (RP)': item.egg_value_rp || 0,
|
||||
'HPP Ayam (RP)': item.hpp_rp || 0,
|
||||
@@ -588,7 +630,11 @@ const HppPerKandangTab = () => {
|
||||
accessorKey: 'feed_suppliers',
|
||||
cell: (props) => {
|
||||
const suppliers = props.row.original.feed_suppliers;
|
||||
return suppliers?.map((s) => s.alias || s.name).join(' | ') || '-';
|
||||
return (
|
||||
suppliers
|
||||
?.map((s: { alias?: string; name: string }) => s.alias || s.name)
|
||||
.join(' | ') || '-'
|
||||
);
|
||||
},
|
||||
footer: () => (
|
||||
<div className='font-semibold text-gray-900'>
|
||||
@@ -602,7 +648,11 @@ const HppPerKandangTab = () => {
|
||||
accessorKey: 'doc_suppliers',
|
||||
cell: (props) => {
|
||||
const suppliers = props.row.original.doc_suppliers;
|
||||
return suppliers?.map((s) => s.alias || s.name).join(' | ') || '-';
|
||||
return (
|
||||
suppliers
|
||||
?.map((s: { alias?: string; name: string }) => s.alias || s.name)
|
||||
.join(' | ') || '-'
|
||||
);
|
||||
},
|
||||
footer: () => (
|
||||
<div className='font-semibold text-gray-900'>
|
||||
|
||||
Reference in New Issue
Block a user