mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-06-11 02:51:49 +00:00
feat: migrate depreciation report to V2 API with daily breakdown view
- Add V2 types (ReportDepreciationV2Item, DepreciationV2Meta, DepreciationV2Response) for the new per-day response shape - Add DepreciationReportV2Api service pointing to /reports/expense/v2/depreciation - Require projectFlock in filter (was optional); auto-open filter modal on first load when none is selected - Replace multi-card farm loop with a single project flock card showing farm_name and period only in the header - Replace kandang sub-table with daily depreciation rows: date, day_n, chickin_date, depreciation_value, pullet_cost_day_n_total, multiplication_percentage, total_value_pullet_after_depreciation - Add Total Hari (limit) NumberInput field (default 10) to filter modal; remove pagination - Switch storeName to report-depreciation-v2-table to avoid loading stale localStorage state Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
'use client';
|
||||
|
||||
import React, { useEffect, useMemo } from 'react';
|
||||
import React, { useEffect, useMemo, useRef } from 'react';
|
||||
import useSWR from 'swr';
|
||||
import { ColumnDef } from '@tanstack/react-table';
|
||||
|
||||
import { Icon } from '@iconify/react';
|
||||
import Card from '@/components/Card';
|
||||
import Pagination from '@/components/Pagination';
|
||||
import Table from '@/components/Table';
|
||||
import ButtonFilter from '@/components/helper/ButtonFilter';
|
||||
import ReportExpenseSkeleton from '@/components/pages/report/expense/skeleton/ReportExpenseSkeleton';
|
||||
@@ -14,11 +13,14 @@ import { useModal } from '@/components/Modal';
|
||||
import ReportDepreciationFilterModal from '@/components/pages/report/expense/tab/ReportDepreciationFilterModal';
|
||||
|
||||
import { useTabActionsStore } from '@/stores/tab-actions/tab-actions.store';
|
||||
import { ReportDepreciation } from '@/types/api/report/report-expense';
|
||||
import { DepreciationReportApi } from '@/services/api/report/expense-report';
|
||||
import {
|
||||
DepreciationV2Response,
|
||||
ReportDepreciationV2Item,
|
||||
} from '@/types/api/report/report-expense';
|
||||
import { DepreciationReportV2Api } from '@/services/api/report/expense-report';
|
||||
import { useTableFilter } from '@/services/hooks/useTableFilter';
|
||||
import { OptionType } from '@/components/input/SelectInput';
|
||||
import { isResponseSuccess } from '@/lib/api-helper';
|
||||
import { httpClientFetcher } from '@/services/http/client';
|
||||
import { formatCurrency, formatDate, formatNumber } from '@/lib/helper';
|
||||
|
||||
interface ReportDepreciationTabProps {
|
||||
@@ -29,8 +31,6 @@ const ReportDepreciationTab = ({ tabId }: ReportDepreciationTabProps) => {
|
||||
const {
|
||||
state: tableFilterState,
|
||||
updateFilter,
|
||||
setPage,
|
||||
setPageSize,
|
||||
toQueryString: getTableFilterQueryString,
|
||||
reset: resetFilter,
|
||||
} = useTableFilter<{
|
||||
@@ -38,78 +38,117 @@ const ReportDepreciationTab = ({ tabId }: ReportDepreciationTabProps) => {
|
||||
location?: OptionType<string>;
|
||||
projectFlock?: OptionType<string>;
|
||||
period: string;
|
||||
totalDays: number;
|
||||
}>({
|
||||
initial: {
|
||||
area: undefined,
|
||||
location: undefined,
|
||||
projectFlock: undefined,
|
||||
period: formatDate(Date.now(), 'YYYY-MM-DD'),
|
||||
totalDays: 10,
|
||||
},
|
||||
paramMap: {
|
||||
pageSize: 'limit',
|
||||
area: 'area_id',
|
||||
location: 'location_id',
|
||||
projectFlock: 'project_flock_id',
|
||||
period: 'period',
|
||||
totalDays: 'limit',
|
||||
},
|
||||
persist: true,
|
||||
storeName: 'report-depreciation-table',
|
||||
storeName: 'report-depreciation-v2-table',
|
||||
});
|
||||
|
||||
const { data: depreciationsResponse, isLoading: isLoadingDepreciations } =
|
||||
useSWR(
|
||||
`${DepreciationReportApi.basePath}${getTableFilterQueryString()}`,
|
||||
DepreciationReportApi.getAllFetcher
|
||||
);
|
||||
const swrKey = tableFilterState.projectFlock
|
||||
? `${DepreciationReportV2Api.basePath}${getTableFilterQueryString()}`
|
||||
: null;
|
||||
|
||||
const depreciations = isResponseSuccess(depreciationsResponse)
|
||||
? depreciationsResponse.data
|
||||
: [];
|
||||
const { data: depreciationsResponse, isLoading: isLoadingDepreciations } =
|
||||
useSWR<DepreciationV2Response>(swrKey, httpClientFetcher);
|
||||
|
||||
const depreciationMeta =
|
||||
depreciationsResponse?.status === 'success'
|
||||
? depreciationsResponse.meta
|
||||
: null;
|
||||
const depreciationData =
|
||||
depreciationsResponse?.status === 'success'
|
||||
? depreciationsResponse.data
|
||||
: [];
|
||||
|
||||
const filterModal = useModal();
|
||||
const { ref: filterModalRef } = filterModal;
|
||||
|
||||
const initialOpenRef = useRef(false);
|
||||
useEffect(() => {
|
||||
if (!initialOpenRef.current) {
|
||||
initialOpenRef.current = true;
|
||||
if (!tableFilterState.projectFlock) {
|
||||
filterModal.openModal();
|
||||
}
|
||||
}
|
||||
}, []);
|
||||
|
||||
const setTabActions = useTabActionsStore((state) => state.setTabActions);
|
||||
const clearTabActions = useTabActionsStore((state) => state.clearTabActions);
|
||||
|
||||
const depreciationKandangColumns: ColumnDef<
|
||||
ReportDepreciation['components']['kandang'][0]
|
||||
>[] = [
|
||||
{
|
||||
accessorKey: 'kandang_name',
|
||||
header: 'Kandang',
|
||||
},
|
||||
{
|
||||
accessorKey: 'house_type',
|
||||
header: 'Tipe Kandang',
|
||||
cell: ({ row }) => row.original.house_type.toUpperCase(),
|
||||
},
|
||||
{
|
||||
accessorKey: 'depreciation_percent',
|
||||
header: 'Persentase Depresiasi',
|
||||
cell: ({ row }) => row.original.depreciation_percent + '%',
|
||||
},
|
||||
{
|
||||
accessorKey: 'depreciation_value',
|
||||
header: 'Nilai Depresiasi',
|
||||
cell: ({ row }) => formatCurrency(row.original.depreciation_value),
|
||||
},
|
||||
{
|
||||
accessorKey: 'depreciation_source',
|
||||
header: 'Asal Depresiasi',
|
||||
cell: ({ row }) => row.original.depreciation_source.toUpperCase(),
|
||||
},
|
||||
{
|
||||
accessorKey: 'cutover_date',
|
||||
header: 'Tanggal Cutover',
|
||||
cell: ({ row }) => formatDate(row.original.cutover_date, 'DD MMM YYYY'),
|
||||
},
|
||||
{
|
||||
accessorKey: 'origin_date',
|
||||
header: 'Tanggal Origin',
|
||||
cell: ({ row }) => formatDate(row.original.origin_date, 'DD MMM YYYY'),
|
||||
},
|
||||
];
|
||||
const depreciationColumns: ColumnDef<ReportDepreciationV2Item>[] = useMemo(
|
||||
() => [
|
||||
{
|
||||
accessorKey: 'date',
|
||||
header: 'Tanggal',
|
||||
cell: ({ row }) => formatDate(row.original.date, 'DD MMM YYYY'),
|
||||
},
|
||||
{
|
||||
accessorKey: 'day_n',
|
||||
header: 'Hari ke-',
|
||||
},
|
||||
{
|
||||
accessorKey: 'chickin_date',
|
||||
header: 'Tanggal Chick-in',
|
||||
cell: ({ row }) => formatDate(row.original.chickin_date, 'DD MMM YYYY'),
|
||||
},
|
||||
{
|
||||
accessorKey: 'depreciation_value',
|
||||
header: 'Nilai Depresiasi',
|
||||
cell: ({ row }) => formatCurrency(row.original.depreciation_value),
|
||||
},
|
||||
{
|
||||
accessorKey: 'pullet_cost_day_n_total',
|
||||
header: 'Total Harga Pullet Hari ke-N',
|
||||
cell: ({ row }) =>
|
||||
formatCurrency(
|
||||
row.original.pullet_cost_day_n_total,
|
||||
'IDR',
|
||||
'id-ID',
|
||||
0,
|
||||
10
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: 'multiplication_percentage',
|
||||
header: 'Persentase Multiplikasi',
|
||||
cell: ({ row }) =>
|
||||
formatNumber(
|
||||
row.original.multiplication_percentage * 100,
|
||||
'en-US',
|
||||
0,
|
||||
4
|
||||
) + '%',
|
||||
},
|
||||
{
|
||||
accessorKey: 'total_value_pullet_after_depreciation',
|
||||
header: 'Total Nilai Pullet Setelah Depresiasi',
|
||||
cell: ({ row }) =>
|
||||
formatCurrency(
|
||||
row.original.total_value_pullet_after_depreciation,
|
||||
'IDR',
|
||||
'id-ID',
|
||||
0,
|
||||
10
|
||||
),
|
||||
},
|
||||
],
|
||||
[]
|
||||
);
|
||||
|
||||
const tabActionsElement = useMemo(
|
||||
() => (
|
||||
@@ -145,9 +184,9 @@ const ReportDepreciationTab = ({ tabId }: ReportDepreciationTabProps) => {
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!isLoadingDepreciations && depreciations.length === 0 && (
|
||||
{!isLoadingDepreciations && !tableFilterState.projectFlock && (
|
||||
<ReportExpenseSkeleton
|
||||
columns={depreciationKandangColumns}
|
||||
columns={depreciationColumns}
|
||||
icon={
|
||||
<Icon
|
||||
icon='heroicons:chart-bar'
|
||||
@@ -156,90 +195,75 @@ const ReportDepreciationTab = ({ tabId }: ReportDepreciationTabProps) => {
|
||||
height={20}
|
||||
/>
|
||||
}
|
||||
title='Data Not Yet Available'
|
||||
subtitle='Please change your filters to get the data.'
|
||||
title='Pilih Project Flock'
|
||||
subtitle='Silakan pilih Project Flock pada filter untuk melihat data depresiasi.'
|
||||
/>
|
||||
)}
|
||||
|
||||
{!isLoadingDepreciations && depreciations.length > 0 && (
|
||||
<>
|
||||
{depreciations.map((depreciationItem, idx) => (
|
||||
<Card
|
||||
key={idx}
|
||||
title={depreciationItem.farm_name}
|
||||
subtitle={`Period: ${formatDate(depreciationItem.period, 'DD MMM YYYY')} | Depresiasi Efektif: ${formatNumber(depreciationItem.depreciation_percent_effective, 'en-US', 0, 10)}% | Nilai Depresiasi: ${formatCurrency(depreciationItem.depreciation_value)} | Total Pullet Cost: ${formatCurrency(depreciationItem.pullet_cost_day_n_total, 'IDR', 'id-ID', 0, 10)}`}
|
||||
className={{
|
||||
wrapper: 'w-full rounded-lg border-none',
|
||||
body: 'p-0',
|
||||
title:
|
||||
'px-2 py-1.5 font-normal text-sm bg-primary text-white',
|
||||
subtitle:
|
||||
'px-2 pb-1.5 bg-primary text-white text-xs font-normal',
|
||||
collapsible: 'rounded-lg',
|
||||
}}
|
||||
variant='bordered'
|
||||
collapsible={true}
|
||||
>
|
||||
<Table
|
||||
data={depreciationItem.components.kandang}
|
||||
columns={depreciationKandangColumns}
|
||||
pageSize={tableFilterState.pageSize}
|
||||
page={
|
||||
isResponseSuccess(depreciationsResponse)
|
||||
? depreciationsResponse?.meta?.page
|
||||
: 0
|
||||
}
|
||||
totalItems={
|
||||
isResponseSuccess(depreciationsResponse)
|
||||
? depreciationsResponse?.meta?.total_results
|
||||
: 0
|
||||
}
|
||||
onPageChange={setPage}
|
||||
onPageSizeChange={setPageSize}
|
||||
isLoading={isLoadingDepreciations}
|
||||
className={{
|
||||
containerClassName: 'w-full mb-0!',
|
||||
tableWrapperClassName:
|
||||
'overflow-x-auto rounded-tr-none rounded-tl-none',
|
||||
tableClassName: 'w-full table-auto text-sm',
|
||||
headerRowClassName: 'border-b border-b-gray-200 bg-gray-50',
|
||||
headerColumnClassName:
|
||||
'px-4 py-3 text-xs font-semibold text-gray-700 text-left border border-gray-200',
|
||||
bodyRowClassName:
|
||||
'hover:bg-gray-50 transition-colors border-b border-l border-r border-b-gray-200 border-l-gray-200 border-r-gray-200',
|
||||
bodyColumnClassName:
|
||||
'px-4 py-3 text-xs text-gray-900 whitespace-nowrap',
|
||||
tableFooterClassName:
|
||||
'bg-gray-100 font-semibold border border-gray-200',
|
||||
footerRowClassName: 'border-t-2 border-gray-300',
|
||||
footerColumnClassName:
|
||||
'px-4 py-3 text-xs text-gray-900 whitespace-nowrap',
|
||||
paginationClassName: 'hidden',
|
||||
}}
|
||||
{!isLoadingDepreciations &&
|
||||
tableFilterState.projectFlock &&
|
||||
depreciationData.length === 0 && (
|
||||
<ReportExpenseSkeleton
|
||||
columns={depreciationColumns}
|
||||
icon={
|
||||
<Icon
|
||||
icon='heroicons:chart-bar'
|
||||
className='text-white'
|
||||
width={20}
|
||||
height={20}
|
||||
/>
|
||||
</Card>
|
||||
))}
|
||||
|
||||
<Pagination
|
||||
totalItems={
|
||||
isResponseSuccess(depreciationsResponse)
|
||||
? (depreciationsResponse?.meta?.total_results ?? 0)
|
||||
: 0
|
||||
}
|
||||
itemsPerPage={tableFilterState.pageSize}
|
||||
currentPage={
|
||||
isResponseSuccess(depreciationsResponse)
|
||||
? (depreciationsResponse?.meta?.page ?? 0)
|
||||
: 0
|
||||
}
|
||||
onPrevPage={() => setPage(tableFilterState.page - 1)}
|
||||
onNextPage={() => setPage(tableFilterState.page + 1)}
|
||||
onPageChange={setPage}
|
||||
rowOptions={[10, 20, 50, 100]}
|
||||
onRowChange={setPageSize}
|
||||
title='Data Not Yet Available'
|
||||
subtitle='Please change your filters to get the data.'
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
)}
|
||||
|
||||
{!isLoadingDepreciations &&
|
||||
depreciationData.length > 0 &&
|
||||
depreciationMeta && (
|
||||
<Card
|
||||
title={depreciationMeta.farm_name}
|
||||
subtitle={`Periode: ${formatDate(depreciationMeta.period, 'DD MMM YYYY')}`}
|
||||
className={{
|
||||
wrapper: 'w-full rounded-lg border-none',
|
||||
body: 'p-0',
|
||||
title: 'px-2 py-1.5 font-normal text-sm bg-primary text-white',
|
||||
subtitle:
|
||||
'px-2 pb-1.5 bg-primary text-white text-xs font-normal',
|
||||
collapsible: 'rounded-lg',
|
||||
}}
|
||||
variant='bordered'
|
||||
collapsible={true}
|
||||
>
|
||||
<Table
|
||||
data={depreciationData}
|
||||
columns={depreciationColumns}
|
||||
pageSize={depreciationData.length}
|
||||
page={1}
|
||||
totalItems={depreciationData.length}
|
||||
isLoading={isLoadingDepreciations}
|
||||
className={{
|
||||
containerClassName: 'w-full mb-0!',
|
||||
tableWrapperClassName:
|
||||
'overflow-x-auto rounded-tr-none rounded-tl-none',
|
||||
tableClassName: 'w-full table-auto text-sm',
|
||||
headerRowClassName: 'border-b border-b-gray-200 bg-gray-50',
|
||||
headerColumnClassName:
|
||||
'px-4 py-3 text-xs font-semibold text-gray-700 text-left border border-gray-200',
|
||||
bodyRowClassName:
|
||||
'hover:bg-gray-50 transition-colors border-b border-l border-r border-b-gray-200 border-l-gray-200 border-r-gray-200',
|
||||
bodyColumnClassName:
|
||||
'px-4 py-3 text-xs text-gray-900 whitespace-nowrap',
|
||||
tableFooterClassName:
|
||||
'bg-gray-100 font-semibold border border-gray-200',
|
||||
footerRowClassName: 'border-t-2 border-gray-300',
|
||||
footerColumnClassName:
|
||||
'px-4 py-3 text-xs text-gray-900 whitespace-nowrap',
|
||||
paginationClassName: 'hidden',
|
||||
}}
|
||||
/>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<ReportDepreciationFilterModal
|
||||
@@ -255,6 +279,7 @@ const ReportDepreciationTab = ({ tabId }: ReportDepreciationTabProps) => {
|
||||
values.period ? formatDate(values.period, 'YYYY-MM-DD') : '',
|
||||
true
|
||||
);
|
||||
updateFilter('totalDays', values.totalDays ?? 10, true);
|
||||
}}
|
||||
/>
|
||||
</>
|
||||
|
||||
Reference in New Issue
Block a user