blob: a24afb57a97a97d05dba574fe7d8baf9342f1991 [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
Anand Doshiabc10032013-06-14 17:44:03 +05306import webnotes.defaults
Anand Doshic2a35272013-06-19 17:19:20 +05307from webnotes.utils import cint, get_fullname, fmt_money
Anand Doshiabc10032013-06-14 17:44:03 +05308
Anand Doshi3dceb842013-06-19 14:57:14 +05309class WebsitePriceListMissingError(webnotes.ValidationError): pass
Anand Doshie8132122013-06-17 15:42:38 +053010
11@webnotes.whitelist()
Anand Doshic2a35272013-06-19 17:19:20 +053012def update_cart(item_code, qty, with_doclist=0):
Anand Doshi3dceb842013-06-19 14:57:14 +053013 quotation = _get_cart_quotation()
Anand Doshiabc10032013-06-14 17:44:03 +053014
Anand Doshic2a35272013-06-19 17:19:20 +053015 qty = cint(qty)
Anand Doshi3dceb842013-06-19 14:57:14 +053016 if qty == 0:
Anand Doshie8132122013-06-17 15:42:38 +053017 quotation.set_doclist(quotation.doclist.get({"item_code": ["!=", item_code]}))
18 else:
19 quotation_items = quotation.doclist.get({"item_code": item_code})
20 if not quotation_items:
21 quotation.doclist.append({
22 "doctype": "Quotation Item",
23 "parentfield": "quotation_details",
24 "item_code": item_code,
Anand Doshi3dceb842013-06-19 14:57:14 +053025 "qty": qty
Anand Doshie8132122013-06-17 15:42:38 +053026 })
27 else:
Anand Doshi3dceb842013-06-19 14:57:14 +053028 quotation_items[0].qty = qty
Anand Doshiabc10032013-06-14 17:44:03 +053029
30 quotation.ignore_permissions = True
31 quotation.save()
32
Anand Doshic2a35272013-06-19 17:19:20 +053033 if with_doclist:
34 return decorate_quotation_doclist(quotation.doclist)
35 else:
36 return quotation.doc.name
Anand Doshie8132122013-06-17 15:42:38 +053037
Anand Doshiabc10032013-06-14 17:44:03 +053038def get_lead_or_customer():
39 customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, "customer")
40 if customer:
41 return webnotes.doc("Customer", customer)
42
43 lead = webnotes.conn.get_value("Lead", {"email_id": webnotes.session.user})
44 if lead:
45 return webnotes.doc("Lead", lead)
46 else:
47 lead_bean = webnotes.bean({
48 "doctype": "Lead",
49 "email_id": webnotes.session.user,
50 "lead_name": get_fullname(webnotes.session.user),
Anand Doshic2a35272013-06-19 17:19:20 +053051 "territory": webnotes.conn.get_value("Shopping Cart Settings", None, "territory") or \
52 "All Territories",
Anand Doshiabc10032013-06-14 17:44:03 +053053 "status": "Open" # TODO: set something better???
54 })
55 lead_bean.ignore_permissions = True
56 lead_bean.insert()
57
58 return lead_bean.doc
59
Anand Doshi3dceb842013-06-19 14:57:14 +053060
61@webnotes.whitelist()
62def get_cart_quotation():
Anand Doshic2a35272013-06-19 17:19:20 +053063 doclist = _get_cart_quotation(get_lead_or_customer()).doclist
64 return decorate_quotation_doclist(doclist)
Anand Doshi3dceb842013-06-19 14:57:14 +053065
Anand Doshic2a35272013-06-19 17:19:20 +053066def decorate_quotation_doclist(doclist):
67 for d in doclist:
68 if d.item_code:
69 d.fields.update(webnotes.conn.get_value("Item", d.item_code,
70 ["website_image", "web_short_description", "page_name"], as_dict=True))
71 d.formatted_rate = fmt_money(d.export_rate, currency=doclist[0].currency)
72 d.formatted_amount = fmt_money(d.export_amount, currency=doclist[0].currency)
73
74 return [d.fields for d in doclist]
Anand Doshi3dceb842013-06-19 14:57:14 +053075
76def _get_cart_quotation(party=None):
77 if not party:
78 party = get_lead_or_customer()
79
Anand Doshiabc10032013-06-14 17:44:03 +053080 quotation = webnotes.conn.get_value("Quotation",
81 {party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0})
82
83 if quotation:
84 qbean = webnotes.bean("Quotation", quotation)
85 else:
86 qbean = webnotes.bean({
87 "doctype": "Quotation",
88 "naming_series": "QTN-CART-",
Anand Doshic2a35272013-06-19 17:19:20 +053089 "quotation_to": party.doctype,
Anand Doshiabc10032013-06-14 17:44:03 +053090 "company": webnotes.defaults.get_user_default("company"),
91 "order_type": "Shopping Cart",
92 "status": "Draft",
93 "__islocal": 1,
94 "price_list_name": get_price_list(party),
95 (party.doctype.lower()): party.name
96 })
97
98 return qbean
Anand Doshiabc10032013-06-14 17:44:03 +053099
100def get_price_list(party):
101 if not party.default_price_list:
102 party.default_price_list = get_price_list_using_geoip()
103 party.save()
104
105 return party.default_price_list
106
107def get_price_list_using_geoip():
108 country = webnotes.session.get("session_country")
109 price_list_name = None
110
111 if country:
112 price_list_name = webnotes.conn.sql("""select parent
113 from `tabPrice List Country` plc
114 where country=%s and exists (select name from `tabPrice List` pl
Anand Doshic2a35272013-06-19 17:19:20 +0530115 where use_for_website=1 and ifnull(valid_for_all_countries, 0)=0 and
116 pl.name = plc.parent)""", country)
Anand Doshiabc10032013-06-14 17:44:03 +0530117
118 if price_list_name:
119 price_list_name = price_list_name[0][0]
120 else:
121 price_list_name = webnotes.conn.get_value("Price List",
122 {"use_for_website": 1, "valid_for_all_countries": 1})
123
124 if not price_list_name:
Anand Doshi3dceb842013-06-19 14:57:14 +0530125 raise WebsitePriceListMissingError, "No website Price List specified"
Anand Doshiabc10032013-06-14 17:44:03 +0530126
127 return price_list_name
128
Anand Doshiabc10032013-06-14 17:44:03 +0530129
Anand Doshie8132122013-06-17 15:42:38 +0530130@webnotes.whitelist()
131def checkout():
Anand Doshi3dceb842013-06-19 14:57:14 +0530132 quotation = _get_cart_quotation()
Anand Doshie8132122013-06-17 15:42:38 +0530133
134 quotation.ignore_permissions = True
135 quotation.submit()
136
137 sales_order = webnotes.bean(webnotes.map_doclist([["Quotation", "Sales Order"], ["Quotation Item", "Sales Order Item"],
138 ["Sales Taxes and Charges", "Sales Taxes and Charges"]], quotation.doc.name))
139
140 sales_order.ignore_permissions = True
141 sales_order.insert()
142 sales_order.submit()
143
144 return sales_order
145
146import unittest
147test_dependencies = ["Item", "Price List", "Contact"]
Anand Doshiabc10032013-06-14 17:44:03 +0530148
149class TestCart(unittest.TestCase):
Anand Doshie8132122013-06-17 15:42:38 +0530150 def test_get_lead_or_customer(self):
151 webnotes.session.user = "test@example.com"
152 party1 = get_lead_or_customer()
153 party2 = get_lead_or_customer()
154 self.assertEquals(party1.name, party2.name)
155 self.assertEquals(party1.doctype, "Lead")
156
157 webnotes.session.user = "test_contact_customer@example.com"
158 party = get_lead_or_customer()
159 self.assertEquals(party.name, "_Test Customer")
160
Anand Doshiabc10032013-06-14 17:44:03 +0530161 def test_add_to_cart(self):
162 webnotes.session.user = "test@example.com"
Anand Doshi3dceb842013-06-19 14:57:14 +0530163 update_cart("_Test Item", 1)
Anand Doshiabc10032013-06-14 17:44:03 +0530164
Anand Doshi3dceb842013-06-19 14:57:14 +0530165 quotation = _get_cart_quotation()
Anand Doshie8132122013-06-17 15:42:38 +0530166 quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"})
167 self.assertTrue(quotation_items)
168 self.assertEquals(quotation_items[0].qty, 1)
169
170 return quotation
171
Anand Doshi3dceb842013-06-19 14:57:14 +0530172 def test_update_cart(self):
Anand Doshie8132122013-06-17 15:42:38 +0530173 self.test_add_to_cart()
174
Anand Doshi3dceb842013-06-19 14:57:14 +0530175 update_cart("_Test Item", 5)
Anand Doshie8132122013-06-17 15:42:38 +0530176
Anand Doshi3dceb842013-06-19 14:57:14 +0530177 quotation = _get_cart_quotation()
Anand Doshie8132122013-06-17 15:42:38 +0530178 quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"})
179 self.assertTrue(quotation_items)
180 self.assertEquals(quotation_items[0].qty, 5)
181
182 return quotation
Anand Doshiabc10032013-06-14 17:44:03 +0530183
184 def test_remove_from_cart(self):
Anand Doshie8132122013-06-17 15:42:38 +0530185 quotation0 = self.test_add_to_cart()
186
Anand Doshi3dceb842013-06-19 14:57:14 +0530187 update_cart("_Test Item", 0)
Anand Doshie8132122013-06-17 15:42:38 +0530188
Anand Doshi3dceb842013-06-19 14:57:14 +0530189 quotation = _get_cart_quotation()
Anand Doshie8132122013-06-17 15:42:38 +0530190 self.assertEquals(quotation0.doc.name, quotation.doc.name)
191
192 quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"})
193 self.assertEquals(quotation_items, [])
Anand Doshiabc10032013-06-14 17:44:03 +0530194
195 def test_checkout(self):
Anand Doshi3dceb842013-06-19 14:57:14 +0530196 quotation = self.test_update_cart()
Anand Doshie8132122013-06-17 15:42:38 +0530197 sales_order = checkout()
198 self.assertEquals(sales_order.doclist.getone({"item_code": "_Test Item"}).prevdoc_docname, quotation.doc.name)
Anand Doshiabc10032013-06-14 17:44:03 +0530199