rapida-partner 8.19.0 → 8.20.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.
- package/package.json +1 -1
- package/src/interfaces/counter.interface.ts +7 -2
- package/src/interfaces/service-tab.interface.ts +37 -0
- package/src/interfaces/store-order.interface.ts +9 -0
- package/src/interfaces/store-settings.interface.ts +3 -0
- package/src/schemas/store-settings.ref.json +1 -0
- package/src/utils/delivery-settlement.test.ts +34 -0
- package/src/utils/delivery-settlement.ts +50 -0
- package/src/utils/service-tab.test.ts +24 -0
- package/src/utils/service-tab.ts +38 -0
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { TServiceMode } from "./service-tab.interface";
|
|
1
2
|
import type { TStorePaymentChoice } from "./store-order.interface";
|
|
2
3
|
|
|
3
|
-
/** Canal da venda: loja online (padrão)
|
|
4
|
-
export type TStoreSalesChannel = "online" | "counter";
|
|
4
|
+
/** Canal da venda: loja online (padrão), balcão (PDV, Fase 6) ou atendente na mesa ou comanda (Fase 7). */
|
|
5
|
+
export type TStoreSalesChannel = "online" | "counter" | "service";
|
|
5
6
|
|
|
6
7
|
/** Formas aceitas no caixa do balcão: dinheiro, Pix pela chave da loja (QR), Pix na maquininha, crédito e débito. */
|
|
7
8
|
export type TCounterPaymentChoice = Extract<TStorePaymentChoice, "cash" | "pixDirect" | "pixTerminal" | "creditCard" | "debitCard">;
|
|
@@ -35,4 +36,8 @@ export interface ICashSettings {
|
|
|
35
36
|
blindClose?: boolean;
|
|
36
37
|
paymentChoices?: TCounterPaymentChoice[];
|
|
37
38
|
registers?: string[];
|
|
39
|
+
// Mesas e comandas (Fase 7): o modo da loja e a numeração de cada uma.
|
|
40
|
+
serviceMode?: TServiceMode;
|
|
41
|
+
tables?: string[];
|
|
42
|
+
tabs?: string[];
|
|
38
43
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { IStoreOrderPayment } from "./counter.interface";
|
|
2
|
+
|
|
3
|
+
/** Onde o atendente lança o pedido: mesa ou comanda numerada (Fase 7 do PDV). */
|
|
4
|
+
export type TServiceSpotKind = "table" | "tab";
|
|
5
|
+
|
|
6
|
+
/** Modo da loja: só mesas, só comandas ou as duas. Sem modo, a tela do atendente fica desligada. */
|
|
7
|
+
export type TServiceMode = "tables" | "tabs" | "both";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Situação da conta de uma mesa ou comanda: aberta (aceita itens), conta pedida (pré-conta impressa, não aceita
|
|
11
|
+
* itens) e fechada (paga por completo; a mesa ou comanda volta a ficar livre).
|
|
12
|
+
*/
|
|
13
|
+
export type TServiceTabStatus = "open" | "billRequested" | "closed";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Conta aberta de uma mesa ou comanda: junta os pedidos enviados à cozinha até o pagamento. Cada pagamento entra no
|
|
17
|
+
* turno de um caixa aberto (cashShiftId), mesmo quando o atendente cobra na mesa com a maquininha. A conta pode ser
|
|
18
|
+
* dividida entre pessoas: vários pagamentos até zerar o total.
|
|
19
|
+
*/
|
|
20
|
+
export interface IServiceTab {
|
|
21
|
+
kind: TServiceSpotKind;
|
|
22
|
+
label: string;
|
|
23
|
+
status: TServiceTabStatus;
|
|
24
|
+
orderIds: string[];
|
|
25
|
+
total: number;
|
|
26
|
+
paid: number;
|
|
27
|
+
payments: (IStoreOrderPayment & { cashShiftId: string; createdAt: string })[];
|
|
28
|
+
openedBy: string;
|
|
29
|
+
openedAt: string;
|
|
30
|
+
closedAt?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Mesa ou comanda do pedido lançado pelo atendente (aparece no quadro, por exemplo "Mesa 7"). */
|
|
34
|
+
export interface IServiceSpot {
|
|
35
|
+
kind: TServiceSpotKind;
|
|
36
|
+
label: string;
|
|
37
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IServiceSpot } from "./service-tab.interface";
|
|
1
2
|
import type { IStoreOrderPayment, TStoreSalesChannel } from "./counter.interface";
|
|
2
3
|
import type { TStoreOptionGroupKind } from "./store-option.interface";
|
|
3
4
|
|
|
@@ -155,6 +156,11 @@ export interface IStoreOrder {
|
|
|
155
156
|
nsu?: string;
|
|
156
157
|
// Conta a receber gerada quando o pedido foi entregue (Fase 3 do PDV); voltar da entrega ou cancelar a estorna.
|
|
157
158
|
receivableAccountId?: string;
|
|
159
|
+
// Acerto do entregador no caixa: quando o caixa conferiu a forma e a maquininha e recebeu o dinheiro, em qual
|
|
160
|
+
// turno e por quem. Vazio enquanto o pedido pago na entrega ou na retirada não foi acertado.
|
|
161
|
+
deliverySettledAt?: string;
|
|
162
|
+
deliverySettledShiftId?: string;
|
|
163
|
+
deliverySettledBy?: string;
|
|
158
164
|
// Saídas de estoque dos insumos da ficha técnica (Fase 5.2 do PDV); cancelar ou voltar antes da etapa estorna.
|
|
159
165
|
recipeStockOutIds?: string[];
|
|
160
166
|
// Venda do balcão (Fase 6): entra no quadro em "Recebido" e segue o mesmo fluxo, mas já paga. payments lista
|
|
@@ -165,6 +171,9 @@ export interface IStoreOrder {
|
|
|
165
171
|
changeGiven?: number;
|
|
166
172
|
cashShiftId?: string;
|
|
167
173
|
receivableAccountIds?: string[];
|
|
174
|
+
// Pedido do atendente (Fase 7): a mesa ou comanda e a conta aberta que junta os pedidos até o pagamento.
|
|
175
|
+
serviceSpot?: IServiceSpot;
|
|
176
|
+
serviceTabId?: string;
|
|
168
177
|
// Foto do custo na entrega (Fase 5.3 do PDV): custo unitário de cada produto (ficha técnica ou custo médio de
|
|
169
178
|
// compra) na hora da entrega, usado pelo relatório de CMV. null quando o custo não era conhecido.
|
|
170
179
|
itemCosts?: { productId: string; unitCost: number | null }[];
|
|
@@ -171,6 +171,9 @@ export interface IStoreSettings extends Partial<Record<TStoreFeatureToggle, bool
|
|
|
171
171
|
pixBankAccountId?: string;
|
|
172
172
|
// Conta do dinheiro (tipo "cash", Fase 3 do PDV): o pedido entregue pago em dinheiro entra como recebido nela.
|
|
173
173
|
cashBankAccountId?: string;
|
|
174
|
+
// Maquininha que sai com o entregador (acerto do entregador): o pedido pago no cartão na entrega usa esta,
|
|
175
|
+
// sem perguntar a quem está no quadro; o caixa corrige no acerto, se o entregador usou outra.
|
|
176
|
+
deliveryCardTerminalId?: string;
|
|
174
177
|
// Etapa em que a ficha técnica baixa os insumos (Fase 5.2 do PDV). Padrão "preparing".
|
|
175
178
|
recipeStockStage?: TRecipeStockStage;
|
|
176
179
|
// Funcionalidade "stockReservation": por quantos minutos o pedido aguardando
|
|
@@ -94,6 +94,7 @@
|
|
|
94
94
|
"pixCity": { "type": "string" },
|
|
95
95
|
"pixBankAccountId": { "type": "string" },
|
|
96
96
|
"cashBankAccountId": { "type": "string" },
|
|
97
|
+
"deliveryCardTerminalId": { "type": "string" },
|
|
97
98
|
"recipeStockStage": { "enum": ["received", "preparing", "delivered"] },
|
|
98
99
|
"reservationMinutes": { "type": "integer", "minimum": 5, "maximum": 60 },
|
|
99
100
|
"ordersPaused": { "type": "boolean" },
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { deliverySettlementActionOf, deliverySettlementSummaryOf, deliveryTerminalOf } from "./delivery-settlement";
|
|
3
|
+
|
|
4
|
+
describe("maquininha do pagamento na entrega", () => {
|
|
5
|
+
test("cartão usa a informada ou a padrão da entrega; dinheiro e Pix direto não usam", () => {
|
|
6
|
+
expect(deliveryTerminalOf("creditCard", null, "t-moto")).toBe("t-moto");
|
|
7
|
+
expect(deliveryTerminalOf("debitCard", "t-balcao", "t-moto")).toBe("t-balcao");
|
|
8
|
+
expect(deliveryTerminalOf("pixTerminal", undefined, undefined)).toBeNull();
|
|
9
|
+
expect(deliveryTerminalOf("cash", "t-balcao", "t-moto")).toBeNull();
|
|
10
|
+
expect(deliveryTerminalOf("pixDirect", null, "t-moto")).toBeNull();
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe("acerto do entregador", () => {
|
|
15
|
+
test("sem conta lança; igual mantém; forma ou maquininha diferente estorna e lança de novo", () => {
|
|
16
|
+
expect(deliverySettlementActionOf({ hasReceivable: false }, { paymentChoice: "creditCard", cardTerminalId: "t1" })).toBe("post");
|
|
17
|
+
expect(deliverySettlementActionOf({ hasReceivable: true, paymentChoice: "cash" }, { paymentChoice: "cash", cardTerminalId: "t1" })).toBe("keep");
|
|
18
|
+
expect(deliverySettlementActionOf({ hasReceivable: true, paymentChoice: "creditCard", cardTerminalId: "t1" }, { paymentChoice: "creditCard", cardTerminalId: "t1" })).toBe("keep");
|
|
19
|
+
expect(deliverySettlementActionOf({ hasReceivable: true, paymentChoice: "creditCard", cardTerminalId: "t1" }, { paymentChoice: "creditCard", cardTerminalId: "t2" })).toBe("repost");
|
|
20
|
+
expect(deliverySettlementActionOf({ hasReceivable: true, paymentChoice: "pixDirect" }, { paymentChoice: "cash" })).toBe("repost");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("resumo: dinheiro para a gaveta, maquininha, Pix direto e cartões sem maquininha", () => {
|
|
24
|
+
expect(
|
|
25
|
+
deliverySettlementSummaryOf([
|
|
26
|
+
{ total: 42.5, paymentChoice: "cash" },
|
|
27
|
+
{ total: 30, paymentChoice: "creditCard", cardTerminalId: "t1" },
|
|
28
|
+
{ total: 18, paymentChoice: "debitCard" },
|
|
29
|
+
{ total: 25, paymentChoice: "pixDirect" },
|
|
30
|
+
{ total: 10.1, paymentChoice: "cash" },
|
|
31
|
+
]),
|
|
32
|
+
).toEqual({ cash: 52.6, terminal: 48, pixDirect: 25, missingTerminal: 1 });
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { TStorePaymentChoice } from "../interfaces/store-order.interface";
|
|
2
|
+
|
|
3
|
+
const cents = (value: number): number => Math.round(value * 100) / 100;
|
|
4
|
+
|
|
5
|
+
const TERMINAL_CHOICES: TStorePaymentChoice[] = ["debitCard", "creditCard", "pixTerminal"];
|
|
6
|
+
|
|
7
|
+
/** Pedido pago na entrega ou na retirada, como o caixa vê no acerto do entregador. */
|
|
8
|
+
export interface IDeliverySettlementLine {
|
|
9
|
+
total: number;
|
|
10
|
+
paymentChoice: TStorePaymentChoice;
|
|
11
|
+
cardTerminalId?: string | null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** O que o acerto faz com o pedido: nada (já lançado certo), lançar (ainda sem conta) ou estornar e lançar de novo. */
|
|
15
|
+
export type TDeliverySettlementAction = "keep" | "post" | "repost";
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Maquininha do pagamento na entrega: a informada (no acerto) ou a padrão da entrega da loja. Dinheiro e Pix direto
|
|
19
|
+
* não usam maquininha.
|
|
20
|
+
*/
|
|
21
|
+
export const deliveryTerminalOf = (
|
|
22
|
+
choice: TStorePaymentChoice | undefined,
|
|
23
|
+
informed: string | null | undefined,
|
|
24
|
+
deliveryDefault: string | null | undefined,
|
|
25
|
+
): string | null => {
|
|
26
|
+
if (!choice || !TERMINAL_CHOICES.includes(choice)) return null;
|
|
27
|
+
return informed || deliveryDefault || null;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Compara o lançado na entrega com o conferido pelo caixa no acerto. */
|
|
31
|
+
export const deliverySettlementActionOf = (
|
|
32
|
+
posted: { hasReceivable: boolean; paymentChoice?: TStorePaymentChoice; cardTerminalId?: string | null },
|
|
33
|
+
checked: { paymentChoice: TStorePaymentChoice; cardTerminalId?: string | null },
|
|
34
|
+
): TDeliverySettlementAction => {
|
|
35
|
+
if (!posted.hasReceivable) return "post";
|
|
36
|
+
const terminal = (choice: TStorePaymentChoice, id: string | null | undefined): string => (TERMINAL_CHOICES.includes(choice) ? id || "" : "");
|
|
37
|
+
const same = posted.paymentChoice === checked.paymentChoice && terminal(checked.paymentChoice, posted.cardTerminalId) === terminal(checked.paymentChoice, checked.cardTerminalId);
|
|
38
|
+
return same ? "keep" : "repost";
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Resumo do acerto: o dinheiro que o entregador entrega na gaveta e o que passou na maquininha ou no Pix. */
|
|
42
|
+
export const deliverySettlementSummaryOf = (lines: IDeliverySettlementLine[]): { cash: number; terminal: number; pixDirect: number; missingTerminal: number } => {
|
|
43
|
+
const sum = (filter: (line: IDeliverySettlementLine) => boolean): number => cents(lines.filter(filter).reduce((total, line) => total + (Number(line.total) || 0), 0));
|
|
44
|
+
return {
|
|
45
|
+
cash: sum((line) => line.paymentChoice === "cash"),
|
|
46
|
+
terminal: sum((line) => TERMINAL_CHOICES.includes(line.paymentChoice)),
|
|
47
|
+
pixDirect: sum((line) => line.paymentChoice === "pixDirect"),
|
|
48
|
+
missingTerminal: lines.filter((line) => TERMINAL_CHOICES.includes(line.paymentChoice) && !line.cardTerminalId).length,
|
|
49
|
+
};
|
|
50
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { mergeServiceLabels, serviceLabelsOfRange, serviceTabPaymentOf, serviceTabTotalOf } from "./service-tab";
|
|
3
|
+
|
|
4
|
+
describe("numeração das mesas e comandas", () => {
|
|
5
|
+
test("faixa e nomes avulsos, sem repetir", () => {
|
|
6
|
+
expect(serviceLabelsOfRange(1, 4)).toEqual(["1", "2", "3", "4"]);
|
|
7
|
+
expect(serviceLabelsOfRange(5, 3)).toEqual(["3", "4", "5"]);
|
|
8
|
+
expect(serviceLabelsOfRange(1, 1000)).toHaveLength(300);
|
|
9
|
+
expect(mergeServiceLabels(["1", "2"], ["2", " Varanda 1 ", ""])).toEqual(["1", "2", "Varanda 1"]);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe("conta da mesa", () => {
|
|
14
|
+
test("total sem os pedidos cancelados", () => {
|
|
15
|
+
expect(serviceTabTotalOf([{ status: "delivered", total: 40 }, { status: "cancelled", total: 15 }, { status: "preparing", total: 22.5 }])).toBe(62.5);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("dividir entre pessoas: pagamento parcial abate, o último fecha e só o dinheiro dá troco", () => {
|
|
19
|
+
expect(serviceTabPaymentOf(62.5, [{ paymentChoice: "debitCard", amount: 30 }])).toEqual({ ok: true, applied: 30, change: 0, closes: false });
|
|
20
|
+
expect(serviceTabPaymentOf(32.5, [{ paymentChoice: "cash", amount: 50 }])).toEqual({ ok: true, applied: 32.5, change: 17.5, closes: true });
|
|
21
|
+
expect(serviceTabPaymentOf(32.5, [{ paymentChoice: "pixDirect", amount: 40 }])).toEqual({ ok: false, reason: "overpaidWithoutCash" });
|
|
22
|
+
expect(serviceTabPaymentOf(0, [{ paymentChoice: "cash", amount: 5 }])).toEqual({ ok: false, reason: "alreadyPaid" });
|
|
23
|
+
});
|
|
24
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { IStoreOrderPayment } from "../interfaces/counter.interface";
|
|
2
|
+
|
|
3
|
+
const cents = (value: number): number => Math.round(value * 100) / 100;
|
|
4
|
+
|
|
5
|
+
/** Numeração por faixa ("1" a "20"), sem repetir e no máximo 300 lugares. */
|
|
6
|
+
export const serviceLabelsOfRange = (from: number, to: number): string[] => {
|
|
7
|
+
const start = Math.max(1, Math.trunc(Math.min(from, to)));
|
|
8
|
+
const end = Math.min(Math.trunc(Math.max(from, to)), start + 299);
|
|
9
|
+
return Array.from({ length: end - start + 1 }, (_, index) => String(start + index));
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/** Junta a numeração nova à atual, sem repetir e sem nomes vazios, na ordem em que foi cadastrada. */
|
|
13
|
+
export const mergeServiceLabels = (current: string[], added: string[]): string[] =>
|
|
14
|
+
[...new Set([...current, ...added].map((label) => label.trim()).filter(Boolean))];
|
|
15
|
+
|
|
16
|
+
/** Total da conta: soma dos pedidos não cancelados da mesa ou comanda. */
|
|
17
|
+
export const serviceTabTotalOf = (orders: { status: string; total: number }[]): number =>
|
|
18
|
+
cents(orders.filter((order) => order.status !== "cancelled").reduce((sum, order) => sum + (Number(order.total) || 0), 0));
|
|
19
|
+
|
|
20
|
+
export type TServiceTabPaymentRefusal = "noPayment" | "invalidAmount" | "overpaidWithoutCash" | "alreadyPaid";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Um pagamento da conta (a conta pode ser dividida entre pessoas): cada valor positivo; só o dinheiro passa do que
|
|
24
|
+
* falta (troco). Devolve quanto abate da conta, o troco e se a conta fecha.
|
|
25
|
+
*/
|
|
26
|
+
export const serviceTabPaymentOf = (
|
|
27
|
+
rest: number,
|
|
28
|
+
payments: IStoreOrderPayment[],
|
|
29
|
+
): { ok: true; applied: number; change: number; closes: boolean } | { ok: false; reason: TServiceTabPaymentRefusal } => {
|
|
30
|
+
if (rest <= 0) return { ok: false, reason: "alreadyPaid" };
|
|
31
|
+
if (payments.length === 0) return { ok: false, reason: "noPayment" };
|
|
32
|
+
if (payments.some((payment) => !(payment.amount > 0))) return { ok: false, reason: "invalidAmount" };
|
|
33
|
+
const nonCash = cents(payments.filter((payment) => payment.paymentChoice !== "cash").reduce((sum, payment) => sum + payment.amount, 0));
|
|
34
|
+
if (nonCash > cents(rest) + 0.001) return { ok: false, reason: "overpaidWithoutCash" };
|
|
35
|
+
const paid = cents(payments.reduce((sum, payment) => sum + payment.amount, 0));
|
|
36
|
+
const applied = cents(Math.min(paid, rest));
|
|
37
|
+
return { ok: true, applied, change: cents(paid - applied), closes: applied + 0.001 >= cents(rest) };
|
|
38
|
+
};
|