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