Skip to content

Commit

Permalink
[ADD] create invoice specific pay_term and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoParadeda committed Jun 7, 2024
1 parent 87c33b9 commit 59e20f2
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 106 deletions.
2 changes: 1 addition & 1 deletion account_payment_term_manual/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"base_sparse_field",
],
"data": [
"security/account_payment_term_manual.xml",
"views/account_invoice_view.xml",
"views/account_payment_term.xml",
],
}
2 changes: 1 addition & 1 deletion account_payment_term_manual/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# from . import account_payment_term_manual_mixin
from . import account_move
from . import account_payment_term
from . import account_move_payment_term_line
from . import account_move_payment_term
95 changes: 65 additions & 30 deletions account_payment_term_manual/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,49 @@ class AccountMove(models.Model):

_inherit = "account.move"

manual_payment_term_id = fields.Many2one(
comodel_name="account.payment.term",
inverse_name="account_move_id",
string="Manual Payment Term",
copy=False,
)

manual_payment_term_line_ids = fields.One2many(
"account.move.payment.term.line",
"account_move_id",
comodel_name="account.payment.term.line",
related="manual_payment_term_id.line_ids",
string="Manual Payment Term Lines",
copy=False,
)

def _update_manual_payment_term_id(self):
"""Check if invoice term has changed and replace manual term if needed."""
if (
self.manual_payment_term_id.mapped("origin_term_id")
== self.invoice_payment_term_id
):
# Do nothing if manual term is already based on current term_id
return

self = self.with_context(skip_manual_term_onchange=True)
# Unlink and create seem to reset invoice_payment_term_id, here we store it
# in a variable to set the term_id again at the END OF THE METHOD
new_inv_term_id = self.invoice_payment_term_id

# THIS SECTION IS ORDER SENSITIVE!
new_manual_term = self.invoice_payment_term_id.copy_data()[0]
new_manual_term["origin_term_id"] = self.invoice_payment_term_id.id
new_manual_term["active"] = False
new_manual_term["note"] = ""
for line in new_manual_term.get("line_ids", []):
line[2].pop("payment_id", None)
# DO NOT MOVE THIS UNLINK
self.manual_payment_term_id.unlink()
# DO NOT MOVE THIS CREATE
manual_term_id = self.manual_payment_term_id.create(new_manual_term)
self.manual_payment_term_id = manual_term_id

# END OF THE METHOD -> ensure term_id gets set to user defined value
self.invoice_payment_term_id = new_inv_term_id

@api.onchange(
"line_ids",
"invoice_payment_term_id",
Expand All @@ -23,33 +59,32 @@ class AccountMove(models.Model):
"invoice_vendor_bill_id",
)
def _onchange_recompute_dynamic_lines(self):
term_lines = [tl.copy_data()[0] for tl in self.invoice_payment_term_id.line_ids]
new_line_ids = self.manual_payment_term_line_ids.create(term_lines)
self.manual_payment_term_line_ids = new_line_ids
# Replace manual lines if invoice term has changed
if not self.env.context.get("skip_manual_term_generation"):
self._update_manual_payment_term_id()

super(AccountMove, self)._onchange_recompute_dynamic_lines()

# if self.manual_payment_term_line_ids:
# self.manual_payment_term_line_ids = False

# if self.invoice_payment_term_id:
# manual_payment_term_lines = self.invoice_payment_term_id.compute(
# self.amount_total, self.invoice_date, self.currency_id
# )
# for line in manual_payment_term_lines:
# if line[1] > 0:
# self.manual_payment_term_line_ids += self.env[
# "account.move.payment.term"
# ].create(
# {
# "account_move_id": self.id,
# "date_maturity": line[0],
# "amount": abs(line[1]),
# }
# )

# def _recompute_payment_terms_lines(self):
# return super(
# AccountMove,
# self.with_context(manual_payment_term_line_ids=self.manual_payment_term_line_ids),
# )._recompute_payment_terms_lines()
def _recompute_payment_terms_lines(self):
return super(
AccountMove,
self.with_context(manual_payment_term_id=self.manual_payment_term_id),
)._recompute_payment_terms_lines()

@api.onchange("manual_payment_term_id")
def _onchange_manual_payment_term_id(self):
# Do nothing if new manual term was just duplicated from term_id
if self.env.context.get("skip_manual_term_onchange"):
return

# Append (edited) to manual term name if it was manually edited
if (
self.manual_payment_term_id
and not self.manual_payment_term_id.has_manual_lines
):
self.manual_payment_term_id.has_manual_lines = True
self.manual_payment_term_id.name += " (edited)"

# Recompute with context
self = self.with_context(skip_manual_term_generation=True)
self._onchange_recompute_dynamic_lines()
18 changes: 18 additions & 0 deletions account_payment_term_manual/models/account_move_payment_term.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright 2024 KMEE
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models


class AccountPaymentTerm(models.Model):
_inherit = "account.payment.term"

origin_term_id = fields.Many2one(
comodel_name="account.payment.term",
string="Payment Terms",
required=False,
)

has_manual_lines = fields.Boolean(
help="Technical field to keep track of manual lines",
)

This file was deleted.

17 changes: 8 additions & 9 deletions account_payment_term_manual/models/account_payment_term.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
class AccountPaymentTerm(models.Model):
_inherit = "account.payment.term"

# def compute(self, value, date_ref=False, currency=None):
# manual_term_lines = self.env.context.get("manual_payment_term_line_ids")
# if manual_term_lines:
# sign = -1 if value < 0 else 1
# return [
# (line.date_maturity, sign * line.amount)
# for line in manual_term_lines
# ]
# return super().compute(value, date_ref=date_ref, currency=currency)
def compute(self, value, date_ref=False, currency=None):
"""Inherit compute to use manual term id if set."""
manual_payment_term_id = self.env.context.get("manual_payment_term_id")
if manual_payment_term_id and self is not manual_payment_term_id:
return manual_payment_term_id.compute(
value, date_ref=date_ref, currency=currency
)
return super().compute(value, date_ref=date_ref, currency=currency)

This file was deleted.

46 changes: 29 additions & 17 deletions account_payment_term_manual/views/account_invoice_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,45 @@
expr="//page[@name='receivable_move_line_tab']/field[@name='financial_move_line_ids']"
position="before"
>
<field name="manual_payment_term_line_ids">
<group>
<group>
<field name="manual_payment_term_id" />
</group>
</group>
<field name="manual_payment_term_line_ids" class="oe_read_only">
<tree editable="bottom">
<field name="date_maturity" />
<field name="amount" />
<field
name="account_move_id"
invisible="1"
force_save="1"
<field name="sequence" widget="handle" />
<field name="value" string="Due Type" />
<field
name="value_amount"
attrs="{'readonly':[('value','=','balance')]}"
/>
<field name="currency_id" invisible="1" />
<field name="days" />
<field name="option" string="" />
<field name="day_of_the_month" string="Day of the month" />
</tree>
</field>
</xpath>
<xpath
expr="//page[@name='payable_move_line_tab']/field[@name='financial_move_line_ids']"
position="before"
>
<field name="manual_payment_term_line_ids">
<tree editable="bottom">
<field name="date_maturity" />
<field name="amount" />
<field
name="account_move_id"
invisible="1"
force_save="1"
<group>
<group>
<field name="manual_payment_term_id" />
</group>
</group>
<field name="manual_payment_term_line_ids" class="oe_read_only">
<tree>
<field name="sequence" widget="handle" />
<field name="value" string="Due Type" />
<field
name="value_amount"
attrs="{'readonly':[('value','=','balance')]}"
/>
<field name="currency_id" invisible="1" />
<field name="days" />
<field name="option" string="" />
<field name="day_of_the_month" string="Day of the month" />
</tree>
</field>
</xpath>
Expand Down
22 changes: 22 additions & 0 deletions account_payment_term_manual/views/account_payment_term.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<odoo>
<record model="ir.ui.view" id="account_payment_term_manual_form_view">
<field
name="name"
>account.payment.term.form (in account_payment_term_manual)</field>
<field name="model">account.payment.term</field>
<field name="inherit_id" ref="account.view_payment_term_form" />
<field name="arch" type="xml">

<xpath expr="//widget[@name='web_ribbon']" position="after">
<field name="origin_term_id" invisible="1" />
</xpath>

<xpath expr="//widget[@name='web_ribbon']" position="attributes">
<attribute name="attrs">{
'invisible': ['|', ('active', '=', True),('origin_term_id','!=',False)]
}</attribute>
</xpath>

</field>
</record>
</odoo>

0 comments on commit 59e20f2

Please sign in to comment.