Search Variants added to Item Master

Added feature for Variants to hande numeric values
This commit is contained in:
Neil Trini Lasrado 2015-08-05 11:59:30 +05:30
parent 169c3ed09d
commit 8b608c9043
9 changed files with 2618 additions and 922 deletions

View File

@ -30,6 +30,10 @@ frappe.ui.form.on("Item", {
frm.add_custom_button(__("Show Variants"), function() {
frappe.set_route("List", "Item", {"variant_of": frm.doc.name});
}, "icon-list", "btn-default");
frm.add_custom_button(__("Search Variant"), function() {
erpnext.item.search_variant()
}, "icon-list", "btn-default");
}
if (frm.doc.variant_of) {
frm.set_intro(__("This Item is a Variant of {0} (Template). Attributes will be copied over from the template unless 'No Copy' is set", [frm.doc.variant_of]), true);
@ -53,7 +57,6 @@ frappe.ui.form.on("Item", {
validate: function(frm){
erpnext.item.weight_to_validate(frm);
erpnext.item.variants_can_not_be_created_manually(frm);
},
image: function(frm) {
@ -83,15 +86,6 @@ frappe.ui.form.on("Item", {
is_stock_item: function(frm) {
erpnext.item.toggle_reqd(frm);
},
manage_variants: function(frm) {
if (cur_frm.doc.__unsaved==1) {
frappe.throw(__("You have unsaved changes. Please save."))
} else {
frappe.route_options = {"item_code": frm.doc.name };
frappe.set_route("List", "Manage Variants");
}
}
});
@ -189,11 +183,126 @@ $.extend(erpnext.item, {
validated = 0;
}
},
search_variant: function(doc) {
var fields = []
for(var i=0;i< cur_frm.doc.valid_attributes.length;i++){
var fieldtype, desc;
var row = cur_frm.doc.valid_attributes[i];
if (row.numeric_values){
fieldtype = "Float";
desc = "Min Value: "+ row.from_range +" , Max Value: "+ row.to_range +", in Increments of: "+ row.increment
}
else {
fieldtype = "Data";
desc = ""
}
fields = fields.concat({
"label": row.attribute,
"fieldname": row.attribute,
"fieldtype": fieldtype,
"reqd": 1,
"description": desc
})
}
fields = fields.concat({
"label": "Result",
"fieldname": "result",
"fieldtype": "HTML"
})
variants_can_not_be_created_manually: function(frm) {
if (frm.doc.__islocal && frm.doc.variant_of)
frappe.throw(__("Variants can not be created manually, add item attributes in the template item"))
var d = new frappe.ui.Dialog({
title: __("Search Variant"),
fields: fields
});
d.set_primary_action(__("Search"), function() {
args = d.get_values();
if(!args) return;
frappe.call({
method:"erpnext.stock.doctype.item.item.get_variant",
args: {
"item": cur_frm.doc.name,
"param": d.get_values()
},
callback: function(r) {
if (r.message) {
d.get_field("result").set_value($('<a class="btn btn-default btn-sm">'+__("View {0}", [r.message[0]])+'</a>')
.on("click", function() {
d.hide();
frappe.set_route("Form", "Item", r.message[0]);
}));
} else {
d.get_field("result").set_value($('<a class="btn btn-default btn-sm">'
+__("Variant Not Found - Create New"+'</a>')).on("click", function() {
d.hide();
frappe.call({
method:"erpnext.stock.doctype.item.item.create_variant",
args: {
"item": cur_frm.doc.name,
"param": d.get_values()
},
callback: function(r) {
var doclist = frappe.model.sync(r.message);
frappe.set_route("Form", doclist[0].doctype, doclist[0].name);
}
});
}));
}
}
});
});
d.show();
$.each(d.fields_dict, function(i, field) {
if(field.df.fieldtype !== "Data") {
return;
}
$(field.input_area).addClass("ui-front");
field.$input.autocomplete({
minLength: 0,
minChars: 0,
autoFocus: true,
source: function(request, response) {
frappe.call({
method:"frappe.client.get_list",
args:{
doctype:"Item Attribute Value",
filters: [
["parent","=", i],
["attribute_value", "like", request.term + "%"]
],
fields: ["attribute_value"]
},
callback: function(r) {
d.get_field("result").set_value("")
if (r.message) {
response($.map(r.message, function(d) { return d.attribute_value; }));
}
}
});
},
select: function(event, ui) {
field.$input.val(ui.item.value);
field.$input.trigger("change");
},
}).on("focus", function(){
setTimeout(function() {
if(!field.$input.val()) {
field.$input.autocomplete("search", "");
}
}, 500);
});
});
}
});
cur_frm.add_fetch('attribute', 'numeric_values', 'numeric_values');
cur_frm.add_fetch('attribute', 'from_range', 'from_range');
cur_frm.add_fetch('attribute', 'to_range', 'to_range');
cur_frm.add_fetch('attribute', 'increment', 'increment');

File diff suppressed because it is too large Load Diff

View File

@ -3,6 +3,7 @@
from __future__ import unicode_literals
import frappe
import json
from frappe import msgprint, _
from frappe.utils import cstr, flt, cint, getdate, now_datetime, formatdate
from frappe.website.website_generator import WebsiteGenerator
@ -63,6 +64,7 @@ class Item(WebsiteGenerator):
self.synced_with_hub = 0
self.validate_has_variants()
self.validate_stock_for_template_must_be_zero()
self.validate_template_attributes()
if not self.get("__islocal"):
self.old_item_group = frappe.db.get_value(self.doctype, self.name, "item_group")
@ -315,7 +317,7 @@ class Item(WebsiteGenerator):
or ifnull(reserved_qty, 0) > 0 or ifnull(indented_qty, 0) > 0 or ifnull(planned_qty, 0) > 0)""", self.name)
if stock_in:
frappe.throw(_("Item Template cannot have stock or Open Sales/Purchase/Production Orders."), ItemTemplateCannotHaveStock)
def validate_uom(self):
if not self.get("__islocal"):
check_stock_uom_with_bin(self.name, self.stock_uom)
@ -327,6 +329,15 @@ class Item(WebsiteGenerator):
if template_uom != self.stock_uom:
frappe.throw(_("Default Unit of Measure for Variant must be same as Template"))
def validate_template_attributes(self):
if self.has_variants:
attributes = []
for d in self.valid_attributes:
if d.attribute in attributes:
frappe.throw(_("Attribute {0} selected multiple times in Attributes Table".format(d.attribute)))
else:
attributes.append(d.attribute)
def validate_end_of_life(item_code, end_of_life=None, verbose=1):
if not end_of_life:
end_of_life = frappe.db.get_value("Item", item_code, "end_of_life")
@ -458,3 +469,60 @@ def check_stock_uom_with_bin(item, stock_uom):
frappe.throw(_("Default Unit of Measure for Item {0} cannot be changed directly because \
you have already made some transaction(s) with another UOM. To change default UOM, \
use 'UOM Replace Utility' tool under Stock module.").format(item))
@frappe.whitelist()
def get_variant(item, param):
args = json.loads(param)
attributes = {}
numeric_attributes = []
for t in frappe.db.get_all("Item Attribute Value", fields=["parent", "attribute_value"]):
attributes.setdefault(t.parent, []).append(t.attribute_value)
for t in frappe.get_list("Item Attribute", filters={"numeric_values":1}):
numeric_attributes.append(t.name)
for d in args:
if d in numeric_attributes:
values = frappe.db.sql("""select from_range, to_range, increment from `tabItem Template Attribute` \
where parent = %s and attribute = %s""", (item, d), as_dict=1)[0]
if (not values.from_range < args[d] < values.to_range) or ((args[d] - values.from_range) % values.increment != 0):
frappe.throw(_("Attribute value {0} for attribute {1} must be within range of {2} to {3} and in increments of {4}")
.format(args[d], d, values.from_range, values.to_range, values.increment))
else:
if args[d] not in attributes.get(d):
frappe.throw(_("Attribute value {0} for attribute {1} does not exist \
in Item Attribute Master.").format(args[d], d))
conds=""
attributes = ""
for d in args:
if conds:
conds+= " and "
attributes+= ", "
conds += """ exists(select iv.name from `tabVariant Attribute` iv where iv.parent = i.name and
iv.attribute= "{0}" and iv.attribute_value= "{1}")""".format(d, args[d])
attributes += "'{0}'".format(d)
conds += """and not exists (select iv.name from `tabVariant Attribute` iv where iv.parent = i.name and
iv.attribute not in ({0}))""".format(attributes)
variant= frappe.db.sql("""select i.name from tabItem i where {0}""".format(conds))
return variant
@frappe.whitelist()
def create_variant(item, param):
from erpnext.stock.doctype.manage_variants.manage_variants import copy_attributes_to_variant
args = json.loads(param)
variant = frappe.new_doc("Item")
variant.item_code = item
variant_attributes = []
for d in args:
variant_attributes.append({
"attribute": d,
"attribute_value": args[d]
})
variant.set("attributes", variant_attributes)
copy_attributes_to_variant(item, variant)
return variant

View File

@ -26,24 +26,211 @@
"report_hide": 0,
"reqd": 1,
"search_index": 0,
"set_only_once": 0
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"default": "1",
"depends_on": "eval: doc.numeric_values==0",
"description": "Lower the number, higher the priority in the Item Code suffix that will be created for this Item Attribute for the Item Variant",
"fieldname": "priority",
"fieldtype": "Int",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Priority",
"no_copy": 0,
"permlevel": 0,
"precision": ""
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "column_break_3",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"default": "0",
"fieldname": "numeric_values",
"fieldtype": "Check",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Numeric Values",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "numeric_values",
"fieldname": "section_break_5",
"fieldtype": "Section Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "from_range",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "From Range",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "increment",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Increment",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "column_break_8",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "to_range",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "To Range",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "eval: doc.numeric_values==0",
"fieldname": "section_break_10",
"fieldtype": "Section Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "item_attribute_values",
"fieldtype": "Table",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Item Attribute Values",
"no_copy": 0,
"options": "Item Attribute Value",
"permlevel": 0,
"precision": ""
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
}
],
"hide_heading": 0,
@ -54,7 +241,7 @@
"is_submittable": 0,
"issingle": 0,
"istable": 0,
"modified": "2015-07-13 05:28:20.561939",
"modified": "2015-08-05 05:27:55.912105",
"modified_by": "Administrator",
"module": "Stock",
"name": "Item Attribute",
@ -69,6 +256,7 @@
"delete": 1,
"email": 0,
"export": 0,
"if_owner": 0,
"import": 0,
"permlevel": 0,
"print": 0,

View File

@ -8,10 +8,20 @@ from frappe import _
class ItemAttribute(Document):
def validate(self):
self.validate_numeric()
self.validate_duplication()
self.validate_attribute_values()
def validate_numeric(self):
if self.numeric_values:
self.set("item_attribute_values", [])
if not self.from_range or not self.to_range:
frappe.throw(_("Please specify from/to Range"))
elif self.from_range > self.to_range:
frappe.throw(_("From Range cannot be greater than to Range"))
else:
self.from_range = self.to_range = self.increment = 0
def validate_duplication(self):
values, abbrs = [], []
for d in self.item_attribute_values:
@ -33,4 +43,5 @@ class ItemAttribute(Document):
if variant_attributes:
for d in variant_attributes:
if d[0] not in attribute_values:
frappe.throw(_("Attribute Value {0} cannot be removed from {1} as Item Variants exist with this Attribute.").format(d[0], self.name))
frappe.throw(_("Attribute Value {0} cannot be removed from {1} as Item Variants \
exist with this Attribute.").format(d[0], self.name))

View File

@ -0,0 +1,192 @@
{
"allow_copy": 0,
"allow_import": 1,
"allow_rename": 0,
"autoname": "",
"creation": "2015-07-31 02:14:41.660844",
"custom": 0,
"docstatus": 0,
"doctype": "DocType",
"document_type": "Other",
"fields": [
{
"allow_on_submit": 0,
"fieldname": "attribute",
"fieldtype": "Link",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 1,
"label": "Attribute",
"no_copy": 0,
"options": "Item Attribute",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 1,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "column_break_2",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "numeric_values",
"fieldtype": "Check",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Numeric Values",
"no_copy": 0,
"options": "",
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 1,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"depends_on": "numeric_values",
"fieldname": "section_break_4",
"fieldtype": "Section Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "from_range",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "From Range",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "increment",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Increment",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "column_break_7",
"fieldtype": "Column Break",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"fieldname": "to_range",
"fieldtype": "Float",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "To Range",
"no_copy": 0,
"permlevel": 0,
"precision": "",
"print_hide": 0,
"read_only": 0,
"report_hide": 0,
"reqd": 0,
"search_index": 0,
"set_only_once": 0,
"unique": 0
}
],
"hide_heading": 0,
"hide_toolbar": 0,
"icon": "",
"in_create": 0,
"in_dialog": 0,
"is_submittable": 0,
"issingle": 0,
"istable": 1,
"modified": "2015-08-05 07:50:03.836731",
"modified_by": "Administrator",
"module": "Stock",
"name": "Item Template Attribute",
"name_case": "",
"owner": "Administrator",
"permissions": [],
"read_only": 0,
"read_only_onload": 0,
"sort_field": "modified",
"sort_order": "DESC"
}

View File

@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
class ItemTemplateAttribute(Document):
pass

View File

@ -6,6 +6,7 @@ from __future__ import unicode_literals
import frappe
from frappe import _
from frappe.model.document import Document
from frappe.utils import cstr
import copy
import json
@ -204,4 +205,4 @@ def copy_attributes_to_variant(item, variant, variant_attribute=None, insert=Fal
if variant.attributes:
variant.description += "\n"
for d in variant.attributes:
variant.description += "<p>" + d.attribute + ": " + d.attribute_value + "</p>"
variant.description += "<p>" + d.attribute + ": " + cstr(d.attribute_value) + "</p>"