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 _ |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 7 | from frappe.utils import cint, flt, cstr, now, now_datetime |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 8 | from frappe.model.meta import get_field_precision |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 9 | from erpnext.stock.utils import get_valuation_method, get_incoming_outgoing_rate_for_cancel |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 10 | from erpnext.stock.utils import get_bin |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 11 | import json |
Achilles Rasquinha | 361366e | 2018-02-14 17:08:59 +0530 | [diff] [blame] | 12 | from six import iteritems |
| 13 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 14 | # future reposting |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 15 | class NegativeStockError(frappe.ValidationError): pass |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 16 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 17 | _exceptions = frappe.local('stockledger_exceptions') |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 18 | # _exceptions = [] |
Anand Doshi | 5b004ff | 2013-09-25 19:55:41 +0530 | [diff] [blame] | 19 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 20 | def make_sl_entries(sl_entries, allow_negative_stock=False, via_landed_cost_voucher=False): |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 21 | if sl_entries: |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 22 | from erpnext.stock.utils import update_bin |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 23 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 24 | cancel = sl_entries[0].get("is_cancelled") |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 25 | if cancel: |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 26 | set_as_cancel(sl_entries[0].get('voucher_type'), sl_entries[0].get('voucher_no')) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 27 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 28 | for sle in sl_entries: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 29 | if cancel: |
| 30 | sle['actual_qty'] = -flt(sle.get('actual_qty')) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 31 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 32 | if sle['actual_qty'] < 0 and not sle.get('outgoing_rate'): |
| 33 | sle['outgoing_rate'] = get_incoming_outgoing_rate_for_cancel(sle.item_code, |
| 34 | sle.voucher_type, sle.voucher_no, sle.voucher_detail_no) |
| 35 | sle['incoming_rate'] = 0.0 |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 36 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 37 | if sle['actual_qty'] > 0 and not sle.get('incoming_rate'): |
| 38 | sle['incoming_rate'] = get_incoming_outgoing_rate_for_cancel(sle.item_code, |
| 39 | sle.voucher_type, sle.voucher_no, sle.voucher_detail_no) |
| 40 | sle['outgoing_rate'] = 0.0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 41 | |
Nabin Hait | 5288bde | 2014-11-03 15:08:21 +0530 | [diff] [blame] | 42 | if sle.get("actual_qty") or sle.get("voucher_type")=="Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 43 | sle_doc = make_entry(sle, allow_negative_stock, via_landed_cost_voucher) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 44 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 45 | args = sle_doc.as_dict() |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 46 | update_bin(args, allow_negative_stock, via_landed_cost_voucher) |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 47 | |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 48 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 49 | def set_as_cancel(voucher_type, voucher_no): |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 50 | frappe.db.sql("""update `tabStock Ledger Entry` set is_cancelled=1, |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 51 | modified=%s, modified_by=%s |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 52 | where voucher_type=%s and voucher_no=%s and is_cancelled = 0""", |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 53 | (now(), frappe.session.user, voucher_type, voucher_no)) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 54 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 55 | 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] | 56 | args.update({"doctype": "Stock Ledger Entry"}) |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 57 | sle = frappe.get_doc(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 58 | sle.flags.ignore_permissions = 1 |
Nabin Hait | 4ccd8d3 | 2015-01-23 12:18:01 +0530 | [diff] [blame] | 59 | sle.allow_negative_stock=allow_negative_stock |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 60 | sle.via_landed_cost_voucher = via_landed_cost_voucher |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 61 | sle.insert() |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 62 | sle.submit() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 63 | return sle |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 64 | |
Nabin Hait | 7177579 | 2021-02-02 22:03:07 +0530 | [diff] [blame] | 65 | def repost_future_sle(args=None, voucher_type=None, voucher_no=None, allow_negative_stock=None, via_landed_cost_voucher=False): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 66 | if not args and voucher_type and voucher_no: |
| 67 | args = get_args_for_voucher(voucher_type, voucher_no) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 68 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 69 | distinct_item_warehouses = [(d.item_code, d.warehouse) for d in args] |
| 70 | |
| 71 | i = 0 |
| 72 | while i < len(args): |
| 73 | obj = update_entries_after({ |
| 74 | "item_code": args[i].item_code, |
| 75 | "warehouse": args[i].warehouse, |
| 76 | "posting_date": args[i].posting_date, |
| 77 | "posting_time": args[i].posting_time |
| 78 | }, allow_negative_stock=allow_negative_stock, via_landed_cost_voucher=via_landed_cost_voucher) |
| 79 | |
| 80 | for item_wh, new_sle in iteritems(obj.new_items): |
| 81 | if item_wh not in distinct_item_warehouses: |
| 82 | args.append(new_sle) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 83 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 84 | i += 1 |
| 85 | |
| 86 | def get_args_for_voucher(voucher_type, voucher_no): |
| 87 | return frappe.db.get_all("Stock Ledger Entry", |
| 88 | filters={"voucher_type": voucher_type, "voucher_no": voucher_no}, |
| 89 | fields=["item_code", "warehouse", "posting_date", "posting_time"], |
| 90 | order_by="creation asc", |
| 91 | group_by="item_code, warehouse" |
| 92 | ) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 93 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 94 | class update_entries_after(object): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 95 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 96 | update valution rate and qty after transaction |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 97 | from the current time-bucket onwards |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 98 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 99 | :param args: args as dict |
| 100 | |
| 101 | args = { |
| 102 | "item_code": "ABC", |
| 103 | "warehouse": "XYZ", |
| 104 | "posting_date": "2012-12-12", |
| 105 | "posting_time": "12:00" |
| 106 | } |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 107 | """ |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 108 | def __init__(self, args, allow_zero_rate=False, allow_negative_stock=None, via_landed_cost_voucher=False, verbose=1): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 109 | self.exceptions = {} |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 110 | self.verbose = verbose |
| 111 | self.allow_zero_rate = allow_zero_rate |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 112 | self.via_landed_cost_voucher = via_landed_cost_voucher |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 113 | self.allow_negative_stock = allow_negative_stock \ |
| 114 | or cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock")) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 115 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 116 | self.args = frappe._dict(args) |
| 117 | self.item_code = args.get("item_code") |
| 118 | if self.args.sle_id: |
| 119 | self.args['name'] = self.args.sle_id |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 120 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 121 | self.company = frappe.get_cached_value("Warehouse", self.args.warehouse, "company") |
| 122 | self.get_precision() |
| 123 | self.valuation_method = get_valuation_method(self.item_code) |
| 124 | self.new_items = {} |
| 125 | |
| 126 | self.data = frappe._dict() |
| 127 | self.initialize_previous_data(self.args) |
| 128 | |
| 129 | self.build() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 130 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 131 | def get_precision(self): |
| 132 | company_base_currency = frappe.get_cached_value('Company', self.company, "default_currency") |
| 133 | self.precision = get_field_precision(frappe.get_meta("Stock Ledger Entry").get_field("stock_value"), |
| 134 | currency=company_base_currency) |
| 135 | |
| 136 | def initialize_previous_data(self, args): |
| 137 | """ |
| 138 | Get previous sl entries for current item for each related warehouse |
| 139 | and assigns into self.data dict |
| 140 | |
| 141 | :Data Structure: |
| 142 | |
| 143 | self.data = { |
| 144 | warehouse1: { |
| 145 | 'previus_sle': {}, |
| 146 | 'qty_after_transaction': 10, |
| 147 | 'valuation_rate': 100, |
| 148 | 'stock_value': 1000, |
| 149 | 'prev_stock_value': 1000, |
| 150 | 'stock_queue': '[[10, 100]]', |
| 151 | 'stock_value_difference': 1000 |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | """ |
| 156 | self.data.setdefault(args.warehouse, frappe._dict()) |
| 157 | warehouse_dict = self.data[args.warehouse] |
| 158 | previous_sle = self.get_sle_before_datetime(args) |
| 159 | warehouse_dict.previous_sle = previous_sle |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 160 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 161 | for key in ("qty_after_transaction", "valuation_rate", "stock_value"): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 162 | setattr(warehouse_dict, key, flt(previous_sle.get(key))) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 163 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 164 | warehouse_dict.update({ |
| 165 | "prev_stock_value": previous_sle.stock_value or 0.0, |
| 166 | "stock_queue": json.loads(previous_sle.stock_queue or "[]"), |
| 167 | "stock_value_difference": 0.0 |
| 168 | }) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 169 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 170 | def build(self): |
| 171 | if self.args.get("sle_id"): |
| 172 | self.process_sle_against_current_voucher() |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 173 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 174 | entries_to_fix = self.get_future_entries_to_fix() |
| 175 | |
| 176 | i = 0 |
| 177 | while i < len(entries_to_fix): |
| 178 | sle = entries_to_fix[i] |
| 179 | i += 1 |
| 180 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 181 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 182 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 183 | if sle.dependant_sle_voucher_detail_no: |
Nabin Hait | 7177579 | 2021-02-02 22:03:07 +0530 | [diff] [blame] | 184 | entries_to_fix = self.get_dependent_entries_to_fix(entries_to_fix, sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 185 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 186 | if self.exceptions: |
| 187 | self.raise_exceptions() |
| 188 | |
| 189 | self.update_bin() |
| 190 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 191 | def process_sle_against_current_voucher(self): |
| 192 | sl_entries = self.get_sle_against_current_voucher() |
| 193 | for sle in sl_entries: |
| 194 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 195 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 196 | def get_sle_against_current_voucher(self): |
| 197 | return frappe.db.sql(""" |
| 198 | select |
| 199 | *, timestamp(posting_date, posting_time) as "timestamp" |
| 200 | from |
| 201 | `tabStock Ledger Entry` |
| 202 | where |
| 203 | item_code = %(item_code)s |
| 204 | and warehouse = %(warehouse)s |
rohitwaghchaure | aa00eb9 | 2021-02-11 13:23:01 +0530 | [diff] [blame^] | 205 | and timestamp(posting_date, time_format(posting_time, '%H:%i:%s')) = timestamp(%(posting_date)s, time_format(%(posting_time)s, '%H:%i:%s')) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 206 | order by |
| 207 | creation ASC |
| 208 | for update |
| 209 | """, self.args, as_dict=1) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 210 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 211 | def get_future_entries_to_fix(self): |
| 212 | # includes current entry! |
| 213 | args = self.data[self.args.warehouse].previous_sle \ |
| 214 | or frappe._dict({"item_code": self.item_code, "warehouse": self.args.warehouse}) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 215 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 216 | return list(self.get_sle_after_datetime(args)) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 217 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 218 | def get_dependent_entries_to_fix(self, entries_to_fix, sle): |
| 219 | dependant_sle = get_sle_by_voucher_detail_no(sle.dependant_sle_voucher_detail_no, |
| 220 | excluded_sle=sle.name) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 221 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 222 | if not dependant_sle: |
Nabin Hait | 7177579 | 2021-02-02 22:03:07 +0530 | [diff] [blame] | 223 | return entries_to_fix |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 224 | elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse == self.args.warehouse: |
Nabin Hait | 7177579 | 2021-02-02 22:03:07 +0530 | [diff] [blame] | 225 | return entries_to_fix |
| 226 | elif dependant_sle.item_code != self.item_code: |
| 227 | if (dependant_sle.item_code, dependant_sle.warehouse) not in self.new_items: |
| 228 | self.new_items[(dependant_sle.item_code, dependant_sle.warehouse)] = dependant_sle |
| 229 | return entries_to_fix |
| 230 | elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse in self.data: |
| 231 | return entries_to_fix |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 232 | |
| 233 | self.initialize_previous_data(dependant_sle) |
| 234 | |
| 235 | args = self.data[dependant_sle.warehouse].previous_sle \ |
| 236 | or frappe._dict({"item_code": self.item_code, "warehouse": dependant_sle.warehouse}) |
| 237 | future_sle_for_dependant = list(self.get_sle_after_datetime(args)) |
| 238 | |
| 239 | entries_to_fix.extend(future_sle_for_dependant) |
Nabin Hait | 7177579 | 2021-02-02 22:03:07 +0530 | [diff] [blame] | 240 | return sorted(entries_to_fix, key=lambda k: k['timestamp']) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 241 | |
| 242 | def process_sle(self, sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 243 | # previous sle data for this warehouse |
| 244 | self.wh_data = self.data[sle.warehouse] |
| 245 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 246 | 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] | 247 | # validate negative stock for serialized items, fifo valuation |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 248 | # or when negative stock is not allowed for moving average |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 249 | if not self.validate_negative_stock(sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 250 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 251 | return |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 252 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 253 | # Get dynamic incoming/outgoing rate |
| 254 | self.get_dynamic_incoming_outgoing_rate(sle) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 255 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 256 | if sle.serial_no: |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 257 | self.get_serialized_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 258 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 259 | if sle.voucher_type == "Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 260 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 261 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 262 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt(self.wh_data.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 263 | else: |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 264 | if sle.voucher_type=="Stock Reconciliation" and not sle.batch_no: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 265 | # assert |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 266 | self.wh_data.valuation_rate = sle.valuation_rate |
| 267 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
| 268 | self.wh_data.stock_queue = [[self.wh_data.qty_after_transaction, self.wh_data.valuation_rate]] |
| 269 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt(self.wh_data.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 270 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 271 | if self.valuation_method == "Moving Average": |
| 272 | self.get_moving_average_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 273 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
| 274 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt(self.wh_data.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 275 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 276 | self.get_fifo_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 277 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
| 278 | self.wh_data.stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.wh_data.stock_queue)) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 279 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 280 | # rounding as per precision |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 281 | self.wh_data.stock_value = flt(self.wh_data.stock_value, self.precision) |
| 282 | stock_value_difference = self.wh_data.stock_value - self.wh_data.prev_stock_value |
| 283 | self.wh_data.prev_stock_value = self.wh_data.stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 284 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 285 | # update current sle |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 286 | sle.qty_after_transaction = self.wh_data.qty_after_transaction |
| 287 | sle.valuation_rate = self.wh_data.valuation_rate |
| 288 | sle.stock_value = self.wh_data.stock_value |
| 289 | sle.stock_queue = json.dumps(self.wh_data.stock_queue) |
Rushabh Mehta | 2e0e711 | 2015-02-18 11:38:05 +0530 | [diff] [blame] | 290 | sle.stock_value_difference = stock_value_difference |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 291 | sle.doctype="Stock Ledger Entry" |
| 292 | frappe.get_doc(sle).db_update() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 293 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 294 | self.update_outgoing_rate_on_transaction(sle) |
| 295 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 296 | def validate_negative_stock(self, sle): |
| 297 | """ |
| 298 | validate negative stock for entries current datetime onwards |
| 299 | will not consider cancelled entries |
| 300 | """ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 301 | diff = self.wh_data.qty_after_transaction + flt(sle.actual_qty) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 302 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 303 | if diff < 0 and abs(diff) > 0.0001: |
| 304 | # negative stock! |
| 305 | exc = sle.copy().update({"diff": diff}) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 306 | self.exceptions.setdefault(sle.warehouse, []).append(exc) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 307 | return False |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 308 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 309 | return True |
| 310 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 311 | def get_dynamic_incoming_outgoing_rate(self, sle): |
| 312 | # Get updated incoming/outgoing rate from transaction |
| 313 | if sle.recalculate_rate: |
| 314 | rate = self.get_incoming_outgoing_rate_from_transaction(sle) |
| 315 | |
| 316 | if flt(sle.actual_qty) >= 0: |
| 317 | sle.incoming_rate = rate |
| 318 | else: |
| 319 | sle.outgoing_rate = rate |
| 320 | |
| 321 | def get_incoming_outgoing_rate_from_transaction(self, sle): |
| 322 | rate = 0 |
| 323 | # Material Transfer, Repack, Manufacturing |
| 324 | if sle.voucher_type == "Stock Entry": |
| 325 | rate = frappe.db.get_value("Stock Entry Detail", sle.voucher_detail_no, "valuation_rate") |
| 326 | # Sales and Purchase Return |
| 327 | elif sle.voucher_type in ("Purchase Receipt", "Purchase Invoice", "Delivery Note", "Sales Invoice"): |
| 328 | if frappe.get_cached_value(sle.voucher_type, sle.voucher_no, "is_return"): |
| 329 | from erpnext.controllers.sales_and_purchase_return import get_rate_for_return # don't move this import to top |
| 330 | rate = get_rate_for_return(sle.voucher_type, sle.voucher_no, sle.item_code, voucher_detail_no=sle.voucher_detail_no) |
| 331 | else: |
| 332 | if sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 333 | rate_field = "valuation_rate" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 334 | else: |
| 335 | rate_field = "incoming_rate" |
| 336 | |
| 337 | # check in item table |
| 338 | item_code, incoming_rate = frappe.db.get_value(sle.voucher_type + " Item", |
| 339 | sle.voucher_detail_no, ["item_code", rate_field]) |
| 340 | |
| 341 | if item_code == sle.item_code: |
| 342 | rate = incoming_rate |
| 343 | else: |
| 344 | if sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 345 | ref_doctype = "Packed Item" |
| 346 | else: |
| 347 | ref_doctype = "Purchase Receipt Item Supplied" |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 348 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 349 | rate = frappe.db.get_value(ref_doctype, {"parent_detail_docname": sle.voucher_detail_no, |
| 350 | "item_code": sle.item_code}, rate_field) |
| 351 | |
| 352 | return rate |
| 353 | |
| 354 | def update_outgoing_rate_on_transaction(self, sle): |
| 355 | """ |
| 356 | Update outgoing rate in Stock Entry, Delivery Note, Sales Invoice and Sales Return |
| 357 | In case of Stock Entry, also calculate FG Item rate and total incoming/outgoing amount |
| 358 | """ |
| 359 | if sle.actual_qty and sle.voucher_detail_no: |
| 360 | outgoing_rate = abs(flt(sle.stock_value_difference)) / abs(sle.actual_qty) |
| 361 | |
| 362 | if flt(sle.actual_qty) < 0 and sle.voucher_type == "Stock Entry": |
| 363 | self.update_rate_on_stock_entry(sle, outgoing_rate) |
| 364 | elif sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 365 | self.update_rate_on_delivery_and_sales_return(sle, outgoing_rate) |
| 366 | elif flt(sle.actual_qty) < 0 and sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
| 367 | self.update_rate_on_purchase_receipt(sle, outgoing_rate) |
| 368 | |
| 369 | def update_rate_on_stock_entry(self, sle, outgoing_rate): |
| 370 | frappe.db.set_value("Stock Entry Detail", sle.voucher_detail_no, "basic_rate", outgoing_rate) |
| 371 | |
| 372 | # Update outgoing item's rate, recalculate FG Item's rate and total incoming/outgoing amount |
| 373 | stock_entry = frappe.get_doc("Stock Entry", sle.voucher_no) |
| 374 | stock_entry.calculate_rate_and_amount(reset_outgoing_rate=False, raise_error_if_no_rate=False) |
| 375 | stock_entry.db_update() |
| 376 | for d in stock_entry.items: |
| 377 | d.db_update() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 378 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 379 | def update_rate_on_delivery_and_sales_return(self, sle, outgoing_rate): |
| 380 | # Update item's incoming rate on transaction |
| 381 | item_code = frappe.db.get_value(sle.voucher_type + " Item", sle.voucher_detail_no, "item_code") |
| 382 | if item_code == sle.item_code: |
| 383 | frappe.db.set_value(sle.voucher_type + " Item", sle.voucher_detail_no, "incoming_rate", outgoing_rate) |
| 384 | else: |
| 385 | # packed item |
| 386 | frappe.db.set_value("Packed Item", |
| 387 | {"parent_detail_docname": sle.voucher_detail_no, "item_code": sle.item_code}, |
| 388 | "incoming_rate", outgoing_rate) |
| 389 | |
| 390 | def update_rate_on_purchase_receipt(self, sle, outgoing_rate): |
| 391 | if frappe.db.exists(sle.voucher_type + " Item", sle.voucher_detail_no): |
| 392 | frappe.db.set_value(sle.voucher_type + " Item", sle.voucher_detail_no, "base_net_rate", outgoing_rate) |
| 393 | else: |
| 394 | frappe.db.set_value("Purchase Receipt Item Supplied", sle.voucher_detail_no, "rate", outgoing_rate) |
| 395 | |
| 396 | # Recalculate subcontracted item's rate in case of subcontracted purchase receipt/invoice |
| 397 | if frappe.db.get_value(sle.voucher_type, sle.voucher_no, "is_subcontracted"): |
| 398 | doc = frappe.get_cached_doc(sle.voucher_type, sle.voucher_no) |
| 399 | doc.update_valuation_rate(reset_outgoing_rate=False) |
| 400 | for d in (doc.items + doc.supplied_items): |
| 401 | d.db_update() |
| 402 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 403 | def get_serialized_values(self, sle): |
| 404 | incoming_rate = flt(sle.incoming_rate) |
| 405 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 406 | serial_nos = cstr(sle.serial_no).split("\n") |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 407 | |
| 408 | if incoming_rate < 0: |
| 409 | # wrong incoming rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 410 | incoming_rate = self.wh_data.valuation_rate |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 411 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 412 | stock_value_change = 0 |
| 413 | if incoming_rate: |
| 414 | stock_value_change = actual_qty * incoming_rate |
| 415 | elif actual_qty < 0: |
| 416 | # In case of delivery/stock issue, get average purchase rate |
| 417 | # of serial nos of current entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 418 | if not sle.is_cancelled: |
| 419 | outgoing_value = self.get_incoming_value_for_serial_nos(sle, serial_nos) |
| 420 | stock_value_change = -1 * outgoing_value |
| 421 | else: |
| 422 | stock_value_change = actual_qty * sle.outgoing_rate |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 423 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 424 | new_stock_qty = self.wh_data.qty_after_transaction + actual_qty |
rohitwaghchaure | 0fe6ced | 2018-07-27 10:33:30 +0530 | [diff] [blame] | 425 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 426 | if new_stock_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 427 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + stock_value_change |
rohitwaghchaure | 0fe6ced | 2018-07-27 10:33:30 +0530 | [diff] [blame] | 428 | if new_stock_value >= 0: |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 429 | # calculate new valuation rate only if stock value is positive |
| 430 | # else it remains the same as that of previous entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 431 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 432 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 433 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 434 | allow_zero_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 435 | if not allow_zero_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 436 | self.wh_data.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 437 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
| 438 | currency=erpnext.get_company_currency(sle.company)) |
| 439 | |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 440 | def get_incoming_value_for_serial_nos(self, sle, serial_nos): |
| 441 | # get rate from serial nos within same company |
| 442 | all_serial_nos = frappe.get_all("Serial No", |
| 443 | fields=["purchase_rate", "name", "company"], |
| 444 | filters = {'name': ('in', serial_nos)}) |
| 445 | |
| 446 | incoming_values = sum([flt(d.purchase_rate) for d in all_serial_nos if d.company==sle.company]) |
| 447 | |
| 448 | # Get rate for serial nos which has been transferred to other company |
| 449 | invalid_serial_nos = [d.name for d in all_serial_nos if d.company!=sle.company] |
| 450 | for serial_no in invalid_serial_nos: |
| 451 | incoming_rate = frappe.db.sql(""" |
| 452 | select incoming_rate |
| 453 | from `tabStock Ledger Entry` |
| 454 | where |
| 455 | company = %s |
| 456 | and actual_qty > 0 |
| 457 | and (serial_no = %s |
| 458 | or serial_no like %s |
| 459 | or serial_no like %s |
| 460 | or serial_no like %s |
| 461 | ) |
| 462 | order by posting_date desc |
| 463 | limit 1 |
| 464 | """, (sle.company, serial_no, serial_no+'\n%', '%\n'+serial_no, '%\n'+serial_no+'\n%')) |
| 465 | |
| 466 | incoming_values += flt(incoming_rate[0][0]) if incoming_rate else 0 |
| 467 | |
| 468 | return incoming_values |
| 469 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 470 | def get_moving_average_values(self, sle): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 471 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 472 | new_stock_qty = flt(self.wh_data.qty_after_transaction) + actual_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 473 | if new_stock_qty >= 0: |
| 474 | if actual_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 475 | if flt(self.wh_data.qty_after_transaction) <= 0: |
| 476 | self.wh_data.valuation_rate = sle.incoming_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 477 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 478 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + \ |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 479 | (actual_qty * sle.incoming_rate) |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 480 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 481 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 482 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 483 | elif sle.outgoing_rate: |
| 484 | if new_stock_qty: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 485 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + \ |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 486 | (actual_qty * sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 487 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 488 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 489 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 490 | self.wh_data.valuation_rate = sle.outgoing_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 491 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 492 | if flt(self.wh_data.qty_after_transaction) >= 0 and sle.outgoing_rate: |
| 493 | self.wh_data.valuation_rate = sle.outgoing_rate |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 494 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 495 | if not self.wh_data.valuation_rate and actual_qty > 0: |
| 496 | self.wh_data.valuation_rate = sle.incoming_rate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 497 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 498 | # 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] | 499 | # allow zero valuration rate flag set |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 500 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 501 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 502 | if not allow_zero_valuation_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 503 | self.wh_data.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 504 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
| 505 | currency=erpnext.get_company_currency(sle.company)) |
| 506 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 507 | def get_fifo_values(self, sle): |
| 508 | incoming_rate = flt(sle.incoming_rate) |
| 509 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 510 | outgoing_rate = flt(sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 511 | |
| 512 | if actual_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 513 | if not self.wh_data.stock_queue: |
| 514 | self.wh_data.stock_queue.append([0, 0]) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 515 | |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 516 | # last row has the same rate, just updated the qty |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 517 | if self.wh_data.stock_queue[-1][1]==incoming_rate: |
| 518 | self.wh_data.stock_queue[-1][0] += actual_qty |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 519 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 520 | if self.wh_data.stock_queue[-1][0] > 0: |
| 521 | self.wh_data.stock_queue.append([actual_qty, incoming_rate]) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 522 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 523 | qty = self.wh_data.stock_queue[-1][0] + actual_qty |
| 524 | self.wh_data.stock_queue[-1] = [qty, incoming_rate] |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 525 | else: |
| 526 | qty_to_pop = abs(actual_qty) |
| 527 | while qty_to_pop: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 528 | if not self.wh_data.stock_queue: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 529 | # 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] | 530 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 531 | if not allow_zero_valuation_rate: |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 532 | _rate = get_valuation_rate(sle.item_code, sle.warehouse, |
| 533 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
| 534 | currency=erpnext.get_company_currency(sle.company)) |
Nabin Hait | 0a6aaf4 | 2017-02-07 01:23:26 +0530 | [diff] [blame] | 535 | else: |
| 536 | _rate = 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 537 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 538 | self.wh_data.stock_queue.append([0, _rate]) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 539 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 540 | index = None |
| 541 | if outgoing_rate > 0: |
| 542 | # Find the entry where rate matched with outgoing rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 543 | for i, v in enumerate(self.wh_data.stock_queue): |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 544 | if v[1] == outgoing_rate: |
| 545 | index = i |
| 546 | break |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 547 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 548 | # If no entry found with outgoing rate, collapse stack |
| 549 | if index == None: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 550 | new_stock_value = sum((d[0]*d[1] for d in self.wh_data.stock_queue)) - qty_to_pop*outgoing_rate |
| 551 | new_stock_qty = sum((d[0] for d in self.wh_data.stock_queue)) - qty_to_pop |
| 552 | self.wh_data.stock_queue = [[new_stock_qty, new_stock_value/new_stock_qty if new_stock_qty > 0 else outgoing_rate]] |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 553 | break |
| 554 | else: |
| 555 | index = 0 |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 556 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 557 | # select first batch or the batch with same rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 558 | batch = self.wh_data.stock_queue[index] |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 559 | if qty_to_pop >= batch[0]: |
| 560 | # consume current batch |
| 561 | qty_to_pop = qty_to_pop - batch[0] |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 562 | self.wh_data.stock_queue.pop(index) |
| 563 | if not self.wh_data.stock_queue and qty_to_pop: |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 564 | # stock finished, qty still remains to be withdrawn |
| 565 | # negative stock, keep in as a negative batch |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 566 | self.wh_data.stock_queue.append([-qty_to_pop, outgoing_rate or batch[1]]) |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 567 | break |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 568 | |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 569 | else: |
| 570 | # qty found in current batch |
| 571 | # consume it and exit |
| 572 | batch[0] = batch[0] - qty_to_pop |
| 573 | qty_to_pop = 0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 574 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 575 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in self.wh_data.stock_queue)) |
| 576 | stock_qty = sum((flt(batch[0]) for batch in self.wh_data.stock_queue)) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 577 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 578 | if stock_qty: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 579 | self.wh_data.valuation_rate = stock_value / flt(stock_qty) |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 580 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 581 | if not self.wh_data.stock_queue: |
| 582 | self.wh_data.stock_queue.append([0, sle.incoming_rate or sle.outgoing_rate or self.wh_data.valuation_rate]) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 583 | |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 584 | def check_if_allow_zero_valuation_rate(self, voucher_type, voucher_detail_no): |
deepeshgarg007 | f9c0ef3 | 2019-07-30 18:49:19 +0530 | [diff] [blame] | 585 | ref_item_dt = "" |
| 586 | |
| 587 | if voucher_type == "Stock Entry": |
| 588 | ref_item_dt = voucher_type + " Detail" |
| 589 | elif voucher_type in ["Purchase Invoice", "Sales Invoice", "Delivery Note", "Purchase Receipt"]: |
| 590 | ref_item_dt = voucher_type + " Item" |
| 591 | |
| 592 | if ref_item_dt: |
| 593 | return frappe.db.get_value(ref_item_dt, voucher_detail_no, "allow_zero_valuation_rate") |
| 594 | else: |
| 595 | return 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 596 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 597 | def get_sle_before_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 598 | """get previous stock ledger entry before current time-bucket""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 599 | sle = get_stock_ledger_entries(args, "<", "desc", "limit 1", for_update=False) |
| 600 | sle = sle[0] if sle else frappe._dict() |
| 601 | return sle |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 602 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 603 | def get_sle_after_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 604 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 605 | return get_stock_ledger_entries(args, ">", "asc", for_update=True, check_serial_no=False) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 606 | |
| 607 | def raise_exceptions(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 608 | msg_list = [] |
| 609 | for warehouse, exceptions in iteritems(self.exceptions): |
| 610 | deficiency = min(e["diff"] for e in exceptions) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 611 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 612 | if ((exceptions[0]["voucher_type"], exceptions[0]["voucher_no"]) in |
| 613 | frappe.local.flags.currently_saving): |
Nabin Hait | 3edefb1 | 2016-07-20 16:13:18 +0530 | [diff] [blame] | 614 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 615 | msg = _("{0} units of {1} needed in {2} to complete this transaction.").format( |
Nabin Hait | 7177579 | 2021-02-02 22:03:07 +0530 | [diff] [blame] | 616 | abs(deficiency), frappe.get_desk_link('Item', exceptions[0]["item_code"]), |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 617 | frappe.get_desk_link('Warehouse', warehouse)) |
| 618 | else: |
| 619 | msg = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( |
Nabin Hait | 7177579 | 2021-02-02 22:03:07 +0530 | [diff] [blame] | 620 | abs(deficiency), frappe.get_desk_link('Item', exceptions[0]["item_code"]), |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 621 | frappe.get_desk_link('Warehouse', warehouse), |
| 622 | exceptions[0]["posting_date"], exceptions[0]["posting_time"], |
| 623 | frappe.get_desk_link(exceptions[0]["voucher_type"], exceptions[0]["voucher_no"])) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 624 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 625 | if msg: |
| 626 | msg_list.append(msg) |
| 627 | |
| 628 | if msg_list: |
| 629 | message = "\n\n".join(msg_list) |
| 630 | if self.verbose: |
| 631 | frappe.throw(message, NegativeStockError, title='Insufficient Stock') |
| 632 | else: |
| 633 | raise NegativeStockError(message) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 634 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 635 | def update_bin(self): |
| 636 | # update bin for each warehouse |
| 637 | for warehouse, data in iteritems(self.data): |
| 638 | bin_doc = get_bin(self.item_code, warehouse) |
| 639 | |
| 640 | bin_doc.update({ |
| 641 | "valuation_rate": data.valuation_rate, |
| 642 | "actual_qty": data.qty_after_transaction, |
| 643 | "stock_value": data.stock_value |
| 644 | }) |
| 645 | bin_doc.flags.via_stock_ledger_entry = True |
| 646 | bin_doc.save(ignore_permissions=True) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 647 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 648 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 649 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 650 | get the last sle on or before the current time-bucket, |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 651 | to get actual qty before transaction, this function |
| 652 | is called from various transaction like stock entry, reco etc |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 653 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 654 | args = { |
| 655 | "item_code": "ABC", |
| 656 | "warehouse": "XYZ", |
| 657 | "posting_date": "2012-12-12", |
| 658 | "posting_time": "12:00", |
| 659 | "sle": "name of reference Stock Ledger Entry" |
| 660 | } |
| 661 | """ |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 662 | args["name"] = args.get("sle", None) or "" |
| 663 | 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] | 664 | return sle and sle[0] or {} |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 665 | |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 666 | def get_stock_ledger_entries(previous_sle, operator=None, |
| 667 | order="desc", limit=None, for_update=False, debug=False, check_serial_no=True): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 668 | """get stock ledger entries filtered by specific posting datetime conditions""" |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 669 | conditions = " and timestamp(posting_date, posting_time) {0} timestamp(%(posting_date)s, %(posting_time)s)".format(operator) |
| 670 | if previous_sle.get("warehouse"): |
| 671 | conditions += " and warehouse = %(warehouse)s" |
| 672 | elif previous_sle.get("warehouse_condition"): |
| 673 | conditions += " and " + previous_sle.get("warehouse_condition") |
| 674 | |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 675 | if check_serial_no and previous_sle.get("serial_no"): |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 676 | conditions += " and serial_no like {}".format(frappe.db.escape('%{0}%'.format(previous_sle.get("serial_no")))) |
| 677 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 678 | if not previous_sle.get("posting_date"): |
| 679 | previous_sle["posting_date"] = "1900-01-01" |
| 680 | if not previous_sle.get("posting_time"): |
| 681 | previous_sle["posting_time"] = "00:00" |
| 682 | |
| 683 | if operator in (">", "<=") and previous_sle.get("name"): |
| 684 | conditions += " and name!=%(name)s" |
| 685 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 686 | return frappe.db.sql(""" |
| 687 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 688 | from `tabStock Ledger Entry` |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 689 | where item_code = %%(item_code)s |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 690 | and is_cancelled = 0 |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 691 | %(conditions)s |
Aditya Hase | 0c16424 | 2019-01-07 22:07:13 +0530 | [diff] [blame] | 692 | order by timestamp(posting_date, posting_time) %(order)s, creation %(order)s |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 693 | %(limit)s %(for_update)s""" % { |
| 694 | "conditions": conditions, |
| 695 | "limit": limit or "", |
| 696 | "for_update": for_update and "for update" or "", |
| 697 | "order": order |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 698 | }, previous_sle, as_dict=1, debug=debug) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 699 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 700 | def get_sle_by_voucher_detail_no(voucher_detail_no, excluded_sle=None): |
| 701 | return frappe.db.get_value('Stock Ledger Entry', |
| 702 | {'voucher_detail_no': voucher_detail_no, 'name': ['!=', excluded_sle]}, |
| 703 | ['item_code', 'warehouse', 'posting_date', 'posting_time', 'timestamp(posting_date, posting_time) as timestamp'], |
| 704 | as_dict=1) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 705 | |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 706 | def get_valuation_rate(item_code, warehouse, voucher_type, voucher_no, |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 707 | allow_zero_rate=False, currency=None, company=None, raise_error_if_no_rate=True): |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 708 | # Get valuation rate from last sle for the same item and warehouse |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 709 | if not company: |
| 710 | company = erpnext.get_default_company() |
| 711 | |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 712 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 713 | from `tabStock Ledger Entry` |
Mangesh-Khairnar | 0df5134 | 2019-08-19 10:04:52 +0530 | [diff] [blame] | 714 | where |
| 715 | item_code = %s |
| 716 | AND warehouse = %s |
| 717 | AND valuation_rate >= 0 |
| 718 | AND NOT (voucher_no = %s AND voucher_type = %s) |
| 719 | order by posting_date desc, posting_time desc, name desc limit 1""", (item_code, warehouse, voucher_no, voucher_type)) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 720 | |
| 721 | if not last_valuation_rate: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 722 | # Get valuation rate from last sle for the item against any warehouse |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 723 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 724 | from `tabStock Ledger Entry` |
Mangesh-Khairnar | 0df5134 | 2019-08-19 10:04:52 +0530 | [diff] [blame] | 725 | where |
| 726 | item_code = %s |
| 727 | AND valuation_rate > 0 |
| 728 | AND NOT(voucher_no = %s AND voucher_type = %s) |
| 729 | order by posting_date desc, posting_time desc, name desc limit 1""", (item_code, voucher_no, voucher_type)) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 730 | |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 731 | if last_valuation_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 732 | return flt(last_valuation_rate[0][0]) |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 733 | |
| 734 | # If negative stock allowed, and item delivered without any incoming entry, |
| 735 | # system does not found any SLE, then take valuation rate from Item |
| 736 | valuation_rate = frappe.db.get_value("Item", item_code, "valuation_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 737 | |
| 738 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 739 | # try Item Standard rate |
| 740 | valuation_rate = frappe.db.get_value("Item", item_code, "standard_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 741 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 742 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 743 | # try in price list |
| 744 | valuation_rate = frappe.db.get_value('Item Price', |
| 745 | dict(item_code=item_code, buying=1, currency=currency), |
| 746 | 'price_list_rate') |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 747 | |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 748 | if not allow_zero_rate and not valuation_rate and raise_error_if_no_rate \ |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 749 | and cint(erpnext.is_perpetual_inventory_enabled(company)): |
Neil Trini Lasrado | 193c891 | 2017-03-28 17:39:34 +0530 | [diff] [blame] | 750 | frappe.local.message_log = [] |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 751 | form_link = frappe.utils.get_link_to_form("Item", item_code) |
| 752 | |
| 753 | message = _("Valuation Rate for the Item {0}, is required to do accounting entries for {1} {2}.").format(form_link, voucher_type, voucher_no) |
| 754 | message += "<br><br>" + _(" Here are the options to proceed:") |
| 755 | solutions = "<li>" + _("If the item is transacting as a Zero Valuation Rate item in this entry, please enable 'Allow Zero Valuation Rate' in the {0} Item table.").format(voucher_type) + "</li>" |
| 756 | solutions += "<li>" + _("If not, you can Cancel / Submit this entry ") + _("{0}").format(frappe.bold("after")) + _(" performing either one below:") + "</li>" |
| 757 | sub_solutions = "<ul><li>" + _("Create an incoming stock transaction for the Item.") + "</li>" |
| 758 | sub_solutions += "<li>" + _("Mention Valuation Rate in the Item master.") + "</li></ul>" |
| 759 | msg = message + solutions + sub_solutions + "</li>" |
| 760 | |
| 761 | frappe.throw(msg=msg, title=_("Valuation Rate Missing")) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 762 | |
| 763 | return valuation_rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 764 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 765 | def validate_negative_qty_in_future_sle(args, allow_negative_stock=None): |
| 766 | allow_negative_stock = allow_negative_stock \ |
| 767 | or cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock")) |
| 768 | |
| 769 | if args.actual_qty < 0 and not allow_negative_stock: |
| 770 | sle = get_future_sle_with_negative_qty(args) |
| 771 | if sle: |
| 772 | message = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( |
| 773 | abs(sle[0]["qty_after_transaction"]), |
| 774 | frappe.get_desk_link('Item', args.item_code), |
| 775 | frappe.get_desk_link('Warehouse', args.warehouse), |
| 776 | sle[0]["posting_date"], sle[0]["posting_time"], |
| 777 | frappe.get_desk_link(sle[0]["voucher_type"], sle[0]["voucher_no"])) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 778 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 779 | frappe.throw(message, NegativeStockError, title='Insufficient Stock') |
| 780 | |
| 781 | def get_future_sle_with_negative_qty(args): |
| 782 | return frappe.db.sql(""" |
| 783 | select |
| 784 | qty_after_transaction, posting_date, posting_time, |
| 785 | voucher_type, voucher_no |
| 786 | from `tabStock Ledger Entry` |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 787 | where |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 788 | item_code = %(item_code)s |
| 789 | and warehouse = %(warehouse)s |
| 790 | and voucher_no != %(voucher_no)s |
| 791 | and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) |
| 792 | and is_cancelled = 0 |
Nabin Hait | 7177579 | 2021-02-02 22:03:07 +0530 | [diff] [blame] | 793 | and qty_after_transaction + {0} < 0 |
| 794 | order by timestamp(posting_date, posting_time) asc |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 795 | limit 1 |
rohitwaghchaure | aa00eb9 | 2021-02-11 13:23:01 +0530 | [diff] [blame^] | 796 | """.format(args.actual_qty), args, as_dict=1) |