@tallyui/core 5.0.0 → 5.2.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.
@@ -262,7 +262,7 @@ function payloadShapeErrors(payload) {
262
262
  if (custom && (!object(entry) || item === void 0)) return;
263
263
  check2(object(item), path, "an object");
264
264
  if (!object(item)) return;
265
- const keys = custom ? ["name", "sku", "taxClass", "taxStatus"] : [id, "name", "amountMinor", "taxMinor", "taxStatus", "taxClass", ...field === "shipping" ? ["methodId"] : []];
265
+ const keys = custom ? ["name", "sku", "taxClass", "taxStatus"] : [id, "name", "amountMinor", "amountIncludesTax", "taxMinor", "taxStatus", "taxClass", ...field === "shipping" ? ["methodId"] : []];
266
266
  for (const key of Object.keys(item)) check2(keys.includes(key), `${path}.${key}`, "no unknown key");
267
267
  check2(typeof item.name === "string" && item.name.length > 0, `${path}.name`, "a non-empty string");
268
268
  check2(item.taxStatus === "taxable" || item.taxStatus === "none", `${path}.taxStatus`, "taxable or none");
@@ -270,6 +270,7 @@ function payloadShapeErrors(payload) {
270
270
  if (item[key] !== void 0) check2(typeof item[key] === "string", `${path}.${key}`, "a string");
271
271
  }
272
272
  if (!custom) {
273
+ if (item.amountIncludesTax !== void 0) check2(typeof item.amountIncludesTax === "boolean", `${path}.amountIncludesTax`, "a boolean");
273
274
  check2(typeof item[id] === "string" && item[id].length > 0, `${path}.${id}`, "a non-empty string");
274
275
  check2(!seen.has(item[id]), `${path}.${id}`, `no duplicate ${id}`);
275
276
  seen.add(item[id]);
@@ -510,9 +511,9 @@ function fiscalFiguresErrors(payload) {
510
511
  if (payload.fees?.length || payload.shipping?.length) check2(display.taxInclusive === payload.pricesIncludeTax, "display.taxInclusive", "payload.pricesIncludeTax");
511
512
  for (const field of ["fees", "shipping"]) {
512
513
  const id = field === "fees" ? "clientFeeId" : "clientShippingId";
513
- const amounts = new Map((payload[field] ?? []).map((entry) => [
514
+ const charges = new Map((payload[field] ?? []).map((entry) => [
514
515
  "clientFeeId" in entry ? entry.clientFeeId : entry.clientShippingId,
515
- entry.amountMinor
516
+ entry
516
517
  ]));
517
518
  const rows = display[field];
518
519
  if (rows !== void 0) check2(Array.isArray(rows), `display.${field}`, "an array");
@@ -522,12 +523,18 @@ function fiscalFiguresErrors(payload) {
522
523
  if (!object(row, path, [id, "amountMinor"])) return;
523
524
  check2(typeof row[id] === "string" && row[id].length > 0, `${path}.${id}`, "a non-empty string");
524
525
  money(row, path, ["amountMinor"]);
525
- check2(amounts.has(row[id]), `${path}.${id}`, `a payload.${field}[].${id}`);
526
+ const entry = charges.get(row[id]);
527
+ check2(entry !== void 0, `${path}.${id}`, `a payload.${field}[].${id}`);
526
528
  check2(!seen3.has(row[id]), `${path}.${id}`, `no duplicate ${id}`);
527
529
  seen3.add(row[id]);
528
- if (amounts.has(row[id])) check2(row.amountMinor === amounts.get(row[id]), `${path}.amountMinor`, `the payload.${field} amountMinor`);
530
+ if (entry) {
531
+ const converted = entry.amountIncludesTax !== void 0 && entry.amountIncludesTax !== payload.pricesIncludeTax;
532
+ const expected = !converted ? entry.amountMinor : entry.amountMinor + (entry.amountIncludesTax ? -entry.taxMinor : entry.taxMinor);
533
+ const matches = converted ? typeof row.amountMinor === "number" && Math.abs(row.amountMinor - expected) <= 1 : row.amountMinor === expected;
534
+ check2(matches, `${path}.amountMinor`, `the payload.${field} amountMinor${converted ? " converted to the display tax mode" : ""}`);
535
+ }
529
536
  });
530
- for (const key of amounts.keys()) check2(seen3.has(key), `display.${field}`, `a row for ${id} ${key}`);
537
+ for (const key of charges.keys()) check2(seen3.has(key), `display.${field}`, `a row for ${id} ${key}`);
531
538
  }
532
539
  const codes = new Set((payload.coupons ?? []).map((coupon) => coupon.code));
533
540
  const seen2 = /* @__PURE__ */ new Set();
@@ -609,6 +616,224 @@ function shapeErrors() {
609
616
  return { errors, check: check2, noNul };
610
617
  }
611
618
 
619
+ // src/server/refund-payload-shape.ts
620
+ function refundPayloadErrors(payload) {
621
+ const { errors, check: check2, noNul } = shapeErrors();
622
+ if (!isPlainObject(payload)) return ["payload: expected an object"];
623
+ const fields = [
624
+ "clientRefundId",
625
+ "orderId",
626
+ "registerId",
627
+ "sessionId",
628
+ "clientOrderId",
629
+ "cashierRef",
630
+ "lines",
631
+ "shippingMinor",
632
+ "totalMinor",
633
+ "adjustmentMinor",
634
+ "destination",
635
+ "reason",
636
+ "createdAt"
637
+ ];
638
+ for (const key of Object.keys(payload)) {
639
+ if (!fields.includes(key) && errors.length < 10) {
640
+ errors.push(`${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.refund version 1`);
641
+ }
642
+ }
643
+ for (const field of ["clientRefundId", "orderId", "registerId", "sessionId", "clientOrderId", "cashierRef"]) {
644
+ const value = payload[field];
645
+ if (["clientOrderId", "cashierRef"].includes(field) && value === void 0) continue;
646
+ check2(
647
+ typeof value === "string" && value.length > 0 && value.length <= 64,
648
+ field,
649
+ "a non-empty string of at most 64 characters"
650
+ );
651
+ noNul(value, field);
652
+ }
653
+ check2(Array.isArray(payload.lines) && payload.lines.length <= 500, "lines", "an array of at most 500 lines");
654
+ const seen = /* @__PURE__ */ new Set();
655
+ if (Array.isArray(payload.lines)) payload.lines.forEach((line, index) => {
656
+ const path = `lines[${index}]`;
657
+ if (!isPlainObject(line)) {
658
+ check2(false, path, "an object");
659
+ return;
660
+ }
661
+ for (const key of Object.keys(line)) {
662
+ if (!["orderLineId", "quantity", "restock"].includes(key) && errors.length < 10) {
663
+ errors.push(`${path}.${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.refund version 1`);
664
+ }
665
+ }
666
+ check2(
667
+ typeof line.orderLineId === "string" && line.orderLineId.length > 0 && line.orderLineId.length <= 64,
668
+ `${path}.orderLineId`,
669
+ "a non-empty string of at most 64 characters"
670
+ );
671
+ noNul(line.orderLineId, `${path}.orderLineId`);
672
+ if (typeof line.orderLineId === "string") {
673
+ check2(!seen.has(line.orderLineId), `${path}.orderLineId`, "unique in lines");
674
+ seen.add(line.orderLineId);
675
+ }
676
+ check2(Number.isSafeInteger(line.quantity) && line.quantity >= 1, `${path}.quantity`, "a safe integer >= 1");
677
+ check2(typeof line.restock === "boolean", `${path}.restock`, "a boolean");
678
+ });
679
+ for (const field of ["shippingMinor", "totalMinor"]) {
680
+ check2(Number.isSafeInteger(payload[field]) && payload[field] >= 0, field, "a safe integer >= 0");
681
+ }
682
+ check2(Number.isSafeInteger(payload.adjustmentMinor), "adjustmentMinor", "a safe integer");
683
+ check2(payload.destination === "original_method" || payload.destination === "cash", "destination", "original_method or cash");
684
+ noNul(payload.destination, "destination");
685
+ check2(
686
+ typeof payload.reason === "string" && payload.reason.trim().length > 0 && payload.reason.length <= 500,
687
+ "reason",
688
+ "a non-empty string after trim of at most 500 characters"
689
+ );
690
+ noNul(payload.reason, "reason");
691
+ check2(
692
+ typeof payload.createdAt === "string" && payload.createdAt.length > 0 && !Number.isNaN(Date.parse(payload.createdAt)),
693
+ "createdAt",
694
+ "a valid date"
695
+ );
696
+ noNul(payload.createdAt, "createdAt");
697
+ return errors;
698
+ }
699
+
700
+ // src/server/register-payload-shape.ts
701
+ function registerPayloadErrors(type, payload) {
702
+ const { errors, check: check2, noNul } = shapeErrors();
703
+ if (!isPlainObject(payload)) return ["payload: expected an object"];
704
+ const string = (field, optional = false) => {
705
+ const value = payload[field];
706
+ if (optional && value === void 0) return;
707
+ check2(typeof value === "string" && (optional || value.length > 0), field, optional ? "a string" : "a non-empty string");
708
+ if (typeof value === "string" && (field.endsWith("Id") || field === "voids")) check2(value.length <= 64, field, "at most 64 characters");
709
+ if (typeof value === "string" && ["openedAt", "at", "createdAt", "closedAt"].includes(field)) {
710
+ check2(!Number.isNaN(Date.parse(value)), field, "a valid date");
711
+ }
712
+ noNul(value, field);
713
+ };
714
+ const integer = (field, min = -Infinity) => check2(Number.isSafeInteger(payload[field]) && payload[field] >= min, field, `a safe integer >= ${min}`);
715
+ const record = (field) => {
716
+ const value = payload[field];
717
+ check2(isPlainObject(value), field, "a record of integers");
718
+ if (isPlainObject(value)) for (const [key, amount] of Object.entries(value)) {
719
+ check2(key.length > 0 && Number.isSafeInteger(amount), `${field}.${key}`, "a non-empty key with a safe integer");
720
+ noNul(key, `${field}.${JSON.stringify(key)}`);
721
+ }
722
+ };
723
+ string("sessionId");
724
+ switch (type) {
725
+ case "register.session.open":
726
+ for (const field of ["registerId", "openedAt"]) string(field);
727
+ for (const field of ["storeKey", "businessDay", "openedBy"]) string(field, true);
728
+ if (payload.deviceName !== void 0) {
729
+ check2(
730
+ typeof payload.deviceName === "string" && payload.deviceName.trim().length >= 1 && payload.deviceName.trim().length <= 64,
731
+ "deviceName",
732
+ "a string of 1 to 64 characters after trim"
733
+ );
734
+ noNul(payload.deviceName, "deviceName");
735
+ }
736
+ if (payload.supersedes !== void 0) {
737
+ check2(
738
+ typeof payload.supersedes === "string" && payload.supersedes.length > 0 && payload.supersedes.length <= 64,
739
+ "supersedes",
740
+ "a non-empty string of at most 64 characters"
741
+ );
742
+ noNul(payload.supersedes, "supersedes");
743
+ }
744
+ integer("countedFloatMinor", 0);
745
+ for (const field of ["expectedFloatMinor", "openingVarianceMinor"]) if (payload[field] !== void 0) integer(field);
746
+ break;
747
+ case "register.session.transition":
748
+ string("at");
749
+ check2(["open", "counting", "closed"].includes(payload.status), "status", "open, counting or closed");
750
+ for (const field of ["counted", "closedBy", "approvedBy"]) if (payload[field] !== void 0) {
751
+ check2(payload.status === "closed", field, "a closing transition");
752
+ if (field === "counted") record(field);
753
+ else string(field, true);
754
+ }
755
+ break;
756
+ case "register.movement.record":
757
+ case "register.movement.void":
758
+ for (const field of ["movementId", "createdAt"]) string(field);
759
+ string("createdBy", true);
760
+ if (type === "register.movement.void") string("voids");
761
+ else {
762
+ check2(
763
+ typeof payload.reason === "string" && payload.reason.trim().length > 0 && payload.reason.length <= 500,
764
+ "reason",
765
+ "a non-empty string after trim of at most 500 characters"
766
+ );
767
+ noNul(payload.reason, "reason");
768
+ check2(["paid_in", "paid_out", "no_sale"].includes(payload.type), "type", "paid_in, paid_out or no_sale");
769
+ integer("amountMinor", payload.type === "no_sale" ? 0 : 1);
770
+ if (payload.type === "no_sale") check2(payload.amountMinor === 0, "amountMinor", "0 for no_sale");
771
+ }
772
+ break;
773
+ case "register.closure.submit":
774
+ for (const field of ["closureId", "registerId", "openedAt", "closedAt", "softwareVersion"]) string(field);
775
+ for (const field of ["businessDay", "closedBy", "approvedBy"]) string(field, true);
776
+ if (payload.approval !== void 0) {
777
+ check2(typeof payload.approval === "string" && payload.approval.length > 0, "approval", "a non-empty string");
778
+ noNul(payload.approval, "approval");
779
+ }
780
+ integer("number", 1);
781
+ for (const field of [
782
+ "periodSalesTotalMinor",
783
+ "periodRefundsTotalMinor",
784
+ "perpetualSalesTotalMinor",
785
+ "perpetualRefundsTotalMinor",
786
+ "unsyncedCount",
787
+ "unsyncedTotalMinor"
788
+ ]) integer(field, 0);
789
+ for (const field of ["number", "unsyncedCount"])
790
+ check2(payload[field] <= 2147483647, field, "at most 2147483647");
791
+ for (const field of ["tillExpected", "counted"]) record(field);
792
+ for (const field of ["orderIds", "movementIds"]) {
793
+ const value = payload[field];
794
+ check2(Array.isArray(value), field, "an array of ids");
795
+ if (Array.isArray(value)) value.forEach((id, index) => {
796
+ check2(typeof id === "string" && id.length > 0 && id.length <= 64, `${field}[${index}]`, "a non-empty string of at most 64 characters");
797
+ noNul(id, `${field}[${index}]`);
798
+ });
799
+ }
800
+ break;
801
+ default:
802
+ check2(false, "type", "a register command type");
803
+ }
804
+ return errors;
805
+ }
806
+
807
+ // src/server/reopen-payload-shape.ts
808
+ function reopenPayloadErrors(payload) {
809
+ const { errors, check: check2, noNul } = shapeErrors();
810
+ if (!isPlainObject(payload)) return ["payload: expected an object"];
811
+ const fields = ["orderId", "clientOrderId", "baseRevision", "cashierRef", "registerId", "storeId", "createdAt"];
812
+ for (const key of Object.keys(payload)) {
813
+ if (!fields.includes(key) && errors.length < 10) {
814
+ errors.push(`${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.reopen version 1`);
815
+ }
816
+ }
817
+ for (const field of ["orderId", "cashierRef", "registerId", "clientOrderId", "baseRevision", "storeId"]) {
818
+ const value = payload[field];
819
+ if (["clientOrderId", "baseRevision", "storeId"].includes(field) && value === void 0) continue;
820
+ const max = field === "baseRevision" ? 128 : 64;
821
+ check2(
822
+ typeof value === "string" && value.length > 0 && value.length <= max,
823
+ field,
824
+ `a non-empty string of at most ${max} characters`
825
+ );
826
+ noNul(value, field);
827
+ }
828
+ check2(
829
+ typeof payload.createdAt === "string" && payload.createdAt.length > 0 && !Number.isNaN(Date.parse(payload.createdAt)),
830
+ "createdAt",
831
+ "a valid date"
832
+ );
833
+ noNul(payload.createdAt, "createdAt");
834
+ return errors;
835
+ }
836
+
612
837
  // src/server/update-payload-shape.ts
613
838
  function updateOnlyErrors(payload) {
614
839
  const { errors, check: check2 } = shapeErrors();
@@ -695,6 +920,7 @@ function validateBatch(body) {
695
920
  }
696
921
  var rejected = (id, code, message, data) => ({ id, status: "rejected", error: data === void 0 ? { code, message } : { code, message, data } });
697
922
  var firstTen = (errors) => errors.slice(0, 10).join("; ");
923
+ var shapeRefusal = (id, errors) => errors.length ? rejected(id, "invalid_payload", firstTen(errors)) : void 0;
698
924
  function unsupportedOptional(envelope, key, versions, noun) {
699
925
  if (versions.includes(envelope.version)) return void 0;
700
926
  return rejected(
@@ -708,8 +934,12 @@ function precheckCommand(envelope, supported) {
708
934
  for (const key of ["orderCreate", "register"]) {
709
935
  if (!supported[key]?.length) throw new TypeError(`precheckCommand: supported.${key} must list at least one version`);
710
936
  }
711
- if (envelope.type === "order.refund") return unsupportedOptional(envelope, "orderRefund", supported.orderRefund ?? [], "refunds");
712
- if (envelope.type === "order.reopen") return unsupportedOptional(envelope, "orderReopen", supported.orderReopen ?? [], "reopens");
937
+ if (envelope.type === "order.refund") {
938
+ return unsupportedOptional(envelope, "orderRefund", supported.orderRefund ?? [], "refunds") ?? shapeRefusal(envelope.id, refundPayloadErrors(envelope.payload));
939
+ }
940
+ if (envelope.type === "order.reopen") {
941
+ return unsupportedOptional(envelope, "orderReopen", supported.orderReopen ?? [], "reopens") ?? shapeRefusal(envelope.id, reopenPayloadErrors(envelope.payload));
942
+ }
713
943
  if (envelope.type === "order.update") {
714
944
  const refusal = unsupportedOptional(envelope, "orderUpdate", supported.orderUpdate ?? [], "updates");
715
945
  if (refusal) return refusal;
@@ -729,7 +959,7 @@ function precheckCommand(envelope, supported) {
729
959
  { register: Math.max(...supported.register) }
730
960
  );
731
961
  }
732
- return void 0;
962
+ return shapeRefusal(envelope.id, registerPayloadErrors(envelope.type, envelope.payload));
733
963
  }
734
964
  const command = envelope;
735
965
  if (!supported.orderCreate.includes(command.version)) {
@@ -743,6 +973,9 @@ function precheckCommand(envelope, supported) {
743
973
  if (command.payload.cashRoundings !== void 0 && supported.cashRounding !== true) {
744
974
  return rejected(command.id, "invalid_payload", "cashRoundings requires the cashRounding capability, which this server does not declare");
745
975
  }
976
+ if (supported.chargeTaxMode !== true && [command.payload.fees, command.payload.shipping].some((items) => Array.isArray(items) && items.some((entry) => entry?.amountIncludesTax !== void 0))) {
977
+ return rejected(command.id, "invalid_payload", "amountIncludesTax requires the chargeTaxMode capability, which this server does not declare");
978
+ }
746
979
  const { lines, discountMinor } = command.payload;
747
980
  const discounted = discountMinor !== void 0 || Array.isArray(lines) && lines.some((line) => line?.discountMinor !== void 0);
748
981
  const payload = command.payload;
@@ -786,7 +1019,7 @@ function precheckCommand(envelope, supported) {
786
1019
  return rejected(command.id, "invalid_payload", firstTen(bounds));
787
1020
  }
788
1021
  const shape = payloadShapeErrors(payload);
789
- const errors = command.version >= 5 && shape.length ? shape : v3 && display !== void 0 && taxByRate !== void 0 && shape.length === 0 ? fiscalFiguresErrors(payload) : [];
1022
+ const errors = shape.length ? shape : v3 && display !== void 0 && taxByRate !== void 0 ? fiscalFiguresErrors(payload) : [];
790
1023
  if (errors.length) {
791
1024
  return rejected(command.id, "invalid_payload", firstTen(errors));
792
1025
  }
@@ -1002,224 +1235,6 @@ function parseCommandResult(value) {
1002
1235
  return result;
1003
1236
  }
1004
1237
 
1005
- // src/server/register-payload-shape.ts
1006
- function registerPayloadErrors(type, payload) {
1007
- const { errors, check: check2, noNul } = shapeErrors();
1008
- if (!isPlainObject(payload)) return ["payload: expected an object"];
1009
- const string = (field, optional = false) => {
1010
- const value = payload[field];
1011
- if (optional && value === void 0) return;
1012
- check2(typeof value === "string" && (optional || value.length > 0), field, optional ? "a string" : "a non-empty string");
1013
- if (typeof value === "string" && (field.endsWith("Id") || field === "voids")) check2(value.length <= 64, field, "at most 64 characters");
1014
- if (typeof value === "string" && ["openedAt", "at", "createdAt", "closedAt"].includes(field)) {
1015
- check2(!Number.isNaN(Date.parse(value)), field, "a valid date");
1016
- }
1017
- noNul(value, field);
1018
- };
1019
- const integer = (field, min = -Infinity) => check2(Number.isSafeInteger(payload[field]) && payload[field] >= min, field, `a safe integer >= ${min}`);
1020
- const record = (field) => {
1021
- const value = payload[field];
1022
- check2(isPlainObject(value), field, "a record of integers");
1023
- if (isPlainObject(value)) for (const [key, amount] of Object.entries(value)) {
1024
- check2(key.length > 0 && Number.isSafeInteger(amount), `${field}.${key}`, "a non-empty key with a safe integer");
1025
- noNul(key, `${field}.${JSON.stringify(key)}`);
1026
- }
1027
- };
1028
- string("sessionId");
1029
- switch (type) {
1030
- case "register.session.open":
1031
- for (const field of ["registerId", "openedAt"]) string(field);
1032
- for (const field of ["storeKey", "businessDay", "openedBy"]) string(field, true);
1033
- if (payload.deviceName !== void 0) {
1034
- check2(
1035
- typeof payload.deviceName === "string" && payload.deviceName.trim().length >= 1 && payload.deviceName.trim().length <= 64,
1036
- "deviceName",
1037
- "a string of 1 to 64 characters after trim"
1038
- );
1039
- noNul(payload.deviceName, "deviceName");
1040
- }
1041
- if (payload.supersedes !== void 0) {
1042
- check2(
1043
- typeof payload.supersedes === "string" && payload.supersedes.length > 0 && payload.supersedes.length <= 64,
1044
- "supersedes",
1045
- "a non-empty string of at most 64 characters"
1046
- );
1047
- noNul(payload.supersedes, "supersedes");
1048
- }
1049
- integer("countedFloatMinor", 0);
1050
- for (const field of ["expectedFloatMinor", "openingVarianceMinor"]) if (payload[field] !== void 0) integer(field);
1051
- break;
1052
- case "register.session.transition":
1053
- string("at");
1054
- check2(["open", "counting", "closed"].includes(payload.status), "status", "open, counting or closed");
1055
- for (const field of ["counted", "closedBy", "approvedBy"]) if (payload[field] !== void 0) {
1056
- check2(payload.status === "closed", field, "a closing transition");
1057
- if (field === "counted") record(field);
1058
- else string(field, true);
1059
- }
1060
- break;
1061
- case "register.movement.record":
1062
- case "register.movement.void":
1063
- for (const field of ["movementId", "createdAt"]) string(field);
1064
- string("createdBy", true);
1065
- if (type === "register.movement.void") string("voids");
1066
- else {
1067
- check2(
1068
- typeof payload.reason === "string" && payload.reason.trim().length > 0 && payload.reason.length <= 500,
1069
- "reason",
1070
- "a non-empty string after trim of at most 500 characters"
1071
- );
1072
- noNul(payload.reason, "reason");
1073
- check2(["paid_in", "paid_out", "no_sale"].includes(payload.type), "type", "paid_in, paid_out or no_sale");
1074
- integer("amountMinor", payload.type === "no_sale" ? 0 : 1);
1075
- if (payload.type === "no_sale") check2(payload.amountMinor === 0, "amountMinor", "0 for no_sale");
1076
- }
1077
- break;
1078
- case "register.closure.submit":
1079
- for (const field of ["closureId", "registerId", "openedAt", "closedAt", "softwareVersion"]) string(field);
1080
- for (const field of ["businessDay", "closedBy", "approvedBy"]) string(field, true);
1081
- if (payload.approval !== void 0) {
1082
- check2(typeof payload.approval === "string" && payload.approval.length > 0, "approval", "a non-empty string");
1083
- noNul(payload.approval, "approval");
1084
- }
1085
- integer("number", 1);
1086
- for (const field of [
1087
- "periodSalesTotalMinor",
1088
- "periodRefundsTotalMinor",
1089
- "perpetualSalesTotalMinor",
1090
- "perpetualRefundsTotalMinor",
1091
- "unsyncedCount",
1092
- "unsyncedTotalMinor"
1093
- ]) integer(field, 0);
1094
- for (const field of ["number", "unsyncedCount"])
1095
- check2(payload[field] <= 2147483647, field, "at most 2147483647");
1096
- for (const field of ["tillExpected", "counted"]) record(field);
1097
- for (const field of ["orderIds", "movementIds"]) {
1098
- const value = payload[field];
1099
- check2(Array.isArray(value), field, "an array of ids");
1100
- if (Array.isArray(value)) value.forEach((id, index) => {
1101
- check2(typeof id === "string" && id.length > 0 && id.length <= 64, `${field}[${index}]`, "a non-empty string of at most 64 characters");
1102
- noNul(id, `${field}[${index}]`);
1103
- });
1104
- }
1105
- break;
1106
- default:
1107
- check2(false, "type", "a register command type");
1108
- }
1109
- return errors;
1110
- }
1111
-
1112
- // src/server/refund-payload-shape.ts
1113
- function refundPayloadErrors(payload) {
1114
- const { errors, check: check2, noNul } = shapeErrors();
1115
- if (!isPlainObject(payload)) return ["payload: expected an object"];
1116
- const fields = [
1117
- "clientRefundId",
1118
- "orderId",
1119
- "registerId",
1120
- "sessionId",
1121
- "clientOrderId",
1122
- "cashierRef",
1123
- "lines",
1124
- "shippingMinor",
1125
- "totalMinor",
1126
- "adjustmentMinor",
1127
- "destination",
1128
- "reason",
1129
- "createdAt"
1130
- ];
1131
- for (const key of Object.keys(payload)) {
1132
- if (!fields.includes(key) && errors.length < 10) {
1133
- errors.push(`${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.refund version 1`);
1134
- }
1135
- }
1136
- for (const field of ["clientRefundId", "orderId", "registerId", "sessionId", "clientOrderId", "cashierRef"]) {
1137
- const value = payload[field];
1138
- if (["clientOrderId", "cashierRef"].includes(field) && value === void 0) continue;
1139
- check2(
1140
- typeof value === "string" && value.length > 0 && value.length <= 64,
1141
- field,
1142
- "a non-empty string of at most 64 characters"
1143
- );
1144
- noNul(value, field);
1145
- }
1146
- check2(Array.isArray(payload.lines) && payload.lines.length <= 500, "lines", "an array of at most 500 lines");
1147
- const seen = /* @__PURE__ */ new Set();
1148
- if (Array.isArray(payload.lines)) payload.lines.forEach((line, index) => {
1149
- const path = `lines[${index}]`;
1150
- if (!isPlainObject(line)) {
1151
- check2(false, path, "an object");
1152
- return;
1153
- }
1154
- for (const key of Object.keys(line)) {
1155
- if (!["orderLineId", "quantity", "restock"].includes(key) && errors.length < 10) {
1156
- errors.push(`${path}.${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.refund version 1`);
1157
- }
1158
- }
1159
- check2(
1160
- typeof line.orderLineId === "string" && line.orderLineId.length > 0 && line.orderLineId.length <= 64,
1161
- `${path}.orderLineId`,
1162
- "a non-empty string of at most 64 characters"
1163
- );
1164
- noNul(line.orderLineId, `${path}.orderLineId`);
1165
- if (typeof line.orderLineId === "string") {
1166
- check2(!seen.has(line.orderLineId), `${path}.orderLineId`, "unique in lines");
1167
- seen.add(line.orderLineId);
1168
- }
1169
- check2(Number.isSafeInteger(line.quantity) && line.quantity >= 1, `${path}.quantity`, "a safe integer >= 1");
1170
- check2(typeof line.restock === "boolean", `${path}.restock`, "a boolean");
1171
- });
1172
- for (const field of ["shippingMinor", "totalMinor"]) {
1173
- check2(Number.isSafeInteger(payload[field]) && payload[field] >= 0, field, "a safe integer >= 0");
1174
- }
1175
- check2(Number.isSafeInteger(payload.adjustmentMinor), "adjustmentMinor", "a safe integer");
1176
- check2(payload.destination === "original_method" || payload.destination === "cash", "destination", "original_method or cash");
1177
- noNul(payload.destination, "destination");
1178
- check2(
1179
- typeof payload.reason === "string" && payload.reason.trim().length > 0 && payload.reason.length <= 500,
1180
- "reason",
1181
- "a non-empty string after trim of at most 500 characters"
1182
- );
1183
- noNul(payload.reason, "reason");
1184
- check2(
1185
- typeof payload.createdAt === "string" && payload.createdAt.length > 0 && !Number.isNaN(Date.parse(payload.createdAt)),
1186
- "createdAt",
1187
- "a valid date"
1188
- );
1189
- noNul(payload.createdAt, "createdAt");
1190
- return errors;
1191
- }
1192
-
1193
- // src/server/reopen-payload-shape.ts
1194
- function reopenPayloadErrors(payload) {
1195
- const { errors, check: check2, noNul } = shapeErrors();
1196
- if (!isPlainObject(payload)) return ["payload: expected an object"];
1197
- const fields = ["orderId", "clientOrderId", "baseRevision", "cashierRef", "registerId", "storeId", "createdAt"];
1198
- for (const key of Object.keys(payload)) {
1199
- if (!fields.includes(key) && errors.length < 10) {
1200
- errors.push(`${key.includes("\0") ? JSON.stringify(key) : key}: unknown field for order.reopen version 1`);
1201
- }
1202
- }
1203
- for (const field of ["orderId", "cashierRef", "registerId", "clientOrderId", "baseRevision", "storeId"]) {
1204
- const value = payload[field];
1205
- if (["clientOrderId", "baseRevision", "storeId"].includes(field) && value === void 0) continue;
1206
- const max = field === "baseRevision" ? 128 : 64;
1207
- check2(
1208
- typeof value === "string" && value.length > 0 && value.length <= max,
1209
- field,
1210
- `a non-empty string of at most ${max} characters`
1211
- );
1212
- noNul(value, field);
1213
- }
1214
- check2(
1215
- typeof payload.createdAt === "string" && payload.createdAt.length > 0 && !Number.isNaN(Date.parse(payload.createdAt)),
1216
- "createdAt",
1217
- "a valid date"
1218
- );
1219
- noNul(payload.createdAt, "createdAt");
1220
- return errors;
1221
- }
1222
-
1223
1238
  // src/server/register-figures.ts
1224
1239
  function deriveSessionFigures(input) {
1225
1240
  const expected = Object.assign(/* @__PURE__ */ Object.create(null), { cash: input.countedFloatMinor });
@@ -1928,6 +1943,13 @@ function roundRatio(n, d, mode = "half_away_from_zero") {
1928
1943
  }
1929
1944
 
1930
1945
  // src/server/refund-share.ts
1946
+ function pricingBase({ totalMinor, taxMinor, soldTotalMinor, soldTaxMinor }) {
1947
+ if (soldTotalMinor === void 0 && soldTaxMinor === void 0) return [totalMinor, taxMinor];
1948
+ if (soldTotalMinor === void 0 || soldTaxMinor === void 0 || !Number.isSafeInteger(soldTotalMinor) || !Number.isSafeInteger(soldTaxMinor)) {
1949
+ throw new RangeError("A line carries both sold figures or neither, as safe integers");
1950
+ }
1951
+ return [soldTotalMinor, soldTaxMinor];
1952
+ }
1931
1953
  function refundableLines(lines, refunds) {
1932
1954
  const earlier = refunds.flatMap((refund) => refund.lines);
1933
1955
  for (const refund of earlier) {
@@ -1938,7 +1960,7 @@ function refundableLines(lines, refunds) {
1938
1960
  throw new RangeError("Order line values must be safe integers and quantity non-negative");
1939
1961
  }
1940
1962
  const refundedQuantity = earlier.reduce((sum, refund) => refund.orderLineId === line.id ? Math.min(line.quantity, sum + Math.abs(refund.quantity)) : sum, 0);
1941
- const [netMinor, taxMinor] = [line.totalMinor, line.taxMinor].map((total) => line.quantity === 0 ? 0 : total - Number(roundRatio(BigInt(refundedQuantity) * BigInt(total), BigInt(line.quantity), "half_up")));
1963
+ const [netMinor, taxMinor] = pricingBase(line).map((total) => line.quantity === 0 ? 0 : total - Number(roundRatio(BigInt(refundedQuantity) * BigInt(total), BigInt(line.quantity), "half_up")));
1942
1964
  return {
1943
1965
  orderLineId: line.id,
1944
1966
  refundedQuantity,
@@ -1963,7 +1985,7 @@ function priceRefund(lines, refunds, request) {
1963
1985
  if (quantity < 1) continue;
1964
1986
  const line = lines[index];
1965
1987
  const refundedQuantity = available[index].refundedQuantity;
1966
- const [netMinor, taxMinor] = [line.totalMinor, line.taxMinor].map((total) => Number(roundRatio(BigInt(refundedQuantity + quantity) * BigInt(total), BigInt(line.quantity), "half_up")) - Number(roundRatio(BigInt(refundedQuantity) * BigInt(total), BigInt(line.quantity), "half_up")));
1988
+ const [netMinor, taxMinor] = pricingBase(line).map((total) => Number(roundRatio(BigInt(refundedQuantity + quantity) * BigInt(total), BigInt(line.quantity), "half_up")) - Number(roundRatio(BigInt(refundedQuantity) * BigInt(total), BigInt(line.quantity), "half_up")));
1967
1989
  const totalMinor = netMinor + taxMinor;
1968
1990
  result.lines.push({ orderLineId, quantity, restock });
1969
1991
  result.priced.push({ orderLineId, quantity, netMinor, taxMinor, totalMinor });
@@ -390,8 +390,10 @@ interface OrderCreateFee {
390
390
  clientFeeId: string;
391
391
  /** Recorded fee title, 1–255 characters. */
392
392
  name: string;
393
- /** Instruction: integer >= 0, gross when pricesIncludeTax, net otherwise; never discounted. */
393
+ /** Instruction: integer >= 0, gross when its own mode (`amountIncludesTax`, else `pricesIncludeTax`) includes tax, net otherwise; never discounted. */
394
394
  amountMinor: number;
395
+ /** Instruction (vendurepos 2.10b): `amountMinor`'s own mode, gross when true and net when false. Absent means `pricesIncludeTax`'s. Sent only to a store whose connector declares `chargeTaxMode`, and only when it differs from `pricesIncludeTax`. */
396
+ amountIncludesTax?: boolean;
395
397
  /** Instruction: `none` means no tax on this fee. */
396
398
  taxStatus: 'taxable' | 'none';
397
399
  /** Instruction, at most 64 characters; absent means the standard class. */
@@ -581,6 +583,12 @@ interface StoreOrderLine {
581
583
  /** After discounts, excluding tax. */
582
584
  totalMinor: number;
583
585
  taxMinor: number;
586
+ /** The line's total after discounts, excluding tax, as sold, before any return; set by a connector whose live totals shrink on return (connector-medusa at plugin contract 7), absent otherwise. */
587
+ soldTotalMinor?: number;
588
+ /** The tax beside `soldTotalMinor`, as sold. Present exactly when it is. */
589
+ soldTaxMinor?: number;
590
+ /** The store's returned quantity for the line (requested, received or dismissed returns); absent when the store does not say. Refund pricing does not read it. */
591
+ returnedQuantity?: number;
584
592
  }
585
593
 
586
594
  /** One ICE server, in the shape `RTCPeerConnection` takes (ADR-085 d7). */
@@ -764,6 +772,8 @@ interface ServerCapabilities {
764
772
  pluginContract?: number;
765
773
  /** True only when the store's connector carries order.create's `cashRoundings` (ADR-094). Absent: the till never rounds a cash leg, and finalize refuses an order with entries. */
766
774
  cashRounding?: boolean;
775
+ /** True only when the store takes a fee's or shipping charge's own tax mode as order.create's `amountIncludesTax` (vendurepos 2.10b). Absent: finalize converts every charge to the order's mode and sends no `amountIncludesTax`. */
776
+ chargeTaxMode?: boolean;
767
777
  }
768
778
  /** #287, ADR-071. `custom`: the till computes as when absent; that server never emits `figures_mismatch` for subtotal or tax. */
769
779
  type TaxRounding = {
@@ -871,8 +881,10 @@ declare function validateBatch(body: unknown): {
871
881
  * claim, in the executor (order.create; register commands follow ADR-068). It pushes any result
872
882
  * one of those steps returns and moves on.
873
883
  *
874
- * For order.update the shape check is updatePayloadErrors (ADR-090): it adds the update's store ids, and below
875
- * createVersion 5 precheckCommand does not repeat it.
884
+ * For order.update the shape check is updatePayloadErrors (ADR-090): it adds the update's store ids; for order.refund
885
+ * refundPayloadErrors, for order.reopen reopenPayloadErrors, for a register command registerPayloadErrors.
886
+ * precheckCommand repeats the shape check for every command type at every version, after the version answer, as a
887
+ * backstop (#1344, #1346): for order.update's create part too.
876
888
  *
877
889
  * The replay lookup goes before precheckCommand so an already-applied command always replays
878
890
  * as `duplicate`, even once a later @tallyui/core tightens what precheckCommand accepts.
@@ -882,6 +894,7 @@ declare function validateBatch(body: unknown): {
882
894
  * `precheckCommand(envelope, { orderCreate: [1, 2, 3], register: [1], orderRefund: [1], orderReopen: [1], orderUpdate: [1] })` (`orderRefund`, `orderReopen` and `orderUpdate` are optional).
883
895
  * `cashRounding` is the server's own word too (ADR-094): without `true`, an order.create (or an order.update's create part)
884
896
  * carrying `cashRoundings` is refused as `invalid_payload`, never sent on with the field dropped, which would be a wrong total.
897
+ * Likewise, a fee or shipping charge carrying `amountIncludesTax` is refused unless the server declares `chargeTaxMode: true`.
885
898
  */
886
899
  declare function precheckCommand(envelope: Pick<AnyCommandEnvelope | ValidatedCommandEnvelope, 'id' | 'type' | 'version' | 'payload'>, supported: {
887
900
  orderCreate: readonly number[];
@@ -890,6 +903,7 @@ declare function precheckCommand(envelope: Pick<AnyCommandEnvelope | ValidatedCo
890
903
  orderReopen?: readonly number[];
891
904
  orderUpdate?: readonly number[];
892
905
  cashRounding?: boolean;
906
+ chargeTaxMode?: boolean;
893
907
  }): CommandResult | undefined;
894
908
 
895
909
  /**
@@ -1096,7 +1110,7 @@ declare function roundRatio(n: bigint, d: bigint, mode?: RoundingMode): bigint;
1096
1110
  */
1097
1111
 
1098
1112
  /** The fields of an order line this needs; a StoreOrderLine fits. */
1099
- type RefundableOrderLine = Pick<StoreOrderLine, 'id' | 'quantity' | 'totalMinor' | 'taxMinor'>;
1113
+ type RefundableOrderLine = Pick<StoreOrderLine, 'id' | 'quantity' | 'totalMinor' | 'taxMinor' | 'soldTotalMinor' | 'soldTaxMinor'>;
1100
1114
  /** Earlier refund lines; negative quantities count by magnitude. */
1101
1115
  interface EarlierRefund {
1102
1116
  lines: ReadonlyArray<Pick<OrderRefundLine, 'orderLineId' | 'quantity'>>;