mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-25 16:04:46 +00:00
[buying, selling] [refactor] get item details
This commit is contained in:
101
selling/utils.py
101
selling/utils.py
@@ -16,6 +16,9 @@
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import webnotes
|
||||
from webnotes import msgprint, _
|
||||
from webnotes.utils import flt
|
||||
import json
|
||||
|
||||
def get_customer_list(doctype, txt, searchfield, start, page_len, filters):
|
||||
if webnotes.conn.get_default("cust_master_name") == "Customer Name":
|
||||
@@ -29,4 +32,100 @@ def get_customer_list(doctype, txt, searchfield, start, page_len, filters):
|
||||
case when customer_name like %s then 0 else 1 end,
|
||||
name, customer_name limit %s, %s""" %
|
||||
(", ".join(fields), searchfield, "%s", "%s", "%s", "%s", "%s", "%s"),
|
||||
("%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, start, page_len))
|
||||
("%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, "%%%s%%" % txt, start, page_len))
|
||||
|
||||
@webnotes.whitelist()
|
||||
def get_item_details(args):
|
||||
"""
|
||||
args = {
|
||||
"item_code": "",
|
||||
"warehouse": None,
|
||||
"customer": "",
|
||||
"conversion_rate": 1.0,
|
||||
"price_list_name": None,
|
||||
"price_list_currency": None,
|
||||
"plc_conversion_rate": 1.0
|
||||
}
|
||||
"""
|
||||
if isinstance(args, basestring):
|
||||
args = json.loads(args)
|
||||
args = webnotes._dict(args)
|
||||
|
||||
item_bean = webnotes.bean("Item", args.item_code)
|
||||
|
||||
_validate_item_details(args, item_bean.doc)
|
||||
|
||||
out = _get_basic_details(args, item_bean)
|
||||
|
||||
if args.price_list_name and args.price_list_currency:
|
||||
out.update(_get_price_list_rate(args, item_bean))
|
||||
|
||||
if out.warehouse or out.reserved_warehouse:
|
||||
out.update(_get_available_qty(args, out.warehouse or out.reserved_warehouse))
|
||||
|
||||
out.customer_item_code = _get_customer_item_code(args, item_bean)
|
||||
|
||||
return out
|
||||
|
||||
def _validate_item_details(args, item):
|
||||
from utilities.transaction_base import validate_item_fetch
|
||||
validate_item_fetch(args, item)
|
||||
|
||||
# validate if sales item or service item
|
||||
if args.order_type == "Maintenance":
|
||||
if item.is_service_item != "Yes":
|
||||
msgprint(_("Item") + (" %s: " % item.name) +
|
||||
_("not a service item.") +
|
||||
_("Please select a service item or change the order type to Sales."),
|
||||
raise_exception=True)
|
||||
|
||||
elif item.is_sales_item != "Yes":
|
||||
msgprint(_("Item") + (" %s: " % item.name) + _("not a sales item"),
|
||||
raise_exception=True)
|
||||
|
||||
def _get_basic_details(args, item_bean):
|
||||
item = item_bean.doc
|
||||
out = webnotes._dict({
|
||||
"description": item.description_html or item.description,
|
||||
"reserved_warehouse": item.default_warehouse,
|
||||
"warehouse": item.default_warehouse or args.warehouse,
|
||||
"income_account": item.default_income_account or args.income_account,
|
||||
"expense_account": item.purchase_account or args.expense_account,
|
||||
"cost_center": item.default_sales_cost_center or args.cost_center,
|
||||
"qty": 1.0,
|
||||
"adj_rate": 0.0,
|
||||
"export_amount": 0.0,
|
||||
"amount": 0.0,
|
||||
"batch_no": None,
|
||||
"item_tax_rate": json.dumps(dict(([d.tax_type, d.tax_rate] for d in
|
||||
item_bean.doclist.get({"parentfield": "item_tax"})))),
|
||||
})
|
||||
|
||||
for fieldname in ("item_name", "item_group", "barcode", "brand", "stock_uom"):
|
||||
out[fieldname] = item.fields.get(fieldname)
|
||||
|
||||
return out
|
||||
|
||||
def _get_price_list_rate(args, item_bean):
|
||||
base_ref_rate = item_bean.doclist.get({
|
||||
"parentfield": "ref_rate_details",
|
||||
"price_list_name": args.price_list_name,
|
||||
"price_list_currency": args.price_list_currency,
|
||||
"selling": 1})
|
||||
out = webnotes._dict()
|
||||
out.base_ref_rate = flt(base_ref_rate[0].ref_rate) if base_ref_rate else 0.0
|
||||
out.basic_rate = out.base_ref_rate
|
||||
out.ref_rate = out.base_ref_rate / flt(args.conversion_rate)
|
||||
out.export_rate = out.ref_rate
|
||||
return out
|
||||
|
||||
def _get_available_qty(args, warehouse):
|
||||
return webnotes.conn.get_value("Bin", {"item_code": args.item_code, "warehouse": warehouse},
|
||||
["projected_qty", "actual_qty"], as_dict=True) or {}
|
||||
|
||||
def _get_customer_item_code(args, item_bean):
|
||||
customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
|
||||
"customer_name": args.customer})
|
||||
|
||||
return customer_item_code and customer_item_code[0].ref_code or None
|
||||
|
||||
Reference in New Issue
Block a user