Debian Codegraph @

addons/point_of_sale/static/tests/unit/data/pos_order.data.js

raw
1 import { models, Command } from "@web/../tests/web_test_helpers";
2
3 export class PosOrder extends models.ServerModel {
4 _name = "pos.order";
5
6 get_preparation_change(id) {
7 const read = this.read([id]);
8 const changes = read[0]?.last_order_preparation_change || "{}";
9 return {
10 last_order_preparation_change: changes,
11 };
12 }
13
14 read_pos_orders(domain) {
15 const results = this.search(domain, this._load_pos_data_fields(), false);
16 return this.read_pos_data(results);
17 }
18
19 _load_pos_data_fields() {
20 return [];
21 }
22
23 action_pos_order_cancel(self) {
24 const records = this.browse(self);
25 const orderIds = [];
26
27 for (const record of records) {
28 this.write([record.id], { state: "cancel" });
29 orderIds.push(record.id);
30 }
31
32 return {
33 "pos.order": this.read(orderIds, this._load_pos_data_fields(), false),
34 };
35 }
36
37 create() {
38 const orderId = super.create(...arguments);
39 this.write([orderId], { pos_reference: "000-0-000000" });
40 return orderId;
41 }
42
43 sync_from_ui(data) {
44 const orderIds = [];
45 for (const record of data) {
46 const record_uuid_mapping = record.relations_uuid_mapping || {};
47 delete record.relations_uuid_mapping;
48 if (record.id) {
49 this.write([record.id], record);
50 orderIds.push(record.id);
51 } else {
52 const id = this.create(record);
53 orderIds.push(id);
54 }
55 for (const [modelName, mapping] of Object.entries(record_uuid_mapping)) {
56 // Search for owner records by UUID
57 const ownerRecords = this.env[modelName].search_read(
58 [["uuid", "in", Object.keys(mapping)]],
59 ["id", "uuid"]
60 );
61 for (const [uuid, fields] of Object.entries(mapping)) {
62 for (const [name, uuids] of Object.entries(fields)) {
63 const field = this.env[modelName]._fields[name];
64 if (["one2many", "many2many"].includes(field.type)) {
65 // Get all related records by uuids
66 const relatedRecords = this.env[field.relation].search_read(
67 [["uuid", "in", uuids]],
68 ["id", "uuid"]
69 );
70 const ownerRecord = ownerRecords.find((r) => r.uuid === uuid);
71 if (ownerRecord) {
72 this.env[modelName].write([ownerRecord.id], {
73 [name]: relatedRecords.map((r) => Command.link(r.id)),
74 });
75 }
76 } else {
77 // single record relation (many2one)
78 const record = this.env[field.relation].search([["uuid", "=", uuids]]);
79 const ownerRecord = ownerRecords.find((r) => r.uuid === uuid);
80 if (ownerRecord && record) {
81 this.env[modelName].write([ownerRecord.id], { [name]: record[0] });
82 }
83 }
84 }
85 }
86 }
87 }
88
89 return this.read_pos_data(orderIds, data, this.config_id);
90 }
91
92 read_pos_data(orderIds, data, config_id) {
93 const posOrder = [];
94 const posSession = [];
95 const posPayment = [];
96 const posOrderLine = [];
97 const posPackOperationLot = [];
98 const posCustomAttributeValue = [];
99 const readOrder = this.read(orderIds, this._load_pos_data_fields(config_id), false);
100
101 for (const order of readOrder) {
102 posOrder.push(order);
103
104 const lines = this.env["pos.order.line"].read(
105 order.lines,
106 this.env["pos.order.line"]._load_pos_data_fields(config_id),
107 false
108 );
109 const payments = this.env["pos.payment"].read(
110 order.payment_ids,
111 this.env["pos.payment"]._load_pos_data_fields(config_id),
112 false
113 );
114 const packLotLineIds = lines.flatMap((line) => line.pack_lot_ids);
115 const packLotLines = this.env["pos.pack.operation.lot"].read(
116 packLotLineIds,
117 this.env["pos.pack.operation.lot"]._load_pos_data_fields(config_id),
118 false
119 );
120 const customAttributeValueIds = lines.flatMap(
121 (line) => line.custom_attribute_value_ids
122 );
123 const customAttributeValues = this.env["product.attribute.custom.value"].read(
124 customAttributeValueIds,
125 this.env["product.attribute.custom.value"]._load_pos_data_fields(config_id),
126 false
127 );
128
129 posOrderLine.push(...lines);
130 posPayment.push(...payments);
131 posPackOperationLot.push(...packLotLines);
132 posCustomAttributeValue.push(...customAttributeValues);
133 }
134
135 return {
136 "pos.order": posOrder,
137 "pos.session": posSession,
138 "pos.payment": posPayment,
139 "pos.order.line": posOrderLine,
140 "pos.pack.operation.lot": posPackOperationLot,
141 "product.attribute.custom.value": posCustomAttributeValue,
142 };
143 }
144 }