From 0bcbe9ab374c56ebf596ae50e5a9ce7c07d094d1 Mon Sep 17 00:00:00 2001 From: houssine Date: Fri, 23 Oct 2020 16:45:28 +0200 Subject: [PATCH] [ADD] add module bringing bba communication to loan line --- easy_my_coop_loan_bba/__init__.py | 1 + easy_my_coop_loan_bba/__manifest__.py | 21 ++++ easy_my_coop_loan_bba/models/__init__.py | 1 + .../models/loan_issue_line.py | 95 +++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 easy_my_coop_loan_bba/__init__.py create mode 100644 easy_my_coop_loan_bba/__manifest__.py create mode 100644 easy_my_coop_loan_bba/models/__init__.py create mode 100644 easy_my_coop_loan_bba/models/loan_issue_line.py diff --git a/easy_my_coop_loan_bba/__init__.py b/easy_my_coop_loan_bba/__init__.py new file mode 100644 index 0000000..7f0969e --- /dev/null +++ b/easy_my_coop_loan_bba/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/easy_my_coop_loan_bba/__manifest__.py b/easy_my_coop_loan_bba/__manifest__.py new file mode 100644 index 0000000..6c7d193 --- /dev/null +++ b/easy_my_coop_loan_bba/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2020 - ongoing Coop IT Easy (http://www.coopiteasy.be) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + 'name': 'Easy My Coop Loan BBA', + 'category': 'Sales', + 'author': "Coop IT Easy - Houssine BAKKALI ", + 'website': 'https://www.coopiteasy.be', + 'version': '12.0.1.0.0', + 'license': 'AGPL-3', + 'depends': [ + 'easy_my_coop_loan', + 'l10n_be_invoice_bba' + ], + "description": """ + This module implements the bba structured communication on the loan line. + """, + 'data': [ + ], + 'installable': True, +} diff --git a/easy_my_coop_loan_bba/models/__init__.py b/easy_my_coop_loan_bba/models/__init__.py new file mode 100644 index 0000000..c1a974b --- /dev/null +++ b/easy_my_coop_loan_bba/models/__init__.py @@ -0,0 +1 @@ +from . import loan_issue_line diff --git a/easy_my_coop_loan_bba/models/loan_issue_line.py b/easy_my_coop_loan_bba/models/loan_issue_line.py new file mode 100644 index 0000000..35e880e --- /dev/null +++ b/easy_my_coop_loan_bba/models/loan_issue_line.py @@ -0,0 +1,95 @@ +# © 2018 Coop IT Easy (http://www.coopiteasy.be) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import random +import re + +from odoo import api, fields, models, _ +from odoo.exceptions import UserError + + +class LoanIssueLine(models.Model): + + _inherit = 'loan.issue.line' + + @api.multi + def generate_bbacomm(self): + self.ensure_one() + algorithm = self.company_id.l10n_be_structured_comm + if algorithm == 'date': + date = fields.Date.from_string(fields.Date.today()) + doy = date.strftime('%j') + year = date.strftime('%Y') + seq = '001' + invoices = self.search([('type', '=', 'out_invoice'), + ('reference', 'like', '+++%s/%s/%%' % + (doy, year))], order='reference') + if invoices: + prev_seq = int(invoices[-1].reference[12:15]) + if prev_seq < 999: + seq = '%03d' % (prev_seq + 1) + else: + raise UserError(_('The daily maximum of outgoing invoices ' + 'with an automatically generated BBA ' + 'Structured Communications has been ' + 'exceeded!' '\nPlease create manually a ' + 'unique BBA Structured Communication.')) + bbacomm = doy + year + seq + base = int(bbacomm) + mod = base % 97 or 97 + reference = '+++%s/%s/%s%02d+++' % (doy, year, seq, mod) + elif algorithm == 'partner_ref': + partner_ref = self.partner_id.ref + print(partner_ref) + partner_ref_nr = re.sub('\D', '', partner_ref or '') + if (len(partner_ref_nr) < 3) or (len(partner_ref_nr) > 7): + raise UserError(_('The Customer should have an Internal ' + 'Reference with min 3 and max 7 digits ' + 'for the generation of BBA Structured ' + 'Communications!')) + else: + partner_ref_nr = partner_ref_nr.ljust(7, '0') + seq = '001' + invoices = self.search([('type', '=', 'out_invoice'), + ('reference', 'like', '+++%s/%s/%%' % + (partner_ref_nr[:3], + partner_ref_nr[3:])) + ], order='reference') + if invoices: + prev_seq = int(invoices[-1].reference[12:15]) + if prev_seq < 999: + seq = '%03d' % (prev_seq + 1) + else: + raise UserError(_( + 'The daily maximum of outgoing invoices with an ' + 'automatically generated BBA Structured ' + 'Communications has been exceeded!' + '\nPlease create manually a unique BBA Structured ' + 'Communication.')) + bbacomm = partner_ref_nr + seq + base = int(bbacomm) + mod = base % 97 or 97 + reference = '+++%s/%s/%s%02d+++' % (partner_ref_nr[:3], + partner_ref_nr[3:], seq, mod) + elif algorithm == 'random': + base = random.randint(1, 9999999999) + bbacomm = str(base).rjust(10, '0') + base = int(bbacomm) + mod = base % 97 or 97 + mod = str(mod).rjust(2, '0') + reference = '+++%s/%s/%s%s+++' % (bbacomm[:3], + bbacomm[3:7], + bbacomm[7:], mod) + else: + raise UserError(_("Unsupported Structured Communication Type " + "Algorithm '%s' !" + "\nPlease contact your Odoo support channel." + ) % algorithm) + return reference + + @api.multi + def action_validate(self): + super(LoanIssueLine, self).action_validate() + for line in self: + bba = line.generate_bbacomm() + line.write({'reference': bba})