blob: cbfaee736db30352cdb91ac4d3c62aa1615b9054 [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))
Nabin Hait50f5fd12013-07-22 13:10:13 +053069
70 out.update(_get_item_discount(out.item_group, args.customer))
Anand Doshi1dde46a2013-05-15 21:15:57 +053071
72 if out.warehouse or out.reserved_warehouse:
Anand Doshifc777182013-05-27 19:29:07 +053073 out.update(get_available_qty(args.item_code, out.warehouse or out.reserved_warehouse))
Anand Doshi1dde46a2013-05-15 21:15:57 +053074
75 out.customer_item_code = _get_customer_item_code(args, item_bean)
76
Anand Doshif3096132013-05-21 19:35:06 +053077 if cint(args.is_pos):
78 pos_settings = get_pos_settings(args.company)
79 out.update(apply_pos_settings(pos_settings, out))
80
Anand Doshi1dde46a2013-05-15 21:15:57 +053081 return out
82
Anand Doshif3096132013-05-21 19:35:06 +053083def _get_item_code(barcode):
84 item_code = webnotes.conn.sql_list("""select name from `tabItem` where barcode=%s""", barcode)
85
86 if not item_code:
87 msgprint(_("No Item found with Barcode") + ": %s" % barcode, raise_exception=True)
88
89 elif len(item_code) > 1:
90 msgprint(_("Items") + " %s " % comma_and(item_code) +
91 _("have the same Barcode") + " %s" % barcode, raise_exception=True)
92
93 return item_code[0]
94
Anand Doshi1dde46a2013-05-15 21:15:57 +053095def _validate_item_details(args, item):
96 from utilities.transaction_base import validate_item_fetch
97 validate_item_fetch(args, item)
98
99 # validate if sales item or service item
100 if args.order_type == "Maintenance":
101 if item.is_service_item != "Yes":
102 msgprint(_("Item") + (" %s: " % item.name) +
103 _("not a service item.") +
104 _("Please select a service item or change the order type to Sales."),
105 raise_exception=True)
106
107 elif item.is_sales_item != "Yes":
108 msgprint(_("Item") + (" %s: " % item.name) + _("not a sales item"),
109 raise_exception=True)
110
111def _get_basic_details(args, item_bean):
112 item = item_bean.doc
113 out = webnotes._dict({
Anand Doshifc777182013-05-27 19:29:07 +0530114 "item_code": item.name,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530115 "description": item.description_html or item.description,
Anand Doshie8132122013-06-17 15:42:38 +0530116 "reserved_warehouse": item.default_warehouse or args.warehouse or args.reserved_warehouse,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530117 "warehouse": item.default_warehouse or args.warehouse,
Rushabh Mehtad9e70702013-07-08 18:01:46 +0530118 "income_account": item.default_income_account or args.income_account \
119 or webnotes.conn.get_value("Company", args.company, "default_income_account"),
120 "expense_account": item.purchase_account or args.expense_account \
121 or webnotes.conn.get_value("Company", args.company, "default_expense_account"),
Anand Doshi1dde46a2013-05-15 21:15:57 +0530122 "cost_center": item.default_sales_cost_center or args.cost_center,
123 "qty": 1.0,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530124 "export_amount": 0.0,
125 "amount": 0.0,
126 "batch_no": None,
127 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
128 item_bean.doclist.get({"parentfield": "item_tax"})))),
129 })
130
131 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
132 out[fieldname] = item.fields.get(fieldname)
133
134 return out
135
Anand Doshifc777182013-05-27 19:29:07 +0530136def _get_price_list_rate(args, item_bean, meta):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530137 base_ref_rate = item_bean.doclist.get({
138 "parentfield": "ref_rate_details",
139 "price_list_name": args.price_list_name,
Anand Doshic2a35272013-06-19 17:19:20 +0530140 "ref_currency": args.price_list_currency,
Anand Doshi060d9242013-06-12 17:40:36 +0530141 "buying_or_selling": "Selling"})
Anand Doshif3096132013-05-21 19:35:06 +0530142
143 if not base_ref_rate:
144 return {}
145
146 # found price list rate - now we can validate
147 from utilities.transaction_base import validate_currency
148 validate_currency(args, item_bean.doc, meta)
149
Anand Doshifc777182013-05-27 19:29:07 +0530150 return {"ref_rate": flt(base_ref_rate[0].ref_rate * args.plc_conversion_rate / args.conversion_rate)}
Nabin Hait50f5fd12013-07-22 13:10:13 +0530151
152def _get_item_discount(item_group, customer):
153 parent_item_groups = [x[0] for x in webnotes.conn.sql("""SELECT parent.name
154 FROM `tabItem Group` AS node, `tabItem Group` AS parent
155 WHERE parent.lft <= node.lft and parent.rgt >= node.rgt and node.name = %s
156 GROUP BY parent.name
157 ORDER BY parent.lft desc""", item_group)]
158
159 discount = 0
160 for d in parent_item_groups:
161 res = webnotes.conn.sql("""select discount, name from `tabCustomer Discount`
162 where parent = %s and item_group = %s""", (customer, d))
163 if res:
164 discount = flt(res[0][0])
165 break
166
167 return {"adj_rate": discount}
Anand Doshifc777182013-05-27 19:29:07 +0530168
169@webnotes.whitelist()
170def get_available_qty(item_code, warehouse):
171 return webnotes.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Anand Doshi1dde46a2013-05-15 21:15:57 +0530172 ["projected_qty", "actual_qty"], as_dict=True) or {}
173
174def _get_customer_item_code(args, item_bean):
175 customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
176 "customer_name": args.customer})
177
178 return customer_item_code and customer_item_code[0].ref_code or None
Anand Doshif3096132013-05-21 19:35:06 +0530179
180def get_pos_settings(company):
181 pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` where user = %s
182 and company = %s""", (webnotes.session['user'], company), as_dict=1)
Nabin Hait50f5fd12013-07-22 13:10:13 +0530183
Anand Doshif3096132013-05-21 19:35:06 +0530184 if not pos_settings:
185 pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting`
186 where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
187
188 return pos_settings and pos_settings[0] or None
189
190def apply_pos_settings(pos_settings, opts):
191 out = {}
192
193 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
194 if not opts.get(fieldname):
195 out[fieldname] = pos_settings.get(fieldname)
196
197 if out.get("warehouse"):
Anand Doshifc777182013-05-27 19:29:07 +0530198 out["actual_qty"] = get_available_qty(opts.item_code, out.get("warehouse")).get("actual_qty")
Anand Doshif3096132013-05-21 19:35:06 +0530199
200 return out