~openbias/bias-trunk/addons_equitek

« back to all changes in this revision

Viewing changes to bias_import_bom/product_bom.py.OTHER

  • Committer: Miguel Miguel
  • Date: 2012-04-24 16:36:51 UTC
  • mfrom: (40.1.6 addons_equitek)
  • Revision ID: miguelmiguel@martha-20120424163651-vk7i4hr76is3dnx6
se agrego boton cancelar movimiento en lineas de produccion

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU Affero General Public License as
9
 
#    published by the Free Software Foundation, either version 3 of the
10
 
#    License, or (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU Affero General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU Affero General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
#
20
 
##############################################################################
21
 
 
22
 
from osv import osv
23
 
from osv import fields
24
 
 
25
 
DEFAULTENCODING = 'utf8'
26
 
PROJECTCOLUMN = 2
27
 
REVCOLUMN = 3
28
 
REFHEADER = ['Project', 'Rev']
29
 
 
30
 
PMPREFIX = 'PM-'
31
 
 
32
 
MAQUINADICT = {'type': 'product',
33
 
               'supply_method': 'produce',
34
 
               'sale_ok': True,
35
 
               'purchase_ok': False,
36
 
               'category': 'Maquina'}
37
 
 
38
 
MODULODICT = {'type': 'product',
39
 
              'supply_method': 'produce',
40
 
              'sale_ok': True,
41
 
              'purchase_ok': False,
42
 
              'category': 'Maquina'}
43
 
 
44
 
BOMDICT = {}
45
 
UOMNAME = 'Piezas'
46
 
 
47
 
def uniquify(mylist):
48
 
    set = {}
49
 
    map(set.__setitem__, mylist, [])
50
 
    return set.keys()
51
 
 
52
 
def xls2list(fname, worksheet=1,encoding=DEFAULTENCODING):
53
 
    from pyExcelerator import parse_xls
54
 
    data = parse_xls(fname, encoding) 
55
 
    sheet_name=data[worksheet-1][0].encode(encoding) 
56
 
    values = data[worksheet-1][1]
57
 
    vdict = {} 
58
 
    row_idx_max = 0 
59
 
    col_idx_max = 0 
60
 
    for row_idx, col_idx in sorted(values.keys()): 
61
 
        row_idx_max = max(row_idx,row_idx_max) 
62
 
        col_idx_max = max(col_idx,col_idx_max) 
63
 
        v = values[(row_idx, col_idx)] 
64
 
        if isinstance(v, unicode): 
65
 
            v = v.encode(encoding, 'backslashreplace') 
66
 
        vdict[(row_idx,col_idx)] = v
67
 
    vlist = [] 
68
 
    for row in range(row_idx_max+1): 
69
 
        vlist.append([]) 
70
 
        for col in range(col_idx_max+1): 
71
 
            if (row,col) not in vdict: 
72
 
                vdict[(row,col)]=None 
73
 
            vlist[row].append(vdict[(row,col)]) 
74
 
    return vlist 
75
 
 
76
 
def getAllWorksheets(fname, encoding=DEFAULTENCODING):
77
 
    from pyExcelerator import parse_xls
78
 
    WorkSheetList = []
79
 
    for i in range(len(parse_xls(fname, encoding))):
80
 
        WorkSheetList.append(xls2list(fname, i+1, encoding))
81
 
    return WorkSheetList
82
 
 
83
 
def getCols(mylist, colnumlist):
84
 
    res = []
85
 
    for row in mylist:
86
 
        myrow = []
87
 
        for colnum in colnumlist:
88
 
            myrow.append(row[colnum])
89
 
        res.append(myrow)
90
 
    return res
91
 
 
92
 
def cleanWorkSheetList(wslist):
93
 
    outlist = []
94
 
    for ws in wslist:
95
 
        while ws[0].count(None) == len(ws[0]):
96
 
            del ws[0]
97
 
        startcol = 0
98
 
        mycol = getCols(ws, [startcol])
99
 
        while mycol.count([None]) == len(mycol):
100
 
            startcol += 1
101
 
            mycol = getCols(ws, [startcol])
102
 
        outlist.append([x[startcol:] for x in ws])
103
 
    return outlist
104
 
 
105
 
class product_product(osv.osv):
106
 
    _inherit = "product.product"
107
 
    _columns = {'equitek_docnum': fields.char('Document Number', size=128)
108
 
                }
109
 
product_product()
110
 
 
111
 
class category_relation(osv.osv):
112
 
    _name = 'category.relation'
113
 
    _columns = {'key': fields.char('Key', size=10),
114
 
                'category_id': fields.many2one('product.category', 'Category')
115
 
                }
116
 
category_relation()
117
 
 
118
 
class bom_file(osv.osv):
119
 
    _inherit = 'ir.attachment'
120
 
#    _name = 'bom.file'
121
 
 
122
 
    def _getModuleList(self, wslist):
123
 
        modlist = []
124
 
        for ws in wslist:
125
 
            modlist.append("%s-%03i" %tuple(ws[0][2:4]))
126
 
        return modlist
127
 
 
128
 
    def _getBOMData(self, cr, uid, myid, context=None):
129
 
        import base64, os, time
130
 
        mydata = base64.standard_b64decode(self.browse(cr, uid, myid).datas)
131
 
        oldumask = os.umask(077)
132
 
        tmpfname = ('/tmp/xlsfile_%.6f' %(time.time(), )).replace('.', '_') + '.xls'
133
 
        open(tmpfname, 'w').write(mydata)
134
 
        WorkSheetList = getAllWorksheets(tmpfname)
135
 
        os.remove(tmpfname)
136
 
        return cleanWorkSheetList(WorkSheetList)
137
 
 
138
 
    def getOldModules(self, cr, uid, attachid):
139
 
        wslist = self._getBOMData(cr, uid, attachid)
140
 
        oldprodlist = []
141
 
        prod_obj = self.pool.get('product.product')
142
 
        for ws in wslist:
143
 
            mycols = getCols(ws, [PROJECTCOLUMN, REVCOLUMN])
144
 
            modulename = "%s-%03i" %tuple(mycols[0])
145
 
            modlist = prod_obj.search(cr, uid, [('default_code', '=', modulename)])
146
 
            if len(modlist) == 1:
147
 
                oldprodlist.append(modlist[0])
148
 
            elif len(modlist) > 1:
149
 
                raise Exception, "Multiple existing modules: %s" %(modulename, )
150
 
        return uniquify(oldprodlist)
151
 
 
152
 
    def getNewModules(self, cr, uid, attachid):
153
 
        wslist = self._getBOMData(cr, uid, attachid)
154
 
        newprodlist = []
155
 
        prod_obj = self.pool.get('product.product')
156
 
        for ws in wslist:
157
 
            mycols = getCols(ws, [PROJECTCOLUMN, REVCOLUMN])
158
 
            modulename = "%s-%03i" %tuple(mycols[0])
159
 
            modlist = prod_obj.search(cr, uid, [('default_code', '=', modulename)])
160
 
            if len(modlist) == 0:
161
 
                newprodlist.append(prodname)
162
 
            elif len(modlist) > 1:
163
 
                raise Exception, "Multiple existing modules: %s" %(modulename, )
164
 
        return uniquify(newprodlist)
165
 
 
166
 
    def importBOM(self, cr, uid, attachid):
167
 
        wslist = self._getBOMData(cr, uid, attachid)
168
 
        prod_obj = self.pool.get('product.product')
169
 
        pastheader = False
170
 
        for ws in wslist:
171
 
            MainProdName = ws[0][0]
172
 
            for row in ws[2:]:
173
 
                docnum, project, rev, description, qty, variant = row[1:7]
174
 
        return []
175
 
 
176
 
    def _addProductFromRow(self, cr, uid, row):
177
 
        mycode = "%s-%03i" %tuple(row[2:4])
178
 
        mydescr = row[4]
179
 
        mydict = MODULODICT
180
 
        mydict['default_code'] = mycode
181
 
        mydict['name'] = mydescr
182
 
        mydict['description'] = mydescr
183
 
        mydict['equitek_docnum'] = row[1]
184
 
        if row[6] and row[6] != "NA":
185
 
            mydict['variants'] = row[6]
186
 
        #### Falta: agregar category de acuerdo a category_relation
187
 
        prod_obj = self.pool.get('product.product')
188
 
        return prod_obj.create(cr, uid, mydict)
189
 
 
190
 
    def getPMList(self, cr, uid, ws):
191
 
        wslist = self._getBOMData(cr, uid, attachid)
192
 
        pmlist = []
193
 
        for ws in wslist[1:]:
194
 
            for row in ws[2:]:
195
 
                if ('find' in dir(row[1])) and (row[1].find(PMPREFIX) == 0):
196
 
                    pmlist.append("%s-%03i" %tuple(row[2:4]))
197
 
        return pmlist
198
 
 
199
 
    def altaMaquina(self, cr, uid, wslist):
200
 
        result = {}
201
 
        myws = wslist[0]
202
 
        mycode = "%s-%03i" %tuple(myws[0][2:4])
203
 
        maqid = "OBTENER..."
204
 
        prod_obj = self.pool.get('product.product')
205
 
        if len(prod_obj.search(cr, uid, [('default_code', '=', mycode)])):
206
 
            maqid = prod_obj.search(cr, uid, [('default_code', '=', mycode)])[0]
207
 
        else:
208
 
            mydescr = myws[0][4]
209
 
            mydict = MAQUINADICT
210
 
            mydict['default_code'] = mycode
211
 
            mydict['name'] = mydescr
212
 
            mydict['description'] = mydescr
213
 
            maqid = prod_obj.create(cr, uid, mydict)
214
 
        result['maquina_id'] = maqid
215
 
        modlist = self._getModuleList(wslist[1:])
216
 
        uomid = self.pool.get('product.uom').search(cr, uid, [('name', '=', UOMNAME)])[0]
217
 
        bom_obj = self.pool.get('mrp.bom')
218
 
        bomdict = BOMDICT
219
 
        bomdict['product_id'] = maqid
220
 
        bomdict['name'] = 'Lista de mat. ' + mycode
221
 
        bomdict['product_uom'] = uomid
222
 
        bomdict['bom_id'] = False
223
 
        bomid = bom_obj.create(cr, uid, bomdict)
224
 
        result['bom_id'] = bomid
225
 
        for row in myws[2:]:
226
 
            mycode = "%s-%03i" %tuple(row[2:4])
227
 
            if mycode in modlist:
228
 
                ###### Si el modulo está incluido en los worksheets anexos suponemos que es un modulo nuevo
229
 
                if len(prod_obj.search(cr, uid, [('default_code', '=', mycode)])):
230
 
                    raise Exception, "El módulo %s ya existe" %(mycode, )
231
 
                else:
232
 
                    mymoduleid = self._addProductFromRow(cr, uid, row)
233
 
                bomdict = BOMDICT
234
 
                bomdict['product_id'] = mymoduleid
235
 
                bomdict['product_uom'] = uomid
236
 
                bomdict['bom_id'] = bomid
237
 
                bomdict['name'] = mycode
238
 
                mybomid = bom_obj.create(cr, uid, bomdict)
239
 
            else:
240
 
                ###### Si no está incluido suponemos que el módulo ya existe (y que tiene su bom, etc).
241
 
                res = prod_obj.search(cr, uid, [('default_code', '=', mycode)])
242
 
                if len(res) == 1:
243
 
                    mymoduleid = res[0]
244
 
                elif not len(res):
245
 
                    raise Exception, "El módulo %s no está dado de alta y no está incluido en el archivo a importar" %(mycode, )
246
 
                else:
247
 
                    raise Exception, "Más de un módulo %s dado de alta." %(mycode, )
248
 
                prod_brw = prod_obj.browse(cr, uid, mymoduleid)
249
 
                bomdict = BOMDICT
250
 
                bomdict['product_id'] = mymoduleid
251
 
                bomdict['product_uom'] = uomid #### Debería ser el uom del template correspondiente al producto
252
 
                bomdict['bom_id'] = bomid
253
 
                bomdict['name'] = mycode
254
 
                mybomid = bom_obj.create(cr, uid, bomdict)
255
 
        return result
256
 
 
257
 
    def _addModuleFromWS(self, cr, uid, ws):
258
 
        result = {}
259
 
        mycode = "%s-%03i" %tuple(ws[0][2:4])
260
 
        prod_obj = self.pool.get('product.product')
261
 
        bom_obj = self.pool.get('mrp.bom')
262
 
        product_id = prod_obj.search(cr, uid, [('default_code', '=', mycode)])[0]
263
 
        result['module_product_id'] = product_id
264
 
        uomid = self.pool.get('product.uom').search(cr, uid, [('name', '=', UOMNAME)])[0]
265
 
        bomdict = BOMDICT
266
 
        bomdict['product_id'] = product_id
267
 
        bomdict['product_uom'] = uomid
268
 
        bomdict['bom_id'] = False
269
 
        bomdict['name'] = 'Lista de material para módulo ' + mycode
270
 
        bomid = bom_obj.create(cr, uid, bomdict)
271
 
        result['module_bom_id'] = bomid
272
 
        result['bom_pmlist'] = []
273
 
        for row in ws[2:]:
274
 
            myprod_code = "%s-%03i" %tuple(row[2:4])
275
 
            myproduct_idlist = prod_obj.search(cr, uid, [('default_code', '=', myprod_code)])
276
 
            mydescr = row[4]
277
 
            if not len(myproduct_idlist):
278
 
                myproduct_id = self._addProductFromRow(cr, uid, row)
279
 
                newproduct = True
280
 
            else:
281
 
                myproduct_id = myproduct_idlist[0]
282
 
                newproduct = False
283
 
            if ('find' in dir(row[1])) and (row[1].find(PMPREFIX) == 0):
284
 
                result['bom_pmlist'].append(myproduct_id)
285
 
            bomdict = BOMDICT
286
 
            bomdict['product_id'] = myproduct_id
287
 
            bomdict['bom_id'] = bomid
288
 
            bomdict['product_uom'] = uomid
289
 
            bomdict['product_qty'] = row[5]
290
 
            bomdict['name'] = mydescr
291
 
            mybomid = bom_obj.create(cr, uid, bomdict)
292
 
            if len(row) >= 8 and row[7]:
293
 
                if newproduct:
294
 
                    prod_obj.write(cr, uid, [myproduct_id], {'variants': False})
295
 
                bomdict = BOMDICT
296
 
                res = prod_obj.search(cr, uid, [('default_code', '=', row[6])])
297
 
                if not res:
298
 
                    raise osv.except_osv(('Error !'), ('El producto %s no existe.' %(row[6], )))
299
 
                bomdict['product_id'] = res[0]
300
 
                bomdict['bom_id'] = mybomid
301
 
                bomdict['product_uom'] = uomid
302
 
                bomdict['product_qty'] = row[7]
303
 
                bomdict['name'] = row[6]
304
 
                mysubbomid = bom_obj.create(cr, uid, bomdict)
305
 
        return result
306
 
 
307
 
 
308
 
    def importMaquina(self, cr, uid, attachid):
309
 
        result = []
310
 
        oldlist = self.getOldModules(cr, uid, attachid)
311
 
        wslist = self._getBOMData(cr, uid, attachid)
312
 
        maqdict = self.altaMaquina(cr, uid, wslist)
313
 
        main_bomid = maqdict['bom_id']
314
 
        bomdata = []
315
 
        for ws in wslist[1:]:
316
 
            bomdata.append(self._addModuleFromWS(cr, uid, ws))
317
 
        return (main_bomid, oldlist, bomdata)
318
 
 
319
 
bom_file()