Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
10030 Vistas

Hi guys,

I'm building a module that copies a whole invoice from one database to another the moment the user clicks on a button. I have a working concept, as long as there is one line (one product) on the invoice. 
The code:

# Get all ids from all taxes in the current database tax_ids = [] for tax_id in self.invoice_line.invoice_line_tax_id: result = tax_proxy.search([('name', '=', tax_id.name)]) # This means the tax doesn't exist in the target db if not result: # Create tax in target database. new_tax_id = tax_proxy.create({ 'name': tax_id.name, 'amount': tax_id.amount, 'price_include': tax_id.price_include, 'include_base_amount': tax_id.include_base_amount, 'child_depend': tax_id.child_depend }) tax_ids.append(new_tax_id.id) else: tax_ids.append(tax_id.id) # Create the invoice! invoice_proxy.create({ 'partner_id': partner_id, # This should be Mavi-Rent (res.contact company record) 'account_id': account[0], 'date_invoice': self.date_invoice, 'fiscal_position': self.fiscal_position, 'payment_term': self.payment_term, 'comment': self.comment, 'type': 'in_invoice', 'invoice_line': [(0, 0, { 'name': self.invoice_line.name, 'product_id': self.invoice_line.product_id.id, 'account_id': self.invoice_line.account_id.id, 'invoice_line_tax_id': [(6, 0, tax_ids)], 'quantity': self.invoice_line.quantity, 'price_unit': self.invoice_line.price_unit })] })

This however leaves me with two problems that I am unsure on how to solve:
1) The taxes are all added to a list before I do invoice_proxy.create, to create the invoice. This means that all taxes from all products will be applied. The reason that I did this before invoice_proxy.create was exactly for the same issue as problem #2.
2) This code will work if there is an invoice with one product line, but not with multiple (otherwise you'll get a singleton error).

So what I'm wondering is how should I exactly create multiple invoice_lines (with the correct taxes filled in in the many2many invoice_line_tax_id) in this .create function?

Regards,
Yenthe

Avatar
Descartar
Autor Mejor respuesta

Hi guys,

I ended up doing invoice_proxy.create first without the orderlines, just the invoice template.
After I created the invoice I loop over every line in invoice_line and do invoice_proxy.write to write one product at a time on the invoice. The code to create the invoice:

# Create the invoice! new_invoice = invoice_proxy.create({ 'partner_id': partner_id, # This should be Mavi-Rent (res.contact company record) 'account_id': account[0], 'date_invoice': self.date_invoice, 'date_due': self.date_due, 'reference': self.reference, 'origin': self.origin, 'fiscal_position': self.fiscal_position, 'payment_term': self.payment_term, 'comment': self.comment, 'type': 'in_invoice'

})

Writing a invoice_line away at a time in a for loop:

for invoice_line in self.invoice_line: # Write a line on the before created invoice new_invoice.write({ 'invoice_line': [(0, 0, { 'name': invoice_line.name, 'product_id': product_id, 'account_id': invoice_line.account_id.id, 'invoice_line_tax_id': [(6, 0, tax_ids)], 'quantity': invoice_line.quantity, 'price_unit': invoice_line.price_unit })]

})


When doing these operations there are a few things to watch out for though. Some examples:

  • Products may exist on your current database, but not on the database you write to

  • Taxes may exist on your current database, but not on the database you write to

  • Customers may exist on your customer database, but not on the database you write to

  • Any record that does not exist on your target database should be created through XML-RPC in the target database. Be sure to take the ID from the record of the remote database, not the current database. You will otherwise try to reference products from your local database in the target database that do not exist there (or, even worse, link to another product)

I hope this helps somebody in the future.

Yenthe

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
ago 21
3260
1
mar 15
7172
2
may 22
11567
1
oct 24
1419
1
abr 24
1485