blob: 43a5ad84c461e0dea73c39a68e743e15e454d227 [file] [log] [blame]
Nabin Haita76a0682013-02-08 14:04:13 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18import webnotes
Anand Doshi1dde46a2013-05-15 21:15:57 +053019from webnotes import msgprint, _
Anand Doshif3096132013-05-21 19:35:06 +053020from webnotes.utils import flt, cint, comma_and
Anand Doshi1dde46a2013-05-15 21:15:57 +053021import json
Nabin Haita76a0682013-02-08 14:04:13 +053022
23def get_customer_list(doctype, txt, searchfield, start, page_len, filters):
24 if webnotes.conn.get_default("cust_master_name") == "Customer Name":
25 fields = ["name", "customer_group", "territory"]
26 else:
27 fields = ["name", "customer_name", "customer_group", "territory"]
28
29 return webnotes.conn.sql("""select %s from `tabCustomer` where docstatus < 2
30 and (%s like %s or customer_name like %s) order by
31 case when name like %s then 0 else 1 end,
32 case when customer_name like %s then 0 else 1 end,
33 name, customer_name limit %s, %s""" %
34 (", ".join(fields), searchfield, "%s", "%s", "%s", "%s", "%s", "%s"),
Anand Doshi1dde46a2013-05-15 21:15:57 +053035 ("%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, start, page_len))
36
37@webnotes.whitelist()
38def get_item_details(args):
39 """
40 args = {
41 "item_code": "",
42 "warehouse": None,
43 "customer": "",
44 "conversion_rate": 1.0,
45 "price_list_name": None,
46 "price_list_currency": None,
47 "plc_conversion_rate": 1.0
48 }
49 """
50 if isinstance(args, basestring):
51 args = json.loads(args)
52 args = webnotes._dict(args)
53
Anand Doshif3096132013-05-21 19:35:06 +053054 if args.barcode:
55 args.item_code = _get_item_code(args.barcode)
56
Anand Doshi1dde46a2013-05-15 21:15:57 +053057 item_bean = webnotes.bean("Item", args.item_code)
58
59 _validate_item_details(args, item_bean.doc)
60
Anand Doshif3096132013-05-21 19:35:06 +053061 meta = webnotes.get_doctype(args.doctype)
Anand Doshi1dc95ed2013-07-23 13:36:38 +053062
63 # hack! for Sales Order Item
64 warehouse_fieldname = "warehouse"
65 if meta.get_field("reserved_warehouse", parentfield=args.parentfield):
66 warehouse_fieldname = "reserved_warehouse"
67
68 out = _get_basic_details(args, item_bean, warehouse_fieldname)
69
Anand Doshif3096132013-05-21 19:35:06 +053070 if meta.get_field("currency"):
71 out.base_ref_rate = out.basic_rate = out.ref_rate = out.export_rate = 0.0
72
73 if args.price_list_name and args.price_list_currency:
74 out.update(_get_price_list_rate(args, item_bean, meta))
Nabin Hait50f5fd12013-07-22 13:10:13 +053075
76 out.update(_get_item_discount(out.item_group, args.customer))
Anand Doshi1dde46a2013-05-15 21:15:57 +053077
Anand Doshi1dc95ed2013-07-23 13:36:38 +053078 if out.get(warehouse_fieldname):
79 out.update(get_available_qty(args.item_code, out.get(warehouse_fieldname)))
Anand Doshi1dde46a2013-05-15 21:15:57 +053080
81 out.customer_item_code = _get_customer_item_code(args, item_bean)
82
Anand Doshif3096132013-05-21 19:35:06 +053083 if cint(args.is_pos):
84 pos_settings = get_pos_settings(args.company)
85 out.update(apply_pos_settings(pos_settings, out))
86
Anand Doshi1dde46a2013-05-15 21:15:57 +053087 return out
88
Anand Doshif3096132013-05-21 19:35:06 +053089def _get_item_code(barcode):
90 item_code = webnotes.conn.sql_list("""select name from `tabItem` where barcode=%s""", barcode)
91
92 if not item_code:
93 msgprint(_("No Item found with Barcode") + ": %s" % barcode, raise_exception=True)
94
95 elif len(item_code) > 1:
96 msgprint(_("Items") + " %s " % comma_and(item_code) +
97 _("have the same Barcode") + " %s" % barcode, raise_exception=True)
98
99 return item_code[0]
100
Anand Doshi1dde46a2013-05-15 21:15:57 +0530101def _validate_item_details(args, item):
102 from utilities.transaction_base import validate_item_fetch
103 validate_item_fetch(args, item)
104
105 # validate if sales item or service item
106 if args.order_type == "Maintenance":
107 if item.is_service_item != "Yes":
108 msgprint(_("Item") + (" %s: " % item.name) +
109 _("not a service item.") +
110 _("Please select a service item or change the order type to Sales."),
111 raise_exception=True)
112
113 elif item.is_sales_item != "Yes":
114 msgprint(_("Item") + (" %s: " % item.name) + _("not a sales item"),
115 raise_exception=True)
116
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530117def _get_basic_details(args, item_bean, warehouse_fieldname):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530118 item = item_bean.doc
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530119
Anand Doshi1dde46a2013-05-15 21:15:57 +0530120 out = webnotes._dict({
Anand Doshifc777182013-05-27 19:29:07 +0530121 "item_code": item.name,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530122 "description": item.description_html or item.description,
Anand Doshi1dc95ed2013-07-23 13:36:38 +0530123 warehouse_fieldname: item.default_warehouse or args.get(warehouse_fieldname),
Rushabh Mehtad9e70702013-07-08 18:01:46 +0530124 "income_account": item.default_income_account or args.income_account \
125 or webnotes.conn.get_value("Company", args.company, "default_income_account"),
126 "expense_account": item.purchase_account or args.expense_account \
127 or webnotes.conn.get_value("Company", args.company, "default_expense_account"),
Anand Doshi1dde46a2013-05-15 21:15:57 +0530128 "cost_center": item.default_sales_cost_center or args.cost_center,
129 "qty": 1.0,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530130 "export_amount": 0.0,
131 "amount": 0.0,
132 "batch_no": None,
133 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
134 item_bean.doclist.get({"parentfield": "item_tax"})))),
135 })
136
137 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
138 out[fieldname] = item.fields.get(fieldname)
139
140 return out
141
Anand Doshifc777182013-05-27 19:29:07 +0530142def _get_price_list_rate(args, item_bean, meta):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530143 base_ref_rate = item_bean.doclist.get({
144 "parentfield": "ref_rate_details",
145 "price_list_name": args.price_list_name,
Anand Doshic2a35272013-06-19 17:19:20 +0530146 "ref_currency": args.price_list_currency,
Anand Doshi060d9242013-06-12 17:40:36 +0530147 "buying_or_selling": "Selling"})
Anand Doshif3096132013-05-21 19:35:06 +0530148
149 if not base_ref_rate:
150 return {}
151
152 # found price list rate - now we can validate
153 from utilities.transaction_base import validate_currency
154 validate_currency(args, item_bean.doc, meta)
155
Nabin Hait2d57e512013-07-31 17:31:23 +0530156 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 +0530157
158def _get_item_discount(item_group, customer):
159 parent_item_groups = [x[0] for x in webnotes.conn.sql("""SELECT parent.name
160 FROM `tabItem Group` AS node, `tabItem Group` AS parent
161 WHERE parent.lft <= node.lft and parent.rgt >= node.rgt and node.name = %s
162 GROUP BY parent.name
163 ORDER BY parent.lft desc""", item_group)]
164
165 discount = 0
166 for d in parent_item_groups:
167 res = webnotes.conn.sql("""select discount, name from `tabCustomer Discount`
168 where parent = %s and item_group = %s""", (customer, d))
169 if res:
170 discount = flt(res[0][0])
171 break
172
173 return {"adj_rate": discount}
Anand Doshifc777182013-05-27 19:29:07 +0530174
175@webnotes.whitelist()
176def get_available_qty(item_code, warehouse):
177 return webnotes.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Anand Doshi1dde46a2013-05-15 21:15:57 +0530178 ["projected_qty", "actual_qty"], as_dict=True) or {}
179
180def _get_customer_item_code(args, item_bean):
181 customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
182 "customer_name": args.customer})
183
184 return customer_item_code and customer_item_code[0].ref_code or None
Anand Doshif3096132013-05-21 19:35:06 +0530185
186def get_pos_settings(company):
187 pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` where user = %s
188 and company = %s""", (webnotes.session['user'], company), as_dict=1)
Nabin Hait50f5fd12013-07-22 13:10:13 +0530189
Anand Doshif3096132013-05-21 19:35:06 +0530190 if not pos_settings:
191 pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting`
192 where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
Anand Doshif90df7d2013-07-29 19:53:44 +0530193
Anand Doshif3096132013-05-21 19:35:06 +0530194 return pos_settings and pos_settings[0] or None
195
196def apply_pos_settings(pos_settings, opts):
197 out = {}
198
199 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
200 if not opts.get(fieldname):
201 out[fieldname] = pos_settings.get(fieldname)
202
203 if out.get("warehouse"):
Anand Doshifc777182013-05-27 19:29:07 +0530204 out["actual_qty"] = get_available_qty(opts.item_code, out.get("warehouse")).get("actual_qty")
Anand Doshif3096132013-05-21 19:35:06 +0530205
206 return out