[ADD]pms: company configuration document required

This commit is contained in:
Darío Lodeiros
2022-11-19 18:17:20 +01:00
parent 977099e426
commit 52e5e545bf
5 changed files with 148 additions and 4 deletions

View File

@@ -531,7 +531,11 @@ class PmsCheckinPartner(models.Model):
"birthdate_date": record.birthdate_date,
"nationality_id": record.nationality_id.id,
}
partner = self.env["res.partner"].create(partner_values)
partner = (
self.env["res.partner"]
.with_context(avoid_document_restriction=True)
.create(partner_values)
)
record.partner_id = partner
@api.depends("email", "mobile")

View File

@@ -43,3 +43,10 @@ class ResCompany(models.Model):
""",
default="no",
)
document_partner_required = fields.Boolean(
string="Document partner required",
help="""If true, the partner document is required
to create a new contact""",
default=False,
)

View File

@@ -879,3 +879,49 @@ class ResPartner(models.Model):
_("The partner %s cannot be deleted"), various_partner.name
)
return super().unlink()
def create(self, vals):
check_missing_document = self._check_document_partner_required(vals)
if check_missing_document:
raise ValidationError(_("A document identification is required"))
return super().create(vals)
def write(self, vals):
check_missing_document = self._check_document_partner_required(
vals, partners=self
)
if check_missing_document:
raise ValidationError(_("A document identification is required"))
return super().write(vals)
@api.model
def _check_document_partner_required(self, vals, partners=False):
company_ids = (
self.env["res.company"].sudo().search([]).ids
if (not partners or any([not partner.company_id for partner in partners]))
else partners.mapped("company_id.id")
)
if not self.env.context.get("avoid_document_restriction") and any(
[
self.env["res.company"].browse(company_id).document_partner_required
for company_id in company_ids
]
):
return self._missing_document(vals, partners)
return False
@api.model
def _missing_document(self, vals, partners=False):
if (
vals.get("vat") is False
or vals.get("vat") == ""
or (
"vat" not in vals
and (
any([not partner.vat for partner in partners]) if partners else True
)
)
):
return True
return False

View File

@@ -15,6 +15,7 @@
<field name="url_advert" />
</xpath>
<xpath expr="//field[@name='vat']" position="after">
<field name="document_partner_required" />
<field name="check_min_partner_data_invoice" />
<field name="pms_invoice_downpayment_policy" />
</xpath>

View File

@@ -1,6 +1,9 @@
import logging
from odoo import api, fields, models
from odoo import _, api, fields, models
from odoo.exceptions import UserError
from odoo.addons.base_vat.models.res_partner import _eu_country_vat
CODE_SPAIN = "ES"
@@ -33,7 +36,90 @@ class ResPartner(models.Model):
res = super(ResPartner, self)._check_enought_invoice_data()
if not res:
return res
if self.country_id.code == "ES":
if not self.state_id and not self.zip:
if not self.country_id or not self.city or not (self.street or self.street2):
return False
if not self.vat:
if self.country_id.code == "ES":
return False
elif not self.aeat_identification:
return False
return True
def write(self, vals):
res = super(ResPartner, self).write(vals)
# REVIEW: Force Contrain vat
# https://github.com/odoo/odoo/issues/23242
if vals.get("vat") or vals.get("country_id"):
self.check_vat()
self._pms_check_unique_vat()
return res
@api.model
def create(self, vals):
records = super(ResPartner, self).create(vals)
# REVIEW: Force Contrain vat
# https://github.com/odoo/odoo/issues/23242
if vals.get("vat") or vals.get("country_id"):
records.check_vat()
records._pms_check_unique_vat()
return records
# This function is a candidate to be moved to the module
# partner_vat_unique
def _pms_check_unique_vat(self):
Partner = self.with_context(active_test=False).sudo()
europe = self.env.ref("base.europe")
if not europe:
europe = self.env["res.country.group"].search(
[("name", "=", "Europe")], limit=1
)
for partner in self.filtered(lambda p: p.vat and p.country_id):
partner_country_code = partner.commercial_partner_id.country_id.code
vat_country, vat_number = self._split_vat(partner.vat)
if europe and partner.country_id.id in europe.country_ids.ids:
vat_country = _eu_country_vat.get(vat_country, vat_country).upper()
vat_with_code = (
partner.vat
if partner_country_code.lower() == vat_country
else partner_country_code.upper() + partner.vat
)
vat_without_code = (
partner.vat
if partner_country_code.lower() != vat_country
else vat_country
)
domain = [
("company_id", "in", [False, partner.company_id.id]),
"|",
("vat", "=", vat_with_code),
("vat", "=", vat_without_code),
]
domain += [("id", "!=", partner.id), "!", ("id", "child_of", partner.id)]
repeat_partner = Partner.search(domain, limit=1)
if bool(partner.vat) and not partner.parent_id and repeat_partner:
raise UserError(
_("The VAT number %s already exists in other contacts: %s")
% (
vat_without_code,
repeat_partner.name,
)
)
def _missing_document(self, vals, partners=False):
res = super(ResPartner, self)._missing_document(vals)
if not res:
return res
if (
vals.get("aeat_identification") is False
or vals.get("aeat_identification") == ""
or (
"aeat_identification" not in vals
and (
any([not partner.aeat_identification for partner in partners])
if partners
else True
)
)
):
return True
return False