Skip to Content
Menu
This question has been flagged
4 Replies
3479 Views

odoo 14 enterprise,


checking with automated action, with model product_template, 

even the standard price is already input greater than 0.0, but error still showing up

if record.standard_price == 0:
raise UserError("Please update, product cost must be greater than 0.0")


I think it confuse between product.product and product.template

the purpose is to enforcing user to input for cost (standard_price)


any advice?

thanks in advance

Avatar
Discard
Author

try both model product.template and product.variant with trigger cost, but while editing product pop up window in quotation form,  this always raise product error "Please update, product cost must be greater than 0.0", even cost have been update greater than 0

pls check screenshot below:
https://drive.google.com/file/d/1Ov0RWo_XyK-OdSS9qyjFT6dsQUBPds13/view?usp=sharing 
https://drive.google.com/file/d/1OwzypGxaVwUTDoezPOqtniR-L60HXTAA/view?usp=sharing

raise UserError('Please update, product cost must be greater than 0.0')
The above line code should be 4 space indent.

create two automatic actions for product.product and product.template and use the below python and add cost field to target fields:

Please copy the code from my below answer, it's already formatted.

Author

I have tried both options, but none is work, below for screen recording, for both automation actions

https://drive.google.com/file/d/139WhsGkJ-XGwjzcLR_Sll51j59gn4C7W/view?usp=sharing

also try with empty database also experience same problem

https://drive.google.com/file/d/13CSQ4AJiivqzOkc3d_aqPgaYW6MoOk7S/view?usp=sharing

It will work with you product without variant because the standard_price in product.template is computed field.

standard_price = fields.Float(
'Cost', compute='_compute_standard_price',
inverse='_set_standard_price', search='_search_standard_price',
digits='Product Price', groups="base.group_user",
help="""In Standard Price & AVCO: value of the product (automatically computed in AVCO).
In FIFO: value of the last unit that left the stock (automatically computed).
Used to value the product when the purchase cost is not known (e.g. inventory adjustment).
Used to compute margins on sale orders.""")

and the compute method set the standard price to zero and that's raise the error see the below compute method for standard_price field:

@api.depends_context('company')
@api.depends('product_variant_ids', 'product_variant_ids.standard_price')
def _compute_standard_price(self):
# Depends on force_company context because standard_price is company_dependent
# on the product_product
unique_variants = self.filtered(lambda template: len(template.product_variant_ids) == 1)
for template in unique_variants:
template.standard_price = template.product_variant_ids.standard_price
for template in (self - unique_variants):
template.standard_price = 0.0

Author

just enable only for automatic action on model product,
it works in product variant (model: product.template that use from add product in SO and PO), for it works as expected.

but for the product, cannot save even cost have change greater than 0

Best Answer

In your automatic action select Cost(product.template) from Trigger Fields. This worked with me.

Python code:

if record.standard_price <= 0:
    raise UserError("Please update, product cost must be greater than 0.0")
Avatar
Discard
Best Answer

Hello, have u found a solutoion please ?

Avatar
Discard
Best Answer

Hi Lewi,

We can use the float_compare method from odoo to compare these values for better result.

>>First create an automated action by Settings > Technical > Automated Actions.

>>Select Product Template for the Model

>>Select On creation & update for Trigger

>>And set Action to do as Execute Python code

>>Now paste this in the Python code field

if float_compare(record.standard_price, 0.0, precision_digits=3) == 0 or record.standard_price < 0:
  raise UserError('Please update, product cost must be greater than 0.0')
This will raise warning if it is equal to 0.0 or less than 0.

Regards

Avatar
Discard
Author Best Answer

I have tried both options, but none is work, below for screen recording, for both automation actions

https://drive.google.com/file/d/139WhsGkJ-XGwjzcLR_Sll51j59gn4C7W/view?usp=sharing 


also try with empty database also experience same problem

https://drive.google.com/file/d/13CSQ4AJiivqzOkc3d_aqPgaYW6MoOk7S/view?usp=sharing 




Avatar
Discard

I can see the issue in your python code in second line, it should be 4 spaces for indentation.

Please copy the code in my answer and paste it in your automatic action python code.

Author

pls check video recording, its already indented