Skip to Content
Menu
This question has been flagged
7 Replies
6793 Views

I have to check the product type of each line in the sale.order.line before invoicing. So in the sale.py I have added the code under action_invoice_create(self, grouped=False, final=False) method (line 342 of source code).

But the code doesn't enter the print condition according to the product type. How to correct?

if group_key not in invoices:
 if 'product_id.product_tmpl_id.type' == 'service':
  print 'Service product'
     elif 'product_id.product_tmpl_id.type' == 'stockable':
         print 'Stockable product'      
     else:
         print 'Service product'

Avatar
Discard
Best Answer

Hello Naksha,

All you did is correct , you just need to correct your field name declaration as you code it in string.

Replace this 'product_id.product_tmpl_id.type' to product_id.product_tmpl_id.type.

Paste this code:

if group_key not in invoices:
  if product_id.product_tmpl_id.type == 'service':
   print 'Service product'
elif product_id.product_tmpl_id.type == 'stockable':
      print 'Stockable product'
  else:
  print 'Service product'

Avatar
Discard
Author

But removing the single quotes just make Unresolved reference 'product_id'. I tried that in pycharm.

Can you share your field declaration code ?

Your .py file.

Author

@api.multi

def action_invoice_create(self, grouped=False, final=False):

"""

Create the invoice associated to the SO.

:param grouped: if True, invoices are grouped by SO id. If False, invoices are grouped by

(partner_invoice_id, currency)

:param final: if True, refunds will be generated if necessary

:returns: list of created invoices

"""

inv_obj = self.env['account.invoice']

precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')

invoices = {}

references = {}

for order in self:

group_key = order.id if grouped else (order.partner_invoice_id.id, order.currency_id.id)

for line in order.order_line.sorted(key=lambda l: l.qty_to_invoice < 0):

if float_is_zero(line.qty_to_invoice, precision_digits=precision):

continue

if group_key not in invoices:

if group_key not in invoices:

if product_id.product_tmpl_id.type == 'service':

print 'Service product'

elif product_id.product_tmpl_id.type == 'stockable':

print 'Stockable product'

else:

print 'Consumable product'

inv_data = order._prepare_invoice()

invoice = inv_obj.create(inv_data)

references[invoice] = order

invoices[group_key] = invoice

elif group_key in invoices:

vals = {}

if order.name not in invoices[group_key].origin.split(', '):

vals['origin'] = invoices[group_key].origin + ', ' + order.name

if order.client_order_ref and order.client_order_ref not in invoices[group_key].name.split(', ') and order.client_order_ref != invoices[group_key].name:

vals['name'] = invoices[group_key].name + ', ' + order.client_order_ref

invoices[group_key].write(vals)

if line.qty_to_invoice > 0:

line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice)

elif line.qty_to_invoice < 0 and final:

line.invoice_line_create(invoices[group_key].id, line.qty_to_invoice)

if references.get(invoices.get(group_key)):

if order not in references[invoices[group_key]]:

references[invoice] = references[invoice] | order

That is the source code from sale.py odoo 10 community edition(from line 324). I was trying my code directly there. (It's not the norm, just experimenting)

Hi,

I think in the loop your are missing variable line.

Can you try with line.product_id.product_tmpl_id.type

Hope it helps,

Regards,

Mayank Gosai

You modified the original sale.py file so you need to upgrade the sales app --> from Apps open sales app and upgrade it. give it a try

Best Answer

action_invoice_create method belogs to "sale.order" model.  product_id field belongs to "sale.order.line" model and is linked in "sale.order" by order_line field.

So you can not refer to product_id without appropriate prefix: line.product_id in this case while iterating by self.order_line.

Anyway  to avoid future headake you should write custom module instead of changing standrd one. 

рекомендую почитать доки по питону (https://docs.python.org).

Avatar
Discard
Related Posts Replies Views Activity
3
May 23
7957
1
Jan 16
4488
0
Jul 24
907
1
Mar 24
14109
0
Mar 15
4696