Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions sale_order_type/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,15 @@ def _compute_sale_type_id(self):

@api.depends("sale_type_id")
def _compute_invoice_payment_term_id(self):
previous = {move: move.invoice_payment_term_id for move in self}
res = super()._compute_invoice_payment_term_id()
for move in self.filtered("sale_type_id.payment_term_id"):
move.invoice_payment_term_id = move.sale_type_id.payment_term_id
for move in self:
if move.sale_type_id.payment_term_id:
move.invoice_payment_term_id = move.sale_type_id.payment_term_id
else:
previous_term = previous.get(move)
if previous_term:
move.invoice_payment_term_id = previous_term
Comment on lines +51 to +57
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For me, the code should be:

Suggested change
for move in self:
if move.sale_type_id.payment_term_id:
move.invoice_payment_term_id = move.sale_type_id.payment_term_id
else:
previous_term = previous.get(move)
if previous_term:
move.invoice_payment_term_id = previous_term
for move in self:
if not move.invoice_payment_term_id and move.sale_type_id.payment_term_id:
move.invoice_payment_term_id = move.sale_type_id.payment_term_id

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this wouldn't solve the issue that motivated the PR.

In this case, after super(), invoice_payment_term_id is already filled with the partner payment term, so your condition would not apply and the manual value would still be lost.

It would also change the current behavior of the module, because sale_order_type would stop overriding the computed payment term and would only act as a fallback when the field is empty.

return res

@api.depends("sale_type_id")
Expand Down
27 changes: 27 additions & 0 deletions sale_order_type/tests/test_sale_order_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,33 @@ def test_invoice_onchange_type(self):
self.assertEqual(invoice.invoice_payment_term_id, sale_type.payment_term_id)
self.assertEqual(invoice.journal_id, sale_type.journal_id)

def test_invoice_change_journal_keeps_manual_payment_term(self):
partner = self.env["res.partner"].create(
{
"name": "Invoice Term Test Partner",
"property_payment_term_id": self.immediate_payment.id,
}
)
manual_payment_term = self.env.ref("account.account_payment_term_30days")
sale_type_without_payment_term = self.sale_type.copy(
{"name": "Sale Type Without Payment Term", "payment_term_id": False}
)
other_journal = self.env["account.journal"].create(
{
"name": "Alternative Sales Journal",
"code": "ASJ1",
"type": "sale",
"company_id": self.env.company.id,
}
)
invoice = self.invoice_model.new()
invoice.partner_id = partner
invoice.sale_type_id = sale_type_without_payment_term
invoice.invoice_payment_term_id = manual_payment_term
invoice.journal_id = other_journal
self.assertEqual(invoice.journal_id, other_journal)
self.assertEqual(invoice.invoice_payment_term_id, manual_payment_term)

def test_invoice_change_partner(self):
invoice = self.create_invoice()
self.assertEqual(invoice.sale_type_id, self.sale_type)
Expand Down
Loading