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.

81 lines
2.3 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Robin Keunen <robin@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. from odoo import api, fields, models
  5. class ExternalIdMixin(models.AbstractModel):
  6. _name = "external.id.mixin"
  7. _description = "External ID Mixin"
  8. # do not access directly, always use get_api_external_id method
  9. _api_external_id = fields.Integer(
  10. string="External ID", index=True, required=False
  11. )
  12. external_id_sequence_id = fields.Many2one(
  13. comodel_name="ir.sequence",
  14. string="External ID Sequence",
  15. required=False,
  16. )
  17. @api.multi
  18. def set_external_sequence(self):
  19. self.ensure_one()
  20. code = "%s.external.id" % self._name
  21. Sequence = self.env["ir.sequence"]
  22. # check if code was created for that model
  23. sequence = Sequence.search([("code", "=", code)])
  24. if not sequence:
  25. sequence = Sequence.sudo().create(
  26. {"name": code, "code": code, "number_next": 1}
  27. )
  28. self.sudo().write({"external_id_sequence_id": sequence.id})
  29. return True
  30. @api.multi
  31. def get_api_external_id(self):
  32. self.ensure_one()
  33. if not self.external_id_sequence_id:
  34. self.set_external_sequence()
  35. if not self._api_external_id:
  36. self.sudo().write(
  37. {"_api_external_id": self.external_id_sequence_id._next()}
  38. )
  39. return self._api_external_id
  40. class ResPartner(models.Model):
  41. _name = "res.partner"
  42. _inherit = ["res.partner", "external.id.mixin"]
  43. class SubscriptionRequest(models.Model):
  44. _name = "subscription.request"
  45. _inherit = ["subscription.request", "external.id.mixin"]
  46. class AccountAccount(models.Model):
  47. _name = "account.account"
  48. _inherit = ["account.account", "external.id.mixin"]
  49. class AccountJournal(models.Model):
  50. _name = "account.journal"
  51. _inherit = ["account.journal", "external.id.mixin"]
  52. class AccountInvoice(models.Model):
  53. _name = "account.invoice"
  54. _inherit = ["account.invoice", "external.id.mixin"]
  55. class AccountPayment(models.Model):
  56. _name = "account.payment"
  57. _inherit = ["account.payment", "external.id.mixin"]
  58. class ProductTemplate(models.Model):
  59. _name = "product.template"
  60. _inherit = ["product.template", "external.id.mixin"]