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
163
164
165
166
|
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2012 TeMPO Consulting, MSF. All Rights Reserved
#
# 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/>.
#
##############################################################################
from datetime import datetime
from osv import osv
from osv import fields
import logging
import tools
from os import path
from tools.translate import _
import base64
from spreadsheet_xml.spreadsheet_xml import SpreadsheetXML
class stock_cost_reevaluation(osv.osv):
_inherit = 'stock.cost.reevaluation'
_columns = {
'file_to_import': fields.binary(string='File to import', filters='*.xml', help='You can use the template of the export for the format that you need to use. \n The file should be in XML Spreadsheet 2003 format. \n The columns should be in this order : Product Code*, Product Description*, Initial Average Cost, Location*, Batch, Expiry Date, Quantity'),
}
def import_file(self, cr, uid, ids, context=None):
'''
Import lines form file
'''
if not context:
context = {}
product_obj = self.pool.get('product.product')
obj_data = self.pool.get('ir.model.data')
import_to_correct = False
vals = {}
vals['reevaluation_line_ids'] = []
msg_to_return = _("All lines successfully imported")
obj = self.browse(cr, uid, ids, context=context)[0]
if not obj.file_to_import:
raise osv.except_osv(_('Error'), _('Nothing to import.'))
product_cache = {}
fileobj = SpreadsheetXML(xmlstring=base64.decodestring(obj.file_to_import))
# iterator on rows
reader = fileobj.getRows()
# ignore the first row
reader.next()
line_num = 1
for row in reader:
line_num += 1
# Check length of the row
if len(row) != 3:
raise osv.except_osv(_('Error'), _("""You should have exactly 3 columns in this order:
Product Code*, Product Description*, Product Cost*"""))
# default values
product_id = False
product_cost = 1.00
product_code = False
product_name = False
currency_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.currency_id.id
# Product code
product_code = row.cells[0].data
if not product_code:
product_code = False
else:
try:
product_code = product_code.strip()
if product_code in product_cache:
product_id = product_cache.get(product_code)
if not product_id:
product_ids = product_obj.search(cr, uid, ['|', ('default_code', '=', product_code.upper()), ('default_code', '=', product_code)])
if product_ids:
product_id = product_ids[0]
product_cache.update({product_code: product_id})
except Exception:
pass
# Product name
product_name = row.cells[1].data
if product_name:
try:
product_name = product_name.strip()
product_ids = product_obj.search(cr, uid, [('name', '=', product_name)])
if product_ids:
product_id = product_ids[0]
except Exception:
pass
if not product_id:
if not product_code and not product_name:
raise osv.except_osv(_('Error'), _('You have to fill at least the product code or the product name on each line'))
raise osv.except_osv(_('Error'), _('The Product [%s] %s was not found in the list of the products') % (product_code or '', product_name or ''))
# Average cost
cost = row.cells[2].data
if not cost:
if product_id:
product_cost = product_obj.browse(cr, uid, product_id).standard_price
else:
product_cost = 1.00
else:
if row.cells[2].type in ('int', 'float'):
product_cost = cost
elif product_id:
product_cost = product_obj.browse(cr, uid, product_id).standard_price
else:
product_cost = 1.00
to_write = {
'product_id': product_id,
'average_cost': product_cost,
'currency_id': currency_id,
}
vals['reevaluation_line_ids'].append((0, 0, to_write))
# write order line on Inventory
vals.update({'file_to_import': False})
self.write(cr, uid, ids, vals, context=context)
view_id = obj_data.get_object_reference(cr, uid, 'specific_rules','cost_reevaluation_form_view')[1]
return self.log(cr, uid, obj.id, msg_to_return, context={'view_id': view_id,})
def button_remove_lines(self, cr, uid, ids, context=None):
'''
Remove lines
'''
if not context:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
vals = {}
vals['reevaluation_line_ids'] = []
for line in self.browse(cr, uid, ids, context=context):
line_browse_list = line.reevaluation_line_ids
for var in line_browse_list:
vals['reevaluation_line_ids'].append((2, var.id))
self.write(cr, uid, ids, vals, context=context)
return True
stock_cost_reevaluation()
|