Skip to content

Commit

Permalink
[REF] *commission*: Abstract even more the base module
Browse files Browse the repository at this point in the history
- Make specific top-level menu and permissions for commissions.
- Don't depend on account.
- Reorganize elements.
- Change some parts of the code to fit better ORM tools.
- Adjust permissions in children modules.
  • Loading branch information
pedrobaeza committed Nov 21, 2022
1 parent 05ec496 commit 001dbf7
Show file tree
Hide file tree
Showing 49 changed files with 1,022 additions and 6,051 deletions.
8 changes: 5 additions & 3 deletions account_commission/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ Account commissions
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/commission-15-0/commission-15-0-account_commission
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/165/15.0
:alt: Try me on Runbot
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/commission&target_branch=15.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module adds the function to calculate commissions in invoices (account moves).

It also allows to create vendor bills from settlements for external agents.

This module depends on the commission module.

**Table of contents**
Expand Down
10 changes: 7 additions & 3 deletions account_commission/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Copyright 2014-2020 Tecnativa - Pedro M. Baeza
# Copyright 2020 Tecnativa - Manuel Calero
# Copyright 2022 Quartile
# Copyright 2014-2022 Tecnativa - Pedro M. Baeza
{
"name": "Account commissions",
"version": "15.0.1.0.0",
"author": "Tecnativa," "Odoo Community Association (OCA)",
"author": "Tecnativa, Odoo Community Association (OCA)",
"category": "Sales Management",
"license": "AGPL-3",
"depends": [
Expand All @@ -13,14 +14,17 @@
"website": "https://github.com/OCA/commission",
"maintainers": ["pedrobaeza"],
"data": [
"security/ir.model.access.csv",
"security/account_commission_security.xml",
"security/ir.model.access.csv",
"data/menuitem_data.xml",
"views/account_move_views.xml",
"views/account_commission_settlement_view.xml",
"views/commission_settlement_views.xml",
"views/commission_views.xml",
"views/report_settlement_templates.xml",
"views/res_partner.xml",
"report/commission_analysis_view.xml",
"wizards/wizard_invoice.xml",
],
"installable": True,
}
9 changes: 9 additions & 0 deletions account_commission/data/menuitem_data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<menuitem
name="Commissions"
id="menu_invoicing_commission"
parent="account.menu_finance"
sequence="10"
/>
</odoo>
2 changes: 1 addition & 1 deletion account_commission/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import account_move
from . import commission
from . import settlement
from . import commission_settlement
from . import res_partner
40 changes: 32 additions & 8 deletions account_commission/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ def _compute_commission_total(self):
for line in record.line_ids:
record.commission_total += sum(x.amount for x in line.agent_ids)

def action_post(self):
"""Put settlements associated to the invoices in invoiced state."""
self.mapped("line_ids.settlement_id").write({"state": "invoiced"})
return super().action_post()

def button_cancel(self):
"""Check settled lines."""
"""Check settled lines and put settlements associated to the invoices in
exception.
"""
if any(self.mapped("invoice_line_ids.any_settled")):
raise exceptions.ValidationError(
_("You can't cancel an invoice with settled lines"),
)
self.mapped("line_ids.settlement_id").write({"state": "except_invoice"})
return super().button_cancel()

def recompute_lines_agents(self):
Expand Down Expand Up @@ -92,6 +100,12 @@ class AccountMoveLine(models.Model):
agent_ids = fields.One2many(comodel_name="account.invoice.line.agent")
any_settled = fields.Boolean(compute="_compute_any_settled")

settlement_id = fields.Many2one(
comodel_name="commission.settlement",
help="Settlement that generates this invoice line",
copy=False,
)

@api.depends("agent_ids", "agent_ids.settled")
def _compute_any_settled(self):
for record in self:
Expand All @@ -118,6 +132,16 @@ def _compute_agent_ids(self):
record.move_id.partner_id
)

def _copy_data_extend_business_fields(self, values):
"""We don't want to loose the settlement from the line when reversing the line
if it was a refund. We need to include it, but as we don't want change it
everytime, we will add the data when a context key is passed.
"""
res = super()._copy_data_extend_business_fields(values)
if self.settlement_id and self.env.context.get("include_settlement", False):
values["settlement_id"] = self.settlement_id.id
return res


class AccountInvoiceLineAgent(models.Model):
_inherit = "commission.line.mixin"
Expand All @@ -137,12 +161,9 @@ class AccountInvoiceLineAgent(models.Model):
store=True,
readonly=True,
)
agent_line = fields.Many2many(
settlement_line_ids = fields.One2many(
comodel_name="commission.settlement.line",
relation="settlement_agent_line_rel",
column1="agent_line_id",
column2="settlement_id",
copy=False,
inverse_name="invoice_agent_line_id",
)
settled = fields.Boolean(compute="_compute_settled", store=True)
company_id = fields.Many2one(
Expand Down Expand Up @@ -174,14 +195,17 @@ def _compute_amount(self):
line.amount = -line.amount

@api.depends(
"agent_line", "agent_line.settlement_id.state", "invoice_id", "invoice_id.state"
"settlement_line_ids",
"settlement_line_ids.settlement_id.state",
"invoice_id",
"invoice_id.state",
)
def _compute_settled(self):
# Count lines of not open or paid invoices as settled for not
# being included in settlements
for line in self:
line.settled = any(
x.settlement_id.state != "cancel" for x in line.agent_line
x.settlement_id.state != "cancel" for x in line.settlement_line_ids
)

@api.depends("object_id", "object_id.company_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,59 @@
# Copyright 2014-2020 Tecnativa - Pedro M. Baeza
# Copyright 2020 Tecnativa - Manuel Calero
# Copyright 2022 Quartile
# Copyright 2014-2022 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from itertools import groupby

from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.tests.common import Form

from odoo.tests import Form
from odoo.tools import groupby

class Settlement(models.Model):
_name = "commission.settlement"
_description = "Settlement"

def _default_currency(self):
return self.env.user.company_id.currency_id.id
class CommissionSettlement(models.Model):
_inherit = "commission.settlement"

name = fields.Char()
total = fields.Float(compute="_compute_total", readonly=True, store=True)
date_from = fields.Date(string="From")
date_to = fields.Date(string="To")
agent_id = fields.Many2one(
comodel_name="res.partner", domain="[('agent', '=', True)]"
)
agent_type = fields.Selection(related="agent_id.agent_type")
settlement_type = fields.Char(
readonly=True,
help="e.g. 'invoice'. A technical field to control the view presentation.",
)
line_ids = fields.One2many(
comodel_name="commission.settlement.line",
inverse_name="settlement_id",
string="Settlement lines",
readonly=True,
settlement_type = fields.Selection(
selection_add=[("sale_invoice", "Sales Invoices")],
ondelete={"sale_invoice": "set default"},
)
state = fields.Selection(
selection=[
("settled", "Settled"),
selection_add=[
("invoiced", "Invoiced"),
("cancel", "Canceled"),
("except_invoice", "Invoice exception"),
],
readonly=True,
default="settled",
ondelete={"invoiced": "set default", "except_invoice": "set default"},
)
invoice_line_ids = fields.One2many(
comodel_name="account.move.line",
inverse_name="settlement_id",
string="Generated invoice",
string="Generated invoice lines",
readonly=True,
)
# TODO: To be removed
invoice_id = fields.Many2one(
store=True,
comodel_name="account.move",
compute="_compute_invoice_id",
)
currency_id = fields.Many2one(
comodel_name="res.currency", readonly=True, default=_default_currency
)
company_id = fields.Many2one(
comodel_name="res.company",
default=lambda self: self.env.user.company_id,
required=True,
)

@api.depends("line_ids", "line_ids.settled_amount")
def _compute_total(self):
for record in self:
record.total = sum(record.mapped("line_ids.settled_amount"))
def _compute_can_edit(self):
"""Make settlements coming from invoice lines to not be editable."""
sale_invoices = self.filtered(lambda x: x.settlement_type == "sale_invoice")
sale_invoices.update({"can_edit": False})
return super(CommissionSettlement, self - sale_invoices)._compute_can_edit()

@api.depends("invoice_line_ids")
def _compute_invoice_id(self):
for record in self:
record.invoice_id = record.invoice_line_ids[:1].move_id

def action_cancel(self):
"""Check if any settlement has been invoiced."""
if any(x.state != "settled" for x in self):
raise UserError(_("Cannot cancel an invoiced settlement."))
self.write({"state": "cancel"})
return super().action_cancel()

def unlink(self):
"""Allow to delete only cancelled settlements"""
"""Allow to delete only cancelled settlements."""
if any(x.state == "invoiced" for x in self):
raise UserError(_("You can't delete invoiced settlements."))
return super().unlink()
Expand All @@ -101,11 +73,9 @@ def _get_invoice_partner(self):
return self[0].agent_id

def _prepare_invoice(self, journal, product, date=False):

move_form = Form(
self.env["account.move"].with_context(default_move_type="in_invoice")
)

if date:
move_form.invoice_date = date
partner = self._get_invoice_partner()
Expand Down Expand Up @@ -156,10 +126,10 @@ def make_invoices(self, journal, product, date=False, grouped=False):
for grouping_key in invoice_grouping_keys
],
),
key=lambda x: [
key=lambda x: tuple(
x._fields[grouping_key].convert_to_write(x[grouping_key], x)
for grouping_key in invoice_grouping_keys
],
),
)
grouped_settlements = [
settlement_obj.union(*list(sett))
Expand All @@ -179,34 +149,27 @@ def make_invoices(self, journal, product, date=False, grouped=False):


class SettlementLine(models.Model):
_name = "commission.settlement.line"
_description = "Line of a commission settlement"
_inherit = "commission.settlement.line"

settlement_id = fields.Many2one(
"commission.settlement",
readonly=True,
ondelete="cascade",
required=True,
)

date = fields.Date()
agent_id = fields.Many2one(
comodel_name="res.partner",
readonly=True,
store=True,
)
settled_amount = fields.Monetary(readonly=True, store=True)
currency_id = fields.Many2one(
comodel_name="res.currency",
store=True,
readonly=True,
)
commission_id = fields.Many2one(
comodel_name="commission",
readonly=True,
invoice_agent_line_id = fields.Many2one(comodel_name="account.invoice.line.agent")
invoice_line_id = fields.Many2one(
comodel_name="account.move.line",
store=True,
related="invoice_agent_line_id.object_id",
string="Source invoice line",
)
company_id = fields.Many2one(
comodel_name="res.company",
related="settlement_id.company_id",
)

@api.depends("invoice_agent_line_id")
def _compute_date(self):
for record in self.filtered("invoice_agent_line_id"):
record.date = record.invoice_agent_line_id.invoice_date

@api.depends("invoice_agent_line_id")
def _compute_commission_id(self):
for record in self.filtered("invoice_agent_line_id"):
record.commission_id = record.invoice_agent_line_id.commission_id

@api.depends("invoice_agent_line_id")
def _compute_settled_amount(self):
for record in self.filtered("invoice_agent_line_id"):
record.settled_amount = record.invoice_agent_line_id.amount
31 changes: 0 additions & 31 deletions account_commission/models/settlement.py

This file was deleted.

2 changes: 2 additions & 0 deletions account_commission/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
This module adds the function to calculate commissions in invoices (account moves).

It also allows to create vendor bills from settlements for external agents.

This module depends on the commission module.
Loading

0 comments on commit 001dbf7

Please sign in to comment.