Skip to Content
Menu
This question has been flagged
1 Reply
1774 Views

Hi everyone, I've been trying to point to specific index in the output of my browse method.


this is my code:

def unir_facturas_boton(self, vals):
active_invoices = self.env['account.move']._context.get('active_ids', [])
active_records = self.env['account.move'].browse(active_invoices)
# source = active_records[--1]
# source_2 = active_records[0]

for items in active_records:
print(items)
# if items[-1].partner_id != items[0].partner_id:
# raise ValidationError('No es posible unir facturas con clientes diferentes')
# else:
# invoice = self.env['account.move'].create(
# {
# 'company_id': items[-1].company_id,
# 'type': items[-1].type,
# 'invoice_date': items[-1].invoice_date,
# 'partner_id': items[-1].partner_id.id,
# 'currency_id': items[-1].currency_id.id,
# 'journal_id': 11
# })
# for lines in items[-1].invoice_line_ids:
# invoice.write({
# "invoice_line_ids": items[0].invoice_line_ids.append(lines)


# })

the commented part is giving me erros since I cant catch the index.



this the output of the browse method:

account.move(2,)
account.move(1,)


how can I point to an specific one of these two?



Avatar
Discard
Best Answer

It looks like you are trying to merge invoices and don't want to merge them if they each have a different partner_id.  My question would be: are you selecting invoices that have the same partner_id?

As for the error, I think you're using your for loop syntax incorrectly.  It might help to instead write it as:

for item in active_records:

Each item will be iterated through, and depending on the type of object they may have elements 0 and -1, but it's unlikely.  You would want to check active_records[0] against active_records[-1] for a match on the partner_id.  At least that's how it makes sense in my head.

Here's a link on how that particular function works in case you'd like a refresher:
https://www.geeksforgeeks.org/iterate-over-a-list-in-python/

Hope that helps.

Avatar
Discard
Author

Yes, I just solved it doing the same thing, instead of iterating over the values of active_record I just created to variables that store each index, In my case I'll always just use 2 active_ids so it would be only active_record[0] and active_record[1], but now I'm having issues trying to append the invoice_line_ids from record[0] with record[1] and making a new invoice based on this

Not sure if this will solve your new issue, but I would think you'd want to create the new invoice object first and append the invoice_line_ids to that new object. Alternatively you could just use one of the existing ones and append the ones from the other prior to archiving it.

Related Posts Replies Views Activity
4
Feb 24
4726
3
Mar 24
1663
0
Nov 22
1218
2
Feb 22
4337
0
Nov 21
1754