[FIX] website_portal(_sale), base: prevent VAT edition if user has SO/invoices
This commit prevent VAT and company name edition if an user has already at least one document (SO or invoice) for his partner or his related company partner. Any module can override this feature to add their own condition to prevent campany name/VAT edition. task-33088 Closes #23420
This commit is contained in:
@@ -139,12 +139,14 @@ class PortalAccount(CustomerPortal):
|
||||
error, error_message = super(PortalAccount, self).details_form_validate(data)
|
||||
# prevent VAT/name change if invoices exist
|
||||
partner = request.env['res.users'].browse(request.uid).partner_id
|
||||
invoices = request.env['account.invoice'].sudo().search_count([('partner_id', '=', partner.id), ('state', 'not in', ['draft', 'cancel'])])
|
||||
if invoices:
|
||||
if not partner.can_edit_vat():
|
||||
if 'vat' in data and (data['vat'] or False) != (partner.vat or False):
|
||||
error['vat'] = 'error'
|
||||
error_message.append(_('Changing VAT number is not allowed once invoices have been issued for your account. Please contact us directly for this operation.'))
|
||||
if 'name' in data and (data['name'] or False) != (partner.name or False):
|
||||
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.'))
|
||||
if 'company_name' in data and (data['company_name'] or False) != (partner.company_name or False):
|
||||
error['company_name'] = 'error'
|
||||
error_message.append(_('Changing your company name is not allowed once invoices have been issued for your account. Please contact us directly for this operation.'))
|
||||
return error, error_message
|
||||
|
||||
@@ -441,3 +441,15 @@ class ResPartner(models.Model):
|
||||
action['domain'] = literal_eval(action['domain'])
|
||||
action['domain'].append(('partner_id', 'child_of', self.id))
|
||||
return action
|
||||
|
||||
def can_edit_vat(self):
|
||||
can_edit_vat = super(ResPartner, self).can_edit_vat()
|
||||
if not can_edit_vat:
|
||||
return can_edit_vat
|
||||
Invoice = self.env['account.invoice']
|
||||
has_invoice = Invoice.search([
|
||||
('type', 'in', ['out_invoice', 'out_refund']),
|
||||
('partner_id', 'child_of', self.commercial_partner_id.id),
|
||||
('state', 'not in', ['draft', 'cancel'])
|
||||
], limit=1)
|
||||
return can_edit_vat and not (bool(has_invoice))
|
||||
|
||||
@@ -198,19 +198,23 @@ class CustomerPortal(Controller):
|
||||
error_message.append(_('Invalid Email! Please enter a valid email address.'))
|
||||
|
||||
# vat validation
|
||||
partner = request.env["res.partner"]
|
||||
if data.get("vat") and hasattr(partner, "check_vat"):
|
||||
if data.get("country_id"):
|
||||
data["vat"] = request.env["res.partner"].fix_eu_vat_number(int(data.get("country_id")), data.get("vat"))
|
||||
partner_dummy = partner.new({
|
||||
'vat': data['vat'],
|
||||
'country_id': (int(data['country_id'])
|
||||
if data.get('country_id') else False),
|
||||
})
|
||||
try:
|
||||
partner_dummy.check_vat()
|
||||
except ValidationError:
|
||||
error["vat"] = 'error'
|
||||
partner = request.env.user.partner_id
|
||||
if data.get("vat") and partner and partner.vat != data.get("vat"):
|
||||
if partner.can_edit_vat():
|
||||
if hasattr(partner, "check_vat"):
|
||||
if data.get("country_id"):
|
||||
data["vat"] = request.env["res.partner"].fix_eu_vat_number(int(data.get("country_id")), data.get("vat"))
|
||||
partner_dummy = partner.new({
|
||||
'vat': data['vat'],
|
||||
'country_id': (int(data['country_id'])
|
||||
if data.get('country_id') else False),
|
||||
})
|
||||
try:
|
||||
partner_dummy.check_vat()
|
||||
except ValidationError:
|
||||
error["vat"] = 'error'
|
||||
else:
|
||||
error_message.append(_('Changing VAT number is not allowed once document(s) have been issued for your account. Please contact us directly for this operation.'))
|
||||
|
||||
# error message for empty required fields
|
||||
if [err for err in error.values() if err == 'missing']:
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
|
||||
from . import mail_thread
|
||||
from . import mail_message
|
||||
from . import portal_mixin
|
||||
from . import portal_mixin
|
||||
from . import res_partner
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class ResPartner(models.Model):
|
||||
_inherit = 'res.partner'
|
||||
|
||||
def can_edit_vat(self):
|
||||
return True
|
||||
@@ -256,11 +256,11 @@
|
||||
<div t-attf-class="form-group #{error.get('company_name') and 'has-error' or ''} col-lg-6">
|
||||
<label class="control-label label-optional" for="company_name">Company Name</label>
|
||||
<p t-if="partner.commercial_partner_id.is_company" class="form-control" t-esc="partner.commercial_company_name"/>
|
||||
<input t-else="" type="text" name="company_name" class="form-control" t-att-value="company_name or partner.company_name"/>
|
||||
<input t-else="" type="text" name="company_name" class="form-control" t-att-value="company_name or partner.company_name" t-att-readonly="None if partner.can_edit_vat() else '1'" t-att-title="None if partner.can_edit_vat() else 'Changing company name is not allowed once document(s) have been issued for your account. Please contact us directly for this operation.'"/>
|
||||
</div>
|
||||
<div t-if="has_check_vat" t-attf-class="form-group #{error.get('vat') and 'has-error' or ''} col-lg-6">
|
||||
<div t-attf-class="form-group #{error.get('vat') and 'has-error' or ''} col-lg-6">
|
||||
<label class="control-label label-optional" for="vat">VAT Number</label>
|
||||
<input type="text" name="vat" class="form-control" t-att-value="vat or partner.vat" />
|
||||
<input type="text" name="vat" class="form-control" t-att-value="vat or partner.vat" t-att-readonly="None if partner.can_edit_vat() else '1'" t-att-title="None if partner.can_edit_vat() else 'Changing VAT number is not allowed once document(s) have been issued for your account. Please contact us directly for this operation.'" />
|
||||
</div>
|
||||
|
||||
<div class="clearfix" />
|
||||
|
||||
@@ -25,3 +25,14 @@ class ResPartner(models.Model):
|
||||
partner_ids = [partner.id] + item.get('child_ids')
|
||||
# then we can sum for all the partner's child
|
||||
partner.sale_order_count = sum(mapped_data.get(child, 0) for child in partner_ids)
|
||||
|
||||
def can_edit_vat(self):
|
||||
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([
|
||||
('partner_id', 'child_of', self.commercial_partner_id.id),
|
||||
('state', 'in', ['sent', 'sale', 'done'])
|
||||
], limit=1)
|
||||
return can_edit_vat and not bool(has_so)
|
||||
|
||||
@@ -590,6 +590,7 @@ class WebsiteSale(http.Controller):
|
||||
return redirection
|
||||
|
||||
mode = (False, False)
|
||||
can_edit_vat = False
|
||||
def_country_id = order.partner_id.country_id
|
||||
values, errors = {}, {}
|
||||
|
||||
@@ -598,6 +599,7 @@ class WebsiteSale(http.Controller):
|
||||
# IF PUBLIC ORDER
|
||||
if order.partner_id.id == request.website.user_id.sudo().partner_id.id:
|
||||
mode = ('new', 'billing')
|
||||
can_edit_vat = True
|
||||
country_code = request.session['geoip'].get('country_code')
|
||||
if country_code:
|
||||
def_country_id = request.env['res.country'].search([('code', '=', country_code)], limit=1)
|
||||
@@ -608,6 +610,7 @@ class WebsiteSale(http.Controller):
|
||||
if partner_id > 0:
|
||||
if partner_id == order.partner_id.id:
|
||||
mode = ('edit', 'billing')
|
||||
can_edit_vat = order.partner_id.can_edit_vat()
|
||||
else:
|
||||
shippings = Partner.search([('id', 'child_of', order.partner_id.commercial_partner_id.ids)])
|
||||
if partner_id in shippings.mapped('id'):
|
||||
@@ -651,6 +654,7 @@ class WebsiteSale(http.Controller):
|
||||
'partner_id': partner_id,
|
||||
'mode': mode,
|
||||
'checkout': values,
|
||||
'can_edit_vat': can_edit_vat,
|
||||
'country': country,
|
||||
'countries': country.get_website_sale_countries(mode=mode[1]),
|
||||
"states": country.get_website_sale_states(mode=mode[1]),
|
||||
|
||||
@@ -1161,14 +1161,14 @@
|
||||
<input type="tel" name="phone" class="form-control" t-att-value="'phone' in checkout and checkout['phone']" />
|
||||
</div>
|
||||
<div class="clearfix" />
|
||||
<t t-if="mode == ('new', 'billing')">
|
||||
<t t-if="mode == ('new', 'billing') or (mode == ('edit', 'billing') and (can_edit_vat or 'vat' in checkout and checkout['vat']))">
|
||||
<div t-attf-class="form-group #{error.get('company_name') and 'has-error' or ''} col-md-6">
|
||||
<label class="control-label font-weight-normal label-optional" for="company_name">Company Name</label>
|
||||
<input type="text" name="company_name" class="form-control" t-att-value="'company_name' in checkout and checkout['company_name']" />
|
||||
<input type="text" name="company_name" class="form-control" t-att-value="'company_name' in checkout and checkout['company_name']" t-att-readonly="'1' if 'vat' in checkout and checkout['vat'] and not can_edit_vat else None" t-att-title="'Changing company name is not allowed once document(s) have been issued for your account. Please contact us directly for this operation.' if 'vat' in checkout and checkout['vat'] and not can_edit_vat else None" />
|
||||
</div>
|
||||
<div t-attf-class="form-group #{error.get('vat') and 'has-error' or ''} col-md-6 div_vat">
|
||||
<label class="control-label font-weight-normal label-optional" for="vat">TIN / VAT </label>
|
||||
<input type="text" name="vat" class="form-control" t-att-value="'vat' in checkout and checkout['vat']" />
|
||||
<input type="text" name="vat" class="form-control" t-att-value="'vat' in checkout and checkout['vat']" t-att-readonly="'1' if 'vat' in checkout and checkout['vat'] and not can_edit_vat else None" t-att-title="'Changing VAT number is not allowed once document(s) have been issued for your account. Please contact us directly for this operation.' if 'vat' in checkout and checkout['vat'] and not can_edit_vat else None" />
|
||||
</div>
|
||||
</t>
|
||||
<div class="clearfix" />
|
||||
|
||||
Reference in New Issue
Block a user