Skip to content

Commit

Permalink
[ADD] osi_account_commission
Browse files Browse the repository at this point in the history
  • Loading branch information
RLeeOSI committed Jan 23, 2024
1 parent 4a295ba commit f37cf49
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 0 deletions.
1 change: 1 addition & 0 deletions osi_account_commission/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Account Commission
4 changes: 4 additions & 0 deletions osi_account_commission/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (C) 2022 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models
23 changes: 23 additions & 0 deletions osi_account_commission/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (C) 2022 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "OSI Account Commision",
"version": "16.0.1.0.0",
"author": "Open Source Integrators",
"summary": "OSI Account Commision",
"website": "http://www.opensourceintegrators.com",
"maintainers": "Open Source Integrators",
"license": "AGPL-3",
"depends": [
"commission",
"account_commission",
],
"data": [
"views/product_template_view.xml",
"views/commission_view.xml",
"views/commission_settlement_views.xml"
],
"category": "Accounting",
"installable": True,
}
Binary file added osi_account_commission/description/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions osi_account_commission/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (C) 2022 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import product_template
from . import commission
from . import commission_mixin
from . import commission_settlement
from . import account_move
50 changes: 50 additions & 0 deletions osi_account_commission/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (C) 2022 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class AccountMove(models.Model):
_inherit = "account.move"

@api.depends("line_ids.product_id.commission_id", "line_ids.agent_ids.amount")
def _compute_commission_total(self):
for record in self:
record.commission_total = 0.0
for line in record.line_ids:
commission = line.product_id.commission_id
if commission:
if commission.commission_type == 'amount':
record.commission_total += commission.amount
elif commission.commission_type == 'fixed':
record.commission_total += (
(line.price_unit * line.quantity) - line.discount) * (commission.fix_qty / 100.0)
elif commission.commission_type == 'section':
record.commission_total += commission.calculate_section(line.price_subtotal)
else:
record.commission_total += sum(x.amount for x in line.agent_ids)


class AccountInvoiceLineAgent(models.Model):
_inherit = "account.invoice.line.agent"

@api.depends(
"object_id.price_subtotal",
"object_id.commission_free",
"commission_id",
)
def _compute_amount(self):
for line in self:
inv_line = line.object_id
commission_id = inv_line.product_id.commission_id if inv_line.product_id.commission_id else line.commission_id
line.amount = line._get_commission_amount(
commission_id,
inv_line.price_subtotal,
inv_line.product_id,
inv_line.quantity,
price_unit=inv_line.price_unit,
discount=inv_line.discount
)
# Refunds commisslineions are negative
if line.invoice_id.move_type and "refund" in line.invoice_id.move_type:
line.amount = -line.amount
10 changes: 10 additions & 0 deletions osi_account_commission/models/commission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (C) 2022 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models

class Commission(models.Model):
_inherit = "commission"

commission_type = fields.Selection(selection_add=[('amount', 'Amount')], ondelete={'amount': 'cascade'})
amount = fields.Float()
32 changes: 32 additions & 0 deletions osi_account_commission/models/commission_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (C) 2022 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class CommissionLineMixin(models.AbstractModel):
_inherit = "commission.line.mixin"

def _get_commission_amount(self, commission, subtotal, product, quantity, price_unit=0, discount=0):
"""Get the commission amount for the data given. It's called by
compute methods of children models.
This means the inheritable method for modifying the amount of the commission.
"""
self.ensure_one()
if product.commission_free or not commission:
return 0.0
if commission.amount_base_type == "net_amount":
# If subtotal (sale_price * quantity) is less than
# standard_price * quantity, it means that we are selling at
# lower price than we bought, so set amount_base to 0
subtotal = max([0, subtotal - product.standard_price * quantity])
if commission.commission_type == "fixed":
if price_unit:
return ((price_unit * quantity) - discount) * (commission.fix_qty / 100.0)
else:
return subtotal * (commission.fix_qty / 100.0)
elif commission.commission_type == "section":
return commission.calculate_section(subtotal)
elif commission.commission_type == "amount":
return commission.amount
36 changes: 36 additions & 0 deletions osi_account_commission/models/commission_settlement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2023 Open Source Integrators Inc.
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import _, fields, models
from odoo.exceptions import UserError


class CommissionSettlement(models.Model):
_inherit = "commission.settlement"

settlement_type = fields.Selection(
selection_add=[("sale_invoice", "Sales Invoices")],
ondelete={"sale_invoice": "set default"},
)
state = fields.Selection(
selection_add=[
("paid", "Paid"),
],
ondelete={"paid": "set default"},
)

def action_paid(self):
"""Mark selected settlements as paid."""
settlements = self.filtered(lambda x: x.state in ("settled","invoiced"))
if settlements:
settlements.write({"state": "paid"})

def unlink(self):
"""Allow to delete only cancelled settlements."""
if any(x.state in ("invoiced", "paid") for x in self):
raise UserError(_("You can't delete invoiced or paid settlements."))
return super().unlink()

def action_invoice(self):
raise UserError(_("Payments happen through Payroll. You can't invoice here."))

10 changes: 10 additions & 0 deletions osi_account_commission/models/product_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (C) 2022 Open Source Integrators
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ProductTemplate(models.Model):
_inherit = "product.template"

commission_id = fields.Many2one('commission')
43 changes: 43 additions & 0 deletions osi_account_commission/views/commission_settlement_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record model="ir.ui.view" id="view_settlement_tree">
<field name="name">Settlements tree - Paid decoration</field>
<field name="model">commission.settlement</field>
<field name="inherit_id" ref="commission.view_settlement_tree" />
<field name="arch" type="xml">
<tree position="attributes">
<attribute name="decoration-muted">state == 'paid'</attribute>
</tree>
</field>
</record>
<record model="ir.ui.view" id="view_settlement_form">
<field name="name">Settlements - Add paid stuff</field>
<field name="model">commission.settlement</field>
<field name="inherit_id" ref="commission.view_settlement_form" />
<field name="arch" type="xml">
<button name="action_invoice" position="attributes">
<attribute name="attrs">{'invisible': True}</attribute>
</button>
<button name="action_cancel" position="before">
<button
string="Mark Paid"
attrs="{'invisible': ['|', ('state', 'not in', ['settled','invoiced']), ('agent_type', '!=', 'agent')]}"
class="oe_highlight"
type="object"
name="action_paid"
groups="account.group_account_invoice"
/>
</button>
</field>
</record>
<record model="ir.actions.server" id="action_mark_paid">
<field name="name">Mark Settlements Paid</field>
<field name="model_id" ref="osi_account_commission.model_commission_settlement"/>
<field name="binding_model_id" ref="osi_account_commission.model_commission_settlement"/>
<field name="state">code</field>
<field name="code">
action = records.action_paid()
</field>
</record>
</odoo>

20 changes: 20 additions & 0 deletions osi_account_commission/views/commission_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="commission_form_inh_comm" model="ir.ui.view">
<field name="name">commissions form inh</field>
<field name="model">commission</field>
<field name="inherit_id" ref="commission.commission_form" />
<field name="arch" type="xml">
<field name="section_ids" position="after">
<group colspan="2">
<field
name="amount"
attrs="{'invisible': [('commission_type', '!=', 'amount')]}"
/>
</group>
</field>
</field>
</record>

</odoo>
15 changes: 15 additions & 0 deletions osi_account_commission/views/product_template_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="product_template_form_view_inh_comm" model="ir.ui.view">
<field name="name">product.template.common.form.inh.comm</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="product.product_template_form_view" />
<field name="arch" type="xml">
<field name="product_tag_ids" position="after">
<field name="commission_id"/>
</field>
</field>
</record>

</odoo>

0 comments on commit f37cf49

Please sign in to comment.