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] [IMP] sale_commission: possibility to add payment date limit on settle commissions #441

Merged

Conversation

odooNextev
Copy link

@odooNextev odooNextev commented Jul 28, 2023

This module add a field on wizard that allow to restring commission settle base on payment date. All invoice that are paid after that date will be removed from settle

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!

Choose a reason for hiding this comment

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

You can generate the readme using oca-gen-addon-readme

https://github.com/OCA/maintainer-tools#readme-generator

@@ -0,0 +1,13 @@
{
"name": "Commission Settlement base on payment date",

Choose a reason for hiding this comment

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

Suggested change
"name": "Commission Settlement base on payment date",
"name": "Commission Settlement based on payment date",

Choose a reason for hiding this comment

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

This also need to be generated again (oca-gen-addon-readme will take care of this as well)

Comment on lines 19 to 22
for line in agent_lines.filtered(
lambda r: r.commission_id.invoice_state == "paid"
and self.date_payment_to != "false"
):
Copy link

@aleuffre aleuffre Aug 30, 2023

Choose a reason for hiding this comment

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

date_payment_to will always be different from the string "false" since it can only be a Date object or the boolean False

In general, I don't think this particular check should go in the lambda since it's not meant to filter agent_lines but it's meant to check if the filtering should happen at all. Additionally I'd group the other checks together since they're not logically separate.

if self.date_payment_to:
    for line in agent_lines.filtered(lambda r: r.commission_id.invoice_state == "paid" and r.invoice_id.invoice_payments_widget and not self.check_payment_date(r)):

or

if self.date_payment_to:
    for line in agent_lines:
        if line.commission_id.invoice_state == "paid" and line.invoice_id.invoice_payments_widget and not self.check_payment_date(line):

(and then let Black format that of course :) )

Copy link
Author

Choose a reason for hiding this comment

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

Here condition on "false" it's my mistake. I will change it and re-publish all changes

and self.date_payment_to != "false"
):
if (
line.invoice_id.invoice_payments_widget != "false"

Choose a reason for hiding this comment

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

Same as above. It's a text field. It'll probably be empty ("" An empty string) or False, but never the string "false". Might as well just do an if line.invoice_id.invoice_payments_widget

Copy link
Author

Choose a reason for hiding this comment

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

@aleuffre Thanks for your review. I know that sound weird, but invoice_payments_widget it is possible to be a string "false".

Choose a reason for hiding this comment

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

ok, that's interesting haha

Copy link
Author

Choose a reason for hiding this comment

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

It happens when an invoice is not paid yet. I could change condition on invoice.state without checking invoice_payments_widget. What could you recommend me?

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.

Code looks good, not sure about the test

Comment on lines 136 to 162
def test_commission_single_invoice(self):
sale_order = self._create_sale_order(
self.agent_monthly, self.commission_net_paid
)
sale_order.action_confirm()
self._invoice_sale_order(sale_order)
date = fields.Date.today()
sale_order.invoice_ids.write(
{
"invoice_date": date + relativedelta(months=-1),
"date": date + relativedelta(months=-1),
}
)
sale_order.invoice_ids.action_post()
sale_order = self._create_sale_order(
self.agent_monthly, self.commission_net_paid
)
sale_order.action_confirm()
inv = self._invoice_sale_order(sale_order)
self.register_payment(inv)
self._settle_agent(self.agent_monthly, 1)
settlements = self.env["sale.commission.settlement"].search(
[
(
"agent_id",
"=",
self.agent_monthly.id,
),
("state", "=", "settled"),
]
)
self.assertEqual(1, len(settlements))
Copy link

Choose a reason for hiding this comment

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

I'm not entirely sure, so please correct me if I'm wrong, but it seems to me that this test only creates a single payment and checks that it's included in the wizard.

You could improve the test by creating two payments (or two sales orders with one payment each, if it's easier), one in the past (as the one you've already created) and one in the future, and making sure that when you call the wizard, only the one before date_payment_to is actually included in the wizard and the other is not.

As it is, if I understand correctly, your module could do nothing and this test would still pass.

@odooNextev odooNextev force-pushed the 14-0-add-sale_commission_payment_date branch 5 times, most recently from b08724a to f0a7af4 Compare September 8, 2023 14:03
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.

Code review, LGTM

@aleuffre
Copy link

aleuffre commented Sep 8, 2023

Only thing, try to make the first line of the commit message shorter! It should be less than 80 characters in total.

You can add more details in other lines.

https://github.com/OCA/odoo-community.org/blob/master/website/Contribution/CONTRIBUTING.rst#id48

@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). 🤖

@francesco-ooops
Copy link
Contributor

@pedrobaeza what do you think?

@odooNextev odooNextev force-pushed the 14-0-add-sale_commission_payment_date branch from f0a7af4 to 9943923 Compare September 8, 2023 15:22
@francesco-ooops
Copy link
Contributor

@pedrobaeza good for merge?

@pedrobaeza pedrobaeza added this to the 14.0 milestone Sep 13, 2023
@pedrobaeza
Copy link
Member

In this case, I consider reasonable to add this to the base module, but if you want to keep it as separate module, no problem.

@francesco-ooops
Copy link
Contributor

We'll gladly turn this PR into an IMP to sale_commission :)

@odooNextev can you proceed?

@pedrobaeza
Copy link
Member

Try to make it in one shot excluding from the initial domain instead of removing them later.

@odooNextev odooNextev changed the title [14 0][Add]Sale_commission_payment_date: possibility to add payment date limit on settle commissions [14 0][Imp]Sale_commission: possibility to add payment date limit on settle commissions Sep 15, 2023
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.

Generally code review LGTM.

There's some minor code cleanup that could be done (in one file, relativedelta is imported twice, with one import overwriting the other; also it's slightly confusing to be using both timedelta and relativedelta, especially when the latter is much nicer to use) but it doesn't really matter.

I'd echo the request by Pedro Baeza of trying to move as much of the search as possible to the domain in the .search method so it's done in the DB directly rather than in python, but I don't know how to do get the payment date info so I can't give advice there without looking into it in more detail.

@aleuffre
Copy link

Oh, and as always, make sure to squash into a single commit, and follow OCA guidelines for the commit message

@odooNextev odooNextev force-pushed the 14-0-add-sale_commission_payment_date branch from baa7a34 to e110826 Compare September 15, 2023 08:55
@odooNextev
Copy link
Author

Hi @aleuffre, thanks for your review. I made some cleanup that you recommended to me. I tried to move everything inside a single domain but it seems, in my opinion, not possible.

Comment on lines 146 to 153
if self.date_payment_to:
aila = aila.filtered(
lambda line: line.commission_id.invoice_state != "paid"
or line.invoice_id.invoice_payments_widget == "false"
or self.check_payment_date(line)
)

Choose a reason for hiding this comment

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

If you want to limit lines to a certain payment date, aren't you with this including all lines that aren't paid as well?

Copy link
Author

Choose a reason for hiding this comment

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

I changed it and now I think that's works properly. Is it possible to call check_payment_date method inside domain?

Choose a reason for hiding this comment

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

unfortunately not. Domains inside a .search are translated into SQL and passed to the DB directly. It's also why you can't use fields that are computed and not stored, since they don't exist as columns in the DB.

@odooNextev odooNextev force-pushed the 14-0-add-sale_commission_payment_date branch 3 times, most recently from 05651ca to bc3cc6a Compare September 15, 2023 09:36
@odooNextev odooNextev force-pushed the 14-0-add-sale_commission_payment_date branch from bc3cc6a to a83f29b Compare September 15, 2023 09:40
Comment on lines 146 to 165
if self.date_payment_to:
aila = aila.filtered(
lambda line: not (
line.commission_id.invoice_state == "paid"
and line.invoice_id.invoice_payments_widget != "false"
and not self.check_payment_date(line)
)
)
return aila

def check_payment_date(self, agent_line):
move_dict_content = json.loads(agent_line.invoice_id.invoice_payments_widget)[
"content"
]
return all(
[
self.date_payment_to > datetime.strptime(x["date"], "%Y-%m-%d").date()
for x in move_dict_content
]
)
Copy link

@aleuffre aleuffre Sep 18, 2023

Choose a reason for hiding this comment

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

Done some digging into this in order to get around using the widget.

As far as I can tell there is no simple field that tells you when an invoice is fully paid. But you can get all the payments associated with an invoice (self.env["account.payment"].search("reconciled_invoice_ids", "in", invoice.id) and then check the date on those.

I also think that this type of check shouldn't be done here but higher up in this file (lines 103 and following).

There's also a function _skip_settlement that does part of the filtering you already want. So perhaps another if condition: continue up there, right after _skip_settlement would make the most sense.

Copy link

@aleuffre aleuffre Sep 18, 2023

Choose a reason for hiding this comment

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

Also, I'd like to point out that according to the existing logic of _skip_settlement, it seems to me (please correct me if I'm wrong) that as long as the invoice is partially paid, the commission settlement is created in full (for commissions of type "Payment Based") so I think we should respect that for now and maybe change the logic in a future PR

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.

Looks clean, finally! :D

Just squash into a single commit following OCA conventions please.

@odooNextev odooNextev changed the title [14 0][Imp]Sale_commission: possibility to add payment date limit on settle commissions [14 0] [IMP] Sale_commission: possibility to add payment date limit on settle commissions Sep 19, 2023
@odooNextev odooNextev changed the title [14 0] [IMP] Sale_commission: possibility to add payment date limit on settle commissions [14 0] [IMP] sale_commission: possibility to add payment date limit on settle commissions Sep 19, 2023
@odooNextev odooNextev force-pushed the 14-0-add-sale_commission_payment_date branch from 1a6f3a4 to 217c77b Compare September 19, 2023 14:56
@francesco-ooops
Copy link
Contributor

Try to make it in one shot excluding from the initial domain instead of removing them later.

@pedrobaeza could it work like this?

@@ -12,6 +12,8 @@ class SaleCommissionMakeSettle(models.TransientModel):
_description = "Wizard for settling commissions in invoices"

date_to = fields.Date("Up to", required=True, default=fields.Date.today())
date_payment_to = fields.Date("Date Payment Up to", default=fields.Date.today())
Copy link
Member

Choose a reason for hiding this comment

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

The name of the field and the label is not very intuitive. Also non having help doesn't help, hehe. Can you please look for other name and put a help for this new field.

Copy link
Contributor

Choose a reason for hiding this comment

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

@pedrobaeza since this is becoming a feature of base module, I would suggest changing labels of both date fields in the wizard for consistency:

Invoice date up to
Payment date up to

shall we proceed?

Copy link
Member

Choose a reason for hiding this comment

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

OK

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

@odooNextev odooNextev force-pushed the 14-0-add-sale_commission_payment_date branch from 217c77b to ad8f775 Compare September 20, 2023 10:22
@odooNextev odooNextev force-pushed the 14-0-add-sale_commission_payment_date branch from ad8f775 to 9ac7706 Compare September 20, 2023 10:30
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.

Thanks for the changes

/ocabot merge minor

@OCA-git-bot
Copy link
Contributor

This PR looks fantastic, let's merge it!
Prepared branch 14.0-ocabot-merge-pr-441-by-pedrobaeza-bump-minor, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit 16c87ad into OCA:14.0 Sep 20, 2023
6 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at 44cd035. 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