blob: 23574dfa9eb3c89a9703df33313dab7da390745a [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, _
20from webnotes.utils import flt
21import 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
54 item_bean = webnotes.bean("Item", args.item_code)
55
56 _validate_item_details(args, item_bean.doc)
57
58 out = _get_basic_details(args, item_bean)
59
60 if args.price_list_name and args.price_list_currency:
61 out.update(_get_price_list_rate(args, item_bean))
62
63 if out.warehouse or out.reserved_warehouse:
64 out.update(_get_available_qty(args, out.warehouse or out.reserved_warehouse))
65
66 out.customer_item_code = _get_customer_item_code(args, item_bean)
67
68 return out
69
70def _validate_item_details(args, item):
71 from utilities.transaction_base import validate_item_fetch
72 validate_item_fetch(args, item)
73
74 # validate if sales item or service item
75 if args.order_type == "Maintenance":
76 if item.is_service_item != "Yes":
77 msgprint(_("Item") + (" %s: " % item.name) +
78 _("not a service item.") +
79 _("Please select a service item or change the order type to Sales."),
80 raise_exception=True)
81
82 elif item.is_sales_item != "Yes":
83 msgprint(_("Item") + (" %s: " % item.name) + _("not a sales item"),
84 raise_exception=True)
85
86def _get_basic_details(args, item_bean):
87 item = item_bean.doc
88 out = webnotes._dict({
89 "description": item.description_html or item.description,
90 "reserved_warehouse": item.default_warehouse,
91 "warehouse": item.default_warehouse or args.warehouse,
92 "income_account": item.default_income_account or args.income_account,
93 "expense_account": item.purchase_account or args.expense_account,
94 "cost_center": item.default_sales_cost_center or args.cost_center,
95 "qty": 1.0,
96 "adj_rate": 0.0,
97 "export_amount": 0.0,
98 "amount": 0.0,
99 "batch_no": None,
100 "item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
101 item_bean.doclist.get({"parentfield": "item_tax"})))),
102 })
103
104 for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
105 out[fieldname] = item.fields.get(fieldname)
106
107 return out
108
109def _get_price_list_rate(args, item_bean):
110 base_ref_rate = item_bean.doclist.get({
111 "parentfield": "ref_rate_details",
112 "price_list_name": args.price_list_name,
113 "price_list_currency": args.price_list_currency,
114 "selling": 1})
115 out = webnotes._dict()
116 out.base_ref_rate = flt(base_ref_rate[0].ref_rate) if base_ref_rate else 0.0
117 out.basic_rate = out.base_ref_rate
118 out.ref_rate = out.base_ref_rate / flt(args.conversion_rate)
119 out.export_rate = out.ref_rate
120 return out
121
122def _get_available_qty(args, warehouse):
123 return webnotes.conn.get_value("Bin", {"item_code": args.item_code, "warehouse": warehouse},
124 ["projected_qty", "actual_qty"], as_dict=True) or {}
125
126def _get_customer_item_code(args, item_bean):
127 customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
128 "customer_name": args.customer})
129
130 return customer_item_code and customer_item_code[0].ref_code or None
131