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 | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 6 | from webnotes import msgprint, _ |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 7 | import webnotes.defaults |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 8 | from webnotes.utils import flt, get_fullname, fmt_money, cstr |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 9 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 10 | class WebsitePriceListMissingError(webnotes.ValidationError): pass |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 11 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 12 | def set_cart_count(quotation=None): |
| 13 | if not quotation: |
| 14 | quotation = _get_cart_quotation() |
| 15 | webnotes.add_cookies["cart_count"] = cstr(len(quotation.doclist.get( |
| 16 | {"parentfield": "quotation_details"})) or "") |
| 17 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 18 | @webnotes.whitelist() |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 19 | def get_cart_quotation(doclist=None): |
| 20 | party = get_lead_or_customer() |
| 21 | |
| 22 | if not doclist: |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 23 | quotation = _get_cart_quotation(party) |
| 24 | doclist = quotation.doclist |
| 25 | set_cart_count(quotation) |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 26 | |
| 27 | return { |
| 28 | "doclist": decorate_quotation_doclist(doclist), |
| 29 | "addresses": [{"name": address.name, "display": address.display} |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 30 | for address in get_address_docs(party)], |
| 31 | "shipping_rules": get_applicable_shipping_rules(party) |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 32 | } |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 33 | |
| 34 | @webnotes.whitelist() |
| 35 | def place_order(): |
| 36 | quotation = _get_cart_quotation() |
| 37 | controller = quotation.make_controller() |
| 38 | for fieldname in ["customer_address", "shipping_address_name"]: |
| 39 | if not quotation.doc.fields.get(fieldname): |
| 40 | msgprint(_("Please select a") + " " + _(controller.meta.get_label(fieldname)), raise_exception=True) |
| 41 | |
| 42 | quotation.ignore_permissions = True |
| 43 | quotation.submit() |
| 44 | |
| 45 | from selling.doctype.quotation.quotation import _make_sales_order |
| 46 | sales_order = webnotes.bean(_make_sales_order(quotation.doc.name, ignore_permissions=True)) |
| 47 | sales_order.ignore_permissions = True |
| 48 | sales_order.insert() |
| 49 | sales_order.submit() |
| 50 | webnotes.add_cookies["cart_count"] = "" |
| 51 | |
| 52 | return sales_order.doc.name |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 53 | |
| 54 | @webnotes.whitelist() |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 55 | def update_cart(item_code, qty, with_doclist=0): |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 56 | quotation = _get_cart_quotation() |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 57 | |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 58 | qty = flt(qty) |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 59 | if qty == 0: |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 60 | quotation.set_doclist(quotation.doclist.get({"item_code": ["!=", item_code]})) |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 61 | if not quotation.doclist.get({"parentfield": "quotation_details"}) and \ |
| 62 | not quotation.doc.fields.get("__islocal"): |
| 63 | quotation.__delete = True |
| 64 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 65 | else: |
| 66 | quotation_items = quotation.doclist.get({"item_code": item_code}) |
| 67 | if not quotation_items: |
| 68 | quotation.doclist.append({ |
| 69 | "doctype": "Quotation Item", |
| 70 | "parentfield": "quotation_details", |
| 71 | "item_code": item_code, |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 72 | "qty": qty |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 73 | }) |
| 74 | else: |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 75 | quotation_items[0].qty = qty |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 76 | |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 77 | apply_cart_settings(quotation=quotation) |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 78 | |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 79 | if hasattr(quotation, "__delete"): |
| 80 | webnotes.delete_doc("Quotation", quotation.doc.name, ignore_permissions=True) |
| 81 | quotation = _get_cart_quotation() |
| 82 | else: |
| 83 | quotation.ignore_permissions = True |
| 84 | quotation.save() |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 85 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 86 | set_cart_count(quotation) |
| 87 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 88 | if with_doclist: |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 89 | return get_cart_quotation(quotation.doclist) |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 90 | else: |
| 91 | return quotation.doc.name |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 92 | |
| 93 | @webnotes.whitelist() |
| 94 | def update_cart_address(address_fieldname, address_name): |
| 95 | from utilities.transaction_base import get_address_display |
| 96 | |
| 97 | quotation = _get_cart_quotation() |
| 98 | address_display = get_address_display(webnotes.doc("Address", address_name).fields) |
| 99 | |
| 100 | if address_fieldname == "shipping_address_name": |
| 101 | quotation.doc.shipping_address_name = address_name |
| 102 | quotation.doc.shipping_address = address_display |
| 103 | |
| 104 | if not quotation.doc.customer_address: |
| 105 | address_fieldname == "customer_address" |
| 106 | |
| 107 | if address_fieldname == "customer_address": |
| 108 | quotation.doc.customer_address = address_name |
| 109 | quotation.doc.address_display = address_display |
| 110 | |
| 111 | |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 112 | apply_cart_settings(quotation=quotation) |
| 113 | |
Anand Doshi | edbf3e1 | 2013-07-02 11:40:16 +0530 | [diff] [blame] | 114 | quotation.ignore_permissions = True |
| 115 | quotation.save() |
| 116 | |
| 117 | return get_cart_quotation(quotation.doclist) |
| 118 | |
| 119 | @webnotes.whitelist() |
| 120 | def get_addresses(): |
| 121 | return [d.fields for d in get_address_docs()] |
| 122 | |
| 123 | @webnotes.whitelist() |
| 124 | def save_address(fields, address_fieldname=None): |
| 125 | party = get_lead_or_customer() |
| 126 | fields = webnotes.load_json(fields) |
| 127 | |
| 128 | if fields.get("name"): |
| 129 | bean = webnotes.bean("Address", fields.get("name")) |
| 130 | else: |
| 131 | bean = webnotes.bean({"doctype": "Address", "__islocal": 1}) |
| 132 | |
| 133 | bean.doc.fields.update(fields) |
| 134 | |
| 135 | party_fieldname = party.doctype.lower() |
| 136 | bean.doc.fields.update({ |
| 137 | party_fieldname: party.name, |
| 138 | (party_fieldname + "_name"): party.fields[party_fieldname + "_name"] |
| 139 | }) |
| 140 | bean.ignore_permissions = True |
| 141 | bean.save() |
| 142 | |
| 143 | if address_fieldname: |
| 144 | update_cart_address(address_fieldname, bean.doc.name) |
| 145 | |
| 146 | return bean.doc.name |
| 147 | |
| 148 | def get_address_docs(party=None): |
| 149 | from webnotes.model.doclist import objectify |
| 150 | from utilities.transaction_base import get_address_display |
| 151 | |
| 152 | if not party: |
| 153 | party = get_lead_or_customer() |
| 154 | |
| 155 | address_docs = objectify(webnotes.conn.sql("""select * from `tabAddress` |
| 156 | where `%s`=%s order by name""" % (party.doctype.lower(), "%s"), party.name, |
| 157 | as_dict=True, update={"doctype": "Address"})) |
| 158 | |
| 159 | for address in address_docs: |
| 160 | address.display = get_address_display(address.fields) |
| 161 | address.display = (address.display).replace("\n", "<br>\n") |
| 162 | |
| 163 | return address_docs |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 164 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 165 | def get_lead_or_customer(): |
| 166 | customer = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user}, "customer") |
| 167 | if customer: |
| 168 | return webnotes.doc("Customer", customer) |
| 169 | |
| 170 | lead = webnotes.conn.get_value("Lead", {"email_id": webnotes.session.user}) |
| 171 | if lead: |
| 172 | return webnotes.doc("Lead", lead) |
| 173 | else: |
| 174 | lead_bean = webnotes.bean({ |
| 175 | "doctype": "Lead", |
| 176 | "email_id": webnotes.session.user, |
| 177 | "lead_name": get_fullname(webnotes.session.user), |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 178 | "territory": guess_territory(), |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 179 | "status": "Open" # TODO: set something better??? |
| 180 | }) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 181 | |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 182 | if webnotes.session.user != "Guest": |
| 183 | lead_bean.ignore_permissions = True |
| 184 | lead_bean.insert() |
| 185 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 186 | return lead_bean.doc |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 187 | |
| 188 | def guess_territory(): |
| 189 | territory = None |
| 190 | geoip_country = webnotes.session.get("session_country") |
| 191 | if geoip_country: |
| 192 | territory = webnotes.conn.get_value("Territory", geoip_country) |
| 193 | |
| 194 | return territory or \ |
| 195 | webnotes.conn.get_value("Shopping Cart Settings", None, "territory") or \ |
| 196 | "All Territories" |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 197 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 198 | def decorate_quotation_doclist(doclist): |
| 199 | for d in doclist: |
| 200 | if d.item_code: |
| 201 | d.fields.update(webnotes.conn.get_value("Item", d.item_code, |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 202 | ["website_image", "description", "page_name"], as_dict=True)) |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 203 | d.formatted_rate = fmt_money(d.export_rate, currency=doclist[0].currency) |
| 204 | d.formatted_amount = fmt_money(d.export_amount, currency=doclist[0].currency) |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 205 | elif d.charge_type: |
| 206 | d.formatted_tax_amount = fmt_money(d.tax_amount / doclist[0].conversion_rate, |
| 207 | currency=doclist[0].currency) |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 208 | |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 209 | doclist[0].formatted_grand_total_export = fmt_money(doclist[0].grand_total_export, |
| 210 | currency=doclist[0].currency) |
| 211 | |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 212 | return [d.fields for d in doclist] |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 213 | |
| 214 | def _get_cart_quotation(party=None): |
| 215 | if not party: |
| 216 | party = get_lead_or_customer() |
| 217 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 218 | quotation = webnotes.conn.get_value("Quotation", |
| 219 | {party.doctype.lower(): party.name, "order_type": "Shopping Cart", "docstatus": 0}) |
| 220 | |
| 221 | if quotation: |
| 222 | qbean = webnotes.bean("Quotation", quotation) |
| 223 | else: |
| 224 | qbean = webnotes.bean({ |
| 225 | "doctype": "Quotation", |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 226 | "naming_series": webnotes.defaults.get_user_default("shopping_cart_quotation_series") or "QTN-CART-", |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 227 | "quotation_to": party.doctype, |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 228 | "company": webnotes.defaults.get_user_default("company"), |
| 229 | "order_type": "Shopping Cart", |
| 230 | "status": "Draft", |
| 231 | "__islocal": 1, |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 232 | (party.doctype.lower()): party.name |
| 233 | }) |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 234 | |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 235 | if party.doctype == "Customer": |
| 236 | qbean.doc.contact_person = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user, |
| 237 | "customer": party.name}) |
| 238 | qbean.run_method("set_contact_fields") |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 239 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 240 | qbean.run_method("onload_post_render") |
| 241 | apply_cart_settings(party, qbean) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 242 | |
| 243 | return qbean |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 244 | |
| 245 | def update_party(fullname, company_name=None, mobile_no=None, phone=None): |
| 246 | party = get_lead_or_customer() |
| 247 | |
| 248 | if party.doctype == "Lead": |
| 249 | party.company_name = company_name |
| 250 | party.lead_name = fullname |
| 251 | party.mobile_no = mobile_no |
| 252 | party.phone = phone |
| 253 | else: |
| 254 | party.customer_name = company_name or fullname |
| 255 | party.customer_type == "Company" if company_name else "Individual" |
| 256 | |
| 257 | contact_name = webnotes.conn.get_value("Contact", {"email_id": webnotes.session.user, |
| 258 | "customer": party.name}) |
| 259 | contact = webnotes.bean("Contact", contact_name) |
| 260 | contact.doc.first_name = fullname |
| 261 | contact.doc.last_name = None |
| 262 | contact.doc.customer_name = party.customer_name |
| 263 | contact.doc.mobile_no = mobile_no |
| 264 | contact.doc.phone = phone |
| 265 | contact.ignore_permissions = True |
| 266 | contact.save() |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 267 | |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 268 | party_bean = webnotes.bean(party.fields) |
| 269 | party_bean.ignore_permissions = True |
| 270 | party_bean.save() |
| 271 | |
| 272 | qbean = _get_cart_quotation(party) |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 273 | if not qbean.doc.fields.get("__islocal"): |
| 274 | qbean.doc.customer_name = company_name or fullname |
| 275 | qbean.run_method("set_contact_fields") |
| 276 | qbean.ignore_permissions = True |
| 277 | qbean.save() |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 278 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 279 | def apply_cart_settings(party=None, quotation=None): |
| 280 | if not party: |
| 281 | party = get_lead_or_customer() |
| 282 | if not quotation: |
| 283 | quotation = _get_cart_quotation(party) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 284 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 285 | cart_settings = webnotes.get_obj("Shopping Cart Settings") |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 286 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 287 | billing_territory = get_address_territory(quotation.doc.customer_address) or \ |
| 288 | party.territory |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 289 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 290 | set_price_list_and_rate(quotation, cart_settings, billing_territory) |
| 291 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 292 | quotation.run_method("calculate_taxes_and_totals") |
| 293 | |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 294 | set_taxes(quotation, cart_settings, billing_territory) |
| 295 | |
| 296 | _apply_shipping_rule(party, quotation, cart_settings) |
| 297 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 298 | def set_price_list_and_rate(quotation, cart_settings, billing_territory): |
| 299 | """set price list based on billing territory""" |
| 300 | quotation.doc.price_list_name = cart_settings.get_price_list(billing_territory) |
| 301 | |
| 302 | # reset values |
| 303 | quotation.doc.price_list_currency = quotation.doc.currency = \ |
| 304 | quotation.doc.plc_conversion_rate = quotation.doc.conversion_rate = None |
| 305 | for item in quotation.doclist.get({"parentfield": "quotation_details"}): |
| 306 | item.ref_rate = item.adj_rate = item.export_rate = item.export_amount = None |
| 307 | |
| 308 | # refetch values |
| 309 | quotation.run_method("set_price_list_and_item_details") |
| 310 | |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 311 | # set it in cookies for using in product page |
| 312 | webnotes.cookies[b"price_list_name"] = quotation.doc.price_list_name |
| 313 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 314 | def set_taxes(quotation, cart_settings, billing_territory): |
| 315 | """set taxes based on billing territory""" |
| 316 | quotation.doc.charge = cart_settings.get_tax_master(billing_territory) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 317 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 318 | # clear table |
Anand Doshi | 2ac0a83 | 2013-07-10 20:49:44 +0530 | [diff] [blame] | 319 | quotation.set_doclist(quotation.doclist.get({"parentfield": ["!=", "other_charges"]})) |
| 320 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 321 | # append taxes |
| 322 | controller = quotation.make_controller() |
| 323 | controller.append_taxes_from_master("other_charges", "charge") |
| 324 | quotation.set_doclist(controller.doclist) |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 325 | |
| 326 | @webnotes.whitelist() |
| 327 | def apply_shipping_rule(shipping_rule): |
| 328 | quotation = _get_cart_quotation() |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 329 | |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 330 | quotation.doc.shipping_rule = shipping_rule |
| 331 | |
| 332 | apply_cart_settings(quotation=quotation) |
| 333 | |
Anand Doshi | 2862c9e | 2013-07-08 18:50:33 +0530 | [diff] [blame] | 334 | quotation.ignore_permissions = True |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 335 | quotation.save() |
| 336 | |
| 337 | return get_cart_quotation(quotation.doclist) |
| 338 | |
| 339 | def _apply_shipping_rule(party=None, quotation=None, cart_settings=None): |
| 340 | shipping_rules = get_shipping_rules(party, quotation, cart_settings) |
| 341 | |
| 342 | if not shipping_rules: |
| 343 | return |
| 344 | |
| 345 | elif quotation.doc.shipping_rule not in shipping_rules: |
| 346 | quotation.doc.shipping_rule = shipping_rules[0] |
| 347 | |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 348 | quotation.run_method("apply_shipping_rule") |
Anand Doshi | 0b4943c | 2013-07-04 23:45:22 +0530 | [diff] [blame] | 349 | quotation.run_method("calculate_taxes_and_totals") |
| 350 | |
| 351 | def get_applicable_shipping_rules(party=None, quotation=None): |
| 352 | shipping_rules = get_shipping_rules(party, quotation) |
| 353 | |
| 354 | if shipping_rules: |
| 355 | rule_label_map = webnotes.conn.get_values("Shipping Rule", shipping_rules, "label") |
| 356 | # we need this in sorted order as per the position of the rule in the settings page |
| 357 | return [[rule, rule_label_map.get(rule)] for rule in shipping_rules] |
| 358 | |
| 359 | def get_shipping_rules(party=None, quotation=None, cart_settings=None): |
| 360 | if not party: |
| 361 | party = get_lead_or_customer() |
| 362 | if not quotation: |
| 363 | quotation = _get_cart_quotation() |
| 364 | if not cart_settings: |
| 365 | cart_settings = webnotes.get_obj("Shopping Cart Settings") |
| 366 | |
| 367 | # set shipping rule based on shipping territory |
| 368 | shipping_territory = get_address_territory(quotation.doc.shipping_address_name) or \ |
| 369 | party.territory |
| 370 | |
| 371 | shipping_rules = cart_settings.get_shipping_rules(shipping_territory) |
| 372 | |
| 373 | return shipping_rules |
Anand Doshi | 99100a4 | 2013-07-04 17:13:53 +0530 | [diff] [blame] | 374 | |
| 375 | def get_address_territory(address_name): |
| 376 | """Tries to match city, state and country of address to existing territory""" |
| 377 | territory = None |
| 378 | |
| 379 | if address_name: |
| 380 | address_fields = webnotes.conn.get_value("Address", address_name, |
| 381 | ["city", "state", "country"]) |
| 382 | for value in address_fields: |
| 383 | territory = webnotes.conn.get_value("Territory", value) |
| 384 | if territory: |
| 385 | break |
| 386 | |
| 387 | return territory |
| 388 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 389 | import unittest |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 390 | test_dependencies = ["Item", "Price List", "Contact", "Shopping Cart Settings"] |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 391 | |
| 392 | class TestCart(unittest.TestCase): |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 393 | def tearDown(self): |
| 394 | return |
| 395 | |
| 396 | cart_settings = webnotes.bean("Shopping Cart Settings") |
| 397 | cart_settings.ignore_permissions = True |
| 398 | cart_settings.doc.enabled = 0 |
| 399 | cart_settings.save() |
| 400 | |
| 401 | def enable_shopping_cart(self): |
| 402 | return |
| 403 | if not webnotes.conn.get_value("Shopping Cart Settings", None, "enabled"): |
| 404 | cart_settings = webnotes.bean("Shopping Cart Settings") |
| 405 | cart_settings.ignore_permissions = True |
| 406 | cart_settings.doc.enabled = 1 |
| 407 | cart_settings.save() |
| 408 | |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 409 | def test_get_lead_or_customer(self): |
| 410 | webnotes.session.user = "test@example.com" |
| 411 | party1 = get_lead_or_customer() |
| 412 | party2 = get_lead_or_customer() |
| 413 | self.assertEquals(party1.name, party2.name) |
| 414 | self.assertEquals(party1.doctype, "Lead") |
| 415 | |
| 416 | webnotes.session.user = "test_contact_customer@example.com" |
| 417 | party = get_lead_or_customer() |
| 418 | self.assertEquals(party.name, "_Test Customer") |
| 419 | |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 420 | def test_add_to_cart(self): |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 421 | self.enable_shopping_cart() |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 422 | webnotes.session.user = "test@example.com" |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 423 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 424 | update_cart("_Test Item", 1) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 425 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 426 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 427 | quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) |
| 428 | self.assertTrue(quotation_items) |
| 429 | self.assertEquals(quotation_items[0].qty, 1) |
| 430 | |
| 431 | return quotation |
| 432 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 433 | def test_update_cart(self): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 434 | self.test_add_to_cart() |
| 435 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 436 | update_cart("_Test Item", 5) |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 437 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 438 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 439 | quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) |
| 440 | self.assertTrue(quotation_items) |
| 441 | self.assertEquals(quotation_items[0].qty, 5) |
| 442 | |
| 443 | return quotation |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 444 | |
| 445 | def test_remove_from_cart(self): |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 446 | quotation0 = self.test_add_to_cart() |
| 447 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 448 | update_cart("_Test Item", 0) |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 449 | |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 450 | quotation = _get_cart_quotation() |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 451 | self.assertEquals(quotation0.doc.name, quotation.doc.name) |
| 452 | |
| 453 | quotation_items = quotation.doclist.get({"parentfield": "quotation_details", "item_code": "_Test Item"}) |
| 454 | self.assertEquals(quotation_items, []) |
Anand Doshi | abc1003 | 2013-06-14 17:44:03 +0530 | [diff] [blame] | 455 | |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 456 | def test_place_order(self): |
Anand Doshi | 3dceb84 | 2013-06-19 14:57:14 +0530 | [diff] [blame] | 457 | quotation = self.test_update_cart() |
Anand Doshi | cefccb9 | 2013-07-15 18:28:14 +0530 | [diff] [blame] | 458 | sales_order_name = place_order() |
| 459 | sales_order = webnotes.bean("Sales Order", sales_order_name) |
Anand Doshi | e813212 | 2013-06-17 15:42:38 +0530 | [diff] [blame] | 460 | 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] | 461 | |