Skip to Content
Menu
This question has been flagged
2 Replies
2113 Views

calculate the quantity invoiced for a product a​nd display the total on the product page, using  new computed field that sums the invoiced quantities from all relevant invoices so i tried this  function :

for product in self:
​total_invoiced_qty = 0
    invoice_lines = self.env['account.move.line'].search([
        ('product_id', '=', product.id),
        ('move_id.state', 'in', ['posted']),  # Consider only posted invoices
        ('move_id.invoice_date', '>=', '2023-01-01'),
        ('move_id.invoice_date', '>=', '2023-12-31'),
        ('move_id.move_type', '=', 'out_invoice')  # Consider only customer invoices
    ])
    for line in invoice_lines:
        total_invoiced_qty =total_invoiced_qty + line.quantity
        product['x_studio_integer_fieldstockint'] = total_invoiced_qty

i found that the given result id difeerent than sales report  

https://i.imgur.com/77tLRO2.png

Avatar
Discard
Author Best Answer

hello

thank you for your answer . i got error on dependencies 

" Unknown field 'invoice_lines' in dependency 'invoice_lines.quantity' "

Avatar
Discard
Best Answer

Hi,

Try creating a new computed field,

  • Go to the product form view in Odoo Studio
  • Add a new field, name it as "x_total_invoiced_qty"
  • Set its type to "Float" and mark it as a computed field

for record in self:

    total_invoiced_qty = 0

    invoice_lines = self.env['account.move.line'].search([

        ('product_id', '=', record.id),

        ('move_id.state', '=', 'posted'),

        ('move_id.invoice_date', '>=', '2023-01-01'),

        ('move_id.invoice_date', '

        ('move_id.move_type', 'in', ['out_invoice', 'out_refund'])

    ])

    for line in invoice_lines:

        if line.move_id.move_type == 'out_invoice':

            total_invoiced_qty += line.quantity

        else:  # out_refund

            total_invoiced_qty -= line.quantity

    record['x_total_invoiced_qty'] = total_invoiced_qty

set these field as the depending fields -> line_ids.quantity,line_ids.move_id.state, line_ids.move_id.invoice_date,line_ids.move_id.move_type

Thanks

Avatar
Discard
Author

hello

thank you for your answer . i got error on dependencies

" Unknown field 'invoice_lines' in dependency 'invoice_lines.quantity' "

Related Posts Replies Views Activity
3
Jul 25
2955
1
Jun 25
4859
2
May 25
2904
1
May 25
1972
1
Feb 25
40