From 897ff24882e7777f9f9163b17aebee27b474bf08 Mon Sep 17 00:00:00 2001 From: Denis Roussel Date: Sun, 7 Nov 2021 10:08:41 +0100 Subject: [PATCH] [14.0][IMP] contract_sale_generation Use the generation_type field defined now in contract base module. Improve tests --- contract_sale_generation/README.rst | 14 +-- contract_sale_generation/__manifest__.py | 3 +- contract_sale_generation/models/__init__.py | 1 + .../models/abstract_contract.py | 14 +-- contract_sale_generation/models/contract.py | 40 ++------ contract_sale_generation/models/sale_order.py | 24 +++++ .../readme/CONTRIBUTORS.rst | 1 + .../static/description/index.html | 10 +- .../tests/test_contract_sale.py | 97 +++++++++++-------- contract_sale_generation/views/contract.xml | 13 +-- .../views/contract_template.xml | 12 --- 11 files changed, 119 insertions(+), 110 deletions(-) create mode 100644 contract_sale_generation/models/sale_order.py delete mode 100644 contract_sale_generation/views/contract_template.xml diff --git a/contract_sale_generation/README.rst b/contract_sale_generation/README.rst index 24e5d2283..1ad37d085 100644 --- a/contract_sale_generation/README.rst +++ b/contract_sale_generation/README.rst @@ -14,18 +14,18 @@ Contracts Management - Recurring Sales :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html :alt: License: AGPL-3 .. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcontract-lightgray.png?logo=github - :target: https://github.com/OCA/contract/tree/12.0/contract_sale_generation + :target: https://github.com/OCA/contract/tree/14.0/contract_sale_generation :alt: OCA/contract .. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png - :target: https://translation.odoo-community.org/projects/contract-12-0/contract-12-0-contract_sale_generation + :target: https://translation.odoo-community.org/projects/contract-14-0/contract-14-0-contract_sale_generation :alt: Translate me on Weblate .. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png - :target: https://runbot.odoo-community.org/runbot/110/12.0 + :target: https://runbot.odoo-community.org/runbot/110/14.0 :alt: Try me on Runbot |badge1| |badge2| |badge3| |badge4| |badge5| -This module extends functionality of contracts to be able to generate sales +This module extends functionality of contracts to be able to generate sales orders instead of invoices. **Table of contents** @@ -50,7 +50,7 @@ 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 `_. +`feedback `_. Do not contact contributors directly about support or help with technical issues. @@ -60,6 +60,7 @@ Credits Authors ~~~~~~~ +* ACSONE SA/NV * PESOL Contributors @@ -68,6 +69,7 @@ Contributors * Angel Moya * Florent THOMAS * Serpent Consulting Services Pvt. Ltd. +* Denis Roussel Maintainers ~~~~~~~~~~~ @@ -82,6 +84,6 @@ 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. -This module is part of the `OCA/contract `_ project on GitHub. +This module is part of the `OCA/contract `_ project on GitHub. You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/contract_sale_generation/__manifest__.py b/contract_sale_generation/__manifest__.py index 9a492577c..758a23e0c 100644 --- a/contract_sale_generation/__manifest__.py +++ b/contract_sale_generation/__manifest__.py @@ -8,13 +8,12 @@ "version": "14.0.1.0.0", "category": "Contract Management", "license": "AGPL-3", - "author": "PESOL, " "Odoo Community Association (OCA)", + "author": "ACSONE SA/NV, PESOL, Odoo Community Association (OCA)", "website": "https://github.com/OCA/contract", "depends": ["contract_sale"], "data": [ "data/contract_cron.xml", "views/contract.xml", - "views/contract_template.xml", ], "installable": True, } diff --git a/contract_sale_generation/models/__init__.py b/contract_sale_generation/models/__init__.py index 5a5b6e24b..69f40465f 100644 --- a/contract_sale_generation/models/__init__.py +++ b/contract_sale_generation/models/__init__.py @@ -2,5 +2,6 @@ from . import abstract_contract from . import contract +from . import sale_order from . import sale_order_line from . import contract_line diff --git a/contract_sale_generation/models/abstract_contract.py b/contract_sale_generation/models/abstract_contract.py index e5c4a60c3..e5452dca5 100644 --- a/contract_sale_generation/models/abstract_contract.py +++ b/contract_sale_generation/models/abstract_contract.py @@ -2,16 +2,16 @@ # Copyright 2017 Angel Moya # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import fields, models +from odoo import api, fields, models class ContractAbstractContract(models.AbstractModel): _inherit = "contract.abstract.contract" - type = fields.Selection( - [("invoice", "Invoice"), ("sale", "Sale")], - string="Type", - default="invoice", - required=True, - ) sale_autoconfirm = fields.Boolean(string="Sale Autoconfirm") + + @api.model + def _get_generation_type_selection(self): + res = super()._get_generation_type_selection() + res.append(("sale", "Sale")) + return res diff --git a/contract_sale_generation/models/contract.py b/contract_sale_generation/models/contract.py index ed048091c..030cbdc46 100644 --- a/contract_sale_generation/models/contract.py +++ b/contract_sale_generation/models/contract.py @@ -114,39 +114,13 @@ class ContractContract(models.Model): def _recurring_create_sale(self, date_ref=False): sales_values = self._prepare_recurring_sales_values(date_ref) - so_rec = self.env["sale.order"].create(sales_values) - for _rec in self.filtered(lambda c: c.sale_autoconfirm): - so_rec.action_confirm() - return so_rec + sale_orders = self.env["sale.order"].create(sales_values) + sale_orders_to_confirm = sale_orders.filtered( + lambda sale: sale.contract_auto_confirm + ) + sale_orders_to_confirm.action_confirm() + return sale_orders @api.model def cron_recurring_create_sale(self, date_ref=None): - if not date_ref: - date_ref = fields.Date.context_today(self) - domain = self._get_contracts_to_invoice_domain(date_ref) - domain.extend([("type", "=", "sale")]) - sales = self.env["sale.order"] - # Sales by companies, so assignation emails get correct context - companies_to_sale = self.read_group(domain, ["company_id"], ["company_id"]) - for row in companies_to_sale: - contracts_to_sale = self.search(row["__domain"]).with_context( - allowed_company_ids=[row["company_id"][0]] - ) - sales |= contracts_to_sale._recurring_create_sale(date_ref) - return sales - - @api.model - def cron_recurring_create_invoice(self, date_ref=None): - if not date_ref: - date_ref = fields.Date.context_today(self) - domain = self._get_contracts_to_invoice_domain(date_ref) - domain.extend([("type", "=", "invoice")]) - invoices = self.env["account.move"] - # Invoice by companies, so assignation emails get correct context - companies_to_invoice = self.read_group(domain, ["company_id"], ["company_id"]) - for row in companies_to_invoice: - contracts_to_invoice = self.search(row["__domain"]).with_context( - allowed_company_ids=[row["company_id"][0]] - ) - invoices |= contracts_to_invoice._recurring_create_invoice(date_ref) - return invoices + return self._cron_recurring_create(date_ref, create_type="sale") diff --git a/contract_sale_generation/models/sale_order.py b/contract_sale_generation/models/sale_order.py new file mode 100644 index 000000000..fb9484677 --- /dev/null +++ b/contract_sale_generation/models/sale_order.py @@ -0,0 +1,24 @@ +# Copyright 2021 ACSONE SA/NV () +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class SaleOrder(models.Model): + _inherit = "sale.order" + + contract_auto_confirm = fields.Boolean( + compute="_compute_contract_auto_confirm", + help="This is a technical field in order to know if the order should" + "be automatically confirmed if generated by contract.", + ) + + def _compute_contract_auto_confirm(self): + sale_auto_confirm = self.filtered( + lambda sale: any( + line.contract_line_id.contract_id.sale_autoconfirm + for line in sale.order_line + ) + ) + sale_auto_confirm.contract_auto_confirm = True + (self - sale_auto_confirm).contract_auto_confirm = False diff --git a/contract_sale_generation/readme/CONTRIBUTORS.rst b/contract_sale_generation/readme/CONTRIBUTORS.rst index 4c26c0480..21cb88647 100644 --- a/contract_sale_generation/readme/CONTRIBUTORS.rst +++ b/contract_sale_generation/readme/CONTRIBUTORS.rst @@ -1,3 +1,4 @@ * Angel Moya * Florent THOMAS * Serpent Consulting Services Pvt. Ltd. +* Denis Roussel diff --git a/contract_sale_generation/static/description/index.html b/contract_sale_generation/static/description/index.html index 74937050d..62720c8c8 100644 --- a/contract_sale_generation/static/description/index.html +++ b/contract_sale_generation/static/description/index.html @@ -3,7 +3,7 @@ - + Contracts Management - Recurring Sales