summaryrefslogtreecommitdiff
path: root/addons/point_of_sale/static/src/js/Screens/OrderManagementScreen/OrderFetcher.js
blob: 57a0263567b2cbd178227ecafb0145cf8c0b4bab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
odoo.define('point_of_sale.OrderFetcher', function (require) {
    'use strict';

    const { EventBus } = owl.core;
    const { Gui } = require('point_of_sale.Gui');
    const { isRpcError } = require('point_of_sale.utils');
    const models = require('point_of_sale.models');

    class OrderFetcher extends EventBus {
        constructor() {
            super();
            this.currentPage = 1;
            this.ordersToShow = [];
            this.cache = {};
            this.totalCount = 0;
        }
        get activeOrders() {
            const allActiveOrders = this.comp.env.pos.get('orders').models;
            return this.searchDomain
                ? allActiveOrders.filter(this._predicateBasedOnSearchDomain.bind(this))
                : allActiveOrders;
        }
        _predicateBasedOnSearchDomain(order) {
            function check(order, field, searchWord) {
                searchWord = searchWord.toLowerCase();
                switch (field) {
                    case 'pos_reference':
                        return order.name.toLowerCase().includes(searchWord);
                    case 'partner_id.display_name':
                        const client = order.get_client();
                        return client ? client.name.toLowerCase().includes(searchWord) : false;
                    case 'date_order':
                        return moment(order.creation_date).format('YYYY-MM-DD hh:mm A').includes(searchWord);
                    default:
                        return false;
                }
            }
            for (let [field, _, searchWord] of (this.searchDomain || []).filter((item) => item !== '|')) {
                // remove surrounding "%" from `searchWord`
                searchWord = searchWord.substring(1, searchWord.length - 1);
                if (check(order, field, searchWord)) {
                    return true;
                }
            }
            return false;
        }
        get nActiveOrders() {
            return this.activeOrders.length;
        }
        get lastPageFullOfActiveOrders() {
            return Math.trunc(this.nActiveOrders / this.nPerPage);
        }
        get remainingActiveOrders() {
            return this.nActiveOrders % this.nPerPage;
        }
        /**
         * for nPerPage = 10
         * +--------+----------+
         * | nItems | lastPage |
         * +--------+----------+
         * |     2  |       1  |
         * |    10  |       1  |
         * |    11  |       2  |
         * |    30  |       3  |
         * |    35  |       4  |
         * +--------+----------+
         */
        get lastPage() {
            const nItems = this.nActiveOrders + this.totalCount;
            return Math.trunc(nItems / (this.nPerPage + 1)) + 1;
        }
        /**
         * Calling this methods populates the `ordersToShow` then trigger `update` event.
         * @related get
         *
         * NOTE: This is tightly-coupled with pagination. So if the current page contains all
         * active orders, it will not fetch anything from the server but only sets `ordersToShow`
         * to the active orders that fits the current page.
         */
        async fetch() {
            try {
                let limit, offset;
                let start, end;
                if (this.currentPage <= this.lastPageFullOfActiveOrders) {
                    // Show only active orders.
                    start = (this.currentPage - 1) * this.nPerPage;
                    end = this.currentPage * this.nPerPage;
                    this.ordersToShow = this.activeOrders.slice(start, end);
                } else if (this.currentPage === this.lastPageFullOfActiveOrders + 1) {
                    // Show partially the remaining active orders and
                    // some orders from the backend.
                    offset = 0;
                    limit = this.nPerPage - this.remainingActiveOrders;
                    start = (this.currentPage - 1) * this.nPerPage;
                    end = this.nActiveOrders;
                    this.ordersToShow = [
                        ...this.activeOrders.slice(start, end),
                        ...(await this._fetch(limit, offset)),
                    ];
                } else {
                    // Show orders from the backend.
                    offset =
                        this.nPerPage -
                        this.remainingActiveOrders +
                        (this.currentPage - (this.lastPageFullOfActiveOrders + 1) - 1) *
                            this.nPerPage;
                    limit = this.nPerPage;
                    this.ordersToShow = await this._fetch(limit, offset);
                }
                this.trigger('update');
            } catch (error) {
                if (isRpcError(error) && error.message.code < 0) {
                    Gui.showPopup('ErrorPopup', {
                        title: this.comp.env._t('Network Error'),
                        body: this.comp.env._t('Unable to fetch orders if offline.'),
                    });
                    Gui.setSyncStatus('error');
                } else {
                    throw error;
                }
            }
        }
        /**
         * This returns the orders from the backend that needs to be shown.
         * If the order is already in cache, the full information about that
         * order is not fetched anymore, instead, we use info from cache.
         *
         * @param {number} limit
         * @param {number} offset
         */
        async _fetch(limit, offset) {
            const { ids, totalCount } = await this._getOrderIdsForCurrentPage(limit, offset);
            const idsNotInCache = ids.filter((id) => !(id in this.cache));
            if (idsNotInCache.length > 0) {
                const fetchedOrders = await this._fetchOrders(idsNotInCache);
                // Cache these fetched orders so that next time, no need to fetch
                // them again, unless invalidated. See `invalidateCache`.
                fetchedOrders.forEach((order) => {
                    this.cache[order.id] = new models.Order(
                        {},
                        { pos: this.comp.env.pos, json: order }
                    );
                });
            }
            this.totalCount = totalCount;
            return ids.map((id) => this.cache[id]);
        }
        async _getOrderIdsForCurrentPage(limit, offset) {
            return await this.rpc({
                model: 'pos.order',
                method: 'search_paid_order_ids',
                kwargs: { config_id: this.configId, domain: this.searchDomain ? this.searchDomain : [], limit, offset },
                context: this.comp.env.session.user_context,
            });
        }
        async _fetchOrders(ids) {
            return await this.rpc({
                model: 'pos.order',
                method: 'export_for_ui',
                args: [ids],
                context: this.comp.env.session.user_context,
            });
        }
        nextPage() {
            if (this.currentPage < this.lastPage) {
                this.currentPage += 1;
                this.fetch();
            }
        }
        prevPage() {
            if (this.currentPage > 1) {
                this.currentPage -= 1;
                this.fetch();
            }
        }
        /**
         * @param {integer|undefined} id id of the cached order
         * @returns {Array<models.Order>}
         */
        get(id) {
            if (id) return this.cache[id];
            return this.ordersToShow;
        }
        setSearchDomain(searchDomain) {
            this.searchDomain = searchDomain;
        }
        setComponent(comp) {
            this.comp = comp;
            return this;
        }
        setConfigId(configId) {
            this.configId = configId;
        }
        setNPerPage(val) {
            this.nPerPage = val;
        }
        setPage(page) {
            this.currentPage = page;
        }
        invalidateCache(ids) {
            for (let id of ids) {
                delete this.cache[id];
            }
        }
        async rpc() {
            Gui.setSyncStatus('connecting');
            const result = await this.comp.rpc(...arguments);
            Gui.setSyncStatus('connected');
            return result;
        }
    }

    return new OrderFetcher();
});