Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | a76a068 | 2013-02-08 14:04:13 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 6 | from webnotes import msgprint, _ |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 7 | from webnotes.utils import flt, cint, comma_and |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 8 | import json |
Nabin Hait | a76a068 | 2013-02-08 14:04:13 +0530 | [diff] [blame] | 9 | |
| 10 | def get_customer_list(doctype, txt, searchfield, start, page_len, filters): |
| 11 | if webnotes.conn.get_default("cust_master_name") == "Customer Name": |
| 12 | fields = ["name", "customer_group", "territory"] |
| 13 | else: |
| 14 | fields = ["name", "customer_name", "customer_group", "territory"] |
| 15 | |
| 16 | return webnotes.conn.sql("""select %s from `tabCustomer` where docstatus < 2 |
| 17 | and (%s like %s or customer_name like %s) order by |
| 18 | case when name like %s then 0 else 1 end, |
| 19 | case when customer_name like %s then 0 else 1 end, |
| 20 | name, customer_name limit %s, %s""" % |
| 21 | (", ".join(fields), searchfield, "%s", "%s", "%s", "%s", "%s", "%s"), |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 22 | ("%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, start, page_len)) |
| 23 | |
| 24 | @webnotes.whitelist() |
| 25 | def get_item_details(args): |
| 26 | """ |
| 27 | args = { |
| 28 | "item_code": "", |
| 29 | "warehouse": None, |
| 30 | "customer": "", |
| 31 | "conversion_rate": 1.0, |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 32 | "selling_price_list": None, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 33 | "price_list_currency": None, |
| 34 | "plc_conversion_rate": 1.0 |
| 35 | } |
| 36 | """ |
| 37 | if isinstance(args, basestring): |
| 38 | args = json.loads(args) |
| 39 | args = webnotes._dict(args) |
| 40 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 41 | if args.barcode: |
| 42 | args.item_code = _get_item_code(args.barcode) |
| 43 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 44 | item_bean = webnotes.bean("Item", args.item_code) |
| 45 | |
| 46 | _validate_item_details(args, item_bean.doc) |
| 47 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 48 | meta = webnotes.get_doctype(args.doctype) |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 49 | |
| 50 | # hack! for Sales Order Item |
| 51 | warehouse_fieldname = "warehouse" |
| 52 | if meta.get_field("reserved_warehouse", parentfield=args.parentfield): |
| 53 | warehouse_fieldname = "reserved_warehouse" |
| 54 | |
| 55 | out = _get_basic_details(args, item_bean, warehouse_fieldname) |
| 56 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 57 | if meta.get_field("currency"): |
| 58 | out.base_ref_rate = out.basic_rate = out.ref_rate = out.export_rate = 0.0 |
| 59 | |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 60 | if args.selling_price_list and args.price_list_currency: |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 61 | out.update(_get_price_list_rate(args, item_bean, meta)) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 62 | |
Nabin Hait | 50f5fd1 | 2013-07-22 13:10:13 +0530 | [diff] [blame] | 63 | out.update(_get_item_discount(out.item_group, args.customer)) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 64 | |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 65 | if out.get(warehouse_fieldname): |
| 66 | out.update(get_available_qty(args.item_code, out.get(warehouse_fieldname))) |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 67 | |
| 68 | out.customer_item_code = _get_customer_item_code(args, item_bean) |
| 69 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 70 | if cint(args.is_pos): |
| 71 | pos_settings = get_pos_settings(args.company) |
Rushabh Mehta | 3423a73 | 2013-08-16 13:03:34 +0530 | [diff] [blame] | 72 | if pos_settings: |
| 73 | out.update(apply_pos_settings(pos_settings, out)) |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 74 | |
| 75 | if args.doctype in ("Sales Invoice", "Delivery Note"): |
| 76 | if item_bean.doc.has_serial_no and not args.serial_no: |
| 77 | out.serial_no = _get_serial_nos_by_fifo(args, item_bean) |
| 78 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 79 | return out |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 80 | |
| 81 | def _get_serial_nos_by_fifo(args, item_bean): |
| 82 | return "\n".join(webnotes.conn.sql_list("""select name from `tabSerial No` |
| 83 | where item_code=%(item_code)s and warehouse=%(warehouse)s and status='Available' |
Rushabh Mehta | 3423a73 | 2013-08-16 13:03:34 +0530 | [diff] [blame] | 84 | order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", { |
Rushabh Mehta | 62030e0 | 2013-08-14 18:37:28 +0530 | [diff] [blame] | 85 | "item_code": args.item_code, |
| 86 | "warehouse": args.warehouse, |
| 87 | "qty": cint(args.qty) |
| 88 | })) |
| 89 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 90 | def _get_item_code(barcode): |
| 91 | item_code = webnotes.conn.sql_list("""select name from `tabItem` where barcode=%s""", barcode) |
| 92 | |
| 93 | if not item_code: |
| 94 | msgprint(_("No Item found with Barcode") + ": %s" % barcode, raise_exception=True) |
| 95 | |
| 96 | elif len(item_code) > 1: |
| 97 | msgprint(_("Items") + " %s " % comma_and(item_code) + |
| 98 | _("have the same Barcode") + " %s" % barcode, raise_exception=True) |
| 99 | |
| 100 | return item_code[0] |
| 101 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 102 | def _validate_item_details(args, item): |
| 103 | from utilities.transaction_base import validate_item_fetch |
| 104 | validate_item_fetch(args, item) |
| 105 | |
| 106 | # validate if sales item or service item |
| 107 | if args.order_type == "Maintenance": |
| 108 | if item.is_service_item != "Yes": |
| 109 | msgprint(_("Item") + (" %s: " % item.name) + |
| 110 | _("not a service item.") + |
| 111 | _("Please select a service item or change the order type to Sales."), |
| 112 | raise_exception=True) |
| 113 | |
| 114 | elif item.is_sales_item != "Yes": |
| 115 | msgprint(_("Item") + (" %s: " % item.name) + _("not a sales item"), |
| 116 | raise_exception=True) |
| 117 | |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 118 | def _get_basic_details(args, item_bean, warehouse_fieldname): |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 119 | item = item_bean.doc |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 120 | |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 121 | out = webnotes._dict({ |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 122 | "item_code": item.name, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 123 | "description": item.description_html or item.description, |
Anand Doshi | 1dc95ed | 2013-07-23 13:36:38 +0530 | [diff] [blame] | 124 | warehouse_fieldname: item.default_warehouse or args.get(warehouse_fieldname), |
Rushabh Mehta | d9e7070 | 2013-07-08 18:01:46 +0530 | [diff] [blame] | 125 | "income_account": item.default_income_account or args.income_account \ |
| 126 | or webnotes.conn.get_value("Company", args.company, "default_income_account"), |
| 127 | "expense_account": item.purchase_account or args.expense_account \ |
| 128 | or webnotes.conn.get_value("Company", args.company, "default_expense_account"), |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 129 | "cost_center": item.default_sales_cost_center or args.cost_center, |
| 130 | "qty": 1.0, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 131 | "export_amount": 0.0, |
| 132 | "amount": 0.0, |
| 133 | "batch_no": None, |
| 134 | "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in |
| 135 | item_bean.doclist.get({"parentfield": "item_tax"})))), |
| 136 | }) |
| 137 | |
| 138 | for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"): |
| 139 | out[fieldname] = item.fields.get(fieldname) |
| 140 | |
| 141 | return out |
| 142 | |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 143 | def _get_price_list_rate(args, item_bean, meta): |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 144 | base_ref_rate = item_bean.doclist.get({ |
| 145 | "parentfield": "ref_rate_details", |
Rushabh Mehta | 4a404e9 | 2013-08-09 18:11:35 +0530 | [diff] [blame] | 146 | "price_list": args.selling_price_list, |
Anand Doshi | c2a3527 | 2013-06-19 17:19:20 +0530 | [diff] [blame] | 147 | "ref_currency": args.price_list_currency, |
Anand Doshi | 060d924 | 2013-06-12 17:40:36 +0530 | [diff] [blame] | 148 | "buying_or_selling": "Selling"}) |
Rushabh Mehta | 7c2a2e2 | 2013-08-07 15:08:11 +0530 | [diff] [blame] | 149 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 150 | if not base_ref_rate: |
| 151 | return {} |
| 152 | |
| 153 | # found price list rate - now we can validate |
| 154 | from utilities.transaction_base import validate_currency |
| 155 | validate_currency(args, item_bean.doc, meta) |
| 156 | |
Nabin Hait | 2d57e51 | 2013-07-31 17:31:23 +0530 | [diff] [blame] | 157 | return {"ref_rate": flt(base_ref_rate[0].ref_rate) * flt(args.plc_conversion_rate) / flt(args.conversion_rate)} |
Nabin Hait | 50f5fd1 | 2013-07-22 13:10:13 +0530 | [diff] [blame] | 158 | |
| 159 | def _get_item_discount(item_group, customer): |
| 160 | parent_item_groups = [x[0] for x in webnotes.conn.sql("""SELECT parent.name |
| 161 | FROM `tabItem Group` AS node, `tabItem Group` AS parent |
| 162 | WHERE parent.lft <= node.lft and parent.rgt >= node.rgt and node.name = %s |
| 163 | GROUP BY parent.name |
| 164 | ORDER BY parent.lft desc""", item_group)] |
| 165 | |
| 166 | discount = 0 |
| 167 | for d in parent_item_groups: |
| 168 | res = webnotes.conn.sql("""select discount, name from `tabCustomer Discount` |
| 169 | where parent = %s and item_group = %s""", (customer, d)) |
| 170 | if res: |
| 171 | discount = flt(res[0][0]) |
| 172 | break |
| 173 | |
| 174 | return {"adj_rate": discount} |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 175 | |
| 176 | @webnotes.whitelist() |
| 177 | def get_available_qty(item_code, warehouse): |
| 178 | return webnotes.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse}, |
Anand Doshi | 1dde46a | 2013-05-15 21:15:57 +0530 | [diff] [blame] | 179 | ["projected_qty", "actual_qty"], as_dict=True) or {} |
| 180 | |
| 181 | def _get_customer_item_code(args, item_bean): |
| 182 | customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details", |
| 183 | "customer_name": args.customer}) |
| 184 | |
| 185 | return customer_item_code and customer_item_code[0].ref_code or None |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 186 | |
| 187 | def get_pos_settings(company): |
| 188 | pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` where user = %s |
| 189 | and company = %s""", (webnotes.session['user'], company), as_dict=1) |
Nabin Hait | 50f5fd1 | 2013-07-22 13:10:13 +0530 | [diff] [blame] | 190 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 191 | if not pos_settings: |
| 192 | pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` |
| 193 | where ifnull(user,'') = '' and company = %s""", company, as_dict=1) |
Anand Doshi | f90df7d | 2013-07-29 19:53:44 +0530 | [diff] [blame] | 194 | |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 195 | return pos_settings and pos_settings[0] or None |
| 196 | |
| 197 | def apply_pos_settings(pos_settings, opts): |
| 198 | out = {} |
| 199 | |
| 200 | for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"): |
| 201 | if not opts.get(fieldname): |
| 202 | out[fieldname] = pos_settings.get(fieldname) |
| 203 | |
| 204 | if out.get("warehouse"): |
Anand Doshi | fc77718 | 2013-05-27 19:29:07 +0530 | [diff] [blame] | 205 | out["actual_qty"] = get_available_qty(opts.item_code, out.get("warehouse")).get("actual_qty") |
Anand Doshi | f309613 | 2013-05-21 19:35:06 +0530 | [diff] [blame] | 206 | |
| 207 | return out |