Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Anand Doshi | 56a3165 | 2013-11-29 19:15:26 +0530 | [diff] [blame] | 3 | from __future__ import unicode_literals |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 4 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 6 | from frappe import _ |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | from frappe.utils import cint, flt, cstr, now |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 8 | from erpnext.stock.utils import get_valuation_method |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 9 | import json |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 10 | |
| 11 | # future reposting |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 12 | class NegativeStockError(frappe.ValidationError): pass |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 13 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 14 | _exceptions = frappe.local('stockledger_exceptions') |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 15 | # _exceptions = [] |
Anand Doshi | 5b004ff | 2013-09-25 19:55:41 +0530 | [diff] [blame] | 16 | |
Nabin Hait | 9c47efb | 2015-01-21 16:22:45 +0530 | [diff] [blame] | 17 | def make_sl_entries(sl_entries, is_amended=None, allow_negative_stock=False): |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 18 | if sl_entries: |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 19 | from erpnext.stock.utils import update_bin |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 20 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 21 | 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 Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 24 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 25 | 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 Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 29 | |
Nabin Hait | 5288bde | 2014-11-03 15:08:21 +0530 | [diff] [blame] | 30 | if sle.get("actual_qty") or sle.get("voucher_type")=="Stock Reconciliation": |
Nabin Hait | 4ccd8d3 | 2015-01-23 12:18:01 +0530 | [diff] [blame] | 31 | sle_id = make_entry(sle, allow_negative_stock) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 32 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 33 | args = sle.copy() |
| 34 | args.update({ |
| 35 | "sle_id": sle_id, |
| 36 | "is_amended": is_amended |
| 37 | }) |
Nabin Hait | 9c47efb | 2015-01-21 16:22:45 +0530 | [diff] [blame] | 38 | update_bin(args, allow_negative_stock) |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 39 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 40 | if cancel: |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 41 | delete_cancelled_entry(sl_entries[0].get('voucher_type'), sl_entries[0].get('voucher_no')) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 42 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 43 | def set_as_cancel(voucher_type, voucher_no): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 44 | frappe.db.sql("""update `tabStock Ledger Entry` set is_cancelled='Yes', |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 45 | modified=%s, modified_by=%s |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 46 | where voucher_no=%s and voucher_type=%s""", |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 47 | (now(), frappe.session.user, voucher_type, voucher_no)) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 48 | |
Nabin Hait | 4ccd8d3 | 2015-01-23 12:18:01 +0530 | [diff] [blame] | 49 | def make_entry(args, allow_negative_stock=False): |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 50 | args.update({"doctype": "Stock Ledger Entry"}) |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 51 | sle = frappe.get_doc(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 52 | sle.flags.ignore_permissions = 1 |
Nabin Hait | 4ccd8d3 | 2015-01-23 12:18:01 +0530 | [diff] [blame] | 53 | sle.allow_negative_stock=allow_negative_stock |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 54 | sle.insert() |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 55 | sle.submit() |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 56 | return sle.name |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 57 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 58 | def delete_cancelled_entry(voucher_type, voucher_no): |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 59 | frappe.db.sql("""delete from `tabStock Ledger Entry` |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 60 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 61 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 62 | class update_entries_after(object): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 63 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 64 | update valution rate and qty after transaction |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 65 | from the current time-bucket onwards |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 66 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 67 | :param args: args as dict |
| 68 | |
| 69 | args = { |
| 70 | "item_code": "ABC", |
| 71 | "warehouse": "XYZ", |
| 72 | "posting_date": "2012-12-12", |
| 73 | "posting_time": "12:00" |
| 74 | } |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 75 | """ |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 76 | def __init__(self, args, allow_zero_rate=False, allow_negative_stock=None, verbose=1): |
| 77 | from frappe.model.meta import get_field_precision |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 78 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 79 | self.exceptions = [] |
| 80 | self.verbose = verbose |
| 81 | self.allow_zero_rate = allow_zero_rate |
| 82 | self.allow_negative_stock = allow_negative_stock |
Nabin Hait | 9c47efb | 2015-01-21 16:22:45 +0530 | [diff] [blame] | 83 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 84 | if self.allow_negative_stock==None: |
| 85 | self.allow_negative_stock = cint(frappe.db.get_single_value("Stock Settings", |
| 86 | "allow_negative_stock")) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 87 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 88 | self.args = args |
| 89 | for key, value in args.iteritems(): |
| 90 | setattr(self, key, value) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 91 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 92 | self.previous_sle = self.get_sle_before_datetime() |
| 93 | self.previous_sle = self.previous_sle[0] if self.previous_sle else frappe._dict() |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 94 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 95 | for key in ("qty_after_transaction", "valuation_rate", "stock_value"): |
| 96 | setattr(self, key, flt(self.previous_sle.get(key))) |
| 97 | |
| 98 | self.company = frappe.db.get_value("Warehouse", self.warehouse, "company") |
| 99 | self.precision = get_field_precision(frappe.get_meta("Stock Ledger Entry").get_field("stock_value"), |
| 100 | currency=frappe.db.get_value("Company", self.company, "default_currency")) |
| 101 | |
Rushabh Mehta | c7a11cc | 2015-02-19 16:28:35 +0530 | [diff] [blame] | 102 | self.prev_stock_value = self.previous_sle.stock_value or 0.0 |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 103 | self.stock_queue = json.loads(self.previous_sle.stock_queue or "[]") |
| 104 | self.valuation_method = get_valuation_method(self.item_code) |
| 105 | self.stock_value_difference = 0.0 |
| 106 | self.build() |
| 107 | |
| 108 | def build(self): |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 109 | # includes current entry! |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 110 | entries_to_fix = self.get_sle_after_datetime() |
| 111 | |
| 112 | for sle in entries_to_fix: |
| 113 | self.process_sle(sle) |
| 114 | |
| 115 | if self.exceptions: |
| 116 | self.raise_exceptions() |
| 117 | |
| 118 | self.update_bin() |
| 119 | |
| 120 | def update_bin(self): |
| 121 | # update bin |
| 122 | bin_name = frappe.db.get_value("Bin", { |
| 123 | "item_code": self.item_code, |
| 124 | "warehouse": self.warehouse |
| 125 | }) |
| 126 | |
| 127 | if not bin_name: |
| 128 | bin_doc = frappe.get_doc({ |
| 129 | "doctype": "Bin", |
| 130 | "item_code": self.item_code, |
| 131 | "warehouse": self.warehouse |
| 132 | }) |
| 133 | bin_doc.insert(ignore_permissions=True) |
| 134 | else: |
| 135 | bin_doc = frappe.get_doc("Bin", bin_name) |
| 136 | |
| 137 | bin_doc.update({ |
| 138 | "valuation_rate": self.valuation_rate, |
| 139 | "actual_qty": self.qty_after_transaction, |
| 140 | "stock_value": self.stock_value |
| 141 | }) |
| 142 | bin_doc.save(ignore_permissions=True) |
| 143 | |
| 144 | def process_sle(self, sle): |
| 145 | if sle.serial_no or not cint(self.allow_negative_stock): |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 146 | # validate negative stock for serialized items, fifo valuation |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 147 | # or when negative stock is not allowed for moving average |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 148 | if not self.validate_negative_stock(sle): |
| 149 | self.qty_after_transaction += flt(sle.actual_qty) |
| 150 | return |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 151 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 152 | if sle.serial_no: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 153 | self.valuation_rate = self.get_serialized_values(sle) |
| 154 | self.qty_after_transaction += flt(sle.actual_qty) |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 155 | self.stock_value = flt(self.qty_after_transaction) * flt(self.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 156 | else: |
| 157 | if sle.voucher_type=="Stock Reconciliation": |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 158 | # assert |
| 159 | self.valuation_rate = sle.valuation_rate |
| 160 | self.qty_after_transaction = sle.qty_after_transaction |
| 161 | self.stock_queue = [[self.qty_after_transaction, self.valuation_rate]] |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 162 | self.stock_value = flt(self.qty_after_transaction) * flt(self.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 163 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 164 | if self.valuation_method == "Moving Average": |
| 165 | self.get_moving_average_values(sle) |
| 166 | self.qty_after_transaction += flt(sle.actual_qty) |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 167 | self.stock_value = flt(self.qty_after_transaction) * flt(self.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 168 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 169 | self.get_fifo_values(sle) |
| 170 | self.qty_after_transaction += flt(sle.actual_qty) |
| 171 | self.stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.stock_queue)) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 172 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 173 | # rounding as per precision |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 174 | self.stock_value = flt(self.stock_value, self.precision) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 175 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 176 | stock_value_difference = self.stock_value - self.prev_stock_value |
| 177 | self.prev_stock_value = self.stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 178 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 179 | # update current sle |
Rushabh Mehta | 2e0e711 | 2015-02-18 11:38:05 +0530 | [diff] [blame] | 180 | sle.qty_after_transaction = self.qty_after_transaction |
| 181 | sle.valuation_rate = self.valuation_rate |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 182 | sle.stock_value = self.stock_value |
Rushabh Mehta | 2e0e711 | 2015-02-18 11:38:05 +0530 | [diff] [blame] | 183 | sle.stock_queue = json.dumps(self.stock_queue) |
| 184 | sle.stock_value_difference = stock_value_difference |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 185 | sle.doctype="Stock Ledger Entry" |
| 186 | frappe.get_doc(sle).db_update() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 187 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 188 | def validate_negative_stock(self, sle): |
| 189 | """ |
| 190 | validate negative stock for entries current datetime onwards |
| 191 | will not consider cancelled entries |
| 192 | """ |
| 193 | diff = self.qty_after_transaction + flt(sle.actual_qty) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 194 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 195 | if diff < 0 and abs(diff) > 0.0001: |
| 196 | # negative stock! |
| 197 | exc = sle.copy().update({"diff": diff}) |
| 198 | self.exceptions.append(exc) |
| 199 | return False |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 200 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 201 | return True |
| 202 | |
| 203 | def get_serialized_values(self, sle): |
| 204 | incoming_rate = flt(sle.incoming_rate) |
| 205 | actual_qty = flt(sle.actual_qty) |
| 206 | serial_no = cstr(sle.serial_no).split("\n") |
| 207 | |
| 208 | if incoming_rate < 0: |
| 209 | # wrong incoming rate |
| 210 | incoming_rate = self.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(frappe.db.sql("""select avg(ifnull(purchase_rate, 0)) |
| 215 | from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))), |
| 216 | tuple(serial_no))[0][0]) |
| 217 | |
| 218 | if incoming_rate and not self.valuation_rate: |
| 219 | self.valuation_rate = incoming_rate |
| 220 | else: |
| 221 | new_stock_qty = self.qty_after_transaction + actual_qty |
| 222 | if new_stock_qty > 0: |
| 223 | new_stock_value = self.qty_after_transaction * self.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 | self.valuation_rate = new_stock_value / new_stock_qty |
| 228 | |
| 229 | def get_moving_average_values(self, sle): |
| 230 | incoming_rate = flt(sle.incoming_rate) |
| 231 | actual_qty = flt(sle.actual_qty) |
| 232 | |
| 233 | if flt(sle.actual_qty) > 0: |
| 234 | if self.qty_after_transaction < 0 and not self.valuation_rate: |
| 235 | # if negative stock, take current valuation rate as incoming rate |
| 236 | self.valuation_rate = incoming_rate |
| 237 | |
| 238 | new_stock_qty = abs(self.qty_after_transaction) + actual_qty |
| 239 | new_stock_value = (abs(self.qty_after_transaction) * self.valuation_rate) + (actual_qty * incoming_rate) |
| 240 | |
| 241 | if new_stock_qty: |
| 242 | self.valuation_rate = new_stock_value / flt(new_stock_qty) |
| 243 | elif not self.valuation_rate and self.qty_after_transaction <= 0: |
Rushabh Mehta | d54031f | 2015-02-22 22:32:39 +0530 | [diff] [blame] | 244 | self.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, self.allow_zero_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 245 | |
Rushabh Mehta | d54031f | 2015-02-22 22:32:39 +0530 | [diff] [blame] | 246 | return abs(flt(self.valuation_rate)) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 247 | |
| 248 | def get_fifo_values(self, sle): |
| 249 | incoming_rate = flt(sle.incoming_rate) |
| 250 | actual_qty = flt(sle.actual_qty) |
| 251 | |
| 252 | if actual_qty > 0: |
| 253 | if not self.stock_queue: |
| 254 | self.stock_queue.append([0, 0]) |
| 255 | |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 256 | # last row has the same rate, just updated the qty |
| 257 | if self.stock_queue[-1][1]==incoming_rate: |
| 258 | self.stock_queue[-1][0] += actual_qty |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 259 | else: |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 260 | if self.stock_queue[-1][0] > 0: |
| 261 | self.stock_queue.append([actual_qty, incoming_rate]) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 262 | else: |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 263 | qty = self.stock_queue[-1][0] + actual_qty |
| 264 | if qty == 0: |
| 265 | self.stock_queue.pop(-1) |
| 266 | else: |
| 267 | self.stock_queue[-1] = [qty, incoming_rate] |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 268 | else: |
| 269 | qty_to_pop = abs(actual_qty) |
| 270 | while qty_to_pop: |
| 271 | if not self.stock_queue: |
| 272 | if self.qty_after_transaction > 0: |
| 273 | _rate = get_valuation_rate(sle.item_code, sle.warehouse, self.allow_zero_rate) |
| 274 | else: |
| 275 | _rate = 0 |
| 276 | self.stock_queue.append([0, _rate]) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 277 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 278 | batch = self.stock_queue[0] |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 279 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 280 | if qty_to_pop >= batch[0]: |
| 281 | # consume current batch |
| 282 | qty_to_pop = qty_to_pop - batch[0] |
| 283 | self.stock_queue.pop(0) |
| 284 | if not self.stock_queue and qty_to_pop: |
| 285 | # stock finished, qty still remains to be withdrawn |
| 286 | # negative stock, keep in as a negative batch |
| 287 | self.stock_queue.append([-qty_to_pop, batch[1]]) |
| 288 | break |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 289 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 290 | else: |
| 291 | # qty found in current batch |
| 292 | # consume it and exit |
| 293 | batch[0] = batch[0] - qty_to_pop |
| 294 | qty_to_pop = 0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 295 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 296 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.stock_queue)) |
| 297 | stock_qty = sum((flt(batch[0]) for batch in self.stock_queue)) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 298 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 299 | self.valuation_rate = (stock_value / flt(stock_qty)) if stock_qty else 0 |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 300 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 301 | def get_sle_before_datetime(self): |
| 302 | """get previous stock ledger entry before current time-bucket""" |
| 303 | return get_stock_ledger_entries(self.args, "<", "desc", "limit 1", for_update=False) |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 304 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 305 | def get_sle_after_datetime(self): |
| 306 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 307 | return get_stock_ledger_entries(self.previous_sle or frappe._dict({ |
| 308 | "item_code": self.args.get("item_code"), "warehouse": self.args.get("warehouse") }), |
| 309 | ">", "asc", for_update=True) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 310 | |
| 311 | def raise_exceptions(self): |
| 312 | deficiency = min(e["diff"] for e in self.exceptions) |
| 313 | msg = _("Negative Stock Error ({6}) for Item {0} in Warehouse {1} on {2} {3} in {4} {5}").format(self.item_code, |
| 314 | self.warehouse, self.exceptions[0]["posting_date"], self.exceptions[0]["posting_time"], |
| 315 | _(self.exceptions[0]["voucher_type"]), self.exceptions[0]["voucher_no"], deficiency) |
| 316 | if self.verbose: |
| 317 | frappe.throw(msg, NegativeStockError) |
| 318 | else: |
| 319 | raise NegativeStockError, msg |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 320 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 321 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 322 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 323 | get the last sle on or before the current time-bucket, |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 324 | to get actual qty before transaction, this function |
| 325 | is called from various transaction like stock entry, reco etc |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 326 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 327 | args = { |
| 328 | "item_code": "ABC", |
| 329 | "warehouse": "XYZ", |
| 330 | "posting_date": "2012-12-12", |
| 331 | "posting_time": "12:00", |
| 332 | "sle": "name of reference Stock Ledger Entry" |
| 333 | } |
| 334 | """ |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 335 | args["name"] = args.get("sle", None) or "" |
| 336 | sle = get_stock_ledger_entries(args, "<=", "desc", "limit 1", for_update=for_update) |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 337 | return sle and sle[0] or {} |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 338 | |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 339 | def get_stock_ledger_entries(previous_sle, operator=None, order="desc", limit=None, for_update=False, debug=False): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 340 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 341 | conditions = "timestamp(posting_date, posting_time) {0} timestamp(%(posting_date)s, %(posting_time)s)".format(operator) |
| 342 | if not previous_sle.get("posting_date"): |
| 343 | previous_sle["posting_date"] = "1900-01-01" |
| 344 | if not previous_sle.get("posting_time"): |
| 345 | previous_sle["posting_time"] = "00:00" |
| 346 | |
| 347 | if operator in (">", "<=") and previous_sle.get("name"): |
| 348 | conditions += " and name!=%(name)s" |
| 349 | |
| 350 | return frappe.db.sql("""select *, timestamp(posting_date, posting_time) as "timestamp" from `tabStock Ledger Entry` |
| 351 | where item_code = %%(item_code)s |
| 352 | and warehouse = %%(warehouse)s |
| 353 | and ifnull(is_cancelled, 'No')='No' |
| 354 | and %(conditions)s |
| 355 | order by timestamp(posting_date, posting_time) %(order)s, name %(order)s |
| 356 | %(limit)s %(for_update)s""" % { |
| 357 | "conditions": conditions, |
| 358 | "limit": limit or "", |
| 359 | "for_update": for_update and "for update" or "", |
| 360 | "order": order |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 361 | }, previous_sle, as_dict=1, debug=debug) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 362 | |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 363 | def get_valuation_rate(item_code, warehouse, allow_zero_rate=False): |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 364 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 365 | from `tabStock Ledger Entry` |
| 366 | where item_code = %s and warehouse = %s |
| 367 | and ifnull(valuation_rate, 0) > 0 |
| 368 | order by posting_date desc, posting_time desc, name desc limit 1""", (item_code, warehouse)) |
| 369 | |
| 370 | if not last_valuation_rate: |
| 371 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 372 | from `tabStock Ledger Entry` |
| 373 | where item_code = %s and ifnull(valuation_rate, 0) > 0 |
| 374 | order by posting_date desc, posting_time desc, name desc limit 1""", item_code) |
| 375 | |
| 376 | valuation_rate = flt(last_valuation_rate[0][0]) if last_valuation_rate else 0 |
| 377 | |
| 378 | if not valuation_rate: |
| 379 | valuation_rate = frappe.db.get_value("Item Price", {"item_code": item_code, "buying": 1}, "price_list_rate") |
| 380 | |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 381 | if not allow_zero_rate and not valuation_rate and cint(frappe.db.get_value("Accounts Settings", None, "auto_accounting_for_stock")): |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 382 | frappe.throw(_("Purchase rate for item: {0} not found, which is required to book accounting entry (expense). Please mention item price against a buying price list.").format(item_code)) |
| 383 | |
| 384 | return valuation_rate |