You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. # Copyright 2019 Coop IT Easy SCRLfs
  2. # @author Pierrick Brun <pierrick.brun@akretion.com>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. import logging
  5. import base64
  6. from odoo import fields, models, api, _
  7. _logger = logging.getLogger(__name__)
  8. class PosOrder(models.Model):
  9. _inherit = "pos.order"
  10. email_receipt_sent = fields.Boolean()
  11. @api.model
  12. def send_mail_receipt(
  13. self, pos_reference, email, body_from_ui, force=True
  14. ):
  15. order = self.search([("pos_reference", "=", pos_reference)])
  16. if len(order) < 1:
  17. _logger.error(_("Error: no order found"))
  18. return
  19. if order.email_receipt_sent:
  20. _logger.info(_("E-mail already sent"))
  21. return
  22. if not email and not order.partner_id and not order.partner_id.email:
  23. _logger.error(
  24. _(
  25. "Cannot send the ticket, no email address found for the client"
  26. )
  27. )
  28. mail_template = self.env.ref("pos_mail_receipt.email_send_ticket")
  29. email_values = {}
  30. if email:
  31. email_values["email_to"] = email
  32. else:
  33. email_values["email_to"] = order.partner_id.email
  34. base64_pdf = self.env["ir.actions.report"]._run_wkhtmltopdf(
  35. [body_from_ui.encode("utf-16")],
  36. landscape=False,
  37. specific_paperformat_args={
  38. "data-report-margin-top": 10,
  39. "data-report-header-spacing": 10,
  40. },
  41. )
  42. attachment = self.env["ir.attachment"].create(
  43. {
  44. "name": pos_reference,
  45. "datas_fname": _("Receipt_{}.pdf".format(pos_reference)),
  46. "type": "binary",
  47. "mimetype": "application/x-pdf",
  48. "db_datas": base64.encodestring(base64_pdf),
  49. "res_model": "pos.order",
  50. "res_id": order.id,
  51. }
  52. )
  53. email_values["attachment_ids"] = [attachment.id]
  54. mail_template.send_mail(
  55. order.id, force_send=force, email_values=email_values,
  56. )
  57. order.email_receipt_sent = True
  58. @api.model
  59. def create_from_ui(self, orders):
  60. res = super(PosOrder, self).create_from_ui(orders)
  61. for order in orders:
  62. if "email" in order["data"]:
  63. self.send_mail_receipt(
  64. order["data"]["name"],
  65. order["data"]["email"],
  66. order["data"]["body_from_ui"],
  67. force=False,
  68. )
  69. return res