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 | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 12 | def get_cart_quotation(doclist=None): |
| 13 | party = get_lead_or_customer() |
| 14 | |
| 15 | if not doclist: |
| 16 | doclist = _get_cart_quotation(party).doclist |
| 17 | |
| 18 | return { |
| 19 | "doclist": decorate_quotation_doclist(doclist), |
| 20 | "addresses": [{"name": address.name, "display": address.display} |
| 21 | for address in get_address_docs(party)] |
| 22 | } |
| 23 | |
| 24 | @webnotes.whitelist() |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 25 | def update_cart(item_code, qty, with_doclist=0): |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 26 | quotation = _get_cart_quotation() |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 27 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 28 | qty = cint(qty) |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 29 | if qty == 0: |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 30 | quotation.set_doclist(quotation.doclist.get({"item_code": ["!=", item_code]})) |
| 31 | else: |
| 32 | quotation_items = quotation.doclist.get({"item_code": item_code}) |
| 33 | if not quotation_items: |
| 34 | quotation.doclist.append({ |
| 35 | "doctype": "Quotation Item", |
| 36 | "parentfield": "quotation_details", |
| 37 | "item_code": item_code, |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 38 | "qty": qty |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 39 | }) |
| 40 | else: |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 41 | quotation_items[0].qty = qty |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 42 | |
| 43 | quotation.ignore_permissions = True |
| 44 | quotation.save() |
| 45 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 46 | if with_doclist: |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 47 | return get_cart_quotation(quotation.doclist) |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 48 | else: |
| 49 | return quotation.doc.name |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 50 | |
| 51 | @webnotes.whitelist() |
| 52 | def update_cart_address(address_fieldname, address_name): |
| 53 | from utilities.transaction_base import get_address_display |
| 54 | |
| 55 | quotation = _get_cart_quotation() |
| 56 | address_display = get_address_display(webnotes.doc("Address", address_name).fields) |
| 57 | |
| 58 | if address_fieldname == "shipping_address_name": |
| 59 | quotation.doc.shipping_address_name = address_name |
| 60 | quotation.doc.shipping_address = address_display |
| 61 | |
| 62 | if not quotation.doc.customer_address: |
| 63 | address_fieldname == "customer_address" |
| 64 | |
| 65 | if address_fieldname == "customer_address": |
| 66 | quotation.doc.customer_address = address_name |
| 67 | quotation.doc.address_display = address_display |
| 68 | |
| 69 | |
| 70 | quotation.ignore_permissions = True |
| 71 | quotation.save() |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 72 | |
| 73 | apply_cart_settings(quotation=quotation) |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 74 | |
| 75 | return get_cart_quotation(quotation.doclist) |
| 76 | |
| 77 | @webnotes.whitelist() |
| 78 | def get_addresses(): |
| 79 | return [d.fields for d in get_address_docs()] |
| 80 | |
| 81 | @webnotes.whitelist() |
| 82 | def save_address(fields, address_fieldname=None): |
| 83 | party = get_lead_or_customer() |
| 84 | fields = webnotes.load_json(fields) |
| 85 | |
| 86 | if fields.get("name"): |
| 87 | bean = webnotes.bean("Address", fields.get("name")) |
| 88 | else: |
| 89 | bean = webnotes.bean({"doctype": "Address", "__islocal": 1}) |
| 90 | |
| 91 | bean.doc.fields.update(fields) |
| 92 | |
| 93 | party_fieldname = party.doctype.lower() |
| 94 | bean.doc.fields.update({ |
| 95 | party_fieldname: party.name, |
| 96 | (party_fieldname + "_name"): party.fields[party_fieldname + "_name"] |
| 97 | }) |
| 98 | bean.ignore_permissions = True |
| 99 | bean.save() |
| 100 | |
| 101 | if address_fieldname: |
| 102 | update_cart_address(address_fieldname, bean.doc.name) |
| 103 | |
| 104 | return bean.doc.name |
| 105 | |
| 106 | def get_address_docs(party=None): |
| 107 | from webnotes.model.doclist import objectify |
| 108 | from utilities.transaction_base import get_address_display |
| 109 | |
| 110 | if not party: |
| 111 | party = get_lead_or_customer() |
| 112 | |
| 113 | address_docs = objectify(webnotes.conn.sql("""select * from `tabAddress` |
| 114 | where `%s`=%s order by name""" % (party.doctype.lower(), "%s"), party.name, |
| 115 | as_dict=True, update={"doctype": "Address"})) |
| 116 | |
| 117 | for address in address_docs: |
| 118 | address.display = get_address_display(address.fields) |
| 119 | address.display = (address.display).replace("\n", "<br>\n") |
| 120 | |
| 121 | return address_docs |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 122 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 123 | def get_lead_or_customer(): |
| 124 | customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, "customer") |
| 125 | if customer: |
| 126 | return webnotes.doc("Customer", customer) |
| 127 | |
| 128 | lead = webnotes.conn.get_value("Lead", {"email_id": webnotes.session.user}) |
| 129 | if lead: |
| 130 | return webnotes.doc("Lead", lead) |
| 131 | else: |
| 132 | lead_bean = webnotes.bean({ |
| 133 | "doctype": "Lead", |
| 134 | "email_id": webnotes.session.user, |
| 135 | "lead_name": get_fullname(webnotes.session.user), |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 136 | "territory": guess_territory(), |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 137 | "status": "Open" # TODO: set something better??? |
| 138 | }) |
| 139 | lead_bean.ignore_permissions = True |
| 140 | lead_bean.insert() |
| 141 | |
| 142 | return lead_bean.doc |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 143 | |
| 144 | def guess_territory(): |
| 145 | territory = None |
| 146 | geoip_country = webnotes.session.get("session_country") |
| 147 | if geoip_country: |
| 148 | territory = webnotes.conn.get_value("Territory", geoip_country) |
| 149 | |
| 150 | return territory or \ |
| 151 | webnotes.conn.get_value("Shopping Cart Settings", None, "territory") or \ |
| 152 | "All Territories" |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 153 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 154 | def decorate_quotation_doclist(doclist): |
| 155 | for d in doclist: |
| 156 | if d.item_code: |
| 157 | d.fields.update(webnotes.conn.get_value("Item", d.item_code, |
| 158 | ["website_image", "web_short_description", "page_name"], as_dict=True)) |
| 159 | d.formatted_rate = fmt_money(d.export_rate, currency=doclist[0].currency) |
| 160 | d.formatted_amount = fmt_money(d.export_amount, currency=doclist[0].currency) |
| 161 | |
| 162 | return [d.fields for d in doclist] |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 163 | |
| 164 | def _get_cart_quotation(party=None): |
| 165 | if not party: |
| 166 | party = get_lead_or_customer() |
| 167 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 168 | quotation = webnotes.conn.get_value("Quotation", |
| 169 | {party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0}) |
| 170 | |
| 171 | if quotation: |
| 172 | qbean = webnotes.bean("Quotation", quotation) |
| 173 | else: |
| 174 | qbean = webnotes.bean({ |
| 175 | "doctype": "Quotation", |
| 176 | "naming_series": "QTN-CART-", |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 177 | "quotation_to": party.doctype, |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 178 | "company": webnotes.defaults.get_user_default("company"), |
| 179 | "order_type": "Shopping Cart", |
| 180 | "status": "Draft", |
| 181 | "__islocal": 1, |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 182 | (party.doctype.lower()): party.name |
| 183 | }) |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 184 | qbean.run_method("onload_post_render") |
| 185 | apply_cart_settings(party, qbean) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 186 | |
| 187 | return qbean |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 188 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 189 | def apply_cart_settings(party=None, quotation=None): |
| 190 | if not party: |
| 191 | party = get_lead_or_customer() |
| 192 | if not quotation: |
| 193 | quotation = _get_cart_quotation(party) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 194 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 195 | cart_settings = webnotes.get_obj("Shopping Cart Settings") |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 196 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 197 | billing_territory = get_address_territory(quotation.doc.customer_address) or \ |
| 198 | party.territory |
| 199 | |
| 200 | set_price_list_and_rate(quotation, cart_settings, billing_territory) |
| 201 | |
| 202 | set_taxes(quotation, cart_settings, billing_territory) |
| 203 | |
| 204 | # set shipping rule based on shipping territory |
| 205 | shipping_territory = get_address_territory(quotation.doc.shipping_address_name) or \ |
| 206 | party.territory |
| 207 | |
| 208 | apply_shipping_rule(quotation, cart_settings, shipping_territory) |
| 209 | |
| 210 | quotation.run_method("calculate_taxes_and_totals") |
| 211 | |
| 212 | quotation.save() |
| 213 | |
| 214 | def set_price_list_and_rate(quotation, cart_settings, billing_territory): |
| 215 | """set price list based on billing territory""" |
| 216 | quotation.doc.price_list_name = cart_settings.get_price_list(billing_territory) |
| 217 | |
| 218 | # reset values |
| 219 | quotation.doc.price_list_currency = quotation.doc.currency = \ |
| 220 | quotation.doc.plc_conversion_rate = quotation.doc.conversion_rate = None |
| 221 | for item in quotation.doclist.get({"parentfield": "quotation_details"}): |
| 222 | item.ref_rate = item.adj_rate = item.export_rate = item.export_amount = None |
| 223 | |
| 224 | # refetch values |
| 225 | quotation.run_method("set_price_list_and_item_details") |
| 226 | |
| 227 | def set_taxes(quotation, cart_settings, billing_territory): |
| 228 | """set taxes based on billing territory""" |
| 229 | quotation.doc.charge = cart_settings.get_tax_master(billing_territory) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 230 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 231 | # clear table |
| 232 | quotation.doclist = quotation.doc.clear_table(quotation.doclist, "other_charges") |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 233 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 234 | # append taxes |
| 235 | controller = quotation.make_controller() |
| 236 | controller.append_taxes_from_master("other_charges", "charge") |
| 237 | quotation.set_doclist(controller.doclist) |
| 238 | |
| 239 | def apply_shipping_rule(quotation, cart_settings, shipping_territory): |
| 240 | quotation.doc.shipping_rule = cart_settings.get_shipping_rule(shipping_territory) |
| 241 | quotation.run_method("apply_shipping_rule") |
| 242 | |
| 243 | def get_address_territory(address_name): |
| 244 | """Tries to match city, state and country of address to existing territory""" |
| 245 | territory = None |
| 246 | |
| 247 | if address_name: |
| 248 | address_fields = webnotes.conn.get_value("Address", address_name, |
| 249 | ["city", "state", "country"]) |
| 250 | for value in address_fields: |
| 251 | territory = webnotes.conn.get_value("Territory", value) |
| 252 | if territory: |
| 253 | break |
| 254 | |
| 255 | return territory |
| 256 | |
| 257 | def get_cart_price_list(territory_list, territory_name_map): |
| 258 | pass |
| 259 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 260 | @webnotes.whitelist() |
| 261 | def checkout(): |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 262 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 263 | |
| 264 | quotation.ignore_permissions = True |
| 265 | quotation.submit() |
| 266 | |
| 267 | sales_order = webnotes.bean(webnotes.map_doclist([["Quotation", "Sales Order"], ["Quotation Item", "Sales Order Item"], |
| 268 | ["Sales Taxes and Charges", "Sales Taxes and Charges"]], quotation.doc.name)) |
| 269 | |
| 270 | sales_order.ignore_permissions = True |
| 271 | sales_order.insert() |
| 272 | sales_order.submit() |
| 273 | |
| 274 | return sales_order |
| 275 | |
| 276 | import unittest |
| 277 | test_dependencies = ["Item", "Price List", "Contact"] |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 278 | |
| 279 | class TestCart(unittest.TestCase): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 280 | def test_get_lead_or_customer(self): |
| 281 | webnotes.session.user = "test@example.com" |
| 282 | party1 = get_lead_or_customer() |
| 283 | party2 = get_lead_or_customer() |
| 284 | self.assertEquals(party1.name, party2.name) |
| 285 | self.assertEquals(party1.doctype, "Lead") |
| 286 | |
| 287 | webnotes.session.user = "test_contact_customer@example.com" |
| 288 | party = get_lead_or_customer() |
| 289 | self.assertEquals(party.name, "_Test Customer") |
| 290 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 291 | def test_add_to_cart(self): |
| 292 | webnotes.session.user = "test@example.com" |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 293 | update_cart("_Test Item", 1) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 294 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 295 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 296 | quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) |
| 297 | self.assertTrue(quotation_items) |
| 298 | self.assertEquals(quotation_items[0].qty, 1) |
| 299 | |
| 300 | return quotation |
| 301 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 302 | def test_update_cart(self): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 303 | self.test_add_to_cart() |
| 304 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 305 | update_cart("_Test Item", 5) |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 306 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 307 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 308 | quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) |
| 309 | self.assertTrue(quotation_items) |
| 310 | self.assertEquals(quotation_items[0].qty, 5) |
| 311 | |
| 312 | return quotation |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 313 | |
| 314 | def test_remove_from_cart(self): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 315 | quotation0 = self.test_add_to_cart() |
| 316 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 317 | update_cart("_Test Item", 0) |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 318 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 319 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 320 | self.assertEquals(quotation0.doc.name, quotation.doc.name) |
| 321 | |
| 322 | quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) |
| 323 | self.assertEquals(quotation_items, []) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 324 | |
| 325 | def test_checkout(self): |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 326 | quotation = self.test_update_cart() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 327 | sales_order = checkout() |
| 328 | 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] | 329 | |