def product_create( self, inv_values, line, item_grid, default_category=None): if not line[2]['fiscal_classification_id']: fc_env = self.env['account.product.fiscal.classification'] ncm = fc_env.search([('name', '=', line[2]['ncm_xml'])], limit=1) if not ncm: ncm = fc_env.create({ 'name': line[2]['ncm_xml'], 'company_id': inv_values['company_id'], 'type': 'normal' }) line[2]['fiscal_classification_id'] = ncm.id vals = { 'name': line[2]['product_name_xml'], 'type': 'product', 'fiscal_type': 'product', 'ncm_id': line[2]['fiscal_classification_id'], 'default_code': line[2]['product_code_xml'], } if default_category: vals['categ_id'] = default_category.id if check_ean(line[2]['ean_xml']): vals['ean13'] = line[2]['ean_xml'] if item_grid.uom_id: vals['uom_id'] = item_grid.uom_id.id vals['uom_po_id'] = item_grid.uom_id.id product_tmpl = self.env['product.template'].create(vals) return product_tmpl.product_variant_ids[0]
def test_generate_serial_id_are_valid_ean(self): for _ in range(1000): serial_id = generate_serial_id() self.assertTrue( check_ean(serial_id), 'The generate Serial Id "{0}" is not a valid EAN-13'.format( serial_id))
def ean13(self, record): if self.has_combinations(record): return {} if record['ean13'] in ['', '0']: return {} if check_ean(record['ean13']): return {'ean13': record['ean13']} return {}
def _check_ean_key(self, cr, uid, ids, context=None): """ Validate ean code """ context = context or {} ids = isinstance(ids, (int, long)) and [ids] or ids for pack in self.browse(cr, uid, ids, context=context): res = Product.check_ean(pack.ean) return res
def test_create_generates_valid_ean(self): name = self.get_new_user_name() user = self.create_user(name=name, login=name) self.assertTrue(user.serial_id) self.assertTrue( check_ean(user.serial_id), 'The user\'s Serial Id is not a valid EAN-13 code.' )
def test_generate_serial_id_are_valid_ean(self): for _ in range(1000): serial_id = generate_serial_id() self.assertTrue( check_ean(serial_id), 'The generate Serial Id "{0}" is not a valid EAN-13'.format( serial_id ) )
def _check_second_ean13(self): for product in self: if product.second_ean13: if not product.ean13: raise ValidationError( _("You should use the second EAN13 field only when " "there is already a value in the main EAN13 field")) if not check_ean(product.second_ean13): raise ValidationError( _("The second EAN13 barcode is invalid."))
def ean13(self, record): if record['ean13'] in ['', '0']: backend_adapter = self.unit_for(GenericAdapter, 'tienda_nube.product.template') template = backend_adapter.read(record['id_product']) ean13 = template.get('ean13', {}) if ean13 == '0': return {} return template['ean13'] and {'ean13': template['ean13']} or {} if check_ean(record['ean13']): return {'ean13': record['ean13']} return {}
def _compute_barcode_image(self): for data in self: data.barcode_image = None if data.name != "/": if t_product.check_ean(data.name): try: barcode = self.env["report"].barcode( "EAN13", data.name, width=300, height=100, humanreadable=0 ) except (ValueError, AttributeError): raise Warning(_("Cannot convert into barcode.")) barcode_base64 = base64.b64encode(barcode) data.barcode_image = "data:image/png;base64," + barcode_base64
def test_generate_fresh_serial_id(self): name_1 = self.get_new_user_name() user_1 = self.create_user(name=name_1, login=name_1) name_2 = self.get_new_user_name() user_2 = self.create_user(name=name_2, login=name_2) user_1.serial_id = None user_2.serial_id = None user_1.generate_fresh_serial_id() user_2.generate_fresh_serial_id() for user in [user_1, user_2]: self.assertTrue(user.serial_id) self.assertTrue( check_ean(user.serial_id), 'The generate Serial Id "{0}" is not a valid EAN-13'.format( user.serial_id))
def test_generate_fresh_serial_id(self): name_1 = self.get_new_user_name() user_1 = self.create_user(name=name_1, login=name_1) name_2 = self.get_new_user_name() user_2 = self.create_user(name=name_2, login=name_2) user_1.serial_id = None user_2.serial_id = None user_1.generate_fresh_serial_id() user_2.generate_fresh_serial_id() for user in [user_1, user_2]: self.assertTrue(user.serial_id) self.assertTrue( check_ean(user.serial_id), 'The generate Serial Id "{0}" is not a valid EAN-13'.format( user.serial_id ) )
def create_new_product(self, cr, uid, line): vals = {} # Check if the EAN is valid # ------------------------- if line[1] and check_ean(line[1]): vals["ean13"] = line[1] else: return {"rejection": "product rejected because EAN is invalid."} # Determine the category # ---------------------- if line[10]: vals["categ_id"] = line[10] elif line[8]: vals["categ_id"] = line[8] elif line[6]: vals["categ_id"] = line[6] elif line[4]: vals["categ_id"] = line[4] elif line[2]: vals["categ_id"] = line[2] else: return {"rejection": "Product rejected because category is not provided."} vals["categ_id"] = next((x["id"] for x in self.categories if x["code"] == vals["categ_id"]), None) if not vals["categ_id"]: return {"rejection": "Product rejected because category is unknown."} vals["name"] = " ".join((line[41], line[40], line[42])) if not vals["name"]: return {"rejection": "Product rejected because name could not be determined."} # THR product code # ---------------- supplier = {} supplier["name"] = self.thr supplier["min_qty"] = 1 supplier["product_code"] = line[0] vals["seller_ids"] = [(0, False, supplier)] # Properties vals["properties"] = [] for i, prop in enumerate(line): if i < 24 or not prop: continue new_prop = {} new_prop["name"] = next((x["id"] for x in self.properties if x["name"] == self.header[i]), None) if not new_prop["name"]: continue new_prop["value"] = prop vals["properties"].append([0, False, new_prop]) vals["short_description"] = line[12] vals["description_sale"] = line[13] vals["procure_method"] = "make_to_order" vals["type"] = "product" vals["state"] = "draft" vals["recommended_price"] = line[24] return {"id": self.create(cr, uid, vals, context=None)}
def ean13(self, record): if record['ean13'] in ['', '0']: return {} if check_ean(record['ean13']): return {'ean13': record['ean13']} return {}
def test_create_generates_valid_ean(self): name = self.get_new_user_name() user = self.create_user(name=name, login=name) self.assertTrue(user.serial_id) self.assertTrue(check_ean(user.serial_id), 'The user\'s Serial Id is not a valid EAN-13 code.')
def _validate_serial_id(self, vals): if 'serial_id' in vals and not check_ean(vals['serial_id']): raise exceptions.Warning( _('Serial Id invalid format'), _('The Serial Id field has not the EAN-13 format standard.') )
def create_new_product(self, cr, uid, line): vals = {} # Check if the EAN is valid # ------------------------- if line[1] and check_ean(line[1]): vals['ean13'] = line[1] else: return {'rejection': 'product rejected because EAN is invalid.'} # Determine the category # ---------------------- if line[10]: vals['categ_id'] = line[10] elif line[8]: vals['categ_id'] = line[8] elif line[6]: vals['categ_id'] = line[6] elif line[4]: vals['categ_id'] = line[4] elif line[2]: vals['categ_id'] = line[2] else: return { 'rejection': 'Product rejected because category is not provided.' } vals['categ_id'] = next( (x['id'] for x in self.categories if x['code'] == vals['categ_id']), None) if not vals['categ_id']: return { 'rejection': 'Product rejected because category is unknown.' } vals['name'] = ' '.join((line[41], line[40], line[42])) if not vals['name']: return { 'rejection': 'Product rejected because name could not be determined.' } # THR product code # ---------------- supplier = {} supplier['name'] = self.thr supplier['min_qty'] = 1 supplier['product_code'] = line[0] vals['seller_ids'] = [(0, False, supplier)] # Properties vals['properties'] = [] for i, prop in enumerate(line): if i < 24 or not prop: continue new_prop = {} new_prop['name'] = next( (x['id'] for x in self.properties if x['name'] == self.header[i]), None) if not new_prop['name']: continue new_prop['value'] = prop vals['properties'].append([0, False, new_prop]) vals['short_description'] = line[12] vals['description_sale'] = line[13] vals['procure_method'] = 'make_to_order' vals['type'] = 'product' vals['state'] = 'draft' vals['recommended_price'] = line[24] return {'id': self.create(cr, uid, vals, context=None)}
def update(self): # TODO: Refactor to satisfy PEP C901 n = len(self.line_ids) pco = None t0 = datetime.now() for i, line in enumerate(self.line_ids): tdiff = datetime.now() - t0 if tdiff.seconds >= self.timeout: return if n != 1: self.progress = float(i) / (float(n) - 1) * 100.0 else: self.progress = 100.0 if int(self.progress) != pco: _logger.info('Import progress %.2f%% (%i/%i)' % (self.progress, i + 1, n)) pco = int(self.progress) if line.status: if line.status == 'done' and self.autoremove_done: line.unlink() continue line.status = False line.observations = False # check if product exists (defualt_code) if not line.default_code: line.status = 'error' line.observations = _("Reference cannot be null") continue product = {} pp = self.env['product.product'].search([('default_code', '=', line.default_code)]) if len(pp) > 1: if self.disambigute_duplicated_refs: if self.supplier_id.id: pp1 = pp.filtered(lambda x: self.supplier_id.id in x. seller_ids.mapped('name.id')) if len(pp1) == 1: pp = pp1 elif len(pp1) == 0: line.status = 'error' line.observations = _("There's more than one " "product with the same " "Reference and none of them " "have the Supplier") continue else: line.status = 'error' line.observations = _("There's more than one " "product with the same " "Reference and the same " "Supplier") continue else: line.status = 'error' line.observations = _( "There's more than one product with the same " "Reference. Select a Supplier to try to narrow " "the search") continue else: line.status = 'error' line.observations = _("There's more than one product with " "the same Reference. Try to enable " "Disambiguate duplicated and select " "a Supplier") continue product['status'] = 'update' if len(pp) == 1 else 'create' # check pricelist_sale if line.pricelist_sale: price = check_float_format(line.pricelist_sale) if price is None: line.status = 'error' line.observations = _('Unknown float format') continue else: line.pricelist_sale = self._format_decimal(price, ret_type='text') # check purchase priceist if line.pricelist_purchase: price = check_float_format(line.pricelist_purchase) if price is None: line.status = 'error' line.observations = _('Unknown float format') continue else: line.pricelist_purchase = self._format_decimal( price, ret_type='text') # populate data product['data'] = {} if product['status'] == 'update': product['object'] = pp if self.update_name: if line.name: if pp.name != pp.name: product['data'].update({'name': line.name}) # TODO: Update name on every language else: line.status = 'error' line.observations = _('Description cannot be null') continue if self.update_category: if line.category: status, cat, msg = self._get_cat(line.category) if status == 'error': line.status = status line.observations = msg continue if status == 'exists': line.category = cat.name if pp.categ_id != cat.id: product['data'].update({'categ_id': cat.id}) line.observations = msg else: if self.create_category_onupdate: product['category'] = { 'status': 'create', 'data': { 'name': line.category } } line.observations = msg else: line.status = 'error' line.observations = _( 'Category did not exist. Enable "Create ' 'Category" on update options to create it') continue else: line.status = 'error' line.observations = _('Category cannot be null') continue if self.update_ean13: if line.ean13: if pp.ean13 != line.ean13: if check_ean(line.ean13): product['data'].update({'ean13': line.ean13}) else: line.status = 'error' line.observations = _( 'Invalid "EAN13 Barcode"') continue else: line.status = 'error' line.observations = _('"EAN13 Barcode" cannot be null') continue if self.update_saleprice: if line.pricelist_sale: price = float(line.pricelist_sale) if not equal(pp.lst_price, price): product['data'].update({'lst_price': price}) else: line.status = 'error' line.observations = _('Sale Price cannot be null') continue if self.update_purchaseprice: if line.pricelist_purchase: if not self.supplier_id.id: line.status = 'error' line.observations = _( 'If there is a Purchase Pricelist the ' 'Supplier cannot be null') continue else: suppinfo = pp.seller_ids.filtered( lambda x: x.name == self.supplier_id) if len(suppinfo) > 1: line.status = 'error' line.observations = _( 'The Supplier is defined more than once') continue elif len(suppinfo) == 0: product['data'].update({ 'seller_ids': [(0, _, { 'name': self.supplier_id.id, 'delay': self.purchase_delay, 'pricelist_ids': [(0, _, { 'min_quantity': 0, 'price': float(line.pricelist_purchase) })] })] }) else: plist = suppinfo.pricelist_ids if len(plist) > 1: plist0 = plist.filtered( lambda x: x.min_quantity == 0) line.status = 'error' line.observations = _( 'There is more than one Purchase ' 'Pricelist') if len(plist0) == 0: line.observations += _( ' and none has quantity=0') elif len(plist0) == 1: line.observations = _( ' and just one has quantity=0') else: line.observations = _( ' and more than one has ' 'quantity=0') continue elif len(plist) == 0: product['data'].update({ 'seller_ids': [(1, suppinfo.id, { 'pricelist_ids': [(0, _, { 'min_quantity': 0, 'price': float(line.pricelist_purchase) })] })] }) else: price = float(line.pricelist_purchase) old_price = pp.seller_ids.filtered( lambda x: x.id == suppinfo.id ).mapped('pricelist_ids').filtered( lambda x: x.id == plist.id).price if not equal(old_price, price): product['data'].update({ 'seller_ids': [(1, suppinfo.id, { 'pricelist_ids': [(1, plist.id, { 'price': price })] })] }) else: line.status = 'error' line.observations = _('Purchase Price cannot be null') continue else: # Create product if not self.create_product: line.status = 'error' line.observations = _( "Product does not exist. Enable 'Create Product' " "to create it") continue product['data'].update({ 'default_code': line.default_code, 'sale_delay': self.sale_delay, 'type': self.product_type, 'cost_method': self.cost_method }) if line.name: product['data'].update({'name': line.name}) else: line.status = 'error' line.observations = _('Description cannot be null') continue if line.category: status, cat, msg = self._get_cat(line.category) if status == 'error': line.status = status line.observations = msg continue if status == 'exists': line.category = cat.name product['data'].update({'categ_id': cat.id}) line.observations = msg else: if self.create_category_oncreate: product['category'] = { 'status': 'create', 'data': { 'name': line.category } } line.observations = msg else: line.status = 'error' line.observations = _( 'Category did not exist. Enable ' '"Create Category" on create options to ' 'create it') continue else: line.status = 'error' line.observations = _('Category cannot be null') continue if line.ean13: if check_ean(line.ean13): product['data'].update({'ean13': line.ean13}) else: line.status = 'error' line.observations = _('Invalid "EAN13 Barcode"') continue if line.pricelist_sale: product['data'].update( {'lst_price': float(line.pricelist_sale)}) else: product['data'].update({'lst_price': self.lst_price}) if line.pricelist_purchase: if not self.supplier_id.id: line.status = 'error' line.observations = _( 'If there is a Purchase Pricelist the Supplier ' 'cannot be null') continue else: product['data'].update({ 'seller_ids': [(0, _, { 'name': self.supplier_id.id, 'delay': self.purchase_delay, 'pricelist_ids': [(0, _, { 'min_quantity': 0, 'price': float(line.pricelist_purchase) })] })] }) else: if self.supplier_id.id: if self.create_always_supplier: product['data'].update({ 'seller_ids': [(0, _, { 'name': self.supplier_id.id, 'delay': self.purchase_delay })] }) else: line.status = 'error' line.observations = _( "There is no Purchase Pricelist. Enable " "'Create always Supplier Info' to force the " "creation of a product with Supplier and " "without Purchase Pricelist") continue else: if not self.create_without_supplier: line.status = 'error' line.observations = _( "There is no Purchase Pricelist and there is " "no Supplier selected. Enable 'Create without " "Supplier Info' to force the creation of a " "product without Supplier and " "Purchase Pricelist") continue # save data if product.get('category') is not None: if product['category']['status'] == 'create': cat = self.env['product.category'].create( product['category']['data']) product['data'].update({'categ_id': cat.id}) if product['status'] == 'update': if product['data'] != {}: product['object'].write(product['data']) else: line.observations = _( "Nothing changed. Data already updated") else: self.env['product.product'].create(product['data']) line.status = 'done' if self.autoremove_done: line.unlink()
def _check_ean_key(self): if self.by_supplier: res = product.check_ean(string.zfill(self.name, 13)) if not res: raise Warning(_('Error: Invalid ean code.'))