示例#1
0
    def exportFatturaPA(self):
        invoice_obj = self.env['account.invoice']
        invoices_by_partner = self.group_invoices_by_partner()
        attachments = self.env['fatturapa.attachment.out']
        for partner_id in invoices_by_partner:
            invoice_ids = invoices_by_partner[partner_id]
            partner = self.getPartnerId(invoice_ids)
            if partner.is_pa:
                fatturapa = FatturaElettronica(versione='FPA12')
            else:
                fatturapa = FatturaElettronica(versione='FPR12')

            company = self.env.user.company_id
            context_partner = self.env.context.copy()
            context_partner.update({'lang': partner.lang})
            try:
                self.with_context(context_partner).setFatturaElettronicaHeader(
                    company, partner, fatturapa)
                for invoice_id in invoice_ids:
                    inv = invoice_obj.with_context(context_partner).browse(
                        invoice_id)
                    inv.set_taxes_for_descriptive_lines()
                    if inv.fatturapa_attachment_out_id:
                        raise UserError(
                            _("Invoice %s has e-invoice export file yet.") %
                            (inv.number))
                    if self.report_print_menu:
                        self.generate_attach_report(inv)
                    invoice_body = FatturaElettronicaBodyType()
                    inv.preventive_checks()
                    self.with_context(
                        context_partner).setFatturaElettronicaBody(
                            inv, invoice_body)
                    fatturapa.FatturaElettronicaBody.append(invoice_body)
                    # TODO DatiVeicoli

                number = self.setProgressivoInvio(fatturapa)
            except (SimpleFacetValueError, SimpleTypeValueError) as e:
                raise UserError(str(e))

            attach = self.saveAttachment(fatturapa, number)
            attachments |= attach

            for invoice_id in invoice_ids:
                inv = invoice_obj.browse(invoice_id)
                inv.write({'fatturapa_attachment_out_id': attach.id})

        action = {
            'view_type': 'form',
            'name': "Export Electronic Invoice",
            'res_model': 'fatturapa.attachment.out',
            'type': 'ir.actions.act_window',
        }
        if len(attachments) == 1:
            action['view_mode'] = 'form'
            action['res_id'] = attachments[0].id
        else:
            action['view_mode'] = 'tree,form'
            action['domain'] = [('id', 'in', attachments.ids)]
        return action
    def exportInvoiceXML(
            self, company, partner, invoice_ids, attach=False, context=None):
        if context is None:
            context = {}
        invoice_obj = self.env['account.invoice']
        if partner.is_pa:
            fatturapa = FatturaElettronica(versione='FPA12')
        else:
            fatturapa = FatturaElettronica(versione='FPR12')

        try:
            self.with_context(context). \
                setFatturaElettronicaHeader(company, partner, fatturapa)
            for invoice_id in invoice_ids:
                inv = invoice_obj.with_context(context).browse(invoice_id)
                if not attach and inv.fatturapa_attachment_out_id:
                    raise UserError(
                        _("E-invoice export file still present for invoice %s.")
                        % (inv.number))
                if self.report_print_menu:
                    self.generate_attach_report(inv)
                invoice_body = FatturaElettronicaBodyType()
                inv.preventive_checks()
                self.with_context(
                    context
                ).setFatturaElettronicaBody(
                    inv, invoice_body)
                fatturapa.FatturaElettronicaBody.append(invoice_body)
                # TODO DatiVeicoli

            number = self.setProgressivoInvio(fatturapa, attach=attach)
        except (SimpleFacetValueError, SimpleTypeValueError) as e:
            raise UserError(unicode(e))
        return fatturapa, number