blob: efeb9759a21e95d5306669cc08e41be3e4abbc7f [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
Anand Doshiabc10032013-06-14 17:44:03 +05309
10@webnotes.whitelist()
11def add_to_cart(item_code):
Anand Doshie8132122013-06-17 15:42:38 +053012 update_qty(item_code, 1)
13
14@webnotes.whitelist()
15def remove_from_cart(item_code):
16 update_qty(item_code, 0)
17
18@webnotes.whitelist()
19def update_qty(item_code, qty_to_set):
Anand Doshiabc10032013-06-14 17:44:03 +053020 party = get_lead_or_customer()
21 quotation = get_shopping_cart_quotation(party)
22
Anand Doshie8132122013-06-17 15:42:38 +053023 if qty_to_set == 0:
24 quotation.set_doclist(quotation.doclist.get({"item_code": ["!=", item_code]}))
25 else:
26 quotation_items = quotation.doclist.get({"item_code": item_code})
27 if not quotation_items:
28 quotation.doclist.append({
29 "doctype": "Quotation Item",
30 "parentfield": "quotation_details",
31 "item_code": item_code,
32 "qty": qty_to_set
33 })
34 else:
35 quotation_items[0].qty = qty_to_set
Anand Doshiabc10032013-06-14 17:44:03 +053036
37 quotation.ignore_permissions = True
38 quotation.save()
39
40 return quotation.doc.name
Anand Doshie8132122013-06-17 15:42:38 +053041
Anand Doshiabc10032013-06-14 17:44:03 +053042def get_lead_or_customer():
43 customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, "customer")
44 if customer:
45 return webnotes.doc("Customer", customer)
46
47 lead = webnotes.conn.get_value("Lead", {"email_id": webnotes.session.user})
48 if lead:
49 return webnotes.doc("Lead", lead)
50 else:
51 lead_bean = webnotes.bean({
52 "doctype": "Lead",
53 "email_id": webnotes.session.user,
54 "lead_name": get_fullname(webnotes.session.user),
55 "status": "Open" # TODO: set something better???
56 })
57 lead_bean.ignore_permissions = True
58 lead_bean.insert()
59
60 return lead_bean.doc
61
62def get_shopping_cart_quotation(party):
63 quotation = webnotes.conn.get_value("Quotation",
64 {party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0})
65
66 if quotation:
67 qbean = webnotes.bean("Quotation", quotation)
68 else:
69 qbean = webnotes.bean({
70 "doctype": "Quotation",
71 "naming_series": "QTN-CART-",
72 "quotation_to": "Customer",
73 "company": webnotes.defaults.get_user_default("company"),
74 "order_type": "Shopping Cart",
75 "status": "Draft",
76 "__islocal": 1,
77 "price_list_name": get_price_list(party),
78 (party.doctype.lower()): party.name
79 })
80
81 return qbean
Anand Doshiabc10032013-06-14 17:44:03 +053082
83def get_price_list(party):
84 if not party.default_price_list:
85 party.default_price_list = get_price_list_using_geoip()
86 party.save()
87
88 return party.default_price_list
89
90def get_price_list_using_geoip():
91 country = webnotes.session.get("session_country")
92 price_list_name = None
93
94 if country:
95 price_list_name = webnotes.conn.sql("""select parent
96 from `tabPrice List Country` plc
97 where country=%s and exists (select name from `tabPrice List` pl
98 where use_for_website=1 and pl.name = plc.parent)""", country)
99
100 if price_list_name:
101 price_list_name = price_list_name[0][0]
102 else:
103 price_list_name = webnotes.conn.get_value("Price List",
104 {"use_for_website": 1, "valid_for_all_countries": 1})
105
106 if not price_list_name:
107 raise Exception, "No website Price List specified"
108
109 return price_list_name
110
Anand Doshiabc10032013-06-14 17:44:03 +0530111
Anand Doshie8132122013-06-17 15:42:38 +0530112@webnotes.whitelist()
113def checkout():
114 party = get_lead_or_customer()
115 quotation = get_shopping_cart_quotation(party)
116
117 quotation.ignore_permissions = True
118 quotation.submit()
119
120 sales_order = webnotes.bean(webnotes.map_doclist([["Quotation", "Sales Order"], ["Quotation Item", "Sales Order Item"],
121 ["Sales Taxes and Charges", "Sales Taxes and Charges"]], quotation.doc.name))
122
123 sales_order.ignore_permissions = True
124 sales_order.insert()
125 sales_order.submit()
126
127 return sales_order
128
129import unittest
130test_dependencies = ["Item", "Price List", "Contact"]
Anand Doshiabc10032013-06-14 17:44:03 +0530131
132class TestCart(unittest.TestCase):
Anand Doshie8132122013-06-17 15:42:38 +0530133 def test_get_lead_or_customer(self):
134 webnotes.session.user = "test@example.com"
135 party1 = get_lead_or_customer()
136 party2 = get_lead_or_customer()
137 self.assertEquals(party1.name, party2.name)
138 self.assertEquals(party1.doctype, "Lead")
139
140 webnotes.session.user = "test_contact_customer@example.com"
141 party = get_lead_or_customer()
142 self.assertEquals(party.name, "_Test Customer")
143
Anand Doshiabc10032013-06-14 17:44:03 +0530144 def test_add_to_cart(self):
145 webnotes.session.user = "test@example.com"
146 add_to_cart("_Test Item")
147
Anand Doshie8132122013-06-17 15:42:38 +0530148 quotation = get_shopping_cart_quotation(get_lead_or_customer())
149 quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"})
150 self.assertTrue(quotation_items)
151 self.assertEquals(quotation_items[0].qty, 1)
152
153 return quotation
154
155 def test_update_qty(self):
156 self.test_add_to_cart()
157
158 update_qty("_Test Item", 5)
159
160 quotation = get_shopping_cart_quotation(get_lead_or_customer())
161 quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"})
162 self.assertTrue(quotation_items)
163 self.assertEquals(quotation_items[0].qty, 5)
164
165 return quotation
Anand Doshiabc10032013-06-14 17:44:03 +0530166
167 def test_remove_from_cart(self):
Anand Doshie8132122013-06-17 15:42:38 +0530168 quotation0 = self.test_add_to_cart()
169
170 remove_from_cart("_Test Item")
171
172 quotation = get_shopping_cart_quotation(get_lead_or_customer())
173 self.assertEquals(quotation0.doc.name, quotation.doc.name)
174
175 quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"})
176 self.assertEquals(quotation_items, [])
Anand Doshiabc10032013-06-14 17:44:03 +0530177
178 def test_checkout(self):
Anand Doshie8132122013-06-17 15:42:38 +0530179 quotation = self.test_update_qty()
180 sales_order = checkout()
181 self.assertEquals(sales_order.doclist.getone({"item_code": "_Test Item"}).prevdoc_docname, quotation.doc.name)
Anand Doshiabc10032013-06-14 17:44:03 +0530182