[cart] create quotation on checkout

This commit is contained in:
Anand Doshi
2013-06-14 17:44:03 +05:30
parent 37982a6a1f
commit abc100302c
17 changed files with 306 additions and 59 deletions

173
website/helpers/cart.py Normal file
View File

@@ -0,0 +1,173 @@
# Copyright (c) 2012 Web Notes Technologies Pvt Ltd.
# License: GNU General Public License (v3). For more information see license.txt
from __future__ import unicode_literals
import webnotes
from webnotes import _, msgprint
import webnotes.defaults
from webnotes.utils import today, get_fullname
import json
@webnotes.whitelist()
def checkout(cart):
# webnotes.msgprint(cart);
if isinstance(cart, basestring):
cart = json.loads(cart) or {}
if webnotes.session.user == "Guest":
msgprint(_("Please login before you checkout!"), raise_exception=True)
elif webnotes.conn.get_value("Profile", webnotes.session.user, "user_type") != "Partner":
msgprint(_("Illegal User"), raise_exception=True)
# make_quotation(cart)
def make_quotation(cart):
from accounts.utils import get_fiscal_year
quotation_defaults = webnotes._dict({
"doctype": "Quotation",
"naming_series": "QTN-13-14-",
"quotation_to": "Customer",
"company": webnotes.defaults.get_user_default("company"),
"order_type": "Sales",
"status": "Draft",
})
quotation = webnotes.bean(quotation_defaults)
quotation.doc.fields.update({
"transaction_date": today(),
"fiscal_year": get_fiscal_year(today()),
# TODO
"price_list_name": "fetch",
"price_list_currency": "fetch",
"plc_conversion_rate": "something",
"currency": "same as price_list_currency",
"conversion_rate": "same as plc_converion_rate",
"territory": "fetch",
})
# TODO add items
for item_code, item in cart.items():
pass
# TODO apply taxes
# save and submit
@webnotes.whitelist()
def add_to_cart(item_code):
party = get_lead_or_customer()
quotation = get_shopping_cart_quotation(party)
quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": item_code})
if not quotation_items:
quotation.doclist.append({
"doctype": "Quotation Item",
"parentfield": "quotation_details",
"item_code": item_code,
"qty": 1
})
quotation.ignore_permissions = True
quotation.save()
return quotation.doc.name
def get_lead_or_customer():
customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, "customer")
if customer:
return webnotes.doc("Customer", customer)
lead = webnotes.conn.get_value("Lead", {"email_id": webnotes.session.user})
if lead:
return webnotes.doc("Lead", lead)
else:
lead_bean = webnotes.bean({
"doctype": "Lead",
"email_id": webnotes.session.user,
"lead_name": get_fullname(webnotes.session.user),
"status": "Open" # TODO: set something better???
})
lead_bean.ignore_permissions = True
lead_bean.insert()
return lead_bean.doc
def get_shopping_cart_quotation(party):
quotation = webnotes.conn.get_value("Quotation",
{party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0})
if quotation:
qbean = webnotes.bean("Quotation", quotation)
else:
qbean = webnotes.bean({
"doctype": "Quotation",
"naming_series": "QTN-CART-",
"quotation_to": "Customer",
"company": webnotes.defaults.get_user_default("company"),
"order_type": "Shopping Cart",
"status": "Draft",
"__islocal": 1,
"price_list_name": get_price_list(party),
(party.doctype.lower()): party.name
})
return qbean
@webnotes.whitelist()
def remove_from_cart(item_code):
pass
@webnotes.whitelist()
def update_qty(item_code, qty):
pass
def get_price_list(party):
if not party.default_price_list:
party.default_price_list = get_price_list_using_geoip()
party.save()
return party.default_price_list
def get_price_list_using_geoip():
country = webnotes.session.get("session_country")
price_list_name = None
if country:
price_list_name = webnotes.conn.sql("""select parent
from `tabPrice List Country` plc
where country=%s and exists (select name from `tabPrice List` pl
where use_for_website=1 and pl.name = plc.parent)""", country)
if price_list_name:
price_list_name = price_list_name[0][0]
else:
price_list_name = webnotes.conn.get_value("Price List",
{"use_for_website": 1, "valid_for_all_countries": 1})
if not price_list_name:
raise Exception, "No website Price List specified"
return price_list_name
import unittest
test_dependencies = ["Item", "Price List"]
class TestCart(unittest.TestCase):
def test_add_to_cart(self):
webnotes.session.user = "test@example.com"
add_to_cart("_Test Item")
def test_change_qty(self):
pass
def test_remove_from_cart(self):
pass
def test_checkout(self):
pass

View File

@@ -4,13 +4,13 @@
from __future__ import unicode_literals
import webnotes
from webnotes.utils import cstr, cint
from webnotes.utils import cstr, cint, fmt_money
from webnotes.webutils import build_html, delete_page_cache
@webnotes.whitelist(allow_guest=True)
def get_product_info(item_code):
"""get product price / stock info"""
price_list = webnotes.conn.get_value("Item", item_code, "website_price_list")
price_list = webnotes.conn.get_value("Price List", {"use_for_website": 1})
warehouse = webnotes.conn.get_value("Item", item_code, "website_warehouse")
if warehouse:
in_stock = webnotes.conn.sql("""select actual_qty from tabBin where
@@ -27,13 +27,16 @@ def get_product_info(item_code):
price = price and price[0] or None
if price:
price["formatted_price"] = fmt_money(price["ref_rate"], currency=price["ref_currency"])
price["ref_currency"] = not cint(webnotes.conn.get_default("hide_currency_symbol")) \
and (webnotes.conn.get_value("Currency", price.ref_currency, "symbol") or price.ref_currency) \
or ""
return {
"price": price,
"stock": in_stock
"stock": in_stock,
"uom": webnotes.conn.get_value("Item", item_code, "stock_uom")
}
@webnotes.whitelist(allow_guest=True)

View File

@@ -19,6 +19,7 @@
$(document).ready(function() {
// make list of items in the cart
wn.cart.render();
wn.cart.bind_events();
});
// shopping cart
@@ -28,23 +29,23 @@ $.extend(wn.cart, {
var $cart_wrapper = $("#cart-added-items").empty();
if(Object.keys(wn.cart.get_cart()).length) {
$('<div class="row">\
<div class="col col-lg-10 col-sm-10">\
<div class="col col-lg-9 col-sm-9">\
<div class="row">\
<div class="col col-lg-3"></div>\
<div class="col col-lg-9"><strong>Item Details</strong></div>\
</div>\
</div>\
<div class="col col-lg-2 col-sm-2"><strong>Qty</strong></div>\
<div class="col col-lg-3 col-sm-3"><strong>Qty</strong></div>\
</div><hr>').appendTo($cart_wrapper);
$.each(wn.cart.get_cart(), function(item_code, item) {
item.image_html = item.image ?
'<div style="height: 120px; overflow: hidden;"><img src="' + item.image + '" /></div>' :
'{% include "app/website/templates/html/product_missing_image.html" %}';
item.price_html = item.price ? ('<p>@ ' + item.price + '</p>') : "";
item.price_html = item.price ? ('<p>at ' + item.price + '</p>') : "";
$(repl('<div class="row">\
<div class="col col-lg-10 col-sm-10">\
<div class="col col-lg-9 col-sm-9">\
<div class="row">\
<div class="col col-lg-3">%(image_html)s</div>\
<div class="col col-lg-9">\
@@ -53,14 +54,47 @@ $.extend(wn.cart, {
</div>\
</div>\
</div>\
<div class="col col-lg-2 col-sm-2">\
<input type="text" placeholder="Qty" value="%(qty)s">\
<div class="col col-lg-3 col-sm-3">\
<p><input type="text" placeholder="Qty" value="%(qty)s" \
item_code="%(item_code)s" class="cart-input-qty"></p>\
%(price_html)s\
</div>\
</div><hr>', item)).appendTo($cart_wrapper);
});
$('<p class="text-right"><button type="button" class="btn btn-success checkout-btn">\
<span class="icon-ok"></span> Checkout</button></p>')
.appendTo($cart_wrapper);
} else {
$('<p class="alert">No Items added to cart.</p>').appendTo($cart_wrapper);
}
},
bind_events: function() {
// on change of qty
$(".cart-input-qty").on("change", function on_change_of_qty() {
wn.cart.set_value_in_cart($(this).attr("item_code"), "qty", $(this).val());
});
// shopping cart button
$(".checkout-btn").on("click", function() {
console.log("checkout!");
console.log(wn.cart.get_cart());
var user_is_logged_in = getCookie("full_name");
if(user_is_logged_in) {
wn.call({
method: "website.helpers.cart.checkout",
args: {cart: wn.cart.get_cart()},
btn: this,
callback: function(r) {
console.log(r);
}
});
} else {
window.location.href = "login?from=cart";
}
});
}
});

View File

@@ -26,8 +26,9 @@ $(document).ready(function() {
success: function(data) {
if(data.message) {
if(data.message.price) {
$("<h4>").html(data.message.price.ref_currency + " "
+ data.message.price.ref_rate).appendTo(".item-price");
$("<h4>")
.html(data.message.price.formatted_price + " per " + data.message.uom)
.appendTo(".item-price");
$(".item-price").removeClass("hide");
}
if(data.message.stock==0) {

View File

@@ -4,7 +4,7 @@
{% block content %}
<div class="col col-lg-12">
<h2 id="blog-title">Sales Partners</h2>
<h2 id="blog-title">Partners</h2>
<hr>
{% for partner_info in partners %}
<div class="row">