Skip to content
Permalink
Browse files

[ADD] mrp: Add new Xlsx report to allow the user to export the Xlsx f…

…ile from the 'Bom Structure and cost' report.

-In the component if there is a vendor and vendor cost defined then export the vendor name and vendor cost in the xlsx report
  • Loading branch information...
anr-odoo committed Apr 15, 2019
1 parent 8c6eaa5 commit 24bfa3d104bd838e45a7f74da65c3a0b95854b0c
@@ -4,6 +4,7 @@
from . import models
from . import wizard
from . import report
from . import controller

from odoo import api, SUPERUSER_ID

@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from . import main
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import io
import xlsxwriter

from odoo import http, _
from odoo.http import content_disposition, request


class BomReportController(http.Controller):
@http.route('/bom_report_xslx/<int:bom_id>', type='http')
def get_report_xlsx(self, bom_id, quantity, variant, report_name, **kw):
response = request.make_response(
None,
headers=[
('Content-Type', 'application/vnd.ms-excel'),
('Content-Disposition', content_disposition('bom_structure_sheet.xlsx'))
]
)
data = request.env['report.mrp.report_bom_structure']._get_report_values([bom_id], {'quantity': quantity, 'variant': variant, 'unfold': False, 'report_name': report_name})
self.prepare_xlsx_sheet({'data': data, 'report_name': report_name}, response)
return response

def prepare_xlsx_sheet(self, data_dict, response):
output = io.BytesIO()
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
sheet = workbook.add_worksheet('bom_structure_sheet')

default_col1_style = workbook.add_format({'font_name': 'Arial', 'font_size': 12, 'font_color': '#666666', 'indent': 2})
default_style = workbook.add_format({'font_name': 'Arial', 'font_size': 12, 'font_color': '#666666'})
title_style = workbook.add_format({'font_name': 'Arial', 'bold': True, 'bottom': 2})
level_0_style = workbook.add_format({'font_name': 'Arial', 'bold': True, 'font_size': 13, 'bottom': 6, 'font_color': '#666666'})
level_1_col1_style = workbook.add_format({'font_name': 'Arial', 'bold': True, 'font_size': 13, 'bottom': 1, 'font_color': '#666666', 'indent': 1})
level_1_style = workbook.add_format({'font_name': 'Arial', 'bold': True, 'font_size': 13, 'bottom': 1, 'font_color': '#666666'})
level_2_col1_style = workbook.add_format({'font_name': 'Arial', 'bold': True, 'font_size': 12, 'font_color': '#666666', 'indent': 2})
level_2_style = workbook.add_format({'font_name': 'Arial', 'bold': True, 'font_size': 12, 'font_color': '#666666'})
level_3_col1_style = workbook.add_format({'font_name': 'Arial', 'font_size': 12, 'font_color': '#666666', 'indent': 3})
level_3_style = workbook.add_format({'font_name': 'Arial', 'font_size': 12, 'font_color': '#666666'})

#Set the first column width to 50
sheet.set_column(0, 0, 50)
sheet.set_column(4, 4, 10)
sheet.set_column(1, 1, 25)
y_offset = 1
x = 0

data = data_dict.get('data')
report_name = data_dict.get('report_name')
docs = data['docs'][0]
lines = docs['lines']

if report_name == 'bom_structure':
header = {'name': _('Product'), 'code': _('Code'), 'quantity': _('Quantity'), 'umo': _('Unit'), 'vendor': _('Vendor'), 'vendor_cost': _('Vendor Cost'), 'type': _('Type'), 'level': _('Level'), 'prod_cost': _('Product Cost')}
elif report_name == 'bom_cost':
header = {'name': _('Product'), 'code': _('Code'), 'quantity': _('Quantity'), 'umo': _('Unit'), 'vendor': _('Vendor'), 'vendor_cost': _('Vendor Cost'), 'type': _('Type'), 'level': _('Level'), 'total': _('Total')}
else:
header = {'name': _('Product'), 'code': _('Code'), 'quantity': _('Quantity'), 'umo': _('Unit'), 'vendor': _('Vendor'), 'vendor_cost': _('Vendor Cost'), 'type': _('Type'), 'level': _('Level'), 'prod_cost': _('Product Cost'), 'total': _('Total')}

#header
for header_key in header:
sheet.write(y_offset, list(header.keys()).index(header_key), header[header_key], title_style)

y_offset += 1
for line_key in header:
sheet.write(y_offset, list(header.keys()).index(line_key), docs.get(line_key, ''), level_0_style)

y_offset += 1
for y in range(0, len(lines)):
level = lines[y].get('level')
if level == 0:
y_offset += 1
style = level_0_style
col1_style = style
elif level == 1:
style = level_1_style
col1_style = level_1_col1_style
elif level == 2:
style = level_2_style
col1_style = level_2_col1_style
elif level == 3:
style = level_3_style
col1_style = level_3_col1_style
else:
style = default_style
col1_style = default_col1_style

for x in header:
sheet.write(y+y_offset, list(header.keys()).index(x), lines[y].get(x, ''), list(header.keys()).index(x) > 0 and style or col1_style)

level_index = list(header.keys()).index('level')
sheet.write(y_offset + len(lines), level_index, 'Unit Cost', level_1_style)

if report_name == 'bom_structure':
sheet.write(y_offset + len(lines), list(header.keys()).index('prod_cost'), docs.get('prod_cost'), level_1_style)
elif report_name == 'bom_cost':
sheet.write(y_offset + len(lines), list(header.keys()).index('total'), docs.get('total'), level_1_style)
else:
sheet.write(y_offset + len(lines), list(header.keys()).index('prod_cost'), docs.get('prod_cost'), level_1_style)
sheet.write(y_offset + len(lines), list(header.keys()).index('total'), docs.get('total'), level_1_style)

workbook.close()
output.seek(0)
response.stream.write(output.read())
output.close()
@@ -4,6 +4,8 @@ odoo.define('mrp.mrp_bom_report', function (require) {
var core = require('web.core');
var framework = require('web.framework');
var stock_report_generic = require('stock.stock_report_generic');
var session = require('web.session');
var crash_manager = require('web.crash_manager')

var QWeb = core.qweb;
var _t = core._t;
@@ -110,6 +112,7 @@ var MrpBomReport = stock_report_generic.extend({
this.$buttonPrint = $(QWeb.render('mrp.button'));
this.$buttonPrint.filter('.o_mrp_bom_print').on('click', this._onClickPrint.bind(this));
this.$buttonPrint.filter('.o_mrp_bom_print_unfolded').on('click', this._onClickPrint.bind(this));
this.$buttonPrint.filter('.o_mrp_bom_print_xlsx').on('click', this._onClickXlsx.bind(this));
this.$searchView = $(QWeb.render('mrp.report_bom_search', _.omit(this.data, 'lines')));
this.$searchView.find('.o_mrp_bom_report_qty').on('change', this._onChangeQty.bind(this));
this.$searchView.find('.o_mrp_bom_report_variants').on('change', this._onChangeVariants.bind(this));
@@ -136,6 +139,31 @@ var MrpBomReport = stock_report_generic.extend({
return this.do_action(action).then(function (){
framework.unblockUI();
});
},
_onClickXlsx: function (ev) {
var childBomIDs = _.map(this.$el.find('.o_mrp_bom_foldable').closest('tr'), function (el) {
return $(el).data('id');
});
framework.blockUI();
var quantity = this.$searchView.find('.o_mrp_bom_report_qty').val();
var bom_id = parseInt(this.given_context.active_id);
var variant = this.$searchView.find('.o_mrp_bom_report_variants').val();
var report_name = this.$searchView.find('.o_mrp_bom_report_name').children('option:selected').attr('data-type');
var def = $.Deferred();
session.get_file({
url: '/bom_report_xslx/'+ bom_id,
data: {
quantity: quantity || 1,
variant: variant || 0,
report_name: report_name,
},
success: def.resolve.bind(def),
error: function () {
crash_manager.rpc_error.apply(crash_manager, arguments);
def.reject();
},
complete: framework.unblockUI,
});
},
_onChangeQty: function (ev) {
var qty = $(ev.currentTarget).val().trim();
@@ -3,6 +3,7 @@
<t t-name="mrp.button">
<button type="button" class="btn btn-primary o_mrp_bom_print">print</button>
<button type="button" class="btn btn-primary ml8 o_mrp_bom_print_unfolded">print unfolded</button>
<button type="button" class="btn btn-primary ml8 o_mrp_bom_print_xlsx">export (xlsx)</button>
</t>

<form class="form-inline" t-name="mrp.report_bom_search">

0 comments on commit 24bfa3d

Please sign in to comment.
You can’t perform that action at this time.