blob: bcea3c2152947766e4fc064df4dbc9ef0b349eee [file] [log] [blame]
Anand Doshiabc10032013-06-14 17:44:03 +05301# Copyright (c) 2012 Web Notes Technologies Pvt Ltd.
2# License: GNU General Public License (v3). For more information see license.txt
3
4from __future__ import unicode_literals
5import webnotes
6from webnotes import _, msgprint
7import webnotes.defaults
8from webnotes.utils import today, get_fullname
9import json
10
11@webnotes.whitelist()
12def checkout(cart):
13 # webnotes.msgprint(cart);
14 if isinstance(cart, basestring):
15 cart = json.loads(cart) or {}
16
17 if webnotes.session.user == "Guest":
18 msgprint(_("Please login before you checkout!"), raise_exception=True)
19 elif webnotes.conn.get_value("Profile", webnotes.session.user, "user_type") != "Partner":
20 msgprint(_("Illegal User"), raise_exception=True)
21
22 # make_quotation(cart)
23
24def make_quotation(cart):
25 from accounts.utils import get_fiscal_year
26
27 quotation_defaults = webnotes._dict({
28 "doctype": "Quotation",
29 "naming_series": "QTN-13-14-",
30 "quotation_to": "Customer",
31 "company": webnotes.defaults.get_user_default("company"),
32 "order_type": "Sales",
33 "status": "Draft",
34 })
35
36 quotation = webnotes.bean(quotation_defaults)
37 quotation.doc.fields.update({
38 "transaction_date": today(),
39 "fiscal_year": get_fiscal_year(today()),
40
41 # TODO
42 "price_list_name": "fetch",
43 "price_list_currency": "fetch",
44 "plc_conversion_rate": "something",
45 "currency": "same as price_list_currency",
46 "conversion_rate": "same as plc_converion_rate",
47 "territory": "fetch",
48
49
50 })
51
52 # TODO add items
53 for item_code, item in cart.items():
54 pass
55
56 # TODO apply taxes
57
58 # save and submit
59
60@webnotes.whitelist()
61def add_to_cart(item_code):
62 party = get_lead_or_customer()
63 quotation = get_shopping_cart_quotation(party)
64
65 quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": item_code})
66 if not quotation_items:
67 quotation.doclist.append({
68 "doctype": "Quotation Item",
69 "parentfield": "quotation_details",
70 "item_code": item_code,
71 "qty": 1
72 })
73
74 quotation.ignore_permissions = True
75 quotation.save()
76
77 return quotation.doc.name
78
79def get_lead_or_customer():
80 customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, "customer")
81 if customer:
82 return webnotes.doc("Customer", customer)
83
84 lead = webnotes.conn.get_value("Lead", {"email_id": webnotes.session.user})
85 if lead:
86 return webnotes.doc("Lead", lead)
87 else:
88 lead_bean = webnotes.bean({
89 "doctype": "Lead",
90 "email_id": webnotes.session.user,
91 "lead_name": get_fullname(webnotes.session.user),
92 "status": "Open" # TODO: set something better???
93 })
94 lead_bean.ignore_permissions = True
95 lead_bean.insert()
96
97 return lead_bean.doc
98
99def get_shopping_cart_quotation(party):
100 quotation = webnotes.conn.get_value("Quotation",
101 {party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0})
102
103 if quotation:
104 qbean = webnotes.bean("Quotation", quotation)
105 else:
106 qbean = webnotes.bean({
107 "doctype": "Quotation",
108 "naming_series": "QTN-CART-",
109 "quotation_to": "Customer",
110 "company": webnotes.defaults.get_user_default("company"),
111 "order_type": "Shopping Cart",
112 "status": "Draft",
113 "__islocal": 1,
114 "price_list_name": get_price_list(party),
115 (party.doctype.lower()): party.name
116 })
117
118 return qbean
119
120@webnotes.whitelist()
121def remove_from_cart(item_code):
122 pass
123
124@webnotes.whitelist()
125def update_qty(item_code, qty):
126 pass
127
128def get_price_list(party):
129 if not party.default_price_list:
130 party.default_price_list = get_price_list_using_geoip()
131 party.save()
132
133 return party.default_price_list
134
135def get_price_list_using_geoip():
136 country = webnotes.session.get("session_country")
137 price_list_name = None
138
139 if country:
140 price_list_name = webnotes.conn.sql("""select parent
141 from `tabPrice List Country` plc
142 where country=%s and exists (select name from `tabPrice List` pl
143 where use_for_website=1 and pl.name = plc.parent)""", country)
144
145 if price_list_name:
146 price_list_name = price_list_name[0][0]
147 else:
148 price_list_name = webnotes.conn.get_value("Price List",
149 {"use_for_website": 1, "valid_for_all_countries": 1})
150
151 if not price_list_name:
152 raise Exception, "No website Price List specified"
153
154 return price_list_name
155
156import unittest
157
158test_dependencies = ["Item", "Price List"]
159
160class TestCart(unittest.TestCase):
161 def test_add_to_cart(self):
162 webnotes.session.user = "test@example.com"
163 add_to_cart("_Test Item")
164
165 def test_change_qty(self):
166 pass
167
168 def test_remove_from_cart(self):
169 pass
170
171 def test_checkout(self):
172 pass
173