Commit 6b35b2f2 authored by Actarus's avatar Actarus
Browse files

Added warnings in customer.site for inconsistent contacts

parent 800f3531
......@@ -11,6 +11,7 @@ class Contact(models.Model):
# ======
name = fields.Char(string="Nom", required=True)
active = fields.Boolean(required=True, default=True)
partner_id = fields.Many2one("customer.partner", string="Société")
# Address
......
......@@ -11,6 +11,7 @@ class Partner(models.Model):
# ======
name = fields.Char(string="Nom", required=True)
active = fields.Boolean(required=True, default=True)
# Address
street = fields.Char(string="Rue")
......
# -*- coding: utf-8 -*-
from odoo import models, fields
from odoo import models, fields, api
from odoo.exceptions import ValidationError
class CustomerSite(models.Model):
_name = "customer.site"
......@@ -11,6 +12,15 @@ class CustomerSite(models.Model):
# ======
name = fields.Char(string="Nom", required=True)
active = fields.Boolean(required=True, default=True)
# Contacts
partner_id = fields.Many2one("customer.partner", string="Propriétaire")
contact_id = fields.Many2one("customer.contact", string="Contact",
domain="[('partner_id', '=?', partner_id)]")
maintenance_partner_id = fields.Many2one("customer.partner", string="Entreprise de maintenance")
maintenance_contact_id = fields.Many2one("customer.contact", string="Contact de maintenance",
domain="[('partner_id', '=?', maintenance_partner_id)]")
# Address
street = fields.Char(string="Rue")
......@@ -21,8 +31,67 @@ class CustomerSite(models.Model):
state_id = fields.Many2one("res.country.state", string="Région")
department_id = fields.Many2one("res.country.department", string="Département")
# Contacts
contact_id = fields.Many2one("customer.contact", string="Contact")
partner_id = fields.Many2one(related="contact_id.partner_id", string="Propriétaire")
maintenance_contact_id = fields.Many2one("customer.contact", string="Contact de maintenance")
maintenance_partner_id = fields.Many2one(related="maintenance_contact_id.partner_id", string="Entreprise de maintenance")
warning_messages = fields.Text(compute="_compute_warning_messages")
warning_message_short = fields.Text(compute="_compute_warning_messages")
warning_count = fields.Integer(compute="_compute_warning_messages")
# =======================
# Computations and action
# =======================
@api.onchange('partner_id')
def _onchange_partner_id(site):
if site.contact_id and site.contact_id.partner_id != site.partner_id:
site.contact_id = False
@api.onchange('contact_id')
def _onchange_contact_id(site):
if site.contact_id and site.contact_id.partner_id and site.contact_id.partner_id != site.partner_id:
site.partner_id = site.contact_id.partner_id
@api.onchange('maintenance_partner_id')
def _onchange_maintenance_partner_id(site):
if site.maintenance_contact_id and site.maintenance_contact_id.partner_id != site.maintenance_partner_id:
site.maintenance_contact_id = False
@api.onchange('maintenance_contact_id')
def _onchange_maintenance_contact_id(site):
if site.maintenance_contact_id and site.maintenance_contact_id.partner_id and site.maintenance_contact_id.partner_id != site.maintenance_partner_id:
site.maintenance_partner_id = site.maintenance_contact_id.partner_id
@api.depends(
"partner_id", "partner_id.active",
"contact_id", "contact_id.active", "contact_id.partner_id",
"maintenance_partner_id",
"maintenance_contact_id", "maintenance_contact_id.active", "maintenance_contact_id.partner_id")
def _compute_warning_messages(self):
for site in self:
# TODO: check if it is ok in case of delete of a field
# TODO: check if some logic is redundant
warnings = []
if not site.partner_id:
warnings.append("Un site doit avoir un propriétaire")
elif not site.partner_id.active:
warnings.append("Le propriétaire du site est inactif")
if not site.contact_id:
warnings.append("Un site doit avoir un contact")
else:
if not site.contact_id.active:
warnings.append("Le contact du site est inactif")
if site.contact_id.partner_id != site.partner_id:
warnings.append("Le contact du site ne correspond pas au propriétaire")
if site.maintenance_contact_id:
if not site.maintenance_contact_id.active:
warnings.append("Le contact de maintenance est incatif")
if site.maintenance_contact_id.partner_id != site.maintenance_partner_id:
warnings.append("Le contact de maintenance ne correspond pas à la société de maintenance")
site.warning_count = len(warnings)
if warnings:
site.warning_messages = "\n".join(warnings)
site.warning_message_short = warnings[0]
else:
site.warning_messages = False
site.warning_message_short = False
<?xml version="1.0"?>
<odoo>
<record id="customer_site_view_tree" model="ir.ui.view">
<field name="name">customer.site.tree</field>
<field name="model">customer.site</field>
<field name="arch" type="xml">
<tree>
<field name="warning_messages" invisible="1"/>
<button class="btn-warning" icon="fa-exclamation-triangle" attrs="{'invisible': [('warning_messages', '=', False)]}"/>
<field name="name"/>
</tree>
</field>
</record>
<record id="customer_site_view_form" model="ir.ui.view">
<field name="name">customer.site.form</field>
<field name="model">customer.site</field>
<field name="arch" type="xml">
<form>
<sheet>
<h1>
<field name="name"/>
</h1>
<div attrs="{'invisible': [('warning_messages', '=', False)]}">
<button class="btn-warning" icon="fa-exclamation-triangle" />
<field name="warning_messages" nolabel="1"/>
</div>
<group col="4">
<field name="partner_id"/>
<field name="contact_id"/>
</group>
<group string="Maintenance" col="4">
<field name="maintenance_partner_id" string="Société"/>
<field name="maintenance_contact_id" string="Contact"/>
</group>
<group>
<group string="Adresse" col="3">
<field name="street" placeholder="Rue" colspan="3" nolabel="1"/>
<field name="street2" placeholder="Rue 2" colspan="3" nolabel="1"/>
<field name="city" placeholder="Ville" colspan="1" nolabel="1"/>
<field name="zip" placeholder="Code postal" colspan="1" nolabel="1"/>
<field name="department_id" placeholder="Département" colspan="1" nolabel="1"/>
<field name="state_id" placeholder="Région" colspan="2" nolabel="1"/>
<field name="country_id" placeholder="Pays" colspan="1" nolabel="1"/>
</group>
</group>
</sheet>
</form>
</field>
</record>
<record id="customer_site_action" model="ir.actions.act_window">
<field name="name">Site client</field>
<field name="res_model">customer.site</field>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment