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 | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 5 | import frappe, erpnext |
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 |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 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 | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 214 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 215 | stock_value_change = 0 |
| 216 | if incoming_rate: |
| 217 | stock_value_change = actual_qty * incoming_rate |
| 218 | elif actual_qty < 0: |
| 219 | # In case of delivery/stock issue, get average purchase rate |
| 220 | # of serial nos of current entry |
| 221 | stock_value_change = -1 * flt(frappe.db.sql("""select sum(purchase_rate) |
| 222 | from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))), |
| 223 | tuple(serial_no))[0][0]) |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 224 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 225 | new_stock_qty = self.qty_after_transaction + actual_qty |
| 226 | if new_stock_qty > 0: |
| 227 | new_stock_value = (self.qty_after_transaction * self.valuation_rate) + stock_value_change |
| 228 | if new_stock_value > 0: |
| 229 | # calculate new valuation rate only if stock value is positive |
| 230 | # else it remains the same as that of previous entry |
| 231 | self.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 232 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 233 | def get_moving_average_values(self, sle): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 234 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 235 | new_stock_qty = flt(self.qty_after_transaction) + actual_qty |
| 236 | if new_stock_qty >= 0: |
| 237 | if actual_qty > 0: |
| 238 | if flt(self.qty_after_transaction) <= 0: |
| 239 | self.valuation_rate = sle.incoming_rate |
| 240 | else: |
| 241 | new_stock_value = (self.qty_after_transaction * self.valuation_rate) + \ |
| 242 | (actual_qty * sle.incoming_rate) |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 243 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 244 | self.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 245 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 246 | elif sle.outgoing_rate: |
| 247 | if new_stock_qty: |
| 248 | new_stock_value = (self.qty_after_transaction * self.valuation_rate) + \ |
| 249 | (actual_qty * sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 250 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 251 | self.valuation_rate = new_stock_value / new_stock_qty |
| 252 | else: |
Nabin Hait | 7590b8f | 2016-07-27 16:44:17 +0530 | [diff] [blame] | 253 | self.valuation_rate = sle.outgoing_rate |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 254 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 255 | else: |
| 256 | if flt(self.qty_after_transaction) >= 0 and sle.outgoing_rate: |
| 257 | self.valuation_rate = sle.outgoing_rate |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 258 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 259 | if not self.valuation_rate and actual_qty > 0: |
| 260 | self.valuation_rate = sle.incoming_rate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 261 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 262 | # Get valuation rate from previous SLE or Item master, if item does not have the |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 263 | # allow zero valuration rate flag set |
Nabin Hait | ea8fab5 | 2017-02-06 17:13:39 +0530 | [diff] [blame] | 264 | if not self.valuation_rate and sle.voucher_detail_no: |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 265 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 266 | if not allow_zero_valuation_rate: |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 267 | self.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, |
| 268 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
| 269 | currency=erpnext.get_company_currency(sle.company)) |
| 270 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 271 | def get_fifo_values(self, sle): |
| 272 | incoming_rate = flt(sle.incoming_rate) |
| 273 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 274 | outgoing_rate = flt(sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 275 | |
| 276 | if actual_qty > 0: |
| 277 | if not self.stock_queue: |
| 278 | self.stock_queue.append([0, 0]) |
| 279 | |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 280 | # last row has the same rate, just updated the qty |
| 281 | if self.stock_queue[-1][1]==incoming_rate: |
| 282 | self.stock_queue[-1][0] += actual_qty |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 283 | else: |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 284 | if self.stock_queue[-1][0] > 0: |
| 285 | self.stock_queue.append([actual_qty, incoming_rate]) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 286 | else: |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 287 | qty = self.stock_queue[-1][0] + actual_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 288 | self.stock_queue[-1] = [qty, incoming_rate] |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 289 | else: |
| 290 | qty_to_pop = abs(actual_qty) |
| 291 | while qty_to_pop: |
| 292 | if not self.stock_queue: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 293 | # Get valuation rate from last sle if exists or from valuation rate field in item master |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 294 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 295 | if not allow_zero_valuation_rate: |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 296 | _rate = get_valuation_rate(sle.item_code, sle.warehouse, |
| 297 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
| 298 | currency=erpnext.get_company_currency(sle.company)) |
Nabin Hait | 0a6aaf4 | 2017-02-07 01:23:26 +0530 | [diff] [blame] | 299 | else: |
| 300 | _rate = 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 301 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 302 | self.stock_queue.append([0, _rate]) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 303 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 304 | index = None |
| 305 | if outgoing_rate > 0: |
| 306 | # Find the entry where rate matched with outgoing rate |
| 307 | for i, v in enumerate(self.stock_queue): |
| 308 | if v[1] == outgoing_rate: |
| 309 | index = i |
| 310 | break |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 311 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 312 | # If no entry found with outgoing rate, collapse stack |
| 313 | if index == None: |
| 314 | new_stock_value = sum((d[0]*d[1] for d in self.stock_queue)) - qty_to_pop*outgoing_rate |
| 315 | new_stock_qty = sum((d[0] for d in self.stock_queue)) - qty_to_pop |
| 316 | self.stock_queue = [[new_stock_qty, new_stock_value/new_stock_qty if new_stock_qty > 0 else outgoing_rate]] |
| 317 | break |
| 318 | else: |
| 319 | index = 0 |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 320 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 321 | # select first batch or the batch with same rate |
| 322 | batch = self.stock_queue[index] |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 323 | if qty_to_pop >= batch[0]: |
| 324 | # consume current batch |
| 325 | qty_to_pop = qty_to_pop - batch[0] |
| 326 | self.stock_queue.pop(index) |
| 327 | if not self.stock_queue and qty_to_pop: |
| 328 | # stock finished, qty still remains to be withdrawn |
| 329 | # negative stock, keep in as a negative batch |
| 330 | self.stock_queue.append([-qty_to_pop, outgoing_rate or batch[1]]) |
| 331 | break |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 332 | |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 333 | else: |
| 334 | # qty found in current batch |
| 335 | # consume it and exit |
| 336 | batch[0] = batch[0] - qty_to_pop |
| 337 | qty_to_pop = 0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 338 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 339 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.stock_queue)) |
| 340 | stock_qty = sum((flt(batch[0]) for batch in self.stock_queue)) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 341 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 342 | if stock_qty: |
| 343 | self.valuation_rate = stock_value / flt(stock_qty) |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 344 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 345 | if not self.stock_queue: |
| 346 | self.stock_queue.append([0, sle.incoming_rate or sle.outgoing_rate or self.valuation_rate]) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 347 | |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 348 | def check_if_allow_zero_valuation_rate(self, voucher_type, voucher_detail_no): |
Nabin Hait | 0a6aaf4 | 2017-02-07 01:23:26 +0530 | [diff] [blame] | 349 | ref_item_dt = voucher_type + (" Detail" if voucher_type == "Stock Entry" else " Item") |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 350 | return frappe.db.get_value(ref_item_dt, voucher_detail_no, "allow_zero_valuation_rate") |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 351 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 352 | def get_sle_before_datetime(self): |
| 353 | """get previous stock ledger entry before current time-bucket""" |
| 354 | 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] | 355 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 356 | def get_sle_after_datetime(self): |
| 357 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 358 | return get_stock_ledger_entries(self.previous_sle or frappe._dict({ |
| 359 | "item_code": self.args.get("item_code"), "warehouse": self.args.get("warehouse") }), |
| 360 | ">", "asc", for_update=True) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 361 | |
| 362 | def raise_exceptions(self): |
| 363 | deficiency = min(e["diff"] for e in self.exceptions) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 364 | |
Rushabh Mehta | 649e253 | 2016-07-20 15:30:17 +0530 | [diff] [blame] | 365 | if ((self.exceptions[0]["voucher_type"], self.exceptions[0]["voucher_no"]) in |
| 366 | frappe.local.flags.currently_saving): |
Nabin Hait | 3edefb1 | 2016-07-20 16:13:18 +0530 | [diff] [blame] | 367 | |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 368 | msg = _("{0} units of {1} needed in {2} to complete this transaction.").format( |
| 369 | abs(deficiency), frappe.get_desk_link('Item', self.item_code), |
| 370 | frappe.get_desk_link('Warehouse', self.warehouse)) |
| 371 | else: |
| 372 | msg = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( |
| 373 | abs(deficiency), frappe.get_desk_link('Item', self.item_code), |
| 374 | frappe.get_desk_link('Warehouse', self.warehouse), |
| 375 | self.exceptions[0]["posting_date"], self.exceptions[0]["posting_time"], |
| 376 | frappe.get_desk_link(self.exceptions[0]["voucher_type"], self.exceptions[0]["voucher_no"])) |
| 377 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 378 | if self.verbose: |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 379 | frappe.throw(msg, NegativeStockError, title='Insufficent Stock') |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 380 | else: |
| 381 | raise NegativeStockError, msg |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 382 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 383 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 384 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 385 | get the last sle on or before the current time-bucket, |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 386 | to get actual qty before transaction, this function |
| 387 | is called from various transaction like stock entry, reco etc |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 388 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 389 | args = { |
| 390 | "item_code": "ABC", |
| 391 | "warehouse": "XYZ", |
| 392 | "posting_date": "2012-12-12", |
| 393 | "posting_time": "12:00", |
| 394 | "sle": "name of reference Stock Ledger Entry" |
| 395 | } |
| 396 | """ |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 397 | args["name"] = args.get("sle", None) or "" |
| 398 | 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] | 399 | return sle and sle[0] or {} |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 400 | |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 401 | 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] | 402 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 403 | conditions = "timestamp(posting_date, posting_time) {0} timestamp(%(posting_date)s, %(posting_time)s)".format(operator) |
| 404 | if not previous_sle.get("posting_date"): |
| 405 | previous_sle["posting_date"] = "1900-01-01" |
| 406 | if not previous_sle.get("posting_time"): |
| 407 | previous_sle["posting_time"] = "00:00" |
| 408 | |
| 409 | if operator in (">", "<=") and previous_sle.get("name"): |
| 410 | conditions += " and name!=%(name)s" |
| 411 | |
| 412 | return frappe.db.sql("""select *, timestamp(posting_date, posting_time) as "timestamp" from `tabStock Ledger Entry` |
| 413 | where item_code = %%(item_code)s |
| 414 | and warehouse = %%(warehouse)s |
| 415 | and ifnull(is_cancelled, 'No')='No' |
| 416 | and %(conditions)s |
| 417 | order by timestamp(posting_date, posting_time) %(order)s, name %(order)s |
| 418 | %(limit)s %(for_update)s""" % { |
| 419 | "conditions": conditions, |
| 420 | "limit": limit or "", |
| 421 | "for_update": for_update and "for update" or "", |
| 422 | "order": order |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 423 | }, previous_sle, as_dict=1, debug=debug) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 424 | |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 425 | def get_valuation_rate(item_code, warehouse, voucher_type, voucher_no, |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 426 | allow_zero_rate=False, currency=None, company=None): |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 427 | # Get valuation rate from last sle for the same item and warehouse |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 428 | if not company: |
| 429 | company = erpnext.get_default_company() |
| 430 | |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 431 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 432 | from `tabStock Ledger Entry` |
| 433 | where item_code = %s and warehouse = %s |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 434 | and valuation_rate > 0 |
Nabin Hait | 4c952f4 | 2017-01-20 12:50:18 +0530 | [diff] [blame] | 435 | order by posting_date desc, posting_time desc, name desc limit 1""", (item_code, warehouse)) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 436 | |
| 437 | if not last_valuation_rate: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 438 | # Get valuation rate from last sle for the item against any warehouse |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 439 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 440 | from `tabStock Ledger Entry` |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 441 | where item_code = %s and valuation_rate > 0 |
Nabin Hait | 4c952f4 | 2017-01-20 12:50:18 +0530 | [diff] [blame] | 442 | order by posting_date desc, posting_time desc, name desc limit 1""", item_code) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 443 | |
| 444 | valuation_rate = flt(last_valuation_rate[0][0]) if last_valuation_rate else 0 |
| 445 | |
| 446 | if not valuation_rate: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 447 | # If negative stock allowed, and item delivered without any incoming entry, |
| 448 | # syste does not found any SLE, then take valuation rate from Item |
Nabin Hait | 4c952f4 | 2017-01-20 12:50:18 +0530 | [diff] [blame] | 449 | valuation_rate = frappe.db.get_value("Item", item_code, "valuation_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 450 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 451 | if not valuation_rate: |
| 452 | # try Item Standard rate |
| 453 | valuation_rate = frappe.db.get_value("Item", item_code, "standard_rate") |
| 454 | |
| 455 | if not valuation_rate: |
| 456 | # try in price list |
| 457 | valuation_rate = frappe.db.get_value('Item Price', |
| 458 | dict(item_code=item_code, buying=1, currency=currency), |
| 459 | 'price_list_rate') |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 460 | |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 461 | if not allow_zero_rate and not valuation_rate \ |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 462 | and cint(erpnext.is_perpetual_inventory_enabled(company)): |
Neil Trini Lasrado | 193c891 | 2017-03-28 17:39:34 +0530 | [diff] [blame] | 463 | frappe.local.message_log = [] |
Nabin Hait | 0a6aaf4 | 2017-02-07 01:23:26 +0530 | [diff] [blame] | 464 | frappe.throw(_("Valuation rate not found for the Item {0}, which is required to do accounting entries for {1} {2}. If the item is transacting as a sample item in the {1}, please mention that in the {1} Item table. Otherwise, please create an incoming stock transaction for the item or mention valuation rate in the Item record, and then try submiting/cancelling this entry").format(item_code, voucher_type, voucher_no)) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 465 | |
| 466 | return valuation_rate |