Merge pull request #4125 from rmehta/variant-in-website

[enhancement] add variants in website
This commit is contained in:
Rushabh Mehta 2015-10-14 16:22:36 +05:30
commit 53f77ad5e8
17 changed files with 882 additions and 689 deletions

View File

@ -224,3 +224,5 @@ erpnext.patches.v6_4.email_digest_update
execute:frappe.delete_doc_if_exists("DocType", "Applicable Territory")
execute:frappe.delete_doc_if_exists("DocType", "Shopping Cart Price List")
execute:frappe.delete_doc_if_exists("DocType", "Shopping Cart Taxes and Charges Master")
erpnext.patches.v6_4.set_user_in_contact
erpnext.patches.v6_4.make_image_thumbnail

View File

@ -0,0 +1,14 @@
import frappe
def execute():
frappe.reload_doctype("File")
frappe.reload_doctype("Item")
for item in frappe.get_all("Item", fields=("name", "website_image")):
if item.website_image:
item_doc = frappe.get_doc("Item", item.name)
try:
item_doc.make_thumbnail()
if item_doc.thumbnail:
item_doc.db_set("thumbnail", item_doc.thumbnail, update_modified=False)
except Exception:
print "Unable to make thumbnail for {0}".format(item.website_image)

View File

@ -0,0 +1,6 @@
import frappe
def execute():
frappe.reload_doctype("Contact")
frappe.db.sql("""update tabContact, tabUser set tabContact.user = tabUser.name
where tabContact.email_id = tabUser.email""")

View File

@ -20,7 +20,6 @@ $.extend(shopping_cart, {
if(!full_name || full_name==="Guest") {
if(localStorage) {
localStorage.setItem("last_visited", window.location.pathname);
localStorage.setItem("pending_add_to_cart", opts.item_code);
}
window.location.href = "/login";
} else {

View File

@ -19,6 +19,8 @@ class Quotation(SellingController):
self.validate_order_type()
self.validate_uom_is_integer("stock_uom", "qty")
self.validate_quotation_to()
if self.items:
self.with_items = 1
def has_sales_order(self):
return frappe.db.get_value("Sales Order Item", {"prevdoc_docname": self.name, "docstatus": 1})

View File

@ -66,11 +66,12 @@ def get_product_list_for_group(product_group=None, start=0, limit=10):
child_groups = ", ".join(['"' + i[0] + '"' for i in get_child_groups(product_group)])
# base query
query = """select name, item_name, page_name, website_image, item_group,
query = """select name, item_name, page_name, website_image, thumbnail, item_group,
web_long_description as website_description,
concat(parent_website_route, "/", page_name) as route
from `tabItem`
where show_in_website = 1
and (variant_of = '' or variant_of is null)
and (item_group in (%s)
or name in (select parent from `tabWebsite Item Group` where item_group in (%s)))
""" % (child_groups, child_groups)

View File

@ -143,7 +143,7 @@ def guess_territory():
def decorate_quotation_doc(doc):
for d in doc.get("items", []):
d.update(frappe.db.get_value("Item", d.item_code,
["website_image", "description", "page_name"], as_dict=True))
["thumbnail", "website_image", "description", "page_name"], as_dict=True))
return doc

View File

@ -1902,6 +1902,28 @@
"set_only_once": 0,
"unique": 0
},
{
"allow_on_submit": 0,
"bold": 0,
"collapsible": 0,
"fieldname": "thumbnail",
"fieldtype": "Data",
"hidden": 0,
"ignore_user_permissions": 0,
"in_filter": 0,
"in_list_view": 0,
"label": "Thumbnail",
"no_copy": 0,
"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,
"bold": 0,
@ -2264,4 +2286,4 @@
"read_only_onload": 0,
"search_fields": "item_name,description,item_group,customer_code",
"title_field": "item_name"
}
}

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
@ -65,6 +66,7 @@ class Item(WebsiteGenerator):
self.validate_has_variants()
self.validate_attributes()
self.validate_variant_attributes()
self.make_thumbnail()
if not self.get("__islocal"):
self.old_item_group = frappe.db.get_value(self.doctype, self.name, "item_group")
@ -78,13 +80,64 @@ class Item(WebsiteGenerator):
self.update_item_price()
self.update_variants()
def make_thumbnail(self):
"""Make a thumbnail of `website_image`"""
if self.website_image and not self.thumbnail:
file_doc = frappe.get_doc("File", {
"file_url": self.website_image,
"attached_to_doctype": "Item",
"attached_to_name": self.name
})
if not file_doc:
file_doc = frappe.get_doc({
"doctype": "File",
"file_url": self.website_image,
"attached_to_doctype": "Item",
"attached_to_name": self.name
}).insert()
if file_doc:
self.thumbnail = file_doc.make_thumbnail()
def get_context(self, context):
context["parent_groups"] = get_parent_item_groups(self.item_group) + \
context.parent_groups = get_parent_item_groups(self.item_group) + \
[{"name": self.name}]
if self.slideshow:
context.update(get_slideshow(self))
context["parents"] = self.get_parents(context)
if self.has_variants:
attribute_values_available = {}
context.attribute_values = {}
# load variants
context.variants = frappe.get_all("Item",
filters={"variant_of": self.name, "show_in_website": 1})
# load attributes
for v in context.variants:
v.attributes = frappe.get_all("Item Variant Attribute",
fields=["attribute", "attribute_value"], filters={"parent": v.name})
for attr in v.attributes:
values = attribute_values_available.setdefault(attr.attribute, [])
if attr.attribute_value not in values:
values.append(attr.attribute_value)
# filter attributes, order based on attribute table
for attr in self.attributes:
values = context.attribute_values.setdefault(attr.attribute, [])
# get list of values defined (for sequence)
for attr_value in frappe.db.get_all("Item Attribute Value",
fields=["attribute_value"], filters={"parent": attr.attribute}, order_by="idx asc"):
if attr_value.attribute_value in attribute_values_available.get(attr.attribute, []):
values.append(attr_value.attribute_value)
context.variant_info = json.dumps(context.variants)
context.parents = self.get_parents(context)
return context

View File

@ -30,6 +30,22 @@
<h4 class="item-price" itemprop="price"></h4>
<div class="item-stock" itemprop="availablity"></div>
<div class="item-cart hide">
{% if has_variants %}
{% for d in attributes %}
<div class="item-view-attribute"
style="margin-bottom: 10px;">
<h6 class="text-muted">{{ _(d.attribute) }}</h6>
<select class="form-control"
style="max-width: 140px"
data-attribute="{{ d.attribute }}">
{% for value in attribute_values[d.attribute] %}
<option value="{{ value }}">{{ _(value) }}</option>
{% endfor %}
</select>
</div>
{% endfor %}
{% endif %}
</select>
<div id="item-add-to-cart">
<button class="btn btn-primary btn-sm">
{{ _("Add to Cart") }}</button>
@ -76,13 +92,10 @@
<script>
{% include "templates/includes/product_page.js" %}
$(function() {
if(window.logged_in && getCookie("system_user")==="yes") {
frappe.has_permission("Item", "{{ name }}", "write", function(r) {
frappe.require("/assets/frappe/js/frappe/website/editable.js");
frappe.make_editable($('[itemprop="description"]'), "Item", "{{ name }}", "web_long_description");
});
}
});
{% if variant_info %}
window.variant_info = {{ variant_info }};
{% else %}
window.variant_info = null;
{% endif %}
</script>
{% endblock %}

View File

@ -2,7 +2,7 @@
<a class="product-link" href="{{ (route or page_name)|abs_url }}">
<div class="col-sm-2 col-xs-4 product-image-wrapper">
{{ product_image_square(website_image) }}
{{ product_image_square(thumbnail or website_image) }}
<div class="text-ellipsis inline-block small product-text">{{ item_name }}</div>
</div>
</a>

View File

@ -3,7 +3,8 @@
<div style="height: 120px; overflow: hidden;">
<a href="{{ (route or page_name)|abs_url }}">
{%- if website_image -%}
<img class="product-image" style="width: 80%; margin: auto;" src="{{ website_image|abs_url }}">
<img class="product-image" style="width: 80%; margin: auto;" src="{{
(thumbnail or website_image)|abs_url }}">
{%- else -%}
<div style="width: 80%; height: 120px; background-color: #F7FAFC;"></div>
{%- endif -%}

View File

@ -2,7 +2,7 @@
// License: GNU General Public License v3. See license.txt
frappe.ready(function() {
var item_code = $('[itemscope] [itemprop="productID"]').text().trim();
window.item_code = $('[itemscope] [itemprop="productID"]').text().trim();
var qty = 0;
frappe.call({
@ -12,7 +12,6 @@ frappe.ready(function() {
item_code: "{{ name }}"
},
callback: function(r) {
console.log(r.message);
$(".item-cart").toggleClass("hide", !!!r.message.price);
if(r.message && r.message.price) {
$(".item-price")
@ -38,7 +37,7 @@ frappe.ready(function() {
$("#item-add-to-cart button").on("click", function() {
shopping_cart.update_cart({
item_code: item_code,
item_code: get_item_code(),
qty: 1,
callback: function(r) {
if(!r.exc) {
@ -52,7 +51,7 @@ frappe.ready(function() {
$("#item-update-cart button").on("click", function() {
shopping_cart.update_cart({
item_code: item_code,
item_code: get_item_code(),
qty: $("#item-update-cart input").val(),
btn: this,
callback: function(r) {
@ -64,11 +63,6 @@ frappe.ready(function() {
},
});
});
if(localStorage && localStorage.getItem("pending_add_to_cart") && full_name) {
localStorage.removeItem("pending_add_to_cart");
$("#item-add-to-cart button").trigger("click");
}
});
var toggle_update_cart = function(qty) {
@ -77,3 +71,29 @@ var toggle_update_cart = function(qty) {
.toggle(qty ? true : false)
.find("input").val(qty);
}
function get_item_code() {
if(window.variant_info) {
attributes = {};
$('[itemscope]').find(".item-view-attribute select").each(function() {
attributes[$(this).attr('data-attribute')] = $(this).val();
});
for(var i in variant_info) {
var variant = variant_info[i];
var match = true;
for(var j in variant.attributes) {
if(attributes[variant.attributes[j].attribute]
!= variant.attributes[j].attribute_value) {
match = false;
break;
}
}
if(match) {
return variant.name;
}
}
throw "Unable to match variant";
} else {
return item_code;
}
}

View File

@ -12,9 +12,9 @@ no_sitemap = 1
@frappe.whitelist(allow_guest=True)
def get_product_list(search=None, start=0, limit=10):
# base query
query = """select name, item_name, page_name, website_image, item_group,
query = """select name, item_name, page_name, website_image, thumbnail, item_group,
web_long_description as website_description, parent_website_route
from `tabItem` where show_in_website = 1"""
from `tabItem` where show_in_website = 1 and (variant_of is null or variant_of = '')"""
# search term condition
if search:

View File

@ -4,11 +4,28 @@
{% include 'controllers/js/contact_address_common.js' %};
cur_frm.email_field = "email_id";
frappe.ui.form.on("Contact", "validate", function(frm) {
// clear linked customer / supplier / sales partner on saving...
$.each(["Customer", "Supplier", "Sales Partner"], function(i, doctype) {
var name = frm.doc[doctype.toLowerCase().replace(/ /g, "_")];
if(name && locals[doctype] && locals[doctype][name])
frappe.model.remove_from_locals(doctype, name);
});
frappe.ui.form.on("Contact", {
refresh: function(frm) {
if(!frm.doc.user && !frm.is_new() && frm.perm[0].write) {
frm.add_custom_button(__("Invite as User"), function() {
frappe.call({
method: "erpnext.utilities.doctype.contact.contact.invite_user",
args: {
contact: frm.doc.name
},
callback: function(r) {
frm.set_value("user", r.message);
}
});
});
}
},
validate: function(frm) {
// clear linked customer / supplier / sales partner on saving...
$.each(["Customer", "Supplier", "Sales Partner"], function(i, doctype) {
var name = frm.doc[doctype.toLowerCase().replace(/ /g, "_")];
if(name && locals[doctype] && locals[doctype][name])
frappe.model.remove_from_locals(doctype, name);
});
}
});

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
from __future__ import unicode_literals
import frappe
from frappe.utils import cstr, extract_email_id
from frappe.utils import cstr
from erpnext.controllers.status_updater import StatusUpdater
@ -22,6 +22,11 @@ class Contact(StatusUpdater):
def validate(self):
self.set_status()
self.validate_primary_contact()
self.set_user()
def set_user(self):
if not self.user and self.email_id:
self.user = frappe.db.get_value("User", {"email": self.email_id})
def validate_primary_contact(self):
if self.is_primary_contact == 1:
@ -53,6 +58,21 @@ class Contact(StatusUpdater):
frappe.db.sql("""update `tabIssue` set contact='' where contact=%s""",
self.name)
@frappe.whitelist()
def invite_user(contact):
contact = frappe.get_doc("Contact", contact)
if contact.has_permission("write"):
user = frappe.get_doc({
"doctype": "User",
"first_name": contact.first_name,
"last_name": contact.last_name,
"email": contact.email_id,
"user_type": "Website User",
"send_welcome_email": 1
}).insert(ignore_permissions = True)
return user.name
@frappe.whitelist()
def get_contact_details(contact):
contact = frappe.get_doc("Contact", contact)