2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2014-02-10 09:17:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2019-03-18 09:04:19 +00:00
|
|
|
import json
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _, throw
|
2022-02-15 05:27:06 +00:00
|
|
|
from frappe.model import child_table_fields, default_fields
|
2014-09-29 06:47:03 +00:00
|
|
|
from frappe.model.meta import get_field_precision
|
2023-03-28 06:46:27 +00:00
|
|
|
from frappe.query_builder.functions import IfNull, Sum
|
2017-04-21 07:10:19 +00:00
|
|
|
from frappe.utils import add_days, add_months, cint, cstr, flt, getdate
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-02-12 09:48:57 +00:00
|
|
|
from erpnext import get_company_currency
|
2019-11-19 13:17:48 +00:00
|
|
|
from erpnext.accounts.doctype.pricing_rule.pricing_rule import (
|
|
|
|
get_pricing_rule_for_item,
|
2018-07-17 12:31:44 +00:00
|
|
|
set_transaction_type,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2018-09-25 09:53:12 +00:00
|
|
|
from erpnext.setup.doctype.brand.brand import get_brand_defaults
|
2018-07-17 12:31:44 +00:00
|
|
|
from erpnext.setup.doctype.item_group.item_group import get_item_group_defaults
|
2014-07-01 12:15:15 +00:00
|
|
|
from erpnext.setup.utils import get_exchange_rate
|
2018-09-11 11:52:25 +00:00
|
|
|
from erpnext.stock.doctype.item.item import get_item_defaults, get_uom_conv_factor
|
2019-06-02 10:33:05 +00:00
|
|
|
from erpnext.stock.doctype.item_manufacturer.item_manufacturer import get_item_manufacturer_part_no
|
2018-02-15 05:58:55 +00:00
|
|
|
from erpnext.stock.doctype.price_list.price_list import get_price_list_details
|
2017-11-23 07:35:15 +00:00
|
|
|
|
2021-01-28 12:28:55 +00:00
|
|
|
sales_doctypes = ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice", "POS Invoice"]
|
2019-02-14 13:00:10 +00:00
|
|
|
purchase_doctypes = [
|
|
|
|
"Material Request",
|
|
|
|
"Supplier Quotation",
|
|
|
|
"Purchase Order",
|
|
|
|
"Purchase Receipt",
|
|
|
|
"Purchase Invoice",
|
|
|
|
]
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-02-14 13:00:10 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2023-04-14 04:29:42 +00:00
|
|
|
def get_item_details(args, doc=None, for_validate=False, overwrite_warehouse=True):
|
2014-02-10 09:17:54 +00:00
|
|
|
"""
|
|
|
|
args = {
|
|
|
|
"item_code": "",
|
|
|
|
"warehouse": None,
|
|
|
|
"customer": "",
|
|
|
|
"conversion_rate": 1.0,
|
|
|
|
"selling_price_list": None,
|
|
|
|
"price_list_currency": None,
|
2014-02-25 13:31:20 +00:00
|
|
|
"plc_conversion_rate": 1.0,
|
2016-01-15 11:29:26 +00:00
|
|
|
"doctype": "",
|
|
|
|
"name": "",
|
2014-02-10 09:17:54 +00:00
|
|
|
"supplier": None,
|
|
|
|
"transaction_date": None,
|
|
|
|
"conversion_rate": 1.0,
|
|
|
|
"buying_price_list": None,
|
2022-04-19 09:27:31 +00:00
|
|
|
"is_subcontracted": 0/1,
|
2014-06-20 10:29:49 +00:00
|
|
|
"ignore_pricing_rule": 0/1
|
2016-03-09 11:32:59 +00:00
|
|
|
"project": ""
|
2019-01-09 08:43:06 +00:00
|
|
|
"set_warehouse": ""
|
2014-02-10 09:17:54 +00:00
|
|
|
}
|
|
|
|
"""
|
2019-08-16 02:46:22 +00:00
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
args = process_args(args)
|
2020-07-14 11:33:17 +00:00
|
|
|
for_validate = process_string_args(for_validate)
|
|
|
|
overwrite_warehouse = process_string_args(overwrite_warehouse)
|
2018-08-08 11:07:31 +00:00
|
|
|
item = frappe.get_cached_doc("Item", args.item_code)
|
2014-02-10 09:17:54 +00:00
|
|
|
validate_item_details(args, item)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2020-01-06 10:04:15 +00:00
|
|
|
if isinstance(doc, str):
|
|
|
|
doc = json.loads(doc)
|
|
|
|
|
|
|
|
if doc:
|
2022-06-16 17:03:47 +00:00
|
|
|
args["transaction_date"] = doc.get("transaction_date") or doc.get("posting_date")
|
2020-01-06 10:04:15 +00:00
|
|
|
|
2022-06-16 17:03:47 +00:00
|
|
|
if doc.get("doctype") == "Purchase Invoice":
|
|
|
|
args["bill_date"] = doc.get("bill_date")
|
2020-01-06 10:04:15 +00:00
|
|
|
|
2023-04-14 04:29:42 +00:00
|
|
|
out = get_basic_details(args, item, overwrite_warehouse)
|
2023-04-02 04:05:27 +00:00
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
get_item_tax_template(args, item, out)
|
2019-01-02 22:48:01 +00:00
|
|
|
out["item_tax_rate"] = get_item_tax_map(
|
|
|
|
args.company,
|
|
|
|
args.get("item_tax_template")
|
|
|
|
if out.get("item_tax_template") is None
|
|
|
|
else out.get("item_tax_template"),
|
|
|
|
as_json=True,
|
|
|
|
)
|
2018-12-26 21:11:07 +00:00
|
|
|
|
2018-08-08 11:07:31 +00:00
|
|
|
get_party_item_code(args, item, out)
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2018-08-08 11:07:31 +00:00
|
|
|
set_valuation_rate(out, args)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2018-05-28 14:37:08 +00:00
|
|
|
update_party_blanket_order(args, out)
|
|
|
|
|
2023-02-13 15:26:05 +00:00
|
|
|
# Never try to find a customer price if customer is set in these Doctype
|
|
|
|
current_customer = args.customer
|
|
|
|
if args.get("doctype") in ["Purchase Order", "Purchase Receipt", "Purchase Invoice"]:
|
|
|
|
args.customer = None
|
|
|
|
|
2021-08-09 05:11:24 +00:00
|
|
|
out.update(get_price_list_rate(args, item))
|
2014-02-10 09:17:54 +00:00
|
|
|
|
2023-02-13 15:26:05 +00:00
|
|
|
args.customer = current_customer
|
|
|
|
|
2016-01-15 11:29:26 +00:00
|
|
|
if args.customer and cint(args.is_pos):
|
2021-05-07 06:41:09 +00:00
|
|
|
out.update(get_pos_profile_item_details(args.company, args, update_data=True))
|
2017-03-14 11:29:11 +00:00
|
|
|
|
2020-05-01 12:46:25 +00:00
|
|
|
if (
|
|
|
|
args.get("doctype") == "Material Request"
|
|
|
|
and args.get("material_request_type") == "Material Transfer"
|
|
|
|
):
|
|
|
|
out.update(get_bin_details(args.item_code, args.get("from_warehouse")))
|
|
|
|
|
|
|
|
elif out.get("warehouse"):
|
2021-10-12 07:59:32 +00:00
|
|
|
if doc and doc.get("doctype") == "Purchase Order":
|
|
|
|
# calculate company_total_stock only for po
|
2022-12-29 07:04:18 +00:00
|
|
|
bin_details = get_bin_details(
|
|
|
|
args.item_code, out.warehouse, args.company, include_child_warehouses=True
|
|
|
|
)
|
2021-10-12 07:59:32 +00:00
|
|
|
else:
|
2022-12-29 07:04:18 +00:00
|
|
|
bin_details = get_bin_details(args.item_code, out.warehouse, include_child_warehouses=True)
|
2021-10-12 07:59:32 +00:00
|
|
|
|
|
|
|
out.update(bin_details)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-05-28 07:19:20 +00:00
|
|
|
# update args with out, if key or value not exists
|
2018-02-15 05:58:55 +00:00
|
|
|
for key, value in out.items():
|
2014-05-28 07:19:20 +00:00
|
|
|
if args.get(key) is None:
|
|
|
|
args[key] = value
|
|
|
|
|
2022-12-08 12:34:40 +00:00
|
|
|
data = get_pricing_rule_for_item(args, doc=doc, for_validate=for_validate)
|
2019-11-19 13:17:48 +00:00
|
|
|
|
2019-03-18 09:04:19 +00:00
|
|
|
out.update(data)
|
2018-08-08 11:07:31 +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)
|
|
|
|
|
2022-04-19 09:27:31 +00:00
|
|
|
if args.get("is_subcontracted"):
|
2018-08-08 11:07:31 +00:00
|
|
|
out.bom = args.get("bom") or get_default_bom(args.item_code)
|
|
|
|
|
|
|
|
get_gross_profit(out)
|
|
|
|
if args.doctype == "Material Request":
|
|
|
|
out.rate = args.rate or out.price_list_rate
|
2021-03-31 09:58:26 +00:00
|
|
|
out.amount = flt(args.qty) * flt(out.rate)
|
2018-08-08 11:07:31 +00:00
|
|
|
|
2022-02-15 05:27:06 +00:00
|
|
|
out = remove_standard_fields(out)
|
2023-04-14 04:29:42 +00:00
|
|
|
return out
|
2018-08-08 11:07:31 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-02-15 05:27:06 +00:00
|
|
|
def remove_standard_fields(details):
|
|
|
|
for key in child_table_fields + default_fields:
|
|
|
|
details.pop(key, None)
|
|
|
|
return details
|
|
|
|
|
|
|
|
|
2018-08-08 11:07:31 +00:00
|
|
|
def set_valuation_rate(out, args):
|
2018-08-08 13:13:04 +00:00
|
|
|
if frappe.db.exists("Product Bundle", args.item_code, cache=True):
|
2018-08-08 11:07:31 +00:00
|
|
|
valuation_rate = 0.0
|
|
|
|
bundled_items = frappe.get_doc("Product Bundle", args.item_code)
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2018-08-08 11:07:31 +00:00
|
|
|
for bundle_item in bundled_items.items:
|
|
|
|
valuation_rate += flt(
|
|
|
|
get_valuation_rate(bundle_item.item_code, args.company, out.get("warehouse")).get(
|
|
|
|
"valuation_rate"
|
|
|
|
)
|
|
|
|
* bundle_item.qty
|
|
|
|
)
|
|
|
|
|
|
|
|
out.update({"valuation_rate": valuation_rate})
|
|
|
|
|
|
|
|
else:
|
|
|
|
out.update(get_valuation_rate(args.item_code, args.company, out.get("warehouse")))
|
2015-11-27 09:07:40 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
def process_args(args):
|
2018-02-13 09:12:40 +00:00
|
|
|
if isinstance(args, str):
|
2014-07-01 12:15:15 +00:00
|
|
|
args = json.loads(args)
|
|
|
|
|
|
|
|
args = frappe._dict(args)
|
|
|
|
|
|
|
|
if not args.get("price_list"):
|
|
|
|
args.price_list = args.get("selling_price_list") or args.get("buying_price_list")
|
|
|
|
|
2022-05-25 04:05:22 +00:00
|
|
|
if not args.item_code and args.barcode:
|
2014-07-01 12:15:15 +00:00
|
|
|
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)
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2016-01-21 10:40:22 +00:00
|
|
|
set_transaction_type(args)
|
2014-07-01 12:15:15 +00:00
|
|
|
return args
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-07-14 11:33:17 +00:00
|
|
|
def process_string_args(args):
|
|
|
|
if isinstance(args, str):
|
|
|
|
args = json.loads(args)
|
|
|
|
return args
|
2017-11-23 07:35:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
def get_item_code(barcode=None, serial_no=None):
|
|
|
|
if barcode:
|
2017-12-10 16:27:09 +00:00
|
|
|
item_code = frappe.db.get_value("Item Barcode", {"barcode": barcode}, fieldname=["parent"])
|
2014-04-16 11:51:25 +00:00
|
|
|
if not item_code:
|
|
|
|
frappe.throw(_("No Item with Barcode {0}").format(barcode))
|
2014-02-10 09:17:54 +00:00
|
|
|
elif serial_no:
|
2014-02-26 07:05:33 +00:00
|
|
|
item_code = frappe.db.get_value("Serial No", serial_no, "item_code")
|
2014-04-16 11:51:25 +00:00
|
|
|
if not item_code:
|
|
|
|
frappe.throw(_("No Item with Serial No {0}").format(serial_no))
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
return item_code
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2017-11-23 07:35:15 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
def validate_item_details(args, item):
|
|
|
|
if not args.company:
|
|
|
|
throw(_("Please specify Company"))
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
from erpnext.stock.doctype.item.item import validate_end_of_life
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-10-29 06:51:41 +00:00
|
|
|
validate_end_of_life(item.name, item.end_of_life, item.disabled)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2023-01-18 17:51:29 +00:00
|
|
|
if cint(item.has_variants):
|
|
|
|
msg = f"Item {item.name} is a template, please select one of its variants"
|
|
|
|
|
|
|
|
throw(_(msg), title=_("Template Item Selected"))
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2017-11-23 07:35:15 +00:00
|
|
|
elif args.transaction_type == "buying" and args.doctype != "Material Request":
|
2022-06-18 10:16:59 +00:00
|
|
|
if args.get("is_subcontracted"):
|
|
|
|
if args.get("is_old_subcontracting_flow"):
|
|
|
|
if item.is_sub_contracted_item != 1:
|
|
|
|
throw(_("Item {0} must be a Sub-contracted Item").format(item.name))
|
|
|
|
else:
|
|
|
|
if item.is_stock_item:
|
|
|
|
throw(_("Item {0} must be a Non-Stock Item").format(item.name))
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2017-11-23 07:35:15 +00:00
|
|
|
|
2019-08-16 02:46:22 +00:00
|
|
|
def get_basic_details(args, item, overwrite_warehouse=True):
|
2017-11-23 07:35:15 +00:00
|
|
|
"""
|
|
|
|
:param args: {
|
|
|
|
"item_code": "",
|
|
|
|
"warehouse": None,
|
|
|
|
"customer": "",
|
|
|
|
"conversion_rate": 1.0,
|
|
|
|
"selling_price_list": None,
|
|
|
|
"price_list_currency": None,
|
2017-12-06 13:47:04 +00:00
|
|
|
"price_list_uom_dependant": None,
|
2017-11-23 07:35:15 +00:00
|
|
|
"plc_conversion_rate": 1.0,
|
|
|
|
"doctype": "",
|
|
|
|
"name": "",
|
|
|
|
"supplier": None,
|
|
|
|
"transaction_date": None,
|
|
|
|
"conversion_rate": 1.0,
|
|
|
|
"buying_price_list": None,
|
2022-04-19 09:27:31 +00:00
|
|
|
"is_subcontracted": 0/1,
|
2017-11-23 07:35:15 +00:00
|
|
|
"ignore_pricing_rule": 0/1
|
|
|
|
"project": "",
|
|
|
|
barcode: "",
|
|
|
|
serial_no: "",
|
|
|
|
currency: "",
|
|
|
|
update_stock: "",
|
|
|
|
price_list: "",
|
|
|
|
company: "",
|
|
|
|
order_type: "",
|
|
|
|
is_pos: "",
|
|
|
|
project: "",
|
|
|
|
qty: "",
|
|
|
|
stock_qty: "",
|
2019-12-04 10:37:11 +00:00
|
|
|
conversion_factor: "",
|
|
|
|
against_blanket_order: 0/1
|
2017-11-23 07:35:15 +00:00
|
|
|
}
|
|
|
|
:param item: `item_code` of Item object
|
|
|
|
:return: frappe._dict
|
|
|
|
"""
|
|
|
|
|
2014-10-03 11:06:43 +00:00
|
|
|
if not item:
|
|
|
|
item = frappe.get_doc("Item", args.get("item_code"))
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-10-07 09:59:58 +00:00
|
|
|
if item.variant_of:
|
|
|
|
item.update_template_tables()
|
|
|
|
|
2018-05-04 11:19:33 +00:00
|
|
|
item_defaults = get_item_defaults(item.name, args.company)
|
2018-07-17 12:31:44 +00:00
|
|
|
item_group_defaults = get_item_group_defaults(item.name, args.company)
|
2018-09-25 09:53:12 +00:00
|
|
|
brand_defaults = get_brand_defaults(item.name, args.company)
|
2018-07-17 12:31:44 +00:00
|
|
|
|
2020-04-03 04:32:56 +00:00
|
|
|
defaults = frappe._dict(
|
|
|
|
{
|
|
|
|
"item_defaults": item_defaults,
|
|
|
|
"item_group_defaults": item_group_defaults,
|
|
|
|
"brand_defaults": brand_defaults,
|
|
|
|
}
|
|
|
|
)
|
2020-04-06 16:43:13 +00:00
|
|
|
|
2020-04-03 04:32:56 +00:00
|
|
|
warehouse = get_item_warehouse(item, args, overwrite_warehouse, defaults)
|
2014-02-12 13:39:28 +00:00
|
|
|
|
2018-05-18 11:00:14 +00:00
|
|
|
if args.get("doctype") == "Material Request" and not args.get("material_request_type"):
|
|
|
|
args["material_request_type"] = frappe.db.get_value(
|
2018-08-08 11:07:31 +00:00
|
|
|
"Material Request", args.get("name"), "material_request_type", cache=True
|
|
|
|
)
|
2017-11-21 10:47:22 +00:00
|
|
|
|
2019-11-28 13:22:16 +00:00
|
|
|
expense_account = None
|
|
|
|
|
|
|
|
if args.get("doctype") == "Purchase Invoice" and item.is_fixed_asset:
|
|
|
|
from erpnext.assets.doctype.asset_category.asset_category import get_asset_category_account
|
|
|
|
|
|
|
|
expense_account = get_asset_category_account(
|
|
|
|
fieldname="fixed_asset_account", item=args.item_code, company=args.company
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2017-09-29 12:44:10 +00:00
|
|
|
# Set the UOM to the Default Sales UOM or Default Purchase UOM if configured in the Item Master
|
2020-04-03 04:32:56 +00:00
|
|
|
if not args.get("uom"):
|
2019-02-14 13:00:10 +00:00
|
|
|
if args.get("doctype") in sales_doctypes:
|
2017-09-29 12:44:10 +00:00
|
|
|
args.uom = item.sales_uom if item.sales_uom else item.stock_uom
|
2017-11-21 10:47:22 +00:00
|
|
|
elif (args.get("doctype") in ["Purchase Order", "Purchase Receipt", "Purchase Invoice"]) or (
|
2018-05-18 11:00:14 +00:00
|
|
|
args.get("doctype") == "Material Request" and args.get("material_request_type") == "Purchase"
|
|
|
|
):
|
2017-09-29 12:44:10 +00:00
|
|
|
args.uom = item.purchase_uom if item.purchase_uom else item.stock_uom
|
|
|
|
else:
|
|
|
|
args.uom = item.stock_uom
|
|
|
|
|
2022-11-09 13:26:09 +00:00
|
|
|
# Set stock UOM in args, so that it can be used while fetching item price
|
|
|
|
args.stock_uom = item.stock_uom
|
|
|
|
|
2021-08-23 08:57:55 +00:00
|
|
|
if args.get("batch_no") and item.name != frappe.get_cached_value(
|
|
|
|
"Batch", args.get("batch_no"), "item"
|
|
|
|
):
|
|
|
|
args["batch_no"] = ""
|
2022-03-28 13:22:46 +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,
|
2015-02-10 11:19:16 +00:00
|
|
|
"description": cstr(item.description).strip(),
|
|
|
|
"image": cstr(item.image).strip(),
|
2016-02-25 13:29:20 +00:00
|
|
|
"warehouse": warehouse,
|
2018-09-25 09:53:12 +00:00
|
|
|
"income_account": get_default_income_account(
|
|
|
|
args, item_defaults, item_group_defaults, brand_defaults
|
|
|
|
),
|
2019-11-28 13:22:16 +00:00
|
|
|
"expense_account": expense_account
|
|
|
|
or get_default_expense_account(args, item_defaults, item_group_defaults, brand_defaults),
|
2021-11-29 09:35:06 +00:00
|
|
|
"discount_account": get_default_discount_account(args, item_defaults),
|
2022-04-11 09:05:22 +00:00
|
|
|
"provisional_expense_account": get_provisional_account(args, item_defaults),
|
2018-09-25 09:53:12 +00:00
|
|
|
"cost_center": get_default_cost_center(
|
|
|
|
args, item_defaults, item_group_defaults, brand_defaults
|
|
|
|
),
|
2017-04-21 07:10:19 +00:00
|
|
|
"has_serial_no": item.has_serial_no,
|
|
|
|
"has_batch_no": item.has_batch_no,
|
2020-04-06 16:43:13 +00:00
|
|
|
"batch_no": args.get("batch_no"),
|
2017-09-29 12:44:10 +00:00
|
|
|
"uom": args.uom,
|
2022-05-31 14:18:30 +00:00
|
|
|
"stock_uom": item.stock_uom,
|
2016-01-15 11:29:26 +00:00
|
|
|
"min_order_qty": flt(item.min_order_qty) if args.doctype == "Material Request" else "",
|
2019-01-11 10:01:33 +00:00
|
|
|
"qty": flt(args.qty) or 1.0,
|
|
|
|
"stock_qty": flt(args.qty) or 1.0,
|
2014-02-12 09:23:18 +00:00
|
|
|
"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,
|
2015-02-23 11:28:30 +00:00
|
|
|
"net_rate": 0.0,
|
|
|
|
"net_amount": 0.0,
|
2015-10-19 08:47:52 +00:00
|
|
|
"discount_percentage": 0.0,
|
2022-05-31 14:18:30 +00:00
|
|
|
"discount_amount": flt(args.discount_amount) or 0.0,
|
2018-09-25 09:53:12 +00:00
|
|
|
"supplier": get_default_supplier(args, item_defaults, item_group_defaults, brand_defaults),
|
2017-04-03 08:47:08 +00:00
|
|
|
"update_stock": args.get("update_stock")
|
|
|
|
if args.get("doctype") in ["Sales Invoice", "Purchase Invoice"]
|
|
|
|
else 0,
|
2016-12-12 07:31:43 +00:00
|
|
|
"delivered_by_supplier": item.delivered_by_supplier
|
|
|
|
if args.get("doctype") in ["Sales Order", "Sales Invoice"]
|
|
|
|
else 0,
|
2017-11-29 10:39:59 +00:00
|
|
|
"is_fixed_asset": item.is_fixed_asset,
|
2018-08-01 12:14:34 +00:00
|
|
|
"last_purchase_rate": item.last_purchase_rate
|
|
|
|
if args.get("doctype") in ["Purchase Order"]
|
|
|
|
else 0,
|
2019-12-04 10:37:11 +00:00
|
|
|
"transaction_date": args.get("transaction_date"),
|
2020-05-19 14:21:45 +00:00
|
|
|
"against_blanket_order": args.get("against_blanket_order"),
|
2021-03-22 18:06:48 +00:00
|
|
|
"bom_no": item.get("default_bom"),
|
2021-09-03 16:30:14 +00:00
|
|
|
"weight_per_unit": args.get("weight_per_unit") or item.get("weight_per_unit"),
|
2021-11-30 12:24:18 +00:00
|
|
|
"weight_uom": args.get("weight_uom") or item.get("weight_uom"),
|
|
|
|
"grant_commission": item.get("grant_commission"),
|
2014-02-12 09:23:18 +00:00
|
|
|
}
|
|
|
|
)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2019-01-15 08:46:32 +00:00
|
|
|
if item.get("enable_deferred_revenue") or item.get("enable_deferred_expense"):
|
2018-08-29 06:09:08 +00:00
|
|
|
out.update(calculate_service_end_date(args, item))
|
2018-05-14 10:46:46 +00:00
|
|
|
|
2017-04-03 08:47:08 +00:00
|
|
|
# calculate conversion factor
|
2017-04-10 13:45:57 +00:00
|
|
|
if item.stock_uom == args.uom:
|
|
|
|
out.conversion_factor = 1.0
|
|
|
|
else:
|
2019-02-14 13:00:10 +00:00
|
|
|
out.conversion_factor = args.conversion_factor or get_conversion_factor(item.name, args.uom).get(
|
|
|
|
"conversion_factor"
|
|
|
|
)
|
2017-04-10 13:45:57 +00:00
|
|
|
|
|
|
|
args.conversion_factor = out.conversion_factor
|
|
|
|
out.stock_qty = out.qty * out.conversion_factor
|
2022-02-14 06:03:34 +00:00
|
|
|
args.stock_qty = out.stock_qty
|
2017-04-03 08:47:08 +00:00
|
|
|
|
2018-01-08 09:28:20 +00:00
|
|
|
# calculate last purchase rate
|
2023-01-09 13:19:48 +00:00
|
|
|
if args.get("doctype") in purchase_doctypes and not frappe.db.get_single_value(
|
|
|
|
"Buying Settings", "disable_last_purchase_rate"
|
|
|
|
):
|
2019-02-14 13:00:10 +00:00
|
|
|
from erpnext.buying.doctype.purchase_order.purchase_order import item_last_purchase_rate
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-02-14 13:00:10 +00:00
|
|
|
out.last_purchase_rate = item_last_purchase_rate(
|
|
|
|
args.name, args.conversion_rate, item.name, out.conversion_factor
|
|
|
|
)
|
2018-01-08 09:28:20 +00:00
|
|
|
|
2015-02-17 05:03:58 +00:00
|
|
|
# if default specified in item is for another company, fetch from company
|
2017-04-20 09:51:01 +00:00
|
|
|
for d in [
|
|
|
|
["Account", "income_account", "default_income_account"],
|
2016-04-22 11:52:22 +00:00
|
|
|
["Account", "expense_account", "default_expense_account"],
|
2017-04-20 09:51:01 +00:00
|
|
|
["Cost Center", "cost_center", "cost_center"],
|
|
|
|
["Warehouse", "warehouse", ""],
|
|
|
|
]:
|
2018-05-26 03:39:02 +00:00
|
|
|
if not out[d[1]]:
|
2018-08-08 11:07:31 +00:00
|
|
|
out[d[1]] = frappe.get_cached_value("Company", args.company, d[2]) if d[2] else None
|
2015-02-17 05:03:58 +00:00
|
|
|
|
2022-03-09 10:07:14 +00:00
|
|
|
for fieldname in ("item_name", "item_group", "brand", "stock_uom"):
|
2014-03-28 08:25:00 +00:00
|
|
|
out[fieldname] = item.get(fieldname)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2019-06-02 10:33:05 +00:00
|
|
|
if args.get("manufacturer"):
|
|
|
|
part_no = get_item_manufacturer_part_no(args.get("item_code"), args.get("manufacturer"))
|
|
|
|
if part_no:
|
|
|
|
out["manufacturer_part_no"] = part_no
|
|
|
|
else:
|
|
|
|
out["manufacturer_part_no"] = None
|
|
|
|
out["manufacturer"] = None
|
2020-04-07 15:30:57 +00:00
|
|
|
else:
|
2020-05-01 05:20:17 +00:00
|
|
|
data = frappe.get_value(
|
|
|
|
"Item", item.name, ["default_item_manufacturer", "default_manufacturer_part_no"], as_dict=1
|
|
|
|
)
|
|
|
|
|
|
|
|
if data:
|
|
|
|
out.update(
|
|
|
|
{
|
|
|
|
"manufacturer": data.default_item_manufacturer,
|
|
|
|
"manufacturer_part_no": data.default_manufacturer_part_no,
|
|
|
|
}
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
child_doctype = args.doctype + " Item"
|
|
|
|
meta = frappe.get_meta(child_doctype)
|
|
|
|
if meta.get_field("barcode"):
|
|
|
|
update_barcode_value(out)
|
|
|
|
|
2021-03-22 18:06:48 +00:00
|
|
|
if out.get("weight_per_unit"):
|
|
|
|
out["total_weight"] = out.weight_per_unit * out.stock_qty
|
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
return out
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-10-06 16:58:48 +00:00
|
|
|
def get_item_warehouse(item, args, overwrite_warehouse, defaults=None):
|
2020-04-03 04:32:56 +00:00
|
|
|
if not defaults:
|
|
|
|
defaults = frappe._dict(
|
|
|
|
{
|
|
|
|
"item_defaults": get_item_defaults(item.name, args.company),
|
|
|
|
"item_group_defaults": get_item_group_defaults(item.name, args.company),
|
|
|
|
"brand_defaults": get_brand_defaults(item.name, args.company),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if overwrite_warehouse or not args.warehouse:
|
|
|
|
warehouse = (
|
|
|
|
args.get("set_warehouse")
|
|
|
|
or defaults.item_defaults.get("default_warehouse")
|
|
|
|
or defaults.item_group_defaults.get("default_warehouse")
|
|
|
|
or defaults.brand_defaults.get("default_warehouse")
|
|
|
|
or args.get("warehouse")
|
|
|
|
)
|
|
|
|
|
|
|
|
if not warehouse:
|
|
|
|
defaults = frappe.defaults.get_defaults() or {}
|
|
|
|
warehouse_exists = frappe.db.exists(
|
|
|
|
"Warehouse", {"name": defaults.default_warehouse, "company": args.company}
|
|
|
|
)
|
|
|
|
if defaults.get("default_warehouse") and warehouse_exists:
|
|
|
|
warehouse = defaults.default_warehouse
|
|
|
|
|
|
|
|
else:
|
|
|
|
warehouse = args.get("warehouse")
|
2020-04-06 16:43:13 +00:00
|
|
|
|
2020-10-22 11:34:31 +00:00
|
|
|
if not warehouse:
|
|
|
|
default_warehouse = frappe.db.get_single_value("Stock Settings", "default_warehouse")
|
|
|
|
if frappe.db.get_value("Warehouse", default_warehouse, "company") == args.company:
|
|
|
|
return default_warehouse
|
|
|
|
|
2020-04-03 04:32:56 +00:00
|
|
|
return warehouse
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
def update_barcode_value(out):
|
|
|
|
barcode_data = get_barcode_data([out])
|
|
|
|
|
|
|
|
# If item has one barcode then update the value of the barcode field
|
|
|
|
if barcode_data and len(barcode_data.get(out.item_code)) == 1:
|
|
|
|
out["barcode"] = barcode_data.get(out.item_code)[0]
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
|
2020-07-23 13:21:26 +00:00
|
|
|
def get_barcode_data(items_list):
|
|
|
|
# get itemwise batch no data
|
|
|
|
# exmaple: {'LED-GRE': [Batch001, Batch002]}
|
|
|
|
# where LED-GRE is item code, SN0001 is serial no and Pune is warehouse
|
|
|
|
|
|
|
|
itemwise_barcode = {}
|
|
|
|
for item in items_list:
|
2023-02-18 06:48:41 +00:00
|
|
|
barcodes = frappe.db.get_all(
|
|
|
|
"Item Barcode", filters={"parent": item.item_code}, fields="barcode"
|
2020-07-23 13:21:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for barcode in barcodes:
|
|
|
|
if item.item_code not in itemwise_barcode:
|
|
|
|
itemwise_barcode.setdefault(item.item_code, [])
|
|
|
|
itemwise_barcode[item.item_code].append(barcode.get("barcode"))
|
|
|
|
|
|
|
|
return itemwise_barcode
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
@frappe.whitelist()
|
2021-06-24 13:47:58 +00:00
|
|
|
def get_item_tax_info(company, tax_category, item_codes, item_rates=None, item_tax_templates=None):
|
2018-12-26 21:11:07 +00:00
|
|
|
out = {}
|
2021-06-25 08:04:00 +00:00
|
|
|
|
2021-06-25 08:08:06 +00:00
|
|
|
if item_tax_templates is None:
|
2021-06-25 08:04:00 +00:00
|
|
|
item_tax_templates = {}
|
2021-07-13 06:15:41 +00:00
|
|
|
|
2021-06-25 08:08:06 +00:00
|
|
|
if item_rates is None:
|
|
|
|
item_rates = {}
|
2021-06-25 08:04:00 +00:00
|
|
|
|
2021-06-24 13:47:58 +00:00
|
|
|
if isinstance(item_codes, (str,)):
|
2018-12-26 21:11:07 +00:00
|
|
|
item_codes = json.loads(item_codes)
|
|
|
|
|
2021-06-24 13:47:58 +00:00
|
|
|
if isinstance(item_rates, (str,)):
|
2021-06-04 17:23:26 +00:00
|
|
|
item_rates = json.loads(item_rates)
|
|
|
|
|
2021-06-24 13:47:58 +00:00
|
|
|
if isinstance(item_tax_templates, (str,)):
|
2021-06-23 15:26:27 +00:00
|
|
|
item_tax_templates = json.loads(item_tax_templates)
|
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
for item_code in item_codes:
|
2021-06-23 15:26:27 +00:00
|
|
|
if not item_code or item_code[1] in out or not item_tax_templates.get(item_code[1]):
|
2018-12-26 21:11:07 +00:00
|
|
|
continue
|
2021-06-23 15:26:27 +00:00
|
|
|
|
2021-06-04 17:23:26 +00:00
|
|
|
out[item_code[1]] = {}
|
|
|
|
item = frappe.get_cached_doc("Item", item_code[0])
|
2021-06-25 08:08:06 +00:00
|
|
|
args = {
|
|
|
|
"company": company,
|
|
|
|
"tax_category": tax_category,
|
|
|
|
"net_rate": item_rates.get(item_code[1]),
|
|
|
|
}
|
2021-06-23 17:22:51 +00:00
|
|
|
|
|
|
|
if item_tax_templates:
|
|
|
|
args.update({"item_tax_template": item_tax_templates.get(item_code[1])})
|
2021-06-23 15:26:27 +00:00
|
|
|
|
2021-06-04 17:23:26 +00:00
|
|
|
get_item_tax_template(args, item, out[item_code[1]])
|
|
|
|
out[item_code[1]]["item_tax_rate"] = get_item_tax_map(
|
|
|
|
company, out[item_code[1]].get("item_tax_template"), as_json=True
|
|
|
|
)
|
2018-12-26 21:11:07 +00:00
|
|
|
|
|
|
|
return out
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
def get_item_tax_template(args, item, out):
|
|
|
|
"""
|
|
|
|
args = {
|
|
|
|
"tax_category": None
|
|
|
|
"item_tax_template": None
|
|
|
|
}
|
|
|
|
"""
|
2021-10-12 07:59:32 +00:00
|
|
|
item_tax_template = None
|
|
|
|
if item.taxes:
|
|
|
|
item_tax_template = _get_item_tax_template(args, item.taxes, out)
|
2018-12-26 21:11:07 +00:00
|
|
|
|
|
|
|
if not item_tax_template:
|
|
|
|
item_group = item.item_group
|
|
|
|
while item_group and not item_tax_template:
|
|
|
|
item_group_doc = frappe.get_cached_doc("Item Group", item_group)
|
|
|
|
item_tax_template = _get_item_tax_template(args, item_group_doc.taxes, out)
|
|
|
|
item_group = item_group_doc.parent_item_group
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-05-03 18:04:34 +00:00
|
|
|
def _get_item_tax_template(args, taxes, out=None, for_validate=False):
|
|
|
|
if out is None:
|
|
|
|
out = {}
|
2020-01-06 10:04:15 +00:00
|
|
|
taxes_with_validity = []
|
|
|
|
taxes_with_no_validity = []
|
|
|
|
|
|
|
|
for tax in taxes:
|
2021-10-12 07:59:32 +00:00
|
|
|
tax_company = frappe.get_cached_value("Item Tax Template", tax.item_tax_template, "company")
|
|
|
|
if tax_company == args["company"]:
|
|
|
|
if tax.valid_from or tax.maximum_net_rate:
|
|
|
|
# In purchase Invoice first preference will be given to supplier invoice date
|
|
|
|
# if supplier date is not present then posting date
|
2022-06-16 17:03:47 +00:00
|
|
|
validation_date = args.get("bill_date") or args.get("transaction_date")
|
2021-10-12 07:59:32 +00:00
|
|
|
|
|
|
|
if getdate(tax.valid_from) <= getdate(validation_date) and is_within_valid_range(args, tax):
|
|
|
|
taxes_with_validity.append(tax)
|
|
|
|
else:
|
2020-06-18 06:42:53 +00:00
|
|
|
taxes_with_no_validity.append(tax)
|
2020-01-06 10:04:15 +00:00
|
|
|
|
|
|
|
if taxes_with_validity:
|
2023-04-09 04:08:13 +00:00
|
|
|
taxes = sorted(
|
|
|
|
taxes_with_validity, key=lambda i: i.valid_from or tax.maximum_net_rate, reverse=True
|
|
|
|
)
|
2020-01-06 10:04:15 +00:00
|
|
|
else:
|
|
|
|
taxes = taxes_with_no_validity
|
|
|
|
|
|
|
|
if for_validate:
|
|
|
|
return [
|
|
|
|
tax.item_tax_template
|
|
|
|
for tax in taxes
|
|
|
|
if (
|
|
|
|
cstr(tax.tax_category) == cstr(args.get("tax_category"))
|
|
|
|
and (tax.item_tax_template not in taxes)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-01-06 10:04:15 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# all templates have validity and no template is valid
|
|
|
|
if not taxes_with_validity and (not taxes_with_no_validity):
|
|
|
|
return None
|
|
|
|
|
2021-06-04 17:23:26 +00:00
|
|
|
# do not change if already a valid template
|
2021-06-24 13:47:58 +00:00
|
|
|
if args.get("item_tax_template") in {t.item_tax_template for t in taxes}:
|
2021-06-23 15:26:27 +00:00
|
|
|
out["item_tax_template"] = args.get("item_tax_template")
|
2021-06-05 07:51:03 +00:00
|
|
|
return args.get("item_tax_template")
|
2021-06-04 17:23:26 +00:00
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
for tax in taxes:
|
|
|
|
if cstr(tax.tax_category) == cstr(args.get("tax_category")):
|
|
|
|
out["item_tax_template"] = tax.item_tax_template
|
|
|
|
return tax.item_tax_template
|
|
|
|
return None
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-06-04 17:23:26 +00:00
|
|
|
def is_within_valid_range(args, tax):
|
|
|
|
if not flt(tax.maximum_net_rate):
|
|
|
|
# No range specified, just ignore
|
|
|
|
return True
|
|
|
|
elif flt(tax.minimum_net_rate) <= flt(args.get("net_rate")) <= flt(tax.maximum_net_rate):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-12-26 21:11:07 +00:00
|
|
|
@frappe.whitelist()
|
2018-12-28 21:25:00 +00:00
|
|
|
def get_item_tax_map(company, item_tax_template, as_json=True):
|
2018-12-26 21:11:07 +00:00
|
|
|
item_tax_map = {}
|
|
|
|
if item_tax_template:
|
|
|
|
template = frappe.get_cached_doc("Item Tax Template", item_tax_template)
|
|
|
|
for d in template.taxes:
|
2018-12-28 21:25:00 +00:00
|
|
|
if frappe.get_cached_value("Account", d.tax_type, "company") == company:
|
|
|
|
item_tax_map[d.tax_type] = d.tax_rate
|
2018-12-26 21:11:07 +00:00
|
|
|
|
|
|
|
return json.dumps(item_tax_map) if as_json else item_tax_map
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-08-29 06:09:08 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def calculate_service_end_date(args, item=None):
|
|
|
|
args = process_args(args)
|
|
|
|
if not item:
|
|
|
|
item = frappe.get_cached_doc("Item", args.item_code)
|
|
|
|
|
2019-01-15 08:46:32 +00:00
|
|
|
doctype = args.get("parenttype") or args.get("doctype")
|
2019-01-15 09:06:11 +00:00
|
|
|
if doctype == "Sales Invoice":
|
|
|
|
enable_deferred = "enable_deferred_revenue"
|
|
|
|
no_of_months = "no_of_months"
|
|
|
|
account = "deferred_revenue_account"
|
|
|
|
else:
|
|
|
|
enable_deferred = "enable_deferred_expense"
|
|
|
|
no_of_months = "no_of_months_exp"
|
|
|
|
account = "deferred_expense_account"
|
2018-09-21 10:15:40 +00:00
|
|
|
|
2018-08-29 06:09:08 +00:00
|
|
|
service_start_date = args.service_start_date if args.service_start_date else args.transaction_date
|
2018-09-21 10:15:40 +00:00
|
|
|
service_end_date = add_months(service_start_date, item.get(no_of_months))
|
2018-08-29 06:09:08 +00:00
|
|
|
deferred_detail = {"service_start_date": service_start_date, "service_end_date": service_end_date}
|
2018-09-21 10:15:40 +00:00
|
|
|
deferred_detail[enable_deferred] = item.get(enable_deferred)
|
|
|
|
deferred_detail[account] = get_default_deferred_account(args, item, fieldname=account)
|
2018-08-29 06:09:08 +00:00
|
|
|
|
|
|
|
return deferred_detail
|
2017-11-23 07:35:15 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-09-25 09:53:12 +00:00
|
|
|
def get_default_income_account(args, item, item_group, brand):
|
2018-05-16 11:25:23 +00:00
|
|
|
return (
|
|
|
|
item.get("income_account")
|
2018-07-17 12:31:44 +00:00
|
|
|
or item_group.get("income_account")
|
2018-09-25 09:53:12 +00:00
|
|
|
or brand.get("income_account")
|
2018-07-17 12:31:44 +00:00
|
|
|
or args.income_account
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-10-03 11:06:43 +00:00
|
|
|
|
2018-09-25 09:53:12 +00:00
|
|
|
def get_default_expense_account(args, item, item_group, brand):
|
2018-05-16 11:25:23 +00:00
|
|
|
return (
|
|
|
|
item.get("expense_account")
|
2018-07-17 12:31:44 +00:00
|
|
|
or item_group.get("expense_account")
|
2018-09-25 09:53:12 +00:00
|
|
|
or brand.get("expense_account")
|
2018-07-17 12:31:44 +00:00
|
|
|
or args.expense_account
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-10-03 11:06:43 +00:00
|
|
|
|
2022-04-11 09:05:22 +00:00
|
|
|
def get_provisional_account(args, item):
|
|
|
|
return item.get("default_provisional_account") or args.default_provisional_account
|
|
|
|
|
|
|
|
|
2021-07-12 20:07:30 +00:00
|
|
|
def get_default_discount_account(args, item):
|
|
|
|
return item.get("default_discount_account") or args.discount_account
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-07-06 11:21:12 +00:00
|
|
|
|
2018-09-21 10:15:40 +00:00
|
|
|
def get_default_deferred_account(args, item, fieldname=None):
|
2019-01-15 08:46:32 +00:00
|
|
|
if item.get("enable_deferred_revenue") or item.get("enable_deferred_expense"):
|
2018-09-21 10:15:40 +00:00
|
|
|
return (
|
|
|
|
item.get(fieldname)
|
|
|
|
or args.get(fieldname)
|
|
|
|
or frappe.get_cached_value("Company", args.company, "default_" + fieldname)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2018-05-14 10:46:46 +00:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-11-15 04:10:44 +00:00
|
|
|
def get_default_cost_center(args, item=None, item_group=None, brand=None, company=None):
|
2018-08-08 13:13:04 +00:00
|
|
|
cost_center = None
|
2020-11-15 04:10:44 +00:00
|
|
|
|
|
|
|
if not company and args.get("company"):
|
|
|
|
company = args.get("company")
|
|
|
|
|
2018-08-08 13:13:04 +00:00
|
|
|
if args.get("project"):
|
|
|
|
cost_center = frappe.db.get_value("Project", args.get("project"), "cost_center", cache=True)
|
2018-08-30 13:20:48 +00:00
|
|
|
|
2020-11-15 04:10:44 +00:00
|
|
|
if not cost_center and (item and item_group and brand):
|
2018-08-08 13:13:04 +00:00
|
|
|
if args.get("customer"):
|
2018-09-25 09:53:12 +00:00
|
|
|
cost_center = (
|
|
|
|
item.get("selling_cost_center")
|
|
|
|
or item_group.get("selling_cost_center")
|
|
|
|
or brand.get("selling_cost_center")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2018-08-08 13:13:04 +00:00
|
|
|
else:
|
2018-09-25 09:53:12 +00:00
|
|
|
cost_center = (
|
|
|
|
item.get("buying_cost_center")
|
|
|
|
or item_group.get("buying_cost_center")
|
|
|
|
or brand.get("buying_cost_center")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2018-08-30 13:20:48 +00:00
|
|
|
|
2020-11-15 04:10:44 +00:00
|
|
|
elif not cost_center and args.get("item_code") and company:
|
|
|
|
for method in ["get_item_defaults", "get_item_group_defaults", "get_brand_defaults"]:
|
|
|
|
path = "erpnext.stock.get_item_details.{0}".format(method)
|
|
|
|
data = frappe.get_attr(path)(args.get("item_code"), company)
|
|
|
|
|
|
|
|
if data and (data.selling_cost_center or data.buying_cost_center):
|
|
|
|
return data.selling_cost_center or data.buying_cost_center
|
|
|
|
|
|
|
|
if not cost_center and args.get("cost_center"):
|
|
|
|
cost_center = args.get("cost_center")
|
2019-05-15 07:12:37 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
company
|
|
|
|
and cost_center
|
|
|
|
and frappe.get_cached_value("Cost Center", cost_center, "company") != company
|
|
|
|
):
|
|
|
|
return None
|
|
|
|
|
2020-11-15 04:10:44 +00:00
|
|
|
if not cost_center and company:
|
|
|
|
cost_center = frappe.get_cached_value("Company", company, "cost_center")
|
|
|
|
|
2019-05-15 07:12:37 +00:00
|
|
|
return cost_center
|
2014-10-03 11:06:43 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-09-25 09:53:12 +00:00
|
|
|
def get_default_supplier(args, item, item_group, brand):
|
2018-07-17 12:31:44 +00:00
|
|
|
return (
|
|
|
|
item.get("default_supplier")
|
2018-09-25 09:53:12 +00:00
|
|
|
or item_group.get("default_supplier")
|
|
|
|
or brand.get("default_supplier")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2018-07-17 12:31:44 +00:00
|
|
|
|
2021-08-09 05:11:24 +00:00
|
|
|
def get_price_list_rate(args, item_doc, out=None):
|
|
|
|
if out is None:
|
|
|
|
out = frappe._dict()
|
|
|
|
|
2016-02-15 06:12:18 +00:00
|
|
|
meta = frappe.get_meta(args.parenttype or args.doctype)
|
2014-02-10 09:17:54 +00:00
|
|
|
|
2018-05-15 16:42:44 +00:00
|
|
|
if meta.get_field("currency") or args.get("currency"):
|
2021-04-19 05:35:21 +00:00
|
|
|
if not args.get("price_list_currency") or not args.get("plc_conversion_rate"):
|
|
|
|
# if currency and plc_conversion_rate exist then
|
|
|
|
# `get_price_list_currency_and_exchange_rate` has already been called
|
|
|
|
pl_details = get_price_list_currency_and_exchange_rate(args)
|
|
|
|
args.update(pl_details)
|
|
|
|
|
2019-05-16 13:47:02 +00:00
|
|
|
if meta.get_field("currency"):
|
2017-07-25 08:35:01 +00:00
|
|
|
validate_conversion_rate(args, meta)
|
2014-02-10 09:17:54 +00:00
|
|
|
|
2021-08-09 05:11:24 +00:00
|
|
|
price_list_rate = get_price_list_rate_for(args, item_doc.name)
|
2015-09-03 04:59:38 +00:00
|
|
|
|
2019-01-08 17:34:08 +00:00
|
|
|
# variant
|
2021-08-09 05:11:24 +00:00
|
|
|
if price_list_rate is None and item_doc.variant_of:
|
2019-01-08 17:34:08 +00:00
|
|
|
price_list_rate = get_price_list_rate_for(args, item_doc.variant_of)
|
2015-09-03 04:59:38 +00:00
|
|
|
|
2016-01-15 11:29:26 +00:00
|
|
|
# insert in database
|
2021-08-09 05:11:24 +00:00
|
|
|
if price_list_rate is None:
|
2015-08-03 07:48:54 +00:00
|
|
|
if args.price_list and args.rate:
|
|
|
|
insert_item_price(args)
|
2021-08-09 05:11:24 +00:00
|
|
|
return out
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
out.price_list_rate = (
|
|
|
|
flt(price_list_rate) * flt(args.plc_conversion_rate) / flt(args.conversion_rate)
|
|
|
|
)
|
2017-04-10 13:45:57 +00:00
|
|
|
|
2023-01-09 09:42:46 +00:00
|
|
|
if frappe.db.get_single_value("Buying Settings", "disable_last_purchase_rate"):
|
|
|
|
return out
|
|
|
|
|
2016-01-19 10:15:49 +00:00
|
|
|
if not out.price_list_rate and args.transaction_type == "buying":
|
2014-02-10 09:17:54 +00:00
|
|
|
from erpnext.stock.doctype.item.item import get_last_purchase_details
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-04-11 11:21:27 +00:00
|
|
|
out.update(get_last_purchase_details(item_doc.name, args.name, args.conversion_rate))
|
|
|
|
|
2021-08-09 05:11:24 +00:00
|
|
|
return out
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-08-03 07:48:54 +00:00
|
|
|
def insert_item_price(args):
|
|
|
|
"""Insert Item Price if Price List and Price List Rate are specified and currency is the same"""
|
2018-08-08 11:07:31 +00:00
|
|
|
if frappe.db.get_value(
|
|
|
|
"Price List", args.price_list, "currency", cache=True
|
|
|
|
) == args.currency and cint(
|
2015-08-03 07:48:54 +00:00
|
|
|
frappe.db.get_single_value("Stock Settings", "auto_insert_price_list_rate_if_missing")
|
|
|
|
):
|
|
|
|
if frappe.has_permission("Item Price", "write"):
|
2018-08-01 12:14:34 +00:00
|
|
|
price_list_rate = (
|
2022-12-15 11:04:06 +00:00
|
|
|
(flt(args.rate) + flt(args.discount_amount)) / args.get("conversion_factor")
|
2022-05-31 14:18:30 +00:00
|
|
|
if args.get("conversion_factor")
|
2022-12-15 11:04:06 +00:00
|
|
|
else (flt(args.rate) + flt(args.discount_amount))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2018-09-07 07:47:11 +00:00
|
|
|
item_price = frappe.db.get_value(
|
|
|
|
"Item Price",
|
|
|
|
{"item_code": args.item_code, "price_list": args.price_list, "currency": args.currency},
|
|
|
|
["name", "price_list_rate"],
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
if item_price and item_price.name:
|
2021-11-07 12:12:51 +00:00
|
|
|
if item_price.price_list_rate != price_list_rate and frappe.db.get_single_value(
|
|
|
|
"Stock Settings", "update_existing_price_list_rate"
|
|
|
|
):
|
2018-09-07 07:47:11 +00:00
|
|
|
frappe.db.set_value("Item Price", item_price.name, "price_list_rate", price_list_rate)
|
|
|
|
frappe.msgprint(
|
|
|
|
_("Item Price updated for {0} in Price List {1}").format(args.item_code, args.price_list),
|
|
|
|
alert=True,
|
|
|
|
)
|
2016-12-30 09:36:54 +00:00
|
|
|
else:
|
2018-08-08 11:07:31 +00:00
|
|
|
item_price = frappe.get_doc(
|
|
|
|
{
|
|
|
|
"doctype": "Item Price",
|
|
|
|
"price_list": args.price_list,
|
|
|
|
"item_code": args.item_code,
|
|
|
|
"currency": args.currency,
|
|
|
|
"price_list_rate": price_list_rate,
|
2022-05-31 14:18:30 +00:00
|
|
|
"uom": args.stock_uom,
|
2018-08-08 11:07:31 +00:00
|
|
|
}
|
|
|
|
)
|
2016-06-24 11:41:15 +00:00
|
|
|
item_price.insert()
|
|
|
|
frappe.msgprint(
|
|
|
|
_("Item Price added for {0} in Price List {1}").format(args.item_code, args.price_list),
|
2018-08-30 13:20:48 +00:00
|
|
|
alert=True,
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-08-03 07:48:54 +00:00
|
|
|
|
2019-01-25 10:55:15 +00:00
|
|
|
def get_item_price(args, item_code, ignore_party=False):
|
2018-08-01 12:14:34 +00:00
|
|
|
"""
|
|
|
|
Get name, price_list_rate from Item Price based on conditions
|
2019-04-29 17:48:47 +00:00
|
|
|
Check if the desired qty is within the increment of the packing list.
|
2018-08-01 12:14:34 +00:00
|
|
|
:param args: dict (or frappe._dict) with mandatory fields price_list, uom
|
2020-01-20 10:15:04 +00:00
|
|
|
optional fields transaction_date, customer, supplier
|
2018-08-01 12:14:34 +00:00
|
|
|
:param item_code: str, Item Doctype field item_code
|
|
|
|
"""
|
|
|
|
|
2023-02-18 06:48:41 +00:00
|
|
|
ip = frappe.qb.DocType("Item Price")
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(ip)
|
|
|
|
.select(ip.name, ip.price_list_rate, ip.uom)
|
|
|
|
.where(
|
|
|
|
(ip.item_code == item_code)
|
|
|
|
& (ip.price_list == args.get("price_list"))
|
|
|
|
& (IfNull(ip.uom, "").isin(["", args.get("uom")]))
|
|
|
|
& (IfNull(ip.batch_no, "").isin(["", args.get("batch_no")]))
|
|
|
|
)
|
|
|
|
.orderby(ip.valid_from, order=frappe.qb.desc)
|
|
|
|
.orderby(IfNull(ip.batch_no, ""), order=frappe.qb.desc)
|
|
|
|
.orderby(ip.uom, order=frappe.qb.desc)
|
|
|
|
)
|
2021-01-28 06:35:57 +00:00
|
|
|
|
2019-01-25 10:55:15 +00:00
|
|
|
if not ignore_party:
|
|
|
|
if args.get("customer"):
|
2023-02-18 06:48:41 +00:00
|
|
|
query = query.where(ip.customer == args.get("customer"))
|
2019-01-29 08:32:08 +00:00
|
|
|
elif args.get("supplier"):
|
2023-02-18 06:48:41 +00:00
|
|
|
query = query.where(ip.supplier == args.get("supplier"))
|
2019-01-29 08:32:08 +00:00
|
|
|
else:
|
2023-02-18 06:48:41 +00:00
|
|
|
query = query.where((IfNull(ip.customer, "") == "") & (IfNull(ip.supplier, "") == ""))
|
2019-01-25 10:55:15 +00:00
|
|
|
|
2018-08-01 12:14:34 +00:00
|
|
|
if args.get("transaction_date"):
|
2023-02-18 06:48:41 +00:00
|
|
|
query = query.where(
|
|
|
|
(IfNull(ip.valid_from, "2000-01-01") <= args["transaction_date"])
|
|
|
|
& (IfNull(ip.valid_upto, "2500-12-31") >= args["transaction_date"])
|
|
|
|
)
|
|
|
|
|
|
|
|
return query.run()
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-08-01 12:14:34 +00:00
|
|
|
|
|
|
|
def get_price_list_rate_for(args, item_code):
|
|
|
|
"""
|
2019-04-29 17:48:47 +00:00
|
|
|
:param customer: link to Customer DocType
|
|
|
|
:param supplier: link to Supplier DocType
|
2018-08-01 12:14:34 +00:00
|
|
|
:param price_list: str (Standard Buying or Standard Selling)
|
|
|
|
:param item_code: str, Item Doctype field item_code
|
2019-04-29 17:48:47 +00:00
|
|
|
:param qty: Desired Qty
|
2018-08-01 12:14:34 +00:00
|
|
|
:param transaction_date: Date of the price
|
|
|
|
"""
|
|
|
|
item_price_args = {
|
|
|
|
"item_code": item_code,
|
|
|
|
"price_list": args.get("price_list"),
|
|
|
|
"customer": args.get("customer"),
|
|
|
|
"supplier": args.get("supplier"),
|
|
|
|
"uom": args.get("uom"),
|
|
|
|
"transaction_date": args.get("transaction_date"),
|
2021-01-28 06:35:57 +00:00
|
|
|
"batch_no": args.get("batch_no"),
|
2018-08-01 12:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
item_price_data = 0
|
|
|
|
price_list_rate = get_item_price(item_price_args, item_code)
|
|
|
|
if price_list_rate:
|
|
|
|
desired_qty = args.get("qty")
|
2019-02-27 14:01:32 +00:00
|
|
|
if desired_qty and check_packing_list(price_list_rate[0][0], desired_qty, item_code):
|
2018-08-01 12:14:34 +00:00
|
|
|
item_price_data = price_list_rate
|
|
|
|
else:
|
2020-01-09 06:54:43 +00:00
|
|
|
for field in ["customer", "supplier"]:
|
2018-08-01 12:14:34 +00:00
|
|
|
del item_price_args[field]
|
|
|
|
|
2020-01-16 08:07:25 +00:00
|
|
|
general_price_list_rate = get_item_price(
|
|
|
|
item_price_args, item_code, ignore_party=args.get("ignore_party")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-01-09 06:54:43 +00:00
|
|
|
|
2018-08-01 12:14:34 +00:00
|
|
|
if not general_price_list_rate and args.get("uom") != args.get("stock_uom"):
|
2019-04-29 17:48:47 +00:00
|
|
|
item_price_args["uom"] = args.get("stock_uom")
|
2019-01-25 10:55:15 +00:00
|
|
|
general_price_list_rate = get_item_price(
|
|
|
|
item_price_args, item_code, ignore_party=args.get("ignore_party")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2018-08-01 12:14:34 +00:00
|
|
|
|
|
|
|
if general_price_list_rate:
|
|
|
|
item_price_data = general_price_list_rate
|
|
|
|
|
|
|
|
if item_price_data:
|
|
|
|
if item_price_data[0][2] == args.get("uom"):
|
|
|
|
return item_price_data[0][1]
|
|
|
|
elif not args.get("price_list_uom_dependant"):
|
|
|
|
return flt(item_price_data[0][1] * flt(args.get("conversion_factor", 1)))
|
|
|
|
else:
|
|
|
|
return item_price_data[0][1]
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-08-01 12:14:34 +00:00
|
|
|
def check_packing_list(price_list_rate_name, desired_qty, item_code):
|
|
|
|
"""
|
2019-04-29 17:48:47 +00:00
|
|
|
Check if the desired qty is within the increment of the packing list.
|
2018-08-01 12:14:34 +00:00
|
|
|
:param price_list_rate_name: Name of Item Price
|
2019-04-29 17:48:47 +00:00
|
|
|
:param desired_qty: Desired Qt
|
2018-08-01 12:14:34 +00:00
|
|
|
:param item_code: str, Item Doctype field item_code
|
2019-04-29 17:48:47 +00:00
|
|
|
:param qty: Desired Qt
|
2018-08-01 12:14:34 +00:00
|
|
|
"""
|
|
|
|
|
2019-02-27 12:31:25 +00:00
|
|
|
flag = True
|
2018-08-01 12:14:34 +00:00
|
|
|
item_price = frappe.get_doc("Item Price", price_list_rate_name)
|
2019-02-27 14:01:32 +00:00
|
|
|
if item_price.packing_unit:
|
2018-08-01 12:14:34 +00:00
|
|
|
packing_increment = desired_qty % item_price.packing_unit
|
|
|
|
|
2019-02-27 12:31:25 +00:00
|
|
|
if packing_increment != 0:
|
|
|
|
flag = False
|
|
|
|
|
|
|
|
return flag
|
2014-10-03 11:06:43 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
def validate_conversion_rate(args, meta):
|
2014-09-29 06:47:03 +00:00
|
|
|
from erpnext.controllers.accounts_controller import validate_conversion_rate
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2021-07-13 06:15:41 +00:00
|
|
|
company_currency = frappe.get_cached_value("Company", args.company, "default_currency")
|
|
|
|
if not args.conversion_rate and args.currency == company_currency:
|
2015-08-25 07:19:40 +00:00
|
|
|
args.conversion_rate = 1.0
|
|
|
|
|
2021-07-13 06:15:41 +00:00
|
|
|
if (
|
|
|
|
not args.ignore_conversion_rate
|
|
|
|
and args.conversion_rate == 1
|
|
|
|
and args.currency != company_currency
|
|
|
|
):
|
|
|
|
args.conversion_rate = (
|
|
|
|
get_exchange_rate(args.currency, company_currency, args.transaction_date, "for_buying") or 1.0
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2021-07-13 06:15:41 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
# validate currency conversion rate
|
2014-04-11 11:21:27 +00:00
|
|
|
validate_conversion_rate(
|
|
|
|
args.currency, args.conversion_rate, meta.get_label("conversion_rate"), args.company
|
2014-02-10 09:17:54 +00:00
|
|
|
)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
|
|
|
args.conversion_rate = flt(
|
|
|
|
args.conversion_rate,
|
|
|
|
get_field_precision(meta.get_field("conversion_rate"), frappe._dict({"fields": args})),
|
2014-02-14 10:17:51 +00:00
|
|
|
)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2019-05-16 13:47:02 +00:00
|
|
|
if args.price_list:
|
|
|
|
if not args.plc_conversion_rate and args.price_list_currency == frappe.db.get_value(
|
|
|
|
"Price List", args.price_list, "currency", cache=True
|
|
|
|
):
|
|
|
|
args.plc_conversion_rate = 1.0
|
2018-06-06 07:16:06 +00:00
|
|
|
|
2019-05-16 13:47:02 +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,
|
|
|
|
)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2019-05-16 13:47:02 +00:00
|
|
|
if meta.get_field("plc_conversion_rate"):
|
|
|
|
args.plc_conversion_rate = flt(
|
|
|
|
args.plc_conversion_rate,
|
|
|
|
get_field_precision(meta.get_field("plc_conversion_rate"), frappe._dict({"fields": args})),
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-04-03 09:00:42 +00:00
|
|
|
def get_party_item_code(args, item_doc, out):
|
2016-01-19 10:15:49 +00:00
|
|
|
if args.transaction_type == "selling" and args.customer:
|
2018-03-09 06:40:45 +00:00
|
|
|
out.customer_item_code = None
|
2019-07-03 05:04:31 +00:00
|
|
|
|
|
|
|
if args.quotation_to and args.quotation_to != "Customer":
|
|
|
|
return
|
|
|
|
|
2014-12-26 06:11:15 +00:00
|
|
|
customer_item_code = item_doc.get("customer_items", {"customer_name": args.customer})
|
2018-03-09 06:40:45 +00:00
|
|
|
|
|
|
|
if customer_item_code:
|
|
|
|
out.customer_item_code = customer_item_code[0].ref_code
|
|
|
|
else:
|
2018-08-09 05:17:09 +00:00
|
|
|
customer_group = frappe.get_cached_value("Customer", args.customer, "customer_group")
|
2018-03-09 06:40:45 +00:00
|
|
|
customer_group_item_code = item_doc.get("customer_items", {"customer_group": customer_group})
|
|
|
|
if customer_group_item_code and not customer_group_item_code[0].customer_name:
|
|
|
|
out.customer_item_code = customer_group_item_code[0].ref_code
|
2016-01-21 16:58:47 +00:00
|
|
|
|
2016-01-19 10:15:49 +00:00
|
|
|
if args.transaction_type == "buying" and args.supplier:
|
2014-12-26 06:11:15 +00:00
|
|
|
item_supplier = item_doc.get("supplier_items", {"supplier": args.supplier})
|
2014-02-10 09:17:54 +00:00
|
|
|
out.supplier_part_no = item_supplier[0].supplier_part_no if item_supplier else None
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-04-12 08:07:08 +00:00
|
|
|
def get_pos_profile_item_details(company, args, pos_profile=None, update_data=False):
|
2014-02-14 10:17:51 +00:00
|
|
|
res = frappe._dict()
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2018-08-08 11:07:31 +00:00
|
|
|
if not frappe.flags.pos_profile and not pos_profile:
|
|
|
|
pos_profile = frappe.flags.pos_profile = get_pos_profile(company, args.get("pos_profile"))
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2015-08-18 09:59:42 +00:00
|
|
|
if pos_profile:
|
2014-02-10 09:17:54 +00:00
|
|
|
for fieldname in ("income_account", "cost_center", "warehouse", "expense_account"):
|
2018-04-12 08:07:08 +00:00
|
|
|
if (not args.get(fieldname) or update_data) and pos_profile.get(fieldname):
|
2015-08-18 09:59:42 +00:00
|
|
|
res[fieldname] = pos_profile.get(fieldname)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
if res.get("warehouse"):
|
2022-12-29 07:04:18 +00:00
|
|
|
res.actual_qty = get_bin_details(
|
|
|
|
args.item_code, res.warehouse, include_child_warehouses=True
|
|
|
|
).get("actual_qty")
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2014-02-10 09:17:54 +00:00
|
|
|
return res
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-08-18 09:59:42 +00:00
|
|
|
@frappe.whitelist()
|
2017-11-28 10:41:15 +00:00
|
|
|
def get_pos_profile(company, pos_profile=None, user=None):
|
2019-04-04 19:12:48 +00:00
|
|
|
if pos_profile:
|
|
|
|
return frappe.get_cached_doc("POS Profile", pos_profile)
|
2017-11-28 10:41:15 +00:00
|
|
|
|
|
|
|
if not user:
|
|
|
|
user = frappe.session["user"]
|
|
|
|
|
2023-02-18 06:48:41 +00:00
|
|
|
pf = frappe.qb.DocType("POS Profile")
|
|
|
|
pfu = frappe.qb.DocType("POS Profile User")
|
|
|
|
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(pf)
|
|
|
|
.left_join(pfu)
|
|
|
|
.on(pf.name == pfu.parent)
|
|
|
|
.select(pf.star)
|
|
|
|
.where((pfu.user == user) & (pfu.default == 1))
|
2018-11-21 09:06:58 +00:00
|
|
|
)
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2023-02-18 06:48:41 +00:00
|
|
|
if company:
|
|
|
|
query = query.where(pf.company == company)
|
|
|
|
|
|
|
|
pos_profile = query.run(as_dict=True)
|
|
|
|
|
2019-04-04 19:12:48 +00:00
|
|
|
if not pos_profile and company:
|
2023-02-18 06:48:41 +00:00
|
|
|
pos_profile = (
|
|
|
|
frappe.qb.from_(pf)
|
|
|
|
.left_join(pfu)
|
|
|
|
.on(pf.name == pfu.parent)
|
|
|
|
.select(pf.star)
|
|
|
|
.where((pf.company == company) & (pf.disabled == 0))
|
|
|
|
).run(as_dict=True)
|
2019-04-04 19:12:48 +00:00
|
|
|
|
2015-08-18 09:59:42 +00:00
|
|
|
return pos_profile and pos_profile[0] or None
|
2014-04-11 11:21:27 +00:00
|
|
|
|
2022-03-28 13:22:46 +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):
|
2018-08-08 11:07:31 +00:00
|
|
|
variant_of = frappe.db.get_value("Item", item_code, "variant_of", cache=True)
|
2014-10-07 09:59:58 +00:00
|
|
|
filters = {"parent": item_code, "uom": uom}
|
2023-02-28 09:44:34 +00:00
|
|
|
|
2014-10-07 09:59:58 +00:00
|
|
|
if variant_of:
|
2015-10-07 06:25:01 +00:00
|
|
|
filters["parent"] = ("in", (item_code, variant_of))
|
2018-09-11 11:52:25 +00:00
|
|
|
conversion_factor = frappe.db.get_value("UOM Conversion Detail", filters, "conversion_factor")
|
|
|
|
if not conversion_factor:
|
|
|
|
stock_uom = frappe.db.get_value("Item", item_code, "stock_uom")
|
|
|
|
conversion_factor = get_uom_conv_factor(uom, stock_uom)
|
2023-02-18 06:48:41 +00:00
|
|
|
|
2019-01-08 10:51:25 +00:00
|
|
|
return {"conversion_factor": conversion_factor or 1.0}
|
2014-02-28 06:00:47 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
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-04-11 11:21:27 +00:00
|
|
|
return {
|
|
|
|
"projected_qty": frappe.db.get_value(
|
2014-02-10 09:17:54 +00:00
|
|
|
"Bin", {"item_code": item_code, "warehouse": warehouse}, "projected_qty"
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2014-02-10 09:17:54 +00:00
|
|
|
}
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-02-28 06:00:47 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2022-12-29 07:04:18 +00:00
|
|
|
def get_bin_details(item_code, warehouse, company=None, include_child_warehouses=False):
|
2023-01-16 03:20:39 +00:00
|
|
|
bin_details = {"projected_qty": 0, "actual_qty": 0, "reserved_qty": 0}
|
2022-12-29 07:04:18 +00:00
|
|
|
|
|
|
|
if warehouse:
|
2022-12-29 11:08:08 +00:00
|
|
|
from frappe.query_builder.functions import Coalesce, Sum
|
2023-02-28 09:44:34 +00:00
|
|
|
|
2022-12-29 07:04:18 +00:00
|
|
|
from erpnext.stock.doctype.warehouse.warehouse import get_child_warehouses
|
|
|
|
|
|
|
|
warehouses = get_child_warehouses(warehouse) if include_child_warehouses else [warehouse]
|
2022-12-29 11:08:08 +00:00
|
|
|
|
|
|
|
bin = frappe.qb.DocType("Bin")
|
|
|
|
bin_details = (
|
|
|
|
frappe.qb.from_(bin)
|
|
|
|
.select(
|
|
|
|
Coalesce(Sum(bin.projected_qty), 0).as_("projected_qty"),
|
|
|
|
Coalesce(Sum(bin.actual_qty), 0).as_("actual_qty"),
|
|
|
|
Coalesce(Sum(bin.reserved_qty), 0).as_("reserved_qty"),
|
|
|
|
)
|
|
|
|
.where((bin.item_code == item_code) & (bin.warehouse.isin(warehouses)))
|
|
|
|
).run(as_dict=True)[0]
|
2022-12-29 07:04:18 +00:00
|
|
|
|
2021-03-15 06:07:46 +00:00
|
|
|
if company:
|
|
|
|
bin_details["company_total_stock"] = get_company_total_stock(item_code, company)
|
2023-01-02 05:55:13 +00:00
|
|
|
|
2021-03-15 06:07:46 +00:00
|
|
|
return bin_details
|
2016-11-13 15:49:09 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-03-15 06:07:46 +00:00
|
|
|
def get_company_total_stock(item_code, company):
|
2023-02-18 06:48:41 +00:00
|
|
|
bin = frappe.qb.DocType("Bin")
|
|
|
|
wh = frappe.qb.DocType("Warehouse")
|
|
|
|
|
|
|
|
return (
|
|
|
|
frappe.qb.from_(bin)
|
|
|
|
.inner_join(wh)
|
|
|
|
.on(bin.warehouse == wh.name)
|
|
|
|
.select(Sum(bin.actual_qty))
|
|
|
|
.where((wh.company == company) & (bin.item_code == item_code))
|
|
|
|
).run()[0][0]
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-03-15 06:07:46 +00:00
|
|
|
|
2015-03-10 09:49:29 +00:00
|
|
|
@frappe.whitelist()
|
2017-04-20 09:51:01 +00:00
|
|
|
def get_batch_qty(batch_no, warehouse, item_code):
|
2017-04-25 08:28:57 +00:00
|
|
|
from erpnext.stock.doctype.batch import batch
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-03-10 09:34:28 +00:00
|
|
|
if batch_no:
|
2017-04-20 09:51:01 +00:00
|
|
|
return {"actual_batch_qty": batch.get_batch_qty(batch_no, warehouse)}
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-07-24 09:46:25 +00:00
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
@frappe.whitelist()
|
2016-01-15 11:29:26 +00:00
|
|
|
def apply_price_list(args, as_doc=False):
|
|
|
|
"""Apply pricelist on a document-like dict object and return as
|
|
|
|
{'parent': dict, 'children': list}
|
|
|
|
|
|
|
|
:param args: See below
|
|
|
|
:param as_doc: Updates value in the passed dict
|
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
args = {
|
2016-01-15 11:29:26 +00:00
|
|
|
"doctype": "",
|
|
|
|
"name": "",
|
|
|
|
"items": [{"doctype": "", "name": "", "item_code": "", "brand": "", "item_group": ""}, ...],
|
2014-07-01 12:15:15 +00:00
|
|
|
"conversion_rate": 1.0,
|
|
|
|
"selling_price_list": None,
|
|
|
|
"price_list_currency": None,
|
2017-12-06 13:47:04 +00:00
|
|
|
"price_list_uom_dependant": None,
|
2014-07-01 12:15:15 +00:00
|
|
|
"plc_conversion_rate": 1.0,
|
2016-01-15 11:29:26 +00:00
|
|
|
"doctype": "",
|
|
|
|
"name": "",
|
2014-07-01 12:15:15 +00:00
|
|
|
"supplier": None,
|
|
|
|
"transaction_date": None,
|
|
|
|
"conversion_rate": 1.0,
|
|
|
|
"buying_price_list": None,
|
|
|
|
"ignore_pricing_rule": 0/1
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
args = process_args(args)
|
|
|
|
|
|
|
|
parent = get_price_list_currency_and_exchange_rate(args)
|
2021-04-19 05:35:21 +00:00
|
|
|
args.update(parent)
|
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
children = []
|
|
|
|
|
2016-01-15 11:29:26 +00:00
|
|
|
if "items" in args:
|
|
|
|
item_list = args.get("items")
|
2014-07-01 12:15:15 +00:00
|
|
|
args.update(parent)
|
|
|
|
|
|
|
|
for item in item_list:
|
|
|
|
args_copy = frappe._dict(args.copy())
|
|
|
|
args_copy.update(item)
|
|
|
|
item_details = apply_price_list_on_item(args_copy)
|
|
|
|
children.append(item_details)
|
|
|
|
|
2016-01-15 11:29:26 +00:00
|
|
|
if as_doc:
|
2017-12-06 13:47:04 +00:00
|
|
|
args.price_list_currency = (parent.price_list_currency,)
|
2016-01-15 11:29:26 +00:00
|
|
|
args.plc_conversion_rate = parent.plc_conversion_rate
|
|
|
|
if args.get("items"):
|
|
|
|
for i, item in enumerate(args.get("items")):
|
|
|
|
for fieldname in children[i]:
|
|
|
|
# if the field exists in the original doc
|
|
|
|
# update the value
|
|
|
|
if fieldname in item and fieldname not in ("name", "doctype"):
|
|
|
|
item[fieldname] = children[i][fieldname]
|
|
|
|
return args
|
|
|
|
else:
|
|
|
|
return {"parent": parent, "children": children}
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
|
|
|
|
def apply_price_list_on_item(args):
|
2021-12-20 06:46:14 +00:00
|
|
|
item_doc = frappe.db.get_value("Item", args.item_code, ["name", "variant_of"], as_dict=1)
|
2021-08-09 05:11:24 +00:00
|
|
|
item_details = get_price_list_rate(args, item_doc)
|
2022-12-02 11:44:06 +00:00
|
|
|
item_details.update(get_pricing_rule_for_item(args))
|
2016-01-15 11:29:26 +00:00
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
return item_details
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
def get_price_list_currency_and_exchange_rate(args):
|
2015-11-27 09:07:40 +00:00
|
|
|
if not args.price_list:
|
|
|
|
return {}
|
|
|
|
|
2018-05-15 11:29:20 +00:00
|
|
|
if args.doctype in ["Quotation", "Sales Order", "Delivery Note", "Sales Invoice"]:
|
|
|
|
args.update({"exchange_rate": "for_selling"})
|
|
|
|
elif args.doctype in ["Purchase Order", "Purchase Receipt", "Purchase Invoice"]:
|
|
|
|
args.update({"exchange_rate": "for_buying"})
|
|
|
|
|
2019-11-19 13:17:48 +00:00
|
|
|
price_list_details = get_price_list_details(args.price_list)
|
|
|
|
|
|
|
|
price_list_currency = price_list_details.get("currency")
|
|
|
|
price_list_uom_dependant = price_list_details.get("price_list_uom_dependant")
|
|
|
|
|
2014-07-01 12:15:15 +00:00
|
|
|
plc_conversion_rate = args.plc_conversion_rate
|
2018-02-12 09:48:57 +00:00
|
|
|
company_currency = get_company_currency(args.company)
|
2014-07-01 12:15:15 +00:00
|
|
|
|
2015-05-11 11:19:17 +00:00
|
|
|
if (not plc_conversion_rate) or (
|
|
|
|
price_list_currency
|
|
|
|
and args.price_list_currency
|
|
|
|
and price_list_currency != args.price_list_currency
|
|
|
|
):
|
2017-03-14 11:29:11 +00:00
|
|
|
# cksgb 19/09/2016: added args.transaction_date as posting_date argument for get_exchange_rate
|
2018-02-12 09:48:57 +00:00
|
|
|
plc_conversion_rate = (
|
|
|
|
get_exchange_rate(
|
|
|
|
price_list_currency, company_currency, args.transaction_date, args.exchange_rate
|
2018-05-15 11:29:20 +00:00
|
|
|
)
|
|
|
|
or plc_conversion_rate
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2018-05-15 11:29:20 +00:00
|
|
|
return frappe._dict(
|
2022-03-28 13:22:46 +00:00
|
|
|
{
|
2014-07-01 12:15:15 +00:00
|
|
|
"price_list_currency": price_list_currency,
|
2018-05-15 11:29:20 +00:00
|
|
|
"price_list_uom_dependant": price_list_uom_dependant,
|
|
|
|
"plc_conversion_rate": plc_conversion_rate or 1,
|
2022-03-28 13:22:46 +00:00
|
|
|
}
|
|
|
|
)
|
2014-07-01 12:15:15 +00:00
|
|
|
|
2014-11-05 14:44:53 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_default_bom(item_code=None):
|
2022-06-14 11:50:44 +00:00
|
|
|
def _get_bom(item):
|
|
|
|
bom = frappe.get_all(
|
|
|
|
"BOM", dict(item=item, is_active=True, is_default=True, docstatus=1), limit=1
|
2014-11-05 14:44:53 +00:00
|
|
|
)
|
2022-06-14 11:50:44 +00:00
|
|
|
return bom[0].name if bom else None
|
|
|
|
|
|
|
|
if not item_code:
|
|
|
|
return
|
|
|
|
|
|
|
|
bom_name = _get_bom(item_code)
|
|
|
|
|
|
|
|
template_item = frappe.db.get_value("Item", item_code, "variant_of")
|
|
|
|
if not bom_name and template_item:
|
|
|
|
bom_name = _get_bom(template_item)
|
|
|
|
|
|
|
|
return bom_name
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-02-18 06:58:41 +00:00
|
|
|
@frappe.whitelist()
|
2018-05-04 10:32:38 +00:00
|
|
|
def get_valuation_rate(item_code, company, warehouse=None):
|
2018-05-04 11:19:33 +00:00
|
|
|
item = get_item_defaults(item_code, company)
|
2018-07-17 12:31:44 +00:00
|
|
|
item_group = get_item_group_defaults(item_code, company)
|
2018-09-25 09:53:12 +00:00
|
|
|
brand = get_brand_defaults(item_code, company)
|
2018-05-04 10:32:38 +00:00
|
|
|
# item = frappe.get_doc("Item", item_code)
|
2018-05-16 10:20:17 +00:00
|
|
|
if item.get("is_stock_item"):
|
2016-03-07 09:02:23 +00:00
|
|
|
if not warehouse:
|
2018-09-25 09:53:12 +00:00
|
|
|
warehouse = (
|
|
|
|
item.get("default_warehouse")
|
|
|
|
or item_group.get("default_warehouse")
|
|
|
|
or brand.get("default_warehouse")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2016-04-22 11:52:22 +00:00
|
|
|
|
|
|
|
return frappe.db.get_value(
|
|
|
|
"Bin", {"item_code": item_code, "warehouse": warehouse}, ["valuation_rate"], as_dict=True
|
2016-03-07 09:02:23 +00:00
|
|
|
) or {"valuation_rate": 0}
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2018-05-16 10:20:17 +00:00
|
|
|
elif not item.get("is_stock_item"):
|
2023-02-18 06:48:41 +00:00
|
|
|
pi_item = frappe.qb.DocType("Purchase Invoice Item")
|
|
|
|
valuation_rate = (
|
|
|
|
frappe.qb.from_(pi_item)
|
|
|
|
.select((Sum(pi_item.base_net_amount) / Sum(pi_item.qty * pi_item.conversion_factor)))
|
2023-02-28 09:44:34 +00:00
|
|
|
.where((pi_item.docstatus == 1) & (pi_item.item_code == item_code))
|
2023-02-18 06:48:41 +00:00
|
|
|
).run()
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2016-03-07 09:02:23 +00:00
|
|
|
if valuation_rate:
|
|
|
|
return {"valuation_rate": valuation_rate[0][0] or 0.0}
|
|
|
|
else:
|
|
|
|
return {"valuation_rate": 0.0}
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-02-26 05:39:20 +00:00
|
|
|
def get_gross_profit(out):
|
2016-02-26 12:32:55 +00:00
|
|
|
if out.valuation_rate:
|
2017-02-10 08:57:11 +00:00
|
|
|
out.update({"gross_profit": ((out.base_rate - out.valuation_rate) * out.stock_qty)})
|
2016-04-22 11:52:22 +00:00
|
|
|
|
2016-02-25 13:29:20 +00:00
|
|
|
return out
|
2016-01-21 10:40:22 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-11-13 15:49:09 +00:00
|
|
|
@frappe.whitelist()
|
2018-08-01 12:17:07 +00:00
|
|
|
def get_serial_no(args, serial_nos=None, sales_order=None):
|
2023-03-28 06:46:27 +00:00
|
|
|
serial_nos = serial_nos or []
|
|
|
|
return serial_nos
|
2018-05-28 14:37:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_party_blanket_order(args, out):
|
2019-12-04 10:37:11 +00:00
|
|
|
if out["against_blanket_order"]:
|
|
|
|
blanket_order_details = get_blanket_order_details(args)
|
|
|
|
if blanket_order_details:
|
|
|
|
out.update(blanket_order_details)
|
2018-05-28 14:37:08 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-05-28 14:37:08 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_blanket_order_details(args):
|
|
|
|
if isinstance(args, str):
|
|
|
|
args = frappe._dict(json.loads(args))
|
|
|
|
|
|
|
|
blanket_order_details = None
|
2023-02-18 06:48:41 +00:00
|
|
|
|
2018-05-28 14:37:08 +00:00
|
|
|
if args.item_code:
|
2023-02-18 06:48:41 +00:00
|
|
|
bo = frappe.qb.DocType("Blanket Order")
|
|
|
|
bo_item = frappe.qb.DocType("Blanket Order Item")
|
|
|
|
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(bo)
|
|
|
|
.from_(bo_item)
|
|
|
|
.select(bo_item.rate.as_("blanket_order_rate"), bo.name.as_("blanket_order"))
|
|
|
|
.where(
|
|
|
|
(bo.company == args.company)
|
|
|
|
& (bo_item.item_code == args.item_code)
|
|
|
|
& (bo.docstatus == 1)
|
|
|
|
& (bo.name == bo_item.parent)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2018-05-28 14:37:08 +00:00
|
|
|
if args.customer and args.doctype == "Sales Order":
|
2023-02-18 06:48:41 +00:00
|
|
|
query = query.where(bo.customer == args.customer)
|
2018-05-28 14:37:08 +00:00
|
|
|
elif args.supplier and args.doctype == "Purchase Order":
|
2023-02-18 06:48:41 +00:00
|
|
|
query = query.where(bo.supplier == args.supplier)
|
2018-05-28 14:37:08 +00:00
|
|
|
if args.blanket_order:
|
2023-02-18 06:48:41 +00:00
|
|
|
query = query.where(bo.name == args.blanket_order)
|
2018-06-14 12:37:22 +00:00
|
|
|
if args.transaction_date:
|
2023-02-18 06:48:41 +00:00
|
|
|
query = query.where(bo.to_date >= args.transaction_date)
|
2018-06-14 12:37:22 +00:00
|
|
|
|
2023-02-18 06:48:41 +00:00
|
|
|
blanket_order_details = query.run(as_dict=True)
|
2018-05-28 14:37:08 +00:00
|
|
|
blanket_order_details = blanket_order_details[0] if blanket_order_details else ""
|
2023-02-18 06:48:41 +00:00
|
|
|
|
2018-05-28 14:37:08 +00:00
|
|
|
return blanket_order_details
|