-
Notifications
You must be signed in to change notification settings - Fork 179
/
payment_order.py
45 lines (38 loc) · 1.6 KB
/
payment_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
# © 2016 Alessandro Fernandes Martini, Trustcode
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import time
import base64
from ..febraban.cnab import Cnab
from datetime import datetime
from odoo import api, fields, models
from odoo.exceptions import UserError
class PaymentOrder(models.Model):
_inherit = 'payment.order'
cnab_file = fields.Binary('CNAB File', readonly=True)
file_number = fields.Integer(u'Número sequencial do arquivo', readonly=1)
data_emissao_cnab = fields.Datetime('Data de Emissão do CNAB')
@api.multi
def gerar_cnab(self):
if len(self.line_ids) < 1:
raise UserError(
u'Ordem de Cobrança não possui Linhas de Cobrança!')
self.data_emissao_cnab = datetime.now()
self.file_number = self.env['ir.sequence'].next_by_code('cnab.nsa')
for order_id in self:
order = self.env['payment.order'].browse(order_id.id)
cnab = Cnab.get_cnab(
order.payment_mode_id.bank_account_id.bank_bic, '240')()
remessa = cnab.remessa(order)
self.name = 'CNAB%s%s.REM' % (
time.strftime('%d%m'), str(order.file_number))
self.state = 'done'
self.cnab_file = base64.b64encode(remessa)
self.env['ir.attachment'].create({
'name': self.name,
'datas': self.cnab_file,
'datas_fname': self.name,
'description': 'Arquivo CNAB 240',
'res_model': 'payment.order',
'res_id': order_id
})