[refactor] stock entry, minor ux
This commit is contained in:
parent
0fb678e7cf
commit
df9e80c87a
@ -435,7 +435,7 @@ def create_territories():
|
||||
}).insert()
|
||||
|
||||
def login_as_first_user(args):
|
||||
if args.get("email"):
|
||||
if args.get("email") and hasattr(frappe.local, "login_manager"):
|
||||
frappe.local.login_manager.user = args.get("email")
|
||||
frappe.local.login_manager.post_login()
|
||||
|
||||
|
@ -1,12 +1,13 @@
|
||||
frappe.listview_settings['Material Request'] = {
|
||||
add_fields: ["material_request_type", "status", "per_ordered"],
|
||||
get_status: function(doc) {
|
||||
get_indicator: function(doc) {
|
||||
console.log()
|
||||
if(doc.status=="Stopped") {
|
||||
return [__("Stopped"), "red", "status,=,Stopped"];
|
||||
} if(doc.docstatus==1 && doc.per_ordered < 100) {
|
||||
} else if(doc.docstatus==1 && doc.per_ordered < 100) {
|
||||
return [__("Pending"), "orange", "per_ordered,<,100"];
|
||||
} else if(doc.status==1 && doc.per_ordered == 100) {
|
||||
return [__("Completed"), "green", "per_ordered,=,100"];
|
||||
} else if(doc.docstatus==1 && doc.per_ordered == 100) {
|
||||
return [__("Ordered"), "green", "per_ordered,=,100"];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -286,7 +286,7 @@ class PurchaseReceipt(BuyingController):
|
||||
if d.item_code in stock_items and flt(d.valuation_rate) and flt(d.qty):
|
||||
if warehouse_account.get(d.warehouse):
|
||||
|
||||
val_rate_db_precision = 6 if cint(self.precision("valuation_rate")) <= 6 else 9
|
||||
val_rate_db_precision = 6 if cint(d.precision("valuation_rate")) <= 6 else 9
|
||||
|
||||
# warehouse account
|
||||
gl_entries.append(self.get_gl_dict({
|
||||
|
@ -9,7 +9,7 @@ from frappe.utils import cstr, cint, flt, comma_or, nowdate
|
||||
|
||||
from frappe import _
|
||||
from erpnext.stock.utils import get_incoming_rate
|
||||
from erpnext.stock.stock_ledger import get_previous_sle
|
||||
from erpnext.stock.stock_ledger import get_previous_sle, NegativeStockError
|
||||
from erpnext.controllers.queries import get_match_cond
|
||||
from erpnext.stock.get_item_details import get_available_qty, get_default_cost_center, get_conversion_factor
|
||||
from erpnext.manufacturing.doctype.bom.bom import validate_bom_no
|
||||
@ -255,7 +255,7 @@ class StockEntry(StockController):
|
||||
if d.docstatus==1 and d.s_warehouse and not allow_negative_stock and d.actual_qty < d.transfer_qty:
|
||||
frappe.throw(_("""Row {0}: Qty not avalable in warehouse {1} on {2} {3}.
|
||||
Available Qty: {4}, Transfer Qty: {5}""").format(d.idx, d.s_warehouse,
|
||||
self.posting_date, self.posting_time, d.actual_qty, d.transfer_qty))
|
||||
self.posting_date, self.posting_time, d.actual_qty, d.transfer_qty), NegativeStockError)
|
||||
|
||||
# get incoming rate
|
||||
if not flt(d.incoming_rate) or d.s_warehouse or self.purpose == "Sales Return" or force:
|
||||
|
@ -59,278 +59,256 @@ def delete_cancelled_entry(voucher_type, voucher_no):
|
||||
frappe.db.sql("""delete from `tabStock Ledger Entry`
|
||||
where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
|
||||
|
||||
def update_entries_after(args, allow_zero_rate=False, allow_negative_stock=False, verbose=1):
|
||||
class update_entries_after(object):
|
||||
"""
|
||||
update valution rate and qty after transaction
|
||||
from the current time-bucket onwards
|
||||
|
||||
args = {
|
||||
"item_code": "ABC",
|
||||
"warehouse": "XYZ",
|
||||
"posting_date": "2012-12-12",
|
||||
"posting_time": "12:00"
|
||||
}
|
||||
:param args: args as dict
|
||||
|
||||
args = {
|
||||
"item_code": "ABC",
|
||||
"warehouse": "XYZ",
|
||||
"posting_date": "2012-12-12",
|
||||
"posting_time": "12:00"
|
||||
}
|
||||
"""
|
||||
if not _exceptions:
|
||||
frappe.local.stockledger_exceptions = []
|
||||
def __init__(self, args, allow_zero_rate=False, allow_negative_stock=None, verbose=1):
|
||||
from frappe.model.meta import get_field_precision
|
||||
|
||||
if not allow_negative_stock:
|
||||
allow_negative_stock = cint(frappe.db.get_default("allow_negative_stock"))
|
||||
self.exceptions = []
|
||||
self.verbose = verbose
|
||||
self.allow_zero_rate = allow_zero_rate
|
||||
self.allow_negative_stock = allow_negative_stock
|
||||
|
||||
previous_sle = get_sle_before_datetime(args)
|
||||
if self.allow_negative_stock==None:
|
||||
self.allow_negative_stock = cint(frappe.db.get_single_value("Stock Settings",
|
||||
"allow_negative_stock"))
|
||||
|
||||
qty_after_transaction = flt(previous_sle.get("qty_after_transaction"))
|
||||
valuation_rate = flt(previous_sle.get("valuation_rate"))
|
||||
stock_queue = json.loads(previous_sle.get("stock_queue") or "[]")
|
||||
stock_value = flt(previous_sle.get("stock_value"))
|
||||
prev_stock_value = flt(previous_sle.get("stock_value"))
|
||||
self.args = args
|
||||
for key, value in args.iteritems():
|
||||
setattr(self, key, value)
|
||||
|
||||
entries_to_fix = get_sle_after_datetime(previous_sle or \
|
||||
{"item_code": args["item_code"], "warehouse": args["warehouse"]}, for_update=True)
|
||||
valuation_method = get_valuation_method(args["item_code"])
|
||||
stock_value_difference = 0.0
|
||||
self.previous_sle = self.get_sle_before_datetime()
|
||||
self.previous_sle = self.previous_sle[0] if self.previous_sle else frappe._dict()
|
||||
|
||||
for sle in entries_to_fix:
|
||||
if sle.serial_no or not cint(frappe.db.get_value("Stock Settings", None, "allow_negative_stock")):
|
||||
for key in ("qty_after_transaction", "valuation_rate", "stock_value"):
|
||||
setattr(self, key, flt(self.previous_sle.get(key)))
|
||||
|
||||
self.company = frappe.db.get_value("Warehouse", self.warehouse, "company")
|
||||
self.precision = get_field_precision(frappe.get_meta("Stock Ledger Entry").get_field("stock_value"),
|
||||
currency=frappe.db.get_value("Company", self.company, "default_currency"))
|
||||
|
||||
self.prev_stock_value = self.stock_value
|
||||
self.stock_queue = json.loads(self.previous_sle.stock_queue or "[]")
|
||||
self.valuation_method = get_valuation_method(self.item_code)
|
||||
self.stock_value_difference = 0.0
|
||||
self.build()
|
||||
|
||||
def build(self):
|
||||
entries_to_fix = self.get_sle_after_datetime()
|
||||
|
||||
for sle in entries_to_fix:
|
||||
self.process_sle(sle)
|
||||
|
||||
if self.exceptions:
|
||||
self.raise_exceptions()
|
||||
|
||||
self.update_bin()
|
||||
|
||||
def update_bin(self):
|
||||
# update bin
|
||||
bin_name = frappe.db.get_value("Bin", {
|
||||
"item_code": self.item_code,
|
||||
"warehouse": self.warehouse
|
||||
})
|
||||
|
||||
if not bin_name:
|
||||
bin_doc = frappe.get_doc({
|
||||
"doctype": "Bin",
|
||||
"item_code": self.item_code,
|
||||
"warehouse": self.warehouse
|
||||
})
|
||||
bin_doc.insert(ignore_permissions=True)
|
||||
else:
|
||||
bin_doc = frappe.get_doc("Bin", bin_name)
|
||||
|
||||
bin_doc.update({
|
||||
"valuation_rate": self.valuation_rate,
|
||||
"actual_qty": self.qty_after_transaction,
|
||||
"stock_value": self.stock_value
|
||||
})
|
||||
bin_doc.save(ignore_permissions=True)
|
||||
|
||||
def process_sle(self, sle):
|
||||
if sle.serial_no or not cint(self.allow_negative_stock):
|
||||
# validate negative stock for serialized items, fifo valuation
|
||||
# or when negative stock is not allowed for moving average
|
||||
if not validate_negative_stock(qty_after_transaction, sle):
|
||||
qty_after_transaction += flt(sle.actual_qty)
|
||||
continue
|
||||
|
||||
if not self.validate_negative_stock(sle):
|
||||
self.qty_after_transaction += flt(sle.actual_qty)
|
||||
return
|
||||
|
||||
if sle.serial_no:
|
||||
valuation_rate = get_serialized_values(qty_after_transaction, sle, valuation_rate)
|
||||
qty_after_transaction += flt(sle.actual_qty)
|
||||
|
||||
self.valuation_rate = self.get_serialized_values(sle)
|
||||
self.qty_after_transaction += flt(sle.actual_qty)
|
||||
self.stock_value = self.qty_after_transaction * self.valuation_rate
|
||||
else:
|
||||
if sle.voucher_type=="Stock Reconciliation":
|
||||
valuation_rate = sle.valuation_rate
|
||||
qty_after_transaction = sle.qty_after_transaction
|
||||
stock_queue = [[qty_after_transaction, valuation_rate]]
|
||||
# assert
|
||||
self.valuation_rate = sle.valuation_rate
|
||||
self.qty_after_transaction = sle.qty_after_transaction
|
||||
self.stock_queue = [[self.qty_after_transaction, self.valuation_rate]]
|
||||
self.stock_value = self.qty_after_transaction * self.valuation_rate
|
||||
else:
|
||||
if valuation_method == "Moving Average":
|
||||
valuation_rate = get_moving_average_values(qty_after_transaction, sle, valuation_rate, allow_zero_rate)
|
||||
if self.valuation_method == "Moving Average":
|
||||
self.get_moving_average_values(sle)
|
||||
self.qty_after_transaction += flt(sle.actual_qty)
|
||||
self.stock_value = self.qty_after_transaction * self.valuation_rate
|
||||
else:
|
||||
valuation_rate = get_fifo_values(qty_after_transaction, sle, stock_queue, allow_zero_rate)
|
||||
self.get_fifo_values(sle)
|
||||
self.qty_after_transaction += flt(sle.actual_qty)
|
||||
self.stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.stock_queue))
|
||||
|
||||
|
||||
qty_after_transaction += flt(sle.actual_qty)
|
||||
|
||||
# get stock value
|
||||
if sle.serial_no:
|
||||
stock_value = qty_after_transaction * valuation_rate
|
||||
elif valuation_method == "Moving Average":
|
||||
stock_value = qty_after_transaction * valuation_rate
|
||||
else:
|
||||
stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue))
|
||||
|
||||
# rounding as per precision
|
||||
from frappe.model.meta import get_field_precision
|
||||
meta = frappe.get_meta("Stock Ledger Entry")
|
||||
self.stock_value = flt(self.stock_value, self.precision)
|
||||
|
||||
stock_value = flt(stock_value, get_field_precision(meta.get_field("stock_value"),
|
||||
frappe._dict({"fields": sle})))
|
||||
|
||||
stock_value_difference = stock_value - prev_stock_value
|
||||
prev_stock_value = stock_value
|
||||
stock_value_difference = self.stock_value - self.prev_stock_value
|
||||
self.prev_stock_value = self.stock_value
|
||||
|
||||
# update current sle
|
||||
frappe.db.sql("""update `tabStock Ledger Entry`
|
||||
set qty_after_transaction=%s, valuation_rate=%s, stock_queue=%s,
|
||||
stock_value=%s, stock_value_difference=%s where name=%s""",
|
||||
(qty_after_transaction, valuation_rate,
|
||||
json.dumps(stock_queue), stock_value, stock_value_difference, sle.name))
|
||||
(self.qty_after_transaction, self.valuation_rate,
|
||||
json.dumps(self.stock_queue), self.stock_value, stock_value_difference, sle.name))
|
||||
|
||||
if _exceptions:
|
||||
_raise_exceptions(args, verbose)
|
||||
def validate_negative_stock(self, sle):
|
||||
"""
|
||||
validate negative stock for entries current datetime onwards
|
||||
will not consider cancelled entries
|
||||
"""
|
||||
diff = self.qty_after_transaction + flt(sle.actual_qty)
|
||||
|
||||
# update bin
|
||||
if not frappe.db.exists({"doctype": "Bin", "item_code": args["item_code"],
|
||||
"warehouse": args["warehouse"]}):
|
||||
bin_wrapper = frappe.get_doc({
|
||||
"doctype": "Bin",
|
||||
"item_code": args["item_code"],
|
||||
"warehouse": args["warehouse"],
|
||||
})
|
||||
bin_wrapper.flags.ignore_permissions = 1
|
||||
bin_wrapper.insert()
|
||||
|
||||
frappe.db.sql("""update `tabBin` set valuation_rate=%s, actual_qty=%s,
|
||||
stock_value=%s,
|
||||
projected_qty = (actual_qty + indented_qty + ordered_qty + planned_qty - reserved_qty)
|
||||
where item_code=%s and warehouse=%s""", (valuation_rate, qty_after_transaction,
|
||||
stock_value, args["item_code"], args["warehouse"]))
|
||||
|
||||
def get_sle_before_datetime(args, for_update=False):
|
||||
"""
|
||||
get previous stock ledger entry before current time-bucket
|
||||
|
||||
Details:
|
||||
get the last sle before the current time-bucket, so that all values
|
||||
are reposted from the current time-bucket onwards.
|
||||
this is necessary because at the time of cancellation, there may be
|
||||
entries between the cancelled entries in the same time-bucket
|
||||
"""
|
||||
sle = get_stock_ledger_entries(args,
|
||||
["timestamp(posting_date, posting_time) < timestamp(%(posting_date)s, %(posting_time)s)"],
|
||||
"desc", "limit 1", for_update=for_update)
|
||||
|
||||
return sle and sle[0] or frappe._dict()
|
||||
|
||||
def get_sle_after_datetime(args, for_update=False):
|
||||
"""get Stock Ledger Entries after a particular datetime, for reposting"""
|
||||
# NOTE: using for update of
|
||||
conditions = ["timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s)"]
|
||||
|
||||
# Excluding name: Workaround for MariaDB timestamp() floating microsecond issue
|
||||
if args.get("name"):
|
||||
conditions.append("name!=%(name)s")
|
||||
|
||||
return get_stock_ledger_entries(args, conditions, "asc", for_update=for_update)
|
||||
|
||||
def get_stock_ledger_entries(args, conditions=None, order="desc", limit=None, for_update=False):
|
||||
"""get stock ledger entries filtered by specific posting datetime conditions"""
|
||||
if not args.get("posting_date"):
|
||||
args["posting_date"] = "1900-01-01"
|
||||
if not args.get("posting_time"):
|
||||
args["posting_time"] = "00:00"
|
||||
|
||||
return frappe.db.sql("""select *, timestamp(posting_date, posting_time) as "timestamp" from `tabStock Ledger Entry`
|
||||
where item_code = %%(item_code)s
|
||||
and warehouse = %%(warehouse)s
|
||||
and ifnull(is_cancelled, 'No')='No'
|
||||
%(conditions)s
|
||||
order by timestamp(posting_date, posting_time) %(order)s, name %(order)s
|
||||
%(limit)s %(for_update)s""" % {
|
||||
"conditions": conditions and ("and " + " and ".join(conditions)) or "",
|
||||
"limit": limit or "",
|
||||
"for_update": for_update and "for update" or "",
|
||||
"order": order
|
||||
}, args, as_dict=1)
|
||||
|
||||
def validate_negative_stock(qty_after_transaction, sle):
|
||||
"""
|
||||
validate negative stock for entries current datetime onwards
|
||||
will not consider cancelled entries
|
||||
"""
|
||||
diff = qty_after_transaction + flt(sle.actual_qty)
|
||||
|
||||
if not _exceptions:
|
||||
frappe.local.stockledger_exceptions = []
|
||||
|
||||
if diff < 0 and abs(diff) > 0.0001:
|
||||
# negative stock!
|
||||
exc = sle.copy().update({"diff": diff})
|
||||
_exceptions.append(exc)
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
|
||||
def get_serialized_values(qty_after_transaction, sle, valuation_rate):
|
||||
incoming_rate = flt(sle.incoming_rate)
|
||||
actual_qty = flt(sle.actual_qty)
|
||||
serial_no = cstr(sle.serial_no).split("\n")
|
||||
|
||||
if incoming_rate < 0:
|
||||
# wrong incoming rate
|
||||
incoming_rate = valuation_rate
|
||||
elif incoming_rate == 0 or flt(sle.actual_qty) < 0:
|
||||
# In case of delivery/stock issue, get average purchase rate
|
||||
# of serial nos of current entry
|
||||
incoming_rate = flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0))
|
||||
from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))),
|
||||
tuple(serial_no))[0][0])
|
||||
|
||||
if incoming_rate and not valuation_rate:
|
||||
valuation_rate = incoming_rate
|
||||
else:
|
||||
new_stock_qty = qty_after_transaction + actual_qty
|
||||
if new_stock_qty > 0:
|
||||
new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate
|
||||
if new_stock_value > 0:
|
||||
# calculate new valuation rate only if stock value is positive
|
||||
# else it remains the same as that of previous entry
|
||||
valuation_rate = new_stock_value / new_stock_qty
|
||||
|
||||
return valuation_rate
|
||||
|
||||
def get_moving_average_values(qty_after_transaction, sle, valuation_rate, allow_zero_rate):
|
||||
incoming_rate = flt(sle.incoming_rate)
|
||||
actual_qty = flt(sle.actual_qty)
|
||||
|
||||
if flt(sle.actual_qty) > 0:
|
||||
if qty_after_transaction < 0 and not valuation_rate:
|
||||
# if negative stock, take current valuation rate as incoming rate
|
||||
valuation_rate = incoming_rate
|
||||
|
||||
new_stock_qty = abs(qty_after_transaction) + actual_qty
|
||||
new_stock_value = (abs(qty_after_transaction) * valuation_rate) + (actual_qty * incoming_rate)
|
||||
|
||||
if new_stock_qty:
|
||||
valuation_rate = new_stock_value / flt(new_stock_qty)
|
||||
elif not valuation_rate and qty_after_transaction <= 0:
|
||||
valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, allow_zero_rate)
|
||||
|
||||
return abs(flt(valuation_rate))
|
||||
|
||||
def get_fifo_values(qty_after_transaction, sle, stock_queue, allow_zero_rate):
|
||||
incoming_rate = flt(sle.incoming_rate)
|
||||
actual_qty = flt(sle.actual_qty)
|
||||
|
||||
if actual_qty > 0:
|
||||
if not stock_queue:
|
||||
stock_queue.append([0, 0])
|
||||
|
||||
if stock_queue[-1][0] > 0:
|
||||
stock_queue.append([actual_qty, incoming_rate])
|
||||
if diff < 0 and abs(diff) > 0.0001:
|
||||
# negative stock!
|
||||
exc = sle.copy().update({"diff": diff})
|
||||
self.exceptions.append(exc)
|
||||
return False
|
||||
else:
|
||||
qty = stock_queue[-1][0] + actual_qty
|
||||
if qty == 0:
|
||||
stock_queue.pop(-1)
|
||||
return True
|
||||
|
||||
def get_serialized_values(self, sle):
|
||||
incoming_rate = flt(sle.incoming_rate)
|
||||
actual_qty = flt(sle.actual_qty)
|
||||
serial_no = cstr(sle.serial_no).split("\n")
|
||||
|
||||
if incoming_rate < 0:
|
||||
# wrong incoming rate
|
||||
incoming_rate = self.valuation_rate
|
||||
elif incoming_rate == 0 or flt(sle.actual_qty) < 0:
|
||||
# In case of delivery/stock issue, get average purchase rate
|
||||
# of serial nos of current entry
|
||||
incoming_rate = flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0))
|
||||
from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))),
|
||||
tuple(serial_no))[0][0])
|
||||
|
||||
if incoming_rate and not self.valuation_rate:
|
||||
self.valuation_rate = incoming_rate
|
||||
else:
|
||||
new_stock_qty = self.qty_after_transaction + actual_qty
|
||||
if new_stock_qty > 0:
|
||||
new_stock_value = self.qty_after_transaction * self.valuation_rate + actual_qty * incoming_rate
|
||||
if new_stock_value > 0:
|
||||
# calculate new valuation rate only if stock value is positive
|
||||
# else it remains the same as that of previous entry
|
||||
self.valuation_rate = new_stock_value / new_stock_qty
|
||||
|
||||
def get_moving_average_values(self, sle):
|
||||
incoming_rate = flt(sle.incoming_rate)
|
||||
actual_qty = flt(sle.actual_qty)
|
||||
|
||||
if flt(sle.actual_qty) > 0:
|
||||
if self.qty_after_transaction < 0 and not self.valuation_rate:
|
||||
# if negative stock, take current valuation rate as incoming rate
|
||||
self.valuation_rate = incoming_rate
|
||||
|
||||
new_stock_qty = abs(self.qty_after_transaction) + actual_qty
|
||||
new_stock_value = (abs(self.qty_after_transaction) * self.valuation_rate) + (actual_qty * incoming_rate)
|
||||
|
||||
if new_stock_qty:
|
||||
self.valuation_rate = new_stock_value / flt(new_stock_qty)
|
||||
elif not self.valuation_rate and self.qty_after_transaction <= 0:
|
||||
valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, self.allow_zero_rate)
|
||||
|
||||
return abs(flt(valuation_rate))
|
||||
|
||||
def get_fifo_values(self, sle):
|
||||
incoming_rate = flt(sle.incoming_rate)
|
||||
actual_qty = flt(sle.actual_qty)
|
||||
|
||||
if actual_qty > 0:
|
||||
if not self.stock_queue:
|
||||
self.stock_queue.append([0, 0])
|
||||
|
||||
if self.stock_queue[-1][0] > 0:
|
||||
self.stock_queue.append([actual_qty, incoming_rate])
|
||||
else:
|
||||
stock_queue[-1] = [qty, incoming_rate]
|
||||
else:
|
||||
qty_to_pop = abs(actual_qty)
|
||||
while qty_to_pop:
|
||||
if not stock_queue:
|
||||
stock_queue.append([0, get_valuation_rate(sle.item_code, sle.warehouse, allow_zero_rate)
|
||||
if qty_after_transaction <= 0 else 0])
|
||||
qty = self.stock_queue[-1][0] + actual_qty
|
||||
if qty == 0:
|
||||
self.stock_queue.pop(-1)
|
||||
else:
|
||||
self.stock_queue[-1] = [qty, incoming_rate]
|
||||
else:
|
||||
qty_to_pop = abs(actual_qty)
|
||||
while qty_to_pop:
|
||||
if not self.stock_queue:
|
||||
if self.qty_after_transaction > 0:
|
||||
_rate = get_valuation_rate(sle.item_code, sle.warehouse, self.allow_zero_rate)
|
||||
else:
|
||||
_rate = 0
|
||||
self.stock_queue.append([0, _rate])
|
||||
|
||||
batch = stock_queue[0]
|
||||
batch = self.stock_queue[0]
|
||||
|
||||
if qty_to_pop >= batch[0]:
|
||||
# consume current batch
|
||||
qty_to_pop = qty_to_pop - batch[0]
|
||||
stock_queue.pop(0)
|
||||
if not stock_queue and qty_to_pop:
|
||||
# stock finished, qty still remains to be withdrawn
|
||||
# negative stock, keep in as a negative batch
|
||||
stock_queue.append([-qty_to_pop, batch[1]])
|
||||
break
|
||||
if qty_to_pop >= batch[0]:
|
||||
# consume current batch
|
||||
qty_to_pop = qty_to_pop - batch[0]
|
||||
self.stock_queue.pop(0)
|
||||
if not self.stock_queue and qty_to_pop:
|
||||
# stock finished, qty still remains to be withdrawn
|
||||
# negative stock, keep in as a negative batch
|
||||
self.stock_queue.append([-qty_to_pop, batch[1]])
|
||||
break
|
||||
|
||||
else:
|
||||
# qty found in current batch
|
||||
# consume it and exit
|
||||
batch[0] = batch[0] - qty_to_pop
|
||||
qty_to_pop = 0
|
||||
else:
|
||||
# qty found in current batch
|
||||
# consume it and exit
|
||||
batch[0] = batch[0] - qty_to_pop
|
||||
qty_to_pop = 0
|
||||
|
||||
stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue))
|
||||
stock_qty = sum((flt(batch[0]) for batch in stock_queue))
|
||||
stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.stock_queue))
|
||||
stock_qty = sum((flt(batch[0]) for batch in self.stock_queue))
|
||||
|
||||
valuation_rate = (stock_value / flt(stock_qty)) if stock_qty else 0
|
||||
self.valuation_rate = (stock_value / flt(stock_qty)) if stock_qty else 0
|
||||
|
||||
return abs(valuation_rate)
|
||||
def get_sle_before_datetime(self):
|
||||
"""get previous stock ledger entry before current time-bucket"""
|
||||
return get_stock_ledger_entries(self.args, "<", "desc", "limit 1", for_update=False)
|
||||
|
||||
def _raise_exceptions(args, verbose=1):
|
||||
deficiency = min(e["diff"] for e in _exceptions)
|
||||
msg = _("Negative Stock Error ({6}) for Item {0} in Warehouse {1} on {2} {3} in {4} {5}").format(args["item_code"],
|
||||
args.get("warehouse"), _exceptions[0]["posting_date"], _exceptions[0]["posting_time"],
|
||||
_(_exceptions[0]["voucher_type"]), _exceptions[0]["voucher_no"], deficiency)
|
||||
if verbose:
|
||||
frappe.throw(msg, NegativeStockError)
|
||||
else:
|
||||
raise NegativeStockError, msg
|
||||
def get_sle_after_datetime(self):
|
||||
"""get Stock Ledger Entries after a particular datetime, for reposting"""
|
||||
return get_stock_ledger_entries(self.previous_sle or self.args, ">", "asc", for_update=True)
|
||||
|
||||
def raise_exceptions(self):
|
||||
deficiency = min(e["diff"] for e in self.exceptions)
|
||||
msg = _("Negative Stock Error ({6}) for Item {0} in Warehouse {1} on {2} {3} in {4} {5}").format(self.item_code,
|
||||
self.warehouse, self.exceptions[0]["posting_date"], self.exceptions[0]["posting_time"],
|
||||
_(self.exceptions[0]["voucher_type"]), self.exceptions[0]["voucher_no"], deficiency)
|
||||
if self.verbose:
|
||||
frappe.throw(msg, NegativeStockError)
|
||||
else:
|
||||
raise NegativeStockError, msg
|
||||
|
||||
def get_previous_sle(args, for_update=False):
|
||||
"""
|
||||
@ -346,13 +324,34 @@ def get_previous_sle(args, for_update=False):
|
||||
"sle": "name of reference Stock Ledger Entry"
|
||||
}
|
||||
"""
|
||||
if not args.get("sle"): args["sle"] = ""
|
||||
|
||||
sle = get_stock_ledger_entries(args, ["name != %(sle)s",
|
||||
"timestamp(posting_date, posting_time) <= timestamp(%(posting_date)s, %(posting_time)s)"],
|
||||
"desc", "limit 1", for_update=for_update)
|
||||
args["name"] = args.get("sle", None) or ""
|
||||
sle = get_stock_ledger_entries(args, "<=", "desc", "limit 1", for_update=for_update)
|
||||
return sle and sle[0] or {}
|
||||
|
||||
def get_stock_ledger_entries(previous_sle, operator=None, order="desc", limit=None, for_update=False):
|
||||
"""get stock ledger entries filtered by specific posting datetime conditions"""
|
||||
conditions = "timestamp(posting_date, posting_time) {0} timestamp(%(posting_date)s, %(posting_time)s)".format(operator)
|
||||
if not previous_sle.get("posting_date"):
|
||||
previous_sle["posting_date"] = "1900-01-01"
|
||||
if not previous_sle.get("posting_time"):
|
||||
previous_sle["posting_time"] = "00:00"
|
||||
|
||||
if operator in (">", "<=") and previous_sle.get("name"):
|
||||
conditions += " and name!=%(name)s"
|
||||
|
||||
return frappe.db.sql("""select *, timestamp(posting_date, posting_time) as "timestamp" from `tabStock Ledger Entry`
|
||||
where item_code = %%(item_code)s
|
||||
and warehouse = %%(warehouse)s
|
||||
and ifnull(is_cancelled, 'No')='No'
|
||||
and %(conditions)s
|
||||
order by timestamp(posting_date, posting_time) %(order)s, name %(order)s
|
||||
%(limit)s %(for_update)s""" % {
|
||||
"conditions": conditions,
|
||||
"limit": limit or "",
|
||||
"for_update": for_update and "for update" or "",
|
||||
"order": order
|
||||
}, previous_sle, as_dict=1)
|
||||
|
||||
def get_valuation_rate(item_code, warehouse, allow_zero_rate=False):
|
||||
last_valuation_rate = frappe.db.sql("""select valuation_rate
|
||||
from `tabStock Ledger Entry`
|
||||
|
@ -14,9 +14,9 @@
|
||||
<br>{%= doc.item_name %}{% } %}
|
||||
{% if(doc.item_name != doc.description) { %}
|
||||
<p>{%= doc.description %}</p>{% } %}
|
||||
<div>
|
||||
<p>
|
||||
{% if(doc.sales_order || doc.against_sales_order) { %}
|
||||
<span class="label label-default"
|
||||
<span class="label label-default" style="margin-right: 10px;"
|
||||
title="{%= __("Sales Order") %}">
|
||||
<i class="icon-file"></i>
|
||||
{%= doc.sales_order || doc.against_sales_order %}
|
||||
@ -36,22 +36,28 @@
|
||||
}
|
||||
}
|
||||
%}
|
||||
<span style="margin-left: 10px;"
|
||||
<span style="margin-right: 10px;"
|
||||
title="{%= title %}">
|
||||
<span class="label {%= label_class %}">
|
||||
{%= doc.warehouse %}
|
||||
</span>
|
||||
</span>
|
||||
{% } %}
|
||||
</div>
|
||||
</p>
|
||||
{% include "templates/form_grid/includes/visible_cols.html" %}
|
||||
{% if(doc.schedule_date) { %}
|
||||
<div><span title="{%= __("Reqd By Date") %}" class="label {%=
|
||||
(frappe.datetime.get_diff(doc.schedule_date) < 1
|
||||
&& doc.received_qty < doc.qty)
|
||||
? "label-danger" : "label-default" %}">
|
||||
{%= doc.get_formatted("schedule_date") %}</span>
|
||||
</div>
|
||||
<div class="clearfix"></div>
|
||||
<div>
|
||||
{% if(frappe.datetime.get_diff(doc.schedule_date) < 1 && doc.received_qty < doc.qty) { %}
|
||||
<span class="text-danger">
|
||||
{%= __("Overdue on {0}", [doc.get_formatted("schedule_date")]) %}
|
||||
</span>
|
||||
{% } else { %}
|
||||
<span class="text-muted">
|
||||
{%= __("Due on {0}", [doc.get_formatted("schedule_date")]) %}
|
||||
</span>
|
||||
{% } %}
|
||||
</div>
|
||||
{% } %}
|
||||
</div>
|
||||
|
||||
|
@ -13,24 +13,7 @@
|
||||
<br>{%= doc.item_name %}{% } %}
|
||||
{% if(doc.item_name != doc.description) { %}
|
||||
<p>{%= doc.description %}</p>{% } %}
|
||||
{% include "templates/form_grid/includes/visible_cols.html" %}
|
||||
{% if(doc.schedule_date) { %}
|
||||
<br><span title="{%= __("Reqd By Date") %}" class="label {%=
|
||||
(frappe.datetime.get_diff(doc.schedule_date, frappe.datetime.get_today()) < 0
|
||||
&& doc.ordered_qty < doc.qty)
|
||||
? "label-danger" : "label-default" %}">
|
||||
{%= doc.get_formatted("schedule_date") %}</span>
|
||||
{% } %}
|
||||
</div>
|
||||
|
||||
<!-- qty -->
|
||||
<div class="col-sm-3 text-right">
|
||||
{%= doc.get_formatted("qty") %}
|
||||
<small>{%= doc.uom || doc.stock_uom %}</small>
|
||||
{% var completed =
|
||||
100 - cint((doc.qty - cint(doc.ordered_qty)) * 100 / doc.qty),
|
||||
title = __("Ordered"); %}
|
||||
{% include "templates/form_grid/includes/progress.html" %}
|
||||
<div>
|
||||
{% if(doc.warehouse) { %}
|
||||
<div style="overflow:hidden; min-height: 25px;
|
||||
white-space:nowrap; text-overflow: ellipsis;"
|
||||
@ -40,6 +23,26 @@
|
||||
</span>
|
||||
</div>
|
||||
{% } %}
|
||||
{% if(doc.schedule_date) { %}
|
||||
<span title="{%= __("Reqd By Date") %}" class="label {%=
|
||||
(frappe.datetime.get_diff(doc.schedule_date, frappe.datetime.get_today()) < 0
|
||||
&& doc.ordered_qty < doc.qty)
|
||||
? "label-danger" : "label-default" %}">
|
||||
{%= doc.get_formatted("schedule_date") %}</span>
|
||||
{% } %}
|
||||
</div>
|
||||
{% include "templates/form_grid/includes/visible_cols.html" %}
|
||||
</div>
|
||||
|
||||
<!-- qty -->
|
||||
<div class="col-sm-3 text-right">
|
||||
{%= doc.get_formatted("qty") %}
|
||||
<div class="small">{%= doc.uom || doc.stock_uom %}</div>
|
||||
{% if(doc.qty == doc.ordered_qty) { %}
|
||||
<div class="small text-success">{%= __("Ordered") %}</div>
|
||||
{% } else { %}
|
||||
<div class="small text-muted">{%= __("{0} Ordered", doc.ordered_qty) %}</div>
|
||||
{% } %}
|
||||
</div>
|
||||
</div>
|
||||
{% } %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user