blob: e8a84c2ab1d7d1c5644523d6912264e31f3eaafb [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Anand Doshi56a31652013-11-29 19:15:26 +05303from __future__ import unicode_literals
Nabin Hait902e8602013-01-08 18:29:24 +05304
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
Rushabh Mehta9f0d6252014-04-14 19:20:45 +05306from frappe import _
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05307from frappe.utils import cint, flt, cstr, now
Rushabh Mehta1f847992013-12-12 19:12:19 +05308from erpnext.stock.utils import get_valuation_method
Nabin Hait26d46552013-01-09 15:23:05 +05309import json
Nabin Hait902e8602013-01-08 18:29:24 +053010
11# future reposting
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053012class NegativeStockError(frappe.ValidationError): pass
Nabin Hait902e8602013-01-08 18:29:24 +053013
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053014_exceptions = frappe.local('stockledger_exceptions')
Pratik Vyas16371b72013-09-18 18:31:03 +053015# _exceptions = []
Anand Doshi5b004ff2013-09-25 19:55:41 +053016
Nabin Hait74c281c2013-08-19 16:17:18 +053017def make_sl_entries(sl_entries, is_amended=None):
Nabin Haitca775742013-09-26 16:16:44 +053018 if sl_entries:
Rushabh Mehta1f847992013-12-12 19:12:19 +053019 from erpnext.stock.utils import update_bin
Nabin Haitdc82d4f2014-04-07 12:02:57 +053020
Nabin Haitca775742013-09-26 16:16:44 +053021 cancel = True if sl_entries[0].get("is_cancelled") == "Yes" else False
22 if cancel:
23 set_as_cancel(sl_entries[0].get('voucher_no'), sl_entries[0].get('voucher_type'))
Nabin Haitdc82d4f2014-04-07 12:02:57 +053024
Nabin Haitca775742013-09-26 16:16:44 +053025 for sle in sl_entries:
26 sle_id = None
27 if sle.get('is_cancelled') == 'Yes':
28 sle['actual_qty'] = -flt(sle['actual_qty'])
Nabin Haitdc82d4f2014-04-07 12:02:57 +053029
Nabin Haitbfa7f172014-10-07 12:17:31 +053030 if sle.get("actual_qty") or sle.voucher_type=="Stock Reconciliation":
Nabin Haitca775742013-09-26 16:16:44 +053031 sle_id = make_entry(sle)
Nabin Haitdc82d4f2014-04-07 12:02:57 +053032
Nabin Haitca775742013-09-26 16:16:44 +053033 args = sle.copy()
34 args.update({
35 "sle_id": sle_id,
36 "is_amended": is_amended
37 })
38 update_bin(args)
Nabin Haitadeb9762014-10-06 11:53:52 +053039
Nabin Haitca775742013-09-26 16:16:44 +053040 if cancel:
Nabin Haitadeb9762014-10-06 11:53:52 +053041 delete_cancelled_entry(sl_entries[0].get('voucher_type'), sl_entries[0].get('voucher_no'))
Nabin Haitdc82d4f2014-04-07 12:02:57 +053042
Nabin Hait9653f602013-08-20 15:37:33 +053043def set_as_cancel(voucher_type, voucher_no):
Anand Doshie9baaa62014-02-26 12:35:33 +053044 frappe.db.sql("""update `tabStock Ledger Entry` set is_cancelled='Yes',
Nabin Hait9653f602013-08-20 15:37:33 +053045 modified=%s, modified_by=%s
Nabin Haitdc82d4f2014-04-07 12:02:57 +053046 where voucher_no=%s and voucher_type=%s""",
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053047 (now(), frappe.session.user, voucher_type, voucher_no))
Nabin Haitdc82d4f2014-04-07 12:02:57 +053048
Nabin Hait74c281c2013-08-19 16:17:18 +053049def make_entry(args):
50 args.update({"doctype": "Stock Ledger Entry"})
Rushabh Mehtaa504f062014-04-04 12:16:26 +053051 sle = frappe.get_doc(args)
Nabin Hait74c281c2013-08-19 16:17:18 +053052 sle.ignore_permissions = 1
53 sle.insert()
Nabin Haitaeba24e2013-08-23 15:17:36 +053054 sle.submit()
Anand Doshif78d1ae2014-03-28 13:55:00 +053055 return sle.name
Nabin Haitdc82d4f2014-04-07 12:02:57 +053056
Nabin Hait9653f602013-08-20 15:37:33 +053057def delete_cancelled_entry(voucher_type, voucher_no):
Nabin Haitdc82d4f2014-04-07 12:02:57 +053058 frappe.db.sql("""delete from `tabStock Ledger Entry`
Nabin Hait9653f602013-08-20 15:37:33 +053059 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Nabin Hait74c281c2013-08-19 16:17:18 +053060
Nabin Hait902e8602013-01-08 18:29:24 +053061def update_entries_after(args, verbose=1):
62 """
Nabin Haitdc82d4f2014-04-07 12:02:57 +053063 update valution rate and qty after transaction
Nabin Hait902e8602013-01-08 18:29:24 +053064 from the current time-bucket onwards
Nabin Haitdc82d4f2014-04-07 12:02:57 +053065
Nabin Hait902e8602013-01-08 18:29:24 +053066 args = {
67 "item_code": "ABC",
68 "warehouse": "XYZ",
69 "posting_date": "2012-12-12",
70 "posting_time": "12:00"
71 }
72 """
Pratik Vyas16371b72013-09-18 18:31:03 +053073 if not _exceptions:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053074 frappe.local.stockledger_exceptions = []
Nabin Haitdc82d4f2014-04-07 12:02:57 +053075
Nabin Hait902e8602013-01-08 18:29:24 +053076 previous_sle = get_sle_before_datetime(args)
Nabin Haitdc82d4f2014-04-07 12:02:57 +053077
Nabin Hait902e8602013-01-08 18:29:24 +053078 qty_after_transaction = flt(previous_sle.get("qty_after_transaction"))
79 valuation_rate = flt(previous_sle.get("valuation_rate"))
80 stock_queue = json.loads(previous_sle.get("stock_queue") or "[]")
Nabin Hait27994c22013-08-26 16:53:30 +053081 stock_value = flt(previous_sle.get("stock_value"))
82 prev_stock_value = flt(previous_sle.get("stock_value"))
Nabin Haitdc82d4f2014-04-07 12:02:57 +053083
Nabin Hait902e8602013-01-08 18:29:24 +053084 entries_to_fix = get_sle_after_datetime(previous_sle or \
Anand Doshi4dc7caa2013-01-11 11:44:49 +053085 {"item_code": args["item_code"], "warehouse": args["warehouse"]}, for_update=True)
Nabin Hait902e8602013-01-08 18:29:24 +053086 valuation_method = get_valuation_method(args["item_code"])
Nabin Haitaeba24e2013-08-23 15:17:36 +053087 stock_value_difference = 0.0
Nabin Haitbb777562013-08-29 18:19:37 +053088
Nabin Hait902e8602013-01-08 18:29:24 +053089 for sle in entries_to_fix:
Anand Doshie9baaa62014-02-26 12:35:33 +053090 if sle.serial_no or not cint(frappe.db.get_default("allow_negative_stock")):
Nabin Haitdc82d4f2014-04-07 12:02:57 +053091 # validate negative stock for serialized items, fifo valuation
Nabin Hait902e8602013-01-08 18:29:24 +053092 # or when negative stock is not allowed for moving average
93 if not validate_negative_stock(qty_after_transaction, sle):
94 qty_after_transaction += flt(sle.actual_qty)
95 continue
Nabin Haitbb777562013-08-29 18:19:37 +053096
Nabin Haitb96c0142014-10-07 11:25:04 +053097
Anand Doshi1b531862013-01-10 19:29:51 +053098 if sle.serial_no:
Nabin Hait1b4f56c2013-01-17 17:01:51 +053099 valuation_rate = get_serialized_values(qty_after_transaction, sle, valuation_rate)
Nabin Haitb96c0142014-10-07 11:25:04 +0530100 qty_after_transaction += flt(sle.actual_qty)
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530101
Nabin Haitb96c0142014-10-07 11:25:04 +0530102 else:
103 if sle.voucher_type=="Stock Reconciliation":
104 valuation_rate = sle.valuation_rate
105 qty_after_transaction = sle.qty_after_transaction
106 stock_queue = [[qty_after_transaction, valuation_rate]]
107 else:
108 if valuation_method == "Moving Average":
109 valuation_rate = get_moving_average_values(qty_after_transaction, sle, valuation_rate)
110 else:
111 valuation_rate = get_fifo_values(qty_after_transaction, sle, stock_queue)
112
113 qty_after_transaction += flt(sle.actual_qty)
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530114
Nabin Hait902e8602013-01-08 18:29:24 +0530115 # get stock value
Anand Doshi1b531862013-01-10 19:29:51 +0530116 if sle.serial_no:
Nabin Hait902e8602013-01-08 18:29:24 +0530117 stock_value = qty_after_transaction * valuation_rate
118 elif valuation_method == "Moving Average":
119 stock_value = (qty_after_transaction > 0) and \
120 (qty_after_transaction * valuation_rate) or 0
121 else:
122 stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue))
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530123
Rushabh Mehta54047782013-12-26 11:07:46 +0530124 # rounding as per precision
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530125 from frappe.model.meta import get_field_precision
Rushabh Mehtae88bc8b2014-03-27 17:51:41 +0530126 meta = frappe.get_meta("Stock Ledger Entry")
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530127
128 stock_value = flt(stock_value, get_field_precision(meta.get_field("stock_value"),
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530129 frappe._dict({"fields": sle})))
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530130
Nabin Haitaeba24e2013-08-23 15:17:36 +0530131 stock_value_difference = stock_value - prev_stock_value
132 prev_stock_value = stock_value
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530133
Nabin Hait902e8602013-01-08 18:29:24 +0530134 # update current sle
Anand Doshie9baaa62014-02-26 12:35:33 +0530135 frappe.db.sql("""update `tabStock Ledger Entry`
Anand Doshi1b531862013-01-10 19:29:51 +0530136 set qty_after_transaction=%s, valuation_rate=%s, stock_queue=%s,
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530137 stock_value=%s, stock_value_difference=%s where name=%s""",
Anand Doshi1b531862013-01-10 19:29:51 +0530138 (qty_after_transaction, valuation_rate,
Nabin Haitaeba24e2013-08-23 15:17:36 +0530139 json.dumps(stock_queue), stock_value, stock_value_difference, sle.name))
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530140
Nabin Hait902e8602013-01-08 18:29:24 +0530141 if _exceptions:
Nabin Hait9514d172013-01-10 10:40:37 +0530142 _raise_exceptions(args, verbose)
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530143
Nabin Hait902e8602013-01-08 18:29:24 +0530144 # update bin
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530145 if not frappe.db.exists({"doctype": "Bin", "item_code": args["item_code"],
Nabin Haitac53b112013-01-11 19:25:46 +0530146 "warehouse": args["warehouse"]}):
Rushabh Mehtaa504f062014-04-04 12:16:26 +0530147 bin_wrapper = frappe.get_doc({
Nabin Haitac53b112013-01-11 19:25:46 +0530148 "doctype": "Bin",
149 "item_code": args["item_code"],
150 "warehouse": args["warehouse"],
Rushabh Mehtaa504f062014-04-04 12:16:26 +0530151 })
Anand Doshic313d662013-01-14 15:46:17 +0530152 bin_wrapper.ignore_permissions = 1
153 bin_wrapper.insert()
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530154
Anand Doshie9baaa62014-02-26 12:35:33 +0530155 frappe.db.sql("""update `tabBin` set valuation_rate=%s, actual_qty=%s,
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530156 stock_value=%s,
Nabin Hait902e8602013-01-08 18:29:24 +0530157 projected_qty = (actual_qty + indented_qty + ordered_qty + planned_qty - reserved_qty)
158 where item_code=%s and warehouse=%s""", (valuation_rate, qty_after_transaction,
159 stock_value, args["item_code"], args["warehouse"]))
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530160
Anand Doshi4dc7caa2013-01-11 11:44:49 +0530161def get_sle_before_datetime(args, for_update=False):
Nabin Hait902e8602013-01-08 18:29:24 +0530162 """
163 get previous stock ledger entry before current time-bucket
164
165 Details:
166 get the last sle before the current time-bucket, so that all values
167 are reposted from the current time-bucket onwards.
168 this is necessary because at the time of cancellation, there may be
169 entries between the cancelled entries in the same time-bucket
170 """
171 sle = get_stock_ledger_entries(args,
Nabin Hait26d46552013-01-09 15:23:05 +0530172 ["timestamp(posting_date, posting_time) < timestamp(%(posting_date)s, %(posting_time)s)"],
Anand Doshi4dc7caa2013-01-11 11:44:49 +0530173 "desc", "limit 1", for_update=for_update)
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530174
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530175 return sle and sle[0] or frappe._dict()
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530176
Anand Doshi4dc7caa2013-01-11 11:44:49 +0530177def get_sle_after_datetime(args, for_update=False):
Nabin Hait902e8602013-01-08 18:29:24 +0530178 """get Stock Ledger Entries after a particular datetime, for reposting"""
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530179 # NOTE: using for update of
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530180 conditions = ["timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s)"]
181
182 # Excluding name: Workaround for MariaDB timestamp() floating microsecond issue
183 if args.get("name"):
184 conditions.append("name!=%(name)s")
185
186 return get_stock_ledger_entries(args, conditions, "asc", for_update=for_update)
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530187
Anand Doshi4dc7caa2013-01-11 11:44:49 +0530188def get_stock_ledger_entries(args, conditions=None, order="desc", limit=None, for_update=False):
Nabin Hait902e8602013-01-08 18:29:24 +0530189 """get stock ledger entries filtered by specific posting datetime conditions"""
190 if not args.get("posting_date"):
191 args["posting_date"] = "1900-01-01"
192 if not args.get("posting_time"):
Anand Doshi71bed312013-03-13 12:57:04 +0530193 args["posting_time"] = "00:00"
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530194
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530195 return frappe.db.sql("""select *, timestamp(posting_date, posting_time) as "timestamp" from `tabStock Ledger Entry`
Nabin Hait902e8602013-01-08 18:29:24 +0530196 where item_code = %%(item_code)s
197 and warehouse = %%(warehouse)s
Nabin Haitbb777562013-08-29 18:19:37 +0530198 and ifnull(is_cancelled, 'No')='No'
Nabin Hait902e8602013-01-08 18:29:24 +0530199 %(conditions)s
Nabin Hait9514d172013-01-10 10:40:37 +0530200 order by timestamp(posting_date, posting_time) %(order)s, name %(order)s
Anand Doshi4dc7caa2013-01-11 11:44:49 +0530201 %(limit)s %(for_update)s""" % {
Nabin Hait902e8602013-01-08 18:29:24 +0530202 "conditions": conditions and ("and " + " and ".join(conditions)) or "",
Nabin Hait9514d172013-01-10 10:40:37 +0530203 "limit": limit or "",
Anand Doshi4dc7caa2013-01-11 11:44:49 +0530204 "for_update": for_update and "for update" or "",
Nabin Hait9514d172013-01-10 10:40:37 +0530205 "order": order
Nabin Hait902e8602013-01-08 18:29:24 +0530206 }, args, as_dict=1)
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530207
Nabin Hait902e8602013-01-08 18:29:24 +0530208def validate_negative_stock(qty_after_transaction, sle):
209 """
210 validate negative stock for entries current datetime onwards
211 will not consider cancelled entries
212 """
213 diff = qty_after_transaction + flt(sle.actual_qty)
Pratik Vyas16371b72013-09-18 18:31:03 +0530214
215 if not _exceptions:
Rushabh Mehta793ba6b2014-02-14 15:47:51 +0530216 frappe.local.stockledger_exceptions = []
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530217
Nabin Hait902e8602013-01-08 18:29:24 +0530218 if diff < 0 and abs(diff) > 0.0001:
219 # negative stock!
Nabin Hait902e8602013-01-08 18:29:24 +0530220 exc = sle.copy().update({"diff": diff})
221 _exceptions.append(exc)
222 return False
223 else:
224 return True
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530225
Nabin Hait902e8602013-01-08 18:29:24 +0530226def get_serialized_values(qty_after_transaction, sle, valuation_rate):
227 incoming_rate = flt(sle.incoming_rate)
228 actual_qty = flt(sle.actual_qty)
Anand Doshi1b531862013-01-10 19:29:51 +0530229 serial_no = cstr(sle.serial_no).split("\n")
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530230
Nabin Hait902e8602013-01-08 18:29:24 +0530231 if incoming_rate < 0:
232 # wrong incoming rate
233 incoming_rate = valuation_rate
234 elif incoming_rate == 0 or flt(sle.actual_qty) < 0:
235 # In case of delivery/stock issue, get average purchase rate
236 # of serial nos of current entry
Anand Doshie9baaa62014-02-26 12:35:33 +0530237 incoming_rate = flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0))
Anand Doshi1b531862013-01-10 19:29:51 +0530238 from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))),
239 tuple(serial_no))[0][0])
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530240
Nabin Hait902e8602013-01-08 18:29:24 +0530241 if incoming_rate and not valuation_rate:
242 valuation_rate = incoming_rate
243 else:
244 new_stock_qty = qty_after_transaction + actual_qty
245 if new_stock_qty > 0:
246 new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate
247 if new_stock_value > 0:
248 # calculate new valuation rate only if stock value is positive
249 # else it remains the same as that of previous entry
250 valuation_rate = new_stock_value / new_stock_qty
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530251
Nabin Hait914c6df2013-01-14 13:15:42 +0530252 return valuation_rate
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530253
Nabin Hait902e8602013-01-08 18:29:24 +0530254def get_moving_average_values(qty_after_transaction, sle, valuation_rate):
255 incoming_rate = flt(sle.incoming_rate)
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530256 actual_qty = flt(sle.actual_qty)
257
Anand Doshi1b531862013-01-10 19:29:51 +0530258 if not incoming_rate:
Nabin Hait902e8602013-01-08 18:29:24 +0530259 # In case of delivery/stock issue in_rate = 0 or wrong incoming rate
260 incoming_rate = valuation_rate
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530261
Anand Doshi1b531862013-01-10 19:29:51 +0530262 elif qty_after_transaction < 0:
263 # if negative stock, take current valuation rate as incoming rate
264 valuation_rate = incoming_rate
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530265
Nabin Hait902e8602013-01-08 18:29:24 +0530266 new_stock_qty = qty_after_transaction + actual_qty
267 new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530268
Anand Doshi1b531862013-01-10 19:29:51 +0530269 if new_stock_qty > 0 and new_stock_value > 0:
Nabin Hait902e8602013-01-08 18:29:24 +0530270 valuation_rate = new_stock_value / flt(new_stock_qty)
271 elif new_stock_qty <= 0:
272 valuation_rate = 0.0
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530273
Anand Doshi1b531862013-01-10 19:29:51 +0530274 # NOTE: val_rate is same as previous entry if new stock value is negative
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530275
Nabin Hait914c6df2013-01-14 13:15:42 +0530276 return valuation_rate
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530277
Nabin Hait902e8602013-01-08 18:29:24 +0530278def get_fifo_values(qty_after_transaction, sle, stock_queue):
279 incoming_rate = flt(sle.incoming_rate)
280 actual_qty = flt(sle.actual_qty)
Nabin Hait902e8602013-01-08 18:29:24 +0530281 if not stock_queue:
282 stock_queue.append([0, 0])
Nabin Hait9514d172013-01-10 10:40:37 +0530283
Nabin Hait902e8602013-01-08 18:29:24 +0530284 if actual_qty > 0:
285 if stock_queue[-1][0] > 0:
286 stock_queue.append([actual_qty, incoming_rate])
287 else:
288 qty = stock_queue[-1][0] + actual_qty
289 stock_queue[-1] = [qty, qty > 0 and incoming_rate or 0]
290 else:
291 incoming_cost = 0
292 qty_to_pop = abs(actual_qty)
293 while qty_to_pop:
Nabin Hait9514d172013-01-10 10:40:37 +0530294 if not stock_queue:
295 stock_queue.append([0, 0])
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530296
Nabin Hait902e8602013-01-08 18:29:24 +0530297 batch = stock_queue[0]
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530298
Nabin Hait902e8602013-01-08 18:29:24 +0530299 if 0 < batch[0] <= qty_to_pop:
300 # if batch qty > 0
301 # not enough or exactly same qty in current batch, clear batch
302 incoming_cost += flt(batch[0]) * flt(batch[1])
303 qty_to_pop -= batch[0]
304 stock_queue.pop(0)
305 else:
306 # all from current batch
307 incoming_cost += flt(qty_to_pop) * flt(batch[1])
308 batch[0] -= qty_to_pop
309 qty_to_pop = 0
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530310
Nabin Hait902e8602013-01-08 18:29:24 +0530311 stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue))
312 stock_qty = sum((flt(batch[0]) for batch in stock_queue))
313
314 valuation_rate = stock_qty and (stock_value / flt(stock_qty)) or 0
Nabin Hait9514d172013-01-10 10:40:37 +0530315
Nabin Hait914c6df2013-01-14 13:15:42 +0530316 return valuation_rate
Nabin Hait902e8602013-01-08 18:29:24 +0530317
Nabin Hait9514d172013-01-10 10:40:37 +0530318def _raise_exceptions(args, verbose=1):
Nabin Hait902e8602013-01-08 18:29:24 +0530319 deficiency = min(e["diff"] for e in _exceptions)
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530320 msg = _("Negative Stock Error ({6}) for Item {0} in Warehouse {1} on {2} {3} in {4} {5}").format(args["item_code"],
321 args.get("warehouse"), _exceptions[0]["posting_date"], _exceptions[0]["posting_time"],
322 _(_exceptions[0]["voucher_type"]), _exceptions[0]["voucher_no"], deficiency)
Nabin Hait902e8602013-01-08 18:29:24 +0530323 if verbose:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +0530324 frappe.throw(msg, NegativeStockError)
Nabin Hait902e8602013-01-08 18:29:24 +0530325 else:
Rushabh Mehta4c17f942013-08-12 14:18:09 +0530326 raise NegativeStockError, msg
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530327
Anand Doshi4dc7caa2013-01-11 11:44:49 +0530328def get_previous_sle(args, for_update=False):
Anand Doshi1b531862013-01-10 19:29:51 +0530329 """
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530330 get the last sle on or before the current time-bucket,
Anand Doshi1b531862013-01-10 19:29:51 +0530331 to get actual qty before transaction, this function
332 is called from various transaction like stock entry, reco etc
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530333
Anand Doshi1b531862013-01-10 19:29:51 +0530334 args = {
335 "item_code": "ABC",
336 "warehouse": "XYZ",
337 "posting_date": "2012-12-12",
338 "posting_time": "12:00",
339 "sle": "name of reference Stock Ledger Entry"
340 }
341 """
342 if not args.get("sle"): args["sle"] = ""
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530343
Anand Doshi1b531862013-01-10 19:29:51 +0530344 sle = get_stock_ledger_entries(args, ["name != %(sle)s",
345 "timestamp(posting_date, posting_time) <= timestamp(%(posting_date)s, %(posting_time)s)"],
Anand Doshi4dc7caa2013-01-11 11:44:49 +0530346 "desc", "limit 1", for_update=for_update)
Pratik Vyas16371b72013-09-18 18:31:03 +0530347 return sle and sle[0] or {}