def get_id_type(barcode): if barcode.isdigit(): # could be UPC/ISBN barcode_split = str(barcode) odd_chars = barcode_split[0:-1][::2] even_digits = barcode_split[1:-1][::2] sum_odd = sum(map(lambda x: int(x), odd_chars)) sum_even = sum(map(lambda x: int(x), even_digits)) if len(barcode_split) == 10: check_sum = 0 for index, elem in enumerate(barcode_split): check_sum += int(barcode_split[index]) * (10 - index) if check_sum % 11 == 0: return "ISBN" elif len(barcode_split) == 13: check_sum = (sum_even * 3) + sum_odd check_digit = check_sum % 10 if (check_sum % 10 == 0) else 10 - (check_sum % 10) if check_digit == barcode_split[-1]: return "ISBN" check_sum = (sum_odd * 3) + sum_even check_digit = (check_sum % 10) if check_digit != 0: check_digit = 10 - check_digit if check_digit == int(barcode_split[-1]): return "UPC" if barcodenumber.check_code('ean13', barcode_split): return "EAN" if barcodenumber.check_code('ean', barcode_split): return "EAN"
def gen_barcode(code): CODE39 = barcode.get_barcode_class('Code39') code39=CODE39(u"{0}".format(barcodenumber.check_code('CODE39',\ code),writer=ImageWriter())) code39.save('../tmp/barcode') return "../tmp/barcode.png"
def test_codes_numbers(self): ''' Test Bank codes ''' for code, number, result in CODES: if result: test = self.assertTrue else: test = self.assertFalse test(barcodenumber.check_code(code, number))
def save(self, force_insert=False, force_update=False, using=None, update_fields=None, hard_save=None, *args, **kwargs): # this snippet is probably not being used product = self.get_product() if product is not None: self.product = product # called if stock created by sku only sku = self.get_sku() if sku is not None: self.sku_instance = sku # called if stock created by ean and state only if self.ean_vollstaendig != "" and self.ean_vollstaendig is not None: if barcodenumber.check_code("ean13", self.ean_vollstaendig) is True: if self.ean_vollstaendig != "" and self.zustand != "": product = Product.objects.filter( ean=self.ean_vollstaendig, single_product__isnull=True, packing_unit=1).first() if product is None: product = Product.objects.create( ean=self.ean_vollstaendig or "") self.sku_instance = product.sku_set.filter( state=self.zustand, sku__icontains=product.main_sku).first() # if hard_save is None: # if is_stock_reserved(self) > self.bestand: # return super().save(*args, **kwargs)
def _check_ean13(self, code): return barcodenumber.check_code("ean13", code)
def _check_upc(self, code): return barcodenumber.check_code("upc", code)
def _check_isbn10(self, code): return barcodenumber.check_code("isbn10", code)
def ean13_validator(value): if not barcodenumber.check_code('ean13', str(value)): raise ValidationError( 'контрольная сумма штрихкода "%(value)s" неверна, проверьте введённые данные', params={'value': value})
def is_valid_barcode(barcode: str, barcode_type) -> bool: print(barcode) print(barcodenumber.check_code(barcode_type, barcode)) return barcodenumber.check_code(barcode_type, barcode)
################################### # START NUMBER ean_number = '8053831640150' # <<<---- HERE # modify your starting number here tot_ean_requested = 300 # produce tot ean codes in txt format name_file_output = 'ean13_numbers.txt' # name of the output file .txt for the numbers ################################### ean_produced = 0 if len(ean_number) != 13: print('error : your ean number has not 13 digits') else: while ean_produced < tot_ean_requested: valid = barcodenumber.check_code('ean13', ean_number) if valid: with open(name_file_output, '+a') as f: # every restart of this program delete the file .txt f.write(ean_number + '\n') ean_number = str(1 + int(ean_number)) ean_produced += 1 else: ean_number = str(1 + int(ean_number))
def stock_validation(instance, form): bestand = form.data.get("bestand", "") or "" bestand = bestand.strip() state = form.cleaned_data.get("zustand", "") or "" state = state.strip() ean = form.cleaned_data.get("ean_vollstaendig", "") or "" ean = ean.strip() sku = form.cleaned_data.get("sku", "") or "" sku = sku.strip() position = instance.lagerplatz or form.cleaned_data.get("lagerplatz", "") or "" position = position.strip() print(f"barrani: {position}") if ean != "": if barcodenumber.check_code("ean13", ean) is False: form.add_error("ean_vollstaendig", "Bitte geben Sie eine gültige EAN ein") validate_stock_is_not_reserved(instance, bestand, state, form) states_sku = None if instance is not None and instance.sku_instance is not None and instance.sku_instance.product is not None: if state != "" and ean == "": if state != instance.sku_instance.state: states_sku = instance.sku_instance.product.sku_set.filter( state=state, sku__icontains=instance.sku_instance.product.main_sku).first() validate_ean_has_not_multiple_products(ean, state, form) if validate_changed_state_of_sku_exists(instance, state, ean, form) is True: form.cleaned_data["sku"] = states_sku.sku form.cleaned_data["zustand"] = None validate_single_product_stock_is_not_greater_than_one(instance, bestand, form) if sku == "" and ean == "": form.add_error(None, "<h3 style='color:red;'>Sie müssen entweder eine EAN oder eine SKU eingeben</h3>") if sku != "" and ean != "": form.add_error(None, "<h3 style='color:red;'>Sie dürfen nur eine Angabe machen: EAN oder SKU</h3>") return sku_instance = None if sku != "": sku_instance = Sku.objects.filter(sku=sku, main_sku=True).first() if sku_instance is None: online_sku_instance = Sku.objects.filter(sku=sku, main_sku__isnull=True).first() print(f"wiee biee: {online_sku_instance}") if online_sku_instance is not None: form.add_error("sku", "Die angegebene SKU ist eine Online-SKU. Bitte geben Sie eine normale SKU an.") else: form.add_error("sku", "Die angegebene SKU ist im Sytem nicht vorhanden.") if sku != "" and state != "": # states_sku hat nur einen Wert, wenn der Zustand beim Updaten geändert wird if states_sku is None: form.add_error("zustand", "Wenn Sie beim Anlegen eine SKU angeben dürfen Sie keinen Zustand auswählen.") if ean != "": if state == "": form.add_error("zustand", "Wenn Sie eine EAN angeben, müssen Sie einen Zustand auswählen.") if sku_instance is not None: if sku_instance.product.packing_unit > 1: # product_ean = sku_instance.product.ean # if product_ean is not None and product_ean != "": # products = Product.objects.filter(ean=product_ean, packing_unit=1).exclude(product=sku_instance.product) form.add_error(None, "<p style='color:red;'>Von dem Artikel kann kein Bestand erfasst werden, " "da die Verpackungseinheit größer als 1 ist</p>") sku_or_changed_sku = form.cleaned_data.get("sku", "") or "" validate_stock_has_no_duplicates(ean, sku_or_changed_sku, state, position, form, instance, sku_instance)