summaryrefslogtreecommitdiff
path: root/addons/l10n_se_ocr/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/l10n_se_ocr/models
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/l10n_se_ocr/models')
-rw-r--r--addons/l10n_se_ocr/models/__init__.py6
-rw-r--r--addons/l10n_se_ocr/models/account_journal.py17
-rw-r--r--addons/l10n_se_ocr/models/account_move.py73
-rw-r--r--addons/l10n_se_ocr/models/res_partner.py21
4 files changed, 117 insertions, 0 deletions
diff --git a/addons/l10n_se_ocr/models/__init__.py b/addons/l10n_se_ocr/models/__init__.py
new file mode 100644
index 00000000..f7830456
--- /dev/null
+++ b/addons/l10n_se_ocr/models/__init__.py
@@ -0,0 +1,6 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from . import account_move
+from . import account_journal
+from . import res_partner \ No newline at end of file
diff --git a/addons/l10n_se_ocr/models/account_journal.py b/addons/l10n_se_ocr/models/account_journal.py
new file mode 100644
index 00000000..806f07c5
--- /dev/null
+++ b/addons/l10n_se_ocr/models/account_journal.py
@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import fields, models, api, _
+from odoo.exceptions import ValidationError
+
+
+class AccountJournal(models.Model):
+ _inherit = 'account.journal'
+
+ invoice_reference_model = fields.Selection(selection_add=[('se_ocr2', 'Sweden OCR Level 1 & 2'), ('se_ocr3', 'Sweden OCR Level 3'), ('se_ocr4', 'Sweden OCR Level 4')], ondelete={'se_ocr2': 'set default', 'se_ocr3': 'set default', 'se_ocr4': 'set default'})
+ l10n_se_invoice_ocr_length = fields.Integer(string='OCR Number Length', help="Total length of OCR Reference Number including checksum.", default=6)
+
+ @api.constrains('l10n_se_invoice_ocr_length')
+ def _check_l10n_se_invoice_ocr_length(self):
+ if self.l10n_se_invoice_ocr_length < 6:
+ return ValidationError(_('OCR Reference Number length need to be greater than 5. Please correct settings under invoice journal settings.'))
diff --git a/addons/l10n_se_ocr/models/account_move.py b/addons/l10n_se_ocr/models/account_move.py
new file mode 100644
index 00000000..569683f0
--- /dev/null
+++ b/addons/l10n_se_ocr/models/account_move.py
@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, fields, models, _
+from odoo.exceptions import UserError
+from stdnum import luhn
+
+
+class AccountMove(models.Model):
+ _inherit = 'account.move'
+
+ def _get_invoice_reference_se_ocr2(self, reference):
+ self.ensure_one()
+ return reference + luhn.calc_check_digit(reference)
+
+ def _get_invoice_reference_se_ocr3(self, reference):
+ self.ensure_one()
+ reference = reference + str(len(reference) + 2)[:1]
+ return reference + luhn.calc_check_digit(reference)
+
+ def _get_invoice_reference_se_ocr4(self, reference):
+ self.ensure_one()
+
+ ocr_length = self.journal_id.l10n_se_invoice_ocr_length
+
+ if len(reference) + 1 > ocr_length:
+ raise UserError(_("OCR Reference Number length is greater than allowed. Allowed length in invoice journal setting is %s.") % str(ocr_length))
+
+ reference = reference.rjust(ocr_length - 1, '0')
+ return reference + luhn.calc_check_digit(reference)
+
+
+ def _get_invoice_reference_se_ocr2_invoice(self):
+ self.ensure_one()
+ return self._get_invoice_reference_se_ocr2(str(self.id))
+
+ def _get_invoice_reference_se_ocr3_invoice(self):
+ self.ensure_one()
+ return self._get_invoice_reference_se_ocr3(str(self.id))
+
+ def _get_invoice_reference_se_ocr4_invoice(self):
+ self.ensure_one()
+ return self._get_invoice_reference_se_ocr4(str(self.id))
+
+ def _get_invoice_reference_se_ocr2_partner(self):
+ self.ensure_one()
+ return self._get_invoice_reference_se_ocr2(self.partner_id.ref if str(self.partner_id.ref).isdecimal() else str(self.partner_id.id))
+
+ def _get_invoice_reference_se_ocr3_partner(self):
+ self.ensure_one()
+ return self._get_invoice_reference_se_ocr3(self.partner_id.ref if str(self.partner_id.ref).isdecimal() else str(self.partner_id.id))
+
+ def _get_invoice_reference_se_ocr4_partner(self):
+ self.ensure_one()
+ return self._get_invoice_reference_se_ocr4(self.partner_id.ref if str(self.partner_id.ref).isdecimal() else str(self.partner_id.id))
+
+ @api.onchange('partner_id')
+ def _onchange_partner_id(self):
+ """ If Vendor Bill and Vendor OCR is set, add it. """
+ if self.partner_id and self.move_type == 'in_invoice' and self.partner_id.l10n_se_default_vendor_payment_ref:
+ self.payment_reference = self.partner_id.l10n_se_default_vendor_payment_ref
+ return super(AccountMove, self)._onchange_partner_id()
+
+ @api.onchange('payment_reference')
+ def _onchange_payment_reference(self):
+ """ If Vendor Bill and Payment Reference is changed check validation. """
+ if self.partner_id and self.move_type == 'in_invoice' and self.partner_id.l10n_se_check_vendor_ocr:
+ reference = self.payment_reference
+ try:
+ luhn.validate(reference)
+ except:
+ return {'warning': {'title': _('Warning'), 'message': _('Vendor require OCR Number as payment reference. Payment reference isn\'t a valid OCR Number.')}}
+ return super(AccountMove, self)._onchange_payment_reference()
diff --git a/addons/l10n_se_ocr/models/res_partner.py b/addons/l10n_se_ocr/models/res_partner.py
new file mode 100644
index 00000000..e42b3f1e
--- /dev/null
+++ b/addons/l10n_se_ocr/models/res_partner.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import fields, models, api, _
+from stdnum import luhn
+
+
+class ResPartner(models.Model):
+ _inherit = 'res.partner'
+
+ l10n_se_check_vendor_ocr = fields.Boolean(string='Check Vendor OCR', help='This Vendor uses OCR Number on their Vendor Bills.')
+ l10n_se_default_vendor_payment_ref = fields.Char(string='Default Vendor Payment Ref', help='If set, the vendor uses the same Default Payment Reference or OCR Number on all their Vendor Bills.')
+
+ @api.onchange('l10n_se_default_vendor_payment_ref')
+ def onchange_l10n_se_default_vendor_payment_ref(self):
+ if not self.l10n_se_default_vendor_payment_ref == "" and self.l10n_se_check_vendor_ocr:
+ reference = self.l10n_se_default_vendor_payment_ref
+ try:
+ luhn.validate(reference)
+ except:
+ return {'warning': {'title': _('Warning'), 'message': _('Default vendor OCR number isn\'t a valid OCR number.')}}