@asumano/worklip-typeorm-db 1.1.59 → 1.1.60

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.
@@ -1,12 +1,14 @@
1
1
  export type TItemStatus = "GOOD" | "REGULAR" | "BAD" | "NOT_APPLICABLE" | null;
2
2
  export type TSummaryStatus = Exclude<TItemStatus, null> | "PENDING";
3
3
  export type TCostApproval = "PENDING" | "APPROVED" | "REJECTED";
4
+ export type TTaxMode = "inherit" | "exempt" | "taxed";
4
5
  export interface IVisitCosts {
5
6
  labor_cost: number;
6
7
  replacement_cost: number;
7
8
  total: number;
8
9
  approval?: TCostApproval;
9
10
  responded_at?: number | null;
11
+ tax_mode?: TTaxMode;
10
12
  }
11
13
  export interface IVisitEvidence {
12
14
  id: string;
@@ -40,6 +42,7 @@ export interface IVisitSection {
40
42
  export interface IVisitData {
41
43
  sections: IVisitSection[];
42
44
  summary?: IVisitSummary;
45
+ service_tax_mode?: TTaxMode;
43
46
  }
44
47
  export interface IVisitQuotationSummary {
45
48
  servicePrice: number;
@@ -50,10 +53,13 @@ export interface IVisitQuotationSummary {
50
53
  rejectedTotal: number;
51
54
  pendingTotal: number;
52
55
  subtotal: number;
56
+ taxableBase: number;
57
+ exemptBase: number;
53
58
  taxTotal: number;
54
59
  totalToPay: number;
55
60
  taxPercentage: number;
56
61
  isTaxIncluded: boolean;
62
+ conceptsExempt: number;
57
63
  itemsQuoted: number;
58
64
  itemsApproved: number;
59
65
  itemsRejected: number;
@@ -19,6 +19,10 @@ const HEALTH_WEIGHT = {
19
19
  BAD: 0,
20
20
  };
21
21
  const round2 = (value) => Math.round(value * 100) / 100;
22
+ // DOC: Un concepto se grava salvo que esté exento, y solo si la empresa tiene tarifa:
23
+ // sin tarifa no hay con qué calcular, así que ni "taxed" puede inventarse un impuesto.
24
+ // Ausente = "inherit" = lo que haga la empresa.
25
+ const isConceptTaxed = (mode, rate) => rate > 0 && (mode ?? "inherit") !== "exempt";
22
26
  const buildVisitSummary = (data, { servicePrice, taxPercentage, isTaxIncluded }) => {
23
27
  const statusCounts = {
24
28
  GOOD: 0,
@@ -40,6 +44,27 @@ const buildVisitSummary = (data, { servicePrice, taxPercentage, isTaxIncluded })
40
44
  let itemsApproved = 0;
41
45
  let itemsRejected = 0;
42
46
  let itemsPending = 0;
47
+ const rate = (Number(taxPercentage) || 0) / 100;
48
+ // DOC: Acumuladores del impuesto EN CRUDO, sin redondear. Con conceptos mezclados
49
+ // hay que sumar concepto por concepto, y si se redondea cada uno la suma se
50
+ // desvía del total y el documento queda descuadrado por pesos sueltos.
51
+ let rawTax = 0;
52
+ let rawExemptBase = 0;
53
+ let conceptsExempt = 0;
54
+ // DOC: Suma un concepto cobrable (una operación aprobada o el precio del servicio).
55
+ // `amount` viene en los mismos términos en que se cotizó: con impuesto adentro si
56
+ // isTaxIncluded, sin él si no.
57
+ const addChargeableConcept = (amount, mode) => {
58
+ if (isConceptTaxed(mode, rate)) {
59
+ rawTax += isTaxIncluded ? amount - amount / (1 + rate) : amount * rate;
60
+ return;
61
+ }
62
+ rawExemptBase += amount;
63
+ // DOC: Solo se cuenta la exención que de verdad cambió algo. Si la empresa no
64
+ // cobra impuesto, nada está "exento": simplemente no hay impuesto.
65
+ if (rate > 0 && (mode ?? "inherit") === "exempt")
66
+ conceptsExempt++;
67
+ };
43
68
  for (const section of data?.sections ?? []) {
44
69
  for (const item of section.items ?? []) {
45
70
  // DOC: Un ítem oculto no va en el informe del cliente, así que no pide
@@ -63,6 +88,8 @@ const buildVisitSummary = (data, { servicePrice, taxPercentage, isTaxIncluded })
63
88
  if (approval === "APPROVED") {
64
89
  itemsApproved++;
65
90
  approvedTotal += total;
91
+ // DOC: Solo lo aprobado se cobra, así que solo lo aprobado lleva impuesto.
92
+ addChargeableConcept(total, item.costs.tax_mode);
66
93
  }
67
94
  else if (approval === "REJECTED") {
68
95
  itemsRejected++;
@@ -75,27 +102,33 @@ const buildVisitSummary = (data, { servicePrice, taxPercentage, isTaxIncluded })
75
102
  }
76
103
  }
77
104
  const itemsReviewed = itemsTotal - statusCounts.PENDING;
105
+ // DOC: El precio del servicio es un concepto cobrable más, con su propio modo.
106
+ const price = Number(servicePrice) || 0;
107
+ addChargeableConcept(price, data?.service_tax_mode);
78
108
  // DOC: Lo que efectivamente se va a cobrar: el servicio fijo de la visita más
79
109
  // las reparaciones que el cliente aprobó. Lo pendiente y lo rechazado no entran.
80
- const chargeable = round2((Number(servicePrice) || 0) + approvedTotal);
81
- const rate = (Number(taxPercentage) || 0) / 100;
82
- let subtotal = chargeable;
83
- let taxTotal = 0;
84
- let totalToPay = chargeable;
85
- if (rate > 0) {
86
- if (isTaxIncluded) {
87
- // DOC: Los valores digitados ya traen el impuesto: se desglosa hacia atrás.
88
- // El impuesto se calcula como total - base para que la suma cuadre
89
- // exacto con el total.
90
- subtotal = round2(chargeable / (1 + rate));
91
- taxTotal = round2(chargeable - subtotal);
92
- totalToPay = chargeable;
93
- }
94
- else {
95
- taxTotal = round2(chargeable * rate);
96
- totalToPay = round2(chargeable + taxTotal);
97
- }
98
- }
110
+ const chargeableRaw = price + approvedTotal;
111
+ const taxTotal = round2(rawTax);
112
+ // DOC: Cuál de los tres números manda depende de cómo se cotizó, y el resto se
113
+ // DERIVA de él. Si se redondearan los tres por separado, el documento mostraría
114
+ // un subtotal + IVA que no da el total.
115
+ //
116
+ // · Precios con impuesto adentro -> manda el TOTAL: el cliente paga lo que se le
117
+ // cotizó, pase lo que pase con el desglose.
118
+ // · Precios sin impuesto -> manda la BASE: lo cotizado es la base y el
119
+ // total se construye sumándole el impuesto.
120
+ const totalToPay = isTaxIncluded
121
+ ? round2(chargeableRaw)
122
+ : round2(round2(chargeableRaw) + taxTotal);
123
+ const subtotal = isTaxIncluded
124
+ ? round2(totalToPay - taxTotal)
125
+ : round2(chargeableRaw);
126
+ // DOC: Lo exento nunca se divide (no hay impuesto que sacarle), así que su base es
127
+ // exacta; la gravada se deriva para que las dos sumen el subtotal sin descuadre.
128
+ // Sin tarifa de empresa todo cae en exenta y la gravada queda en 0: no hay
129
+ // impuesto en juego, y así se mantiene taxableBase + exemptBase === subtotal.
130
+ const exemptBase = round2(rawExemptBase);
131
+ const taxableBase = round2(subtotal - exemptBase);
99
132
  return {
100
133
  itemsTotal,
101
134
  itemsReviewed,
@@ -104,7 +137,7 @@ const buildVisitSummary = (data, { servicePrice, taxPercentage, isTaxIncluded })
104
137
  vehicleHealthPercentage: healthCount === 0 ? null : Math.round(healthSum / healthCount),
105
138
  evidenceCount,
106
139
  quotation: {
107
- servicePrice: round2(Number(servicePrice) || 0),
140
+ servicePrice: round2(price),
108
141
  laborTotal: round2(laborTotal),
109
142
  replacementTotal: round2(replacementTotal),
110
143
  quotedTotal: round2(approvedTotal + rejectedTotal + pendingTotal),
@@ -112,10 +145,13 @@ const buildVisitSummary = (data, { servicePrice, taxPercentage, isTaxIncluded })
112
145
  rejectedTotal: round2(rejectedTotal),
113
146
  pendingTotal: round2(pendingTotal),
114
147
  subtotal,
148
+ taxableBase,
149
+ exemptBase,
115
150
  taxTotal,
116
151
  totalToPay,
117
152
  taxPercentage: Number(taxPercentage) || 0,
118
153
  isTaxIncluded,
154
+ conceptsExempt,
119
155
  itemsQuoted,
120
156
  itemsApproved,
121
157
  itemsRejected,
@@ -184,7 +220,14 @@ const preserveClientApprovals = (storedData, incomingData) => {
184
220
  const stored = storedCosts.get(item.id);
185
221
  if (!stored)
186
222
  continue;
187
- const hasChanged = (Number(item.costs.total) || 0) !== (Number(stored.total) || 0);
223
+ // DOC: También cuenta el cambio de `tax_mode`, no solo el del monto: exentar o
224
+ // gravar un concepto mueve lo que el cliente termina pagando sin tocar
225
+ // `total`. Sin esto, quedaría avalando un total distinto al que aceptó.
226
+ // (Con precios que ya incluyen impuesto el total no se mueve, pero sí el
227
+ // desglose del documento; se reinicia igual para no depender de esa
228
+ // configuración.)
229
+ const hasChanged = (Number(item.costs.total) || 0) !== (Number(stored.total) || 0) ||
230
+ (item.costs.tax_mode ?? "inherit") !== (stored.tax_mode ?? "inherit");
188
231
  item.costs.approval = hasChanged ? "PENDING" : (stored.approval ?? "PENDING");
189
232
  item.costs.responded_at = hasChanged ? null : (stored.responded_at ?? null);
190
233
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asumano/worklip-typeorm-db",
3
- "version": "1.1.59",
3
+ "version": "1.1.60",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },