|
| 1 | +import base64 |
| 2 | +from odoo import http |
| 3 | +from odoo.http import request |
| 4 | +from odoo.exceptions import ValidationError |
| 5 | + |
| 6 | + |
| 7 | +class SupplierPortal(http.Controller): |
| 8 | + @http.route("/supplier/upload", type="http", auth="user", website=True) |
| 9 | + def supplier_portal_upload_document(self): |
| 10 | + return request.render("supplier_portal.supplier_portal_upload_template") |
| 11 | + |
| 12 | + @http.route("/supplier/portal/submit", type="http", auth="user", website=True) |
| 13 | + def supplier_portal_submit_document(self, **kwargs): |
| 14 | + """Handles supplier document upload by creating a vendor bill (`account.move`) and attaching uploaded PDF and XML files.""" |
| 15 | + |
| 16 | + bills = ( |
| 17 | + request.env["account.move"] |
| 18 | + .sudo() |
| 19 | + .create( |
| 20 | + { |
| 21 | + "partner_id": request.env.user.partner_id.id, |
| 22 | + "move_type": "in_invoice", |
| 23 | + "company_id": int(kwargs.get("company")), |
| 24 | + } |
| 25 | + ) |
| 26 | + ) |
| 27 | + # Handle PDF Upload |
| 28 | + pdf_file = kwargs.get("pdf_file") |
| 29 | + if pdf_file: |
| 30 | + request.env["ir.attachment"].sudo().create( |
| 31 | + { |
| 32 | + "res_id": bills.id, |
| 33 | + "res_model": "account.move", |
| 34 | + "name": pdf_file.filename or "Uploaded PDF", |
| 35 | + "datas": base64.b64encode(pdf_file.read()), |
| 36 | + "mimetype": "application/pdf", |
| 37 | + "type": "binary", |
| 38 | + } |
| 39 | + ) |
| 40 | + # Handle XML Upload |
| 41 | + xml_file = kwargs.get("xml_file") |
| 42 | + if xml_file: |
| 43 | + request.env["ir.attachment"].sudo().create( |
| 44 | + { |
| 45 | + "res_id": bills.id, |
| 46 | + "res_model": "account.move", |
| 47 | + "name": xml_file.filename or "Uploaded XML", |
| 48 | + "datas": base64.b64encode(xml_file.read()), |
| 49 | + "mimetype": "application/xml", |
| 50 | + "type": "binary", |
| 51 | + } |
| 52 | + ) |
| 53 | + # Redirect based on whether files were uploaded |
| 54 | + if pdf_file and xml_file: |
| 55 | + return request.render( |
| 56 | + "supplier_portal.after_submit_message_template", |
| 57 | + {"success_message": "Documents uploaded successfully."}, |
| 58 | + ) |
| 59 | + else: |
| 60 | + return request.render( |
| 61 | + "supplier_portal.after_submit_message_template", |
| 62 | + {"error_message": "Please upload both pdf and xml files."}, |
| 63 | + ) |
0 commit comments