Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] product_pricelist_margin #1561

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from

Conversation

nguyenminhchien
Copy link

@nguyenminhchien nguyenminhchien commented Mar 21, 2024

This module shows the product's cost and margin from the pricelists.

@nguyenminhchien nguyenminhchien marked this pull request as ready for review March 21, 2024 04:21
@nguyenminhchien nguyenminhchien force-pushed the 16.0-mig-product_pricelist_margins branch from d577643 to 9e28a94 Compare March 21, 2024 04:43
)

@api.depends("fixed_price", "cost")
def _compute_margin(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a unit test the behavior of this method with using pricelist item with a different price computation ?
For example : percentage or formula

Copy link
Contributor

@legalsylvain legalsylvain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting module. However, if you want to compute margin you should depends on account to take into account the vat that can be included.

See similar module : https://github.com/OCA/product-attribute/blob/16.0/product_pricelist_simulation_margin/wizards/wizard_preview_pricelist.py#L42

with Form(self.line) as line:
line.compute_price = "percentage"
self.assertEqual(line.cost, 20.0)
self.assertEqual(line.margin, 0)
Copy link
Contributor

@santostelmo santostelmo Mar 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We apply 20% discount and the margin is 0 ? 🤔

@cyrilmanuel
Copy link

Hii @legalsylvain is it good for you ?

have a great day

@legalsylvain
Copy link
Contributor

No. Margin is still bad computed. See previous comment.

@chaule97 chaule97 force-pushed the 16.0-mig-product_pricelist_margins branch 3 times, most recently from 0905b26 to a238034 Compare July 11, 2024 05:21
@nguyenminhchien
Copy link
Author

No. Margin is still bad computed. See previous comment.

Hi @legalsylvain , Mr. @chaule97 just improved the compute function. Please have a look. Thanks.

@chaule97
Copy link
Contributor

Hi @legalsylvain , I have updated as per your request. Hope it is good for you

Copy link
Contributor

@legalsylvain legalsylvain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. code review and test. Some remarks inline.
Also, UX improvment proposal, to save place :
image

Thanks a lot for your contribution !

"website": "https://github.com/OCA/product-attribute",
"author": "Camptocamp, Odoo Community Association (OCA)",
"license": "AGPL-3",
"depends": ["product"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"depends": ["product"],
"depends": ["account"],

account_tax.compute_all() function is in account module

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is still pending.

Comment on lines 10 to 11
<field name="margin" />
<field name="margin_percent" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="margin" />
<field name="margin_percent" />
<field name="margin" optional="show"/>
<field name="margin_percent" optional="show"/>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow users to hide percent and / or net value if they don't need the information.

Comment on lines 27 to 28
<field name="margin" />
<field name="margin_percent" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="margin" />
<field name="margin_percent" />
<field name="margin" optional="show" />
<field name="margin_percent" optional="show" />

Comment on lines 13 to 14
<field name="margin" />
<field name="margin_percent" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="margin" />
<field name="margin_percent" />
<field name="margin" optional="show" />
<field name="margin_percent" optional="show" />

Comment on lines 21 to 22
<field name="margin" />
<field name="margin_percent" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="margin" />
<field name="margin_percent" />
<field name="margin" optional="show"/>
<field name="margin_percent" optional="show"/>

<field name="model">product.pricelist.item</field>
<field name="arch" type="xml">
<field name="fixed_price" position="after">
<field name="cost" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="cost" />
<field name="cost" invisible="1" />

/>
<field name="arch" type="xml">
<field name="fixed_price" position="after">
<field name="cost" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="cost" />
<field name="cost" invisible="1" />

product_pricelist_margins/models/product_pricelist_item.py Outdated Show resolved Hide resolved
for item in self:
margin = percentage = 0
if not float_is_zero(
item.fixed_price, precision_digits=item.currency_id.rounding
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not use fixed_price. better user the computation of the price done by "product.pricelist". so the value is correct for all computation type.

compute="_compute_margin",
)

@api.depends("fixed_price", "product_tmpl_id", "cost")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on a lot of field in fact : compute_price, applied_on, percent_price, "base", "price_discount", price_surcharge, price_round, price_min_margin.

@chaule97 chaule97 force-pushed the 16.0-mig-product_pricelist_margins branch from a238034 to 87b6e36 Compare July 11, 2024 11:29
@chaule97
Copy link
Contributor

Hi @legalsylvain , I have updated. I hope it was fine

Copy link
Contributor

@legalsylvain legalsylvain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks ! some remaining points inline.
the rests looks great to me !

"website": "https://github.com/OCA/product-attribute",
"author": "Camptocamp, Odoo Community Association (OCA)",
"license": "AGPL-3",
"depends": ["product"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is still pending.

):
margin = percentage = 0

price_rule = item.pricelist_id._compute_price_rule(item.product_tmpl_id, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that 1 is the quantity. If yes, it should be replaced by the min quantity of the item. don't you think ?

def _compute_margin(self):
for item in self.filtered(
lambda x: x.applied_on in ("1_product", "0_product_variant")
and x.compute_price == "fixed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
and x.compute_price == "fixed"

I don't understand this line. The algorithm can work for all compute_price method. don't you think ?


for item in self.filtered(
lambda x: x.applied_on not in ("1_product", "0_product_variant")
or x.compute_price != "fixed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
or x.compute_price != "fixed"

same here.

@chaule97 chaule97 force-pushed the 16.0-mig-product_pricelist_margins branch 2 times, most recently from 8b93a6b to aa06320 Compare July 15, 2024 03:52
@chaule97
Copy link
Contributor

Hi @legalsylvain , I have updated it. please review again

Copy link

@cyrilmanuel cyrilmanuel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not an expert but LGTM

Copy link
Contributor

@santostelmo santostelmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment (optional). Otherwise LG

@chaule97 chaule97 force-pushed the 16.0-mig-product_pricelist_margins branch from aa06320 to f2b6cdb Compare July 22, 2024 05:24
# Copyright 2024 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Product Pricelist Margins",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module name should not end w/ plural form. Please rename it to _margin.
See pt 1 https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#11modules

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @simahawk, I have updated it. Thanks for your suggestion



@tagged("post_install", "-at_install")
class TestMargin(TransactionCase):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls usa BaseCommon as base class to disable tracking

"price_min_margin",
"product_tmpl_id",
"cost",
"min_quantity",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"min_quantity",
"min_quantity",
"fixed_price",

<field name="inherit_id" ref="product.product_pricelist_item_tree_view" />
<field name="arch" type="xml">
<field name="price" position="after">
<field name="cost" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="cost" />
<field name="cost" invisible="1"/>

Comment on lines 13 to 14
<field name="margin" optional="show" />
<field name="margin_percent" optional="show" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="margin" optional="show" />
<field name="margin_percent" optional="show" />
<field name="margin" optional="show" attrs="{'invisible': [('applied_on', 'not in', ["1_product", "0_product_variant"])]}" />
<field name="margin_percent" optional="show" attrs="{'invisible': [('applied_on', 'not in', ["1_product", "0_product_variant"])]}" />

Comment on lines 21 to 22
<field name="margin" optional="show" />
<field name="margin_percent" optional="show" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<field name="margin" optional="show" />
<field name="margin_percent" optional="show" />
<field name="margin" optional="show" attrs="{'invisible': [('applied_on', 'not in', ["1_product", "0_product_variant"])]}"/>
<field name="margin_percent" optional="show" attrs="{'invisible': [('applied_on', 'not in', ["1_product", "0_product_variant"])]}"/>

<field name="model">product.pricelist.item</field>
<field name="arch" type="xml">
<xpath expr="//group[@name='pricelist_rule_base']/div" position="after">
<group attrs="{'invisible': [('compute_price', '!=', 'fixed')]}">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<group attrs="{'invisible': [('compute_price', '!=', 'fixed')]}">
<group attrs="{'invisible': [('applied_on', 'not in', ["1_product", "0_product_variant"])]}">

@chaule97 chaule97 force-pushed the 16.0-mig-product_pricelist_margins branch from f2b6cdb to 8fc7577 Compare July 23, 2024 03:25
@chaule97
Copy link
Contributor

Hi @legalsylvain , I have updated it. please review again

Copy link
Contributor

@legalsylvain legalsylvain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution !

@simahawk simahawk changed the title [ADD] product_pricelist_margins [ADD] product_pricelist_margin Jul 25, 2024
Copy link
Contributor

@simahawk simahawk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bit of cleanup is still required 😉
Please:

  • find n replace product_pricelist_margins everywhere (eg: translations)
  • rewrite the commit message as well

@chaule97 chaule97 force-pushed the 16.0-mig-product_pricelist_margins branch from 8fc7577 to 96e33be Compare July 25, 2024 08:09
@chaule97
Copy link
Contributor

chaule97 commented Jul 25, 2024

A little bit of cleanup is still required 😉 Please:

* find n replace `product_pricelist_margins` everywhere (eg: translations)

* rewrite the commit message as well

Thanks @simahawk . I have replaced all of them

item.margin_percent = 0
continue

res = item.product_tmpl_id.taxes_id.compute_all(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
res = item.product_tmpl_id.taxes_id.compute_all(
current_company = self.env.company
res = item.product_tmpl_id.taxes_id.filtered(lambda t: t.company_id and t.company_id == current_company).compute_all(
price,
item.currency_id,
product=item.product_tmpl_id,
)

taxe_id is a M2M. In multicompany mode all taxes linked to this product (doesn't matter the company) will be taken into account to compute total_excluded. That is not correct from the point of view of the user. IMO we should take into account only the products taxes belonging to the current logged company.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated it

@chaule97 chaule97 force-pushed the 16.0-mig-product_pricelist_margins branch from 96e33be to 612e178 Compare November 5, 2024 08:23
Copy link
Contributor

@legalsylvain legalsylvain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quick review. no test.
LGTM. Thanks for applying change requests !

@chaule97 chaule97 force-pushed the 16.0-mig-product_pricelist_margins branch from 612e178 to 2dc3934 Compare December 1, 2024 01:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants