# coding=utf-8
from sii import models, __SII_VERSION__


class FacturasEmitidasDictGenerator(models.SuministroFacturasEmitidas):

    def dump(self, invoice, many=None, update_fields=True, **kwargs):
        base_imponible = ''
        for tax in invoice.tax_line:
            if 'IVA' in tax.name:
                base_imponible = tax.base
                break
        if base_imponible:
            tipo_desglose = {
                'DesgloseFactura': {
                    'Sujeta': {
                        'NoExenta': {  # TODO Exenta o no exenta??
                            'TipoNoExenta': 'S1',  # TODO to change
                            'DesgloseIVA': {
                                'DetalleIVA': {
                                    'TipoImpositivo': '',
                                    'BaseImponible': base_imponible,
                                    'CuotaRepercutida': '0'  # TODO
                                }
                            }
                        }
                    }
                }
            }
        else:
            tipo_desglose = {
                'DesgloseFactura': {
                    'NoSujeta': {

                    }
                }
            }

        factura_expedida = {
            'TipoFactura': '',
            'ClaveRegimenEspecialOTrascendencia': '',
            'ImporteTotal': '',
            'DescripcionOperacion': '',
            'Contraparte': {
                'NombreRazon': '',
                'NIF': ''
            },
            'TipoDesglose': {
                'DesgloseFactura': {
                    'Sujeta': ''
                }
            }
        }

        new_obj = {
            'SuministroLRFacturasEmitidas': {
                'Cabecera': {
                    'IDVersionSii': __SII_VERSION__,
                    'Titular': {
                        'NombreRazon': invoice.partner_id.name,
                        'NIF': invoice.partner_id.nif
                    },
                    'TipoComunicacion': 'A0'
                },
                'RegistroLRFacturasEmitidas': {
                    'PeriodoImpositivo': {
                        'Ejercicio': '',
                        'Periodo': ''
                    },
                    'IDFactura': {
                        'IDEmisorFactura': {
                            'NIF': ''
                        },
                        'NumSerieFacturaEmisor': invoice.number,
                        'FechaExpedicionFacturaEmisor': ''
                    },
                    # 'FacturaExpedida': factura_expedida
                }
            }
        }

        object_generated = self.load(new_obj)
        errors = object_generated.errors
        if errors:
            raise Exception(
                'Errors were found while trying to generate the dump', errors)

        res = super(models.SuministroFacturasEmitidas, self).dump(
            new_obj, many, update_fields, **kwargs)

        return res


class SII(object):
    @staticmethod
    def generate_object(invoice):

        if invoice.type == 'in_invoice':
            invoice_model = models.SuministroFacturasRecibidas()
        elif invoice.type == 'out_invoice':
            invoice_model = FacturasEmitidasDictGenerator()
        else:
            raise Exception('Error in invoice.type')

        res = invoice_model.dump(invoice).data
        return res

@@ -1273,24 +1273,26 @@
 oFactura': '
+F1
 ',%0A         
@@ -1364,18 +1364,36 @@
 Total': 
-''
+invoice.amount_total
 ,%0A      
@@ -1563,111 +1563,21 @@
 e': 
-%7B%0A                'DesgloseFactura': %7B%0A                    'Sujeta': ''%0A                %7D%0A            %7D
+tipo_desglose
 %0A   
@@ -2095,26 +2095,51 @@
 Ejercicio': 
-''
+invoice.period_id.name%5B3:7%5D
 ,%0A          
@@ -2163,18 +2163,43 @@
 riodo': 
-''
+invoice.period_id.name%5B0:2%5D
 %0A       
@@ -2480,18 +2480,36 @@
 misor': 
-''
+invoice.date_invoice
 %0A       
@@ -2543,18 +2543,16 @@
         
- #
  'Factur
@@ -2635,86 +2635,39 @@
     
-object_generated = self.load(new_obj)%0A        errors = object_generated.errors
+errors = self.validate(new_obj)
 %0A   
@@ -2779,16 +2779,17 @@
 the dump
+:
 ', error

Update fields obtained from invoice in resource.py