Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
1 Responder
176 Visualizações

Hey, I'm new to Odoo. I need to send 100s of invoices that are either for product A or product A+B to 100s of customers. I was hoping I could make an automation in the CRM module so that when I change an opportunity to stage X in the CRM -> create and send invoice to customer associated with the opportunity. If my custom property checkbox is left unchecked, then invoice with item A should be sent, and if checked, invoice with products A+B should be sent. Prices are constant for both products so the outgoing invoices are always one of two amounts (no customisation needed). 


How might I go about doing this? In Automation Rules, the Trigger seems straight forward, but the Action not so much...


Thank you

Avatar
Cancelar
Melhor resposta

Hi,

Steps:

Go to Settings → Technical → Automation → Automated Actions.

Create a new one:

Model = CRM Lead

Trigger = On Update

Condition = stage_id == 'X'

Action to do: Execute Python Code.

Code:

# This runs in the context of crm.lead record(s)

for lead in records:

    partner = lead.partner_id

    if not partner:

        continue


    # Decide which products to add

    product_ids = []

    if lead.x_my_checkbox:  # your custom field

        product_ids = [env.ref('your_module.product_a').id,

                       env.ref('your_module.product_b').id]

    else:

        product_ids = [env.ref('your_module.product_a').id]


    # Create invoice (draft)

    invoice = env['account.move'].create({

        'move_type': 'out_invoice',

        'partner_id': partner.id,

        'invoice_line_ids': [

            (0, 0, {

                'product_id': pid,

                'quantity': 1,

                'price_unit': env['product.product'].browse(pid).lst_price,

            }) for pid in product_ids

        ]

    })


    # Post and send by email

    invoice.action_post()

    template = env.ref('account.email_template_edi_invoice')

    invoice.message_post_with_template(template.id)


Hope it helps.

Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
1
ago. 24
2215
1
nov. 23
3621
0
mai. 20
3180
2
ago. 25
1596
2
dez. 24
1618