<commit_before># Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe import _

def execute(filters=None):
	columns = get_columns(filters.item)
	data = get_data(filters.item)
	return columns, data

def get_data(item):
	if not item:
		return []
	item_dicts = []
	variants = None

	variant_results = frappe.db.sql("""select name from `tabItem`
		where variant_of = %s""", item, as_dict=1)
	if not variant_results:
		frappe.msgprint(_("There isn't any item variant for the selected item"))
		return []
	else:
		variants = ",".join(['"' + frappe.db.escape(variant['name']) + '"' for variant in variant_results])

	order_count_map = get_open_sales_orders_map(variants)
	stock_details_map = get_stock_details_map(variants)
	buying_price_map = get_buying_price_map(variants)
	selling_price_map = get_selling_price_map(variants)
	attr_val_map = get_attribute_values_map(variants)

	attribute_list = [d[0] for d in frappe.db.sql("""select attribute
		from `tabItem Variant Attribute`
		where parent in ({variants}) group by attribute""".format(variants=variants))]

	# Prepare dicts
	variant_dicts = [{"variant_name": d['name']} for d in variant_results]
	for item_dict in variant_dicts:
		name = item_dict["variant_name"]

		for d in attribute_list:
			item_dict[d] = attr_val_map[name][d]

		item_dict["Open Orders"] = order_count_map.get(name) or 0

		if stock_details_map.get(name):
			item_dict["Inventory"] = stock_details_map.get(name)["Inventory"] or 0
			item_dict["In Production"] = stock_details_map.get(name)["In Production"] or 0
			item_dict["Available Selling"] = stock_details_map.get(name)["Available Selling"] or 0
		else:
			item_dict["Inventory"] = item_dict["In Production"] = item_dict["Available Selling"] = 0

		item_dict["Avg. Buying Price List Rate"] = buying_price_map.get(name) or 0
		item_dict["Avg. Selling Price List Rate"] = selling_price_map.get(name) or 0

		item_dicts.append(item_dict)

	return item_dicts

def get_columns(item):
	columns = [{
		"fieldname": "variant_name",
		"label": "Variant",
		"fieldtype": "Link",
		"options": "Item",<commit_msg>[Fix] Item variant details report (#13545)<commit_after># Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt

from __future__ import unicode_literals
import frappe
from frappe import _

def execute(filters=None):
	columns = get_columns(filters.item)
	data = get_data(filters.item)
	return columns, data

def get_data(item):
	if not item:
		return []
	item_dicts = []
	variants = None

	variant_results = frappe.db.sql("""select name from `tabItem`
		where variant_of = %s""", item, as_dict=1)
	if not variant_results:
		frappe.msgprint(_("There isn't any item variant for the selected item"))
		return []
	else:
		variants = ",".join(['"' + frappe.db.escape(variant['name']) + '"' for variant in variant_results])

	order_count_map = get_open_sales_orders_map(variants)
	stock_details_map = get_stock_details_map(variants)
	buying_price_map = get_buying_price_map(variants)
	selling_price_map = get_selling_price_map(variants)
	attr_val_map = get_attribute_values_map(variants)

	attribute_list = [d[0] for d in frappe.db.sql("""select attribute
		from `tabItem Variant Attribute`
		where parent in ({variants}) group by attribute""".format(variants=variants))]

	# Prepare dicts
	variant_dicts = [{"variant_name": d['name']} for d in variant_results]
	for item_dict in variant_dicts:
		name = item_dict["variant_name"]

		for d in attribute_list:
			attr_dict = attr_val_map[name]
			if attr_dict and attr_dict.get(d):
				item_dict[d] = attr_val_map[name][d]

		item_dict["Open Orders"] = order_count_map.get(name) or 0

		if stock_details_map.get(name):
			item_dict["Inventory"] = stock_details_map.get(name)["Inventory"] or 0
			item_dict["In Production"] = stock_details_map.get(name)["In Production"] or 0
			item_dict["Available Selling"] = stock_details_map.get(name)["Available Selling"] or 0
		else:
			item_dict["Inventory"] = item_dict["In Production"] = item_dict["Available Selling"] = 0

		item_dict["Avg. Buying Price List Rate"] = buying_price_map.get(name) or 0
		item_dict["Avg. Selling Price List Rate"] = selling_price_map.get(name) or 0

		item_dicts.append(item_dict)

	return item_dicts

def get_columns(item):
	columns = [{
		"fieldname": "variant_name",
		"label": "Variant",
		"fieldtype": "Link",
		"options": "Item",
{'fraction_non_alphanumeric': 0.12295444493586909, 'fraction_numerical': 0.006855373728438744, 'mean_word_length': 5.661266568483064, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5563255', 'n_tokens_mistral': 1663, 'n_tokens_neox': 1551, 'n_words': 376}