@asumano/worklip-typeorm-db 1.1.52 → 1.1.53
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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/visit.summary.d.ts +81 -0
- package/dist/visit.summary.js +170 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { DataSource, EntityTarget, ObjectLiteral, Repository } from "typeorm";
|
|
2
2
|
import { IBasicFilter } from "./types";
|
|
3
3
|
export * from "./entities.views.routes";
|
|
4
|
+
export * from "./visit.summary";
|
|
4
5
|
export declare function createDataBaseSource(): DataSource;
|
|
5
6
|
export declare function showEntityName<T extends ObjectLiteral>(entity: EntityTarget<T>): Promise<Repository<T>>;
|
|
6
7
|
export declare function callStoredProcedureName(storedProcedure: any, elements: any): Promise<any>;
|
package/dist/index.js
CHANGED
|
@@ -48,6 +48,9 @@ const typeorm_1 = require("typeorm");
|
|
|
48
48
|
const EntitiesViewsRoutes = __importStar(require("./entities.index"));
|
|
49
49
|
const entities_views_routes_1 = require("./entities.views.routes");
|
|
50
50
|
__exportStar(require("./entities.views.routes"), exports);
|
|
51
|
+
// DOC: Cálculo del resumen de la visita. Lo consumen el front (en vivo) y el API
|
|
52
|
+
// (al guardar), y tiene que ser la MISMA implementación en los dos.
|
|
53
|
+
__exportStar(require("./visit.summary"), exports);
|
|
51
54
|
let connection = null;
|
|
52
55
|
// DOC: Función para crear la conexión de la base de datos.
|
|
53
56
|
function createDataBaseSource() {
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export type TItemStatus = "GOOD" | "REGULAR" | "BAD" | "NOT_APPLICABLE" | null;
|
|
2
|
+
export type TSummaryStatus = Exclude<TItemStatus, null> | "PENDING";
|
|
3
|
+
export type TCostApproval = "PENDING" | "APPROVED" | "REJECTED";
|
|
4
|
+
export interface IVisitCosts {
|
|
5
|
+
labor_cost: number;
|
|
6
|
+
replacement_cost: number;
|
|
7
|
+
total: number;
|
|
8
|
+
approval?: TCostApproval;
|
|
9
|
+
responded_at?: number | null;
|
|
10
|
+
}
|
|
11
|
+
export interface IVisitEvidence {
|
|
12
|
+
id: string;
|
|
13
|
+
type: "image" | "video";
|
|
14
|
+
key: string;
|
|
15
|
+
thumbnailKey?: string;
|
|
16
|
+
visible: boolean;
|
|
17
|
+
createdAt: number;
|
|
18
|
+
capturedAt?: number | null;
|
|
19
|
+
}
|
|
20
|
+
export interface IVisitItem {
|
|
21
|
+
id: string;
|
|
22
|
+
template_item_id: number | null;
|
|
23
|
+
custom_item: boolean;
|
|
24
|
+
name: string;
|
|
25
|
+
observation: string;
|
|
26
|
+
visible: boolean;
|
|
27
|
+
status: TItemStatus;
|
|
28
|
+
comments: string;
|
|
29
|
+
appointment_reminder?: string | Date | null;
|
|
30
|
+
costs: IVisitCosts | null;
|
|
31
|
+
evidence: IVisitEvidence[];
|
|
32
|
+
order: number;
|
|
33
|
+
}
|
|
34
|
+
export interface IVisitSection {
|
|
35
|
+
id: number;
|
|
36
|
+
name: string;
|
|
37
|
+
order: number;
|
|
38
|
+
items: IVisitItem[];
|
|
39
|
+
}
|
|
40
|
+
export interface IVisitData {
|
|
41
|
+
sections: IVisitSection[];
|
|
42
|
+
summary?: IVisitSummary;
|
|
43
|
+
}
|
|
44
|
+
export interface IVisitQuotationSummary {
|
|
45
|
+
servicePrice: number;
|
|
46
|
+
laborTotal: number;
|
|
47
|
+
replacementTotal: number;
|
|
48
|
+
quotedTotal: number;
|
|
49
|
+
approvedTotal: number;
|
|
50
|
+
rejectedTotal: number;
|
|
51
|
+
pendingTotal: number;
|
|
52
|
+
subtotal: number;
|
|
53
|
+
taxTotal: number;
|
|
54
|
+
totalToPay: number;
|
|
55
|
+
taxPercentage: number;
|
|
56
|
+
isTaxIncluded: boolean;
|
|
57
|
+
itemsQuoted: number;
|
|
58
|
+
itemsApproved: number;
|
|
59
|
+
itemsRejected: number;
|
|
60
|
+
itemsPending: number;
|
|
61
|
+
}
|
|
62
|
+
export interface IVisitSummary {
|
|
63
|
+
itemsTotal: number;
|
|
64
|
+
itemsReviewed: number;
|
|
65
|
+
progressPercentage: number;
|
|
66
|
+
statusCounts: Record<TSummaryStatus, number>;
|
|
67
|
+
vehicleHealthPercentage: number | null;
|
|
68
|
+
evidenceCount: number;
|
|
69
|
+
quotation: IVisitQuotationSummary;
|
|
70
|
+
}
|
|
71
|
+
export interface IPropsBuildVisitSummary {
|
|
72
|
+
servicePrice: number;
|
|
73
|
+
taxPercentage: number;
|
|
74
|
+
isTaxIncluded: boolean;
|
|
75
|
+
}
|
|
76
|
+
export declare const buildVisitSummary: (data: IVisitData | null | undefined, { servicePrice, taxPercentage, isTaxIncluded }: IPropsBuildVisitSummary) => IVisitSummary;
|
|
77
|
+
export declare const getTaxConfig: (companyDetails: any, countryDetails?: any) => {
|
|
78
|
+
taxPercentage: number;
|
|
79
|
+
isTaxIncluded: boolean;
|
|
80
|
+
};
|
|
81
|
+
export declare const preserveClientApprovals: (storedData: IVisitData | null | undefined, incomingData: IVisitData) => IVisitData;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// DOC: Cálculo del resumen de una visita a partir de su JSON (service_order.data):
|
|
3
|
+
// avance de la revisión, conteo por estado, salud del vehículo y cotización.
|
|
4
|
+
//
|
|
5
|
+
// Vive en la librería compartida porque la necesitan los dos lados y tiene que dar
|
|
6
|
+
// EXACTAMENTE el mismo resultado: el front la usa para pintar en vivo mientras se
|
|
7
|
+
// edita la visita, y el API para recalcular y guardar el resumen en cada guardado.
|
|
8
|
+
// Si estuviera duplicada, el número que ve el usuario y el que queda guardado
|
|
9
|
+
// podrían desviarse.
|
|
10
|
+
//
|
|
11
|
+
// Es una función pura sobre el JSON: no toca la base de datos ni depende de TypeORM.
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.preserveClientApprovals = exports.getTaxConfig = exports.buildVisitSummary = void 0;
|
|
14
|
+
// DOC: Peso de cada estado para la "salud del vehículo". NOT_APPLICABLE y los
|
|
15
|
+
// ítems sin marcar quedan fuera del promedio.
|
|
16
|
+
const HEALTH_WEIGHT = {
|
|
17
|
+
GOOD: 100,
|
|
18
|
+
REGULAR: 50,
|
|
19
|
+
BAD: 0,
|
|
20
|
+
};
|
|
21
|
+
const round2 = (value) => Math.round(value * 100) / 100;
|
|
22
|
+
const buildVisitSummary = (data, { servicePrice, taxPercentage, isTaxIncluded }) => {
|
|
23
|
+
const statusCounts = {
|
|
24
|
+
GOOD: 0,
|
|
25
|
+
REGULAR: 0,
|
|
26
|
+
BAD: 0,
|
|
27
|
+
NOT_APPLICABLE: 0,
|
|
28
|
+
PENDING: 0,
|
|
29
|
+
};
|
|
30
|
+
let itemsTotal = 0;
|
|
31
|
+
let evidenceCount = 0;
|
|
32
|
+
let healthSum = 0;
|
|
33
|
+
let healthCount = 0;
|
|
34
|
+
let laborTotal = 0;
|
|
35
|
+
let replacementTotal = 0;
|
|
36
|
+
let approvedTotal = 0;
|
|
37
|
+
let rejectedTotal = 0;
|
|
38
|
+
let pendingTotal = 0;
|
|
39
|
+
let itemsQuoted = 0;
|
|
40
|
+
let itemsApproved = 0;
|
|
41
|
+
let itemsRejected = 0;
|
|
42
|
+
let itemsPending = 0;
|
|
43
|
+
for (const section of data?.sections ?? []) {
|
|
44
|
+
for (const item of section.items ?? []) {
|
|
45
|
+
// DOC: Un ítem oculto no va en el informe del cliente, así que no pide
|
|
46
|
+
// revisión ni se le cotiza nada.
|
|
47
|
+
if (item.visible === false)
|
|
48
|
+
continue;
|
|
49
|
+
itemsTotal++;
|
|
50
|
+
statusCounts[item.status ?? "PENDING"]++;
|
|
51
|
+
if (item.status && item.status in HEALTH_WEIGHT) {
|
|
52
|
+
healthSum += HEALTH_WEIGHT[item.status];
|
|
53
|
+
healthCount++;
|
|
54
|
+
}
|
|
55
|
+
evidenceCount += (item.evidence ?? []).filter((file) => file.visible !== false).length;
|
|
56
|
+
if (!item.costs)
|
|
57
|
+
continue;
|
|
58
|
+
const total = Number(item.costs.total) || 0;
|
|
59
|
+
itemsQuoted++;
|
|
60
|
+
laborTotal += Number(item.costs.labor_cost) || 0;
|
|
61
|
+
replacementTotal += Number(item.costs.replacement_cost) || 0;
|
|
62
|
+
const approval = item.costs.approval ?? "PENDING";
|
|
63
|
+
if (approval === "APPROVED") {
|
|
64
|
+
itemsApproved++;
|
|
65
|
+
approvedTotal += total;
|
|
66
|
+
}
|
|
67
|
+
else if (approval === "REJECTED") {
|
|
68
|
+
itemsRejected++;
|
|
69
|
+
rejectedTotal += total;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
itemsPending++;
|
|
73
|
+
pendingTotal += total;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
const itemsReviewed = itemsTotal - statusCounts.PENDING;
|
|
78
|
+
// DOC: Lo que efectivamente se va a cobrar: el servicio fijo de la visita más
|
|
79
|
+
// 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
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
itemsTotal,
|
|
101
|
+
itemsReviewed,
|
|
102
|
+
progressPercentage: itemsTotal === 0 ? 0 : Math.round((itemsReviewed / itemsTotal) * 100),
|
|
103
|
+
statusCounts,
|
|
104
|
+
vehicleHealthPercentage: healthCount === 0 ? null : Math.round(healthSum / healthCount),
|
|
105
|
+
evidenceCount,
|
|
106
|
+
quotation: {
|
|
107
|
+
servicePrice: round2(Number(servicePrice) || 0),
|
|
108
|
+
laborTotal: round2(laborTotal),
|
|
109
|
+
replacementTotal: round2(replacementTotal),
|
|
110
|
+
quotedTotal: round2(approvedTotal + rejectedTotal + pendingTotal),
|
|
111
|
+
approvedTotal: round2(approvedTotal),
|
|
112
|
+
rejectedTotal: round2(rejectedTotal),
|
|
113
|
+
pendingTotal: round2(pendingTotal),
|
|
114
|
+
subtotal,
|
|
115
|
+
taxTotal,
|
|
116
|
+
totalToPay,
|
|
117
|
+
taxPercentage: Number(taxPercentage) || 0,
|
|
118
|
+
isTaxIncluded,
|
|
119
|
+
itemsQuoted,
|
|
120
|
+
itemsApproved,
|
|
121
|
+
itemsRejected,
|
|
122
|
+
itemsPending,
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
exports.buildVisitSummary = buildVisitSummary;
|
|
127
|
+
// DOC: Configuración de impuesto de la empresa dueña de la visita, con la tarifa
|
|
128
|
+
// del país como respaldo. Si no hay tarifa, no se aplica impuesto.
|
|
129
|
+
const getTaxConfig = (companyDetails, countryDetails) => {
|
|
130
|
+
const company = companyDetails || {};
|
|
131
|
+
const country = countryDetails || {};
|
|
132
|
+
return {
|
|
133
|
+
taxPercentage: Number(company.tax_percentage ?? country.tax_percentage ?? 0) || 0,
|
|
134
|
+
isTaxIncluded: company.is_tax_included ?? true,
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
exports.getTaxConfig = getTaxConfig;
|
|
138
|
+
// DOC: La respuesta del cliente (aprobar/rechazar) es SUYA, no del formulario del
|
|
139
|
+
// asesor. El asesor autoguarda todo el `data` cada pocos segundos con la copia que
|
|
140
|
+
// tenía cargada, así que si no se protegen estos campos, un autoguardado borraría
|
|
141
|
+
// una aprobación que llegó en el intermedio.
|
|
142
|
+
//
|
|
143
|
+
// Regla: se conserva la respuesta guardada en la base de datos, salvo que el
|
|
144
|
+
// asesor haya cambiado el valor cotizado — ahí vuelve a PENDING, porque nadie
|
|
145
|
+
// puede quedar avalando un monto distinto al que aceptó.
|
|
146
|
+
const preserveClientApprovals = (storedData, incomingData) => {
|
|
147
|
+
const storedCosts = new Map();
|
|
148
|
+
for (const section of storedData?.sections ?? []) {
|
|
149
|
+
for (const item of section.items ?? []) {
|
|
150
|
+
if (item.costs)
|
|
151
|
+
storedCosts.set(item.id, item.costs);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (storedCosts.size === 0)
|
|
155
|
+
return incomingData;
|
|
156
|
+
for (const section of incomingData?.sections ?? []) {
|
|
157
|
+
for (const item of section.items ?? []) {
|
|
158
|
+
if (!item.costs)
|
|
159
|
+
continue;
|
|
160
|
+
const stored = storedCosts.get(item.id);
|
|
161
|
+
if (!stored)
|
|
162
|
+
continue;
|
|
163
|
+
const hasChanged = (Number(item.costs.total) || 0) !== (Number(stored.total) || 0);
|
|
164
|
+
item.costs.approval = hasChanged ? "PENDING" : (stored.approval ?? "PENDING");
|
|
165
|
+
item.costs.responded_at = hasChanged ? null : (stored.responded_at ?? null);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return incomingData;
|
|
169
|
+
};
|
|
170
|
+
exports.preserveClientApprovals = preserveClientApprovals;
|