|
| 1 | +import base64 |
| 2 | +from odoo import http, exceptions |
| 3 | +from odoo.http import request |
| 4 | + |
| 5 | +class SupplierPortal(http.Controller): |
| 6 | + @http.route('/supplier_portal', auth='user', type='http', website=True) |
| 7 | + def show_supplier_portal(self, *args, **kwargs): |
| 8 | + return request.render('supplier_portal.supplier_portal_template', {}) |
| 9 | + |
| 10 | + @http.route('/supplier_portal_thank_you', auth='user', type='http', website=True) |
| 11 | + def show_supplier_portal_thank_you(self): |
| 12 | + return request.render('supplier_portal.supplier_portal_thank_you_template') |
| 13 | + |
| 14 | + @http.route('/submit_supplier_portal_form', auth='user', type='http', website=True) |
| 15 | + def submit_supplier_portal_form(self, *args, **kwargs): |
| 16 | + partner_id = request.env.user.partner_id.id |
| 17 | + |
| 18 | + try: |
| 19 | + # Creating invoice |
| 20 | + in_invoice = request.env['account.move'].sudo().create({ |
| 21 | + 'move_type': 'in_invoice', |
| 22 | + 'partner_id': partner_id, |
| 23 | + 'company_id': int(kwargs.get('company_id')), |
| 24 | + }) |
| 25 | + |
| 26 | + # Creating attachments |
| 27 | + request.env['ir.attachment'].sudo().create({ |
| 28 | + 'datas': base64.b64encode(kwargs.get('pdf_file').read()), |
| 29 | + 'type': 'binary', |
| 30 | + 'name': 'invoice.pdf', |
| 31 | + 'mimetype': 'application/pdf', |
| 32 | + 'res_model': 'account.move', |
| 33 | + 'res_id': in_invoice.id, |
| 34 | + }) |
| 35 | + request.env['ir.attachment'].sudo().create({ |
| 36 | + 'datas': base64.b64encode(kwargs.get('xml_file').read()), |
| 37 | + 'type': 'binary', |
| 38 | + 'name': "invoice.xml", |
| 39 | + 'mimetype': 'text/xml', |
| 40 | + 'res_model': 'account.move', |
| 41 | + 'res_id': in_invoice.id, |
| 42 | + }) |
| 43 | + |
| 44 | + return request.redirect("/supplier_portal_thank_you") |
| 45 | + |
| 46 | + except Exception as e: |
| 47 | + raise exceptions.UserError( |
| 48 | + f"An unexpected error occurred while processing the invoice: {str(e)}" |
| 49 | + ) |
0 commit comments