summaryrefslogtreecommitdiff
path: root/addons/l10n_se_ocr/models/account_move.py
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/account_move.py
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/l10n_se_ocr/models/account_move.py')
-rw-r--r--addons/l10n_se_ocr/models/account_move.py73
1 files changed, 73 insertions, 0 deletions
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()