2
votes

I have a situation like this:

I want to create a Customer Invoice with 3 invoice lines: one for Customer payment, one for payment processing fee and one for a commission.

When the invoice is created, I've got an automated action set up to create 3 separate Supplier invoices, one per invoice line. That's fine. However I need a way to select values from specific invoice lines, based on the invoice.line.product_id. At the moment I can get the invoice line values like this, using python expression on the Automatic Actions interface:

[(0, 0, {'name': object.invoice_line.name, 'product_id': object.invoice_line.product_id.id, 'account_id': object.invoice_line.account_id.id, 'account_analytic_id': object.invoice_line.account_analytic_id.id, 'quantity': object.invoice_line.quantity, 'price_unit': object.invoice_line.price_unit})]

And this works fine when there's a single invoice line. However, I want to do something like this:

for line in object.invoice_line:
    if line.product_id == 2:
        [(0, 0, {'name': line.name, 'product_id': line.product_id.id, 'account_id': line.account_id.id, 'account_analytic_id': line.account_analytic_id.id, 'quantity': line.quantity, 'price_unit': line.price_unit})]

I get an error like this:

File "<string>", line 1
for line in object.invoice_line:
  ^
SyntaxError: invalid syntax

I'm not too familiar with python but that looks like it should work, given that object.invoice_line can be iterated through like an array. I'm sure there's a correct way to iterate through one2many records like this, that I'm not aware with.

I could simply put Payment Provider Fee and Commission as custom fields on the Customer invoice record and then access them directly without much of an issue. That works but it's less optimal than having them all live as invoice lines.

EDIT 1:

Seems like you can't add condition statements at all through Automated Actions, at least the ones created through the interface. However, I can just call specific element by doing something like object.invoice_line[0], [1], etc. Since the order of items will always be the same, I can simply use that. However, if there's a better way, or even a way, to search for a specific line using conditions, I'd rather use that.

Thanks.

2

2 Answers

0
votes

The "for" declaration is ok, the line below is not.

if line.product_id = 2: should be if line.product_id == 2: with 2 = since is a comparison.

Moreover, you could have errors above that line. You should provide the whole code and the whole traceback.

0
votes

you can do it in a bit different way since you need to create vendor bills from a single invoice, do it not from invoice but from invoice lines. So for example, after the invoice will be validated, invoice lines also get a status update so you can trigger vendor bill creation from each line.