Pelingier
Pelingier

Reputation: 133

How can i print my invoice report from a server action

I made a server action code to process my delivery's en to create invoices of them if they meet some criteria, now i would like to print the invoice after creation. How can i do this. Below you can see my full code :

sale_ids = []
sale_ids_onkosten = []

for picking in records:
    if picking.sale_id:
        if picking.sale_id.invoice_status != 'to invoice':
            last_invoice = picking.sale_id.invoice_ids.sorted(key=lambda i: i.id, reverse=True)[0]
            last_chauffeurblad = picking.x_chauffeurblad.sorted(key=lambda r: r.id, reverse=True)[0]
            last_chauffeurblad.update({'x_invoice_id': last_invoice.id})
            picking.update({'x_invoice_id': last_invoice.id})
            error_message = 'FACTUUR REEDS GEMAAKT'
            picking.write({'x_error': error_message})
        else:
            if any(keyword in picking.x_ff.x_name.lower() for keyword in ['maand','lever', 'week', 'weke', 'post']):
                error_message = 'NIET GEFACTUREERD FACTURATIE TYPE: ' + picking.x_ff.x_name.upper()
                picking.write({'x_error': error_message})
            else:
                sale_id_list = sale_ids_onkosten if any("onkosten" in line.product_id.name.lower() for line in picking.move_ids_without_package) else sale_ids
                if picking.sale_id.id not in sale_id_list:
                    sale_id_list.append(picking.sale_id.id)

def process_invoices(sale_ids, onkosten=False):
    grouped_sale_orders = {}
    for sale_id in sale_ids:
        sale_order = env['sale.order'].browse(sale_id)
        key = (sale_order.partner_id.id, sale_order.partner_shipping_id.id)
        grouped_sale_orders.setdefault(key, []).append(sale_order)

    for order_group in grouped_sale_orders.values():
        sale_orders = env['sale.order'].concat(*order_group)
        invoices = sale_orders._create_invoices()

        for invoice in invoices:
            partner_email = invoice.partner_id.x_invoice_email or invoice.partner_id.email
            if invoice.amount_total <= 0:
                invoice.write({'x_message': 'Fout bij bedrag'})
            else:
                invoice.write({'x_message': '✓'})
                invoice.action_post()

            for picking in records:
                if picking.sale_id and picking.sale_id.name in invoice.invoice_origin:
                    error_message = 'FACTUUR'
                    if onkosten:
                        error_message += ' (onkosten)'
                    picking.write({'x_error': error_message})

                    last_chauffeurblad = picking.x_chauffeurblad.sorted(key=lambda r: r.id, reverse=True)[0]
                    volgorde = picking.x_volgorde
                    last_note_value = last_chauffeurblad.x_drivers_sheet_id.x_name if last_chauffeurblad.x_drivers_sheet_id else ''
                    if volgorde != 0.0:
                        invoice.write({'x_volgorde': volgorde})
                    
                    last_chauffeurblad.update({'x_invoice_id': invoice.id}) #update het chauffeursblad met link naar factuur
                    picking.update({'x_invoice_id': invoice.id}) #update de link naar de factuur in de leveringsbon
                    invoice.write({'x_chauffeur': last_note_value}) #update chauffeur op de factuur
                    
                    
                    
                    

if sale_ids:
    process_invoices(sale_ids)

if sale_ids_onkosten:
    process_invoices(sale_ids_onkosten, onkosten=True)

the invoice printing should happen after invoice.write({'x_volgorde': volgorde}) i have tried .with_context(print_mode=True).print_report(report_name='account.report_invoice_with_payments')

Upvotes: 1

Views: 63

Answers (0)

Related Questions