blob: 801d82bf40ec9d82119966451c049ebcc088eb99 [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:
43 args.item_code = _get_item_code(args.barcode)
44
Anand Doshi1dde46a2013-05-15 21:15:57 +053045 item_bean = webnotes.bean("Item", args.item_code)
46
47 _validate_item_details(args, item_bean.doc)
48
Anand Doshif3096132013-05-21 19:35:06 +053049 meta = webnotes.get_doctype(args.doctype)
Anand Doshi1dc95ed2013-07-23 13:36:38 +053050
51 # hack! for Sales Order Item
52 warehouse_fieldname = "warehouse"
53 if meta.get_field("reserved_warehouse", parentfield=args.parentfield):
54 warehouse_fieldname = "reserved_warehouse"
55
56 out = _get_basic_details(args, item_bean, warehouse_fieldname)
57
Anand Doshif3096132013-05-21 19:35:06 +053058 if meta.get_field("currency"):
59 out.base_ref_rate = out.basic_rate = out.ref_rate = out.export_rate = 0.0
60
Rushabh Mehta4a404e92013-08-09 18:11:35 +053061 if args.selling_price_list and args.price_list_currency:
Anand Doshif3096132013-05-21 19:35:06 +053062 out.update(_get_price_list_rate(args, item_bean, meta))
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +053063
Nabin Hait50f5fd12013-07-22 13:10:13 +053064 out.update(_get_item_discount(out.item_group, args.customer))
Anand Doshi1dde46a2013-05-15 21:15:57 +053065
Anand Doshi1dc95ed2013-07-23 13:36:38 +053066 if out.get(warehouse_fieldname):
67 out.update(get_available_qty(args.item_code, out.get(warehouse_fieldname)))
Anand Doshi1dde46a2013-05-15 21:15:57 +053068
69 out.customer_item_code = _get_customer_item_code(args, item_bean)
70
Anand Doshif3096132013-05-21 19:35:06 +053071 if cint(args.is_pos):
72 pos_settings = get_pos_settings(args.company)
Rushabh Mehta3423a732013-08-16 13:03:34 +053073 if pos_settings:
74 out.update(apply_pos_settings(pos_settings, out))
Rushabh Mehta62030e02013-08-14 18:37:28 +053075
76 if args.doctype in ("Sales Invoice", "Delivery Note"):
77 if item_bean.doc.has_serial_no and not args.serial_no:
78 out.serial_no = _get_serial_nos_by_fifo(args, item_bean)
79
Anand Doshi1dde46a2013-05-15 21:15:57 +053080 return out
Rushabh Mehta62030e02013-08-14 18:37:28 +053081
82def _get_serial_nos_by_fifo(args, item_bean):
83 return "\n".join(webnotes.conn.sql_list("""select name from `tabSerial No`
84 where item_code=%(item_code)s and warehouse=%(warehouse)s and status='Available'
Rushabh Mehta3423a732013-08-16 13:03:34 +053085 order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
Rushabh Mehta62030e02013-08-14 18:37:28 +053086 "item_code": args.item_code,
87 "warehouse": args.warehouse,
88 "qty": cint(args.qty)
89 }))
90
Anand Doshif3096132013-05-21 19:35:06 +053091def _get_item_code(barcode):
92 item_code = webnotes.conn.sql_list("""select name from `tabItem` where barcode=%s""", barcode)
93
94 if not item_code:
95 msgprint(_("No Item found with Barcode") + ": %s" % barcode, raise_exception=True)
96
97 elif len(item_code) > 1:
98 msgprint(_("Items") + " %s " % comma_and(item_code) +
99 _("have the same Barcode") + " %s" % barcode, raise_exception=True)
100
101 return item_code[0]
102
Anand Doshi1dde46a2013-05-15 21:15:57 +0530103def _validate_item_details(args, item):
104 from utilities.transaction_base import validate_item_fetch
105 validate_item_fetch(args, item)
106
107 # validate if sales item or service item
108 if args.order_type == "Maintenance":
109 if item.is_service_item != "Yes":
110 msgprint(_("Item") + (" %s: " % item.name) +
111 _("not a service item.") +
112 _("Please select a service item or change the order type to Sales."),
113 raise_exception=True)
114
115 elif item.is_sales_item != "Yes":
116 msgprint(_("Item") + (" %s: " % item.name) + _("not a sales item"),
117 raise_exception=True)
118
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530119def _get_basic_details(args, item_bean, warehouse_fieldname):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530120 item = item_bean.doc
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530121
Anand Doshi1dde46a2013-05-15 21:15:57 +0530122 out = webnotes._dict({
Anand Doshifc777182013-05-27 19:29:07 +0530123 "item_code": item.name,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530124 "description": item.description_html or item.description,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530125 warehouse_fieldname: item.default_warehouse or args.get(warehouse_fieldname),
Rushabh Mehtad9e70702013-07-08 18:01:46 +0530126 "income_account": item.default_income_account or args.income_account \
127 or webnotes.conn.get_value("Company", args.company, "default_income_account"),
128 "expense_account": item.purchase_account or args.expense_account \
129 or webnotes.conn.get_value("Company", args.company, "default_expense_account"),
Anand Doshi1dde46a2013-05-15 21:15:57 +0530130 "cost_center": item.default_sales_cost_center or args.cost_center,
131 "qty": 1.0,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530132 "export_amount": 0.0,
133 "amount": 0.0,
134 "batch_no": None,
135 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
136 item_bean.doclist.get({"parentfield": "item_tax"})))),
137 })
138
139 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
140 out[fieldname] = item.fields.get(fieldname)
141
142 return out
143
Anand Doshifc777182013-05-27 19:29:07 +0530144def _get_price_list_rate(args, item_bean, meta):
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530145 ref_rate = webnotes.conn.sql("""select ip.ref_rate from `tabItem Price` ip,
146 `tabPrice List` pl where ip.parent = pl.name and ip.parent=%s and
147 ip.item_code=%s and pl.buying_or_selling='Selling'""",
148 (args.selling_price_list, args.item_code), as_dict=1)
Rushabh Mehta7c2a2e22013-08-07 15:08:11 +0530149
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530150 if not ref_rate:
Anand Doshif3096132013-05-21 19:35:06 +0530151 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
Akhilesh Darjeedb59ffb2013-09-11 13:05:24 +0530157 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 +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
Anand Doshi42633442013-09-03 16:21:21 +0530164 ORDER BY parent.lft desc""", (item_group,))]
Nabin Hait50f5fd12013-07-22 13:10:13 +0530165
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