| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from odoo import api, models |
| 3 |
from odoo.fields import Domain |
| 4 |
from odoo.exceptions import AccessError |
| 5 |
|
| 6 |
|
| 7 |
class PosLoadMixin(models.AbstractModel): |
| 8 |
_name = 'pos.load.mixin' |
| 9 |
_description = "PoS data loading mixin" |
| 10 |
|
| 11 |
@api.model |
| 12 |
def _load_pos_data_search_read(self, data, config): |
| 13 |
""" Search and return records to be loaded in the pos """ |
| 14 |
if not config: |
| 15 |
raise ValueError("config must be provided to search for PoS data.") |
| 16 |
|
| 17 |
domain = self._server_date_to_domain(self._load_pos_data_domain(data, config)) |
| 18 |
if domain is False: |
| 19 |
return [] |
| 20 |
|
| 21 |
records = self.search(domain) |
| 22 |
return self._load_pos_data_read(records, config) |
| 23 |
|
| 24 |
@api.model |
| 25 |
def _load_pos_data_domain(self, data, config): |
| 26 |
""" Return the domain used to filter records """ |
| 27 |
return [] |
| 28 |
|
| 29 |
@api.model |
| 30 |
def _server_date_to_domain(self, domain): |
| 31 |
""" Optionally restrict the domain to records modified after the last server sync """ |
| 32 |
if domain is False: |
| 33 |
return domain |
| 34 |
|
| 35 |
last_server_date = self.env.context.get('pos_last_server_date', False) |
| 36 |
limited_loading = self.env.context.get('pos_limited_loading', True) |
| 37 |
model_included = self._name not in ['pos.session', 'pos.config'] |
| 38 |
|
| 39 |
if limited_loading and last_server_date and model_included: |
| 40 |
domain = Domain.AND([domain, [('write_date', '>', last_server_date)]]) |
| 41 |
|
| 42 |
return domain |
| 43 |
|
| 44 |
@api.model |
| 45 |
def _load_pos_data_read(self, records, config): |
| 46 |
""" Read specific fields from the given records """ |
| 47 |
if not config: |
| 48 |
raise ValueError("config must be provided to read PoS data.") |
| 49 |
|
| 50 |
fields = self._load_pos_data_fields(config) |
| 51 |
records = records._filtered_access("read").read(fields, load=False) |
| 52 |
return records or [] |
| 53 |
|
| 54 |
def _unrelevant_records(self, config): |
| 55 |
unrelevant_record_ids = [] |
| 56 |
for record in self: |
| 57 |
try: |
| 58 |
if not record.active: |
| 59 |
unrelevant_record_ids.append(record.id) |
| 60 |
except AccessError: |
| 61 |
# If the user has no read access, consider the record as unrelevant |
| 62 |
unrelevant_record_ids.append(record.id) |
| 63 |
return unrelevant_record_ids |
| 64 |
|
| 65 |
@api.model |
| 66 |
def _load_pos_data_fields(self, config): |
| 67 |
""" Return the list of fields to be loaded """ |
| 68 |
return [] |