mirror of
https://gitlab.com/mbugroup/lti-web-client.git
synced 2026-05-25 07:45:47 +00:00
refactor(FE-364): Refactor purchases export to use supplier reports
This commit is contained in:
@@ -10,7 +10,7 @@ import {
|
|||||||
pdf,
|
pdf,
|
||||||
} from '@react-pdf/renderer';
|
} from '@react-pdf/renderer';
|
||||||
import { LogisticPurchasePerSupplierReport } from '@/types/api/report/logistic-stock';
|
import { LogisticPurchasePerSupplierReport } from '@/types/api/report/logistic-stock';
|
||||||
import { formatDate, formatNumber } from '@/lib/helper';
|
import { formatCurrency, formatDate, formatNumber } from '@/lib/helper';
|
||||||
|
|
||||||
Font.register({
|
Font.register({
|
||||||
family: 'Helvetica',
|
family: 'Helvetica',
|
||||||
@@ -177,7 +177,7 @@ const pdfStyles = StyleSheet.create({
|
|||||||
});
|
});
|
||||||
|
|
||||||
interface PurchasesPerSupplierExportParams {
|
interface PurchasesPerSupplierExportParams {
|
||||||
data: LogisticPurchasePerSupplierReport['rows'];
|
data: LogisticPurchasePerSupplierReport[];
|
||||||
params: {
|
params: {
|
||||||
area_name?: string;
|
area_name?: string;
|
||||||
supplier_name?: string;
|
supplier_name?: string;
|
||||||
@@ -192,36 +192,6 @@ interface PurchasesPerSupplierExportParams {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GroupedSupplierData {
|
|
||||||
id: number;
|
|
||||||
supplier: LogisticPurchasePerSupplierReport['rows'][number]['supplier'];
|
|
||||||
items: LogisticPurchasePerSupplierReport['rows'][number][];
|
|
||||||
}
|
|
||||||
|
|
||||||
const groupDataBySupplier = (
|
|
||||||
data: LogisticPurchasePerSupplierReport['rows']
|
|
||||||
): GroupedSupplierData[] => {
|
|
||||||
const groups: {
|
|
||||||
[key: number]: GroupedSupplierData;
|
|
||||||
} = {};
|
|
||||||
|
|
||||||
data.forEach((item) => {
|
|
||||||
const supplierId = item.supplier?.id;
|
|
||||||
if (supplierId && !groups[supplierId]) {
|
|
||||||
groups[supplierId] = {
|
|
||||||
id: supplierId,
|
|
||||||
supplier: item.supplier,
|
|
||||||
items: [],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
if (groups[supplierId]) {
|
|
||||||
groups[supplierId].items.push(item);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return Object.values(groups) as GroupedSupplierData[];
|
|
||||||
};
|
|
||||||
|
|
||||||
const getParameterText = (
|
const getParameterText = (
|
||||||
params: PurchasesPerSupplierExportParams['params']
|
params: PurchasesPerSupplierExportParams['params']
|
||||||
) => {
|
) => {
|
||||||
@@ -249,7 +219,7 @@ const getParameterText = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const createPDFDocument = (
|
const createPDFDocument = (
|
||||||
groupedData: GroupedSupplierData[],
|
supplierReports: LogisticPurchasePerSupplierReport[],
|
||||||
params: PurchasesPerSupplierExportParams['params']
|
params: PurchasesPerSupplierExportParams['params']
|
||||||
) => (
|
) => (
|
||||||
<Document>
|
<Document>
|
||||||
@@ -277,20 +247,23 @@ const createPDFDocument = (
|
|||||||
</View>
|
</View>
|
||||||
|
|
||||||
{/* Supplier Sections */}
|
{/* Supplier Sections */}
|
||||||
{groupedData.map(
|
{supplierReports.map(
|
||||||
(supplierGroup: GroupedSupplierData, supplierIndex: number) => {
|
(
|
||||||
|
supplierReport: LogisticPurchasePerSupplierReport,
|
||||||
|
supplierIndex: number
|
||||||
|
) => {
|
||||||
return (
|
return (
|
||||||
<View
|
<View
|
||||||
key={supplierGroup.id}
|
key={supplierReport.supplier.id}
|
||||||
style={[
|
style={[
|
||||||
pdfStyles.supplierSection,
|
pdfStyles.supplierSection,
|
||||||
supplierIndex < groupedData.length - 1
|
supplierIndex < supplierReports.length - 1
|
||||||
? pdfStyles.supplierSectionBreak
|
? pdfStyles.supplierSectionBreak
|
||||||
: {},
|
: {},
|
||||||
]}
|
]}
|
||||||
>
|
>
|
||||||
<Text style={pdfStyles.supplierTitle}>
|
<Text style={pdfStyles.supplierTitle}>
|
||||||
{supplierGroup.supplier.name}
|
{supplierReport.supplier.name}
|
||||||
</Text>
|
</Text>
|
||||||
|
|
||||||
<View style={pdfStyles.table}>
|
<View style={pdfStyles.table}>
|
||||||
@@ -338,7 +311,7 @@ const createPDFDocument = (
|
|||||||
</View>
|
</View>
|
||||||
|
|
||||||
{/* Table Body */}
|
{/* Table Body */}
|
||||||
{supplierGroup.items.map(
|
{supplierReport.rows.map(
|
||||||
(
|
(
|
||||||
item: LogisticPurchasePerSupplierReport['rows'][number],
|
item: LogisticPurchasePerSupplierReport['rows'][number],
|
||||||
index: number
|
index: number
|
||||||
@@ -347,7 +320,7 @@ const createPDFDocument = (
|
|||||||
key={index}
|
key={index}
|
||||||
style={[
|
style={[
|
||||||
pdfStyles.tableRow,
|
pdfStyles.tableRow,
|
||||||
index < supplierGroup.items.length - 1
|
index < supplierReport.rows.length - 1
|
||||||
? pdfStyles.tableBorderBottom
|
? pdfStyles.tableBorderBottom
|
||||||
: {},
|
: {},
|
||||||
]}
|
]}
|
||||||
@@ -376,18 +349,18 @@ const createPDFDocument = (
|
|||||||
<Text>{formatNumber(item.qty || 0)}</Text>
|
<Text>{formatNumber(item.qty || 0)}</Text>
|
||||||
</View>
|
</View>
|
||||||
<View style={[pdfStyles.tableCellRight, { flex: 1.2 }]}>
|
<View style={[pdfStyles.tableCellRight, { flex: 1.2 }]}>
|
||||||
<Text>{formatNumber(item.unit_price || 0)}</Text>
|
<Text>{formatCurrency(item.unit_price || 0)}</Text>
|
||||||
</View>
|
</View>
|
||||||
<View style={[pdfStyles.tableCellRight, { flex: 1.5 }]}>
|
<View style={[pdfStyles.tableCellRight, { flex: 1.5 }]}>
|
||||||
<Text>{formatNumber(item.purchase_value || 0)}</Text>
|
<Text>{formatCurrency(item.purchase_value || 0)}</Text>
|
||||||
</View>
|
</View>
|
||||||
<View style={[pdfStyles.tableCellRight, { flex: 1.2 }]}>
|
<View style={[pdfStyles.tableCellRight, { flex: 1.2 }]}>
|
||||||
<Text>
|
<Text>
|
||||||
{formatNumber(item.transport_unit_price || 0)}
|
{formatCurrency(item.transport_unit_price || 0)}
|
||||||
</Text>
|
</Text>
|
||||||
</View>
|
</View>
|
||||||
<View style={[pdfStyles.tableCellRight, { flex: 1.5 }]}>
|
<View style={[pdfStyles.tableCellRight, { flex: 1.5 }]}>
|
||||||
<Text>{formatNumber(item.total_amount || 0)}</Text>
|
<Text>{formatCurrency(item.total_amount || 0)}</Text>
|
||||||
</View>
|
</View>
|
||||||
<View style={[pdfStyles.tableCell, { flex: 1.2 }]}>
|
<View style={[pdfStyles.tableCell, { flex: 1.2 }]}>
|
||||||
<View style={pdfStyles.badge}>
|
<View style={pdfStyles.badge}>
|
||||||
@@ -410,11 +383,10 @@ const createPDFDocument = (
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const generatePurchasesPerSupplierPDF = async (
|
export const generatePurchasesPerSupplierPDF = async (
|
||||||
data: LogisticPurchasePerSupplierReport['rows'],
|
data: LogisticPurchasePerSupplierReport[],
|
||||||
params: PurchasesPerSupplierExportParams['params']
|
params: PurchasesPerSupplierExportParams['params']
|
||||||
): Promise<void> => {
|
): Promise<void> => {
|
||||||
const groupedData = groupDataBySupplier(data);
|
const PDFDocument = createPDFDocument(data, params);
|
||||||
const PDFDocument = createPDFDocument(groupedData, params);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const blob = await pdf(PDFDocument).toBlob();
|
const blob = await pdf(PDFDocument).toBlob();
|
||||||
|
|||||||
@@ -313,7 +313,7 @@ const PurchasesPerSupplierTab = () => {
|
|||||||
end_date: tableFilterState.end_date || undefined,
|
end_date: tableFilterState.end_date || undefined,
|
||||||
sort_by: tableFilterState.sort_by || undefined,
|
sort_by: tableFilterState.sort_by || undefined,
|
||||||
filter_by: tableFilterState.filter_by || undefined,
|
filter_by: tableFilterState.filter_by || undefined,
|
||||||
limit: 10000,
|
limit: 100,
|
||||||
page: 1,
|
page: 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -459,10 +459,6 @@ const PurchasesPerSupplierTab = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const allRows = allDataForExport.flatMap(
|
|
||||||
(supplierReport) => supplierReport.rows
|
|
||||||
);
|
|
||||||
|
|
||||||
const areaName =
|
const areaName =
|
||||||
tableFilterState.area_id.length > 0
|
tableFilterState.area_id.length > 0
|
||||||
? tableFilterState.area_id
|
? tableFilterState.area_id
|
||||||
@@ -518,7 +514,7 @@ const PurchasesPerSupplierTab = () => {
|
|||||||
end_date: tableFilterState.end_date || '',
|
end_date: tableFilterState.end_date || '',
|
||||||
};
|
};
|
||||||
|
|
||||||
await generatePurchasesPerSupplierPDF(allRows, exportParams);
|
await generatePurchasesPerSupplierPDF(allDataForExport, exportParams);
|
||||||
toast.success('PDF berhasil dibuat dan diunduh.');
|
toast.success('PDF berhasil dibuat dan diunduh.');
|
||||||
} catch {
|
} catch {
|
||||||
toast.error('Gagal membuat PDF. Silakan coba lagi.');
|
toast.error('Gagal membuat PDF. Silakan coba lagi.');
|
||||||
@@ -821,7 +817,7 @@ const PurchasesPerSupplierTab = () => {
|
|||||||
/>
|
/>
|
||||||
<SelectInput
|
<SelectInput
|
||||||
label='Urutkan Berdasarkan'
|
label='Urutkan Berdasarkan'
|
||||||
placeholder='Pilih Urutkan Berdasarkan'
|
placeholder='Urutkan Berdasarkan'
|
||||||
options={sortByOptions}
|
options={sortByOptions}
|
||||||
value={
|
value={
|
||||||
sortByOptions?.find(
|
sortByOptions?.find(
|
||||||
|
|||||||
Reference in New Issue
Block a user