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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2011 TeMPO Consulting, MSF
#
# 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 osv import osv
from osv import fields
from tools.translate import _
from tempfile import TemporaryFile
import base64
import csv
class wizard_import_list(osv.osv_memory):
_name = 'procurement.request.import'
_description = 'Import of Internal Request'
_columns = {
'file': fields.binary(strign='File to import', required=True),
'message': fields.text(string='Message', readonly=True),
'info': fields.text(string='Info', readonly=True),
}
_defaults = {
'info': lambda *a : """
The file should be in CSV format (with ';' character as delimiter).
The columns should be in this order :
* Product code
* Product name
* UoM
* Quantity
* Comment
* From stock ? (Write True if you want that the product will be supply from stock, leave blank if not)
"""
}
def close_window(self, cr, uid, ids, context=None):
'''
Simply close the wizard
'''
if context is None:
context = {}
return {'type': 'ir.actions.act_window',
'res_model': 'sale.order',
'view_type': 'form',
'view_mode': 'form,tree',
'res_id': context.get('list_id'),
'target': 'crush'}
def default_get(self, cr, uid, fields, context=None):
'''
Check if the Procurement List is saved and
set the error message
'''
if context is None:
context = {}
res = super(wizard_import_list, self).default_get(cr, uid, fields, context=context)
# If we are after the importation
if context.get('step', False) and context.get('step', False) == 'import':
res['message'] = context.get('message', '')
return res
else:
if not context.get('active_id', False):
raise osv.except_osv(_('Warning'), _('Please save the Internal Request before importing lines'))
# We check if the Procurement List is in 'Draft' state
list_id = self.pool.get('sale.order').browse(cr, uid, context.get('active_id', []))
if not list_id.procurement_request:
raise osv.except_osv(_('Error'), _('You cannot import lines in a Sale Order'))
elif not list_id or list_id.state != 'draft':
raise osv.except_osv(_('Warning'), _('You cannot import lines in a confirmed Internal Request'))
return res
def import_file(self, cr, uid, ids, context=None):
'''
Import the file passed on the wizard in the
Procurement List
'''
if context is None:
context = {}
list_line_obj = self.pool.get('sale.order.line')
list_obj = self.pool.get('sale.order')
product_obj = self.pool.get('product.product')
uom_obj = self.pool.get('product.uom')
model_data_obj = self.pool.get('ir.model.data')
list_id = context.get('active_id', False)
if not list_id:
raise osv.except_osv(_('Error'), _('The system hasn\'t found a Internal Request to import lines'))
import_list = self.browse(cr, uid, ids[0], context)
fileobj = TemporaryFile('w+')
fileobj.write(base64.decodestring(import_list.file))
# now we determine the file format
fileobj.seek(0)
reader = csv.reader(fileobj, quotechar='\'', delimiter=';')
error = ''
line_num = 0
for line in reader:
line_num += 1
if len(line) < 6:
error += 'Line %s is not valid !' % (line_num)
error += '\n'
continue
# Get the product
product_ids = product_obj.search(cr, uid, [('default_code', '=', line[0])], context=context)
if not product_ids:
product_ids = product_obj.search(cr, uid, [('name', '=', line[1])], context=context)
if not product_ids:
error += 'Product [%s] %s not found !' % (line[0], line[1])
error += '\n'
continue
product_id = product_ids[0]
# Get the UoM
uom_ids = uom_obj.search(cr, uid, [('name', '=', line[2])])
if not uom_ids:
error += 'Uom %s not found !' % line[2]
error += '\n'
continue
uom_id = uom_ids[0]
# Get the quantity
product_qty = float(line[3].replace(',', '.'))
# Get the comment
comment = line[4]
# Get the method
from_stock = 'make_to_stock'
if line[5] == 'True':
from_stock = 'make_to_order'
list_line_obj.create(cr, uid, {'product_id': product_id,
'product_uom': uom_id,
'product_uom_qty': product_qty,
'notes': comment,
'type': from_stock,
'procurement_request': True,
'order_id': list_id})
view_ids = model_data_obj.search(cr, uid,
[('module', '=', 'procurement_request'),
('name', '=', 'wizard_import_list_done')],
offset=0, limit=1)[0]
view_id = model_data_obj.browse(cr, uid, view_ids).res_id
if error and error != '':
context['message'] = error
else:
context['message'] = 'All lines have been succesfully imported !'
context['step'] = 'import'
context['list_id'] = list_id
return {'type': 'ir.actions.act_window',
'res_model': 'procurement.request.import',
'view_type': 'form',
'view_mode': 'form',
'target': 'new',
'view_id': [view_id],
'context': context,
}
wizard_import_list()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|