Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 3 | |
| 4 | import webnotes |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 5 | from webnotes import msgprint |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 6 | from webnotes.utils import cint, flt, cstr, now |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 7 | from stock.utils import get_valuation_method |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 8 | import json |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 9 | |
| 10 | # future reposting |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 11 | class NegativeStockError(webnotes.ValidationError): pass |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 12 | |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 13 | def make_sl_entries(sl_entries, is_amended=None): |
| 14 | from stock.utils import update_bin |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 15 | |
| 16 | cancel = True if sl_entries[0].get("is_cancelled") == "Yes" else False |
| 17 | if cancel: |
| 18 | set_as_cancel(sl_entries[0].get('voucher_no'), sl_entries[0].get('voucher_type')) |
| 19 | |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 20 | for sle in sl_entries: |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 21 | sle_id = None |
| 22 | if sle.get('is_cancelled') == 'Yes': |
| 23 | sle['actual_qty'] = -flt(sle['actual_qty']) |
| 24 | |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 25 | if sle.get("actual_qty"): |
| 26 | sle_id = make_entry(sle) |
| 27 | |
| 28 | args = sle.copy() |
| 29 | args.update({ |
| 30 | "sle_id": sle_id, |
| 31 | "is_amended": is_amended |
| 32 | }) |
| 33 | update_bin(args) |
| 34 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 35 | if cancel: |
| 36 | delete_cancelled_entry(sl_entries[0].get('voucher_no'), sl_entries[0].get('voucher_type')) |
| 37 | |
| 38 | def set_as_cancel(voucher_type, voucher_no): |
| 39 | webnotes.conn.sql("""update `tabStock Ledger Entry` set is_cancelled='Yes', |
| 40 | modified=%s, modified_by=%s |
| 41 | where voucher_no=%s and voucher_type=%s""", |
| 42 | (now(), webnotes.session.user, voucher_type, voucher_no)) |
| 43 | |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 44 | def make_entry(args): |
| 45 | args.update({"doctype": "Stock Ledger Entry"}) |
| 46 | sle = webnotes.bean([args]) |
| 47 | sle.ignore_permissions = 1 |
| 48 | sle.insert() |
Nabin Hait | c13c193 | 2013-08-20 12:28:44 +0530 | [diff] [blame] | 49 | # sle.submit() |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 50 | return sle.doc.name |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 51 | |
| 52 | def delete_cancelled_entry(voucher_type, voucher_no): |
| 53 | webnotes.conn.sql("""delete from `tabStock Ledger Entry` |
| 54 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 55 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 56 | _exceptions = [] |
| 57 | def update_entries_after(args, verbose=1): |
| 58 | """ |
| 59 | update valution rate and qty after transaction |
| 60 | from the current time-bucket onwards |
| 61 | |
| 62 | args = { |
| 63 | "item_code": "ABC", |
| 64 | "warehouse": "XYZ", |
| 65 | "posting_date": "2012-12-12", |
| 66 | "posting_time": "12:00" |
| 67 | } |
| 68 | """ |
Nabin Hait | f4591ec | 2013-05-23 19:07:10 +0530 | [diff] [blame] | 69 | global _exceptions |
| 70 | _exceptions = [] |
| 71 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 72 | previous_sle = get_sle_before_datetime(args) |
Anand Doshi | 71bed31 | 2013-03-13 12:57:04 +0530 | [diff] [blame] | 73 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 74 | qty_after_transaction = flt(previous_sle.get("qty_after_transaction")) |
| 75 | valuation_rate = flt(previous_sle.get("valuation_rate")) |
| 76 | stock_queue = json.loads(previous_sle.get("stock_queue") or "[]") |
Nabin Hait | 469ee71 | 2013-08-07 12:33:37 +0530 | [diff] [blame] | 77 | stock_value = flt(previous_sle.get("stock_value")) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 78 | |
| 79 | entries_to_fix = get_sle_after_datetime(previous_sle or \ |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 80 | {"item_code": args["item_code"], "warehouse": args["warehouse"]}, for_update=True) |
Nabin Hait | 469ee71 | 2013-08-07 12:33:37 +0530 | [diff] [blame] | 81 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 82 | valuation_method = get_valuation_method(args["item_code"]) |
| 83 | |
| 84 | for sle in entries_to_fix: |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 85 | if sle.serial_no or not cint(webnotes.conn.get_default("allow_negative_stock")): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 86 | # validate negative stock for serialized items, fifo valuation |
| 87 | # or when negative stock is not allowed for moving average |
| 88 | if not validate_negative_stock(qty_after_transaction, sle): |
| 89 | qty_after_transaction += flt(sle.actual_qty) |
| 90 | continue |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 91 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 92 | if sle.serial_no: |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 93 | valuation_rate = get_serialized_values(qty_after_transaction, sle, valuation_rate) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 94 | elif valuation_method == "Moving Average": |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 95 | valuation_rate = get_moving_average_values(qty_after_transaction, sle, valuation_rate) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 96 | else: |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 97 | valuation_rate = get_fifo_values(qty_after_transaction, sle, stock_queue) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 98 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 99 | qty_after_transaction += flt(sle.actual_qty) |
| 100 | |
| 101 | # get stock value |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 102 | if sle.serial_no: |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 103 | stock_value = qty_after_transaction * valuation_rate |
| 104 | elif valuation_method == "Moving Average": |
| 105 | stock_value = (qty_after_transaction > 0) and \ |
| 106 | (qty_after_transaction * valuation_rate) or 0 |
| 107 | else: |
| 108 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue)) |
Nabin Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 109 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 110 | # update current sle |
| 111 | webnotes.conn.sql("""update `tabStock Ledger Entry` |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 112 | set qty_after_transaction=%s, valuation_rate=%s, stock_queue=%s, |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 113 | stock_value=%s where name=%s""", |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 114 | (qty_after_transaction, valuation_rate, |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 115 | json.dumps(stock_queue), stock_value, sle.name)) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 116 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 117 | if _exceptions: |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 118 | _raise_exceptions(args, verbose) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 119 | |
| 120 | # update bin |
Nabin Hait | ac53b11 | 2013-01-11 19:25:46 +0530 | [diff] [blame] | 121 | if not webnotes.conn.exists({"doctype": "Bin", "item_code": args["item_code"], |
| 122 | "warehouse": args["warehouse"]}): |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 123 | bin_wrapper = webnotes.bean([{ |
Nabin Hait | ac53b11 | 2013-01-11 19:25:46 +0530 | [diff] [blame] | 124 | "doctype": "Bin", |
| 125 | "item_code": args["item_code"], |
| 126 | "warehouse": args["warehouse"], |
Anand Doshi | c313d66 | 2013-01-14 15:46:17 +0530 | [diff] [blame] | 127 | }]) |
| 128 | bin_wrapper.ignore_permissions = 1 |
| 129 | bin_wrapper.insert() |
Nabin Hait | ac53b11 | 2013-01-11 19:25:46 +0530 | [diff] [blame] | 130 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 131 | webnotes.conn.sql("""update `tabBin` set valuation_rate=%s, actual_qty=%s, |
| 132 | stock_value=%s, |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 133 | projected_qty = (actual_qty + indented_qty + ordered_qty + planned_qty - reserved_qty) |
| 134 | where item_code=%s and warehouse=%s""", (valuation_rate, qty_after_transaction, |
| 135 | stock_value, args["item_code"], args["warehouse"])) |
| 136 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 137 | def get_sle_before_datetime(args, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 138 | """ |
| 139 | get previous stock ledger entry before current time-bucket |
| 140 | |
| 141 | Details: |
| 142 | get the last sle before the current time-bucket, so that all values |
| 143 | are reposted from the current time-bucket onwards. |
| 144 | this is necessary because at the time of cancellation, there may be |
| 145 | entries between the cancelled entries in the same time-bucket |
| 146 | """ |
| 147 | sle = get_stock_ledger_entries(args, |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 148 | ["timestamp(posting_date, posting_time) < timestamp(%(posting_date)s, %(posting_time)s)"], |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 149 | "desc", "limit 1", for_update=for_update) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 150 | |
| 151 | return sle and sle[0] or webnotes._dict() |
| 152 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 153 | def get_sle_after_datetime(args, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 154 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 155 | # NOTE: using for update of |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 156 | return get_stock_ledger_entries(args, |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 157 | ["timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s)"], |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 158 | "asc", for_update=for_update) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 159 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 160 | def get_stock_ledger_entries(args, conditions=None, order="desc", limit=None, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 161 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 162 | if not args.get("posting_date"): |
| 163 | args["posting_date"] = "1900-01-01" |
| 164 | if not args.get("posting_time"): |
Anand Doshi | 71bed31 | 2013-03-13 12:57:04 +0530 | [diff] [blame] | 165 | args["posting_time"] = "00:00" |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 166 | |
| 167 | return webnotes.conn.sql("""select * from `tabStock Ledger Entry` |
| 168 | where item_code = %%(item_code)s |
| 169 | and warehouse = %%(warehouse)s |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 170 | %(conditions)s |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 171 | order by timestamp(posting_date, posting_time) %(order)s, name %(order)s |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 172 | %(limit)s %(for_update)s""" % { |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 173 | "conditions": conditions and ("and " + " and ".join(conditions)) or "", |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 174 | "limit": limit or "", |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 175 | "for_update": for_update and "for update" or "", |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 176 | "order": order |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 177 | }, args, as_dict=1) |
| 178 | |
| 179 | def validate_negative_stock(qty_after_transaction, sle): |
| 180 | """ |
| 181 | validate negative stock for entries current datetime onwards |
| 182 | will not consider cancelled entries |
| 183 | """ |
| 184 | diff = qty_after_transaction + flt(sle.actual_qty) |
| 185 | |
| 186 | if diff < 0 and abs(diff) > 0.0001: |
| 187 | # negative stock! |
| 188 | global _exceptions |
| 189 | exc = sle.copy().update({"diff": diff}) |
| 190 | _exceptions.append(exc) |
| 191 | return False |
| 192 | else: |
| 193 | return True |
| 194 | |
| 195 | def get_serialized_values(qty_after_transaction, sle, valuation_rate): |
| 196 | incoming_rate = flt(sle.incoming_rate) |
| 197 | actual_qty = flt(sle.actual_qty) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 198 | serial_no = cstr(sle.serial_no).split("\n") |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 199 | |
| 200 | if incoming_rate < 0: |
| 201 | # wrong incoming rate |
| 202 | incoming_rate = valuation_rate |
| 203 | elif incoming_rate == 0 or flt(sle.actual_qty) < 0: |
| 204 | # In case of delivery/stock issue, get average purchase rate |
| 205 | # of serial nos of current entry |
| 206 | incoming_rate = flt(webnotes.conn.sql("""select avg(ifnull(purchase_rate, 0)) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 207 | from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))), |
| 208 | tuple(serial_no))[0][0]) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 209 | |
| 210 | if incoming_rate and not valuation_rate: |
| 211 | valuation_rate = incoming_rate |
| 212 | else: |
| 213 | new_stock_qty = qty_after_transaction + actual_qty |
| 214 | if new_stock_qty > 0: |
| 215 | new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate |
| 216 | if new_stock_value > 0: |
| 217 | # calculate new valuation rate only if stock value is positive |
| 218 | # else it remains the same as that of previous entry |
| 219 | valuation_rate = new_stock_value / new_stock_qty |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 220 | |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 221 | return valuation_rate |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 222 | |
| 223 | def get_moving_average_values(qty_after_transaction, sle, valuation_rate): |
| 224 | incoming_rate = flt(sle.incoming_rate) |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 225 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 226 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 227 | if not incoming_rate: |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 228 | # In case of delivery/stock issue in_rate = 0 or wrong incoming rate |
| 229 | incoming_rate = valuation_rate |
| 230 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 231 | elif qty_after_transaction < 0: |
| 232 | # if negative stock, take current valuation rate as incoming rate |
| 233 | valuation_rate = incoming_rate |
| 234 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 235 | new_stock_qty = qty_after_transaction + actual_qty |
| 236 | new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 237 | |
| 238 | if new_stock_qty > 0 and new_stock_value > 0: |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 239 | valuation_rate = new_stock_value / flt(new_stock_qty) |
| 240 | elif new_stock_qty <= 0: |
| 241 | valuation_rate = 0.0 |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 242 | |
| 243 | # NOTE: val_rate is same as previous entry if new stock value is negative |
| 244 | |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 245 | return valuation_rate |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 246 | |
| 247 | def get_fifo_values(qty_after_transaction, sle, stock_queue): |
| 248 | incoming_rate = flt(sle.incoming_rate) |
| 249 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 250 | if not stock_queue: |
| 251 | stock_queue.append([0, 0]) |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 252 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 253 | if actual_qty > 0: |
| 254 | if stock_queue[-1][0] > 0: |
| 255 | stock_queue.append([actual_qty, incoming_rate]) |
| 256 | else: |
| 257 | qty = stock_queue[-1][0] + actual_qty |
| 258 | stock_queue[-1] = [qty, qty > 0 and incoming_rate or 0] |
| 259 | else: |
| 260 | incoming_cost = 0 |
| 261 | qty_to_pop = abs(actual_qty) |
| 262 | while qty_to_pop: |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 263 | if not stock_queue: |
| 264 | stock_queue.append([0, 0]) |
| 265 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 266 | batch = stock_queue[0] |
| 267 | |
| 268 | if 0 < batch[0] <= qty_to_pop: |
| 269 | # if batch qty > 0 |
| 270 | # not enough or exactly same qty in current batch, clear batch |
| 271 | incoming_cost += flt(batch[0]) * flt(batch[1]) |
| 272 | qty_to_pop -= batch[0] |
| 273 | stock_queue.pop(0) |
| 274 | else: |
| 275 | # all from current batch |
| 276 | incoming_cost += flt(qty_to_pop) * flt(batch[1]) |
| 277 | batch[0] -= qty_to_pop |
| 278 | qty_to_pop = 0 |
| 279 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 280 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue)) |
| 281 | stock_qty = sum((flt(batch[0]) for batch in stock_queue)) |
| 282 | |
| 283 | valuation_rate = stock_qty and (stock_value / flt(stock_qty)) or 0 |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 284 | |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 285 | return valuation_rate |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 286 | |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 287 | def _raise_exceptions(args, verbose=1): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 288 | deficiency = min(e["diff"] for e in _exceptions) |
| 289 | msg = """Negative stock error: |
| 290 | Cannot complete this transaction because stock will start |
| 291 | becoming negative (%s) for Item <b>%s</b> in Warehouse |
| 292 | <b>%s</b> on <b>%s %s</b> in Transaction %s %s. |
| 293 | Total Quantity Deficiency: <b>%s</b>""" % \ |
| 294 | (_exceptions[0]["diff"], args.get("item_code"), args.get("warehouse"), |
| 295 | _exceptions[0]["posting_date"], _exceptions[0]["posting_time"], |
| 296 | _exceptions[0]["voucher_type"], _exceptions[0]["voucher_no"], |
| 297 | abs(deficiency)) |
| 298 | if verbose: |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 299 | msgprint(msg, raise_exception=NegativeStockError) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 300 | else: |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 301 | raise NegativeStockError, msg |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 302 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 303 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 304 | """ |
| 305 | get the last sle on or before the current time-bucket, |
| 306 | to get actual qty before transaction, this function |
| 307 | is called from various transaction like stock entry, reco etc |
| 308 | |
| 309 | args = { |
| 310 | "item_code": "ABC", |
| 311 | "warehouse": "XYZ", |
| 312 | "posting_date": "2012-12-12", |
| 313 | "posting_time": "12:00", |
| 314 | "sle": "name of reference Stock Ledger Entry" |
| 315 | } |
| 316 | """ |
| 317 | if not args.get("sle"): args["sle"] = "" |
| 318 | |
| 319 | sle = get_stock_ledger_entries(args, ["name != %(sle)s", |
| 320 | "timestamp(posting_date, posting_time) <= timestamp(%(posting_date)s, %(posting_time)s)"], |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 321 | "desc", "limit 1", for_update=for_update) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 322 | return sle and sle[0] or {} |