Skip to content

Commit

Permalink
[MIG] account_payment_promissory_note: Migration to 15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosRoca13 committed Nov 3, 2022
1 parent d2a1bff commit dc1ac2e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
13 changes: 12 additions & 1 deletion account_payment_promissory_note/models/account_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,19 @@ def _prepare_payment_moves(self):
def _onchange_promissory_note(self):
result = super()._onchange_promissory_note()
if not self.date_due and self.promissory_note:
invoices = self.invoice_ids
invoices = self.reconciled_invoice_ids
same_partner = len(invoices.mapped("partner_id")) == 1
if invoices and same_partner:
self.date_due = max(invoices.mapped("invoice_date_due"))
return result

def write(self, vals):
for payment in self:
if "promissory_note" in vals:
if not vals["promissory_note"]:
payment.line_ids.date_maturity = vals.get("date") or payment.date
elif "date_due" in vals:
payment.line_ids.date_maturity = vals["date_due"]
elif payment.promissory_note and "date_due" in vals:
payment.line_ids.date_maturity = vals["date_due"]
return super().write(vals)
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,10 @@ def setUp(self):
}
)
self.invoice_2.action_post()
self.payment_1 = self.env["account.payment"].create(
{
"payment_type": "inbound",
"payment_method_line_id": self.payment_method.id,
"amount": 50.00,
"journal_id": self.env["account.journal"]
.search([("type", "=", "sale")], limit=1)
.id,
}
)
self.payment_2 = self.env["account.payment"].create(

def test_1_onchange_promissory_note_without_invoices(self):
payment = self.env["account.payment"].create(
{
"invoice_ids": [(4, self.invoice_1.id), (4, self.invoice_2.id)],
"payment_type": "inbound",
"payment_method_line_id": self.payment_method.id,
"amount": 50.00,
Expand All @@ -55,27 +46,34 @@ def setUp(self):
.id,
}
)

def test_1_onchange_promissory_note_without_invoices(self):
self.payment_1.date_due = "2020-09-21"
self.payment_1._onchange_promissory_note()
self.assertFalse(self.payment_1.date_due)
self.payment_1.promissory_note = True
self.payment_1.date_due = "2020-09-21"
self.payment_1._onchange_promissory_note()
payment.date_due = "2020-09-21"
payment._onchange_promissory_note()
self.assertFalse(payment.date_due)
payment.promissory_note = True
payment.date_due = "2020-09-21"
payment._onchange_promissory_note()
self.assertEqual(
self.payment_1.date_due,
payment.date_due,
datetime.datetime.strptime("2020-09-21", "%Y-%m-%d").date(),
)

def test_2_onchange_promissory_note_with_invoices(self):
self.payment_2.date_due = "2020-09-21"
self.payment_2._onchange_promissory_note()
self.assertFalse(self.payment_2.date_due)
self.payment_2.promissory_note = True
self.payment_2._onchange_promissory_note()
wiz_form = Form(
self.env["account.payment.register"].with_context(
active_model="account.move",
active_ids=[self.invoice_1.id, self.invoice_2.id],
)
)
wiz_form.group_payment = True
wiz = wiz_form.save()
payment = wiz._create_payments()
payment.date_due = "2020-09-21"
payment._onchange_promissory_note()
self.assertFalse(payment.date_due)
payment.promissory_note = True
payment._onchange_promissory_note()
self.assertEqual(
self.payment_2.date_due,
payment.date_due,
datetime.datetime.strptime("2020-09-23", "%Y-%m-%d").date(),
)

Expand All @@ -86,12 +84,10 @@ def test_3_due_date_propagation(self):
active_ids=[self.invoice_1.id, self.invoice_2.id],
)
)
wiz_form.payment_method_line_id = self.payment_method
wiz_form.promissory_note = True
wiz_form.group_payment = True
wiz_form.date_due = datetime.datetime.strptime("2020-09-23", "%Y-%m-%d").date()
wiz = wiz_form.save()
action_vals = wiz.create_payments()
payment = self.env["account.payment"].search(action_vals["domain"])
for line in payment.move_line_ids:
payment = wiz._create_payments()
for line in payment.line_ids:
self.assertEqual(line.date_maturity, payment.date_due)
22 changes: 10 additions & 12 deletions account_payment_promissory_note/wizard/account_register_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,24 @@ class AccountRegisterPayments(models.TransientModel):
_name = "account.payment.register"
_inherit = ["account.payment.register", "account.promissory.note.mixin"]

def get_payments_vals(self):
vals = super().get_payments_vals()
for val in vals:
def _create_payments(self):
payments = super()._create_payments()
for payment in payments:
if not self.date_due:
invoices = self.env["account.move"].browse(val["invoice_ids"][0][2])
invoices = payment.reconciled_invoice_ids
max_date = max(invoices.mapped("invoice_date_due"))
val.update(
{"promissory_note": self.promissory_note, "date_due": max_date}
)
payment.promissory_note = self.promissory_note
payment.date_due = max_date
else:
val.update(
{"promissory_note": self.promissory_note, "date_due": self.date_due}
)
return vals
payment.promissory_note = self.promissory_note
payment.date_due = self.date_due
return payments

@api.onchange("promissory_note")
def _onchange_promissory_note(self):
result = super()._onchange_promissory_note()
if not self.date_due and self.promissory_note:
active_ids = self._context.get("active_ids")
active_ids = self.env.context.get("active_ids")
invoices = self.env["account.move"].browse(active_ids)
same_partner = len(invoices.mapped("partner_id")) == 1
if invoices and self.group_payment and same_partner:
Expand Down

0 comments on commit dc1ac2e

Please sign in to comment.