Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15.0][ADD] contract_sale_generation_merge: New module contract_sale_generation_merge #1196

Open
wants to merge 1 commit into
base: 15.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions contract_sale_generation_merge/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
============================================
Contracts Management - Recurring Sales Merge
============================================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f10acd5ec9b53b9daf0b48c0d0ac856355aa3ceb797516528bffde97af0a61de
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
: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/15.0/contract_sale_generation_merge
:alt: OCA/contract
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/contract-15-0/contract-15-0-contract_sale_generation_merge
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/contract&target_branch=15.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module extends contract sale generation by merging contract lines into existing sales orders within the same commitment (delivery) date.

**Table of contents**

.. contents::
:local:

Usage
=====

To use this module, you need to:

1. Go to **Sales -> Orders -> Contracts** and select or create a new contract.
2. Add contract lines with the desired product and quantity.
3. Set the **Generation Type** field to **Sale**
4. Check the **Merge Existing Orders** field. This will merge the contract lines into an existing sale order within the same commitment (delivery) date as the **Date of Next Invoice** field on the contract. If no matching sale order is found, a new one will be created.

To force the sale order generation, enable the **Debug Mode** and click on the **CREATE SALES** button.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/contract/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/contract/issues/new?body=module:%20contract_sale_generation_merge%0Aversion:%2015.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* APSL-Nagarro

Contributors
~~~~~~~~~~~~

* `APSL-Nagarro <https://www.apsl.tech>`_:
* Patryk Pyczko <[email protected]>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

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.

.. |maintainer-ppyczko| image:: https://github.com/ppyczko.png?size=40px
:target: https://github.com/ppyczko
:alt: ppyczko

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-ppyczko|

This module is part of the `OCA/contract <https://github.com/OCA/contract/tree/15.0/contract_sale_generation_merge>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions contract_sale_generation_merge/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
20 changes: 20 additions & 0 deletions contract_sale_generation_merge/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2025 Patryk Pyczko (APSL-Nagarro)<[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "Contracts Management - Recurring Sales Merge",
"version": "15.0.1.0.0",
"summary": "Merges contract lines into existing sale orders "
"with the same commitment (delivery) date.",
"category": "Contract Management",
"website": "https://github.com/OCA/contract",
"author": "APSL-Nagarro, Odoo Community Association (OCA)",
"maintainers": ["ppyczko"],
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": ["contract_sale_generation"],
"data": [
"views/contract.xml",
],
}
34 changes: 34 additions & 0 deletions contract_sale_generation_merge/i18n/ca.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * contract_sale_generation_merge
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-02-17 08:59+0000\n"
"PO-Revision-Date: 2025-02-17 08:59+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: contract_sale_generation_merge
#: model:ir.model,name:contract_sale_generation_merge.model_contract_contract
msgid "Contract"
msgstr "Contracte"

#. module: contract_sale_generation_merge
#: model:ir.model.fields,help:contract_sale_generation_merge.field_contract_contract__merge_sales_orders
msgid ""
"If enabled, contract lines will be added to an existing sales order with the"
" same commitment (delivery) date instead of creating a new one."
msgstr ""
"Si està habilitat, les línies del contracte s'afegiran a una comanda de venda existent amb la mateixa data de compromís (lliurament) en lloc de crear-ne una de nova."

#. module: contract_sale_generation_merge
#: model:ir.model.fields,field_description:contract_sale_generation_merge.field_contract_contract__merge_sales_orders
msgid "Merge Existing Orders"
msgstr "Fusionar comandes existents"
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * contract_sale_generation_merge
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-02-17 08:59+0000\n"
"PO-Revision-Date: 2025-02-17 08:59+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: contract_sale_generation_merge
#: model:ir.model,name:contract_sale_generation_merge.model_contract_contract
msgid "Contract"
msgstr ""

#. module: contract_sale_generation_merge
#: model:ir.model.fields,help:contract_sale_generation_merge.field_contract_contract__merge_sales_orders
msgid ""
"If enabled, contract lines will be added to an existing sales order with the"
" same commitment (delivery) date instead of creating a new one."
msgstr ""

#. module: contract_sale_generation_merge
#: model:ir.model.fields,field_description:contract_sale_generation_merge.field_contract_contract__merge_sales_orders
msgid "Merge Existing Orders"
msgstr ""
34 changes: 34 additions & 0 deletions contract_sale_generation_merge/i18n/es.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * contract_sale_generation_merge
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 15.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-02-17 08:59+0000\n"
"PO-Revision-Date: 2025-02-17 08:59+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: contract_sale_generation_merge
#: model:ir.model,name:contract_sale_generation_merge.model_contract_contract
msgid "Contract"
msgstr "Contrato"

#. module: contract_sale_generation_merge
#: model:ir.model.fields,help:contract_sale_generation_merge.field_contract_contract__merge_sales_orders
msgid ""
"If enabled, contract lines will be added to an existing sales order with the"
" same commitment (delivery) date instead of creating a new one."
msgstr ""
"Si está habilitado, las líneas del contrato se agregarán a un pedido de venta existente con la misma fecha de compromiso (entrega) en lugar de crear uno nuevo."

#. module: contract_sale_generation_merge
#: model:ir.model.fields,field_description:contract_sale_generation_merge.field_contract_contract__merge_sales_orders
msgid "Merge Existing Orders"
msgstr "Fusionar Pedidos Existentes"
3 changes: 3 additions & 0 deletions contract_sale_generation_merge/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import contract
64 changes: 64 additions & 0 deletions contract_sale_generation_merge/models/contract.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Copyright 2025 Patryk Pyczko (APSL-Nagarro)<[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import datetime, timedelta

from odoo import fields, models


class ContractContract(models.Model):
_inherit = "contract.contract"

merge_sales_orders = fields.Boolean(
string="Merge Existing Orders",
help="If enabled, contract lines will be added to an existing sales order "
"with the same commitment (delivery) date instead of creating a new one.",
)

def _recurring_create_sale(self, date_ref=False):
sales_values = self._prepare_recurring_sales_values(date_ref)
sale_orders = self.env["sale.order"]
new_sale_values = []

for sale_values in sales_values:
existing_sale = (
self._get_existing_sale(sale_values)
if self.merge_sales_orders
else None
)

if existing_sale:
self._add_contract_lines(existing_sale)
sale_orders |= existing_sale
else:
new_sale_values.append(sale_values)

if new_sale_values:
sale_orders |= self.env["sale.order"].create(new_sale_values)

sale_orders.filtered(lambda sale: sale.contract_auto_confirm).action_confirm()
self._compute_recurring_next_date()
return sale_orders

def _get_existing_sale(self, sale_values):
"""Find an existing active sale order for the same partner
within the same day (commitment date)."""
date_only = sale_values["date_order"].date()
date_start = datetime.combine(date_only, datetime.min.time())
date_end = datetime.combine(date_only + timedelta(days=1), datetime.min.time())

return self.env["sale.order"].search(
[
("partner_id", "=", sale_values["partner_id"]),
("commitment_date", ">=", date_start),
("commitment_date", "<", date_end),
("state", "!=", "cancel"),
],
limit=1,
)

def _add_contract_lines(self, existing_sale):
"""Add contract lines to an existing sale order."""
for contract_line in self.contract_line_ids:
sale_line_vals = contract_line._prepare_sale_line(order_id=existing_sale)
existing_sale.write({"order_line": [(0, 0, sale_line_vals)]})
2 changes: 2 additions & 0 deletions contract_sale_generation_merge/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* `APSL-Nagarro <https://www.apsl.tech>`_:
* Patryk Pyczko <[email protected]>
1 change: 1 addition & 0 deletions contract_sale_generation_merge/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module extends contract sale generation by merging contract lines into existing sales orders within the same commitment (delivery) date.
8 changes: 8 additions & 0 deletions contract_sale_generation_merge/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To use this module, you need to:

1. Go to **Sales -> Orders -> Contracts** and select or create a new contract.
2. Add contract lines with the desired product and quantity.
3. Set the **Generation Type** field to **Sale**
4. Check the **Merge Existing Orders** field. This will merge the contract lines into an existing sale order within the same commitment (delivery) date as the **Date of Next Invoice** field on the contract. If no matching sale order is found, a new one will be created.

To force the sale order generation, enable the **Debug Mode** and click on the **CREATE SALES** button.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading