Add export product picture function,
Export the product picture of the sales order to excel
thank you.
I can pay, but not too much
line.product_id.image_small# -*- coding: utf-8 -*-
from odoo import api, fields, modelsimport base64from io import BytesIOfrom dateutil import parserimport datetimefrom datetime import datetime, timedeltafrom odoo.exceptions import Warning
try: import xlwt from xlwt import Bordersexcept ImportError: xlwt = Nonetry: import base64except ImportError: _logger.debug('Cannot `import base64`.')
class YWTExportSaleOrder(models.TransientModel): # Model Name _name = "ywt.export.sale.order"
# Field Declaration datas = fields.Binary(string='File')
@api.multi def ywt_exprot_sale_order(self): sale_order_obj = self.env['sale.order'] active_id = self.ids[0]
today = datetime.now().strftime("%Y-%m-%d") file_name = 'Export Sale Order' + ' ' + today
sale_order_ids = self._context.get('active_ids', []) if not sale_order_ids: raise Warning(_('Please Select At least One Sale Order to Export')) if sale_order_ids: custom_value = {} sale_order_ids_ls = sale_order_obj.search([('id', 'in', sale_order_ids)])
workbook, header_bold, value_style = self.ywt_prepare_design()
for order in sale_order_ids_ls: sheet = workbook.add_sheet(order.name) order_lst = [] for line in order.order_line: order_line = {} order_line['product_name'] = line.product_id.name order_line['product_description'] = line.name
# Add display pictures order_line['product_image'] = line.product_id.image_small
order_line['order_qty'] = line.product_uom_qty order_line['qty_delivered'] = line.qty_delivered order_line['qty_invoiced'] = line.qty_invoiced order_line['price_unit'] = line.price_total order_line['price_subunit'] = str(line.price_subtotal) + ' ' + line.currency_id.symbol order_lst.append(order_line)
custom_value['products'] = order_lst custom_value['partner_id'] = order.partner_id.name custom_value[ 'partner_street'] = order.partner_id and order.partner_id.country_id.name + order.partner_id.city + order.partner_id.street # custom_value['patner_city'] = order.partner_id.city # custom_value['patner_country'] = order.partner_id and order.partner_id.country_id.name custom_value['partner_phone'] = order.partner_id.phone custom_value['partner_mobile'] = order.partner_id.mobile
custom_value['date_order'] = str(order.confirmation_date.strftime("%Y-%m-%d")) custom_value['amount_total'] = str(order.amount_total) + ' ' + order.currency_id.symbol custom_value['amount_untaxed'] = str(order.amount_untaxed) + ' ' + order.currency_id.symbol custom_value['amount_tax'] = str(order.amount_tax) + ' ' + order.currency_id.symbol
sheet.write_merge(10, 10, 0, 1, "Product Name", header_bold) sheet.write_merge(10, 10, 2, 3, "Description", header_bold)
sheet.write_merge(10, 10, 6, 6, "Product Image", header_bold)
sheet.write_merge(10, 10, 7, 8, "Ordered Quantity", header_bold) sheet.write_merge(10, 10, 9, 10, "Unit Price", header_bold) sheet.write(10, 11, 'Sub Total', header_bold)
row = 11 col = 1 for product in custom_value['products']: sheet.write_merge(row, row, 0, 1, product['product_name'], value_style) sheet.write_merge(row, row, 2, 3, product['product_description'], value_style) # sheet.write_merge(row, row, 6, 6, product['product_image'], value_style) sheet.write_merge(row, row, 7, 8, product['order_qty'], value_style) sheet.write_merge(row, row, 9, 10, product['price_unit'], value_style) sheet.write(row, 11, product['price_subunit'], value_style) row += 1; col += 1
sheet.write(5, 1, 'Customer', header_bold) sheet.write(5, 2, custom_value['partner_id']) sheet.write(6, 2, custom_value['partner_street']) sheet.write(7, 2, custom_value['partner_phone']) sheet.write(8, 2, custom_value['partner_mobile']) sheet.write_merge(5, 5, 8, 9, 'Order Date', header_bold) sheet.write_merge(5, 5, 10, 11, custom_value['date_order'])
sheet.write_merge(row + 1, row + 1, 9, 10, 'Untaxed Amount', header_bold) sheet.write(row + 1, 11, custom_value['amount_untaxed'], value_style) sheet.write_merge(row + 2, row + 2, 9, 10, 'Taxes', header_bold) sheet.write(row + 2, 11, custom_value['amount_tax'], value_style) sheet.write_merge(row + 3, row + 3, 9, 10, 'Total', header_bold) sheet.write(row + 3, 11, custom_value['amount_total'], value_style)
fp = BytesIO() workbook.save(fp) fp.seek(0) sale_file = base64.encodestring(fp.read()) fp.close() self.write({'datas': sale_file}) if self.datas: return { 'type': 'ir.actions.act_url', 'url': 'web/content/?download=1&model=ywt.export.sale.order&field=datas&id=%s&filename=%s.xls' % ( active_id, file_name), 'target': 'self', }
@api.multi def ywt_prepare_design(self): workbook = xlwt.Workbook() borders = Borders()
header_border = Borders() header_border.left, header_border.right, header_border.top, header_border.bottom = Borders.THIN, Borders.THIN, Borders.THIN, Borders.THICK
borders.left, borders.right, borders.top, borders.bottom = Borders.THIN, Borders.THIN, Borders.THIN, Borders.THIN
header_bold = xlwt.easyxf( "font: bold on, height 200; pattern: pattern solid, fore_colour gray25;alignment: horizontal center ,vertical center") header_bold.borders = header_border
value_style = xlwt.easyxf( "font: height 200, name Arial; alignment: horizontal center ,vertical center; borders: top thin,right thin,bottom thin,left thin")
return workbook, header_bold, value_style