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