account_move.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models, fields, tools, api, _
  22. import json
  23. import base64
  24. import requests
  25. import io
  26. try:
  27. import qrcode
  28. except ImportError:
  29. qrcode = None
  30. class account_invoice(models.Model):
  31. _name = 'account.invoice'
  32. _inherit = 'account.invoice'
  33. _description = 'Add extra data to SET in account invoice'
  34. cdc = fields.Char(string='Código de Control (CDC)')
  35. numero_factura = fields.Char(string='Factura Nº', related='number', store = True)
  36. sucursal = fields.Char(string='Sucursal', compute='_compute_suc_sec', store=True)
  37. sec = fields.Char('Sec', compute='_compute_suc_sec', store=True)
  38. nro_actual = fields.Char('Nro Actual', compute='_compute_suc_sec', store=True)
  39. talonario_id = fields.Many2one('talonario', string='Talonario')
  40. timbrado_name = fields.Char(related='talonario_id.name', string='Número de timbrado', readonly=True)
  41. talonario_tipo_documento = fields.Selection(related='talonario_id.tipo_documento', string='Tipo de documento', readonly=True)
  42. fecha_final = fields.Date(related='talonario_id.fecha_final', string='Fecha de expiración de timbrado', readonly=True)
  43. no_mostrar_libro_iva = fields.Boolean(string='No visualizar en el libro IVA')
  44. importacion = fields.Boolean(string='Fact. de importación')
  45. importacion_gasto = fields.Boolean(string='Fact. de gastos de importación')
  46. tipo_emision = fields.Selection([('1','Normal'),('2','Contingencia')],'Tipo de Emisión')
  47. tipo_transaccion = fields.Selection([('1','Venta de mercadería'),('2','Prestación de servicios'),('3','Mixto (Venta de mercadería y servicios)'),('4','Venta de activo fijo'),('5','Venta de divisas'),('6','Compra de divisas'),('7','Promoción o entrega de muestras'),('8','Donación'),('9','Anticipo'),('10','Compra de productos'),('11','Compra de servicios'),('12','Venta de crédito fiscal'),('13','Muestras médicas (Art. 3 RG 24/2014)')],'Tipo de transacción')
  48. tipo_impuesto = fields.Selection([('1','Normal'),('2','Contingencia')],'Tipo de impuesto')
  49. indicador_presencia = fields.Selection([('1','Operación presencial'),('2','Operación electrónica'),('3','Operación telemarketing'),('4','Venta a domicilio'),('5','Operación bancaria'),('6','Operación cíclica'),('7','ver1'),('8','ver2'),('9','Otros')],'Indicador de Presencia')
  50. descripcion_indi_presencia = fields.Char('Describir Otros')
  51. # qr_code = fields.Binary('Código QR', compute='_compute_qr_code')
  52. # texto_qr = fields.Char(invisible=True)
  53. dEstRes = fields.Char('Estado de respuesta')
  54. dProtAut = fields.Char('Código de operación')
  55. dFecProc = fields.Datetime(string='Fecha de respuesta')
  56. # mje_resultado_ids = fields.One2many('mje.resultado', 'invoice_id', string='Resultados de mensajes')
  57. operacion_credito = fields.Selection([('1','Normal'),('2','Contingencia')],'Operación Crédito')
  58. metodo_pago = fields.Selection([('1','Dinero En Efectivo'),('2','Transferencia'),('3','Cheque'),('4','Tarjeta de crédito'),('5','Tarjeta de débito')],'Método de Pago')
  59. estado_de = fields.Selection([
  60. ('noenviado', 'No enviado'),
  61. ('enviado', 'Enviado'),
  62. ('aprobado', 'Aprobado'),
  63. ('rechazado', 'Rechazado'),
  64. ('cancelado', 'Cancelado')],
  65. string='Estado DE',
  66. default='noenviado'
  67. )
  68. @api.onchange('talonario_id')
  69. def _onchange_talonario_id(self):
  70. if self.talonario_id:
  71. self.timbrado_name = self.talonario_id.name
  72. self.talonario_tipo_documento = self.talonario_id.tipo_documento
  73. self.fecha_final = self.talonario_id.fecha_final
  74. else:
  75. self.timbrado_name = False
  76. self.talonario_tipo_documento = False
  77. self.fecha_final = False
  78. @api.depends('number', 'journal_id', 'state')
  79. def _compute_suc_sec(self):
  80. for invoice in self:
  81. if invoice.state == 'cancel' and invoice.type == 'out_invoice':
  82. invoice.sucursal = ''
  83. invoice.sec = ''
  84. invoice.nro_actual = ''
  85. elif invoice.journal_id.id == 2 and invoice.number and invoice.type == 'out_invoice':
  86. parts = invoice.number.split('-')
  87. invoice.sucursal = parts[0] if len(parts) > 0 else ''
  88. invoice.sec = parts[1] if len(parts) > 1 else ''
  89. invoice.nro_actual = parts[2] if len(parts) > 2 else ''
  90. else:
  91. invoice.sucursal = ''
  92. invoice.sec = ''
  93. invoice.nro_actual = ''
  94. @api.multi
  95. def action_invoice_open(self):
  96. for invoice in self:
  97. cdc = self.calcular_cdc(invoice)
  98. invoice.cdc = cdc
  99. return super(AccountInvoice, self).action_invoice_open()
  100. @staticmethod
  101. def calcular_cdc(invoice):
  102. journal_id = invoice.journal_id.code.zfill(2)
  103. ruc = invoice.partner_id.ruc
  104. fecha_factura = invoice.date_invoice.strftime("%Y%m%d")
  105. cdc = journal_id + ruc + fecha_factura
  106. return cdc
  107. # class AccountInvoiceLine(models.Model):
  108. # _inherit = 'account.invoice.line'
  109. #
  110. # product_id = fields.Many2one('product.product', 'Producto')
  111. # quantity = fields.Float('Cantidad')
  112. # price_unit = fields.Float('Precio de Venta')
  113. # subtotal = fields.Float('Subtotal', compute='_compute_subtotal')
  114. #
  115. # @api.depends('quantity', 'price_unit')
  116. # def _compute_subtotal(self):
  117. # for line in self:
  118. # line.subtotal = line.quantity * line.price_unit
  119. # class AccountInvoice(models.Model):
  120. # _inherit = 'account.invoice'
  121. #
  122. # qr_code = fields.Binary('Código QR', compute='_compute_qr_code')
  123. #
  124. # @api.depends('partner_id', 'number', 'amount_total', 'invoice_line_ids')
  125. # def _compute_qr_code(self):
  126. # for invoice in self:
  127. # if not qrcode:
  128. # invoice.qr_code = False
  129. # continue
  130. #
  131. # qr_data = {
  132. # 'ruc_emisor': invoice.company_id.partner_id.vat,
  133. # 'ruc_receptor': invoice.partner_id.vat,
  134. # 'tipo_documento': 'FACT',
  135. # 'numero_documento': invoice.number,
  136. # 'monto_total': invoice.amount_total,
  137. # 'lineas_factura': [],
  138. # }
  139. #
  140. # for line in invoice.invoice_line_ids:
  141. # linea_factura = {
  142. # 'producto': line.product_id.name,
  143. # 'cantidad': line.quantity,
  144. # 'precio_venta': line.price_unit,
  145. # 'subtotal': line.subtotal,
  146. # }
  147. # qr_data['lineas_factura'].append(linea_factura)
  148. #
  149. # qr_string = '|'.join(str(value) for value in qr_data.values())
  150. # qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_M, box_size=10, border=4)
  151. # qr.add_data(qr_string)
  152. # qr.make(fit=True)
  153. # qr_image = qr.make_image(fill_color="black", back_color="white")
  154. #
  155. # qr_image_data = io.BytesIO()
  156. # qr_image.save(qr_image_data, format='PNG')
  157. # invoice.qr_code = base64.b64encode(qr_image_data.getvalue())