diff --git a/controllers/accounts_controller.py b/controllers/accounts_controller.py index bf5da8dd40..9ca68a7ee7 100644 --- a/controllers/accounts_controller.py +++ b/controllers/accounts_controller.py @@ -17,8 +17,9 @@ from __future__ import unicode_literals import webnotes from webnotes import _, msgprint -from webnotes.utils import flt, cint +from webnotes.utils import flt, cint, today from setup.utils import get_company_currency, get_price_list_currency +from accounts.utils import get_fiscal_year from utilities.transaction_base import TransactionBase, validate_conversion_rate import json @@ -36,6 +37,13 @@ class AccountsController(TransactionBase): self.validate_value("grand_total", ">=", 0) self.set_total_in_words() + def set_missing_values(self, for_validate=False): + for fieldname in ["posting_date", "transaction_date"]: + if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname): + self.doc.fields[fieldname] = today() + if not self.doc.fiscal_year: + self.doc.fiscal_year = get_fiscal_year(self.doc.fields[fieldname])[0] + def set_price_list_currency(self, buying_or_selling): # TODO - change this, since price list now has only one currency allowed if self.meta.get_field("price_list_name") and self.doc.price_list_name and \ @@ -45,6 +53,14 @@ class AccountsController(TransactionBase): "buying_or_selling": buying_or_selling })) + if not self.doc.plc_conversion_rate: + self.doc.plc_conversion_rate = flt(webnotes.conn.get_value("Price List", + self.doc.price_list_name, "conversion_rate")) + + if not self.doc.currency: + self.doc.currency = self.doc.price_list_currency + self.doc.conversion_rate = self.doc.plc_conversion_rate + def set_missing_item_details(self, get_item_details): """set missing item values""" for item in self.doclist.get({"parentfield": self.fname}): diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py index 36ce28541b..52dc3a8858 100644 --- a/controllers/buying_controller.py +++ b/controllers/buying_controller.py @@ -28,11 +28,8 @@ class WrongWarehouseCompany(Exception): pass class BuyingController(StockController): def onload_post_render(self): - self.set_price_list_currency("buying") - # contact, address, item details self.set_missing_values() - self.set_taxes("Purchase Taxes and Charges", "purchase_tax_details", "purchase_other_charges") def validate(self): @@ -41,6 +38,10 @@ class BuyingController(StockController): self.validate_warehouse_belongs_to_company() def set_missing_values(self, for_validate=False): + super(BuyingController, self).set_missing_values(for_validate) + + self.set_price_list_currency("Buying") + # set contact and address details for supplier, if they are not mentioned if self.doc.supplier and not (self.doc.contact_person and self.doc.supplier_address): for fieldname, val in self.get_default_address_and_contact("supplier").items(): diff --git a/controllers/selling_controller.py b/controllers/selling_controller.py index 3adc639271..087df344c8 100644 --- a/controllers/selling_controller.py +++ b/controllers/selling_controller.py @@ -25,21 +25,41 @@ from controllers.stock_controller import StockController class SellingController(StockController): def onload_post_render(self): - self.set_price_list_currency("selling") - # contact, address, item details and pos details (if applicable) self.set_missing_values() self.set_taxes("Sales Taxes and Charges", "other_charges", "charge") def set_missing_values(self, for_validate=False): + super(SellingController, self).set_missing_values(for_validate) + + self.set_price_list_currency("Selling") + # set contact and address details for customer, if they are not mentioned - if self.doc.customer and not (self.doc.contact_person and self.doc.customer_address): - for fieldname, val in self.get_default_address_and_contact("customer").items(): - if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname): - self.doc.fields[fieldname] = val + self.set_missing_lead_customer_details() self.set_missing_item_details(get_item_details) + + def set_missing_lead_customer_details(self): + if self.doc.customer: + if not (self.doc.contact_person and self.doc.customer_address): + for fieldname, val in self.get_default_address_and_contact("customer").items(): + if not self.doc.fields.get(fieldname) and self.meta.get_field(fieldname): + self.doc.fields[fieldname] = val + + customer_fetch = webnotes.conn.get_value("Customer", self.doc.customer, + ['customer_name', 'customer_group', 'territory'], as_dict=True) + for fieldname in ['customer_name', 'customer_group', 'territory']: + if not self.doc.fields.get(fieldname): + self.doc.fields[fieldname] = customer_fetch[fieldname] + + elif self.doc.lead: + lead_fetch = webnotes.conn.get_value("Lead", self.doc.lead, + ['company_name', 'lead_name', 'territory'], as_dict=True) + if not self.doc.customer_name: + self.doc.customer_name = lead_fetch.company_name or lead_fetch.lead_name + if not self.doc.territory: + self.doc.territory = lead_fetch.territory def get_other_charges(self): self.doclist = self.doc.clear_table(self.doclist, "other_charges") @@ -241,7 +261,7 @@ class SellingController(StockController): " " + _("should be 100%"), raise_exception=True) def validate_order_type(self): - valid_types = ["Sales", "Maintenance"] + valid_types = ["Sales", "Maintenance", "Shopping Cart"] if self.doc.order_type not in valid_types: msgprint(_(self.meta.get_label("order_type")) + " " + _("must be one of") + ": " + comma_or(valid_types), diff --git a/public/js/website_utils.js b/public/js/website_utils.js index 73fb04b107..1484f00946 100644 --- a/public/js/website_utils.js +++ b/public/js/website_utils.js @@ -201,5 +201,13 @@ $.extend(wn.cart, { update_display: function() { $(".cart-count").text("( " + wn.cart.get_count() + " )"); + }, + + set_value_in_cart: function(item_code, fieldname, value) { + var cart = this.get_cart(); + if(cart[item_code]) { + cart[item_code][fieldname] = value; + this.set_cart(cart); + } } }); \ No newline at end of file diff --git a/selling/doctype/lead/lead.txt b/selling/doctype/lead/lead.txt index 2463f017c6..0b602ea7e1 100644 --- a/selling/doctype/lead/lead.txt +++ b/selling/doctype/lead/lead.txt @@ -2,7 +2,7 @@ { "creation": "2013-04-10 11:45:37", "docstatus": 0, - "modified": "2013-04-10 11:49:11", + "modified": "2013-06-14 16:20:17", "modified_by": "Administrator", "owner": "Administrator" }, @@ -343,6 +343,13 @@ "oldfieldtype": "Select", "options": "\nProduct Enquiry\nRequest for Information\nSuggestions\nOther" }, + { + "doctype": "DocField", + "fieldname": "default_price_list", + "fieldtype": "Link", + "label": "Default Price List", + "options": "Price List" + }, { "doctype": "DocField", "fieldname": "fiscal_year", @@ -426,17 +433,6 @@ "options": "Company", "reqd": 0 }, - { - "doctype": "DocField", - "fieldname": "trash_reason", - "fieldtype": "Small Text", - "label": "Trash Reason", - "no_copy": 1, - "oldfieldname": "trash_reason", - "oldfieldtype": "Small Text", - "print_hide": 1, - "read_only": 1 - }, { "doctype": "DocField", "fieldname": "unsubscribed", diff --git a/selling/doctype/quotation/quotation.py b/selling/doctype/quotation/quotation.py index c474d998c2..d33cd6c1f0 100644 --- a/selling/doctype/quotation/quotation.py +++ b/selling/doctype/quotation/quotation.py @@ -170,9 +170,6 @@ class DocType(SellingController): def on_update(self): # Set Quotation Status webnotes.conn.set(self.doc, 'status', 'Draft') - - # subject for follow - self.doc.subject = '[%(status)s] To %(customer)s worth %(currency)s %(grand_total)s' % self.doc.fields #update enquiry #------------------ diff --git a/setup/doctype/price_list/price_list.py b/setup/doctype/price_list/price_list.py index eae6a3cdd3..c4feb60a2e 100644 --- a/setup/doctype/price_list/price_list.py +++ b/setup/doctype/price_list/price_list.py @@ -18,12 +18,9 @@ from __future__ import unicode_literals import webnotes from webnotes import msgprint, _ from webnotes.utils import cint, comma_or +from webnotes.model.controller import DocListController - -class DocType: - def __init__(self, d, dl): - self.doc, self.doclist = d, dl - +class DocType(DocListController): def onload(self): self.doclist.extend(webnotes.conn.sql("""select * from `tabItem Price` where price_list_name=%s""", self.doc.name, as_dict=True, update={"doctype": "Item Price"})) diff --git a/setup/doctype/price_list/price_list.txt b/setup/doctype/price_list/price_list.txt index ed432ec7b0..daffecedc3 100644 --- a/setup/doctype/price_list/price_list.txt +++ b/setup/doctype/price_list/price_list.txt @@ -2,7 +2,7 @@ { "creation": "2013-01-25 11:35:09", "docstatus": 0, - "modified": "2013-06-11 20:00:07", + "modified": "2013-06-14 16:07:25", "modified_by": "Administrator", "owner": "Administrator" }, @@ -60,6 +60,13 @@ "options": "Currency", "reqd": 1 }, + { + "doctype": "DocField", + "fieldname": "conversion_rate", + "fieldtype": "Float", + "label": "Conversion Rate", + "reqd": 1 + }, { "default": "Selling", "doctype": "DocField", diff --git a/setup/doctype/price_list/test_price_list.py b/setup/doctype/price_list/test_price_list.py index fe87821904..dc727c5ab7 100644 --- a/setup/doctype/price_list/test_price_list.py +++ b/setup/doctype/price_list/test_price_list.py @@ -3,6 +3,9 @@ test_records = [ "doctype": "Price List", "price_list_name": "_Test Price List", "currency": "INR", - "valid_for_all_countries": 1 + "valid_for_all_countries": 1, + "buying_or_selling": "Selling", + "use_for_website": 1, + "conversion_rate": 1.0 }] ] \ No newline at end of file diff --git a/setup/utils.py b/setup/utils.py index 69481171d6..b4f38ceb8e 100644 --- a/setup/utils.py +++ b/setup/utils.py @@ -41,8 +41,8 @@ def get_price_list_currency(args): args = json.loads(args) result = webnotes.conn.sql("""select distinct ref_currency from `tabItem Price` - where price_list_name=%s and buying_or_selling=%s""" % ("%s", args.get("buying_or_selling")), - (args.get("price_list_name"),)) + where price_list_name=%s and buying_or_selling=%s""", + (args.get("price_list_name"), args.get("buying_or_selling"))) if result and len(result)==1: return {"price_list_currency": result[0][0]} else: diff --git a/stock/doctype/item/item.txt b/stock/doctype/item/item.txt index 611ae5e937..e23bbe053e 100644 --- a/stock/doctype/item/item.txt +++ b/stock/doctype/item/item.txt @@ -2,7 +2,7 @@ { "creation": "2013-05-03 10:45:46", "docstatus": 0, - "modified": "2013-05-22 15:49:27", + "modified": "2013-06-13 16:17:42", "modified_by": "Administrator", "owner": "Administrator" }, @@ -810,16 +810,6 @@ "fieldtype": "Column Break", "read_only": 0 }, - { - "depends_on": "show_in_website", - "description": "Show Price in Website (if set)", - "doctype": "DocField", - "fieldname": "website_price_list", - "fieldtype": "Link", - "label": "Website Price List", - "options": "Price List", - "read_only": 0 - }, { "depends_on": "show_in_website", "description": "Show \"In Stock\" or \"Not in Stock\" based on stock available in this warehouse.", diff --git a/stock/doctype/item/test_item.py b/stock/doctype/item/test_item.py index f5a688ca70..e4f1144302 100644 --- a/stock/doctype/item/test_item.py +++ b/stock/doctype/item/test_item.py @@ -66,7 +66,8 @@ test_records = [ "parentfield": "ref_rate_details", "price_list_name": "_Test Price List", "ref_rate": 100, - "ref_currency": "INR" + "ref_currency": "INR", + "buying_or_selling": "Selling" } ], [{ diff --git a/website/helpers/cart.py b/website/helpers/cart.py new file mode 100644 index 0000000000..bcea3c2152 --- /dev/null +++ b/website/helpers/cart.py @@ -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 + \ No newline at end of file diff --git a/website/helpers/product.py b/website/helpers/product.py index a8b60fdbec..fb4d4e4aed 100644 --- a/website/helpers/product.py +++ b/website/helpers/product.py @@ -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) diff --git a/website/templates/js/cart.js b/website/templates/js/cart.js index 8746dd6d5a..eaa1fab553 100644 --- a/website/templates/js/cart.js +++ b/website/templates/js/cart.js @@ -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) { $('
@ ' + item.price + '
') : ""; + item.price_html = item.price ? ('at ' + item.price + '
') : ""; $(repl('No Items added to cart.
').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"; + } + }); } }); \ No newline at end of file diff --git a/website/templates/js/product_page.js b/website/templates/js/product_page.js index 338f25331d..e3e4c618ab 100644 --- a/website/templates/js/product_page.js +++ b/website/templates/js/product_page.js @@ -26,8 +26,9 @@ $(document).ready(function() { success: function(data) { if(data.message) { if(data.message.price) { - $("