Files
ksef-pdf-generator/src/lib-public/generators/FA2/Platnosc.ts
Michał Chudy f48680a65f chore(): 1.0.0
2026-01-30 20:06:43 +01:00

135 lines
4.8 KiB
TypeScript

import { Content } from 'pdfmake/interfaces';
import {
createHeader,
createLabelText,
generateLine,
generateTwoColumns,
getContentTable,
getTable,
hasValue,
} from '../../../shared/PDF-functions';
import { HeaderDefine } from '../../../shared/types/pdf-types';
import { Platnosc, TerminPlatnosci, ZaplataCzesciowa } from '../../types/fa2.types';
import { getFormaPlatnosciString } from '../../../shared/generators/common/functions';
import { generujRachunekBankowy } from './RachunekBankowy';
import FormatTyp from '../../../shared/enums/common.enum';
import { FP } from '../../types/fa1.types';
import { FormContentState } from '../../../shared/types/additional-data.types';
export function generatePlatnosc(platnosc: Platnosc | undefined): Content {
if (!platnosc) {
return [];
}
const terminPlatnosci: TerminPlatnosci[] = getTable(platnosc.TerminPlatnosci);
const zaplataCzesciowaHeader: HeaderDefine[] = [
{
name: 'Termin',
title: 'Termin płatności',
format: FormatTyp.Default,
},
];
if (terminPlatnosci.some((termin: TerminPlatnosci): FP | undefined => termin.TerminOpis)) {
zaplataCzesciowaHeader.push({ name: 'TerminOpis', title: 'Opis płatności', format: FormatTyp.Default });
}
const zaplataCzesciowaNaglowek: HeaderDefine[] = [
{
name: 'DataZaplatyCzesciowej',
title: 'Data zapłaty częściowej',
format: FormatTyp.Default,
},
{ name: 'KwotaZaplatyCzesciowej', title: 'Kwota zapłaty częściowej', format: FormatTyp.Currency },
{ name: 'FormaPlatnosci', title: 'Forma płatności', format: FormatTyp.FormOfPayment },
];
const table: Content[] = [generateLine(), ...createHeader('Płatność')];
if (platnosc.Zaplacono?._text === '1') {
table.push(createLabelText('Informacja o płatności: ', 'Zapłacono'));
table.push(createLabelText('Data zapłaty: ', platnosc.DataZaplaty, FormatTyp.Date));
} else if (platnosc.ZnacznikZaplatyCzesciowej?._text === '1') {
table.push(createLabelText('Informacja o płatności: ', 'Zapłata częściowa'));
} else {
table.push(createLabelText('Informacja o płatności: ', 'Brak zapłaty'));
}
if (hasValue(platnosc.FormaPlatnosci)) {
table.push(createLabelText('Forma płatności: ', getFormaPlatnosciString(platnosc.FormaPlatnosci)));
} else {
if (platnosc.OpisPlatnosci?._text) {
table.push(createLabelText('Forma płatności: ', 'Płatność inna'));
table.push(createLabelText('Opis płatności innej: ', platnosc.OpisPlatnosci));
}
}
const zaplataCzesciowa: ZaplataCzesciowa[] = getTable(platnosc.ZaplataCzesciowa);
const tableZaplataCzesciowa: FormContentState = getContentTable<(typeof zaplataCzesciowa)[0]>(
zaplataCzesciowaNaglowek,
zaplataCzesciowa,
'*'
);
const terminPatnosciContent: TerminPlatnosci[] = terminPlatnosci.map(
(platnosc: TerminPlatnosci): TerminPlatnosci => {
if (!terminPlatnosci.some((termin: TerminPlatnosci): FP | undefined => termin.TerminOpis)) {
return platnosc;
} else {
return {
...platnosc,
TerminOpis: {
_text: `${platnosc.Termin?._text ?? ''}`,
} as any,
};
}
}
);
const tableTerminPlatnosci: FormContentState = getContentTable<(typeof terminPlatnosci)[0]>(
zaplataCzesciowaHeader,
terminPatnosciContent,
'*'
);
if (zaplataCzesciowa.length > 0 && terminPlatnosci.length > 0) {
table.push(
generateTwoColumns(
tableZaplataCzesciowa.content ?? [],
tableTerminPlatnosci.content ?? [],
[0, 4, 0, 0]
)
);
} else if (terminPlatnosci.length > 0) {
if (tableTerminPlatnosci.content) {
table.push(generateTwoColumns([], tableTerminPlatnosci.content));
}
} else if (zaplataCzesciowa.length > 0 && tableZaplataCzesciowa.content) {
table.push(tableZaplataCzesciowa.content);
}
const rachunekBankowy: Content[][] = getTable(platnosc.RachunekBankowy as Record<string, FP>[]).map(
(rachunek) => generujRachunekBankowy([rachunek], 'Numer rachunku bankowego')
);
const rachunekBankowyFaktora: Content[][] = getTable(
platnosc.RachunekBankowyFaktora as Record<string, FP>[]
).map((rachunek) => generujRachunekBankowy([rachunek], 'Numer rachunku bankowego faktora'));
const rachunkiBankowe: Content[][] = [...rachunekBankowy, ...rachunekBankowyFaktora];
if (rachunkiBankowe.length > 0) {
rachunkiBankowe.forEach((rachunek, index) => {
if (index % 2 === 0) {
table.push(generateTwoColumns(rachunek, rachunkiBankowe[index + 1] ?? []));
}
});
}
if (platnosc.Skonto) {
table.push(createHeader('Skonto', [0, 0]));
table.push(createLabelText('Warunki skonta: ', platnosc.Skonto.WarunkiSkonta));
table.push(createLabelText('Wysokość skonta: ', platnosc.Skonto.WysokoscSkonta));
}
return table;
}