You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.9 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Tecnativa - Jairo Llopis
  3. # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl).
  4. from odoo import _, api, fields, models
  5. from odoo.exceptions import ValidationError
  6. from oca.decorators import foreach # pylint: disable=W7935
  7. import logging
  8. _logger = logging.getLogger(__name__)
  9. class PosConfig(models.Model):
  10. _inherit = 'pos.config'
  11. # Redefine preexisting field
  12. pricelist_id = fields.Many2one(
  13. string="Default Pricelist",
  14. help="The pricelist used if no customer is selected or if the "
  15. "customer has no Sale Pricelist configured.",
  16. )
  17. # New fields
  18. available_pricelist_ids = fields.Many2many(
  19. 'product.pricelist',
  20. string='Available Pricelists',
  21. default=lambda self: self._default_pricelist(),
  22. help="Make several pricelists available in the Point of Sale. "
  23. "You can also apply a pricelist to specific customers from "
  24. "their contact form (in Sales tab). To be valid, this pricelist "
  25. "must be listed here as an available pricelist. Otherwise the "
  26. "default pricelist will apply.",
  27. )
  28. use_pricelist = fields.Boolean(
  29. "Use pricelists",
  30. help="Set shop-specific prices, seasonal discounts, etc.",
  31. )
  32. group_sale_pricelist = fields.Boolean(
  33. "Use pricelists to adapt your price per customers",
  34. implied_group='product.group_sale_pricelist',
  35. help="Allows to manage different prices based on rules per "
  36. "category of customers. Example: 10% for retailers, promotion "
  37. "of 5 EUR on this product, etc.",
  38. )
  39. group_pricelist_item = fields.Boolean(
  40. "Show pricelists to customers",
  41. implied_group='product.group_pricelist_item',
  42. )
  43. @api.constrains('pricelist_id', 'available_pricelist_ids', 'journal_id',
  44. 'invoice_journal_id', 'journal_ids')
  45. @foreach()
  46. def _check_currencies(self):
  47. if self.pricelist_id not in self.available_pricelist_ids:
  48. raise ValidationError(_(
  49. "The default pricelist must be included in "
  50. "the available pricelists."))
  51. if self.available_pricelist_ids.filtered(
  52. lambda pricelist: pricelist.currency_id != self.currency_id):
  53. raise ValidationError(_(
  54. "All available pricelists must be in the same currency "
  55. "as the company or as the Sales Journal set on this "
  56. "point of sale if you use the Accounting application."))
  57. if (self.invoice_journal_id.currency_id and
  58. self.invoice_journal_id.currency_id != self.currency_id):
  59. raise ValidationError(_(
  60. "The invoice journal must be in the same currency as the "
  61. "Sales Journal or the company currency if that is not set."))
  62. if self.journal_ids.filtered(
  63. lambda journal: (journal.currency_id and
  64. journal.currency_id != self.currency_id)):
  65. raise ValidationError(_(
  66. "All payment methods must be in the same currency as the "
  67. "Sales Journal or the company currency if that is not set."))
  68. @api.onchange('use_pricelist')
  69. def _onchange_use_pricelist(self):
  70. """If the 'pricelist' box is unchecked, reset the pricelist_id
  71. This makes the posbox to stop using a pricelist.
  72. """
  73. if not self.use_pricelist:
  74. self.pricelist_id = self.available_pricelist_ids = \
  75. self._default_pricelist()
  76. else:
  77. self.update({
  78. 'group_sale_pricelist': True,
  79. 'group_pricelist_item': True,
  80. })
  81. @api.onchange('available_pricelist_ids')
  82. def _onchange_available_pricelist_ids(self):
  83. if self.pricelist_id not in self.available_pricelist_ids:
  84. self.pricelist_id = False
  85. @foreach()
  86. def _check_groups_implied(self):
  87. for field_name in (f for f in self.fields_get_keys()
  88. if f.startswith('group_')):
  89. field = self._fields[field_name]
  90. if (field.type in {'boolean', 'selection'} and
  91. hasattr(field, 'implied_group')):
  92. field_group_xmlids = getattr(
  93. field, 'group', 'base.group_user').split(',')
  94. field_groups = self.env['res.groups'].concat(
  95. *(self.env.ref(it) for it in field_group_xmlids))
  96. field_groups.write({
  97. 'implied_ids': [(4, self.env.ref(field.implied_group).id)],
  98. })
  99. @api.model
  100. def create(self, vals):
  101. result = super(PosConfig, self).create(vals)
  102. result.sudo()._check_groups_implied()
  103. return result
  104. def write(self, vals):
  105. result = super(PosConfig, self).write(vals)
  106. self.sudo()._check_groups_implied()
  107. return result