Debian Codegraph @

addons/point_of_sale/static/src/app/screens/receipt_screen/receipt_screen.js

raw
1 import { _t } from "@web/core/l10n/translation";
2 import { useErrorHandlers, useTrackedAsync } from "@point_of_sale/app/hooks/hooks";
3 import { registry } from "@web/core/registry";
4 import { OrderReceipt } from "@point_of_sale/app/screens/receipt_screen/receipt/order_receipt";
5 import { useState, Component } from "@odoo/owl";
6 import { usePos } from "@point_of_sale/app/hooks/pos_hook";
7 import { useService } from "@web/core/utils/hooks";
8 import { ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog";
9 import { isValidEmail } from "@point_of_sale/utils";
10 import { useRouterParamsChecker } from "@point_of_sale/app/hooks/pos_router_hook";
11
12 export class ReceiptScreen extends Component {
13 static template = "point_of_sale.ReceiptScreen";
14 static components = { OrderReceipt };
15 static props = {
16 orderUuid: { type: String },
17 };
18
19 setup() {
20 super.setup();
21 this.pos = usePos();
22 useRouterParamsChecker();
23 useErrorHandlers();
24 this.ui = useService("ui");
25 this.renderer = useService("renderer");
26 this.notification = useService("notification");
27 this.dialog = useService("dialog");
28 const partner = this.currentOrder.getPartner();
29 const email = partner?.invoice_emails || partner?.email || "";
30 this.state = useState({
31 email: email,
32 phone: partner?.phone || "",
33 });
34 this.sendReceipt = useTrackedAsync(this._sendReceiptToCustomer.bind(this));
35 this.doFullPrint = useTrackedAsync(() =>
36 this.pos.printReceipt({ order: this.currentOrder })
37 );
38 this.doBasicPrint = useTrackedAsync(() =>
39 this.pos.printReceipt({ order: this.currentOrder, basic: true })
40 );
41 }
42 actionSendReceiptOnEmail() {
43 this.sendReceipt.call({
44 action: "action_send_receipt",
45 destination: this.state.email,
46 name: "Email",
47 });
48 }
49 get currentOrder() {
50 return this.pos.models["pos.order"].getBy("uuid", this.props.orderUuid);
51 }
52 get orderAmountPlusTip() {
53 const order = this.currentOrder;
54 const orderTotalAmount = order.priceIncl;
55 const tip_product_id = this.pos.config.tip_product_id?.id;
56 const tipLine = order
57 .getOrderlines()
58 .find((line) => tip_product_id && line.product_id.id === tip_product_id);
59 const tipAmount = tipLine ? tipLine.prices.total_included : 0;
60 const orderAmountStr = this.env.utils.formatCurrency(orderTotalAmount - tipAmount);
61 if (!tipAmount) {
62 return orderAmountStr;
63 }
64 const tipAmountStr = this.env.utils.formatCurrency(tipAmount);
65 return `${orderAmountStr} + ${tipAmountStr} tip`;
66 }
67 get ticketScreen() {
68 return { name: "TicketScreen" };
69 }
70 get isValidEmail() {
71 return isValidEmail(this.state.email);
72 }
73 get isValidPhone() {
74 return this.state.phone && /^\+?[()\d\s-.]{8,18}$/.test(this.state.phone);
75 }
76 showPhoneInput() {
77 return false;
78 }
79
80 generateTicketImage = async (basicReceipt = false) =>
81 await this.renderer.toJpeg(
82 OrderReceipt,
83 {
84 order: this.currentOrder,
85 basic_receipt: basicReceipt,
86 },
87 { addClass: "pos-receipt-print p-3" }
88 );
89 async _sendReceiptToCustomer({ action, destination }) {
90 const order = this.currentOrder;
91 if (!order.isSynced) {
92 this.dialog.add(ConfirmationDialog, {
93 title: _t("Unsynced order"),
94 body: _t(
95 "This order is not yet synced to server. Make sure it is synced then try again."
96 ),
97 showReloadButton: true,
98 });
99 return Promise.reject();
100 }
101 const fullTicketImage = await this.generateTicketImage();
102 const basicTicketImage = this.pos.config.basic_receipt
103 ? await this.generateTicketImage(true)
104 : null;
105 await this.pos.data.call("pos.order", action, [
106 [order.id],
107 destination,
108 fullTicketImage,
109 basicTicketImage,
110 ]);
111 }
112 }
113
114 registry.category("pos_pages").add("ReceiptScreen", {
115 name: "ReceiptScreen",
116 component: ReceiptScreen,
117 route: `/pos/ui/${odoo.pos_config_id}/receipt/{string:orderUuid}`,
118 params: {
119 orderUuid: true,
120 orderFinalized: true,
121 },
122 });