@catalogicus-international/n8n-nodes-invoice-parser 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,7 +13,7 @@ var require_profiles = __commonJS({
13
13
  "dist/nodes/InvoicePdfParser/profiles.js"(exports2) {
14
14
  "use strict";
15
15
  Object.defineProperty(exports2, "__esModule", { value: true });
16
- exports2.LEMAN_SHIFT = exports2.LEMAN_OFFER = exports2.PETROVICH_COMPACT = exports2.PETROVICH_DISCOUNT = exports2.PETROVICH_NARROW = exports2.PETROVICH_MAIN = exports2.PETROVICH_WIDE = exports2.TEREM_NARROW = exports2.TEREM = exports2.NORMALVENT = exports2.LUNDA = exports2.LEMAN = exports2.PROFILES = exports2.UNIVERSAL = exports2.ETM = void 0;
16
+ exports2.MEGAPOLIS = exports2.LEMAN_SHIFT = exports2.LEMAN_OFFER = exports2.PETROVICH_COMPACT = exports2.PETROVICH_DISCOUNT = exports2.PETROVICH_NARROW = exports2.PETROVICH_MAIN = exports2.PETROVICH_WIDE = exports2.TEREM_NARROW = exports2.TEREM = exports2.NORMALVENT = exports2.LUNDA = exports2.LEMAN = exports2.PROFILES = exports2.UNIVERSAL = exports2.ETM = void 0;
17
17
  exports2.ETM = {
18
18
  id: "etm",
19
19
  layoutId: "etm/main",
@@ -937,6 +937,332 @@ var require_profiles = __commonJS({
937
937
  }
938
938
  };
939
939
  exports2.PROFILES.push(exports2.LEMAN_SHIFT);
940
+ var ONEC_RENT = [
941
+ { key: "num", from: 0, to: 60 },
942
+ { key: "name", from: 60, to: 390 },
943
+ { key: "qty", from: 390, to: 424.7 },
944
+ { key: "unit", from: 424.7, to: 455 },
945
+ { key: "price", from: 455, to: 521 },
946
+ { key: "sum", from: 521, to: 620 }
947
+ ];
948
+ exports2.MEGAPOLIS = {
949
+ id: "megapolis",
950
+ layoutId: "onec-rent",
951
+ title: "\u041E\u041E\u041E \xAB\u041C\u0415\u0413\u0410\u041F\u041E\u041B\u0418\u0421\xBB",
952
+ detect: [/(?<!\d)7731283974(?!\d)/u],
953
+ pricesIncludeVat: true,
954
+ vatRate: 22,
955
+ columns: ONEC_RENT,
956
+ headerColumns: {
957
+ labels: {
958
+ num: /^(?:№|N|No)$/u,
959
+ name: /^Наименование(?:\s+товара)?$/iu,
960
+ qty: /^(?:Кол-?во|Количество)$/iu,
961
+ unit: /^Ед\.?(?:\s*изм\.?)?$/iu,
962
+ price: /^Цена(?:,?\s*руб\.?)?(?:\s+с\s+НДС)?$/iu,
963
+ sum: /^Сумма(?:,?\s*руб\.?)?(?:\s+с\s+НДС)?$/iu
964
+ },
965
+ exclude: {
966
+ sum: /^Сумма(?:,?\s*руб\.?)?\s+(?:НДС|без)(?:\s|$)/iu
967
+ }
968
+ },
969
+ rowPattern: { num: /^\d+$/ },
970
+ stopPattern: /^итог/iu,
971
+ totalsPatterns: TOTALS_PAYABLE
972
+ };
973
+ exports2.PROFILES.push(exports2.MEGAPOLIS);
974
+ var ONEC_DOCUMENT_HEADER = {
975
+ labels: {
976
+ num: /^(?:№|N|No)$/u,
977
+ name: /^(?:Наименование(?:\s+товара)?|Товары?\s*\(работы,\s*услуги\)|Товар(?:\s*\(Услуга\))?|ТОВАР\s*\/\s*УСЛУГА|Название услуги)$/iu,
978
+ qty: /^(?:Кол-?во|Количество)$/iu,
979
+ unit: /^Ед\.?(?:\s*изм\.?)?$/iu,
980
+ price: /^Цена(?:,?\s*руб\.?)?(?:\s+с\s+НДС)?$/iu,
981
+ sum: /^Сумма(?:,?\s*руб\.?)?(?:\s+с\s+НДС)?$/iu
982
+ },
983
+ exclude: {
984
+ sum: /^Сумма(?:,?\s*руб\.?)?\s+(?:НДС|без)(?:\s|$)/iu
985
+ }
986
+ };
987
+ var ONEC_DOCUMENT_COLUMNS = [
988
+ { key: "num", from: 0, to: 58 },
989
+ { key: "name", from: 58, to: 330 },
990
+ { key: "qty", from: 330, to: 380 },
991
+ { key: "unit", from: 380, to: 420 },
992
+ { key: "price", from: 420, to: 500 },
993
+ { key: "sum", from: 500, to: 620 }
994
+ ];
995
+ var PROFILE_NPD_PAVLYUCHENKO = {
996
+ id: "npd-pavlyuchenko",
997
+ layoutId: "onec-npd",
998
+ title: "\u0418\u041F \u041F\u0430\u0432\u043B\u044E\u0447\u0435\u043D\u043A\u043E \u0412. \u0412. (\u041D\u041F\u0414)",
999
+ detect: [/(?<!\d)611336211593(?!\d)/u],
1000
+ columns: ONEC_DOCUMENT_COLUMNS,
1001
+ headerColumns: ONEC_DOCUMENT_HEADER,
1002
+ rowPattern: { num: /^\d+$/ },
1003
+ stopPattern: /^итог/iu,
1004
+ totalsPatterns: { total: /^Итого к оплате/iu }
1005
+ };
1006
+ exports2.PROFILES.push(PROFILE_NPD_PAVLYUCHENKO);
1007
+ var PROFILE_NPD_ZABOLOTNY = {
1008
+ id: "npd-zabolotny",
1009
+ layoutId: "onec-npd",
1010
+ title: "\u0418\u041F \u0417\u0430\u0431\u043E\u043B\u043E\u0442\u043D\u044B\u0439 \u0410. \u041E. (\u041D\u041F\u0414)",
1011
+ detect: [/(?<!\d)151010902928(?!\d)/u],
1012
+ columns: ONEC_DOCUMENT_COLUMNS,
1013
+ headerColumns: ONEC_DOCUMENT_HEADER,
1014
+ rowPattern: { num: /^\d+$/ },
1015
+ stopPattern: /^итог/iu,
1016
+ totalsPatterns: { total: /^Итого к оплате/iu }
1017
+ };
1018
+ exports2.PROFILES.push(PROFILE_NPD_ZABOLOTNY);
1019
+ var PROFILE_TURDUGULOV = {
1020
+ id: "turdugulov",
1021
+ layoutId: "onec-contract",
1022
+ title: "\u0418\u041F \u0422\u0443\u0440\u0434\u0443\u0433\u0443\u043B\u043E\u0432 \u0410. \u0410.",
1023
+ detect: [/(?<!\d)773433196000(?!\d)/u],
1024
+ columns: [
1025
+ { key: "num", from: 0, to: 58 },
1026
+ { key: "name", from: 58, to: 385 },
1027
+ { key: "price", from: 385, to: 450 },
1028
+ { key: "qty", from: 450, to: 480 },
1029
+ { key: "unit", from: 480, to: 505 },
1030
+ { key: "sum", from: 505, to: 620 }
1031
+ ],
1032
+ headerColumns: ONEC_DOCUMENT_HEADER,
1033
+ rowPattern: { num: /^\d+$/ },
1034
+ stopPattern: /^итог/iu,
1035
+ totalsPatterns: {
1036
+ sum: /^Итого:/iu,
1037
+ vat: /Сумма НДС/iu,
1038
+ total: /^Всего к оплате/iu
1039
+ }
1040
+ };
1041
+ exports2.PROFILES.push(PROFILE_TURDUGULOV);
1042
+ var PROFILE_PTK_MIRA = {
1043
+ id: "ptk-mira",
1044
+ layoutId: "onec-simple",
1045
+ title: "\u041E\u041E\u041E \xAB\u041F\u0422\u041A \xAB\u041C\u0418\u0420\u0410\xBB",
1046
+ detect: [/(?<!\d)7842073161(?!\d)/u],
1047
+ columns: ONEC_DOCUMENT_COLUMNS,
1048
+ headerColumns: ONEC_DOCUMENT_HEADER,
1049
+ rowPattern: { num: /^\d+$/ },
1050
+ stopPattern: /^итог/iu,
1051
+ totalsPatterns: { total: /^Всего к оплате/iu }
1052
+ };
1053
+ exports2.PROFILES.push(PROFILE_PTK_MIRA);
1054
+ var PROFILE_DELTSOVA = {
1055
+ id: "deltsova",
1056
+ layoutId: "onec-gross",
1057
+ title: "\u0418\u041F \u0414\u0435\u043B\u044C\u0446\u043E\u0432\u0430 \u041E. \u0415.",
1058
+ detect: [/(?<!\d)773700483641(?!\d)/u],
1059
+ pricesIncludeVat: true,
1060
+ vatRate: 22,
1061
+ columns: ONEC_DOCUMENT_COLUMNS,
1062
+ headerColumns: ONEC_DOCUMENT_HEADER,
1063
+ rowPattern: { num: /^\d+$/ },
1064
+ stopPattern: /^итог/iu,
1065
+ totalsPatterns: {
1066
+ vat: /В том числе НДС/iu,
1067
+ total: /^Всего к оплате/iu
1068
+ }
1069
+ };
1070
+ exports2.PROFILES.push(PROFILE_DELTSOVA);
1071
+ var PROFILE_BRATOVA = {
1072
+ id: "bratova",
1073
+ layoutId: "onec-simple",
1074
+ title: "\u0418\u041F \u0411\u0440\u0430\u0442\u043E\u0432\u0430 \u041D. \u0421.",
1075
+ detect: [/(?<!\d)232018561037(?!\d)/u],
1076
+ columns: ONEC_DOCUMENT_COLUMNS,
1077
+ headerColumns: ONEC_DOCUMENT_HEADER,
1078
+ rowPattern: { num: /^\d+$/ },
1079
+ stopPattern: /^итог/iu,
1080
+ totalsPatterns: { total: /^Всего к оплате/iu }
1081
+ };
1082
+ exports2.PROFILES.push(PROFILE_BRATOVA);
1083
+ var PROFILE_LESKRAFT = {
1084
+ id: "leskraft",
1085
+ layoutId: "onec-code",
1086
+ title: "\u041E\u041E\u041E \xAB\u041B\u0415\u0421\u041A\u0420\u0410\u0424\u0422\xBB",
1087
+ detect: [/(?<!\d)2308205621(?!\d)/u],
1088
+ pricesIncludeVat: true,
1089
+ vatRate: 22,
1090
+ descriptionAbove: true,
1091
+ columns: [
1092
+ { key: "num", from: 0, to: 55 },
1093
+ { key: "code", from: 55, to: 100 },
1094
+ { key: "name", from: 100, to: 390 },
1095
+ { key: "qty", from: 390, to: 435 },
1096
+ { key: "unit", from: 435, to: 480 },
1097
+ { key: "price", from: 480, to: 525 },
1098
+ { key: "sum", from: 525, to: 620 }
1099
+ ],
1100
+ rowPattern: { num: /^\d+$/, code: /^УТ-?\d+$/iu },
1101
+ stopPattern: /^итог/iu,
1102
+ totalsPatterns: {
1103
+ vat: /В т\.ч\. НДС/iu,
1104
+ total: /^Итого с НДС/iu
1105
+ }
1106
+ };
1107
+ exports2.PROFILES.push(PROFILE_LESKRAFT);
1108
+ var PROFILE_MALINOVSKAYA = {
1109
+ id: "malinovskaya",
1110
+ layoutId: "onec-color",
1111
+ title: "\u0418\u041F \u041C\u0430\u043B\u0438\u043D\u043E\u0432\u0441\u043A\u0430\u044F \u0410. \u041F.",
1112
+ detect: [/(?<!\d)552503291108(?!\d)/u],
1113
+ columns: [
1114
+ { key: "num", from: 0, to: 55 },
1115
+ { key: "name", from: 55, to: 380 },
1116
+ { key: "qty", from: 380, to: 415 },
1117
+ { key: "unit", from: 415, to: 462 },
1118
+ { key: "price", from: 462, to: 520 },
1119
+ { key: "sum", from: 520, to: 620 }
1120
+ ],
1121
+ rowPattern: { num: /^\d+$/ },
1122
+ stopPattern: /^итог/iu,
1123
+ totalsPatterns: {
1124
+ sum: /^Итого:/iu,
1125
+ vat: /Сумма НДС/iu,
1126
+ total: /^Всего к оплате/iu
1127
+ }
1128
+ };
1129
+ exports2.PROFILES.push(PROFILE_MALINOVSKAYA);
1130
+ var PROFILE_KOROLEV = {
1131
+ id: "korneev",
1132
+ layoutId: "onec-term",
1133
+ title: "\u0418\u041F \u041A\u043E\u0440\u043D\u0435\u0435\u0432 \u041F. \u0410.",
1134
+ detect: [/(?<!\d)027716762358(?!\d)/u],
1135
+ columns: [
1136
+ { key: "num", from: 0, to: 60 },
1137
+ { key: "name", from: 60, to: 335 },
1138
+ { key: "qty", from: 335, to: 382 },
1139
+ { key: "unit", from: 382, to: 416 },
1140
+ { key: "price", from: 455, to: 500 },
1141
+ { key: "sum", from: 500, to: 620 }
1142
+ ],
1143
+ rowPattern: { num: /^\d+$/ },
1144
+ stopPattern: /^итог/iu,
1145
+ totalsPatterns: { total: /ВСЕГО К ОПЛАТЕ/iu }
1146
+ };
1147
+ exports2.PROFILES.push(PROFILE_KOROLEV);
1148
+ var PROFILE_BASHKIROVA = {
1149
+ id: "bashkirova",
1150
+ layoutId: "onec-service-above",
1151
+ title: "\u0418\u041F \u0411\u0430\u0448\u043A\u0438\u0440\u043E\u0432\u0430 \u0412. \u0410.",
1152
+ detect: [/(?<!\d)561504764263(?!\d)/u],
1153
+ descriptionAbove: true,
1154
+ columns: [
1155
+ { key: "num", from: 0, to: 35 },
1156
+ { key: "name", from: 35, to: 218 },
1157
+ { key: "qty", from: 218, to: 261 },
1158
+ { key: "unit", from: 261, to: 328 },
1159
+ { key: "price", from: 328, to: 526 },
1160
+ { key: "sum", from: 526, to: 620 }
1161
+ ],
1162
+ headerColumns: ONEC_DOCUMENT_HEADER,
1163
+ rowPattern: { num: /^\d+$/ },
1164
+ stopPattern: /^итог/iu,
1165
+ totalsPatterns: {
1166
+ sum: /^Итого:/iu,
1167
+ vat: /Сумма НДС/iu,
1168
+ total: /^Всего к оплате/iu
1169
+ }
1170
+ };
1171
+ exports2.PROFILES.push(PROFILE_BASHKIROVA);
1172
+ var PROFILE_SATURN_YUG = {
1173
+ id: "saturn-yug",
1174
+ layoutId: "onec-weight",
1175
+ title: "\u0410\u041E \xAB\u0421\u0410\u0422\u0423\u0420\u041D \u042E\u0413\xBB",
1176
+ detect: [/(?<!\d)2312254452(?!\d)/u],
1177
+ pricesIncludeVat: true,
1178
+ vatRate: 22,
1179
+ columns: [
1180
+ { key: "num", from: 0, to: 60 },
1181
+ { key: "code", from: 60, to: 105 },
1182
+ { key: "name", from: 105, to: 330 },
1183
+ { key: "qty", from: 330, to: 370 },
1184
+ { key: "unit", from: 370, to: 400 },
1185
+ { key: "price", from: 400, to: 460 },
1186
+ { key: "sum", from: 460, to: 510 }
1187
+ ],
1188
+ rowPattern: { num: /^\d+$/ },
1189
+ stopPattern: /^итог/iu,
1190
+ totalsPatterns: {
1191
+ vat: /В том числе НДС/iu,
1192
+ total: /^Итого\s*:/iu
1193
+ }
1194
+ };
1195
+ exports2.PROFILES.push(PROFILE_SATURN_YUG);
1196
+ var PROFILE_INVISIBLE = {
1197
+ id: "invisible",
1198
+ layoutId: "onec-price-before-qty",
1199
+ title: "\u041E\u041E\u041E \xAB\u0418\u041D\u0412\u0418\u0417\u0418\u0411\u041B-\u0414\u041E\u0420\u0421\xBB",
1200
+ detect: [/(?<!\d)9723136786(?!\d)/u],
1201
+ descriptionAbove: true,
1202
+ pricesIncludeVat: true,
1203
+ vatRate: 22,
1204
+ columns: [
1205
+ { key: "num", from: 0, to: 60 },
1206
+ { key: "name", from: 60, to: 350 },
1207
+ { key: "price", from: 350, to: 400 },
1208
+ { key: "qty", from: 400, to: 450 },
1209
+ { key: "unit", from: 480, to: 500 },
1210
+ { key: "sum", from: 500, to: 620 }
1211
+ ],
1212
+ rowPattern: { num: /^\d+$/ },
1213
+ stopPattern: /^итог/iu,
1214
+ totalsPatterns: {
1215
+ vat: /В том числе НДС/iu,
1216
+ total: /^Итого\s*:/iu
1217
+ }
1218
+ };
1219
+ exports2.PROFILES.push(PROFILE_INVISIBLE);
1220
+ var PROFILE_BAKHAREVA = {
1221
+ id: "bakhareva",
1222
+ layoutId: "onec-packed",
1223
+ title: "\u0418\u041F \u0411\u0430\u0445\u0430\u0440\u0435\u0432\u0430 \u041A. \u0413.",
1224
+ detect: [/(?<!\d)232015903973(?!\d)/u],
1225
+ qtyPriceInOneColumn: true,
1226
+ columns: [
1227
+ { key: "num", from: 0, to: 25 },
1228
+ { key: "name", from: 25, to: 300 },
1229
+ { key: "qty", from: 300, to: 415 },
1230
+ { key: "vat", from: 415, to: 439 },
1231
+ { key: "vatflag", from: 439, to: 495 },
1232
+ { key: "sum", from: 495, to: 620 }
1233
+ ],
1234
+ rowPattern: { num: /^\d+$/ },
1235
+ stopPattern: /^итог/iu,
1236
+ totalsPatterns: {
1237
+ sum: /^Итого:/iu,
1238
+ total: /^Итого:/iu
1239
+ }
1240
+ };
1241
+ exports2.PROFILES.push(PROFILE_BAKHAREVA);
1242
+ var PROFILE_BAS = {
1243
+ id: "bas-engineering",
1244
+ layoutId: "onec-number-above",
1245
+ title: "\u041E\u041E\u041E \xAB\u0411\u0410\u0421 \u0418\u041D\u0416\u0418\u041D\u0418\u0420\u0418\u041D\u0413 \u0413\u0420\u0423\u041F\u041F\xBB",
1246
+ detect: [/(?<!\d)2366050350(?!\d)/u],
1247
+ numberAbove: true,
1248
+ pricesIncludeVat: true,
1249
+ vatRate: 22,
1250
+ columns: [
1251
+ { key: "num", from: 0, to: 68 },
1252
+ { key: "name", from: 68, to: 335 },
1253
+ { key: "price", from: 335, to: 371 },
1254
+ { key: "qty", from: 371, to: 410 },
1255
+ { key: "unit", from: 410, to: 467 },
1256
+ { key: "sum", from: 467, to: 620 }
1257
+ ],
1258
+ rowPattern: { num: /^\d+$/ },
1259
+ stopPattern: /^итог/iu,
1260
+ totalsPatterns: {
1261
+ vat: /В том числе НДС/iu,
1262
+ total: /^Итого\s*:/iu
1263
+ }
1264
+ };
1265
+ exports2.PROFILES.push(PROFILE_BAS);
940
1266
  }
941
1267
  });
942
1268
 
@@ -85670,6 +85996,8 @@ exports.rowCells = rowCells;
85670
85996
  exports.layoutScore = layoutScore;
85671
85997
  exports.detectProfile = detectProfile;
85672
85998
  exports.detectCandidates = detectCandidates;
85999
+ exports.headerColumns = headerColumns;
86000
+ exports.resolveProfileColumns = resolveProfileColumns;
85673
86001
  exports.parseRows = parseRows;
85674
86002
  exports.parseInvoicePdf = parseInvoicePdf;
85675
86003
  var node_path_1 = __importDefault(require("node:path"));
@@ -85845,6 +86173,137 @@ function detectProfile(text) {
85845
86173
  function detectCandidates(text) {
85846
86174
  return profiles_1.PROFILES.filter((profile) => profile.detect.some((re) => re.test(text)));
85847
86175
  }
86176
+ var HEADER_REQUIRED = ["name", "qty", "price", "sum"];
86177
+ var roundCoordinate = (value) => Math.round(value * 100) / 100;
86178
+ var HEADER_CELL_GAP = 1.5;
86179
+ var HEADER_CONTEXT_GAP = 8;
86180
+ var HEADER_WINDOW = 4;
86181
+ function headerCells(row) {
86182
+ const cells = [];
86183
+ for (const item of row.items) {
86184
+ const text = item.str.trim();
86185
+ if (!text)
86186
+ continue;
86187
+ const previous = cells[cells.length - 1];
86188
+ const gap = previous ? item.x - (previous.x + previous.w) : Number.POSITIVE_INFINITY;
86189
+ if (previous && gap <= HEADER_CELL_GAP) {
86190
+ previous.text += `${gap > 1 ? " " : ""}${text}`;
86191
+ previous.w = item.x + (item.w || 0) - previous.x;
86192
+ continue;
86193
+ }
86194
+ cells.push({ x: item.x, w: item.w || 0, text });
86195
+ }
86196
+ return cells;
86197
+ }
86198
+ function windowText(cells, start, end) {
86199
+ const parts = [];
86200
+ for (let index = start; index <= end; index += 1) {
86201
+ const cell = cells[index];
86202
+ if (!cell)
86203
+ return void 0;
86204
+ if (index > start) {
86205
+ const previous = cells[index - 1];
86206
+ if (!previous || cell.x - (previous.x + previous.w) > HEADER_CONTEXT_GAP)
86207
+ return void 0;
86208
+ }
86209
+ parts.push(cell.text);
86210
+ }
86211
+ return parts.join(" ");
86212
+ }
86213
+ function findLabel(cells, pattern, exclude) {
86214
+ for (let start = cells.length - 1; start >= 0; start -= 1) {
86215
+ for (let end = Math.min(start + HEADER_WINDOW - 1, cells.length - 1); end >= start; end -= 1) {
86216
+ const text = windowText(cells, start, end);
86217
+ const first = cells[start];
86218
+ const last = cells[end];
86219
+ if (text === void 0 || !first || !last || !pattern.test(text))
86220
+ continue;
86221
+ const extended = windowText(cells, start, end + 1);
86222
+ if (exclude && extended !== void 0 && exclude.test(extended))
86223
+ continue;
86224
+ return { x: first.x, w: last.x + last.w - first.x };
86225
+ }
86226
+ }
86227
+ return void 0;
86228
+ }
86229
+ function headerColumns(rows, spec) {
86230
+ var _a, _b;
86231
+ const labels = Object.entries(spec.labels).filter(([, pattern]) => Boolean(pattern));
86232
+ for (const row of rows) {
86233
+ const cells = headerCells(row);
86234
+ const found = /* @__PURE__ */ new Map();
86235
+ for (const [key, pattern] of labels) {
86236
+ const span = findLabel(cells, pattern, (_a = spec.exclude) === null || _a === void 0 ? void 0 : _a[key]);
86237
+ if (span)
86238
+ found.set(key, span);
86239
+ }
86240
+ if (!HEADER_REQUIRED.every((key) => found.has(key)))
86241
+ continue;
86242
+ const ordered = [...found.entries()].sort((a, b) => a[1].x - b[1].x);
86243
+ let overlapped = false;
86244
+ for (let index = 1; index < ordered.length; index += 1) {
86245
+ const previous = ordered[index - 1];
86246
+ const current = ordered[index];
86247
+ if (previous && current && current[1].x < previous[1].x + previous[1].w - 1e-3) {
86248
+ overlapped = true;
86249
+ break;
86250
+ }
86251
+ }
86252
+ if (overlapped)
86253
+ continue;
86254
+ const insideSpan = (cell) => ordered.some(([, span]) => cell.x >= span.x - 0.01 && cell.x + cell.w <= span.x + span.w + 0.01);
86255
+ const segments = [];
86256
+ for (const [key, span] of ordered)
86257
+ segments.push({ key, x: span.x, w: span.w });
86258
+ for (const cell of cells)
86259
+ if (!insideSpan(cell))
86260
+ segments.push({ x: cell.x, w: cell.w });
86261
+ segments.sort((a, b) => a.x - b.x);
86262
+ const boundaryBetween = (left, right) => {
86263
+ if (left.key === "num")
86264
+ return left.x + left.w + 8;
86265
+ if (left.key === "name")
86266
+ return right.x - 2;
86267
+ return (left.x + left.w + right.x) / 2;
86268
+ };
86269
+ const columns = [];
86270
+ for (let index = 0; index < segments.length; index += 1) {
86271
+ const segment = segments[index];
86272
+ if (!(segment === null || segment === void 0 ? void 0 : segment.key))
86273
+ continue;
86274
+ const previous = segments[index - 1];
86275
+ const next = segments[index + 1];
86276
+ const from = previous ? boundaryBetween(previous, segment) : 0;
86277
+ const to = next ? boundaryBetween(segment, next) : (_b = spec.endX) !== null && _b !== void 0 ? _b : 620;
86278
+ if (to > from)
86279
+ columns.push({ key: segment.key, from: roundCoordinate(from), to: roundCoordinate(to) });
86280
+ }
86281
+ let columnsOverlap = false;
86282
+ for (let index = 1; index < columns.length; index += 1) {
86283
+ const previous = columns[index - 1];
86284
+ const current = columns[index];
86285
+ if (previous && current && current.from < previous.to - 1e-3) {
86286
+ columnsOverlap = true;
86287
+ break;
86288
+ }
86289
+ }
86290
+ if (columnsOverlap)
86291
+ continue;
86292
+ if (columns.length === ordered.length)
86293
+ return columns;
86294
+ }
86295
+ return void 0;
86296
+ }
86297
+ function resolveProfileColumns(rows, profile) {
86298
+ if (!profile.headerColumns)
86299
+ return profile;
86300
+ const columns = headerColumns(rows, profile.headerColumns);
86301
+ if (!columns)
86302
+ return profile;
86303
+ const resolved = { ...profile, columns };
86304
+ delete resolved.headerColumns;
86305
+ return resolved;
86306
+ }
85848
86307
  function isRepeatedLine(previous, line) {
85849
86308
  const nameMatches = !previous.name || !line.name || previous.name === line.name;
85850
86309
  return previous.num === line.num && nameMatches && previous.qty === line.qty && previous.price === line.price && previous.sum === line.sum;
@@ -85856,13 +86315,19 @@ function isContinuation(prev, row) {
85856
86315
  return prev.y - row.y <= lineHeight * 2.5;
85857
86316
  }
85858
86317
  function parseRows(rows, profile, layoutScoreValue = 1, isUniversal = false) {
85859
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
86318
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
85860
86319
  const lines = [];
85861
86320
  const warnings = [];
85862
86321
  const unparsedRows = [];
86322
+ const headerExpected = Boolean(profile.headerColumns);
86323
+ profile = resolveProfileColumns(rows, profile);
86324
+ if (headerExpected && profile.headerColumns) {
86325
+ warnings.push(`\u0428\u0430\u043F\u043A\u0430 \u0442\u0430\u0431\u043B\u0438\u0446\u044B \u043D\u0435 \u043D\u0430\u0439\u0434\u0435\u043D\u0430: \u0438\u0441\u043F\u043E\u043B\u044C\u0437\u0443\u044E\u0442\u0441\u044F \u0437\u0430\u043F\u0430\u0441\u043D\u044B\u0435 \u0433\u0440\u0430\u043D\u0438\u0446\u044B \u043F\u0440\u043E\u0444\u0438\u043B\u044F ${profile.layoutId}`);
86326
+ }
85863
86327
  let current;
85864
86328
  let currentRow;
85865
86329
  let pending = [];
86330
+ let lastNumbered;
85866
86331
  const flushPending = () => {
85867
86332
  var _a2, _b2;
85868
86333
  for (const entry of pending) {
@@ -85879,16 +86344,32 @@ function parseRows(rows, profile, layoutScoreValue = 1, isUniversal = false) {
85879
86344
  }
85880
86345
  pending = [];
85881
86346
  };
86347
+ const flushLastNumbered = () => {
86348
+ if (!lastNumbered)
86349
+ return;
86350
+ for (const sourceRow of lastNumbered.rows)
86351
+ unparsedRows.push(rowText(sourceRow));
86352
+ lastNumbered = void 0;
86353
+ };
85882
86354
  for (const row of rows) {
85883
86355
  const text = rowText(row);
85884
86356
  if (!text)
85885
86357
  continue;
85886
86358
  if (profile.stopPattern.test(text)) {
85887
86359
  flushPending();
86360
+ flushLastNumbered();
85888
86361
  current = void 0;
85889
86362
  continue;
85890
86363
  }
85891
86364
  const cells = rowCells(row, profile);
86365
+ if (profile.qtyPriceInOneColumn && cells.qty) {
86366
+ const match = /^([\d\s.,]+)\s+([^\d\s].*?)\s+([\d\s.,]+)$/.exec(cells.qty.trim());
86367
+ if ((match === null || match === void 0 ? void 0 : match[1]) && match[2] && match[3]) {
86368
+ cells.qty = match[1].replace(/\s+/g, "");
86369
+ cells.unit = match[2].trim();
86370
+ cells.price = match[3];
86371
+ }
86372
+ }
85892
86373
  if (profile.qtyUnitInOneColumn && cells.qty) {
85893
86374
  const match = /^([\d\s.,]+)\s*([^\d\s].*)$/.exec(cells.qty.trim());
85894
86375
  if ((match === null || match === void 0 ? void 0 : match[1]) && match[2]) {
@@ -85896,24 +86377,50 @@ function parseRows(rows, profile, layoutScoreValue = 1, isUniversal = false) {
85896
86377
  cells.unit = match[2].trim();
85897
86378
  }
85898
86379
  }
85899
- const { num, code } = cells;
85900
86380
  const priceValue = number(cells.price);
85901
86381
  const hasQuantity = number(cells.qty) > 0;
85902
86382
  const serviceLike = priceValue > 0 && Math.abs(priceValue - number(cells.sum)) < 5e-3;
86383
+ const numericRow = hasQuantity && priceValue > 0 && number(cells.sum) > 0;
86384
+ if (profile.numberAbove) {
86385
+ const previousNumbered = lastNumbered && lastNumbered.lastRow.page === row.page && lastNumbered.lastRow.y - row.y <= 25 ? lastNumbered : void 0;
86386
+ if (!cells.num && !numericRow && cells.name && previousNumbered) {
86387
+ previousNumbered.cells.name = `${(_a = previousNumbered.cells.name) !== null && _a !== void 0 ? _a : ""} ${(_b = cells.name) !== null && _b !== void 0 ? _b : ""}`.replace(/\s+/g, " ").trim();
86388
+ if (cells.code)
86389
+ previousNumbered.cells.code = cells.code;
86390
+ previousNumbered.rows.push(row);
86391
+ previousNumbered.lastRow = row;
86392
+ continue;
86393
+ }
86394
+ if (!cells.num && numericRow && previousNumbered) {
86395
+ cells.num = (_c = previousNumbered.cells.num) !== null && _c !== void 0 ? _c : "";
86396
+ cells.name = `${(_d = previousNumbered.cells.name) !== null && _d !== void 0 ? _d : ""} ${(_e = cells.name) !== null && _e !== void 0 ? _e : ""}`.replace(/\s+/g, " ").trim();
86397
+ lastNumbered = void 0;
86398
+ } else {
86399
+ flushLastNumbered();
86400
+ if (cells.num && !numericRow && !serviceLike) {
86401
+ lastNumbered = { rows: [row], cells, lastRow: row };
86402
+ continue;
86403
+ }
86404
+ }
86405
+ }
86406
+ const { num, code } = cells;
85903
86407
  const codePattern = profile.rowPattern.code;
86408
+ const nameText = (_g = (_f = cells.name) === null || _f === void 0 ? void 0 : _f.trim()) !== null && _g !== void 0 ? _g : "";
86409
+ const numericName = /^[\d\s.,:;()-]+$/u.test(nameText);
86410
+ const hasUsableName = Boolean(nameText) && !numericName;
85904
86411
  const pendingCode = profile.descriptionAbove ? pending.find((entry) => {
85905
86412
  var _a2, _b2, _c2;
85906
86413
  return entry.row.page === row.page && entry.row.y - row.y <= 20 && Boolean((_a2 = entry.cells.code) === null || _a2 === void 0 ? void 0 : _a2.trim()) && (!codePattern || codePattern.test((_c2 = (_b2 = entry.cells.code) === null || _b2 === void 0 ? void 0 : _b2.trim()) !== null && _c2 !== void 0 ? _c2 : ""));
85907
86414
  }) : void 0;
85908
- const isItem = Boolean(num) && Number.parseInt(num !== null && num !== void 0 ? num : "", 10) > 0 && profile.rowPattern.num.test(num !== null && num !== void 0 ? num : "") && (!codePattern || Boolean(code) && codePattern.test(code !== null && code !== void 0 ? code : "") || pendingCode !== void 0) && (Boolean(codePattern) || Boolean((_a = cells.name) === null || _a === void 0 ? void 0 : _a.trim()) || hasQuantity) && (Boolean(codePattern) || hasQuantity || serviceLike);
86415
+ const isItem = Boolean(num) && Number.parseInt(num !== null && num !== void 0 ? num : "", 10) > 0 && profile.rowPattern.num.test(num !== null && num !== void 0 ? num : "") && (!codePattern || Boolean(code) && codePattern.test(code !== null && code !== void 0 ? code : "") || pendingCode !== void 0) && (Boolean(codePattern) || hasUsableName || hasQuantity && !nameText) && (Boolean(codePattern) || hasQuantity || serviceLike);
85909
86416
  if (isItem) {
85910
86417
  if (profile.descriptionAbove && pending.length) {
85911
86418
  const near = pending.filter((entry) => entry.row.page === row.page && entry.row.y - row.y <= 20);
85912
86419
  for (const entry of [...near].reverse()) {
85913
- const name = (_b = entry.cells.name) === null || _b === void 0 ? void 0 : _b.trim();
85914
- const aboveCode = (_c = entry.cells.code) === null || _c === void 0 ? void 0 : _c.trim();
86420
+ const name = (_h = entry.cells.name) === null || _h === void 0 ? void 0 : _h.trim();
86421
+ const aboveCode = (_j = entry.cells.code) === null || _j === void 0 ? void 0 : _j.trim();
85915
86422
  if (name)
85916
- cells.name = `${name} ${(_d = cells.name) !== null && _d !== void 0 ? _d : ""}`.replace(/\s+/g, " ").trim();
86423
+ cells.name = `${name} ${(_k = cells.name) !== null && _k !== void 0 ? _k : ""}`.replace(/\s+/g, " ").trim();
85917
86424
  if (aboveCode && !cells.code)
85918
86425
  cells.code = aboveCode;
85919
86426
  }
@@ -85922,11 +86429,11 @@ function parseRows(rows, profile, layoutScoreValue = 1, isUniversal = false) {
85922
86429
  }
85923
86430
  const line = {
85924
86431
  num: Number.parseInt(num !== null && num !== void 0 ? num : "", 10),
85925
- code: (_e = cells.code) !== null && _e !== void 0 ? _e : "",
85926
- name: (_f = cells.name) !== null && _f !== void 0 ? _f : "",
85927
- article: (profile.articleFromCode ? (_g = cells.code) !== null && _g !== void 0 ? _g : "" : (_h = cells.art) !== null && _h !== void 0 ? _h : "").replace(/^(?:[\d\s]+)$/, (value) => value.replace(/\s+/g, "")),
85928
- productType: ((_j = cells.type) !== null && _j !== void 0 ? _j : "").trim(),
85929
- unit: (_k = cells.unit) !== null && _k !== void 0 ? _k : "",
86432
+ code: (_l = cells.code) !== null && _l !== void 0 ? _l : "",
86433
+ name: (_m = cells.name) !== null && _m !== void 0 ? _m : "",
86434
+ article: (profile.articleFromCode ? (_o = cells.code) !== null && _o !== void 0 ? _o : "" : (_p = cells.art) !== null && _p !== void 0 ? _p : "").replace(/^(?:[\d\s]+)$/, (value) => value.replace(/\s+/g, "")),
86435
+ productType: ((_q = cells.type) !== null && _q !== void 0 ? _q : "").trim(),
86436
+ unit: (_r = cells.unit) !== null && _r !== void 0 ? _r : "",
85930
86437
  vat: number(cells.vat),
85931
86438
  qty: number(cells.qty),
85932
86439
  price: number(cells.price),
@@ -85960,6 +86467,7 @@ function parseRows(rows, profile, layoutScoreValue = 1, isUniversal = false) {
85960
86467
  unparsedRows.push(text);
85961
86468
  }
85962
86469
  flushPending();
86470
+ flushLastNumbered();
85963
86471
  if (profile.articleFromCode) {
85964
86472
  for (const line of lines) {
85965
86473
  line.article = line.code.replace(/^(?:[\d\s]+)$/, (value) => value.replace(/\s+/g, ""));
@@ -85971,7 +86479,7 @@ function parseRows(rows, profile, layoutScoreValue = 1, isUniversal = false) {
85971
86479
  const text = rowText(row);
85972
86480
  if (!profile.totalsRow.test(text))
85973
86481
  continue;
85974
- const numbers = (_l = text.match(/[\d\s]+[.,]\d{2}/g)) !== null && _l !== void 0 ? _l : [];
86482
+ const numbers = (_s = text.match(/[\d\s]+[.,]\d{2}/g)) !== null && _s !== void 0 ? _s : [];
85975
86483
  const first = numbers[0];
85976
86484
  const last = numbers[numbers.length - 1];
85977
86485
  if (first && last) {
@@ -86047,9 +86555,9 @@ function parseRows(rows, profile, layoutScoreValue = 1, isUniversal = false) {
86047
86555
  }
86048
86556
  const totals = {
86049
86557
  qty: summed.qty,
86050
- sum: (_m = documented.sum) !== null && _m !== void 0 ? _m : summed.sum,
86051
- vat: (_o = documented.vat) !== null && _o !== void 0 ? _o : summed.vat,
86052
- total: (_p = documented.total) !== null && _p !== void 0 ? _p : summed.total
86558
+ sum: (_t = documented.sum) !== null && _t !== void 0 ? _t : summed.sum,
86559
+ vat: (_u = documented.vat) !== null && _u !== void 0 ? _u : summed.vat,
86560
+ total: (_v = documented.total) !== null && _v !== void 0 ? _v : summed.total
86053
86561
  };
86054
86562
  if (!isUniversal && layoutScoreValue < 0.85) {
86055
86563
  warnings.push(`\u041C\u0430\u043A\u0435\u0442 \u043D\u0435 \u0441\u043E\u0432\u043F\u0430\u043B \u0441 \u043F\u0440\u043E\u0444\u0438\u043B\u0435\u043C ${profile.layoutId}: \u0441\u043E\u0432\u043F\u0430\u0434\u0435\u043D\u0438\u0435 ${(layoutScoreValue * 100).toFixed(1)}%, \u043F\u0440\u043E\u0432\u0435\u0440\u044C\u0442\u0435 \u0432\u0451\u0440\u0441\u0442\u043A\u0443 \u0441\u0447\u0451\u0442\u0430`);
@@ -86105,8 +86613,9 @@ async function parseInvoicePdf(data, profileId = "auto", yTolerance = 2) {
86105
86613
  let best;
86106
86614
  for (const candidate of candidates) {
86107
86615
  const { rows: rows2 } = await load((_a = candidate.yTolerance) !== null && _a !== void 0 ? _a : yTolerance);
86108
- const score2 = layoutScore(rows2, candidate);
86109
- const parsed = parseRows(rows2, candidate, score2);
86616
+ const resolved2 = resolveProfileColumns(rows2, candidate);
86617
+ const score2 = layoutScore(rows2, resolved2);
86618
+ const parsed = parseRows(rows2, resolved2, score2);
86110
86619
  const consistent = parsed.lines.filter((line) => {
86111
86620
  const expected = candidate.pricesIncludeVat ? line.total : line.sum;
86112
86621
  return line.qty > 0 && line.price > 0 && Math.abs(line.qty * line.price - expected) <= Math.max(0.05, expected * 2e-3);
@@ -86123,8 +86632,9 @@ async function parseInvoicePdf(data, profileId = "auto", yTolerance = 2) {
86123
86632
  profile = (_c = profiles_1.PROFILES.find((p) => p.id === profileId)) !== null && _c !== void 0 ? _c : profiles_1.UNIVERSAL;
86124
86633
  }
86125
86634
  const { rows, pages } = await load((_d = profile.yTolerance) !== null && _d !== void 0 ? _d : yTolerance);
86126
- const score = profile === profiles_1.UNIVERSAL ? 0 : layoutScore(rows, profile);
86127
- const result = parseRows(rows, profile, profile === profiles_1.UNIVERSAL ? 1 : score, profile === profiles_1.UNIVERSAL);
86635
+ const resolved = resolveProfileColumns(rows, profile);
86636
+ const score = profile === profiles_1.UNIVERSAL ? 0 : layoutScore(rows, resolved);
86637
+ const result = parseRows(rows, resolved, profile === profiles_1.UNIVERSAL ? 1 : score, profile === profiles_1.UNIVERSAL);
86128
86638
  result.pages = pages;
86129
86639
  return { ...result, text };
86130
86640
  }