def create_variant_and_submit(template_item_code, args):

    start_time = time.time()

    if isinstance(args, basestring):
        args = json.loads(args)

    template = frappe.get_doc("Item", template_item_code)

    start_time1 = time.time()
    create_missing_attributes_values(template, args)

    start_time2 = time.time()
    validate_item_variant_attributes(template, args)

    start_time3 = time.time()
    variant = erpnext.controllers.item_variant.get_variant(template.name, args)
    if variant is None:
        start_time4 = time.time()
        variant = create_variant(template, args)

        #Tenter de trouver un item avec le meme code
        #duplicate_item_code = frappe.db.sql_list("""select item_name from `tabItem` item where item_name=%s)""",variant.item_code)

        start_time5 = time.time()
        if not frappe.db.exists("Item", variant.item_code):
            variant.docs = 1
            variant.save(True)

        start_time6 = time.time()
        variant = frappe.get_doc("Item", variant.item_code)

        return variant

    return variant
Beispiel #2
0
    def validate_exising_items(self):
        '''Validate that if there are existing items with attributes, they are valid'''
        for item in frappe.db.sql(
                '''select distinct i.name from `tabItem Variant Attribute` iva, `tabItem` i
			where iva.attribute = %s and iva.parent = i.name and i.has_variants = 0''',
                self.name):
            validate_item_variant_attributes(item[0])
def create_variant_and_submit(template_item_code, args):
    print_debug = True
    if print_debug: frappe.errprint("--- create_variant_and_submit ---")

    start_time = time.time()

    if isinstance(args, basestring):
        args = json.loads(args)

    if print_debug: frappe.errprint(template_item_code)
    #if print_debug: frappe.errprint(args)

    template = frappe.get_doc("Item", template_item_code)

    start_time1 = time.time()
    create_missing_attributes_values(template, args)
    frappe.errprint("--- create_missing_attributes_values %s seconds ---" %
                    (time.time() - start_time1))

    start_time2 = time.time()
    validate_item_variant_attributes(template, args)
    frappe.errprint("--- validate_item_variant_attributes %s seconds ---" %
                    (time.time() - start_time2))

    start_time3 = time.time()
    variant = erpnext.controllers.item_variant.get_variant(template.name, args)
    frappe.errprint("--- get_variant %s seconds ---" %
                    (time.time() - start_time3))
    if variant is None:
        start_time4 = time.time()
        variant = create_variant(template, args)
        frappe.errprint("--- create_variant %s seconds ---" %
                        (time.time() - start_time4))

        #Tenter de trouver un item avec le meme code
        #duplicate_item_code = frappe.db.sql_list("""select item_name from `tabItem` item where item_name=%s)""",variant.item_code)

        start_time5 = time.time()
        if not frappe.db.exists("Item", variant.item_code):
            variant.docs = 1
            variant.save(True)
            frappe.errprint("--- variant.save(True) ---")
        frappe.errprint("--- save %s seconds ---" %
                        (time.time() - start_time5))

        start_time6 = time.time()
        variant = frappe.get_doc("Item", variant.item_code)
        frappe.errprint("--- get_doc %s seconds ---" %
                        (time.time() - start_time6))

        frappe.errprint("--- create_variant_and_submit %s seconds ---" %
                        (time.time() - start_time))
        return variant

    frappe.errprint("--- create_variant_and_submit %s seconds ---" %
                    (time.time() - start_time))
    return variant
Beispiel #4
0
	def validate_variant_attributes(self):
		if self.variant_of and self.variant_based_on=='Item Attribute':
			args = {}
			for d in self.attributes:
				if not d.attribute_value:
					frappe.throw(_("Please specify Attribute Value for attribute {0}").format(d.attribute))
				args[d.attribute] = d.attribute_value

			variant = get_variant(self.variant_of, args, self.name)
			if variant:
				frappe.throw(_("Item variant {0} exists with same attributes")
					.format(variant), ItemVariantExistsError)

			validate_item_variant_attributes(self, args)
Beispiel #5
0
	def validate_variant_attributes(self):
		if self.variant_of:
			args = {}
			for d in self.attributes:
				if not d.attribute_value:
					frappe.throw(_("Please specify Attribute Value for attribute {0}").format(d.attribute))
				args[d.attribute] = d.attribute_value

			variant = get_variant(self.variant_of, args, self.name)
			if variant:
				frappe.throw(_("Item variant {0} exists with same attributes")
					.format(variant), ItemVariantExistsError)

			validate_item_variant_attributes(self, args)
Beispiel #6
0
	def validate_variant_attributes(self):
		if self.is_new() and self.variant_of and self.variant_based_on == 'Item Attribute':
			args = {}
			for d in self.attributes:
				if cstr(d.attribute_value).strip() == '':
					frappe.throw(_("Please specify Attribute Value for attribute {0}").format(d.attribute))
				args[d.attribute] = d.attribute_value

			variant = get_variant(self.variant_of, args, self.name)
			if variant:
				frappe.throw(_("Item variant {0} exists with same attributes")
					.format(variant), ItemVariantExistsError)

			validate_item_variant_attributes(self, args)
Beispiel #7
0
	def validate_variant_attributes(self):
		if self.is_new() and self.variant_of and self.variant_based_on == 'Item Attribute':
			# remove attributes with no attribute_value set
			self.attributes = [d for d in self.attributes if cstr(d.attribute_value).strip()]

			args = {}
			for i, d in enumerate(self.attributes):
				d.idx = i + 1
				args[d.attribute] = d.attribute_value

			variant = get_variant(self.variant_of, args, self.name)
			if variant:
				frappe.throw(_("Item variant {0} exists with same attributes")
					.format(variant), ItemVariantExistsError)

			validate_item_variant_attributes(self, args)

			# copy variant_of value for each attribute row
			for d in self.attributes:
				d.variant_of = self.variant_of
Beispiel #8
0
	def validate_exising_items(self):
		'''Validate that if there are existing items with attributes, they are valid'''
		for item in frappe.db.sql('''select distinct i.name from `tabItem Variant Attribute` iva, `tabItem` i
			where iva.attribute = %s and iva.parent = i.name and i.has_variants = 0''', self.name):
			validate_item_variant_attributes(item[0])