| 1 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 |
from odoo import api, models, fields, _ |
| 3 |
from odoo.exceptions import UserError |
| 4 |
|
| 5 |
|
| 6 |
class ProductProduct(models.Model): |
| 7 |
_name = 'product.product' |
| 8 |
_inherit = ['product.product', 'pos.load.mixin'] |
| 9 |
|
| 10 |
@api.model |
| 11 |
def _load_pos_data_domain(self, data, config): |
| 12 |
return [('product_tmpl_id', 'in', [p['id'] for p in data['product.template']])] |
| 13 |
|
| 14 |
@api.model |
| 15 |
def _load_pos_data_fields(self, config): |
| 16 |
taxes = self.env['account.tax'].search(self.env['account.tax']._check_company_domain(config.company_id.id)) |
| 17 |
product_fields = taxes._eval_taxes_computation_prepare_product_fields() |
| 18 |
return list(product_fields.union({ |
| 19 |
'id', 'lst_price', 'display_name', 'product_tmpl_id', 'product_template_variant_value_ids', |
| 20 |
'product_template_attribute_value_ids', 'barcode', 'product_tag_ids', 'default_code', 'standard_price' |
| 21 |
})) |
| 22 |
|
| 23 |
@api.ondelete(at_uninstall=False) |
| 24 |
def _unlink_except_active_pos_session(self): |
| 25 |
product_ctx = dict(self.env.context or {}, active_test=False) |
| 26 |
if self.env['pos.session'].sudo().search_count([('state', '!=', 'closed')]): |
| 27 |
if self.with_context(product_ctx).search_count([('id', 'in', self.ids), ('product_tmpl_id.available_in_pos', '=', True)]): |
| 28 |
raise UserError(_( |
| 29 |
"To delete a product, make sure all point of sale sessions are closed.\n\n" |
| 30 |
"Deleting a product available in a session would be like attempting to snatch a hamburger from a customer’s hand mid-bite; chaos will ensue as ketchup and mayo go flying everywhere!", |
| 31 |
)) |
| 32 |
|
| 33 |
@api.model |
| 34 |
def _load_pos_data_read(self, records, config): |
| 35 |
read_records = super()._load_pos_data_read(records, config) |
| 36 |
different_currency = config.currency_id != self.env.company.currency_id |
| 37 |
if different_currency: |
| 38 |
for product in read_records: |
| 39 |
product['lst_price'] = self.env.company.currency_id._convert( |
| 40 |
product['lst_price'], config.currency_id, self.env.company, fields.Date.today() |
| 41 |
) |
| 42 |
product['standard_price'] = self.env.company.currency_id._convert( |
| 43 |
product['standard_price'], config.currency_id, self.env.company, fields.Date.today() |
| 44 |
) |
| 45 |
return read_records |
| 46 |
|
| 47 |
def _can_return_content(self, field_name=None, access_token=None): |
| 48 |
if field_name == "image_128" and self.sudo().available_in_pos: |
| 49 |
return True |
| 50 |
return super()._can_return_content(field_name, access_token) |
| 51 |
|
| 52 |
def action_archive(self): |
| 53 |
self.product_tmpl_id._ensure_unused_in_pos() |
| 54 |
return super().action_archive() |