acc_product.py
13.5 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# -*- coding: utf-8 -*-
import logging
from odoo import api, fields, models, _
from odoo.http import request
import logging
import xlrd
from collections import Counter
import re
import pytz
from odoo.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
from datetime import datetime
# from ..controllers.common import localizeStrTime
from odoo.exceptions import UserError, ValidationError
_logger = logging.getLogger(__name__)
class product_templ_acc(models.Model):
_inherit = 'product.template'
@api.model
def create(self,vals):
if not vals.get('acc_code',''):
last_name = self.env['ir.sequence'].get('product.template') or ''
vals['acc_code'] = "%s"%(last_name)
if vals.get('product_model') and vals.get('brand'):
self.check_products(vals)
result = super(product_templ_acc,self).create(vals)
return result
en_name = fields.Char(string='产品英文名称')
partner_code = fields.Char(string='供应商编码')
brand = fields.Char(string='品牌',required=True)
internal_des = fields.Char(string='内部描述')
product_describe_cn = fields.Text(string='产品中文描述')
product_describe_en = fields.Text(string='产品英文描述')
product_model = fields.Char(string='产品型号',required=True)
acc_code = fields.Char(string='产品编码',readonly=True)
# acc_code = fields.Char(string='产品编码')
template_currency_id = fields.Many2one('res.currency',string='币种')
acc_purchase_price = fields.Float(string='采购价格',default=0.0)
partner_id = fields.Many2one('res.partner',string='供应商')
image_code = fields.Char(string='图号')
part_code = fields.Char(string='编号')
# standard_price = fields.Float('Cost', company_dependent=False)
# def _compute_set_standard_price(self):
# self.standard_price = self.acc_purchase_price
def check_products(self,vals):
product_model = vals.get('product_model')
brand = vals.get('brand')
product_name = vals.get('name')
pt = self.env['product.template'].search([('product_model', '=', product_model),('brand', '=', brand),('name', '=', product_name),('active', '=', True)])
if pt:
raise ValidationError("产品重复(系统存在该型号品牌的产品)!")
@api.depends('product_variant_ids', 'product_variant_ids.standard_price')
def _compute_standard_price(self):
res = super(product_templ_acc, self)._compute_standard_price()
# _logger.debug('===========%s222===============', self)
for order in self:
order.standard_price = order.acc_purchase_price
return res
@api.onchange('acc_purchase_price')
def _onchange_acc_purchase_price(self):
# if self.forcast_date and self.order_id.state == 'purchase':
if self.acc_purchase_price:
product = self.env['product.product'].search([('product_tmpl_id', '=', self._origin.id)],limit=1)
# _logger.debug('===========%s111===============', product)
product.write({'standard_price':self.acc_purchase_price})
@api.multi
def action_see_attachments(self):
product = self.env['product.product'].search([('product_tmpl_id', '=', self.id)])
domain = [
'|',
'&', ('res_model', '=', 'product.product'), ('res_id', '=', product.id),
'&', ('res_model', '=', 'product.template'), ('res_id', '=', self.id)]
attachment_view = self.env.ref('acct_base.view_document_file_kanban_acct')
return {
'name': _('Attachments'),
'domain': domain,
'res_model': 'mrp.document',
'type': 'ir.actions.act_window',
'view_id': attachment_view.id,
'views': [(attachment_view.id, 'kanban'), (False, 'form')],
'view_mode': 'kanban,tree,form',
'view_type': 'form',
'help': _('''<p class="o_view_nocontent_smiling_face">
Upload files to your product
</p><p>
Use this feature to store any files, like drawings or specifications.
</p>'''),
'limit': 80,
'context': "{'default_res_model': '%s','default_res_id': %d}" % ('product.template', self.id)
}
@api.multi
def make_acccode(self):
pt = self.env['product.template'].search([('acc_code', 'like', 'ACC-PRO-')])
for i in pt:
i.write({'active':False})
_logger.debug('===========%s===============', i.name)
def import_product_data(self, fileName=None, content=None):
import_tips = ""
# card_number_repeated = False
try:
if content:
workbook = xlrd.open_workbook(file_contents=content)
else:
raise ValidationError(u'请选择正确的文档')
book_sheet = workbook.sheet_by_index(0)
all_data = []
all_card_number = []
for row in range(1, book_sheet.nrows):
row_data = []
for col in range(book_sheet.ncols):
cel = book_sheet.cell(row, col)
val = cel.value
row_data.append(val)
if type(row_data[2]).__name__ == 'float':
row_data[2] = int(row_data[2])
all_data.append(row_data)
cr = self.env.cr
error_product_name = []
success_num = 0
all_data,repeat_name = self.check_repeat(all_data)
for import_line in all_data:
res_partner = self.env['res.partner'].search([('name', '=', str(import_line[4]))])
uom = self.env['uom.uom'].search([('name', '=', str(import_line[7]))])
if uom:
uom_id = uom.id
else:
uom_id = 38
# uom_id = 21
product_category = self.env['product.category'].search([('name', '=', str(import_line[3]))])
if product_category:
categ_id = product_category.id
else:
categ_id = 1
try:
vals = {
"type":'product',
"name":import_line[0],
"acc_code":import_line[1],
"product_model":import_line[2],
'categ_id':categ_id,
"partner_id":res_partner.id,
"brand":import_line[5],
"list_price":float(import_line[6]),
"acc_purchase_price":float(import_line[6]),
"sale_ok":True,
"purchase_ok":True,
'uom_id':uom_id,
"product_describe_cn":import_line[8],
"product_describe_en":import_line[9],
"internal_des":import_line[12],
"en_name":import_line[13],
"partner_code":import_line[14],
"description":import_line[10]
}
# employee_id = self.with_context(user_type='employee').create(vals)
self.env['product.template'].create(vals)
cr.commit()
success_num += 1
# print (import_line[0],import_line[1])
_logger.debug('===========%s===============%s', import_line[0], import_line[1])
except Exception as e:
logging.error(e)
error_product_name.append(import_line[0])
import_tips = "一共导入 {} 条数据,导入成功条数为{} 导入失败产品名称{} 系统中已存在{}".format(len(all_data), success_num,error_product_name,repeat_name)
# import_tips = "一共导入 {} 条数据,导入成功条数为{} 导入失败产品名称{}".format(len(all_data), success_num,error_product_name)
except Exception as e:
logging.error(e)
else:
raise ValidationError(import_tips)
# def import_product_data(self, fileName=None, content=None):
# if content:
# workbook = xlrd.open_workbook(file_contents=content)
# else:
# raise ValidationError(u'请选择正确的文档')
# book_sheet = workbook.sheet_by_index(0)
# all_data = []
# all_card_number = []
# for row in range(1, book_sheet.nrows):
# row_data = []
# for col in range(book_sheet.ncols):
# cel = book_sheet.cell(row, col)
# val = cel.value
# row_data.append(val)
# all_data.append(row_data)
# cr = self.env.cr
# for import_line in all_data:
# # cr=self.env.cr
# cr.execute(
# """UPDATE product_template
# SET acc_code = '%s'
# WHERE
# NAME = '%s'
# AND product_model = '%s'
# AND brand = '%s'
# AND product_describe_cn = '%s'
# """%(import_line[1],import_line[0],import_line[2],import_line[5],import_line[8]))
# print (import_line[0],import_line[1])
# _logger.debug('===========%s===============%s', import_line[0], import_line[1])
#
def check_repeat(self,all_data):
repeat_name = []
# repeat_code = []
for i in range(len(all_data)-1,-1,-1):
# pt = self.env['product.template'].search([('name', '=', all_data[i][0]),('product_model', '=', all_data[i][2]),('brand', '=', all_data[i][5]),('product_describe_cn', '=', all_data[i][8])])
pt = self.env['product.template'].search([('product_model', '=', all_data[i][2]),('brand', '=', all_data[i][5]),('active', '=', True)])
if pt:
# print (pt[0].name,pt[0].acc_code)
repeat_name.append(pt[0].name)
# repeat_code.append(pt[0].acc_code)
all_data.remove(all_data[i])
return all_data,repeat_name
class product_product_acc(models.Model):
_inherit = 'product.product'
# product_model = fields.Char(string='产品型号')
# brand = fields.Char(string='品牌')
# product_describe_cn = fields.Text(string='产品中文描述')
# product_describe_en = fields.Text(string='产品英文描述')
# acc_purchase_price = fields.Float(string='采购价格',default=0.0)
# partner_id = fields.Many2one('res.partner',string='供应商')
# acc_code = fields.Char(string='产品编码')
# template_currency_id = fields.Many2one('res.currency',string='币种')
# image_code = fields.Char(string='图号')
# part_code = fields.Char(string='编号')
product_model = fields.Char(related='product_tmpl_id.product_model',readonly=True, store=True,string='产品型号')
brand = fields.Char(related='product_tmpl_id.brand',readonly=True, store=True,string='品牌')
en_name = fields.Char(related='product_tmpl_id.en_name',readonly=True, store=True,string='产品英文名称')
partner_code = fields.Char(related='product_tmpl_id.partner_code',readonly=True, store=True,string='供应商编码')
internal_des = fields.Char(related='product_tmpl_id.internal_des',readonly=True, store=True,string='内部描述')
product_describe_cn = fields.Text(related='product_tmpl_id.product_describe_cn',readonly=True, store=True,string='产品中文描述')
product_describe_en = fields.Text(related='product_tmpl_id.product_describe_en',readonly=True, store=True,string='产品英文描述')
acc_purchase_price = fields.Float(related='product_tmpl_id.acc_purchase_price',readonly=True, store=True,string='采购价格')
partner_id = fields.Many2one('res.partner',related='product_tmpl_id.partner_id',readonly=True, store=True,string='供应商')
acc_code = fields.Char(related='product_tmpl_id.acc_code',readonly=True, store=True,string='产品编码')
template_currency_id = fields.Many2one('res.currency',related='product_tmpl_id.template_currency_id',readonly=True, store=True,string='币种')
image_code = fields.Char(related='product_tmpl_id.image_code',readonly=True, store=True,string='图号')
part_code = fields.Char(related='product_tmpl_id.part_code',readonly=True, store=True,string='编号')
standard_price = fields.Float('Cost', company_dependent=False)
@api.multi
def action_see_attachments(self):
domain = [
'|',
'&', ('res_model', '=', 'product.template'), ('res_id', '=', self.product_tmpl_id.id),
'&', ('res_model', '=', 'product.product'), ('res_id', '=', self.id)]
attachment_view = self.env.ref('acct_base.view_document_file_kanban_acct')
return {
'name': _('Attachments'),
'domain': domain,
'res_model': 'mrp.document',
'type': 'ir.actions.act_window',
'view_id': attachment_view.id,
'views': [(attachment_view.id, 'kanban'), (False, 'form')],
'view_mode': 'kanban,tree,form',
'view_type': 'form',
'help': _('''<p class="o_view_nocontent_smiling_face">
Upload files to your product
</p><p>
Use this feature to store any files, like drawings or specifications.
</p>'''),
'limit': 80,
'context': "{'default_res_model': '%s','default_res_id': %d}" % ('product.template', self.id)
}