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

145 lines
5.9 KiB
TypeScript

import { Content, ContentStack, ContentText } from 'pdfmake/interfaces';
import {
createHeader,
createLabelTextArray,
createSection,
formatText,
getContentTable,
getDifferentColumnsValue,
getTable,
getTStawkaPodatku,
getValue,
} from '../../../shared/PDF-functions';
import { HeaderDefine } from '../../../shared/types/pdf-types';
import { Procedura, TRodzajFaktury } from '../../../shared/consts/const';
import { Fa, FP } from '../../types/fa1.types';
import FormatTyp, { Position } from '../../../shared/enums/common.enum';
import { FormContentState } from '../../../shared/types/additional-data.types';
import { addMarza } from '../common/Wiersze';
export function generateWiersze(faVat: Fa): Content {
const table: Content[] = [];
const rodzajFaktury: string | number | undefined = getValue(faVat.RodzajFaktury);
const isP_PMarzy = Boolean(Number(getValue(faVat.Adnotacje?.P_PMarzy)));
const faWiersze: Record<string, FP>[] = getTable(faVat.FaWiersze?.FaWiersz).map(
(wiersz: Record<string, FP>): Record<string, FP> => {
const marza: Record<string, FP> = addMarza(rodzajFaktury, isP_PMarzy, wiersz)!;
if (getValue(wiersz.P_12)) {
wiersz.P_12._text = getTStawkaPodatku(getValue(wiersz.P_12) as string, 1);
}
return { ...wiersz, ...marza };
}
);
const definedHeaderLp: HeaderDefine[] = [
{ name: 'NrWierszaFa', title: 'Lp.', format: FormatTyp.Default, width: 'auto' },
];
const definedHeader1: HeaderDefine[] = [
{ name: 'UU_ID', title: 'Unikalny numer wiersza', format: FormatTyp.Default, width: 'auto' },
{ name: 'P_7', title: 'Nazwa towaru lub usługi', format: FormatTyp.Default, width: '*' },
{ name: 'P_9A', title: 'Cena jedn. netto', format: FormatTyp.Currency, width: 'auto' },
{ name: 'P_9B', title: 'Cena jedn. brutto', format: FormatTyp.Currency, width: 'auto' },
{ name: 'P_8B', title: 'Ilość', format: FormatTyp.Number, width: 'auto' },
{ name: 'P_8A', title: 'Miara', format: FormatTyp.Default, width: 'auto' },
{ name: 'P_10', title: 'Rabat', format: FormatTyp.Currency, width: 'auto' },
{ name: 'P_12', title: 'Stawka podatku', format: FormatTyp.Default, width: 'auto' },
{ name: 'P_12_XII', title: 'Stawka podatku OSS', format: FormatTyp.Percentage, width: 'auto' },
{ name: 'P_11', title: 'Wartość sprzedaży netto', format: FormatTyp.Currency, width: 'auto' },
{ name: 'P_11A', title: 'Wartość sprzedaży brutto', format: FormatTyp.Currency, width: 'auto' },
];
if (getDifferentColumnsValue('KursWaluty', faWiersze).length !== 1) {
definedHeader1.push({
name: 'KursWaluty',
title: 'Kurs waluty',
format: FormatTyp.Currency6,
width: 'auto',
});
}
const definedHeader2: HeaderDefine[] = [
{ name: 'GTIN', title: 'GTIN', format: FormatTyp.Default, width: 'auto' },
{ name: 'PKWiU', title: 'PKWiU', format: FormatTyp.Default, width: 'auto' },
{ name: 'CN', title: 'CN', format: FormatTyp.Default, width: 'auto' },
{ name: 'PKOB', title: 'PKOB', format: FormatTyp.Default, width: 'auto' },
{ name: 'DodatkoweInfo', title: 'Dodatkowe informacje', format: FormatTyp.Default, width: 'auto' },
{
name: 'P_12_Procedura',
title: 'Procedura',
format: FormatTyp.Default,
mappingData: Procedura,
width: '*',
},
{ name: 'KwotaAkcyzy', title: 'KwotaAkcyzy', format: FormatTyp.Default, width: 'auto' },
{ name: 'GTU', title: 'GTU', format: FormatTyp.Default, width: 'auto' },
{ name: 'Procedura', title: 'Oznaczenia dotyczące procedur', format: FormatTyp.Default, width: '*' },
{ name: 'P_6A', title: 'Data dostawy / wykonania', format: FormatTyp.Default, width: 'auto' },
];
let content: FormContentState = getContentTable<(typeof faWiersze)[0]>(
[...definedHeaderLp, ...definedHeader1, ...definedHeader2],
faWiersze,
'*'
);
const ceny: string | ContentText = formatText(
`Faktura wystawiona w cenach ${content.fieldsWithValue.includes('P_11') ? 'netto' : 'brutto'} w walucie ${faVat.KodWaluty?._text}`,
[FormatTyp.Label, FormatTyp.MarginBottom8]
);
const p_15: string | number | undefined = getValue(faVat.P_15);
let opis: ContentStack[] = [];
if (rodzajFaktury == TRodzajFaktury.ROZ && Number(p_15) !== 0) {
opis = [
{
stack: createLabelTextArray([
{ value: 'Kwota pozostała do zapłaty: ', formatTyp: FormatTyp.LabelGreater },
{
value: p_15,
formatTyp: FormatTyp.CurrencyGreater,
currency: getValue(faVat.KodWaluty)?.toString() ?? '',
},
]),
alignment: Position.RIGHT,
margin: [0, 8, 0, 0],
},
];
} else if (
(rodzajFaktury == TRodzajFaktury.VAT ||
rodzajFaktury == TRodzajFaktury.KOR ||
rodzajFaktury == TRodzajFaktury.KOR_ROZ ||
rodzajFaktury == TRodzajFaktury.UPR) &&
Number(p_15) !== 0
) {
opis = [
{
stack: createLabelTextArray([
{ value: 'Kwota należności ogółem: ', formatTyp: FormatTyp.LabelGreater },
{
value: p_15,
formatTyp: [FormatTyp.CurrencyGreater],
currency: getValue(faVat.KodWaluty)?.toString() ?? '',
},
]),
alignment: Position.RIGHT,
margin: [0, 8, 0, 0],
},
];
}
if (content.fieldsWithValue.length <= 9 && content.content) {
table.push(content.content);
} else {
content = getContentTable<(typeof faWiersze)[0]>([...definedHeaderLp, ...definedHeader1], faWiersze, '*');
if (content.content) {
table.push(content.content);
}
content = getContentTable<(typeof faWiersze)[0]>([...definedHeaderLp, ...definedHeader2], faWiersze, '*');
if (content.content && content.fieldsWithValue.length > 1) {
table.push('\n');
table.push(content.content);
}
}
if (table.length < 1) {
return [];
}
return createSection([...createHeader('Pozycje'), ceny, ...table, ...opis], true);
}