brotherton-erpnext/erpnext/utilities/product.py

118 lines
4.0 KiB
Python
Raw Normal View History

# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
2014-10-21 10:46:30 +00:00
# License: GNU General Public License v3. See license.txt
from __future__ import unicode_literals
import frappe
from frappe.utils import cint, fmt_money, flt, nowdate, getdate
2016-11-22 16:53:31 +00:00
from erpnext.accounts.doctype.pricing_rule.pricing_rule import get_pricing_rule_for_item
from erpnext.stock.doctype.batch.batch import get_batch_qty
2014-10-21 10:46:30 +00:00
def get_qty_in_stock(item_code, item_warehouse_field, warehouse=None):
in_stock, stock_qty = 0, ''
template_item_code, is_stock_item = frappe.db.get_value("Item", item_code, ["variant_of", "is_stock_item"])
Hub (#10934) * [WIP]Hub * [listing] Show items, users and item_groups * Show filters * [start] cart, api for rfq and opp * rfq working * [wip] keys * wip quotes * [hub] register/unregister * [hub] rename password to access_token, remove passed company field * [hub] publishing cases, api call wrapper * [hub] add and remove fields working * [hub] fix flags, update on client save working * [hub] new hub page, client item CUD at hub working * listing, standard rate, local site hack * item listing, item page, search, back to home * [hub] implement hub company * [hub] company filter * [hub] basic rfq-ing, item page cleanup * categories wip * [hub] use get_doc_before_save() * [hub] send opportunity message to hub, api to make locally * [hub] enqueueing in hub api request wrapper * cleanup * [hub] refactor shopping cart's product.py to reuse * sync dynamic item fields daily * Scheduler heartbeat check * [wip] hub categories * [hub] wip enqueued callbacks * [hub] outgoing messages, fixing callback loop * [hub] bug: callback save after primary save * [hub] pricing, stock, currency * [hub] replace send_hub_request with make_and_enqueue * add hub.less, refactor code * Remove template html files, add styling for hub item cards * fix paging * add breadcrumb * Add sidebar * [hub] add company page, change country * [hub] order_by filters * [hub] make hub category a tree * [hub] enqueue batched item enqueueing * [hub] requested products page * [minor] * update hub url * [fix] url * [fix] more reform * fix recursion * [hub] data migration plans as jsons * Hub register, create data connector, sync with run * [add] user registration by session user * Removed hub_message * Remove sync code from hub_settings * Remove hub methods from item.py * Update Hub Sync plan * Hub unregister * Update Hub connector on reregister * Dont delete Hub Connector on unregister * Enable hub on success response * Add new hub whitelisted methods * [hub] list working * Hub register from hub page * [hub] Add hub logo in desk icon, link to page * [hub] hide page head on empty state * [hub] make rfq * [hub] push opportunity doc, poll for opportunity docs * add fields to item mapping * update hub mappings * Make RFQ * [hub] item, home routing * Make rfq and send opportunity refactor * [hub][fix] remote lead data * images passed as base64 * set default company on register * Revert "images passed as base64" This reverts commit 0b033a5fb7072b2d39a1b87a47dc41e7af707bb4. * Add sync to hub page * Prompt for publish items to hub * add post process to hub document to lead * Rename Hub document to Hub message, create opportunity in post process
2017-10-05 05:47:30 +00:00
if not warehouse:
warehouse = frappe.db.get_value("Item", item_code, item_warehouse_field)
if not warehouse and template_item_code and template_item_code != item_code:
Hub (#10934) * [WIP]Hub * [listing] Show items, users and item_groups * Show filters * [start] cart, api for rfq and opp * rfq working * [wip] keys * wip quotes * [hub] register/unregister * [hub] rename password to access_token, remove passed company field * [hub] publishing cases, api call wrapper * [hub] add and remove fields working * [hub] fix flags, update on client save working * [hub] new hub page, client item CUD at hub working * listing, standard rate, local site hack * item listing, item page, search, back to home * [hub] implement hub company * [hub] company filter * [hub] basic rfq-ing, item page cleanup * categories wip * [hub] use get_doc_before_save() * [hub] send opportunity message to hub, api to make locally * [hub] enqueueing in hub api request wrapper * cleanup * [hub] refactor shopping cart's product.py to reuse * sync dynamic item fields daily * Scheduler heartbeat check * [wip] hub categories * [hub] wip enqueued callbacks * [hub] outgoing messages, fixing callback loop * [hub] bug: callback save after primary save * [hub] pricing, stock, currency * [hub] replace send_hub_request with make_and_enqueue * add hub.less, refactor code * Remove template html files, add styling for hub item cards * fix paging * add breadcrumb * Add sidebar * [hub] add company page, change country * [hub] order_by filters * [hub] make hub category a tree * [hub] enqueue batched item enqueueing * [hub] requested products page * [minor] * update hub url * [fix] url * [fix] more reform * fix recursion * [hub] data migration plans as jsons * Hub register, create data connector, sync with run * [add] user registration by session user * Removed hub_message * Remove sync code from hub_settings * Remove hub methods from item.py * Update Hub Sync plan * Hub unregister * Update Hub connector on reregister * Dont delete Hub Connector on unregister * Enable hub on success response * Add new hub whitelisted methods * [hub] list working * Hub register from hub page * [hub] Add hub logo in desk icon, link to page * [hub] hide page head on empty state * [hub] make rfq * [hub] push opportunity doc, poll for opportunity docs * add fields to item mapping * update hub mappings * Make RFQ * [hub] item, home routing * Make rfq and send opportunity refactor * [hub][fix] remote lead data * images passed as base64 * set default company on register * Revert "images passed as base64" This reverts commit 0b033a5fb7072b2d39a1b87a47dc41e7af707bb4. * Add sync to hub page * Prompt for publish items to hub * add post process to hub document to lead * Rename Hub document to Hub message, create opportunity in post process
2017-10-05 05:47:30 +00:00
warehouse = frappe.db.get_value("Item", template_item_code, item_warehouse_field)
if warehouse:
stock_qty = frappe.db.sql("""select GREATEST(actual_qty - reserved_qty, 0) from tabBin where
item_code=%s and warehouse=%s""", (item_code, warehouse))
2017-03-21 10:58:03 +00:00
if stock_qty:
in_stock = stock_qty[0][0] > 0 and 1 or 0
if stock_qty[0][0]:
stock_qty = adjust_for_expired_items(item_code, stock_qty, warehouse)
if stock_qty[0][0]:
in_stock = stock_qty[0][0] > 0 and 1 or 0
return frappe._dict({"in_stock": in_stock, "stock_qty": stock_qty, "is_stock_item": is_stock_item})
def adjust_for_expired_items(item_code, stock_qty, warehouse):
batches = frappe.get_list('Batch', filters=[{'item': item_code}], fields=['expiry_date', 'name'])
expired_batches = get_expired_batches(batches)
stock_qty = [list(item) for item in stock_qty]
if expired_batches:
for batch in expired_batches:
if warehouse:
stock_qty[0][0] = max(0, stock_qty[0][0] - get_batch_qty(batch, warehouse))
else:
stock_qty[0][0] = max(0, stock_qty[0][0] - qty_from_all_warehouses(get_batch_qty(batch)))
if not stock_qty[0][0]:
break
return stock_qty
def get_expired_batches(batches):
"""
:param batches: A list of dict in the form [{'expiry_date': datetime.date(20XX, 1, 1), 'name': 'batch_id'}, ...]
"""
return [b.name for b in batches if b.expiry_date and b.expiry_date <= getdate(nowdate())]
def qty_from_all_warehouses(batch_info):
"""
:param batch_info: A list of dict in the form [{u'warehouse': u'Stores - I', u'qty': 0.8}, ...]
"""
qty = 0
for batch in batch_info:
qty = qty + batch.qty
return qty
Hub (#10934) * [WIP]Hub * [listing] Show items, users and item_groups * Show filters * [start] cart, api for rfq and opp * rfq working * [wip] keys * wip quotes * [hub] register/unregister * [hub] rename password to access_token, remove passed company field * [hub] publishing cases, api call wrapper * [hub] add and remove fields working * [hub] fix flags, update on client save working * [hub] new hub page, client item CUD at hub working * listing, standard rate, local site hack * item listing, item page, search, back to home * [hub] implement hub company * [hub] company filter * [hub] basic rfq-ing, item page cleanup * categories wip * [hub] use get_doc_before_save() * [hub] send opportunity message to hub, api to make locally * [hub] enqueueing in hub api request wrapper * cleanup * [hub] refactor shopping cart's product.py to reuse * sync dynamic item fields daily * Scheduler heartbeat check * [wip] hub categories * [hub] wip enqueued callbacks * [hub] outgoing messages, fixing callback loop * [hub] bug: callback save after primary save * [hub] pricing, stock, currency * [hub] replace send_hub_request with make_and_enqueue * add hub.less, refactor code * Remove template html files, add styling for hub item cards * fix paging * add breadcrumb * Add sidebar * [hub] add company page, change country * [hub] order_by filters * [hub] make hub category a tree * [hub] enqueue batched item enqueueing * [hub] requested products page * [minor] * update hub url * [fix] url * [fix] more reform * fix recursion * [hub] data migration plans as jsons * Hub register, create data connector, sync with run * [add] user registration by session user * Removed hub_message * Remove sync code from hub_settings * Remove hub methods from item.py * Update Hub Sync plan * Hub unregister * Update Hub connector on reregister * Dont delete Hub Connector on unregister * Enable hub on success response * Add new hub whitelisted methods * [hub] list working * Hub register from hub page * [hub] Add hub logo in desk icon, link to page * [hub] hide page head on empty state * [hub] make rfq * [hub] push opportunity doc, poll for opportunity docs * add fields to item mapping * update hub mappings * Make RFQ * [hub] item, home routing * Make rfq and send opportunity refactor * [hub][fix] remote lead data * images passed as base64 * set default company on register * Revert "images passed as base64" This reverts commit 0b033a5fb7072b2d39a1b87a47dc41e7af707bb4. * Add sync to hub page * Prompt for publish items to hub * add post process to hub document to lead * Rename Hub document to Hub message, create opportunity in post process
2017-10-05 05:47:30 +00:00
def get_price(item_code, price_list, customer_group, company, qty=1):
template_item_code = frappe.db.get_value("Item", item_code, "variant_of")
2016-11-22 16:53:31 +00:00
Hub (#10934) * [WIP]Hub * [listing] Show items, users and item_groups * Show filters * [start] cart, api for rfq and opp * rfq working * [wip] keys * wip quotes * [hub] register/unregister * [hub] rename password to access_token, remove passed company field * [hub] publishing cases, api call wrapper * [hub] add and remove fields working * [hub] fix flags, update on client save working * [hub] new hub page, client item CUD at hub working * listing, standard rate, local site hack * item listing, item page, search, back to home * [hub] implement hub company * [hub] company filter * [hub] basic rfq-ing, item page cleanup * categories wip * [hub] use get_doc_before_save() * [hub] send opportunity message to hub, api to make locally * [hub] enqueueing in hub api request wrapper * cleanup * [hub] refactor shopping cart's product.py to reuse * sync dynamic item fields daily * Scheduler heartbeat check * [wip] hub categories * [hub] wip enqueued callbacks * [hub] outgoing messages, fixing callback loop * [hub] bug: callback save after primary save * [hub] pricing, stock, currency * [hub] replace send_hub_request with make_and_enqueue * add hub.less, refactor code * Remove template html files, add styling for hub item cards * fix paging * add breadcrumb * Add sidebar * [hub] add company page, change country * [hub] order_by filters * [hub] make hub category a tree * [hub] enqueue batched item enqueueing * [hub] requested products page * [minor] * update hub url * [fix] url * [fix] more reform * fix recursion * [hub] data migration plans as jsons * Hub register, create data connector, sync with run * [add] user registration by session user * Removed hub_message * Remove sync code from hub_settings * Remove hub methods from item.py * Update Hub Sync plan * Hub unregister * Update Hub connector on reregister * Dont delete Hub Connector on unregister * Enable hub on success response * Add new hub whitelisted methods * [hub] list working * Hub register from hub page * [hub] Add hub logo in desk icon, link to page * [hub] hide page head on empty state * [hub] make rfq * [hub] push opportunity doc, poll for opportunity docs * add fields to item mapping * update hub mappings * Make RFQ * [hub] item, home routing * Make rfq and send opportunity refactor * [hub][fix] remote lead data * images passed as base64 * set default company on register * Revert "images passed as base64" This reverts commit 0b033a5fb7072b2d39a1b87a47dc41e7af707bb4. * Add sync to hub page * Prompt for publish items to hub * add post process to hub document to lead * Rename Hub document to Hub message, create opportunity in post process
2017-10-05 05:47:30 +00:00
if price_list:
price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
filters={"price_list": price_list, "item_code": item_code})
if template_item_code and not price:
price = frappe.get_all("Item Price", fields=["price_list_rate", "currency"],
filters={"price_list": price_list, "item_code": template_item_code})
if price:
2016-11-22 16:53:31 +00:00
pricing_rule = get_pricing_rule_for_item(frappe._dict({
"item_code": item_code,
"qty": qty,
"transaction_type": "selling",
"price_list": price_list,
Hub (#10934) * [WIP]Hub * [listing] Show items, users and item_groups * Show filters * [start] cart, api for rfq and opp * rfq working * [wip] keys * wip quotes * [hub] register/unregister * [hub] rename password to access_token, remove passed company field * [hub] publishing cases, api call wrapper * [hub] add and remove fields working * [hub] fix flags, update on client save working * [hub] new hub page, client item CUD at hub working * listing, standard rate, local site hack * item listing, item page, search, back to home * [hub] implement hub company * [hub] company filter * [hub] basic rfq-ing, item page cleanup * categories wip * [hub] use get_doc_before_save() * [hub] send opportunity message to hub, api to make locally * [hub] enqueueing in hub api request wrapper * cleanup * [hub] refactor shopping cart's product.py to reuse * sync dynamic item fields daily * Scheduler heartbeat check * [wip] hub categories * [hub] wip enqueued callbacks * [hub] outgoing messages, fixing callback loop * [hub] bug: callback save after primary save * [hub] pricing, stock, currency * [hub] replace send_hub_request with make_and_enqueue * add hub.less, refactor code * Remove template html files, add styling for hub item cards * fix paging * add breadcrumb * Add sidebar * [hub] add company page, change country * [hub] order_by filters * [hub] make hub category a tree * [hub] enqueue batched item enqueueing * [hub] requested products page * [minor] * update hub url * [fix] url * [fix] more reform * fix recursion * [hub] data migration plans as jsons * Hub register, create data connector, sync with run * [add] user registration by session user * Removed hub_message * Remove sync code from hub_settings * Remove hub methods from item.py * Update Hub Sync plan * Hub unregister * Update Hub connector on reregister * Dont delete Hub Connector on unregister * Enable hub on success response * Add new hub whitelisted methods * [hub] list working * Hub register from hub page * [hub] Add hub logo in desk icon, link to page * [hub] hide page head on empty state * [hub] make rfq * [hub] push opportunity doc, poll for opportunity docs * add fields to item mapping * update hub mappings * Make RFQ * [hub] item, home routing * Make rfq and send opportunity refactor * [hub][fix] remote lead data * images passed as base64 * set default company on register * Revert "images passed as base64" This reverts commit 0b033a5fb7072b2d39a1b87a47dc41e7af707bb4. * Add sync to hub page * Prompt for publish items to hub * add post process to hub document to lead * Rename Hub document to Hub message, create opportunity in post process
2017-10-05 05:47:30 +00:00
"customer_group": customer_group,
"company": company,
2016-11-22 16:53:31 +00:00
"conversion_rate": 1,
"for_shopping_cart": True
}))
if pricing_rule:
if pricing_rule.pricing_rule_for == "Discount Percentage":
price[0].price_list_rate = flt(price[0].price_list_rate * (1.0 - (flt(pricing_rule.discount_percentage) / 100.0)))
2016-11-22 16:53:31 +00:00
if pricing_rule.pricing_rule_for == "Price":
price[0].price_list_rate = pricing_rule.price_list_rate
Hub (#10934) * [WIP]Hub * [listing] Show items, users and item_groups * Show filters * [start] cart, api for rfq and opp * rfq working * [wip] keys * wip quotes * [hub] register/unregister * [hub] rename password to access_token, remove passed company field * [hub] publishing cases, api call wrapper * [hub] add and remove fields working * [hub] fix flags, update on client save working * [hub] new hub page, client item CUD at hub working * listing, standard rate, local site hack * item listing, item page, search, back to home * [hub] implement hub company * [hub] company filter * [hub] basic rfq-ing, item page cleanup * categories wip * [hub] use get_doc_before_save() * [hub] send opportunity message to hub, api to make locally * [hub] enqueueing in hub api request wrapper * cleanup * [hub] refactor shopping cart's product.py to reuse * sync dynamic item fields daily * Scheduler heartbeat check * [wip] hub categories * [hub] wip enqueued callbacks * [hub] outgoing messages, fixing callback loop * [hub] bug: callback save after primary save * [hub] pricing, stock, currency * [hub] replace send_hub_request with make_and_enqueue * add hub.less, refactor code * Remove template html files, add styling for hub item cards * fix paging * add breadcrumb * Add sidebar * [hub] add company page, change country * [hub] order_by filters * [hub] make hub category a tree * [hub] enqueue batched item enqueueing * [hub] requested products page * [minor] * update hub url * [fix] url * [fix] more reform * fix recursion * [hub] data migration plans as jsons * Hub register, create data connector, sync with run * [add] user registration by session user * Removed hub_message * Remove sync code from hub_settings * Remove hub methods from item.py * Update Hub Sync plan * Hub unregister * Update Hub connector on reregister * Dont delete Hub Connector on unregister * Enable hub on success response * Add new hub whitelisted methods * [hub] list working * Hub register from hub page * [hub] Add hub logo in desk icon, link to page * [hub] hide page head on empty state * [hub] make rfq * [hub] push opportunity doc, poll for opportunity docs * add fields to item mapping * update hub mappings * Make RFQ * [hub] item, home routing * Make rfq and send opportunity refactor * [hub][fix] remote lead data * images passed as base64 * set default company on register * Revert "images passed as base64" This reverts commit 0b033a5fb7072b2d39a1b87a47dc41e7af707bb4. * Add sync to hub page * Prompt for publish items to hub * add post process to hub document to lead * Rename Hub document to Hub message, create opportunity in post process
2017-10-05 05:47:30 +00:00
price_obj = price[0]
if price_obj:
price_obj["formatted_price"] = fmt_money(price_obj["price_list_rate"], currency=price_obj["currency"])
price_obj["currency_symbol"] = not cint(frappe.db.get_default("hide_currency_symbol")) \
and (frappe.db.get_value("Currency", price_obj.currency, "symbol") or price_obj.currency) \
or ""
if not price_obj["price_list_rate"]:
price_obj["price_list_rate"] = 0
if not price_obj["currency"]:
price_obj["currency"] = ""
if not price_obj["formatted_price"]:
price_obj["formatted_price"] = ""
return price_obj