blob: 5f2cfbd307c6864bba37d475db7462aac74531e0 [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,
Anand Doshie8132122013-06-17 15:42:38 +0530114 "reserved_warehouse": item.default_warehouse or args.warehouse or args.reserved_warehouse,
Anand Doshi1dde46a2013-05-15 21:15:57 +0530115 "warehouse": item.default_warehouse or args.warehouse,
Rushabh Mehtad9e70702013-07-08 18:01:46 +0530116 "income_account": item.default_income_account or args.income_account \
117 or webnotes.conn.get_value("Company", args.company, "default_income_account"),
118 "expense_account": item.purchase_account or args.expense_account \
119 or webnotes.conn.get_value("Company", args.company, "default_expense_account"),
Anand Doshi1dde46a2013-05-15 21:15:57 +0530120 "cost_center": item.default_sales_cost_center or args.cost_center,
121 "qty": 1.0,
122 "adj_rate": 0.0,
123 "export_amount": 0.0,
124 "amount": 0.0,
125 "batch_no": None,
126 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
127 item_bean.doclist.get({"parentfield": "item_tax"})))),
128 })
129
130 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
131 out[fieldname] = item.fields.get(fieldname)
132
133 return out
134
Anand Doshifc777182013-05-27 19:29:07 +0530135def _get_price_list_rate(args, item_bean, meta):
Anand Doshi1dde46a2013-05-15 21:15:57 +0530136 base_ref_rate = item_bean.doclist.get({
137 "parentfield": "ref_rate_details",
138 "price_list_name": args.price_list_name,
Anand Doshic2a35272013-06-19 17:19:20 +0530139 "ref_currency": args.price_list_currency,
Anand Doshi060d9242013-06-12 17:40:36 +0530140 "buying_or_selling": "Selling"})
Anand Doshif3096132013-05-21 19:35:06 +0530141
142 if not base_ref_rate:
143 return {}
144
145 # found price list rate - now we can validate
146 from utilities.transaction_base import validate_currency
147 validate_currency(args, item_bean.doc, meta)
148
Anand Doshifc777182013-05-27 19:29:07 +0530149 return {"ref_rate": flt(base_ref_rate[0].ref_rate * args.plc_conversion_rate / args.conversion_rate)}
150
151@webnotes.whitelist()
152def get_available_qty(item_code, warehouse):
153 return webnotes.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
Anand Doshi1dde46a2013-05-15 21:15:57 +0530154 ["projected_qty", "actual_qty"], as_dict=True) or {}
155
156def _get_customer_item_code(args, item_bean):
157 customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
158 "customer_name": args.customer})
159
160 return customer_item_code and customer_item_code[0].ref_code or None
Anand Doshif3096132013-05-21 19:35:06 +0530161
162def get_pos_settings(company):
163 pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting` where user = %s
164 and company = %s""", (webnotes.session['user'], company), as_dict=1)
165
166 if not pos_settings:
167 pos_settings = webnotes.conn.sql("""select * from `tabPOS Setting`
168 where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
169
170 return pos_settings and pos_settings[0] or None
171
172def apply_pos_settings(pos_settings, opts):
173 out = {}
174
175 for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
176 if not opts.get(fieldname):
177 out[fieldname] = pos_settings.get(fieldname)
178
179 if out.get("warehouse"):
Anand Doshifc777182013-05-27 19:29:07 +0530180 out["actual_qty"] = get_available_qty(opts.item_code, out.get("warehouse")).get("actual_qty")
Anand Doshif3096132013-05-21 19:35:06 +0530181
182 return out