account_move.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import UserError, ValidationError
  5. from stdnum import luhn
  6. class AccountMove(models.Model):
  7. _inherit = 'account.move'
  8. def _get_invoice_reference_se_ocr2(self, reference):
  9. self.ensure_one()
  10. return reference + luhn.calc_check_digit(reference)
  11. def _get_invoice_reference_se_ocr3(self, reference):
  12. self.ensure_one()
  13. reference = reference + str(len(reference) + 2)[:1]
  14. return reference + luhn.calc_check_digit(reference)
  15. def _get_invoice_reference_se_ocr4(self, reference):
  16. self.ensure_one()
  17. ocr_length = self.journal_id.l10n_se_invoice_ocr_length
  18. if len(reference) + 1 > ocr_length:
  19. raise UserError(_("OCR Reference Number length is greater than allowed. Allowed length in invoice journal setting is %s.") % str(ocr_length))
  20. reference = reference.rjust(ocr_length - 1, '0')
  21. return reference + luhn.calc_check_digit(reference)
  22. def _get_invoice_reference_se_ocr2_invoice(self):
  23. self.ensure_one()
  24. return self._get_invoice_reference_se_ocr2(str(self.id))
  25. def _get_invoice_reference_se_ocr3_invoice(self):
  26. self.ensure_one()
  27. return self._get_invoice_reference_se_ocr3(str(self.id))
  28. def _get_invoice_reference_se_ocr4_invoice(self):
  29. self.ensure_one()
  30. return self._get_invoice_reference_se_ocr4(str(self.id))
  31. def _get_invoice_reference_se_ocr2_partner(self):
  32. self.ensure_one()
  33. return self._get_invoice_reference_se_ocr2(self.partner_id.ref if str(self.partner_id.ref).isdecimal() else str(self.partner_id.id))
  34. def _get_invoice_reference_se_ocr3_partner(self):
  35. self.ensure_one()
  36. return self._get_invoice_reference_se_ocr3(self.partner_id.ref if str(self.partner_id.ref).isdecimal() else str(self.partner_id.id))
  37. def _get_invoice_reference_se_ocr4_partner(self):
  38. self.ensure_one()
  39. return self._get_invoice_reference_se_ocr4(self.partner_id.ref if str(self.partner_id.ref).isdecimal() else str(self.partner_id.id))
  40. @api.onchange('partner_id')
  41. def _onchange_partner_id(self):
  42. """ If Vendor Bill and Vendor OCR is set, add it. """
  43. if self.partner_id and self.move_type == 'in_invoice' and self.partner_id.l10n_se_default_vendor_payment_ref:
  44. self.payment_reference = self.partner_id.l10n_se_default_vendor_payment_ref
  45. return super(AccountMove, self)._onchange_partner_id()
  46. @api.constrains('payment_reference', 'state')
  47. def _l10n_se_check_payment_reference(self):
  48. for invoice in self:
  49. if (
  50. (invoice.payment_reference or invoice.state == 'posted')
  51. and invoice.partner_id
  52. and invoice.move_type == 'in_invoice'
  53. and invoice.partner_id.l10n_se_check_vendor_ocr
  54. and invoice.country_code == 'SE'
  55. ):
  56. try:
  57. luhn.validate(invoice.payment_reference)
  58. except Exception:
  59. raise ValidationError(_("Vendor require OCR Number as payment reference. Payment reference isn't a valid OCR Number."))