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.

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