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.

86 lines
2.8 KiB

  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, "
  26. "no email address found for the client"
  27. )
  28. )
  29. email_values = {}
  30. if email:
  31. email_values["email_to"] = email
  32. else:
  33. email_values["email_to"] = order.partner_id.email
  34. receipt = (
  35. "<main><div class='article'><div class='pos'>"
  36. "<div class='pos-receipt-container'>"
  37. "{}</div></div></div></main>".format(body_from_ui)
  38. )
  39. bodies, html_ids, header, footer, specific_paperformat_args = self.env[
  40. "ir.actions.report"
  41. ]._prepare_html(receipt)
  42. base64_pdf = self.env["ir.actions.report"]._run_wkhtmltopdf(
  43. bodies,
  44. landscape=False,
  45. specific_paperformat_args=specific_paperformat_args,
  46. )
  47. attachment = self.env["ir.attachment"].create(
  48. {
  49. "name": pos_reference,
  50. "datas_fname": _("Receipt_{}.pdf".format(pos_reference)),
  51. "type": "binary",
  52. "mimetype": "application/x-pdf",
  53. "db_datas": base64.encodestring(base64_pdf),
  54. "res_model": "pos.order",
  55. "res_id": order.id,
  56. }
  57. )
  58. email_values["attachment_ids"] = [attachment.id]
  59. mail_template = self.env.ref("pos_mail_receipt.email_send_ticket")
  60. mail_template.send_mail(
  61. order.id, force_send=force, email_values=email_values,
  62. )
  63. order.email_receipt_sent = True
  64. @api.model
  65. def create_from_ui(self, orders):
  66. res = super(PosOrder, self).create_from_ui(orders)
  67. for order in orders:
  68. if "email" in order["data"]:
  69. self.send_mail_receipt(
  70. order["data"]["name"],
  71. order["data"]["email"],
  72. order["data"]["body_from_ui"],
  73. force=False,
  74. )
  75. return res