Skip to content

Commit

Permalink
[ADD] g_p_change_sale_move: Aggregate policy
Browse files Browse the repository at this point in the history
Add a new parameter on res.company to choose how move.line are
aggregated when closing a pos.session.

Two options can be selected:
1. "Group by account and tax", it means that:
    - product move.line are aggregated by account and by taxes.
    - tax move.line are aggregated by tax.
    - only one counter part move.line are written for the whole pos.session
2. "Group by account, tax and partner", it means that:
    - product move.line are aggregated by account, by taxes and by partner.
    - tax move.line are aggregated by tax and by partner.
    - counter part move.line are aggregated by partner.

This is a first attempt. Readme should be changed later when these
changes will be validated.
  • Loading branch information
remytms committed Jan 20, 2021
1 parent 6e2a284 commit ca3b3c6
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 44 deletions.
1 change: 1 addition & 0 deletions grap_pos_change_sale_move/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"depends": ["point_of_sale"],
"license": "AGPL-3",
"demo": ["demo/res_groups.xml"],
"data": ["views/res_company_views.xml"],
"installable": True,
}
1 change: 1 addition & 0 deletions grap_pos_change_sale_move/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import res_company
from . import pos_order
73 changes: 43 additions & 30 deletions grap_pos_change_sale_move/models/pos_order.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright (C) 2017 - Today: GRAP (http://www.grap.coop)
# Copyright (C) 2021 - Today: Coop IT Easy (http://coopiteasy.be)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# @author: Rémy TAYMANS ([email protected])
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, models
Expand All @@ -8,48 +10,57 @@
class PosOrder(models.Model):
_inherit = "pos.order"

def _get_group_by_keys(self):
"""Return keys used to aggregate the account moves."""
company = self[0].session_id.config_id.company_id
pos_sale_move_policy = company.pos_sale_move_policy
return {"partner": "partner" in pos_sale_move_policy}

# Overwrite this Odoo function.
# Remove partner_id product_id and name from the keys.
@api.model
def _get_account_move_line_group_data_type_key(
self, data_type, values, options=False
):
options = options or {}
groupby = self._get_group_by_keys()
if data_type == "product":
return (
"product",
# Remove partner_id
False,
# remove product_id (1) and name (3)
(False, tuple(values["tax_ids"][0][2]), False),
values["analytic_account_id"],
values["debit"] > 0,
values.get("currency_id"),
# Add new key "account_id"
values["account_id"],
)
tax_ids = values["tax_ids"][0][2]

product_key = ["product"]
if groupby["partner"]:
product_key.append(values["partner_id"])
# remove product_id (0) and name (2), keep tax (1)
product_key.append((False, tuple(tax_ids), False))
product_key.append(values["analytic_account_id"])
product_key.append(values["debit"] > 0)
product_key.append(values.get("currency_id"))
# Add new key "account_id"
product_key.append(values["account_id"])

return tuple(product_key)
elif data_type == "tax":
order_id = values.pop("order_id", False)
tax_key = (
"tax",
# remove partner_id
False,
values["tax_line_id"],
values["debit"] > 0,
values.get("currency_id"),
)

tax_key = ["tax"]
if groupby["partner"]:
tax_key.append(values["partner_id"])
tax_key.append(values["tax_line_id"])
tax_key.append(values["debit"] > 0)
if options.get("rounding_method") == "round_globally":
tax_key = ("tax", values["tax_line_id"], order_id)
return tax_key

return tuple(tax_key)
elif data_type == "counter_part":
return (
"counter_part",
# remove partner_id
False,
values["account_id"],
values["debit"] > 0,
values.get("currency_id"),
)
counter_part_key = ["counter_part"]

if groupby["partner"]:
counter_part_key.append(values["partner_id"] or 0)
counter_part_key.append(values["account_id"])
counter_part_key.append(values["debit"] > 0)
counter_part_key.append(values.get("currency_id"))

return tuple(counter_part_key)
return False

# Overload this Odoo function.
Expand All @@ -58,9 +69,11 @@ def _prepare_account_move_and_lines(self, session=None, move=None):
res = super()._prepare_account_move_and_lines(session=session, move=move)
AccountAccount = self.env["account.account"]
grouped_data = res.get("grouped_data")
groupby = self._get_group_by_keys()
for k, values in grouped_data.items():
for value in values:
value["partner_id"] = False
if not groupby["partner"]:
value["partner_id"] = False
value["product_id"] = False
value["quantity"] = False
if k[0] == "product":
Expand Down
18 changes: 18 additions & 0 deletions grap_pos_change_sale_move/models/res_company.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Copyright (C) 2021 - Today: Coop IT Easy (http://coopiteasy.be)
# @author: Rémy TAYMANS ([email protected])
# 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"

pos_sale_move_policy = fields.Selection(
selection=[
("groupby_account_tax", "Group by Account and tax"),
("groupby_account_tax_partner", "Group by account, tax and partner"),
],
default="groupby_account_tax",
help="Select how PoS account moves should be aggregated",
)
13 changes: 13 additions & 0 deletions grap_pos_change_sale_move/views/res_company_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<record id="view_company_form" model="ir.ui.view">
<field name="name">res.company.form</field>
<field name="model">res.company</field>
<field name="inherit_id" ref="base.view_company_form"/>
<field name="arch" type="xml">
<field name="currency_id" position="after">
<field name="pos_sale_move_policy"/>
</field>
</field>
</record>
</odoo>
6 changes: 4 additions & 2 deletions grap_pos_change_sale_move_test/demo/account_tax.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<record id="sale_vat_5" model="account.tax">
<field name="name">SALE VAT 5% Included (grap_pos_change_sale_move)</field>
<field name="type_tax_use">sale</field>
<field name="amount_type">percent</field>
<field name="amount">5.0</field>
<field name="price_include" eval="True"/>
<field name="price_include" eval="False"/>
<field name="company_id" ref="base.main_company" />
<field name="account_id" ref="account_vat_5" />
<field name="refund_account_id" ref="account_vat_5" />
Expand All @@ -20,8 +21,9 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<record id="sale_vat_20" model="account.tax">
<field name="name">SALE VAT 20% Included (grap_pos_change_sale_move)</field>
<field name="type_tax_use">sale</field>
<field name="amount_type">percent</field>
<field name="amount">20.0</field>
<field name="price_include" eval="True"/>
<field name="price_include" eval="False"/>
<field name="company_id" ref="base.main_company" />
<field name="account_id" ref="account_vat_20" />
<field name="refund_account_id" ref="account_vat_20" />
Expand Down
8 changes: 8 additions & 0 deletions grap_pos_change_sale_move_test/demo/product_product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
<field name="property_account_income_id" ref="account_income_701"/>
</record>

<record id="product_5_20_701" model="product.product">
<field name="name">Product VAT 20% and 5% (701)</field>
<field name="type">consu</field>
<field name="available_in_pos" eval="True"/>
<field name="taxes_id" eval="[(6, 0, [ref('sale_vat_20'), ref('sale_vat_5')])]" />
<field name="property_account_income_id" ref="account_income_701"/>
</record>

</odoo>
Loading

0 comments on commit ca3b3c6

Please sign in to comment.