|
| 1 | +# Copyright 2026 ForgeFlow S.L. |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 3 | + |
| 4 | +import base64 |
| 5 | + |
| 6 | +from odoo import http |
| 7 | +from odoo.http import request |
| 8 | + |
| 9 | + |
| 10 | +class ProductInquiryController(http.Controller): |
| 11 | + @http.route( |
| 12 | + "/shop/product/inquiry", |
| 13 | + type="http", |
| 14 | + auth="public", |
| 15 | + methods=["POST"], |
| 16 | + website=True, |
| 17 | + csrf=False, |
| 18 | + ) |
| 19 | + def product_inquiry(self, **kwargs): |
| 20 | + product_id = kwargs.get("product_id", "").strip() |
| 21 | + contact_name = kwargs.get("contact_name", "").strip() |
| 22 | + email = kwargs.get("email", "").strip() |
| 23 | + phone = kwargs.get("phone", "").strip() |
| 24 | + company_name = kwargs.get("company_name", "").strip() |
| 25 | + inquiry_type = kwargs.get("inquiry_type", "more_info") |
| 26 | + question = kwargs.get("question", "").strip() |
| 27 | + |
| 28 | + if not product_id or not contact_name or not email or not question: |
| 29 | + return request.make_json_response( |
| 30 | + {"success": False, "error": "Required fields are missing."}, |
| 31 | + status=400, |
| 32 | + ) |
| 33 | + try: |
| 34 | + product_id = int(product_id) |
| 35 | + except (ValueError, TypeError): |
| 36 | + return request.make_json_response( |
| 37 | + {"success": False, "error": "Invalid product."}, status=400 |
| 38 | + ) |
| 39 | + variant = request.env["product.product"].sudo().browse(product_id) |
| 40 | + if not variant.exists(): |
| 41 | + return request.make_json_response( |
| 42 | + {"success": False, "error": "Product not found."}, status=404 |
| 43 | + ) |
| 44 | + description = f"<p>{question}</p>" |
| 45 | + lead_vals = { |
| 46 | + "name": f"Product inquiry: {variant.product_tmpl_id.name}", |
| 47 | + "contact_name": contact_name, |
| 48 | + "email_from": email, |
| 49 | + "phone": phone or False, |
| 50 | + "partner_name": company_name or False, |
| 51 | + "product_id": variant.id, |
| 52 | + "inquiry_type": inquiry_type, |
| 53 | + "description": description, |
| 54 | + "type": "lead", |
| 55 | + "company_id": request.website.company_id.id, |
| 56 | + } |
| 57 | + salesperson_id = self._get_product_salesperson(variant) # pylint: disable=assignment-from-none |
| 58 | + if salesperson_id is not None: |
| 59 | + lead_vals["user_id"] = salesperson_id or False |
| 60 | + lead = request.env["crm.lead"].sudo().create(lead_vals) |
| 61 | + attachment_file = kwargs.get("attachment") |
| 62 | + if attachment_file and hasattr(attachment_file, "read"): |
| 63 | + data = attachment_file.read() |
| 64 | + if data: |
| 65 | + request.env["ir.attachment"].sudo().create( |
| 66 | + { |
| 67 | + "name": attachment_file.filename, |
| 68 | + "datas": base64.b64encode(data), |
| 69 | + "res_model": "crm.lead", |
| 70 | + "res_id": lead.id, |
| 71 | + } |
| 72 | + ) |
| 73 | + return request.make_json_response({"success": True}) |
| 74 | + |
| 75 | + def _get_product_salesperson(self, variant): |
| 76 | + """Return salesperson assignment for the lead. |
| 77 | + - Return a user ID to assign that user. |
| 78 | + - Return False to explicitly leave user_id blank. |
| 79 | + - Return None (default) to let the model default apply. |
| 80 | + """ |
| 81 | + return None |
0 commit comments