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