[FIX] account, portal, sale, website_sale: fix name editing

Name can be edited more frequently than VAT, and we should
have a dedicated method to check whether it can be edited
or not.

closes odoo/odoo#118428

Signed-off-by: Victor Feyens (vfe) <vfe@odoo.com>
This commit is contained in:
Valeriya(vchu)
2023-10-06 13:08:53 +00:00
committed by Victor Feyens
parent d9c7ff69a4
commit ac18850621
5 changed files with 58 additions and 42 deletions
+12 -7
View File
@@ -656,17 +656,22 @@ class ResPartner(models.Model):
return action_vals
def can_edit_vat(self):
''' Can't edit `vat` if there is (non draft) issued invoices. '''
can_edit_vat = super(ResPartner, self).can_edit_vat()
if not can_edit_vat:
return can_edit_vat
has_invoice = self.env['account.move'].search([
def _has_invoice(self):
self.ensure_one()
invoice = self.env['account.move'].search([
('move_type', 'in', ['out_invoice', 'out_refund']),
('partner_id', 'child_of', self.commercial_partner_id.id),
('state', '=', 'posted')
], limit=1)
return can_edit_vat and not (bool(has_invoice))
return bool(invoice)
def _can_edit_name(self):
""" Can't edit `name` if there is (non draft) issued invoices. """
return super()._can_edit_name() and not self._has_invoice()
def can_edit_vat(self):
""" Can't edit `vat` if there is (non draft) issued invoices. """
return super().can_edit_vat() and not self._has_invoice()
@api.model_create_multi
def create(self, vals_list):
+8 -2
View File
@@ -7,8 +7,14 @@ from odoo import models
class ResPartner(models.Model):
_inherit = 'res.partner'
def _can_edit_name(self):
""" Name can be changed more often than the VAT """
self.ensure_one()
return True
def can_edit_vat(self):
''' `vat` is a commercial field, synced between the parent (commercial
""" `vat` is a commercial field, synced between the parent (commercial
entity) and the children. Only the commercial entity should be able to
edit it (as in backend). '''
edit it (as in backend)."""
self.ensure_one()
return not self.parent_id
+12 -8
View File
@@ -36,17 +36,21 @@ class ResPartner(models.Model):
partner.sale_order_count += count
partner = partner.parent_id
def can_edit_vat(self):
''' Can't edit `vat` if there is (non draft) issued SO. '''
can_edit_vat = super(ResPartner, self).can_edit_vat()
if not can_edit_vat:
return can_edit_vat
SaleOrder = self.env['sale.order']
has_so = SaleOrder.search([
def _has_order(self):
self.ensure_one()
sale_order = self.env['sale.order'].search([
('partner_id', 'child_of', self.commercial_partner_id.id),
('state', 'in', ['sent', 'sale'])
], limit=1)
return can_edit_vat and not bool(has_so)
return bool(sale_order)
def _can_edit_name(self):
""" Can't edit `name` if there is (non draft) issued SO. """
return super()._can_edit_name() and not self._has_order()
def can_edit_vat(self):
""" Can't edit `vat` if there is (non draft) issued SO. """
return super().can_edit_vat() and not self._has_order()
def action_view_sale_order(self):
action = self.env['ir.actions.act_window']._for_xml_id('sale.act_res_partner_2_sale_order')
+22 -24
View File
@@ -1061,33 +1061,31 @@ class WebsiteSale(payment_portal.PaymentPortal):
error = dict()
error_message = []
if mode == ('edit', 'billing') and data.get('partner_id'):
if data.get('partner_id'):
partner_su = request.env['res.partner'].sudo().browse(int(data['partner_id'])).exists()
name_change = partner_su and partner_su.name and 'name' in data and data['name'] != partner_su.name
email_change = partner_su and 'email' in data and data['email'] != partner_su.email and partner_su.email
invoices = request.env['account.move'].sudo().search(
[('partner_id', '=', partner_su.id)], limit=1
) if partner_su else False
if partner_su:
name_change = 'name' in data and partner_su.name and data['name'] != partner_su.name
email_change = 'email' in data and partner_su.email and data['email'] != partner_su.email
# Prevent changing the billing partner name if invoices have been issued.
if name_change and not partner_su.can_edit_vat() and invoices:
error['name'] = 'error'
error_message.append(_(
"Changing your name is not allowed once documents have been issued for your"
" account. Please contact us directly for this operation."
))
# Prevent changing the partner name if invoices have been issued.
if name_change and not partner_su._can_edit_name():
error['name'] = 'error'
error_message.append(_(
"Changing your name is not allowed once invoices have been issued for your"
" account. Please contact us directly for this operation."
))
# Prevent change the partner name or email if it is an internal user.
if (name_change or email_change) and not all(partner_su.user_ids.mapped('share')):
error.update({
'name': 'error' if name_change else None,
'email': 'error' if email_change else None,
})
error_message.append(_(
"If you are ordering for an external person, please place your order via the"
" backend. If you wish to change your name or email address, please do so in"
" the account settings or contact your administrator."
))
# Prevent change the partner name or email if it is an internal user.
if (name_change or email_change) and not all(partner_su.user_ids.mapped('share')):
error.update({
'name': 'error' if name_change else None,
'email': 'error' if email_change else None,
})
error_message.append(_(
"If you are ordering for an external person, please place your order via the"
" backend. If you wish to change your name or email address, please do so in"
" the account settings or contact your administrator."
))
# Required fields from form
required_fields = [f for f in (all_form_values.get('field_required') or '').split(',') if f]
+4 -1
View File
@@ -1922,7 +1922,10 @@
<t t-if="website._display_partner_b2b_fields()">
<div class="w-100"/>
<t t-set='vat_warning' t-value="'vat' in checkout and checkout['vat'] and not can_edit_vat" />
<t t-if="mode == ('new', 'billing') or (mode == ('edit', 'billing') and (can_edit_vat or 'vat' in checkout and checkout['vat']))">
<t t-if="(mode == ('new', 'billing') and website_sale_order._is_public_order()
or mode == ('edit', 'billing') and partner_id == website_sale_order.partner_id.id)
and (can_edit_vat or 'vat' in checkout and checkout['vat'])"
>
<div t-attf-class="mb-3 #{error.get('company_name') and 'o_has_error' or ''} col-lg-6 mb-0">
<label class="col-form-label fw-normal label-optional" for="company_name">Company Name</label>
<input type="text" name="company_name" t-attf-class="form-control #{error.get('company_name') and 'is-invalid' or ''}" t-att-value="'commercial_company_name' in checkout and checkout['commercial_company_name'] or 'company_name' in checkout and checkout['company_name']" t-att-readonly="'1' if vat_warning else None" />