diff --git a/stock_account_prepare_anglo_saxon_out_lines_hook/README.rst b/stock_account_prepare_anglo_saxon_out_lines_hook/README.rst new file mode 100644 index 000000000..df161f9bc --- /dev/null +++ b/stock_account_prepare_anglo_saxon_out_lines_hook/README.rst @@ -0,0 +1,63 @@ +.. image:: https://img.shields.io/badge/license-AGPLv3-blue.svg + :target: https://www.gnu.org/licenses/agpl.html + :alt: License: AGPL-3 + +============================= +Account Move Anglo Saxon Hook +============================= + +This module introduces a hook that allows to customize the criteria why +anglosaxon entries for COGS are created + +Standard Odoo will not create those entries for Kits where those are +consumables. But they should be considered because of the interim output +account is hit in the invoice. + +References: https://github.com/odoo/odoo/issues/60679 +Proposal to Odoo: https://github.com/odoo/odoo/pull/68511 + +It also introduces a hook in order to add/modify the values on the +anglo saxon journal entries creation. For example, the sales order information + + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/92/11.0 + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Aaron Henriquez + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/stock_account_prepare_anglo_saxon_out_lines_hook/__init__.py b/stock_account_prepare_anglo_saxon_out_lines_hook/__init__.py new file mode 100644 index 000000000..e3bed0638 --- /dev/null +++ b/stock_account_prepare_anglo_saxon_out_lines_hook/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import models +from .hooks import post_load_hook diff --git a/stock_account_prepare_anglo_saxon_out_lines_hook/__manifest__.py b/stock_account_prepare_anglo_saxon_out_lines_hook/__manifest__.py new file mode 100644 index 000000000..21be30198 --- /dev/null +++ b/stock_account_prepare_anglo_saxon_out_lines_hook/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2020 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +{ + "name": "Stock Account Prepare Anglo Saxon Oout Lines Hook", + "summary": "Modify when and how anglo saxon journal items are created", + "version": "13.0.1.0.0", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "website": "http://www.github.com/OCA/account-financial-tools", + "category": "Generic", + "depends": ["stock_account"], + "post_load": "post_load_hook", + "license": "AGPL-3", + "installable": True, +} diff --git a/stock_account_prepare_anglo_saxon_out_lines_hook/hooks.py b/stock_account_prepare_anglo_saxon_out_lines_hook/hooks.py new file mode 100644 index 000000000..78fab6342 --- /dev/null +++ b/stock_account_prepare_anglo_saxon_out_lines_hook/hooks.py @@ -0,0 +1,67 @@ +# Copyright 2020 ForgeFlow, S.L. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html). + +from odoo.addons.stock_account.models.account_move import AccountMove + + +def post_load_hook(): + def new_stock_account_prepare_anglo_saxon_out_lines_vals(self): + lines_vals_list = [] + for move in self: + if ( + not move.is_sale_document(include_receipts=True) + or not move.company_id.anglo_saxon_accounting + ): + continue + + for line in move.invoice_line_ids: + + # Filter out lines being not eligible for COGS. + # FIRST HOOK STARTS + if not line._eligible_for_cogs(): + continue + # FIRST HOOK ENDS + + # Retrieve accounts needed to generate the COGS. + accounts = line.product_id.product_tmpl_id.with_context( + force_company=line.company_id.id + ).get_product_accounts(fiscal_pos=move.fiscal_position_id) + debit_interim_account = accounts["stock_output"] + credit_expense_account = accounts["expense"] + if not credit_expense_account: + if self.type == "out_refund": + credit_expense_account = ( + self.journal_id.default_credit_account_id + ) + else: # out_invoice/out_receipt + credit_expense_account = ( + self.journal_id.default_debit_account_id + ) + if not debit_interim_account or not credit_expense_account: + continue + + # Add interim account line. + # SECOND HOOK STARTS + if not debit_interim_account or not credit_expense_account: + continue + interim_account_line_vals = self._prepare_interim_account_line_vals( + line, move, debit_interim_account + ) + lines_vals_list.append(interim_account_line_vals) + expense_account_line_vals = self._prepare_expense_account_line_vals( + line, move, credit_expense_account + ) + lines_vals_list.append(expense_account_line_vals) + # SECOND HOOK ENDS + return lines_vals_list + + if not hasattr( + AccountMove, "stock_account_prepare_anglo_saxon_out_lines_vals_original" + ): + AccountMove.stock_account_prepare_anglo_saxon_out_lines_vals_original = ( + AccountMove._stock_account_prepare_anglo_saxon_out_lines_vals + ) + + AccountMove._stock_account_prepare_anglo_saxon_out_lines_vals = ( + new_stock_account_prepare_anglo_saxon_out_lines_vals + ) diff --git a/stock_account_prepare_anglo_saxon_out_lines_hook/models/__init__.py b/stock_account_prepare_anglo_saxon_out_lines_hook/models/__init__.py new file mode 100644 index 000000000..17c8e8864 --- /dev/null +++ b/stock_account_prepare_anglo_saxon_out_lines_hook/models/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from . import account_move +from . import account_move_line diff --git a/stock_account_prepare_anglo_saxon_out_lines_hook/models/account_move.py b/stock_account_prepare_anglo_saxon_out_lines_hook/models/account_move.py new file mode 100644 index 000000000..a1635b3d0 --- /dev/null +++ b/stock_account_prepare_anglo_saxon_out_lines_hook/models/account_move.py @@ -0,0 +1,49 @@ +# Copyright 2020 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import models + + +class AccountMove(models.Model): + + _inherit = "account.move" + + def _prepare_interim_account_line_vals(self, line, move, debit_interim_account): + # Compute accounting fields. + sign = -1 if move.type == "out_refund" else 1 + price_unit = line._stock_account_get_anglo_saxon_price_unit() + balance = sign * line.quantity * price_unit + return { + "name": line.name[:64], + "move_id": move.id, + "product_id": line.product_id.id, + "product_uom_id": line.product_uom_id.id, + "quantity": line.quantity, + "price_unit": price_unit, + "debit": balance < 0.0 and -balance or 0.0, + "credit": balance > 0.0 and balance or 0.0, + "account_id": debit_interim_account.id, + "exclude_from_invoice_tab": True, + "is_anglo_saxon_line": True, + } + + def _prepare_expense_account_line_vals(self, line, move, credit_expense_account): + # Compute accounting fields. + sign = -1 if move.type == "out_refund" else 1 + price_unit = line._stock_account_get_anglo_saxon_price_unit() + balance = sign * line.quantity * price_unit + return { + "name": line.name[:64], + "move_id": move.id, + "product_id": line.product_id.id, + "product_uom_id": line.product_uom_id.id, + "quantity": line.quantity, + "price_unit": -price_unit, + "debit": balance > 0.0 and balance or 0.0, + "credit": balance < 0.0 and -balance or 0.0, + "account_id": credit_expense_account.id, + "analytic_account_id": line.analytic_account_id.id, + "analytic_tag_ids": [(6, 0, line.analytic_tag_ids.ids)], + "exclude_from_invoice_tab": True, + "is_anglo_saxon_line": True, + } diff --git a/stock_account_prepare_anglo_saxon_out_lines_hook/models/account_move_line.py b/stock_account_prepare_anglo_saxon_out_lines_hook/models/account_move_line.py new file mode 100644 index 000000000..57a5d540f --- /dev/null +++ b/stock_account_prepare_anglo_saxon_out_lines_hook/models/account_move_line.py @@ -0,0 +1,15 @@ +# Copyright 2020 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import models + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + def _eligible_for_cogs(self): + self.ensure_one() + return ( + self.product_id.type == "product" + and self.product_id.valuation == "real_time" + ) diff --git a/stock_account_prepare_anglo_saxon_out_lines_hook/static/description/icon.png b/stock_account_prepare_anglo_saxon_out_lines_hook/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/stock_account_prepare_anglo_saxon_out_lines_hook/static/description/icon.png differ