2014-02-10 09:17:54 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _, throw
|
|
|
|
from frappe.utils import flt, cint, add_days
|
2014-02-10 09:17:54 +00:00
|
|
|
import json
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2014-02-10 09:17:54 +00:00
|
|
|
def get_item_details(args):
|
|
|
|
"""
|
|
|
|
args = {
|
|
|
|
"item_code": "",
|
|
|
|
"warehouse": None,
|
|
|
|
"customer": "",
|
|
|
|
"conversion_rate": 1.0,
|
|
|
|
"selling_price_list": None,
|
|
|
|
"price_list_currency": None,
|
|
|
|
"plc_conversion_rate": 1.0
|
|
|
|
"doctype": "",
|
|
|
|
"docname": "",
|
|
|
|
"supplier": None,
|
|
|
|
"transaction_date": None,
|
|
|
|
"conversion_rate": 1.0,
|
|
|
|
"buying_price_list": None,
|
|
|
|
"is_subcontracted": "Yes" / "No",
|
|
|
|
"transaction_type": "selling"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
if isinstance(args, basestring):
|
|
|
|
args = json.loads(args)
|
2014-02-14 10:17:51 +00:00
|
|
|
args = frappe._dict(args)
|
2014-02-10 09:17:54 +00:00
|
|
|
|
2014-02-11 10:44:52 +00:00
|
|
|
if not args.get("transaction_type"):
|
|
|
|
args.transaction_type = "selling" if args.get("customer") else "buying"
|
2014-02-12 09:23:18 +00:00
|
|
|
|
|
|
|
if not args.get("price_list"):
|
|
|
|
args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
|
2014-02-11 10:44:52 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
if args.barcode:
|
|
|
|
args.item_code = get_item_code(barcode=args.barcode)
|
|
|
|
elif not args.item_code and args.serial_no:
|
|
|
|
args.item_code = get_item_code(serial_no=args.serial_no)
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
item_bean = frappe.bean("Item", args.item_code)
|
2014-02-10 09:17:54 +00:00
|
|
|
item = item_bean.doc
|
|
|
|
|
|
|
|
validate_item_details(args, item)
|
|
|
|
|
|
|
|
out = get_basic_details(args, item_bean)
|
|
|
|
|
|
|
|
get_party_item_code(args, item_bean, out)
|
|
|
|
|
|
|
|
if out.get("warehouse"):
|
|
|
|
out.update(get_available_qty(args.item_code, out.warehouse))
|
|
|
|
out.update(get_projected_qty(item.name, out.warehouse))
|
|
|
|
|
|
|
|
get_price_list_rate(args, item_bean, out)
|
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
out.update(get_item_discount(out.item_group, args.customer))
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
if args.transaction_type == "selling" and cint(args.is_pos):
|
|
|
|
out.update(get_pos_settings_item_details(args.company, args))
|
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
if args.get("doctype") in ("Sales Invoice", "Delivery Note"):
|
2014-02-10 09:17:54 +00:00
|
|
|
if item_bean.doc.has_serial_no == "Yes" and not args.serial_no:
|
2014-02-11 10:44:52 +00:00
|
|
|
out.serial_no = get_serial_nos_by_fifo(args, item_bean)
|
2014-02-12 09:23:18 +00:00
|
|
|
|
|
|
|
if args.transaction_date and item.lead_time_days:
|
|
|
|
out.schedule_date = out.lead_time_date = add_days(args.transaction_date,
|
|
|
|
item.lead_time_days)
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
return out
|
|
|
|
|
|
|
|
def get_item_code(barcode=None, serial_no=None):
|
|
|
|
if barcode:
|
2014-02-14 10:17:51 +00:00
|
|
|
item_code = frappe.conn.get_value("Item", {"barcode": barcode})
|
2014-02-10 09:17:54 +00:00
|
|
|
elif serial_no:
|
2014-02-14 10:17:51 +00:00
|
|
|
item_code = frappe.conn.get_value("Serial No", serial_no, "item_code")
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
if not item_code:
|
|
|
|
throw(_("No Item found with ") + _("Barcode") if barcode else _("Serial No") +
|
|
|
|
": %s" % (barcode or serial_no))
|
|
|
|
|
|
|
|
return item_code
|
|
|
|
|
|
|
|
def validate_item_details(args, item):
|
|
|
|
if not args.company:
|
|
|
|
throw(_("Please specify Company"))
|
|
|
|
|
|
|
|
from erpnext.stock.doctype.item.item import validate_end_of_life
|
|
|
|
validate_end_of_life(item.name, item.end_of_life)
|
|
|
|
|
|
|
|
if args.transaction_type == "selling":
|
|
|
|
# validate if sales item or service item
|
2014-02-12 09:23:18 +00:00
|
|
|
if args.get("order_type") == "Maintenance":
|
2014-02-10 09:17:54 +00:00
|
|
|
if item.is_service_item != "Yes":
|
|
|
|
throw(_("Item") + (" %s: " % item.name) +
|
|
|
|
_("not a service item.") +
|
|
|
|
_("Please select a service item or change the order type to Sales."))
|
|
|
|
|
|
|
|
elif item.is_sales_item != "Yes":
|
|
|
|
throw(_("Item") + (" %s: " % item.name) + _("not a sales item"))
|
|
|
|
|
|
|
|
elif args.transaction_type == "buying":
|
|
|
|
# validate if purchase item or subcontracted item
|
|
|
|
if item.is_purchase_item != "Yes":
|
|
|
|
throw(_("Item") + (" %s: " % item.name) + _("not a purchase item"))
|
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
if args.get("is_subcontracted") == "Yes" and item.is_sub_contracted_item != "Yes":
|
2014-02-10 09:17:54 +00:00
|
|
|
throw(_("Item") + (" %s: " % item.name) +
|
|
|
|
_("not a sub-contracted item.") +
|
|
|
|
_("Please select a sub-contracted item or do not sub-contract the transaction."))
|
|
|
|
|
|
|
|
def get_basic_details(args, item_bean):
|
|
|
|
item = item_bean.doc
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.defaults import get_user_default_as_list
|
2014-02-10 09:17:54 +00:00
|
|
|
user_default_warehouse_list = get_user_default_as_list('warehouse')
|
|
|
|
user_default_warehouse = user_default_warehouse_list[0] \
|
|
|
|
if len(user_default_warehouse_list)==1 else ""
|
2014-02-12 13:39:28 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
out = frappe._dict({
|
2014-02-12 09:23:18 +00:00
|
|
|
"item_code": item.name,
|
|
|
|
"item_name": item.item_name,
|
|
|
|
"description": item.description_html or item.description,
|
|
|
|
"warehouse": user_default_warehouse or args.warehouse or item.default_warehouse,
|
2014-02-12 10:34:17 +00:00
|
|
|
"income_account": item.income_account or args.income_account \
|
2014-02-14 10:17:51 +00:00
|
|
|
or frappe.conn.get_value("Company", args.company, "default_income_account"),
|
2014-02-12 09:23:18 +00:00
|
|
|
"expense_account": item.expense_account or args.expense_account \
|
2014-02-14 10:17:51 +00:00
|
|
|
or frappe.conn.get_value("Company", args.company, "default_expense_account"),
|
2014-02-12 09:23:18 +00:00
|
|
|
"cost_center": item.selling_cost_center \
|
2014-02-12 13:39:28 +00:00
|
|
|
if args.transaction_type == "selling" else item.buying_cost_center,
|
2014-02-12 09:23:18 +00:00
|
|
|
"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"})))),
|
|
|
|
"uom": item.stock_uom,
|
|
|
|
"min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
|
|
|
|
"conversion_factor": 1.0,
|
|
|
|
"qty": 1.0,
|
|
|
|
"price_list_rate": 0.0,
|
|
|
|
"base_price_list_rate": 0.0,
|
|
|
|
"rate": 0.0,
|
|
|
|
"base_rate": 0.0,
|
|
|
|
"amount": 0.0,
|
|
|
|
"base_amount": 0.0,
|
|
|
|
"discount_percentage": 0.0
|
|
|
|
})
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
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, out):
|
2014-02-14 10:17:51 +00:00
|
|
|
meta = frappe.get_doctype(args.doctype)
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
if meta.get_field("currency"):
|
|
|
|
validate_price_list(args)
|
|
|
|
validate_conversion_rate(args, meta)
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
price_list_rate = frappe.conn.get_value("Item Price",
|
2014-02-10 12:24:04 +00:00
|
|
|
{"price_list": args.price_list, "item_code": args.item_code}, "price_list_rate")
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
if not price_list_rate: return {}
|
|
|
|
|
|
|
|
out.price_list_rate = flt(price_list_rate) * flt(args.plc_conversion_rate) \
|
|
|
|
/ flt(args.conversion_rate)
|
|
|
|
|
|
|
|
if not out.price_list_rate and args.transaction_type == "buying":
|
|
|
|
from erpnext.stock.doctype.item.item import get_last_purchase_details
|
|
|
|
out.update(get_last_purchase_details(item_bean.doc.name,
|
|
|
|
args.docname, args.conversion_rate))
|
|
|
|
|
|
|
|
def validate_price_list(args):
|
|
|
|
if args.get("price_list"):
|
2014-02-14 10:17:51 +00:00
|
|
|
if not frappe.conn.get_value("Price List",
|
2014-02-10 09:17:54 +00:00
|
|
|
{"name": args.price_list, args.transaction_type: 1, "enabled": 1}):
|
|
|
|
throw(_("Price List is either disabled or for not ") + _(args.transaction_type))
|
|
|
|
else:
|
|
|
|
throw(_("Price List not selected"))
|
|
|
|
|
|
|
|
def validate_conversion_rate(args, meta):
|
|
|
|
from erpnext.setup.doctype.currency.currency import validate_conversion_rate
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.model.meta import get_field_precision
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
# validate currency conversion rate
|
|
|
|
validate_conversion_rate(args.currency, args.conversion_rate,
|
|
|
|
meta.get_label("conversion_rate"), args.company)
|
|
|
|
|
|
|
|
args.conversion_rate = flt(args.conversion_rate,
|
|
|
|
get_field_precision(meta.get_field("conversion_rate"),
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe._dict({"fields": args})))
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
# validate price list currency conversion rate
|
|
|
|
if not args.get("price_list_currency"):
|
|
|
|
throw(_("Price List Currency not selected"))
|
|
|
|
else:
|
|
|
|
validate_conversion_rate(args.price_list_currency, args.plc_conversion_rate,
|
|
|
|
meta.get_label("plc_conversion_rate"), args.company)
|
|
|
|
|
|
|
|
args.plc_conversion_rate = flt(args.plc_conversion_rate,
|
|
|
|
get_field_precision(meta.get_field("plc_conversion_rate"),
|
2014-02-14 10:17:51 +00:00
|
|
|
frappe._dict({"fields": args})))
|
2014-02-10 09:17:54 +00:00
|
|
|
|
2014-02-12 09:23:18 +00:00
|
|
|
def get_item_discount(item_group, customer):
|
2014-02-14 10:17:51 +00:00
|
|
|
parent_item_groups = [x[0] for x in frappe.conn.sql("""SELECT parent.name
|
2014-02-12 09:23:18 +00:00
|
|
|
FROM `tabItem Group` AS node, `tabItem Group` AS parent
|
|
|
|
WHERE parent.lft <= node.lft and parent.rgt >= node.rgt and node.name = %s
|
|
|
|
GROUP BY parent.name
|
|
|
|
ORDER BY parent.lft desc""", (item_group,))]
|
|
|
|
|
|
|
|
discount = 0
|
|
|
|
for d in parent_item_groups:
|
2014-02-14 10:17:51 +00:00
|
|
|
res = frappe.conn.sql("""select discount, name from `tabCustomer Discount`
|
2014-02-12 09:23:18 +00:00
|
|
|
where parent = %s and item_group = %s""", (customer, d))
|
|
|
|
if res:
|
|
|
|
discount = flt(res[0][0])
|
|
|
|
break
|
|
|
|
|
|
|
|
return {"discount_percentage": discount}
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
def get_party_item_code(args, item_bean, out):
|
|
|
|
if args.transaction_type == "selling":
|
|
|
|
customer_item_code = item_bean.doclist.get({"parentfield": "item_customer_details",
|
|
|
|
"customer_name": args.customer})
|
|
|
|
out.customer_item_code = customer_item_code[0].ref_code if customer_item_code else None
|
|
|
|
else:
|
|
|
|
item_supplier = item_bean.doclist.get({"parentfield": "item_supplier_details",
|
|
|
|
"supplier": args.supplier})
|
|
|
|
out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
|
|
|
|
|
|
|
|
|
|
|
|
def get_pos_settings_item_details(company, args, pos_settings=None):
|
2014-02-14 10:17:51 +00:00
|
|
|
res = frappe._dict()
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
if not pos_settings:
|
|
|
|
pos_settings = get_pos_settings(company)
|
|
|
|
|
|
|
|
if pos_settings:
|
|
|
|
for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
|
|
|
|
if not args.get(fieldname):
|
|
|
|
res[fieldname] = pos_settings.get(fieldname)
|
|
|
|
|
|
|
|
if res.get("warehouse"):
|
|
|
|
res.actual_qty = get_available_qty(args.item_code,
|
|
|
|
res.warehouse).get("actual_qty")
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
def get_pos_settings(company):
|
2014-02-14 10:17:51 +00:00
|
|
|
pos_settings = frappe.conn.sql("""select * from `tabPOS Setting` where user = %s
|
|
|
|
and company = %s""", (frappe.session['user'], company), as_dict=1)
|
2014-02-10 09:17:54 +00:00
|
|
|
|
|
|
|
if not pos_settings:
|
2014-02-14 10:17:51 +00:00
|
|
|
pos_settings = frappe.conn.sql("""select * from `tabPOS Setting`
|
2014-02-10 09:17:54 +00:00
|
|
|
where ifnull(user,'') = '' and company = %s""", company, as_dict=1)
|
|
|
|
|
|
|
|
return pos_settings and pos_settings[0] or None
|
2014-02-12 09:23:18 +00:00
|
|
|
|
|
|
|
def get_serial_nos_by_fifo(args, item_bean):
|
2014-02-14 10:17:51 +00:00
|
|
|
return "\n".join(frappe.conn.sql_list("""select name from `tabSerial No`
|
2014-02-12 09:23:18 +00:00
|
|
|
where item_code=%(item_code)s and warehouse=%(warehouse)s and status='Available'
|
|
|
|
order by timestamp(purchase_date, purchase_time) asc limit %(qty)s""", {
|
|
|
|
"item_code": args.item_code,
|
|
|
|
"warehouse": args.warehouse,
|
|
|
|
"qty": cint(args.qty)
|
|
|
|
}))
|
2014-02-10 09:17:54 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2014-02-10 09:17:54 +00:00
|
|
|
def get_conversion_factor(item_code, uom):
|
2014-02-14 10:17:51 +00:00
|
|
|
return {"conversion_factor": frappe.conn.get_value("UOM Conversion Detail",
|
2014-02-10 09:17:54 +00:00
|
|
|
{"parent": item_code, "uom": uom}, "conversion_factor")}
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2014-02-10 09:17:54 +00:00
|
|
|
def get_projected_qty(item_code, warehouse):
|
2014-02-14 10:17:51 +00:00
|
|
|
return {"projected_qty": frappe.conn.get_value("Bin",
|
2014-02-10 09:17:54 +00:00
|
|
|
{"item_code": item_code, "warehouse": warehouse}, "projected_qty")}
|
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2014-02-10 09:17:54 +00:00
|
|
|
def get_available_qty(item_code, warehouse):
|
2014-02-14 10:17:51 +00:00
|
|
|
return frappe.conn.get_value("Bin", {"item_code": item_code, "warehouse": warehouse},
|
2014-02-10 09:17:54 +00:00
|
|
|
["projected_qty", "actual_qty"], as_dict=True) or {}
|