0

Attachment from website not logging into backend Odoo. Please help.

I have these 3 inputs in the website views.

<input id="customer_po" type="text" class="form-control s_website_form_input" name="client_order_ref"/>
<textarea id="customer_comment" class="form-control s_website_form_input" name="Give us your feedback"/>
<input id="customer_documents" type="file" class="form-control s_website_form_input" name="a_document"/>

I am using this Javascript to call controllers.

odoo.define('test_payment.payment', function(require) {
    "use strict";

    var ajax = require('web.ajax');

    $(document).ready(function() {
        'use strict';
        $('button[name="o_payment_submit_button"]').bind("click", function(ev) {
            var customer_comment = $('#customer_comment').val();
            var client_order_ref = $('#customer_po').val();
            
            var customer_documents_input = $('#customer_documents')[0];
            var customer_documents = customer_documents_input.files;
            var formData = new FormData();
            for (var i = 0; i < customer_documents.length; i++) {
                formData.append('customer_documents', customer_documents[i]);
            }
            
            // Set appropriate options for file upload
            var options = {
                contentType: false,
                processData: false,
            };

            ajax.jsonRpc('/shop/customer_comment/', 'call', {
                'comment': customer_comment,
                'client_order_ref': client_order_ref,
                'customer_documents': formData,
            }, options);
        });
    });
});

And this is how my controller looks like.

"""Add Customer comment functions to the website_sale controller."""
@http.route(['/shop/customer_comment'], type='json', auth="public", methods=['POST'], website=True)
def customer_comment(self, **post):
    """ Json method that used to add a
    comment when the user clicks on 'pay now' button.
    """
    if post.get('comment'):
        order = request.website.sale_get_order()
        redirection = self.checkout_redirection(order)
        if redirection:
            return redirection

        if order and order.id:
            order.write({
                'customer_comment': post.get('comment'),
                'client_order_ref': post.get('client_order_ref'),
            })
            # customer_documents = post.get('formData').getlist('customer_documents')
            customer_documents = request.httprequest.files.getlist('customer_documents')
            attachment_ids = []
            for document in customer_documents:
                attachment_data = {
                    'name': document.filename,
                    'type': 'binary',
                    'res_model': 'sale.order',
                    'res_id': order.id,
                    'datas': document.read(),
                }
                attachment = request.env['ir.attachment'].sudo().create(attachment_data)
                attachment_ids.append(attachment.id)
            order.message_post(attachment_ids=attachment_ids)
    return True

Customer Po and Comments are updated fine, but its not logging attachment, please help what am I missing here.

Ignazio
  • 10,504
  • 1
  • 14
  • 25
Bishal Ghimire
  • 647
  • 7
  • 15

0 Answers0