blob: 6901028710caa04aa2138286ffa48e0613f5efd6 [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
61 out = _get_basic_details(args, item_bean)
62
Anand Doshif3096132013-05-21 19:35:06 +053063 meta = webnotes.get_doctype(args.doctype)
64 if meta.get_field("currency"):
65 out.base_ref_rate = out.basic_rate = out.ref_rate = out.export_rate = 0.0
66
67 if args.price_list_name and args.price_list_currency:
68 out.update(_get_price_list_rate(args, item_bean, meta))
Anand Doshi1dde46a2013-05-15 21:15:57 +053069
70 if out.warehouse or out.reserved_warehouse:
Anand Doshifc777182013-05-27 19:29:07 +053071 out.update(get_available_qty(args.item_code, out.warehouse or out.reserved_warehouse))
Anand Doshi1dde46a2013-05-15 21:15:57 +053072
73 out.customer_item_code = _get_customer_item_code(args, item_bean)
74
Anand Doshif3096132013-05-21 19:35:06 +053075 if cint(args.is_pos):
76 pos_settings = get_pos_settings(args.company)
77 out.update(apply_pos_settings(pos_settings, out))
78
Anand Doshi1dde46a2013-05-15 21:15:57 +053079 return out
80
Anand Doshif3096132013-05-21 19:35:06 +053081def _get_item_code(barcode):
82 item_code = webnotes.conn.sql_list("""select name from `tabItem` where barcode=%s""", barcode)
83
84 if not item_code:
85 msgprint(_("No Item found with Barcode") + ": %s" % barcode, raise_exception=True)
86
87 elif len(item_code) > 1:
88 msgprint(_("Items") + " %s " % comma_and(item_code) +
89 _("have the same Barcode") + " %s" % barcode, raise_exception=True)
90
91 return item_code[0]
92
Anand Doshi1dde46a2013-05-15 21:15:57 +053093def _validate_item_details(args, item):
94 from utilities.transaction_base import validate_item_fetch
95 validate_item_fetch(args, item)
96
97 # validate if sales item or service item
98 if args.order_type == "Maintenance":
99 if item.is_service_item != "Yes":
100 msgprint(_("Item") + (" %s: " % item.name) +
101 _("not a service item.") +
102 _("Please select a service item or change the order type to Sales."),
103 raise_exception=True)
104
105 elif item.is_sales_item != "Yes":
106 msgprint(_("Item") + (" %s: " % item.name) + _("not a sales item"),
107 raise_exception=True)
108
109def _get_basic_details(args, item_bean):
110 item = item_bean.doc
111 out = webnotes._dict({
Anand Doshifc777182013-05-27 19:29:07 +0530112 "item_code": item.name,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530113 "description": item.description_html or item.description,
114 "reserved_warehouse": item.default_warehouse,
115 "warehouse": item.default_warehouse or args.warehouse,
116 "income_account": item.default_income_account or args.income_account,
117 "expense_account": item.purchase_account or args.expense_account,
118 "cost_center": item.default_sales_cost_center or args.cost_center,
119 "qty": 1.0,
120 "adj_rate": 0.0,
121 "export_amount": 0.0,
122 "amount": 0.0,
123 "batch_no": None,
124 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
125 item_bean.doclist.get({"parentfield": "item_tax"})))),
126 })
127
128 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
129 out[fieldname] = item.fields.get(fieldname)
130
131 return out
132
Anand Doshifc777182013-05-27 19:29:07 +0530133def _get_price_list_rate(args, item_bean, meta):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530134 base_ref_rate = item_bean.doclist.get({
135 "parentfield": "ref_rate_details",
136 "price_list_name": args.price_list_name,
137 "price_list_currency": args.price_list_currency,
138 "selling": 1})
Anand Doshif3096132013-05-21 19:35:06 +0530139
140 if not base_ref_rate:
141 return {}
142
143 # found price list rate - now we can validate
144 from utilities.transaction_base import validate_currency
145 validate_currency(args, item_bean.doc, meta)
146
Anand Doshifc777182013-05-27 19:29:07 +0530147 return {"ref_rate": flt(base_ref_rate[0].ref_rate * args.plc_conversion_rate / args.conversion_rate)}
148
149@webnotes.whitelist()
150def get_available_qty(item_code, warehouse):
151 return webnotes.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Anand Doshi1dde46a2013-05-15 21:15:57 +0530152 ["projected_qty", "actual_qty"], as_dict=True) or {}
153
154def _get_customer_item_code(args, item_bean):
155 customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
156 "customer_name": args.customer})
157
158 return customer_item_code and customer_item_code[0].ref_code or None
Anand Doshif3096132013-05-21 19:35:06 +0530159
160def get_pos_settings(company):
161 pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` where user = %s
162 and company = %s""", (webnotes.session['user'], company), as_dict=1)
163
164 if not pos_settings:
165 pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting`
166 where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
167
168 return pos_settings and pos_settings[0] or None
169
170def apply_pos_settings(pos_settings, opts):
171 out = {}
172
173 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
174 if not opts.get(fieldname):
175 out[fieldname] = pos_settings.get(fieldname)
176
177 if out.get("warehouse"):
Anand Doshifc777182013-05-27 19:29:07 +0530178 out["actual_qty"] = get_available_qty(opts.item_code, out.get("warehouse")).get("actual_qty")
Anand Doshif3096132013-05-21 19:35:06 +0530179
180 return out