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

[14.0][ADD]sale_order_general_discount_payment_term: Add sale order general discount on payment term #2660

Conversation

odooNextev
Copy link

This modulo allow to add sale_order general discount based on payment term discount field.
Also hide discount from partner to avoid conflicts

Copy link
Contributor

@francesco-ooops francesco-ooops left a comment

Choose a reason for hiding this comment

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

Functional ok!

Copy link

@aleuffre aleuffre left a comment

Choose a reason for hiding this comment

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

You hide the original field, but for existing installations the value of sale_discount on res.partners will still exist and it will keep being applied to sale.orders, with no way for the user to change it.

There are a few options to deactivate it entirely, One of them could be a post_init_hook that just writes 0 on every partner's sale_discount field. I don't know if people would find it too aggressive, though.

Another option is to override onchange_partner_id, save the existing discount, call super() and then restore the discount that existed before the onchange was called, so as to nullify the effect of the change in sale_order_general_discount (every line's discount would be recalculated twice for nothing, but alas, I've seen worse in Odoo :D ).

Alternatively, since sale_order_general_discount is so small and you want to deactivate half of it, you could just replicate its functionality and not depend on it. (and maybe add it to the excludes key in the manifest so people don't accidentally install both).

I think option number 2 (overriding onchange_partner_id is by far the most preferable, though)

Comment on lines 7 to 9
sale_discount = fields.Float(
store=True,
readonly=False,
)
Copy link

@aleuffre aleuffre Aug 31, 2023

Choose a reason for hiding this comment

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

No point restating obvious defaults IMO. (This is a new field, not an override, correct?)

Instead, I think it'd be good to add the same modifiers that the corresponding field has in sale_order_general_discount

    sale_discount = fields.Float(
        digits=dp.get_precision('Discount'),
        string='Discount (%)',
        company_dependent=True,
        track_visibility="onchange",
    )

Comment on lines 7 to 18
<xpath
expr="//page[@name='sales_purchases']//label[@for='sale_discount']"
position="attributes"
>
<attribute name="invisible">1</attribute>
</xpath>
<xpath
expr="//page[@name='sales_purchases']//div[field[@name='sale_discount']]"
position="attributes"
>
<attribute name="invisible">1</attribute>
</xpath>

Choose a reason for hiding this comment

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

Suggested change
<xpath
expr="//page[@name='sales_purchases']//label[@for='sale_discount']"
position="attributes"
>
<attribute name="invisible">1</attribute>
</xpath>
<xpath
expr="//page[@name='sales_purchases']//div[field[@name='sale_discount']]"
position="attributes"
>
<attribute name="invisible">1</attribute>
</xpath>
<field name="sale_discount" position="attributes">
<attribute name="invisible">1</attribute>
</field>

is this not sufficient, or does it not work for some reason?

Copy link
Author

Choose a reason for hiding this comment

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

Hi @aleuffre, thanks for your review. Set "invisible" on field sale_discount it doesn't works properly because we need to hide a div with sale_discount and a string inside.

Comment on lines 8 to 11
@api.depends("payment_term_id")
def _compute_general_discount(self):
for so in self:
so.general_discount = so.payment_term_id.sale_discount

Choose a reason for hiding this comment

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

api.depends and _compute functions are generally used if they're computing the value of a field.

I think this should be an api.onchange

Cf. the module sale_order_general_discount:

    @api.onchange('partner_id')
    def onchange_partner_id(self):
        super().onchange_partner_id()
        self.general_discount = self.partner_id.sale_discount
        return

Copy link
Author

@odooNextev odooNextev Sep 4, 2023

Choose a reason for hiding this comment

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

Hi @aleuffre, I think that is 12.0 version. In v14 this is the code in sale_order_general_discount:

@api.depends("partner_id")
def _compute_general_discount(self):
for so in self:
so.general_discount = so.partner_id.sale_discount

Copy link

Choose a reason for hiding this comment

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

Ah, my bad. If you're overriding an existing compute and it works, then that's fine by me.

@aleuffre
Copy link

aleuffre commented Sep 4, 2023

Sorry for my oversights during my first review. Trying to do too many things at once will do that to you.

Adding the field parameters to sale_discount seems to be the only thing left. After that I'll approve.

@odooNextev odooNextev force-pushed the 14.0-add-sale_order_general_discount_payment_term branch from e2a82b1 to 3a64185 Compare September 4, 2023 14:15
digits="Discount",
string="Discount (%)",
company_dependent=True,
track_visibility="onchange",
Copy link

@aleuffre aleuffre Sep 4, 2023

Choose a reason for hiding this comment

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

as you correctly spotted in the other comment, I was looking at v12 code during my previous review.

track_visibility was a v12 option, in v14 it's simply tracking=True

@aleuffre
Copy link

aleuffre commented Sep 4, 2023

Code review looks good aside from that one small thing, please squash everything into a single commit.

@odooNextev odooNextev force-pushed the 14.0-add-sale_order_general_discount_payment_term branch 2 times, most recently from e466597 to 1a04352 Compare September 4, 2023 14:41
@francesco-ooops
Copy link
Contributor

@OCA/crm-sales-marketing-maintainers good for merge?

@francesco-ooops
Copy link
Contributor

@pedrobaeza could this be merged please?

@pedrobaeza pedrobaeza added this to the 14.0 milestone Sep 11, 2023
expr="//page[@name='sales_purchases']//label[@for='sale_discount']"
position="attributes"
>
<attribute name="invisible">1</attribute>
Copy link
Member

Choose a reason for hiding this comment

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

This is not correct. This is out of scope of the module. And there's already a group for hiding this for those you don't want to see it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@pedrobaeza the point of this change is that if there is a general discount set in partner and another general discount set in payment term they will be in conflict for setting the general discount on SO.

How else do you suggest we avoid this scenario?

Copy link
Member

Choose a reason for hiding this comment

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

OK, then hide it only if the payment term is set and it contains a discount.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, the flow is unclear to me. What happens if partner 1 has a default general discount set and in SO user selects a payment term with a general discount? Which general discount should be applied?

With this module installed, it's very likely that the only general discount that should be applied is the payment term one.

Shall we make it easier with a flag in settings allowing user to decide whether to hide the general discount in partner field?

Copy link
Member

Choose a reason for hiding this comment

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

You should clear that general discount on payment term assignment IMO.

Copy link
Member

Choose a reason for hiding this comment

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

The question is that you shouldn't remove the possibility of assigning directly a general discount for all cases. Just for those covered by this module (with payment term). If not, you will force to assign a payment term always if you want a general discount.

Copy link
Contributor

Choose a reason for hiding this comment

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

User has the possibility of assigning general discount in SO in 3 ways:

  1. default from partner
  2. manually from field "discount" in SO
  3. default from payment term

base module "sale_order_general_discount" implements the first two options; this module implements the third.

I think if we add a "hide default general discount in partner" flag in settings to this module we will have the cleaner implementation as the user can decide:

  • whether to keep all 3 options,
  • to keep only 2 and 3 (flag enabled), or
  • to keep 1 and 2 by not installing this module.

Could that work?

Copy link
Member

Choose a reason for hiding this comment

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

I still think that you don't need another extra config. Just don't hide the field if you don't have a payment term selected.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright, @odooNextev can you implement that?

Copy link
Contributor

Choose a reason for hiding this comment

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

@pedrobaeza with the latest change by @odooNextev , if payment term discount = 0 and partner default discount is set, partner default is used in SO; otherwise payment term discount is used. Could this work?

@odooNextev odooNextev force-pushed the 14.0-add-sale_order_general_discount_payment_term branch 3 times, most recently from e6c0a55 to f04fe42 Compare September 15, 2023 14:14
@odooNextev odooNextev force-pushed the 14.0-add-sale_order_general_discount_payment_term branch from f04fe42 to c43a9cc Compare September 15, 2023 14:17
Copy link
Member

@pedrobaeza pedrobaeza left a comment

Choose a reason for hiding this comment

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

/ocabot merge nobump

@OCA-git-bot
Copy link
Contributor

Hey, thanks for contributing! Proceeding to merge this for you.
Prepared branch 14.0-ocabot-merge-pr-2660-by-pedrobaeza-bump-nobump, awaiting test results.

@OCA-git-bot
Copy link
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

1 similar comment
@OCA-git-bot
Copy link
Contributor

This PR has the approved label and has been created more than 5 days ago. It should therefore be ready to merge by a maintainer (or a PSC member if the concerned addon has no declared maintainer). 🤖

@OCA-git-bot OCA-git-bot merged commit f9cc5a7 into OCA:14.0 Sep 15, 2023
10 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at fca3a24. Thanks a lot for contributing to OCA. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants