/
stock_valuation_history_xls.py
162 lines (145 loc) · 6.54 KB
/
stock_valuation_history_xls.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2016 Kmee - www.kmee.com.br
# @authors Daniel Sadamo <daniel.sadamo@kmee.com.br>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import xlwt
from openerp.report import report_sxw
from openerp.addons.report_xls.report_xls import report_xls
from openerp.addons.report_xls.utils import _render
import logging
_logger = logging.getLogger(__name__)
class StockHistoryXlsParser(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
super(StockHistoryXlsParser, self).__init__(
cr, uid, name, context=context)
self.context = context
class StockHistoryXls(report_xls):
def __init__(self, name, table, rml=False, parser=False, header=True,
store=False):
super(StockHistoryXls, self).__init__(
name, table, rml, parser, header, store)
# Cell Styles
_xs = self.xls_styles
# header
rh_cell_format = _xs['bold'] + _xs['fill'] + \
_xs['borders_all'] + _xs['right']
self.rh_cell_style = xlwt.easyxf(rh_cell_format)
self.rh_cell_style_center = xlwt.easyxf(rh_cell_format + _xs['center'])
self.rh_cell_style_right = xlwt.easyxf(rh_cell_format + _xs['right'])
self.rh_cell_style_date = xlwt.easyxf(
rh_cell_format, num_format_str=report_xls.date_format)
# lines
line_cell_format = _xs['borders_all']
self.line_cell_style_decimal = xlwt.easyxf(
line_cell_format + _xs['right'],
num_format_str=report_xls.decimal_format)
self.line_cell_style = xlwt.easyxf(line_cell_format)
self.col_specs_template = {
'fiscal_classification_id': {
'header': [1, 10, 'text', _render("('NCM')")],
'lines': [1, 0, 'text',
_render("line.get('fiscal_classification_id', '')")],
'totals': [1, 0, 'text', None]},
'product_id': {
'header': [1, 50, 'text', _render("('Produto')")],
'lines': [1, 0, 'text',
_render("line.get('product_id', False) and "
"line.get('product_id')[1] or ''")],
'totals': [1, 0, 'text', None]},
# 'product_id_count': {
# 'header': [1, 20, 'text', _render("('Contagem')")],
# 'lines': [1, 0, 'text',
# _render("str(line.get('product_id_count', "") or '')")],
# 'totals': [1, 0, 'text', None]},
'price_unit_on_quant': {
'header': [1, 25, 'text',
_render("('Preço de custo no periodo')")],
'lines': [1, 0, 'number',
_render("line.get('price_unit_on_quant')"),
None, self.line_cell_style_decimal],
'totals': [1, 0, 'text', None]},
'quantity': {
'header': [1, 15, 'text', _render("('Quantidade')")],
'lines': [1, 0, 'number', _render("line.get('quantity', 0)")],
'totals': [1, 0, 'text', None]},
'inventory_value': {
'header': [1, 15, 'text', _render("('Valor Total')")],
'lines': [1, 0, 'number',
_render("line.get('inventory_value', 0)"),
None, self.line_cell_style_decimal],
'totals': [1, 0, 'text', None]},
}
def generate_xls_report(self, _p, _xs, data, objects, wb):
report_name = 'Relatório de Valorização de Estoque'
ws = wb.add_sheet(report_name[:31])
ws.panes_frozen = True
ws.remove_splits = True
ws.portrait = 0 # Landscape
ws.fit_width_to_pages = 1
row_pos = 0
# set print header/footer
ws.header_str = self.xls_headers['standard']
ws.footer_str = self.xls_footers['standard']
# Title
c_specs = [
('report_name', 1, 0, 'text', report_name),
]
row_data = self.xls_row_template(c_specs, ['report_name'])
row_pos = self.xls_write_row(
ws, row_pos, row_data, row_style=xlwt.easyxf(_xs['xls_title']))
row_pos += 1
# Column headers
c_specs = map(lambda x: self.render(
x, self.col_specs_template, 'header'),
[
'product_id',
'fiscal_classification_id',
# 'product_id_count',
'quantity',
'price_unit_on_quant',
'inventory_value'
])
row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
row_pos = self.xls_write_row(
ws, row_pos, row_data, row_style=self.rh_cell_style_center,
set_column_size=True)
ws.set_horz_split_pos(row_pos)
# Lines
active_ids = self.context.get('active_ids')
wizard_obj = self.pool.get('wizard.valuation.history').browse(
self.cr, self.uid, active_ids)
data = wizard_obj.compute(
self.cr, self.uid, objects[0].id, date=wizard_obj.date)
for line in data:
c_specs = map(
lambda x: self.render(
x, self.col_specs_template, 'lines'), [
'product_id',
'fiscal_classification_id',
# 'product_id_count',
'quantity',
'price_unit_on_quant',
'inventory_value'
])
row_data = self.xls_row_template(c_specs, [x[0] for x in c_specs])
row_pos = self.xls_write_row(
ws, row_pos, row_data, row_style=self.line_cell_style)
pass
StockHistoryXls('report.wizard.valuation.history',
'wizard.valuation.history', parser=StockHistoryXlsParser)