lims: referral: send email to laboratory

This commit is contained in:
Adrián Bernardi 2022-05-05 20:59:54 -03:00
parent ca898912d4
commit 6eff5a90b9
4 changed files with 134 additions and 6 deletions

View File

@ -229,6 +229,9 @@ class Configuration(ModelSingleton, ModelSQL, ModelView,
'Allow to accept the same analysis with different methods')
results_report_language = fields.Many2One('ir.lang',
'Results Report Language', domain=[('translatable', '=', True)])
mail_referral_subject = fields.Char('Email subject of Referral of Samples',
help="A suffix with the referral number will be added to the text")
mail_referral_body = fields.Text('Email body of Referral of Samples')
@staticmethod
def default_brix_digits():

View File

@ -431,6 +431,14 @@ msgctxt "field:lims.configuration,mail_ack_subject:"
msgid "Email subject of Acknowledgment of Samples Receipt"
msgstr "Asunto del correo de Acuse de recibo de muestras"
msgctxt "field:lims.configuration,mail_referral_body:"
msgid "Email body of Referral of Samples"
msgstr "Cuerpo del correo de Derivación de muestras"
msgctxt "field:lims.configuration,mail_referral_subject:"
msgid "Email subject of Referral of Samples"
msgstr "Asunto del correo de Derivación de muestras"
msgctxt "field:lims.configuration,mcl_fraction_type:"
msgid "MCL fraction type"
msgstr "Tipo de fracción MCL"
@ -6640,6 +6648,10 @@ msgctxt "help:lims.configuration,mail_ack_subject:"
msgid "In the text will be added suffix with the entry report number"
msgstr "En el texto se añadirá sufijo con el número de ingreso del informe"
msgctxt "help:lims.configuration,mail_referral_subject:"
msgid "A suffix with the referral number will be added to the text"
msgstr "Se agregará al texto un sufijo con el número de derivación"
msgctxt "help:lims.configuration,samples_in_progress:"
msgid "Samples allowed for preliminary reports"
msgstr "Muestras permitidas para informes preliminares"
@ -13461,6 +13473,10 @@ msgctxt "view:lims.configuration:"
msgid "Email body of Acknowledgment of Samples Receipt"
msgstr "Cuerpo del correo de Acuse de recibo de muestras"
msgctxt "view:lims.configuration:"
msgid "Email subject of Referral of Samples"
msgstr "Asunto del correo de Derivación de muestras"
msgctxt "view:lims.configuration:"
msgid "Fraction Types"
msgstr "Tipos de fracción"
@ -13473,6 +13489,10 @@ msgctxt "view:lims.configuration:"
msgid "Mail Acknowledgment of Samples Receipt"
msgstr "Email Acuse de Muestras"
msgctxt "view:lims.configuration:"
msgid "Mail Referral of Samples"
msgstr "Email Derivación de muestras"
msgctxt "view:lims.configuration:"
msgid "Reagents"
msgstr "Reactivos"

View File

@ -8,6 +8,10 @@ from datetime import datetime
from dateutil.relativedelta import relativedelta
from decimal import Decimal
from sql.conditionals import Case
from email import encoders
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from trytond.model import ModelView, ModelSQL, fields, Unique, DictSchemaMixin
from trytond.wizard import Wizard, StateTransition, StateView, StateAction, \
@ -19,6 +23,8 @@ from trytond.report import Report
from trytond.exceptions import UserError
from trytond.i18n import gettext
from trytond.rpc import RPC
from trytond.config import config as tconfig
from trytond.tools import get_smtp_server
class Zone(ModelSQL, ModelView):
@ -6817,6 +6823,17 @@ class Referral(ModelSQL, ModelView):
if self.laboratory and self.laboratory.carrier:
self.carrier = self.laboratory.carrier
@classmethod
def create(cls, vlist):
pool = Pool()
Config = pool.get('lims.configuration')
config_ = Config(1)
vlist = [x.copy() for x in vlist]
for values in vlist:
values['number'] = config_.referral_sequence.get()
return super().create(vlist)
@classmethod
@ModelView.button
def send(cls, referrals):
@ -6835,17 +6852,99 @@ class Referral(ModelSQL, ModelView):
EntryDetailAnalysis.write(details, {'state': 'referred'})
cls.write(referrals, {'state': 'sent', 'sent_date': Date.today()})
cls.send_email_laboratory(referrals)
@classmethod
def create(cls, vlist):
def send_email_laboratory(cls, referrals):
from_addr = tconfig.get('email', 'from')
if not from_addr:
return
for referral in referrals:
to_addrs = referral.get_mail_recipients()
if not to_addrs:
continue
subject, body = referral.get_mail_subject_body()
attachment_data = referral.get_mail_attachment()
msg = referral.create_msg(from_addr, to_addrs, subject,
body, attachment_data)
referral.send_msg(from_addr, to_addrs, msg)
def get_mail_recipients(self):
address = self.laboratory.address_get('delivery')
if address:
return [address.email]
return []
def get_mail_subject_body(self):
pool = Pool()
Config = pool.get('lims.configuration')
User = pool.get('res.user')
Lang = pool.get('ir.lang')
vlist = [x.copy() for x in vlist]
config = Config(1)
for values in vlist:
values['number'] = config.referral_sequence.get()
return super().create(vlist)
config_ = Config(1)
lang = User(Transaction().user).language
if not lang:
lang, = Lang.search([
('code', '=', 'en'),
], limit=1)
with Transaction().set_context(language=lang.code):
subject = str('%s %s' % (config_.mail_referral_subject,
self.number)).strip()
body = str(config_.mail_referral_body)
return subject, body
def get_mail_attachment(self):
ReferralReport = Pool().get('lims.referral.report', type='report')
result = ReferralReport.execute([self.id], {})
report_format, report_cache = result[:2]
data = {
'content': report_cache,
'format': report_format,
'mimetype': (report_format == 'pdf' and 'pdf' or
'vnd.oasis.opendocument.text'),
'filename': '%s.%s' % (str(self.number), str(report_format)),
'name': str(self.number),
}
return data
def create_msg(self, from_addr, to_addrs, subject, body, attachment_data):
if not to_addrs:
return None
msg = MIMEMultipart('mixed')
msg['From'] = from_addr
msg['To'] = ', '.join(to_addrs)
msg['Subject'] = subject
msg_body = MIMEText('text', 'plain')
msg_body.set_payload(body.encode('UTF-8'), 'UTF-8')
msg.attach(msg_body)
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(attachment_data['content'])
encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', 'attachment',
filename=attachment_data['filename'])
msg.attach(attachment)
return msg
def send_msg(self, from_addr, to_addrs, msg):
to_addrs = list(set(to_addrs))
success = False
try:
server = get_smtp_server()
server.sendmail(from_addr, to_addrs, msg.as_string())
server.quit()
success = True
except Exception:
logging.getLogger('lims').error(
'Unable to deliver mail for referral %s' % (self.number))
return success
class ReferralReport(Report):

View File

@ -74,6 +74,12 @@
<field name="mail_ack_subject" colspan="4"/>
<field name="mail_ack_body" colspan="4"/>
</page>
<page string="Mail Referral of Samples" id="mail_referral" col="6">
<separator string="Email subject of Referral of Samples"
colspan="4" id="mail_referral_subject"/>
<field name="mail_referral_subject" colspan="4"/>
<field name="mail_referral_body" colspan="4"/>
</page>
<page string="Volume Conversion" id="volume_conversion" col="6">
<label name="brix_digits"/>
<field name="brix_digits"/>