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 |
| 6 | from webnotes import _, msgprint |
| 7 | import webnotes.defaults |
| 8 | from webnotes.utils import today, get_fullname |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 9 | |
| 10 | @webnotes.whitelist() |
| 11 | def add_to_cart(item_code): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 12 | update_qty(item_code, 1) |
| 13 | |
| 14 | @webnotes.whitelist() |
| 15 | def remove_from_cart(item_code): |
| 16 | update_qty(item_code, 0) |
| 17 | |
| 18 | @webnotes.whitelist() |
| 19 | def update_qty(item_code, qty_to_set): |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 20 | party = get_lead_or_customer() |
| 21 | quotation = get_shopping_cart_quotation(party) |
| 22 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 23 | 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 36 | |
| 37 | quotation.ignore_permissions = True |
| 38 | quotation.save() |
| 39 | |
| 40 | return quotation.doc.name |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 41 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 42 | def 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 | |
| 62 | def 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 82 | |
| 83 | def 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 | |
| 90 | def 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 111 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 112 | @webnotes.whitelist() |
| 113 | def 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 | |
| 129 | import unittest |
| 130 | test_dependencies = ["Item", "Price List", "Contact"] |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 131 | |
| 132 | class TestCart(unittest.TestCase): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 133 | 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 144 | def test_add_to_cart(self): |
| 145 | webnotes.session.user = "test@example.com" |
| 146 | add_to_cart("_Test Item") |
| 147 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 148 | 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 166 | |
| 167 | def test_remove_from_cart(self): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 168 | 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 177 | |
| 178 | def test_checkout(self): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 179 | 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 Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 182 | |