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.

95 lines
4.3 KiB

  1. # © 2018 Coop IT Easy (http://www.coopiteasy.be)
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import random
  4. import re
  5. from odoo import api, fields, models, _
  6. from odoo.exceptions import UserError
  7. class LoanIssueLine(models.Model):
  8. _inherit = 'loan.issue.line'
  9. @api.multi
  10. def generate_bbacomm(self):
  11. self.ensure_one()
  12. algorithm = self.company_id.l10n_be_structured_comm
  13. if algorithm == 'date':
  14. date = fields.Date.from_string(fields.Date.today())
  15. doy = date.strftime('%j')
  16. year = date.strftime('%Y')
  17. seq = '001'
  18. invoices = self.search([('type', '=', 'out_invoice'),
  19. ('reference', 'like', '+++%s/%s/%%' %
  20. (doy, year))], order='reference')
  21. if invoices:
  22. prev_seq = int(invoices[-1].reference[12:15])
  23. if prev_seq < 999:
  24. seq = '%03d' % (prev_seq + 1)
  25. else:
  26. raise UserError(_('The daily maximum of outgoing invoices '
  27. 'with an automatically generated BBA '
  28. 'Structured Communications has been '
  29. 'exceeded!' '\nPlease create manually a '
  30. 'unique BBA Structured Communication.'))
  31. bbacomm = doy + year + seq
  32. base = int(bbacomm)
  33. mod = base % 97 or 97
  34. reference = '+++%s/%s/%s%02d+++' % (doy, year, seq, mod)
  35. elif algorithm == 'partner_ref':
  36. partner_ref = self.partner_id.ref
  37. print(partner_ref)
  38. partner_ref_nr = re.sub('\D', '', partner_ref or '')
  39. if (len(partner_ref_nr) < 3) or (len(partner_ref_nr) > 7):
  40. raise UserError(_('The Customer should have an Internal '
  41. 'Reference with min 3 and max 7 digits '
  42. 'for the generation of BBA Structured '
  43. 'Communications!'))
  44. else:
  45. partner_ref_nr = partner_ref_nr.ljust(7, '0')
  46. seq = '001'
  47. invoices = self.search([('type', '=', 'out_invoice'),
  48. ('reference', 'like', '+++%s/%s/%%' %
  49. (partner_ref_nr[:3],
  50. partner_ref_nr[3:]))
  51. ], order='reference')
  52. if invoices:
  53. prev_seq = int(invoices[-1].reference[12:15])
  54. if prev_seq < 999:
  55. seq = '%03d' % (prev_seq + 1)
  56. else:
  57. raise UserError(_(
  58. 'The daily maximum of outgoing invoices with an '
  59. 'automatically generated BBA Structured '
  60. 'Communications has been exceeded!'
  61. '\nPlease create manually a unique BBA Structured '
  62. 'Communication.'))
  63. bbacomm = partner_ref_nr + seq
  64. base = int(bbacomm)
  65. mod = base % 97 or 97
  66. reference = '+++%s/%s/%s%02d+++' % (partner_ref_nr[:3],
  67. partner_ref_nr[3:], seq, mod)
  68. elif algorithm == 'random':
  69. base = random.randint(1, 9999999999)
  70. bbacomm = str(base).rjust(10, '0')
  71. base = int(bbacomm)
  72. mod = base % 97 or 97
  73. mod = str(mod).rjust(2, '0')
  74. reference = '+++%s/%s/%s%s+++' % (bbacomm[:3],
  75. bbacomm[3:7],
  76. bbacomm[7:], mod)
  77. else:
  78. raise UserError(_("Unsupported Structured Communication Type "
  79. "Algorithm '%s' !"
  80. "\nPlease contact your Odoo support channel."
  81. ) % algorithm)
  82. return reference
  83. @api.multi
  84. def action_validate(self):
  85. super(LoanIssueLine, self).action_validate()
  86. for line in self:
  87. bba = line.generate_bbacomm()
  88. line.write({'reference': bba})