pakdatakit 2.3.0 → 2.5.0

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.
Files changed (96) hide show
  1. package/DATA.md +212 -3
  2. package/LICENSE +1 -1
  3. package/NOTICE +14 -0
  4. package/README.md +304 -5
  5. package/dist/address.cjs +5 -4
  6. package/dist/address.js +3 -2
  7. package/dist/bills.cjs +19 -0
  8. package/dist/bills.d.cts +63 -0
  9. package/dist/bills.d.ts +63 -0
  10. package/dist/bills.js +2 -0
  11. package/dist/{chunk-OKMBYX3F.cjs → chunk-33GOK7EE.cjs} +2 -910
  12. package/dist/chunk-34U2ZMHR.js +194 -0
  13. package/dist/chunk-4GSBHJX6.js +165 -0
  14. package/dist/chunk-5DDHO5Z7.cjs +565 -0
  15. package/dist/chunk-5EY5DQS7.js +51 -0
  16. package/dist/chunk-5RGX4DWF.js +559 -0
  17. package/dist/chunk-5UCRHVI4.cjs +302 -0
  18. package/dist/{chunk-TQP2T33V.js → chunk-B7DOQ5QF.js} +2 -1
  19. package/dist/chunk-CF6AMRBU.cjs +198 -0
  20. package/dist/chunk-CZOZB4K3.js +61 -0
  21. package/dist/chunk-E2XU6NOW.js +909 -0
  22. package/dist/chunk-E6GWBOKV.cjs +911 -0
  23. package/dist/chunk-EA3SIHYT.js +313 -0
  24. package/dist/{chunk-YTSMFGTW.js → chunk-IDTRFOQW.js} +8 -3
  25. package/dist/chunk-OTI7HY55.js +297 -0
  26. package/dist/{chunk-J5ARB4TE.cjs → chunk-QFPEFD67.cjs} +5 -4
  27. package/dist/chunk-SF4ECUZ6.cjs +56 -0
  28. package/dist/chunk-UYFTXD4P.js +168 -0
  29. package/dist/{chunk-TBTWZC74.cjs → chunk-XCFZKCV2.cjs} +8 -3
  30. package/dist/{chunk-N4S22XX3.js → chunk-XOTCIXY6.js} +2 -909
  31. package/dist/chunk-XVEG53NN.cjs +322 -0
  32. package/dist/chunk-YDUXIENX.cjs +172 -0
  33. package/dist/chunk-YPAWNWFD.cjs +168 -0
  34. package/dist/chunk-YQFI7W3I.cjs +65 -0
  35. package/dist/class-validator.cjs +16 -8
  36. package/dist/class-validator.d.cts +7 -1
  37. package/dist/class-validator.d.ts +7 -1
  38. package/dist/class-validator.js +15 -8
  39. package/dist/cli.js +1 -1
  40. package/dist/courier.cjs +16 -0
  41. package/dist/courier.d.cts +51 -0
  42. package/dist/courier.d.ts +51 -0
  43. package/dist/courier.js +3 -0
  44. package/dist/fbr-invoice.cjs +25 -0
  45. package/dist/fbr-invoice.d.cts +170 -0
  46. package/dist/fbr-invoice.d.ts +170 -0
  47. package/dist/fbr-invoice.js +4 -0
  48. package/dist/geo.cjs +11 -10
  49. package/dist/geo.js +2 -1
  50. package/dist/incometax.cjs +27 -0
  51. package/dist/incometax.d.cts +91 -0
  52. package/dist/incometax.d.ts +91 -0
  53. package/dist/incometax.js +2 -0
  54. package/dist/index.cjs +227 -123
  55. package/dist/index.d.cts +8 -2
  56. package/dist/index.d.ts +8 -2
  57. package/dist/index.js +21 -13
  58. package/dist/input.cjs +8 -8
  59. package/dist/input.js +2 -2
  60. package/dist/joi.cjs +14 -8
  61. package/dist/joi.d.cts +1 -0
  62. package/dist/joi.d.ts +1 -0
  63. package/dist/joi.js +13 -7
  64. package/dist/quality.cjs +25 -0
  65. package/dist/quality.d.cts +62 -0
  66. package/dist/quality.d.ts +62 -0
  67. package/dist/quality.js +8 -0
  68. package/dist/salestax.cjs +20 -0
  69. package/dist/salestax.d.cts +106 -0
  70. package/dist/salestax.d.ts +106 -0
  71. package/dist/salestax.js +3 -0
  72. package/dist/superstruct.cjs +14 -8
  73. package/dist/superstruct.d.cts +1 -0
  74. package/dist/superstruct.d.ts +1 -0
  75. package/dist/superstruct.js +13 -7
  76. package/dist/text.cjs +21 -5
  77. package/dist/text.d.cts +90 -1
  78. package/dist/text.d.ts +90 -1
  79. package/dist/text.js +1 -1
  80. package/dist/valibot.cjs +14 -8
  81. package/dist/valibot.d.cts +1 -0
  82. package/dist/valibot.d.ts +1 -0
  83. package/dist/valibot.js +13 -7
  84. package/dist/yup.cjs +14 -8
  85. package/dist/yup.d.cts +1 -0
  86. package/dist/yup.d.ts +1 -0
  87. package/dist/yup.js +13 -7
  88. package/dist/zod.cjs +14 -8
  89. package/dist/zod.d.cts +1 -0
  90. package/dist/zod.d.ts +1 -0
  91. package/dist/zod.js +13 -7
  92. package/package.json +68 -5
  93. package/dist/chunk-JKE4AMSZ.js +0 -47
  94. package/dist/chunk-KEHCTYLO.cjs +0 -52
  95. package/dist/{chunk-A3HBEBXY.js → chunk-7DY26QPN.js} +1 -1
  96. package/dist/{chunk-FRBPAGEJ.cjs → chunk-TVL6RDAU.cjs} +1 -1
@@ -0,0 +1,313 @@
1
+ import { normalizeWhitespace, toAsciiDigits, lazy } from './chunk-ENQ7O7VW.js';
2
+
3
+ // src/text.ts
4
+ var URDU_DIGITS = "\u06F0\u06F1\u06F2\u06F3\u06F4\u06F5\u06F6\u06F7\u06F8\u06F9";
5
+ var ROMAN_URDU_REPLACEMENTS = [
6
+ [/\bkia\b/gi, "kya"],
7
+ [/\bker\b/gi, "kar"],
8
+ [/\bkr\b/gi, "kar"],
9
+ [/\bhy\b/gi, "hai"],
10
+ [/\bmujhay\b/gi, "mujhe"],
11
+ [/\bap\b/gi, "aap"],
12
+ [/\bshukria\b/gi, "shukriya"],
13
+ [/\bnhi\b/gi, "nahi"],
14
+ [/\bassalamu alaikum\b/gi, "assalamualaikum"],
15
+ [/\bassalam o alaikum\b/gi, "assalamualaikum"]
16
+ ];
17
+ function normalizeText(text) {
18
+ return normalizeWhitespace(text.normalize("NFC"));
19
+ }
20
+ function normalizeRomanUrdu(text) {
21
+ let result = normalizeText(toEnglishDigits(text));
22
+ for (const [pattern, replacement] of ROMAN_URDU_REPLACEMENTS) {
23
+ result = result.replace(pattern, (match) => matchCase(match, replacement));
24
+ }
25
+ return result;
26
+ }
27
+ function toUrduDigits(text) {
28
+ return toAsciiDigits(text).replace(
29
+ /\d/g,
30
+ (digit) => URDU_DIGITS[Number(digit)]
31
+ );
32
+ }
33
+ function toEnglishDigits(text) {
34
+ return toAsciiDigits(text);
35
+ }
36
+ function matchCase(original, replacement) {
37
+ if (original.length > 1 && original === original.toUpperCase()) {
38
+ return original;
39
+ }
40
+ const first = original[0];
41
+ if (first !== first.toLowerCase()) {
42
+ return replacement[0]?.toUpperCase() + replacement.slice(1);
43
+ }
44
+ return replacement;
45
+ }
46
+ var SKIPPED = /[\u0300-\u036f\u0610-\u061a\u064b-\u065f\u0670\u06d6-\u06ed\u061c\u0640\u200b-\u200f\u202a-\u202e\u2066-\u2069\ufeff]/u;
47
+ function isArabicLetter(cp) {
48
+ return cp >= 1569 && cp <= 1610 && cp !== 1600 || cp >= 1646 && cp <= 1747 || cp === 1749 || cp >= 1786 && cp <= 1791;
49
+ }
50
+ var URDU_FOR_ARABIC = {
51
+ \u0643: "\u06A9",
52
+ \u064A: "\u06CC",
53
+ \u0649: "\u06CC"
54
+ };
55
+ var LATIN1_FOLD = {
56
+ \u00E0: "a",
57
+ \u00E1: "a",
58
+ \u00E2: "a",
59
+ \u00E3: "a",
60
+ \u00E4: "a",
61
+ \u00E5: "a",
62
+ \u00E6: "ae",
63
+ \u00E7: "c",
64
+ \u00E8: "e",
65
+ \u00E9: "e",
66
+ \u00EA: "e",
67
+ \u00EB: "e",
68
+ \u00EC: "i",
69
+ \u00ED: "i",
70
+ \u00EE: "i",
71
+ \u00EF: "i",
72
+ \u00F0: "d",
73
+ \u00F1: "n",
74
+ \u00F2: "o",
75
+ \u00F3: "o",
76
+ \u00F4: "o",
77
+ \u00F5: "o",
78
+ \u00F6: "o",
79
+ \u00F8: "o",
80
+ \u00F9: "u",
81
+ \u00FA: "u",
82
+ \u00FB: "u",
83
+ \u00FC: "u",
84
+ \u00FD: "y",
85
+ \u00FF: "y",
86
+ \u00FE: "th",
87
+ \u00DF: "ss"
88
+ };
89
+ function slugify(input, options = {}) {
90
+ if (typeof input !== "string") return "";
91
+ const script = slugScript(options);
92
+ const keepLatin = script !== "urdu";
93
+ const keepUrdu = script !== "latin";
94
+ let out = "";
95
+ let pending = false;
96
+ const push = (text) => {
97
+ if (pending && out.length > 0) out += "-";
98
+ pending = false;
99
+ out += text;
100
+ };
101
+ for (const char of toAsciiDigits(input)) {
102
+ if (SKIPPED.test(char)) continue;
103
+ const cp = char.codePointAt(0);
104
+ if (cp >= 48 && cp <= 57) {
105
+ push(char);
106
+ continue;
107
+ }
108
+ if (keepLatin) {
109
+ if (cp >= 97 && cp <= 122) {
110
+ push(char);
111
+ continue;
112
+ }
113
+ if (cp >= 65 && cp <= 90) {
114
+ push(String.fromCharCode(cp + 32));
115
+ continue;
116
+ }
117
+ const lower = cp >= 192 && cp <= 222 && cp !== 215 ? String.fromCharCode(cp + 32) : char;
118
+ const folded = LATIN1_FOLD[lower];
119
+ if (folded !== void 0) {
120
+ push(folded);
121
+ continue;
122
+ }
123
+ }
124
+ if (keepUrdu && isArabicLetter(cp)) {
125
+ push(URDU_FOR_ARABIC[char] ?? char);
126
+ continue;
127
+ }
128
+ pending = true;
129
+ }
130
+ return out;
131
+ }
132
+ function slugScript(options) {
133
+ if (typeof options !== "object" || options === null) return "any";
134
+ const script = options.script;
135
+ return script === "latin" || script === "urdu" ? script : "any";
136
+ }
137
+ var URDU_ORDER = [
138
+ "\u0627",
139
+ "\u0622",
140
+ "\u0628",
141
+ "\u0628\u06BE",
142
+ "\u067E",
143
+ "\u067E\u06BE",
144
+ "\u062A",
145
+ "\u062A\u06BE",
146
+ "\u0679",
147
+ "\u0679\u06BE",
148
+ "\u062B",
149
+ "\u062C",
150
+ "\u062C\u06BE",
151
+ "\u0686",
152
+ "\u0686\u06BE",
153
+ "\u062D",
154
+ "\u062E",
155
+ "\u062F",
156
+ "\u062F\u06BE",
157
+ "\u0688",
158
+ "\u0688\u06BE",
159
+ "\u0630",
160
+ "\u0631",
161
+ "\u0631\u06BE",
162
+ "\u0691",
163
+ "\u0691\u06BE",
164
+ "\u0632",
165
+ "\u0698",
166
+ "\u0633",
167
+ "\u0634",
168
+ "\u0635",
169
+ "\u0636",
170
+ "\u0637",
171
+ "\u0638",
172
+ "\u0639",
173
+ "\u063A",
174
+ "\u0641",
175
+ "\u0642",
176
+ "\u06A9",
177
+ "\u06A9\u06BE",
178
+ "\u06AF",
179
+ "\u06AF\u06BE",
180
+ "\u0644",
181
+ "\u0644\u06BE",
182
+ "\u0645",
183
+ "\u0645\u06BE",
184
+ "\u0646",
185
+ "\u0646\u06BE",
186
+ "\u06BA",
187
+ "\u06BA\u06BE",
188
+ "\u0648",
189
+ "\u0648\u06BE",
190
+ "\u06C1",
191
+ "\u06BE",
192
+ "\u06C3",
193
+ "\u0621",
194
+ "\u06CC",
195
+ "\u06CC\u06BE",
196
+ "\u06D2"
197
+ ];
198
+ var HAMZA_VARIANTS = {
199
+ "\u0623": "\u0627",
200
+ "\u0624": "\u0648",
201
+ "\u06C2": "\u06C1",
202
+ "\u0626": "\u06CC",
203
+ "\u06D3": "\u06D2"
204
+ };
205
+ var DIACRITICS = "\u0652\u064E\u0650\u064F\u0670\u0656\u0657\u064B\u064D\u064C\u0654\u0651\u0658\u0653";
206
+ var HAMZA_ABOVE_WEIGHT = 1 + DIACRITICS.indexOf("\u0654");
207
+ var urduRank = /* @__PURE__ */ lazy(
208
+ () => new Map(URDU_ORDER.map((letter, index) => [letter, index]))
209
+ );
210
+ var PUNCTUATION = 1;
211
+ var DIGIT = 2;
212
+ var URDU = 3;
213
+ var OTHER_ARABIC = 4;
214
+ var OTHER = 5;
215
+ function isPunctuation(cp) {
216
+ return cp <= 47 || cp >= 58 && cp <= 64 || cp >= 91 && cp <= 96 || cp >= 123 && cp <= 191 || cp >= 8192 && cp <= 8303 || cp >= 1536 && cp <= 1551 || cp === 1563 || cp >= 1565 && cp <= 1567 || cp >= 1642 && cp <= 1645 || cp === 1748;
217
+ }
218
+ function digitValue(cp) {
219
+ if (cp >= 48 && cp <= 57) return cp - 48;
220
+ if (cp >= 1632 && cp <= 1641) return cp - 1632;
221
+ if (cp >= 1776 && cp <= 1785) return cp - 1776;
222
+ return -1;
223
+ }
224
+ function isArabicScript(cp) {
225
+ return cp >= 1536 && cp <= 1791 || cp >= 1872 && cp <= 1919 || cp >= 2208 && cp <= 2303 || cp >= 64336 && cp <= 65023 || cp >= 65136 && cp <= 65279;
226
+ }
227
+ var weight = (group, rank) => group * 2097152 + rank;
228
+ function collationKey(text) {
229
+ const chars = Array.from(text);
230
+ const ranks = urduRank();
231
+ const primary = [];
232
+ const secondary = [];
233
+ for (let i = 0; i < chars.length; i++) {
234
+ const char = chars[i];
235
+ const diacritic = DIACRITICS.indexOf(char);
236
+ if (diacritic >= 0) {
237
+ secondary.push(1 + diacritic);
238
+ continue;
239
+ }
240
+ if (SKIPPED.test(char)) continue;
241
+ const base = HAMZA_VARIANTS[char];
242
+ let letter = base ?? char;
243
+ if (letter === "\u0627" && chars[i + 1] === "\u0653") {
244
+ letter = "\u0622";
245
+ i++;
246
+ }
247
+ const rank = ranks.get(letter);
248
+ if (rank !== void 0) {
249
+ const joined = ranks.get(letter + (chars[i + 1] ?? ""));
250
+ if (chars[i + 1] === "\u06BE" && joined !== void 0) {
251
+ primary.push(weight(URDU, joined));
252
+ i++;
253
+ } else {
254
+ primary.push(weight(URDU, rank));
255
+ }
256
+ secondary.push(0);
257
+ if (base !== void 0) secondary.push(HAMZA_ABOVE_WEIGHT);
258
+ continue;
259
+ }
260
+ const cp = char.codePointAt(0);
261
+ const digit = digitValue(cp);
262
+ if (digit >= 0) {
263
+ primary.push(weight(DIGIT, digit));
264
+ } else if (isPunctuation(cp)) {
265
+ primary.push(weight(PUNCTUATION, cp));
266
+ } else if (isArabicScript(cp)) {
267
+ primary.push(weight(OTHER_ARABIC, cp));
268
+ } else {
269
+ primary.push(weight(OTHER, cp >= 65 && cp <= 90 ? cp + 32 : cp));
270
+ }
271
+ secondary.push(0);
272
+ }
273
+ return { primary, secondary };
274
+ }
275
+ function compareArrays(a, b) {
276
+ const length = Math.min(a.length, b.length);
277
+ for (let i = 0; i < length; i++) {
278
+ const x = a[i];
279
+ const y = b[i];
280
+ if (x !== y) return x < y ? -1 : 1;
281
+ }
282
+ return a.length === b.length ? 0 : a.length < b.length ? -1 : 1;
283
+ }
284
+ function codePoints(text) {
285
+ return Array.from(text, (char) => char.codePointAt(0));
286
+ }
287
+ function compareUrdu(a, b) {
288
+ const x = typeof a === "string" ? a : "";
289
+ const y = typeof b === "string" ? b : "";
290
+ if (x === y) return 0;
291
+ const kx = collationKey(x);
292
+ const ky = collationKey(y);
293
+ const result = compareArrays(kx.primary, ky.primary) || compareArrays(kx.secondary, ky.secondary) || compareArrays(codePoints(x), codePoints(y));
294
+ return result < 0 ? -1 : result > 0 ? 1 : 0;
295
+ }
296
+ function isRtlCodePoint(cp) {
297
+ return cp >= 1424 && cp <= 2303 || cp >= 64285 && cp <= 65023 || cp >= 65136 && cp <= 65279 || cp >= 67584 && cp <= 69631 || cp >= 124928 && cp <= 126975;
298
+ }
299
+ var LETTER = new RegExp("\\p{L}", "u");
300
+ function isRTL(text) {
301
+ if (typeof text !== "string") return false;
302
+ for (const char of text) {
303
+ if (LETTER.test(char)) return isRtlCodePoint(char.codePointAt(0));
304
+ }
305
+ return false;
306
+ }
307
+ function wrapBidi(text, direction = "auto") {
308
+ if (typeof text !== "string") return "";
309
+ const open = direction === "rtl" ? "\u2067" : direction === "ltr" ? "\u2066" : "\u2068";
310
+ return `${open}${text}\u2069`;
311
+ }
312
+
313
+ export { compareUrdu, isRTL, normalizeRomanUrdu, normalizeText, slugify, toEnglishDigits, toUrduDigits, wrapBidi };
@@ -1,10 +1,11 @@
1
+ import { validatePhone } from './chunk-S274QFFB.js';
2
+ import { phoneRiskSignals } from './chunk-UYFTXD4P.js';
3
+ import { validateSTRN, validateNTN } from './chunk-CEPNSHIC.js';
1
4
  import { validateVehiclePlate } from './chunk-RZQJAZKC.js';
5
+ import { validateIBAN } from './chunk-IBZPJS4H.js';
2
6
  import { validateLandline } from './chunk-LQHM3ADX.js';
3
7
  import { validatePassport } from './chunk-E6HSKRAE.js';
4
- import { validatePhone } from './chunk-S274QFFB.js';
5
- import { validateSTRN, validateNTN } from './chunk-CEPNSHIC.js';
6
8
  import { validateCNIC } from './chunk-EEVNLTB4.js';
7
- import { validateIBAN } from './chunk-IBZPJS4H.js';
8
9
 
9
10
  // src/adapters/rules.ts
10
11
  var RULES = {
@@ -24,6 +25,10 @@ var RULES = {
24
25
  vehiclePlate: {
25
26
  test: validateVehiclePlate,
26
27
  message: "Invalid vehicle registration number"
28
+ },
29
+ codPhone: {
30
+ test: (value) => phoneRiskSignals(value).length === 0,
31
+ message: "Enter a mobile number a courier can call"
27
32
  }
28
33
  };
29
34
 
@@ -0,0 +1,297 @@
1
+ import { calculateSalesTax } from './chunk-34U2ZMHR.js';
2
+ import { toUnits, fromPaisa, divideHalfUp } from './chunk-5EY5DQS7.js';
3
+ import { normalizeKey } from './chunk-ENQ7O7VW.js';
4
+
5
+ // src/fbr-invoice.ts
6
+ var FBR_DI_SPEC_VERSION = "1.12";
7
+ var INVOICE_TYPES = /* @__PURE__ */ new Set(["Sale Invoice", "Debit Note"]);
8
+ var REGISTRATION_TYPES = /* @__PURE__ */ new Set(["Registered", "Unregistered"]);
9
+ var NTN_OR_CNIC = /^(?:\d{7}|\d{13})$/;
10
+ var FBR_INVOICE_NUMBER = /^(?:\d{7}|\d{13})DI\d{13}$/;
11
+ var SCENARIO = /^SN0(?:0[1-9]|1\d|2[0-8])$/;
12
+ var PERCENT_RATE = /^(\d+(?:\.\d+)?)%$/;
13
+ var PER_UNIT_RATE = /^rs\.?\s*(\d+(?:\.\d+)?)$/i;
14
+ var isText = (value) => typeof value === "string";
15
+ var filled = (value) => isText(value) && value.trim().length > 0;
16
+ var isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
17
+ var saleTypeKey = (value) => isText(value) ? normalizeKey(value) : "";
18
+ function decimalPlaces(value) {
19
+ if (!Number.isFinite(value)) return null;
20
+ const match = /^-?\d+(?:\.(\d+))?(?:e([+-]\d+))?$/.exec(String(value));
21
+ if (!match) return null;
22
+ const places = (match[1] ?? "").length - Number(match[2] ?? "0");
23
+ return places > 0 ? places : 0;
24
+ }
25
+ function expectedSalesTax(item) {
26
+ if (!isObject(item) || !isText(item.rate)) return null;
27
+ const rate = item.rate.trim();
28
+ const percent = PERCENT_RATE.exec(rate);
29
+ if (percent) {
30
+ const base = saleTypeKey(item.saleType) === "3rd schedule goods" ? item.fixedNotifiedValueOrRetailPrice : item.valueSalesExcludingST;
31
+ if (typeof base !== "number") return null;
32
+ return calculateSalesTax(base, { rate: Number(percent[1]) })?.tax ?? null;
33
+ }
34
+ const perUnit = PER_UNIT_RATE.exec(rate);
35
+ if (perUnit) {
36
+ if (typeof item.quantity !== "number") return null;
37
+ const quantity = toUnits(item.quantity, 4);
38
+ const price = toUnits(Number(perUnit[1]), 2);
39
+ if (quantity === null || price === null) return null;
40
+ return fromPaisa(divideHalfUp(BigInt(quantity) * BigInt(price), 10000n));
41
+ }
42
+ return null;
43
+ }
44
+ var NUMBER_FIELDS = [
45
+ "quantity",
46
+ "valueSalesExcludingST",
47
+ "salesTaxApplicable",
48
+ "salesTaxWithheldAtSource",
49
+ "extraTax",
50
+ "furtherTax",
51
+ "fedPayable",
52
+ "discount"
53
+ ];
54
+ function columnTotal(items, key) {
55
+ const scale = key === "quantity" ? 4 : 2;
56
+ let sum = 0;
57
+ for (const item of items) {
58
+ const value = item[key];
59
+ if (typeof value !== "number") continue;
60
+ const units = toUnits(value, scale);
61
+ if (units !== null) sum += units;
62
+ }
63
+ return sum / (scale === 4 ? 1e4 : 100);
64
+ }
65
+ function buildFbrInvoice(input) {
66
+ if (!isObject(input) || !Array.isArray(input.items)) return null;
67
+ const items = [];
68
+ for (const raw of input.items) {
69
+ if (!isObject(raw)) return null;
70
+ const line = raw;
71
+ const item = {
72
+ ...line,
73
+ totalValues: line.totalValues ?? 0,
74
+ fixedNotifiedValueOrRetailPrice: line.fixedNotifiedValueOrRetailPrice ?? 0,
75
+ salesTaxWithheldAtSource: line.salesTaxWithheldAtSource ?? 0
76
+ };
77
+ item.salesTaxApplicable = line.salesTaxApplicable ?? expectedSalesTax(item) ?? 0;
78
+ items.push(item);
79
+ }
80
+ const invoice = {
81
+ ...input,
82
+ buyerNTNCNIC: input.buyerNTNCNIC ?? "",
83
+ invoiceRefNo: input.invoiceRefNo ?? "",
84
+ items
85
+ };
86
+ const totals = Object.fromEntries(
87
+ NUMBER_FIELDS.map((key) => [key, columnTotal(items, key)])
88
+ );
89
+ return { invoice, totals };
90
+ }
91
+ function validDate(text) {
92
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(text);
93
+ if (!match) return false;
94
+ const [y, m, d] = [Number(match[1]), Number(match[2]), Number(match[3])];
95
+ const leap = y % 4 === 0 && y % 100 !== 0 || y % 400 === 0;
96
+ const days = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
97
+ return m >= 1 && m <= 12 && d >= 1 && d <= days[m - 1];
98
+ }
99
+ function validateFbrInvoice(invoice, options = {}) {
100
+ const errors = [];
101
+ const add = (code, field, message) => errors.push({ code, field, message });
102
+ if (!isObject(invoice)) {
103
+ add(null, "invoice", "The invoice must be an object");
104
+ return { valid: false, errors };
105
+ }
106
+ const inv = invoice;
107
+ if (!filled(inv.invoiceType)) {
108
+ add("0011", "invoiceType", "Invoice type is empty");
109
+ } else if (!INVOICE_TYPES.has(inv.invoiceType)) {
110
+ add(
111
+ "0003",
112
+ "invoiceType",
113
+ 'Invoice type must be "Sale Invoice" or "Debit Note"'
114
+ );
115
+ }
116
+ if (!filled(inv.invoiceDate)) {
117
+ add("0042", "invoiceDate", "Invoice date is empty");
118
+ } else if (!/^\d{4}-\d{2}-\d{2}$/.test(inv.invoiceDate)) {
119
+ add("0005", "invoiceDate", "Invoice date must be in YYYY-MM-DD format");
120
+ } else if (!validDate(inv.invoiceDate)) {
121
+ add("0113", "invoiceDate", "Invoice date is not a valid date");
122
+ } else if (isText(options?.today) && validDate(options.today) && inv.invoiceDate > options.today) {
123
+ add("0043", "invoiceDate", "Invoice date is later than today");
124
+ }
125
+ if (!isText(inv.sellerNTNCNIC) || !NTN_OR_CNIC.test(inv.sellerNTNCNIC)) {
126
+ add("0401", "sellerNTNCNIC", "Seller NTN or CNIC must be 7 or 13 digits");
127
+ }
128
+ if (!filled(inv.sellerBusinessName)) {
129
+ add(null, "sellerBusinessName", "Seller business name is required");
130
+ }
131
+ if (!filled(inv.sellerProvince)) {
132
+ add("0073", "sellerProvince", "Seller province is empty");
133
+ }
134
+ if (!filled(inv.sellerAddress)) {
135
+ add(null, "sellerAddress", "Seller address is required");
136
+ }
137
+ const registration = inv.buyerRegistrationType;
138
+ if (!isText(registration) || !REGISTRATION_TYPES.has(registration)) {
139
+ add(
140
+ "0012",
141
+ "buyerRegistrationType",
142
+ 'Buyer registration type must be "Registered" or "Unregistered"'
143
+ );
144
+ }
145
+ const buyerId = inv.buyerNTNCNIC;
146
+ if (!filled(buyerId)) {
147
+ if (registration !== "Unregistered") {
148
+ add("0009", "buyerNTNCNIC", "Buyer NTN or CNIC is empty");
149
+ }
150
+ } else if (!NTN_OR_CNIC.test(buyerId)) {
151
+ add("0402", "buyerNTNCNIC", "Buyer NTN or CNIC must be 7 or 13 digits");
152
+ } else if (buyerId === inv.sellerNTNCNIC) {
153
+ add(
154
+ "0058",
155
+ "buyerNTNCNIC",
156
+ "Buyer and seller must be different (self-invoicing is not allowed)"
157
+ );
158
+ }
159
+ if (!filled(inv.buyerBusinessName)) {
160
+ add("0010", "buyerBusinessName", "Buyer business name is empty");
161
+ }
162
+ if (!filled(inv.buyerProvince)) {
163
+ add("0074", "buyerProvince", "Buyer province is empty");
164
+ }
165
+ if (!filled(inv.buyerAddress)) {
166
+ add(null, "buyerAddress", "Buyer address is required");
167
+ }
168
+ if (filled(inv.invoiceRefNo)) {
169
+ if (!FBR_INVOICE_NUMBER.test(inv.invoiceRefNo.trim())) {
170
+ add(
171
+ null,
172
+ "invoiceRefNo",
173
+ 'Invoice reference number must be an FBR invoice number (NTN or CNIC, "DI", 13 digits)'
174
+ );
175
+ }
176
+ } else if (inv.invoiceType === "Debit Note") {
177
+ add(
178
+ "0026",
179
+ "invoiceRefNo",
180
+ "Invoice reference number is required for a debit note"
181
+ );
182
+ }
183
+ if (inv.scenarioId !== void 0 && inv.scenarioId !== null && inv.scenarioId !== "") {
184
+ if (!isText(inv.scenarioId) || !SCENARIO.test(inv.scenarioId)) {
185
+ add(null, "scenarioId", "Scenario ID must be SN001 to SN028");
186
+ }
187
+ }
188
+ if (!Array.isArray(inv.items) || inv.items.length === 0) {
189
+ add(null, "items", "The invoice needs at least one item");
190
+ return { valid: errors.length === 0, errors };
191
+ }
192
+ inv.items.forEach((raw, index) => {
193
+ const at = (key) => `items[${index}].${key}`;
194
+ if (!isObject(raw)) {
195
+ add(null, `items[${index}]`, "Each item must be an object");
196
+ return;
197
+ }
198
+ const item = raw;
199
+ if (!filled(item.hsCode)) add("0044", at("hsCode"), "HS code is empty");
200
+ if (!isText(item.productDescription)) {
201
+ add(null, at("productDescription"), "Product description is required");
202
+ }
203
+ if (!filled(item.rate)) add("0020", at("rate"), "Rate is empty");
204
+ if (!filled(item.uoM)) add(null, at("uoM"), "Unit of measure is required");
205
+ if (!filled(item.saleType))
206
+ add("0013", at("saleType"), "Sale type is empty");
207
+ const missingCodes = {
208
+ quantity: "0098",
209
+ valueSalesExcludingST: "0021",
210
+ salesTaxApplicable: "0023",
211
+ salesTaxWithheldAtSource: "0024",
212
+ totalValues: null,
213
+ fixedNotifiedValueOrRetailPrice: null
214
+ };
215
+ const numbersOk = {};
216
+ for (const key of [
217
+ ...Object.keys(missingCodes),
218
+ "extraTax",
219
+ "furtherTax",
220
+ "fedPayable",
221
+ "discount"
222
+ ]) {
223
+ const value = item[key];
224
+ const required = key in missingCodes;
225
+ if (value === void 0 || value === null || !required && value === "") {
226
+ if (required) {
227
+ add(missingCodes[key] ?? null, at(key), `${key} is required`);
228
+ }
229
+ continue;
230
+ }
231
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0) {
232
+ add("0300", at(key), `${key} must be a number, not negative`);
233
+ continue;
234
+ }
235
+ const places = decimalPlaces(value);
236
+ const limit = key === "quantity" ? 4 : 2;
237
+ if (places > limit) {
238
+ add("0302", at(key), `${key} has more than ${limit} decimal places`);
239
+ continue;
240
+ }
241
+ numbersOk[key] = true;
242
+ }
243
+ const rate = isText(item.rate) ? item.rate.trim() : "";
244
+ if (filled(item.rate) && rate !== "18%" && !filled(item.sroScheduleNo)) {
245
+ add(
246
+ "0077",
247
+ at("sroScheduleNo"),
248
+ "SRO/schedule number is required when the rate is not 18%"
249
+ );
250
+ }
251
+ if (filled(item.sroScheduleNo) && !filled(item.sroItemSerialNo)) {
252
+ add(
253
+ "0078",
254
+ at("sroItemSerialNo"),
255
+ "Item serial number is required when an SRO/schedule number is given"
256
+ );
257
+ }
258
+ const saleType = saleTypeKey(item.saleType);
259
+ if (saleType === "3rd schedule goods" && numbersOk.fixedNotifiedValueOrRetailPrice === true && item.fixedNotifiedValueOrRetailPrice === 0) {
260
+ add(
261
+ "0090",
262
+ at("fixedNotifiedValueOrRetailPrice"),
263
+ "Retail price is required for Third Schedule goods"
264
+ );
265
+ }
266
+ if (saleType === "goods at reduced rate" && item.extraTax !== void 0 && item.extraTax !== null && item.extraTax !== "") {
267
+ add(
268
+ "0091",
269
+ at("extraTax"),
270
+ "Extra tax must be empty for goods at a reduced rate"
271
+ );
272
+ }
273
+ if (numbersOk.salesTaxApplicable === true) {
274
+ const percent = PERCENT_RATE.test(rate);
275
+ const perUnit = PER_UNIT_RATE.test(rate);
276
+ const baseKey = perUnit ? "quantity" : saleType === "3rd schedule goods" ? "fixedNotifiedValueOrRetailPrice" : "valueSalesExcludingST";
277
+ if ((percent || perUnit) && numbersOk[baseKey] === true) {
278
+ const expected = expectedSalesTax(item);
279
+ if (expected !== null) {
280
+ const actual = toUnits(item.salesTaxApplicable, 2);
281
+ const wanted = toUnits(expected, 2);
282
+ if (actual !== null && wanted !== null && Math.abs(actual - wanted) > 1) {
283
+ const code = perUnit ? "0105" : saleType === "3rd schedule goods" ? "0102" : "0104";
284
+ add(
285
+ code,
286
+ at("salesTaxApplicable"),
287
+ `Sales tax should be ${expected.toFixed(2)} for this rate`
288
+ );
289
+ }
290
+ }
291
+ }
292
+ }
293
+ });
294
+ return { valid: errors.length === 0, errors };
295
+ }
296
+
297
+ export { FBR_DI_SPEC_VERSION, buildFbrInvoice, expectedSalesTax, validateFbrInvoice };
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var chunkI7UUPD3G_cjs = require('./chunk-I7UUPD3G.cjs');
4
- var chunkOKMBYX3F_cjs = require('./chunk-OKMBYX3F.cjs');
4
+ var chunk33GOK7EE_cjs = require('./chunk-33GOK7EE.cjs');
5
+ var chunkE6GWBOKV_cjs = require('./chunk-E6GWBOKV.cjs');
5
6
  var chunkVPBMIYO2_cjs = require('./chunk-VPBMIYO2.cjs');
6
7
 
7
8
  // src/address.ts
@@ -9,10 +10,10 @@ function byLengthDesc(a, b) {
9
10
  return b.length - a.length;
10
11
  }
11
12
  var cityNames = /* @__PURE__ */ chunkVPBMIYO2_cjs.lazy(
12
- () => chunkOKMBYX3F_cjs.cities_default.map((entry) => entry.city).sort(byLengthDesc)
13
+ () => chunkE6GWBOKV_cjs.cities_default.map((entry) => entry.city).sort(byLengthDesc)
13
14
  );
14
15
  var districtNames = /* @__PURE__ */ chunkVPBMIYO2_cjs.lazy(
15
- () => chunkOKMBYX3F_cjs.districts_default.map((entry) => entry.district).sort(byLengthDesc)
16
+ () => chunk33GOK7EE_cjs.districts_default.map((entry) => entry.district).sort(byLengthDesc)
16
17
  );
17
18
  var PROVINCES = [
18
19
  "Islamabad Capital Territory",
@@ -82,7 +83,7 @@ function parseAddress(address) {
82
83
  if (postalCode !== null && city === null) {
83
84
  city = chunkI7UUPD3G_cjs.getCityByPostalCode(postalCode);
84
85
  }
85
- const inferredProvince = city !== null ? chunkOKMBYX3F_cjs.getProvince(city) ?? chunkOKMBYX3F_cjs.getDistrictProvince(city) : district !== null ? chunkOKMBYX3F_cjs.getDistrictProvince(district) : null;
86
+ const inferredProvince = city !== null ? chunk33GOK7EE_cjs.getProvince(city) ?? chunk33GOK7EE_cjs.getDistrictProvince(city) : district !== null ? chunk33GOK7EE_cjs.getDistrictProvince(district) : null;
86
87
  if (province === null) {
87
88
  province = inferredProvince;
88
89
  } else if (inferredProvince !== null && inferredProvince !== province) {