Debian Codegraph @

addons/point_of_sale/static/src/app/models/pos_preset.js

raw
1 import { registry } from "@web/core/registry";
2 import { Base } from "./related_models";
3
4 const { DateTime } = luxon;
5
6 export class PosPreset extends Base {
7 static pythonModel = "pos.preset";
8
9 initState() {
10 super.initState();
11 this.uiState = {
12 availabilities: {},
13 };
14
15 // This will compute availabilities locally
16 // when selecting a preset it will be updated with the server data
17 this.computeAvailabilities();
18 }
19
20 get needsSlot() {
21 return this.use_timing;
22 }
23
24 get needsName() {
25 return this.identification === "name";
26 }
27
28 get needsPartner() {
29 return this.identification === "address";
30 }
31
32 get orders() {
33 return this.models["pos.order"].filter((o) => o.preset_id?.id === this.id);
34 }
35
36 get nextSlot() {
37 const dateNow = DateTime.now();
38 const sqlDate = dateNow.toFormat("yyyy-MM-dd");
39 return Object.values(this.uiState.availabilities[sqlDate]).find(
40 (s) => !s.isFull && s.datetime > dateNow
41 );
42 }
43
44 get availabilities() {
45 return this.uiState.availabilities;
46 }
47
48 get slotsUsage() {
49 return (
50 this.orders.reduce((acc, order) => {
51 if (!acc[order.preset_time]) {
52 acc[order.preset_time] = [];
53 }
54
55 acc[order.preset_time].push(order.id);
56 return acc;
57 }, {}) || {}
58 );
59 }
60
61 computeAvailabilities(usages = {}) {
62 this.generateSlots();
63
64 const allSlots = Object.values(this.uiState.availabilities).reduce(
65 (acc, curr) => Object.assign(acc, curr),
66 {}
67 );
68
69 for (const [datetime, slot] of Object.entries(allSlots)) {
70 const usage = usages[datetime];
71 slot.order_ids = new Set([...slot.order_ids, ...(usage || [])]);
72 slot.isFull = slot.order_ids.size >= this.slots_per_interval;
73 }
74
75 return this.uiState.availabilities;
76 }
77
78 get currentSlot() {
79 const now = DateTime.now();
80 const interval = this.interval_time;
81 const todayAvailabilities = this.availabilities[now.toFormat("yyyy-MM-dd")];
82 for (const slot of Object.values(todayAvailabilities)) {
83 if (slot.datetime < now && slot.datetime.plus({ minutes: interval }) > now) {
84 return slot;
85 }
86 }
87 return false;
88 }
89
90 generateSlots() {
91 const now = DateTime.now();
92 const usage = this.slotsUsage;
93 const interval = this.interval_time;
94 const slots = {};
95
96 // Compute slots for next 7 days
97 for (const i of [...Array(7).keys()]) {
98 const dateNow = now.plus({ days: i });
99 const getDateTime = (hour) =>
100 DateTime.fromObject({
101 year: dateNow.year,
102 month: dateNow.month,
103 day: dateNow.day,
104 hour: Math.floor(hour),
105 minute: Math.round((hour % 1) * 60),
106 });
107 const dayOfWeek = (dateNow.weekday - 1).toString();
108 const date = dateNow.toFormat("yyyy-MM-dd");
109 const attToday = this.attendance_ids.filter((a) => a.dayofweek === dayOfWeek);
110 slots[date] = [];
111
112 for (const attendance of attToday) {
113 const dateOpening = getDateTime(attendance.hour_from);
114 const dateClosing = getDateTime(attendance.hour_to);
115
116 let start = dateOpening;
117 while (start >= dateOpening && start <= dateClosing && interval > 0) {
118 if (start >= now) {
119 const sqlDatetime = start.toFormat("yyyy-MM-dd HH:mm:ss");
120
121 if (slots[date][sqlDatetime]) {
122 slots[date][sqlDatetime].order_ids.add(...(usage[sqlDatetime] || []));
123 } else {
124 slots[date][sqlDatetime] = {
125 periode: attendance.day_period,
126 datetime: start,
127 order_ids: new Set(usage[sqlDatetime] || []),
128 };
129 }
130 }
131 start = start.plus({ minutes: interval });
132 }
133 }
134 }
135
136 this.uiState.availabilities = slots;
137 }
138 }
139
140 registry.category("pos_available_models").add(PosPreset.pythonModel, PosPreset);