blob: 53ebbeef34b016498e1b272eed6e9b868574f9a0 [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Nabin Haita76a0682013-02-08 14:04:13 +05303
4from __future__ import unicode_literals
5import webnotes
Anand Doshi1dde46a2013-05-15 21:15:57 +05306from webnotes import msgprint, _
Anand Doshif3096132013-05-21 19:35:06 +05307from webnotes.utils import flt, cint, comma_and
Anand Doshi1dde46a2013-05-15 21:15:57 +05308import json
Nabin Haita76a0682013-02-08 14:04:13 +05309
10def 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 Doshi1dde46a2013-05-15 21:15:57 +053022 ("%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, start, page_len))
23
24@webnotes.whitelist()
25def get_item_details(args):
26 """
27 args = {
28 "item_code": "",
29 "warehouse": None,
30 "customer": "",
31 "conversion_rate": 1.0,
Rushabh Mehta4a404e92013-08-09 18:11:35 +053032 "selling_price_list": None,
Anand Doshi1dde46a2013-05-15 21:15:57 +053033 "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 Doshif3096132013-05-21 19:35:06 +053041 if args.barcode:
42 args.item_code = _get_item_code(args.barcode)
43
Anand Doshi1dde46a2013-05-15 21:15:57 +053044 item_bean = webnotes.bean("Item", args.item_code)
45
46 _validate_item_details(args, item_bean.doc)
47
Anand Doshif3096132013-05-21 19:35:06 +053048 meta = webnotes.get_doctype(args.doctype)
Anand Doshi1dc95ed2013-07-23 13:36:38 +053049
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 Doshif3096132013-05-21 19:35:06 +053057 if meta.get_field("currency"):
58 out.base_ref_rate = out.basic_rate = out.ref_rate = out.export_rate = 0.0
59
Rushabh Mehta4a404e92013-08-09 18:11:35 +053060 if args.selling_price_list and args.price_list_currency:
Anand Doshif3096132013-05-21 19:35:06 +053061 out.update(_get_price_list_rate(args, item_bean, meta))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053062
Nabin Hait50f5fd12013-07-22 13:10:13 +053063 out.update(_get_item_discount(out.item_group, args.customer))
Anand Doshi1dde46a2013-05-15 21:15:57 +053064
Anand Doshi1dc95ed2013-07-23 13:36:38 +053065 if out.get(warehouse_fieldname):
66 out.update(get_available_qty(args.item_code, out.get(warehouse_fieldname)))
Anand Doshi1dde46a2013-05-15 21:15:57 +053067
68 out.customer_item_code = _get_customer_item_code(args, item_bean)
69
Anand Doshif3096132013-05-21 19:35:06 +053070 if cint(args.is_pos):
71 pos_settings = get_pos_settings(args.company)
Rushabh Mehta3423a732013-08-16 13:03:34 +053072 if pos_settings:
73 out.update(apply_pos_settings(pos_settings, out))
Rushabh Mehta62030e02013-08-14 18:37:28 +053074
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 Doshi1dde46a2013-05-15 21:15:57 +053079 return out
Rushabh Mehta62030e02013-08-14 18:37:28 +053080
81def _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 Mehta3423a732013-08-16 13:03:34 +053084 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
Rushabh Mehta62030e02013-08-14 18:37:28 +053085 "item_code": args.item_code,
86 "warehouse": args.warehouse,
87 "qty": cint(args.qty)
88 }))
89
Anand Doshif3096132013-05-21 19:35:06 +053090def _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 Doshi1dde46a2013-05-15 21:15:57 +0530102def _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 Doshi1dc95ed2013-07-23 13:36:38 +0530118def _get_basic_details(args, item_bean, warehouse_fieldname):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530119 item = item_bean.doc
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530120
Anand Doshi1dde46a2013-05-15 21:15:57 +0530121 out = webnotes._dict({
Anand Doshifc777182013-05-27 19:29:07 +0530122 "item_code": item.name,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530123 "description": item.description_html or item.description,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530124 warehouse_fieldname: item.default_warehouse or args.get(warehouse_fieldname),
Rushabh Mehtad9e70702013-07-08 18:01:46 +0530125 "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 Doshi1dde46a2013-05-15 21:15:57 +0530129 "cost_center": item.default_sales_cost_center or args.cost_center,
130 "qty": 1.0,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530131 "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 Doshifc777182013-05-27 19:29:07 +0530143def _get_price_list_rate(args, item_bean, meta):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530144 base_ref_rate = item_bean.doclist.get({
145 "parentfield": "ref_rate_details",
Rushabh Mehta4a404e92013-08-09 18:11:35 +0530146 "price_list": args.selling_price_list,
Anand Doshic2a35272013-06-19 17:19:20 +0530147 "ref_currency": args.price_list_currency,
Anand Doshi060d9242013-06-12 17:40:36 +0530148 "buying_or_selling": "Selling"})
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530149
Anand Doshif3096132013-05-21 19:35:06 +0530150 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 Hait2d57e512013-07-31 17:31:23 +0530157 return {"ref_rate": flt(base_ref_rate[0].ref_rate) * flt(args.plc_conversion_rate) / flt(args.conversion_rate)}
Nabin Hait50f5fd12013-07-22 13:10:13 +0530158
159def _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 Doshifc777182013-05-27 19:29:07 +0530175
176@webnotes.whitelist()
177def get_available_qty(item_code, warehouse):
178 return webnotes.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Anand Doshi1dde46a2013-05-15 21:15:57 +0530179 ["projected_qty", "actual_qty"], as_dict=True) or {}
180
181def _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 Doshif3096132013-05-21 19:35:06 +0530186
187def 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 Hait50f5fd12013-07-22 13:10:13 +0530190
Anand Doshif3096132013-05-21 19:35:06 +0530191 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 Doshif90df7d2013-07-29 19:53:44 +0530194
Anand Doshif3096132013-05-21 19:35:06 +0530195 return pos_settings and pos_settings[0] or None
196
197def 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 Doshifc777182013-05-27 19:29:07 +0530205 out["actual_qty"] = get_available_qty(opts.item_code, out.get("warehouse")).get("actual_qty")
Anand Doshif3096132013-05-21 19:35:06 +0530206
207 return out