Skip to content

Commit

Permalink
[ADD] joint_buying_account : Add the wizard to generate commission in…
Browse files Browse the repository at this point in the history
…voices
  • Loading branch information
legalsylvain committed Mar 13, 2024
1 parent 912ec11 commit a20a3b6
Show file tree
Hide file tree
Showing 22 changed files with 611 additions and 30 deletions.
1 change: 1 addition & 0 deletions joint_buying_account/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from . import wizards
5 changes: 5 additions & 0 deletions joint_buying_account/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
"demo": [
"demo/account_tax.xml",
"demo/product_product.xml",
"demo/res_company.xml",
],
"data": [
"wizards/view_joint_buying_invoice_commission_wizard.xml",
"views/view_res_config_settings.xml",
],
"installable": True,
"auto_install": True,
Expand Down
15 changes: 11 additions & 4 deletions joint_buying_account/demo/account_tax.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,25 @@

<odoo>

<record id="tax_20_included" model="account.tax">
<field name="name">ELD - sale Tax 20% (included)</field>
<record id="tax_ELD_20_included" model="account.tax">
<field name="name">ELD - Sale Tax 20% (included)</field>
<field name="company_id" ref="joint_buying_base.company_ELD"/>
<field name="amount">20</field>
<field name="price_include" eval="True"/>
</record>

<record id="tax_20_excluded" model="account.tax">
<field name="name">ELD - sale Tax 20% (excluded)</field>
<record id="tax_ELD_20_excluded" model="account.tax">
<field name="name">ELD - Sale Tax 20% (excluded)</field>
<field name="company_id" ref="joint_buying_base.company_ELD"/>
<field name="amount">20</field>
<field name="price_include" eval="False"/>
</record>

<record id="tax_LOG_20_excluded" model="account.tax">
<field name="name">LOG - Sale Tax 20% (excluded)</field>
<field name="company_id" ref="joint_buying_base.company_LOG"/>
<field name="amount">20</field>
<field name="price_include" eval="False"/>
</record>

</odoo>
16 changes: 14 additions & 2 deletions joint_buying_account/demo/product_product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<field name="company_id" ref="joint_buying_base.company_ELD"/>
<field name="price">10</field>
<field name="taxes_id" eval="[
(4, ref('joint_buying_account.tax_20_included')),
(4, ref('joint_buying_account.tax_ELD_20_included')),
]"/>
</record>

Expand All @@ -20,7 +20,19 @@
<field name="company_id" ref="joint_buying_base.company_ELD"/>
<field name="price">10</field>
<field name="taxes_id" eval="[
(4, ref('joint_buying_account.tax_20_excluded')),
(4, ref('joint_buying_account.tax_ELD_20_excluded')),
]"/>
</record>

<record id="product_LOG_commission" model="product.product">
<field name="default_code">LOG-COM</field>
<field name="type">service</field>
<field name="name">Commission VAT 20% Excl</field>
<field name="is_joint_buying" eval="False"/>
<field name="company_id" ref="joint_buying_base.company_LOG"/>
<field name="price">0</field>
<field name="taxes_id" eval="[
(4, ref('joint_buying_account.tax_LOG_20_excluded')),
]"/>
</record>

Expand Down
9 changes: 9 additions & 0 deletions joint_buying_account/demo/res_company.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>

<odoo>

<record id="joint_buying_base.company_LOG" model="res.company">
<field name="joint_buying_commission_product_id" ref="product_LOG_commission"/>
</record>

</odoo>
3 changes: 3 additions & 0 deletions joint_buying_account/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from . import res_company
from . import res_config_settings
from . import product_product
from . import joint_buying_purchase_order_grouped
11 changes: 11 additions & 0 deletions joint_buying_account/models/joint_buying_purchase_order_grouped.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Copyright (C) 2024-Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class JointBuyingPurchaseOrderGrouped(models.Model):
_inherit = "joint.buying.purchase.order.grouped"

invoice_line_id = fields.Many2one(comodel_name="account.invoice.line")
14 changes: 14 additions & 0 deletions joint_buying_account/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright (C) 2024-Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResCompany(models.Model):
_inherit = "res.company"

joint_buying_commission_product_id = fields.Many2one(
comodel_name="product.product",
name="Joint Buying Commission Product",
)
16 changes: 16 additions & 0 deletions joint_buying_account/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2024 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models


class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

joint_buying_commission_product_id = fields.Many2one(
comodel_name="product.product",
name="Joint Buying Commission Product",
related="company_id.joint_buying_commission_product_id",
readonly=False,
)
2 changes: 2 additions & 0 deletions joint_buying_account/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Glue module between 'Joint buying' modules and ``account`` module.

* Allow to create invoicing for joint buying suppliers.

* Compute correctly the price of the global product (based on the local database)
for product that have B2C taxes. (tax included.)
3 changes: 2 additions & 1 deletion joint_buying_account/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_module
from . import test_commission
from . import test_global_product
33 changes: 33 additions & 0 deletions joint_buying_account/tests/test_commission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (C) 2022 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from datetime import timedelta

from odoo.tests.common import TransactionCase


class TestCommission(TransactionCase):
def setUp(self):
super().setUp()
self.company_LOG = self.env.ref("joint_buying_base.company_LOG")
self.benoit_ronzon = self.env.ref("joint_buying_base.supplier_benoit_ronzon")
self.CommissionWizard = self.env[
"joint.buying.invoice.commission.wizard"
].with_context(active_ids=self.benoit_ronzon.ids)
self.grouped_orders = self.benoit_ronzon.joint_buying_grouped_order_ids
self.env.user.company_id = self.company_LOG

def test_01_create_commission(self):
# Create a wizard in a date before the grouped orders
day_before_deposit = min(
self.grouped_orders.mapped("deposit_date")
) + timedelta(days=-1)
wizard = self.CommissionWizard.create({"max_deposit_date": day_before_deposit})
self.assertEqual(len(wizard.line_ids), 1)
self.assertEqual(wizard.line_ids[0].grouped_order_qty, 0)

day_after_deposit = max(self.grouped_orders.mapped("deposit_date"))
wizard = self.CommissionWizard.create({"max_deposit_date": day_after_deposit})
self.assertEqual(len(wizard.line_ids), 1)
self.assertEqual(wizard.line_ids[0].grouped_order_qty, len(self.grouped_orders))
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,3 @@ def test_12_create_global_product_with_pricelist_vat_incl(self):
"Global Product (from product vat incl) should has price vat incl."
" (regression regarding pricelist feature)",
)

# def test_02_create_global_product_with_pricelist(self):
# self.company_ELD.joint_buying_pricelist_id = self.pricelist_ELD

# # Test product vat incl
# global_product_vat_incl = self.product_vat_incl.create_joint_buying_product()

# self.assertEqual(
# global_product_vat_incl.lst_price,
# self.product_vat_incl.lst_price / 1.2 * 0.9,
# "Global Product (from product vat incl) should has price vat excl."
# " (regression regarding pricelist feature)",
# )

# # Test product vat incl
# global_product_vat_excl = self.product_vat_incl.create_joint_buying_product()

# self.assertEqual(
# global_product_vat_excl.lst_price,
# self.product_vat_excl.lst_price * 0.9,
# "Global Product (from product vat excl) should has price vat excl."
# " (regression regarding pricelist feature)",
# )
26 changes: 26 additions & 0 deletions joint_buying_account/views/view_res_config_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2021 - Today: GRAP (http://www.grap.coop)
@author: Sylvain LE GAL (https://twitter.com/legalsylvain)
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>

<record id="view_res_config_settings_form" model="ir.ui.view">
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="joint_buying_base.view_res_config_settings_form" />
<field name="arch" type="xml">
<xpath expr="//field[@name='joint_buying_auto_subscribe']/../.." position="after">
<div class="col-xs-12 col-md-6 o_setting_box">
<div class="o_setting_left_pane"/>
<div class="o_setting_right_pane">
<label for="joint_buying_commission_product_id"/>
<div class="text-muted">Set the product used to make commission invoices.</div>
<field name="joint_buying_commission_product_id"/>
</div>
</div>
</xpath>
</field>
</record>

</odoo>
2 changes: 2 additions & 0 deletions joint_buying_account/wizards/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import joint_buying_invoice_commission_wizard
from . import joint_buying_invoice_commission_wizard_line
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from datetime import timedelta

from odoo import _, api, fields, models
from odoo.exceptions import Warning as UserError


class JointBuyingInvoiceCommissionWizard(models.TransientModel):
_name = "joint.buying.invoice.commission.wizard"
_description = "Joint Buying Invoice Commission Wizard"

# Columns Section
max_deposit_date = fields.Date(
string="Max Deposit Date",
required=True,
default=lambda x: x._default_max_deposit_date(),
help="The commission will be computed for the grouped order"
" deposited by the suppliers until this date included.",
)

line_ids = fields.One2many(
comodel_name="joint.buying.invoice.commission.wizard.line",
inverse_name="wizard_id",
default=lambda x: x._default_line_ids(),
)

# Default values Section
def _default_line_ids(self):
res = []
ResPartner = self.env["res.partner"]
WizardLine = self.env["joint.buying.invoice.commission.wizard.line"]
partners = ResPartner.browse(self.env.context.get("active_ids", []))
for partner in partners:
local_partner = partner.get_joint_buying_local_partner_id()
line_vals = {
"partner_id": partner.id,
"local_partner_id": local_partner and local_partner.id,
"commission_rate": partner.joint_buying_commission_rate,
"grouped_order_qty": len(
WizardLine._compute_grouped_order_ids_model(
self._default_max_deposit_date(), partner
)
),
}
res.append([0, 0, line_vals])
return res

def _default_max_deposit_date(self):
today = fields.date.today()
return fields.date(today.year, today.month, 1) - timedelta(days=1)

# Action Section
@api.multi
def invoice_commission(self):
self.ensure_one()
self._check_values()
invoices = self.env["account.invoice"]

Check warning on line 59 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L57-L59

Added lines #L57 - L59 were not covered by tests

for wizard_line in self.line_ids.filtered(lambda x: x.grouped_order_qty):
invoice = wizard_line._create_invoice()
invoices |= invoice

Check warning on line 63 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L62-L63

Added lines #L62 - L63 were not covered by tests

if not invoices:
raise UserError(

Check warning on line 66 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L66

Added line #L66 was not covered by tests
_(
"No Grouped Order to invoice for the"
" selected suppliers and the selected date."
)
)

# Recompute Taxes
invoices.compute_taxes()

Check warning on line 74 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L74

Added line #L74 was not covered by tests

action = self.env.ref("account.action_invoice_tree1").read()[0]

Check warning on line 76 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L76

Added line #L76 was not covered by tests

if len(invoices) > 1:
action["domain"] = (

Check warning on line 79 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L79

Added line #L79 was not covered by tests
"[('id', 'in', [" + ",".join(map(str, invoices.ids)) + "])]"
)
else:
form_view = [(self.env.ref("account.invoice_form").id, "form")]

Check warning on line 83 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L83

Added line #L83 was not covered by tests
action["views"] = form_view + [
(state, view)
for state, view in action.get("views", [])
if view != "form"
]
action["res_id"] = invoices.ids[0]

Check warning on line 89 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L89

Added line #L89 was not covered by tests

return action

Check warning on line 91 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L91

Added line #L91 was not covered by tests

def _check_values(self):
self.mapped("line_ids")._check_values()

Check warning on line 94 in joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py

View check run for this annotation

Codecov / codecov/patch

joint_buying_account/wizards/joint_buying_invoice_commission_wizard.py#L94

Added line #L94 was not covered by tests
Loading

0 comments on commit a20a3b6

Please sign in to comment.