def amount_to_text(self, amount): self.ensure_one() def _num2words(number, lang): try: return num2words(number, lang=lang).title() except NotImplementedError: return num2words(number, lang='en').title() if num2words is None: logging.getLogger(__name__).warning("The library 'num2words' is missing, cannot render textual amounts.") return "" formatted = "%.{0}f".format(self.decimal_places) % amount parts = formatted.partition('.') integer_value = int(parts[0]) fractional_value = int(parts[2] or 0) lang = tools.get_lang(self.env) amount_words = tools.ustr('{amt_value} {amt_word}').format( amt_value=_num2words(integer_value, lang=lang.iso_code), amt_word=self.currency_unit_label, ) if not self.is_zero(amount - integer_value): amount_words += ' ' + _('and') + tools.ustr(' {amt_value} {amt_word}').format( amt_value=_num2words(fractional_value, lang=lang.iso_code), amt_word=self.currency_subunit_label, ) return amount_words
def _message_send_mail(self, body, notif_template_xmlid, message_values, notif_values, mail_values, force_send=False, **kwargs): """ Shortcut to send an email. """ default_lang = get_lang(self.env, lang_code=kwargs.get('lang')).code lang = kwargs.get('lang', default_lang) sign_request = self.with_context(lang=lang) # the notif layout wrapping expects a mail.message record, but we don't want # to actually create the record # See @tde-banana-odoo for details msg = sign_request.env['mail.message'].sudo().new( dict(body=body, **message_values)) notif_layout = sign_request.env.ref(notif_template_xmlid) body_html = notif_layout._render(dict(message=msg, **notif_values), engine='ir.qweb', minimal_qcontext=True) body_html = sign_request.env['mail.render.mixin']._replace_local_links( body_html) mail = sign_request.env['mail.mail'].sudo().create( dict(body_html=body_html, state='outgoing', **mail_values)) if force_send: mail.send() return mail
def _name_search(self, name, args=None, operator='ilike', limit=100, name_get_uid=None): if name and operator == '=' and not args: # search on the name of the pricelist and its currency, opposite of name_get(), # Used by the magic context filter in the product search view. query_args = { 'name': name, 'limit': limit, 'lang': get_lang(self.env).code } query = """SELECT p.id FROM (( SELECT pr.id, pr.name FROM product_pricelist pr JOIN res_currency cur ON (pr.currency_id = cur.id) WHERE pr.name || ' (' || cur.name || ')' = %(name)s ) UNION ( SELECT tr.res_id as id, tr.value as name FROM ir_translation tr JOIN product_pricelist pr ON ( pr.id = tr.res_id AND tr.type = 'model' AND tr.name = 'product.pricelist,name' AND tr.lang = %(lang)s ) JOIN res_currency cur ON (pr.currency_id = cur.id) WHERE tr.value || ' (' || cur.name || ')' = %(name)s ) ) p ORDER BY p.name""" if limit: query += " LIMIT %(limit)s" self._cr.execute(query, query_args) ids = [r[0] for r in self._cr.fetchall()] # regular search() to apply ACLs - may limit results below limit in some cases pricelist_ids = self._search([('id', 'in', ids)], limit=limit, access_rights_uid=name_get_uid) if pricelist_ids: return pricelist_ids return super()._name_search(name, args, operator=operator, limit=limit, name_get_uid=name_get_uid)
def send_signature_accesses(self, subject=None, message=None): base_url = self.env['ir.config_parameter'].sudo().get_param( 'web.base.url') tpl = self.env.ref('sign.sign_template_mail_request') for signer in self: if not signer.partner_id or not signer.partner_id.email: continue if not signer.create_uid.email: continue signer_lang = get_lang(self.env, lang_code=signer.partner_id.lang).code tpl = tpl.with_context(lang=signer_lang) body = tpl._render( { 'record': signer, 'link': url_join( base_url, "sign/document/mail/%(request_id)s/%(access_token)s" % { 'request_id': signer.sign_request_id.id, 'access_token': signer.access_token }), 'subject': subject, 'body': message if message != '<p><br></p>' else False, }, engine='ir.qweb', minimal_qcontext=True) if not signer.signer_email: raise UserError( _("Please configure the signer's email address")) self.env['sign.request']._message_send_mail( body, 'mail.mail_notification_light', {'record_name': signer.sign_request_id.reference}, { 'model_description': 'signature', 'company': signer.create_uid.company_id }, { 'email_from': signer.create_uid.email_formatted, 'author_id': signer.create_uid.partner_id.id, 'email_to': signer.partner_id.email_formatted, 'subject': subject }, force_send=True, lang=signer_lang, )
def _get_cell_type_value(self, cell): if 'date' not in cell.get('class', '') or not cell.get('name'): # cell is not a date return ('text', cell.get('name', '')) if isinstance(cell['name'], (float, datetime.date, datetime.datetime)): # the date is xlsx compatible return ('date', cell['name']) try: # the date is parsable to a xlsx compatible date lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env) return ('date', datetime.strptime(cell['name'], lg.date_format)) except: # the date is not parsable thus is returned as text return ('text', cell['name'])
def send_follower_accesses(self, followers, subject=None, message=None): self.ensure_one() base_url = self.env['ir.config_parameter'].sudo().get_param( 'web.base.url') tpl = self.env.ref('sign.sign_template_mail_follower') for follower in followers: if not follower.email: continue if not self.create_uid.email: raise UserError( _("Please configure the sender's email address")) tpl_follower = tpl.with_context( lang=get_lang(self.env, lang_code=follower.lang).code) body = tpl_follower._render( { 'record': self, 'link': url_join( base_url, 'sign/document/%s/%s' % (self.id, self.access_token)), 'subject': subject, 'body': message, }, engine='ir.qweb', minimal_qcontext=True) self.env['sign.request']._message_send_mail( body, 'mail.mail_notification_light', {'record_name': self.reference}, { 'model_description': 'signature', 'company': self.create_uid.company_id }, { 'email_from': self.create_uid.email_formatted, 'author_id': self.create_uid.partner_id.id, 'email_to': follower.email_formatted, 'subject': subject or _('%s : Signature request', self.reference) }, lang=follower.lang, ) self.message_subscribe(partner_ids=follower.ids)
def _compute_price_unit_and_date_planned_and_name(self): po_lines_without_requisition = self.env['purchase.order.line'] for pol in self: if pol.product_id.id not in pol.order_id.requisition_id.line_ids.product_id.ids: po_lines_without_requisition |= pol continue for line in pol.order_id.requisition_id.line_ids: if line.product_id == pol.product_id: pol.price_unit = line.product_uom_id._compute_price(line.price_unit, pol.product_uom) partner = pol.order_id.partner_id or pol.order_id.requisition.vendor_id product_ctx = {'seller_id': partner.id, 'lang': get_lang(pol.env, partner.lang).code} name = pol._get_product_purchase_description(pol.product_id.with_context(product_ctx)) if line.product_description_variants: name += '\n' + line.product_description_variants pol.name = name break super(PurchaseOrderLine, po_lines_without_requisition)._compute_price_unit_and_date_planned_and_name()
def send_completed_document(self): self.ensure_one() if len(self.request_item_ids) <= 0 or self.state != 'signed': return False if not self.completed_document: self.generate_completed_document() base_url = self.env['ir.config_parameter'].sudo().get_param( 'web.base.url') attachment = self.env['ir.attachment'].create({ 'name': "%s.pdf" % self.reference if self.reference.split('.')[-1] != 'pdf' else self.reference, 'datas': self.completed_document, 'type': 'binary', 'res_model': self._name, 'res_id': self.id, }) report_action = self.env.ref('sign.action_sign_request_print_logs') # print the report with the public user in a sudoed env # public user because we don't want groups to pollute the result # (e.g. if the current user has the group Sign Manager, # some private information will be sent to *all* signers) # sudoed env because we have checked access higher up the stack public_user = self.env.ref('base.public_user', raise_if_not_found=False) if not public_user: # public user was deleted, fallback to avoid crash (info may leak) public_user = self.env.user pdf_content, __ = report_action.with_user( public_user).sudo()._render_qweb_pdf(self.id) attachment_log = self.env['ir.attachment'].create({ 'name': "Certificate of completion - %s.pdf" % time.strftime('%Y-%m-%d - %H:%M:%S'), 'datas': base64.b64encode(pdf_content), 'type': 'binary', 'res_model': self._name, 'res_id': self.id, }) tpl = self.env.ref('sign.sign_template_mail_completed') for signer in self.request_item_ids: if not signer.signer_email: continue signer_lang = get_lang(self.env, lang_code=signer.partner_id.lang).code tpl = tpl.with_context(lang=signer_lang) body = tpl._render( { 'record': self, 'link': url_join( base_url, 'sign/document/%s/%s' % (self.id, signer.access_token)), 'subject': '%s signed' % self.reference, 'body': False, }, engine='ir.qweb', minimal_qcontext=True) if not self.create_uid.email: raise UserError( _("Please configure the sender's email address")) if not signer.signer_email: raise UserError( _("Please configure the signer's email address")) self.env['sign.request']._message_send_mail( body, 'mail.mail_notification_light', {'record_name': self.reference}, { 'model_description': 'signature', 'company': self.create_uid.company_id }, { 'email_from': self.create_uid.email_formatted, 'author_id': self.create_uid.partner_id.id, 'email_to': signer.partner_id.email_formatted, 'subject': _('%s has been signed', self.reference), 'attachment_ids': [(4, attachment.id), (4, attachment_log.id)] }, force_send=True, lang=signer_lang, ) tpl = self.env.ref('sign.sign_template_mail_completed') for follower in self.mapped('message_follower_ids.partner_id' ) - self.request_item_ids.mapped( 'partner_id'): if not follower.email: continue if not self.create_uid.email: raise UserError( _("Please configure the sender's email address")) tpl_follower = tpl.with_context( lang=get_lang(self.env, lang_code=follower.lang).code) body = tpl._render( { 'record': self, 'link': url_join( base_url, 'sign/document/%s/%s' % (self.id, self.access_token)), 'subject': '%s signed' % self.reference, 'body': '', }, engine='ir.qweb', minimal_qcontext=True) self.env['sign.request']._message_send_mail( body, 'mail.mail_notification_light', {'record_name': self.reference}, { 'model_description': 'signature', 'company': self.create_uid.company_id }, { 'email_from': self.create_uid.email_formatted, 'author_id': self.create_uid.partner_id.id, 'email_to': follower.email_formatted, 'subject': _('%s has been signed', self.reference) }, lang=follower.lang, ) return True