~openerp-mongolian/openerp-mongolization/7.0

« back to all changes in this revision

Viewing changes to l10n_mn_initial_data_insertion/.svn/text-base/product_initial_insertion.py.svn-base

  • Committer: Unurjartal Ts
  • Date: 2013-06-17 09:33:42 UTC
  • mfrom: (1.1.2 openerp-mongolization)
  • Revision ID: unuruu25@gmail.com-20130617093342-w2o2hbtz5qg8ioli
ShineERP custom modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    ShineERP, Enterprise Management Solution
 
5
#    Copyright (C) 2007-2013 ShineERP Co.,ltd (<http://www.erp.mn>). All Rights Reserved
 
6
#
 
7
#    Address : Suhkbaatar District, National ITPark, ShineERP LLC
 
8
#    Email : info@erp.mn
 
9
#    Phone : 976 + 11-318043
 
10
#
 
11
##############################################################################
 
12
 
 
13
from osv import osv,fields,orm
 
14
from tools.translate import _
 
15
import time
 
16
import netsvc, decimal, base64, os, time, xlrd
 
17
from tempfile import NamedTemporaryFile
 
18
 
 
19
class product_data_initial(osv.osv_memory):
 
20
    _name = 'product.data.initial'
 
21
    _description = 'Product Data Initial'
 
22
    
 
23
    _columns = {
 
24
        'data': fields.binary('Product Data File', required=True)
 
25
    }
 
26
    
 
27
    def import_data(self, cr, uid, ids, context={}):
 
28
        product_obj = self.pool.get('product.product')
 
29
        product_tmpl_obj = self.pool.get('product.template')
 
30
        product_category_obj = self.pool.get('product.category')
 
31
        product_uom_obj = self.pool.get('product.uom')
 
32
        
 
33
        product_uoms = {u'ш':'Unit(s)'}
 
34
        
 
35
        form = self.browse(cr, uid, ids[0])
 
36
        
 
37
        fileobj = NamedTemporaryFile('w+')
 
38
        fileobj.write(base64.decodestring(form.data))
 
39
        fileobj.seek(0)
 
40
        
 
41
        if not os.path.isfile(fileobj.name):
 
42
            raise osv.except_osv(u'Алдаа',u'Мэдээллийн файлыг уншихад алдаа гарлаа.\nЗөв файл эсэхийг шалгаад дахин оролдоно уу!')
 
43
        book = xlrd.open_workbook(fileobj.name)
 
44
        
 
45
        sheet = book.sheet_by_index(0)
 
46
        nrows = sheet.nrows
 
47
        
 
48
        rowi = 1
 
49
        
 
50
        while rowi < nrows :
 
51
            try:
 
52
                row = sheet.row(rowi)
 
53
                product_name = row[0].value
 
54
                product_category = u'' + row[1].value
 
55
                product_cost = row[2].value
 
56
                product_uom = row[3].value
 
57
                product_type = row[4].value
 
58
                product_supply_method = row[5].value
 
59
                product_procure_method = row[6].value
 
60
                product_sale_ok = row[7].value
 
61
                product_purchase_ok = row[8].value
 
62
                product_cost_method = row[9].value
 
63
                
 
64
                category_id = product_category_obj.search(cr, uid, [('name', '=', product_category)], limit=1)
 
65
                if len(category_id) ==0:
 
66
                    raise osv.except_osv('Error', 'Category does not exist! %s ' % product_category)
 
67
                product_uom_id = product_uom_obj.search(cr, uid, [('name', '=', product_uoms[product_uom] if product_uoms.has_key(product_uom) else '')], limit=1)
 
68
                if len(product_uom_id) ==0:
 
69
                    raise osv.except_osv('Error', 'Product uom does not exist! %s ' % product_uom)
 
70
                
 
71
                product_tmpl_id = product_tmpl_obj.create(cr, uid, {
 
72
                    'name': product_name,
 
73
                    'categ_id': category_id[0],
 
74
                    'standard_price': product_cost,
 
75
                    'uom_id': product_uom_id[0],
 
76
                    'type': product_type,
 
77
                    'supply_method': product_supply_method,
 
78
                    'procure_method': product_procure_method,
 
79
                    'purchase_ok': product_sale_ok,
 
80
                    'sale_ok': product_purchase_ok,
 
81
                    'cost_method': product_cost_method,
 
82
                    'company_id': 1,
 
83
                }, context=context)
 
84
                print 'created product_tmpl_id: ' + str(product_tmpl_id)
 
85
                product_id = product_obj.create(cr, uid, {
 
86
                    'product_tmpl_id': product_tmpl_id,
 
87
                    'active': True,
 
88
                    'valuation': 'manual_periodic',
 
89
                }, context=context)
 
90
                print 'created product_id: ' + str(product_id)
 
91
                
 
92
                rowi += 1
 
93
            except IndexError :
 
94
                raise osv.except_osv('Error', 'Excel sheet must be 10 columned : error on row %s ' % rowi)
 
95
        return True
 
96
product_data_initial()
 
 
b'\\ No newline at end of file'