Skip to Content
Menu
This question has been flagged
2 Replies
5379 Views

I am trying to print some fields of account.invoice and account.invoice.line

Firstly I added the button for printing csv formatted report.

this is i trying in code


def print_csv(self):      
    lines = []
    lines.append((['Product Name','Price Unit','Quantity']))
    for rec in self.invoice_line_ids:  
      lines.append(([rec.product_id.name,rec.price_unit,rec.quantity]))        
    with open('invoice_details.csv', 'w') as fp:
        a = csv.writer(fp, delimiter=',')            
        data_lines = lines
        a.writerows(data_lines)

the csv file file is downloaded in not browser,it is downloaded in the default location.

How to redirect to the file to download in web browser??

I know this can implemented using controllers in odoo. is there any good example of controller to print csv report??

Avatar
Discard
Best Answer

Hi acha aslam,

There are various tricks and logic are available to print csv report.

Just refer this link: https://stackoverflow.com/questions/48049787/how-to-print-csv-report-in-browser-odoo

https://www.odoo.com/forum/help-1/question/how-to-export-product-quantities-to-a-csv-file-4104

Accept answer if helpful.

Thank you.

Avatar
Discard
Best Answer

Hi, here is an example of how I did it using controllers.

Here 'phone.reputation' is a custom model I developed. You can give 'account.invoice', and

give which all fields you want to be printed in the csv file. "name" is the actual field name and "label" is the label how you want that field to have in the csv file. "ids" is the list of ids of records that you want to be printed in the csv  file.

@api.multi
def download_phone_reputation_data(self):
    for intel in self:
        str_dic = """{"model":"phone.reputation",
                            "fields":[{"name":"phone_number","label":"Phone Number"},
                                            {"name":"country_code","label":"Country Code"},
                                            {"name":"country_calling_code","label":"Country Calling Code"},
                                            {"name":"reputation_level","label":"Reputation Level"}, 
                                            {"name":"score","label":"Score"},
                                            {"name":"type","label":"Type"},
                                            {"name":"category","label":"Category"},
                                            {"name":"volume_score","label":"Volume Score"}, 
                                            {"name":"report_count","label":"Report Count"},
                                            {"name":"error","label":"Error"},
                                            {"name":"warnings","label":"Warnings"}],
                                "ids":%s,
                                "domain":[],
                                "context":{},
                                "import_compat":0}""" % intel.phone_reputation_ids.ids
        return {
                'type': 'ir.actions.act_url',
                'url': '/web/export/csv?data=%s&token=' % (str_dic),
                'target': 'current',
                    }


Thanks

Avatar
Discard
Related Posts Replies Views Activity
1
Aug 17
6194
2
Nov 24
25059
2
May 24
5510
3
Mar 24
4960
0
Mar 24
261