-
-
Notifications
You must be signed in to change notification settings - Fork 344
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
[14 0] [IMP] sale_commission: possibility to add payment date limit on settle commissions #441
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functional ok!
There was a problem hiding this comment.
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
@@ -0,0 +1,13 @@ | |||
{ | |||
"name": "Commission Settlement base on payment date", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"name": "Commission Settlement base on payment date", | |
"name": "Commission Settlement based on payment date", |
There was a problem hiding this comment.
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)
for line in agent_lines.filtered( | ||
lambda r: r.commission_id.invoice_state == "paid" | ||
and self.date_payment_to != "false" | ||
): |
There was a problem hiding this comment.
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 :) )
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this 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
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)) |
There was a problem hiding this comment.
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.
b08724a
to
f0a7af4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code review, LGTM
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 |
This PR has the |
@pedrobaeza what do you think? |
f0a7af4
to
9943923
Compare
@pedrobaeza good for merge? |
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. |
We'll gladly turn this PR into an IMP to sale_commission :) @odooNextev can you proceed? |
Try to make it in one shot excluding from the initial domain instead of removing them later. |
There was a problem hiding this 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.
Oh, and as always, make sure to squash into a single commit, and follow OCA guidelines for the commit message |
baa7a34
to
e110826
Compare
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. |
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) | ||
) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
05651ca
to
bc3cc6a
Compare
bc3cc6a
to
a83f29b
Compare
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 | ||
] | ||
) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this 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.
1a6f3a4
to
217c77b
Compare
@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()) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pedrobaeza go?
217c77b
to
ad8f775
Compare
ad8f775
to
9ac7706
Compare
There was a problem hiding this 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
This PR looks fantastic, let's merge it! |
Congratulations, your PR was merged at 44cd035. Thanks a lot for contributing to OCA. ❤️ |
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