| 1 |
import { registry } from "@web/core/registry"; |
| 2 |
import { imageUrl } from "@web/core/utils/urls"; |
| 3 |
import { Base } from "./related_models"; |
| 4 |
import { getImageDataUrl } from "@point_of_sale/utils"; |
| 5 |
import { logPosMessage } from "../utils/pretty_console_log"; |
| 6 |
import { PosOrderlineAccounting } from "./accounting/pos_order_line_accounting"; |
| 7 |
import { PosOrderAccounting } from "./accounting/pos_order_accounting"; |
| 8 |
|
| 9 |
const CONSOLE_COLOR = "#F5B427"; |
| 10 |
|
| 11 |
export class PosConfig extends Base { |
| 12 |
static pythonModel = "pos.config"; |
| 13 |
|
| 14 |
initState() { |
| 15 |
super.initState(); |
| 16 |
this.uiState = {}; |
| 17 |
this.handlePricesComputation(); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Since order lines prices needs to be computed globally we need to recompute the whole |
| 22 |
* order prices each time an order line is created or updated. |
| 23 |
*/ |
| 24 |
handlePricesComputation() { |
| 25 |
const lineModel = this.models["pos.order.line"]; |
| 26 |
const orderModel = this.models["pos.order"]; |
| 27 |
|
| 28 |
const updateLinePrices = (ids, fields) => { |
| 29 |
const fieldTargetted = fields?.some((field) => |
| 30 |
PosOrderlineAccounting.accountingFields.has(field) |
| 31 |
); |
| 32 |
|
| 33 |
if (fieldTargetted || !fields) { |
| 34 |
// Orders needs to be read from raw in case of not fully setuped records |
| 35 |
const lines = lineModel.readMany(ids); |
| 36 |
const orderIds = new Set(lines.map((l) => l.raw.order_id)); |
| 37 |
const orders = orderModel.readMany([...orderIds]); |
| 38 |
orders.forEach((order) => order.triggerRecomputeAllPrices()); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
const updateOrderPrices = (id, fields) => { |
| 43 |
const fieldTargetted = fields?.some((field) => |
| 44 |
PosOrderAccounting.accountingFields.has(field) |
| 45 |
); |
| 46 |
|
| 47 |
if (fieldTargetted) { |
| 48 |
const order = orderModel.get(id); |
| 49 |
order?.triggerRecomputeAllPrices(); |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
lineModel.addEventListener("create", (data) => updateLinePrices(data.ids)); |
| 54 |
lineModel.addEventListener("update", (data) => updateLinePrices([data.id], data.fields)); |
| 55 |
orderModel.addEventListener("update", (data) => updateOrderPrices(data.id, data.fields)); |
| 56 |
} |
| 57 |
|
| 58 |
get hasCashRounding() { |
| 59 |
return this.cash_rounding && this.only_round_cash_method; |
| 60 |
} |
| 61 |
get hasGlobalRounding() { |
| 62 |
return this.cash_rounding && !this.only_round_cash_method; |
| 63 |
} |
| 64 |
get canInvoice() { |
| 65 |
return Boolean(this.raw.invoice_journal_id); |
| 66 |
} |
| 67 |
|
| 68 |
get useProxy() { |
| 69 |
return ( |
| 70 |
this.is_posbox && |
| 71 |
(this.iface_electronic_scale || |
| 72 |
this.iface_print_via_proxy || |
| 73 |
this.iface_scan_via_proxy || |
| 74 |
this.iface_customer_facing_display_via_proxy) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
get isShareable() { |
| 79 |
return this.raw.trusted_config_ids.length > 0; |
| 80 |
} |
| 81 |
|
| 82 |
get printerCategories() { |
| 83 |
const set = new Set(); |
| 84 |
for (const relPrinter of this.models["pos.printer"].getAll()) { |
| 85 |
const printer = relPrinter.raw; |
| 86 |
for (const id of printer.product_categories_ids) { |
| 87 |
set.add(id); |
| 88 |
} |
| 89 |
} |
| 90 |
return set; |
| 91 |
} |
| 92 |
|
| 93 |
get preparationCategories() { |
| 94 |
if (this.printerCategories) { |
| 95 |
return new Set([...this.printerCategories]); |
| 96 |
} |
| 97 |
return new Set(); |
| 98 |
} |
| 99 |
|
| 100 |
get displayBigTrackingNumber() { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
get displayTrackingNumber() { |
| 105 |
return this.module_pos_restaurant; |
| 106 |
} |
| 107 |
|
| 108 |
async cacheReceiptLogo() { |
| 109 |
try { |
| 110 |
this.uiState.receiptLogoDataUrl = await getImageDataUrl(this.receiptCompanyLogoUrl); |
| 111 |
} catch (error) { |
| 112 |
logPosMessage( |
| 113 |
"PosConfig", |
| 114 |
"cacheReceiptLogo", |
| 115 |
"Error while caching receipt logo", |
| 116 |
CONSOLE_COLOR, |
| 117 |
[error] |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
get receiptLogoUrl() { |
| 123 |
return this.uiState.receiptLogoDataUrl || this.receiptCompanyLogoUrl; |
| 124 |
} |
| 125 |
|
| 126 |
get receiptCompanyLogoUrl() { |
| 127 |
return imageUrl("res.company", this.company_id.id, "logo", { |
| 128 |
width: 256, |
| 129 |
height: 256, |
| 130 |
}); |
| 131 |
} |
| 132 |
|
| 133 |
get availablePricelists() { |
| 134 |
if (!this.use_pricelist) { |
| 135 |
return []; |
| 136 |
} |
| 137 |
const available_pricelists = new Set(this.available_pricelist_ids); |
| 138 |
if (this.pricelist_id) { |
| 139 |
available_pricelists.add(this.pricelist_id); |
| 140 |
} |
| 141 |
return Array.from(available_pricelists); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
registry.category("pos_available_models").add(PosConfig.pythonModel, PosConfig); |