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