Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 1 | # Copyright (c) 2012 Web Notes Technologies Pvt Ltd. |
| 2 | # License: GNU General Public License (v3). For more information see license.txt |
| 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 6 | import webnotes.defaults |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 7 | from webnotes.utils import cint, get_fullname, fmt_money |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 8 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 9 | class WebsitePriceListMissingError(webnotes.ValidationError): pass |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 10 | |
| 11 | @webnotes.whitelist() |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 12 | def update_cart(item_code, qty, with_doclist=0): |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 13 | quotation = _get_cart_quotation() |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 14 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 15 | qty = cint(qty) |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 16 | if qty == 0: |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 17 | 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 Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 25 | "qty": qty |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 26 | }) |
| 27 | else: |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 28 | quotation_items[0].qty = qty |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 29 | |
| 30 | quotation.ignore_permissions = True |
| 31 | quotation.save() |
| 32 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 33 | if with_doclist: |
| 34 | return decorate_quotation_doclist(quotation.doclist) |
| 35 | else: |
| 36 | return quotation.doc.name |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 37 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 38 | def 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 Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 51 | "territory": webnotes.conn.get_value("Shopping Cart Settings", None, "territory") or \ |
| 52 | "All Territories", |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 53 | "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 Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 60 | |
| 61 | @webnotes.whitelist() |
| 62 | def get_cart_quotation(): |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 63 | doclist = _get_cart_quotation(get_lead_or_customer()).doclist |
| 64 | return decorate_quotation_doclist(doclist) |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 65 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 66 | def 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 Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 75 | |
| 76 | def _get_cart_quotation(party=None): |
| 77 | if not party: |
| 78 | party = get_lead_or_customer() |
| 79 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 80 | 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 Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 89 | "quotation_to": party.doctype, |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 90 | "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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 99 | |
| 100 | def 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 | |
| 107 | def 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 Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 115 | where use_for_website=1 and ifnull(valid_for_all_countries, 0)=0 and |
| 116 | pl.name = plc.parent)""", country) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 117 | |
| 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 Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 125 | raise WebsitePriceListMissingError, "No website Price List specified" |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 126 | |
| 127 | return price_list_name |
| 128 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 129 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 130 | @webnotes.whitelist() |
| 131 | def checkout(): |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 132 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 133 | |
| 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 | |
| 146 | import unittest |
| 147 | test_dependencies = ["Item", "Price List", "Contact"] |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 148 | |
| 149 | class TestCart(unittest.TestCase): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 150 | 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 161 | def test_add_to_cart(self): |
| 162 | webnotes.session.user = "test@example.com" |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 163 | update_cart("_Test Item", 1) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 164 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 165 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 166 | 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 Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 172 | def test_update_cart(self): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 173 | self.test_add_to_cart() |
| 174 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 175 | update_cart("_Test Item", 5) |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 176 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 177 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 178 | 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 183 | |
| 184 | def test_remove_from_cart(self): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 185 | quotation0 = self.test_add_to_cart() |
| 186 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 187 | update_cart("_Test Item", 0) |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 188 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 189 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 190 | 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 194 | |
| 195 | def test_checkout(self): |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 196 | quotation = self.test_update_cart() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 197 | sales_order = checkout() |
| 198 | self.assertEquals(sales_order.doclist.getone({"item_code": "_Test Item"}).prevdoc_docname, quotation.doc.name) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 199 | |