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