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

[16.0][MIG] crm required field by stage #598

Open
wants to merge 2 commits into
base: 16.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
101 changes: 101 additions & 0 deletions crm_required_field_by_stage/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
===============================
Required Fields by Stage in CRM
===============================

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

.. |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-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcrm-lightgray.png?logo=github
:target: https://github.com/OCA/crm/tree/16.0/crm_required_field_by_stage
:alt: OCA/crm
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/crm-16-0/crm-16-0-crm_required_field_by_stage
: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/crm&target_branch=16.0
:alt: Try me on Runboat

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

This module was written to extend the functionality of CRM leads to
require certain fields to be filled out based on the stage of the lead.

**Table of contents**

.. contents::
:local:

Usage
=====

To use this module, you need to:

- Go to *CRM > Configuration > Stages*.
- Select a stage.
- You will see the new field called required fields.
- Add the fields that you want to be required when the lead is in this
stage.
- Save the stage.
- Now, when a lead is in this stage, the selected fields will be
required.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/crm/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/crm/issues/new?body=module:%20crm_required_field_by_stage%0Aversion:%2016.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
-------

* Jarsa

Contributors
------------

- `Jarsa <https://www.jarsa.com>`__:

- Alan Ramos

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-alan196| image:: https://github.com/alan196.png?size=40px
:target: https://github.com/alan196
:alt: alan196

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

|maintainer-alan196|

This module is part of the `OCA/crm <https://github.com/OCA/crm/tree/16.0/crm_required_field_by_stage>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
4 changes: 4 additions & 0 deletions crm_required_field_by_stage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2024 Jarsa
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import models
17 changes: 17 additions & 0 deletions crm_required_field_by_stage/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2024 Jarsa
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

{
"name": "Required Fields by Stage in CRM",
"summary": "Define required fields by stage in CRM leads",
"version": "16.0.1.0.0",
"category": "Customer Relationship Management",
"website": "https://github.com/OCA/crm",
"author": "Jarsa, Odoo Community Association (OCA)",
"maintainers": ["alan196"],
"license": "LGPL-3",
"application": False,
"installable": True,
"depends": ["crm"],
"data": ["views/crm_stage_views.xml"],
}
38 changes: 38 additions & 0 deletions crm_required_field_by_stage/i18n/es_MX.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * crm_required_field_by_stage
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 17.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-19 15:37+0000\n"
"PO-Revision-Date: 2024-04-19 09:37-0600\n"
"Last-Translator: Jesús Alan Ramos Rodríguez <[email protected]>\n"
"Language-Team: \n"
"Language: es_MX\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 3.4.2\n"

#. module: crm_required_field_by_stage
#: model:ir.model,name:crm_required_field_by_stage.model_crm_stage
msgid "CRM Stages"
msgstr "Etapas del CRM"

#. module: crm_required_field_by_stage
#: model:ir.model.fields,help:crm_required_field_by_stage.field_crm_stage__required_field_ids
msgid "Fields that are required when the lead is in this stage."
msgstr "Campos que son obligatorios cuando la oportunidad se encuentra en esta etapa."

#. module: crm_required_field_by_stage
#: model:ir.model,name:crm_required_field_by_stage.model_crm_lead
msgid "Lead/Opportunity"
msgstr "Lead/oportunidad"

#. module: crm_required_field_by_stage
#: model:ir.model.fields,field_description:crm_required_field_by_stage.field_crm_stage__required_field_ids
msgid "Required Field"
msgstr "Campo requerido"
5 changes: 5 additions & 0 deletions crm_required_field_by_stage/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright 2024 Jarsa
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from . import crm_lead
from . import crm_stage
74 changes: 74 additions & 0 deletions crm_required_field_by_stage/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright 2024 Jarsa
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

import ast
import json as simplejson

from odoo import _, api, models
from odoo.exceptions import UserError


class CrmLead(models.Model):
_inherit = "crm.lead"

@api.model
def _get_view(self, view_id=None, view_type="form", **options):
arch, view = super()._get_view(view_id, view_type, **options)
stages = self.env["crm.stage"].search([("required_field_ids", "!=", False)])
if view.type == "form" and stages:
for field in stages.mapped("required_field_ids"):
stages_with_field = stages.filtered(
lambda stage, field=field: field in stage.required_field_ids
)
for node in arch.xpath("//field[@name='%s']" % field.name):
attrs = ast.literal_eval(node.attrib.get("attrs", "{}"))
if attrs:
if attrs.get("required"):
attrs["required"] = [

Check warning on line 27 in crm_required_field_by_stage/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_required_field_by_stage/models/crm_lead.py#L27

Added line #L27 was not covered by tests
"|",
("stage_id", "in", stages_with_field.ids),
] + attrs["required"]
else:
attrs["required"] = [

Check warning on line 32 in crm_required_field_by_stage/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_required_field_by_stage/models/crm_lead.py#L32

Added line #L32 was not covered by tests
("stage_id", "in", stages_with_field.ids)
]
else:
attrs["required"] = [("stage_id", "in", stages_with_field.ids)]
node.set("attrs", simplejson.dumps(attrs))
return arch, view

@api.model
def _get_view_cache_key(self, view_id=None, view_type="form", **options):
"""The override of _get_view changing the required fields labels according
to the stage makes the view cache dependent on the stages with required fields."""
key = super()._get_view_cache_key(view_id, view_type, **options)
return key + tuple(
self.env["crm.stage"]
.search([("required_field_ids", "!=", False)])
.mapped("required_field_ids.name")
)

@api.constrains("stage_id")
def _check_stage_id_(self):
for rec in self:
stage = self.env["crm.stage"].sudo().search([("id", "=", rec.stage_id.id)])
for s in stage:
fields = (
self.env["ir.model.fields"]
.sudo()
.search([("id", "in", s.required_field_ids.ids)])
)
for field in fields:
if hasattr(self, "%s" % field.name):
if not getattr(self, "%s" % field.name):
raise UserError(
_(
"Field '%(field)s' is mandatory in stage '%(stage)s'."
)
% (
{
"field": field.display_name.split(" (")[0],
"stage": s.display_name,
}
)
)
14 changes: 14 additions & 0 deletions crm_required_field_by_stage/models/crm_stage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2024 Jarsa
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).

from odoo import fields, models


class CrmStage(models.Model):
_inherit = "crm.stage"

required_field_ids = fields.Many2many(
comodel_name="ir.model.fields",
domain=[("model", "=", "crm.lead")],
help="Fields that are required when the lead is in this stage.",
)
3 changes: 3 additions & 0 deletions crm_required_field_by_stage/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
2 changes: 2 additions & 0 deletions crm_required_field_by_stage/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [Jarsa](https://www.jarsa.com):
- Alan Ramos
2 changes: 2 additions & 0 deletions crm_required_field_by_stage/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module was written to extend the functionality of CRM leads to
require certain fields to be filled out based on the stage of the lead.
8 changes: 8 additions & 0 deletions crm_required_field_by_stage/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
To use this module, you need to:

- Go to *CRM \> Configuration \> Stages*.
- Select a stage.
- You will see the new field called required fields.
- Add the fields that you want to be required when the lead is in this stage.
- Save the stage.
- Now, when a lead is in this stage, the selected fields will be required.
Loading
Loading