Raise a warning when submitting complaint for the same origin
Closes #13360
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
* Raise a warning when submitting complaint for the same origin
|
||||
|
||||
Version 7.2.0 - 2024-04-29
|
||||
--------------------------
|
||||
|
||||
@@ -17,8 +17,11 @@ from trytond.modules.currency.fields import Monetary
|
||||
from trytond.modules.product import price_digits
|
||||
from trytond.pool import Pool
|
||||
from trytond.pyson import Bool, Eval, If
|
||||
from trytond.tools import grouped_slice
|
||||
from trytond.transaction import Transaction
|
||||
|
||||
from .exceptions import ComplaintSimilarWarning
|
||||
|
||||
|
||||
class Type(DeactivableMixin, ModelSQL, ModelView):
|
||||
'Customer Complaint Type'
|
||||
@@ -332,7 +335,7 @@ class Complaint(Workflow, ModelSQL, ModelView):
|
||||
@Workflow.transition('waiting')
|
||||
@set_employee('submitted_by')
|
||||
def wait(cls, complaints):
|
||||
pass
|
||||
cls._check_similar(complaints)
|
||||
|
||||
@classmethod
|
||||
@ModelView.button
|
||||
@@ -377,6 +380,57 @@ class Complaint(Workflow, ModelSQL, ModelView):
|
||||
action.result = record
|
||||
Action.save(sum(list(actions.values()), []))
|
||||
|
||||
@classmethod
|
||||
def _check_similar(cls, complaints):
|
||||
pool = Pool()
|
||||
Warning = pool.get('res.user.warning')
|
||||
for sub_complaints in grouped_slice(complaints):
|
||||
sub_complaints = list(sub_complaints)
|
||||
domain = list(filter(None,
|
||||
(c._similar_domain() for c in sub_complaints)))
|
||||
if not domain:
|
||||
continue
|
||||
if cls.search(['OR'] + domain, order=[]):
|
||||
for complaint in sub_complaints:
|
||||
domain = complaint._similar_domain()
|
||||
if not domain:
|
||||
continue
|
||||
try:
|
||||
similar, = cls.search(domain, limit=1)
|
||||
except ValueError:
|
||||
continue
|
||||
warning_key = Warning.format(
|
||||
'complaint_similar', [complaint])
|
||||
if Warning.check(warning_key):
|
||||
raise ComplaintSimilarWarning(warning_key,
|
||||
gettext('sale_complaint.msg_complaint_similar',
|
||||
similar=similar.rec_name,
|
||||
complaint=complaint.rec_name))
|
||||
|
||||
def _similar_domain(self):
|
||||
pool = Pool()
|
||||
Sale = pool.get('sale.sale')
|
||||
SaleLine = pool.get('sale.line')
|
||||
Invoice = pool.get('account.invoice')
|
||||
InvoiceLine = pool.get('account.invoice.line')
|
||||
domain = ['OR',
|
||||
('origin', '=', str(self.origin)),
|
||||
]
|
||||
if isinstance(self.origin, Sale):
|
||||
domain.append(('origin.sale', '=', self.origin.id, 'sale.line'))
|
||||
elif isinstance(self.origin, SaleLine):
|
||||
domain.append(('origin', '=', str(self.origin.sale)))
|
||||
elif isinstance(self.origin, Invoice):
|
||||
domain.append(
|
||||
('origin.invoice', '=', self.origin.id,
|
||||
'account.invoice.line'))
|
||||
elif isinstance(self.origin, InvoiceLine):
|
||||
domain.append(('origin', '=', str(self.origin.invoice)))
|
||||
return [
|
||||
domain,
|
||||
('id', '!=', self.id),
|
||||
]
|
||||
|
||||
|
||||
class Action(ModelSQL, ModelView):
|
||||
'Customer Complaint Action'
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
# this repository contains the full copyright notices and license terms.
|
||||
|
||||
from trytond.exceptions import UserWarning
|
||||
|
||||
|
||||
class ComplaintSimilarWarning(UserWarning):
|
||||
pass
|
||||
@@ -3,6 +3,9 @@
|
||||
this repository contains the full copyright notices and license terms. -->
|
||||
<tryton>
|
||||
<data grouped="1">
|
||||
<record model="ir.message" id="msg_complaint_similar">
|
||||
<field name="text">The complaint "%(complaint)s" is similar to complaint "%(similar)s".</field>
|
||||
</record>
|
||||
<record model="ir.message" id="msg_complaint_delete_draft">
|
||||
<field name="text">To delete complaint "%(complaint)s" you must reset it to draft state.</field>
|
||||
</record>
|
||||
|
||||
@@ -162,6 +162,11 @@ Create a complaint to return partially the sale::
|
||||
>>> complaint.state
|
||||
'draft'
|
||||
>>> complaint.click('wait')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ComplaintSimilarWarning: ...
|
||||
>>> config.skip_warning = True
|
||||
>>> complaint.click('wait')
|
||||
>>> complaint.state
|
||||
'waiting'
|
||||
>>> complaint.click('approve')
|
||||
|
||||
Reference in New Issue
Block a user