blob: fd9c492b89cfd9cd7088c4fa0b8ef410613cc171 [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 """
Akhilesh Darjee10dce342013-09-25 11:09:33 +053037
Anand Doshi1dde46a2013-05-15 21:15:57 +053038 if isinstance(args, basestring):
39 args = json.loads(args)
40 args = webnotes._dict(args)
41
Anand Doshif3096132013-05-21 19:35:06 +053042 if args.barcode:
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +053043 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 Doshif3096132013-05-21 19:35:06 +053046
Anand Doshi1dde46a2013-05-15 21:15:57 +053047 item_bean = webnotes.bean("Item", args.item_code)
48
49 _validate_item_details(args, item_bean.doc)
50
Anand Doshif3096132013-05-21 19:35:06 +053051 meta = webnotes.get_doctype(args.doctype)
Anand Doshi1dc95ed2013-07-23 13:36:38 +053052
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 Doshif3096132013-05-21 19:35:06 +053060 if meta.get_field("currency"):
61 out.base_ref_rate = out.basic_rate = out.ref_rate = out.export_rate = 0.0
62
Rushabh Mehta4a404e92013-08-09 18:11:35 +053063 if args.selling_price_list and args.price_list_currency:
Anand Doshif3096132013-05-21 19:35:06 +053064 out.update(_get_price_list_rate(args, item_bean, meta))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053065
Nabin Hait50f5fd12013-07-22 13:10:13 +053066 out.update(_get_item_discount(out.item_group, args.customer))
Anand Doshi1dde46a2013-05-15 21:15:57 +053067
Anand Doshi1dc95ed2013-07-23 13:36:38 +053068 if out.get(warehouse_fieldname):
69 out.update(get_available_qty(args.item_code, out.get(warehouse_fieldname)))
Anand Doshi1dde46a2013-05-15 21:15:57 +053070
71 out.customer_item_code = _get_customer_item_code(args, item_bean)
72
Anand Doshif3096132013-05-21 19:35:06 +053073 if cint(args.is_pos):
74 pos_settings = get_pos_settings(args.company)
Rushabh Mehta3423a732013-08-16 13:03:34 +053075 if pos_settings:
76 out.update(apply_pos_settings(pos_settings, out))
Rushabh Mehta62030e02013-08-14 18:37:28 +053077
78 if args.doctype in ("Sales Invoice", "Delivery Note"):
Nabin Hait704a9152013-10-05 17:20:23 +053079 if item_bean.doc.has_serial_no == "Yes" and not args.serial_no:
Rushabh Mehta62030e02013-08-14 18:37:28 +053080 out.serial_no = _get_serial_nos_by_fifo(args, item_bean)
81
Anand Doshi1dde46a2013-05-15 21:15:57 +053082 return out
Rushabh Mehta62030e02013-08-14 18:37:28 +053083
84def _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 Mehta3423a732013-08-16 13:03:34 +053087 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
Rushabh Mehta62030e02013-08-14 18:37:28 +053088 "item_code": args.item_code,
89 "warehouse": args.warehouse,
90 "qty": cint(args.qty)
91 }))
92
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +053093def _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 Doshif3096132013-05-21 19:35:06 +0530101
102 if not item_code:
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530103 msgprint(_("No Item found with ") + input_type + ": %s" % (barcode or serial_no), raise_exception=True)
Anand Doshif3096132013-05-21 19:35:06 +0530104
105 return item_code[0]
106
Anand Doshi1dde46a2013-05-15 21:15:57 +0530107def _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 Doshi1dc95ed2013-07-23 13:36:38 +0530123def _get_basic_details(args, item_bean, warehouse_fieldname):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530124 item = item_bean.doc
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530125
Anand Doshi1dde46a2013-05-15 21:15:57 +0530126 out = webnotes._dict({
Anand Doshifc777182013-05-27 19:29:07 +0530127 "item_code": item.name,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530128 "description": item.description_html or item.description,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530129 warehouse_fieldname: item.default_warehouse or args.get(warehouse_fieldname),
Rushabh Mehtad9e70702013-07-08 18:01:46 +0530130 "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 Doshi1dde46a2013-05-15 21:15:57 +0530134 "cost_center": item.default_sales_cost_center or args.cost_center,
135 "qty": 1.0,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530136 "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 Doshifc777182013-05-27 19:29:07 +0530148def _get_price_list_rate(args, item_bean, meta):
Akhilesh Darjeefcd70d02013-10-18 14:24:21 +0530149 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 Darjeedb59ffb2013-09-11 13:05:24 +0530151 (args.selling_price_list, args.item_code), as_dict=1)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530152
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530153 if not ref_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530154 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 Darjeedb59ffb2013-09-11 13:05:24 +0530160 return {"ref_rate": flt(ref_rate[0].ref_rate) * flt(args.plc_conversion_rate) / flt(args.conversion_rate)}
Nabin Hait50f5fd12013-07-22 13:10:13 +0530161
162def _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 Doshi42633442013-09-03 16:21:21 +0530167 ORDER BY parent.lft desc""", (item_group,))]
Nabin Hait50f5fd12013-07-22 13:10:13 +0530168
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 Doshifc777182013-05-27 19:29:07 +0530178
179@webnotes.whitelist()
180def get_available_qty(item_code, warehouse):
181 return webnotes.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Anand Doshi1dde46a2013-05-15 21:15:57 +0530182 ["projected_qty", "actual_qty"], as_dict=True) or {}
183
184def _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 Doshif3096132013-05-21 19:35:06 +0530189
190def 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 Hait50f5fd12013-07-22 13:10:13 +0530193
Anand Doshif3096132013-05-21 19:35:06 +0530194 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 Doshif90df7d2013-07-29 19:53:44 +0530197
Anand Doshif3096132013-05-21 19:35:06 +0530198 return pos_settings and pos_settings[0] or None
199
200def 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 Doshifc777182013-05-27 19:29:07 +0530208 out["actual_qty"] = get_available_qty(opts.item_code, out.get("warehouse")).get("actual_qty")
Anand Doshif3096132013-05-21 19:35:06 +0530209
210 return out