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 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 5 | import copy |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 6 | import json |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 7 | |
| 8 | import frappe |
| 9 | from frappe import _ |
| 10 | from frappe.model.meta import get_field_precision |
| 11 | from frappe.utils import cint, cstr, flt, get_link_to_form, getdate, now |
Achilles Rasquinha | 361366e | 2018-02-14 17:08:59 +0530 | [diff] [blame] | 12 | from six import iteritems |
| 13 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 14 | import erpnext |
| 15 | from erpnext.stock.utils import ( |
| 16 | get_bin, |
| 17 | get_incoming_outgoing_rate_for_cancel, |
| 18 | get_valuation_method, |
| 19 | ) |
| 20 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 21 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 22 | # future reposting |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 23 | class NegativeStockError(frappe.ValidationError): pass |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 24 | class SerialNoExistsInFutureTransaction(frappe.ValidationError): |
| 25 | pass |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 26 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 27 | _exceptions = frappe.local('stockledger_exceptions') |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 28 | # _exceptions = [] |
Anand Doshi | 5b004ff | 2013-09-25 19:55:41 +0530 | [diff] [blame] | 29 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 30 | def make_sl_entries(sl_entries, allow_negative_stock=False, via_landed_cost_voucher=False): |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 31 | from erpnext.controllers.stock_controller import future_sle_exists |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 32 | if sl_entries: |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 33 | from erpnext.stock.utils import update_bin |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 34 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 35 | cancel = sl_entries[0].get("is_cancelled") |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 36 | if cancel: |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 37 | validate_cancellation(sl_entries) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 38 | 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] | 39 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 40 | args = get_args_for_future_sle(sl_entries[0]) |
| 41 | future_sle_exists(args, sl_entries) |
| 42 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 43 | for sle in sl_entries: |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 44 | if sle.serial_no: |
| 45 | validate_serial_no(sle) |
| 46 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 47 | if cancel: |
| 48 | sle['actual_qty'] = -flt(sle.get('actual_qty')) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 49 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 50 | if sle['actual_qty'] < 0 and not sle.get('outgoing_rate'): |
| 51 | sle['outgoing_rate'] = get_incoming_outgoing_rate_for_cancel(sle.item_code, |
| 52 | sle.voucher_type, sle.voucher_no, sle.voucher_detail_no) |
| 53 | sle['incoming_rate'] = 0.0 |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 54 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 55 | if sle['actual_qty'] > 0 and not sle.get('incoming_rate'): |
| 56 | sle['incoming_rate'] = get_incoming_outgoing_rate_for_cancel(sle.item_code, |
| 57 | sle.voucher_type, sle.voucher_no, sle.voucher_detail_no) |
| 58 | sle['outgoing_rate'] = 0.0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 59 | |
Nabin Hait | 5288bde | 2014-11-03 15:08:21 +0530 | [diff] [blame] | 60 | if sle.get("actual_qty") or sle.get("voucher_type")=="Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 61 | sle_doc = make_entry(sle, allow_negative_stock, via_landed_cost_voucher) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 62 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 63 | args = sle_doc.as_dict() |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 64 | |
| 65 | if sle.get("voucher_type") == "Stock Reconciliation": |
| 66 | # preserve previous_qty_after_transaction for qty reposting |
| 67 | args.previous_qty_after_transaction = sle.get("previous_qty_after_transaction") |
| 68 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 69 | update_bin(args, allow_negative_stock, via_landed_cost_voucher) |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 70 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 71 | def get_args_for_future_sle(row): |
| 72 | return frappe._dict({ |
| 73 | 'voucher_type': row.get('voucher_type'), |
| 74 | 'voucher_no': row.get('voucher_no'), |
| 75 | 'posting_date': row.get('posting_date'), |
| 76 | 'posting_time': row.get('posting_time') |
| 77 | }) |
| 78 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 79 | def validate_serial_no(sle): |
| 80 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
| 81 | for sn in get_serial_nos(sle.serial_no): |
| 82 | args = copy.deepcopy(sle) |
| 83 | args.serial_no = sn |
| 84 | args.warehouse = '' |
| 85 | |
| 86 | vouchers = [] |
| 87 | for row in get_stock_ledger_entries(args, '>'): |
| 88 | voucher_type = frappe.bold(row.voucher_type) |
| 89 | voucher_no = frappe.bold(get_link_to_form(row.voucher_type, row.voucher_no)) |
| 90 | vouchers.append(f'{voucher_type} {voucher_no}') |
| 91 | |
| 92 | if vouchers: |
| 93 | serial_no = frappe.bold(sn) |
| 94 | msg = (f'''The serial no {serial_no} has been used in the future transactions so you need to cancel them first. |
| 95 | The list of the transactions are as below.''' + '<br><br><ul><li>') |
| 96 | |
| 97 | msg += '</li><li>'.join(vouchers) |
| 98 | msg += '</li></ul>' |
| 99 | |
| 100 | title = 'Cannot Submit' if not sle.get('is_cancelled') else 'Cannot Cancel' |
| 101 | frappe.throw(_(msg), title=_(title), exc=SerialNoExistsInFutureTransaction) |
| 102 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 103 | def validate_cancellation(args): |
| 104 | if args[0].get("is_cancelled"): |
| 105 | repost_entry = frappe.db.get_value("Repost Item Valuation", { |
| 106 | 'voucher_type': args[0].voucher_type, |
| 107 | 'voucher_no': args[0].voucher_no, |
| 108 | 'docstatus': 1 |
| 109 | }, ['name', 'status'], as_dict=1) |
| 110 | |
| 111 | if repost_entry: |
| 112 | if repost_entry.status == 'In Progress': |
| 113 | frappe.throw(_("Cannot cancel the transaction. Reposting of item valuation on submission is not completed yet.")) |
| 114 | if repost_entry.status == 'Queued': |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 115 | doc = frappe.get_doc("Repost Item Valuation", repost_entry.name) |
| 116 | doc.cancel() |
| 117 | doc.delete() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 118 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 119 | def set_as_cancel(voucher_type, voucher_no): |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 120 | frappe.db.sql("""update `tabStock Ledger Entry` set is_cancelled=1, |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 121 | modified=%s, modified_by=%s |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 122 | where voucher_type=%s and voucher_no=%s and is_cancelled = 0""", |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 123 | (now(), frappe.session.user, voucher_type, voucher_no)) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 124 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 125 | 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] | 126 | args.update({"doctype": "Stock Ledger Entry"}) |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 127 | sle = frappe.get_doc(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 128 | sle.flags.ignore_permissions = 1 |
Nabin Hait | 4ccd8d3 | 2015-01-23 12:18:01 +0530 | [diff] [blame] | 129 | sle.allow_negative_stock=allow_negative_stock |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 130 | sle.via_landed_cost_voucher = via_landed_cost_voucher |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 131 | sle.insert() |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 132 | sle.submit() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 133 | return sle |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 134 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 135 | def repost_future_sle(args=None, voucher_type=None, voucher_no=None, allow_negative_stock=None, via_landed_cost_voucher=False, doc=None): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 136 | if not args and voucher_type and voucher_no: |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 137 | args = get_items_to_be_repost(voucher_type, voucher_no, doc) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 138 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 139 | distinct_item_warehouses = get_distinct_item_warehouse(args, doc) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 140 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 141 | i = get_current_index(doc) or 0 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 142 | while i < len(args): |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 143 | validate_item_warehouse(args[i]) |
| 144 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 145 | obj = update_entries_after({ |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 146 | 'item_code': args[i].get('item_code'), |
| 147 | 'warehouse': args[i].get('warehouse'), |
| 148 | 'posting_date': args[i].get('posting_date'), |
| 149 | 'posting_time': args[i].get('posting_time'), |
| 150 | 'creation': args[i].get('creation'), |
| 151 | 'distinct_item_warehouses': distinct_item_warehouses |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 152 | }, allow_negative_stock=allow_negative_stock, via_landed_cost_voucher=via_landed_cost_voucher) |
| 153 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 154 | distinct_item_warehouses[(args[i].get('item_code'), args[i].get('warehouse'))].reposting_status = True |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 155 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 156 | if obj.new_items_found: |
| 157 | for item_wh, data in iteritems(distinct_item_warehouses): |
| 158 | if ('args_idx' not in data and not data.reposting_status) or (data.sle_changed and data.reposting_status): |
| 159 | data.args_idx = len(args) |
| 160 | args.append(data.sle) |
| 161 | elif data.sle_changed and not data.reposting_status: |
| 162 | args[data.args_idx] = data.sle |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 163 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 164 | data.sle_changed = False |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 165 | i += 1 |
| 166 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 167 | if doc and i % 2 == 0: |
| 168 | update_args_in_repost_item_valuation(doc, i, args, distinct_item_warehouses) |
| 169 | |
| 170 | if doc and args: |
| 171 | update_args_in_repost_item_valuation(doc, i, args, distinct_item_warehouses) |
| 172 | |
| 173 | def validate_item_warehouse(args): |
| 174 | for field in ['item_code', 'warehouse', 'posting_date', 'posting_time']: |
| 175 | if not args.get(field): |
| 176 | validation_msg = f'The field {frappe.unscrub(args.get(field))} is required for the reposting' |
| 177 | frappe.throw(_(validation_msg)) |
| 178 | |
| 179 | def update_args_in_repost_item_valuation(doc, index, args, distinct_item_warehouses): |
| 180 | frappe.db.set_value(doc.doctype, doc.name, { |
| 181 | 'items_to_be_repost': json.dumps(args, default=str), |
| 182 | 'distinct_item_and_warehouse': json.dumps({str(k): v for k,v in distinct_item_warehouses.items()}, default=str), |
| 183 | 'current_index': index |
| 184 | }) |
| 185 | |
| 186 | frappe.db.commit() |
| 187 | |
| 188 | frappe.publish_realtime('item_reposting_progress', { |
| 189 | 'name': doc.name, |
| 190 | 'items_to_be_repost': json.dumps(args, default=str), |
| 191 | 'current_index': index |
| 192 | }) |
| 193 | |
| 194 | def get_items_to_be_repost(voucher_type, voucher_no, doc=None): |
| 195 | if doc and doc.items_to_be_repost: |
| 196 | return json.loads(doc.items_to_be_repost) or [] |
| 197 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 198 | return frappe.db.get_all("Stock Ledger Entry", |
| 199 | filters={"voucher_type": voucher_type, "voucher_no": voucher_no}, |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 200 | fields=["item_code", "warehouse", "posting_date", "posting_time", "creation"], |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 201 | order_by="creation asc", |
| 202 | group_by="item_code, warehouse" |
| 203 | ) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 204 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 205 | def get_distinct_item_warehouse(args=None, doc=None): |
| 206 | distinct_item_warehouses = {} |
| 207 | if doc and doc.distinct_item_and_warehouse: |
| 208 | distinct_item_warehouses = json.loads(doc.distinct_item_and_warehouse) |
| 209 | distinct_item_warehouses = {frappe.safe_eval(k): frappe._dict(v) for k, v in distinct_item_warehouses.items()} |
| 210 | else: |
| 211 | for i, d in enumerate(args): |
| 212 | distinct_item_warehouses.setdefault((d.item_code, d.warehouse), frappe._dict({ |
| 213 | "reposting_status": False, |
| 214 | "sle": d, |
| 215 | "args_idx": i |
| 216 | })) |
| 217 | |
| 218 | return distinct_item_warehouses |
| 219 | |
| 220 | def get_current_index(doc=None): |
| 221 | if doc and doc.current_index: |
| 222 | return doc.current_index |
| 223 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 224 | class update_entries_after(object): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 225 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 226 | update valution rate and qty after transaction |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 227 | from the current time-bucket onwards |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 228 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 229 | :param args: args as dict |
| 230 | |
| 231 | args = { |
| 232 | "item_code": "ABC", |
| 233 | "warehouse": "XYZ", |
| 234 | "posting_date": "2012-12-12", |
| 235 | "posting_time": "12:00" |
| 236 | } |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 237 | """ |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 238 | 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] | 239 | self.exceptions = {} |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 240 | self.verbose = verbose |
| 241 | self.allow_zero_rate = allow_zero_rate |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 242 | self.via_landed_cost_voucher = via_landed_cost_voucher |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 243 | self.allow_negative_stock = allow_negative_stock \ |
| 244 | or cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock")) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 245 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 246 | self.args = frappe._dict(args) |
| 247 | self.item_code = args.get("item_code") |
| 248 | if self.args.sle_id: |
| 249 | self.args['name'] = self.args.sle_id |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 250 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 251 | self.company = frappe.get_cached_value("Warehouse", self.args.warehouse, "company") |
| 252 | self.get_precision() |
| 253 | self.valuation_method = get_valuation_method(self.item_code) |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 254 | |
| 255 | self.new_items_found = False |
| 256 | self.distinct_item_warehouses = args.get("distinct_item_warehouses", frappe._dict()) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 257 | |
| 258 | self.data = frappe._dict() |
| 259 | self.initialize_previous_data(self.args) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 260 | self.build() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 261 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 262 | def get_precision(self): |
| 263 | company_base_currency = frappe.get_cached_value('Company', self.company, "default_currency") |
| 264 | self.precision = get_field_precision(frappe.get_meta("Stock Ledger Entry").get_field("stock_value"), |
| 265 | currency=company_base_currency) |
| 266 | |
| 267 | def initialize_previous_data(self, args): |
| 268 | """ |
| 269 | Get previous sl entries for current item for each related warehouse |
| 270 | and assigns into self.data dict |
| 271 | |
| 272 | :Data Structure: |
| 273 | |
| 274 | self.data = { |
| 275 | warehouse1: { |
| 276 | 'previus_sle': {}, |
| 277 | 'qty_after_transaction': 10, |
| 278 | 'valuation_rate': 100, |
| 279 | 'stock_value': 1000, |
| 280 | 'prev_stock_value': 1000, |
| 281 | 'stock_queue': '[[10, 100]]', |
| 282 | 'stock_value_difference': 1000 |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | """ |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 287 | self.data.setdefault(args.warehouse, frappe._dict()) |
| 288 | warehouse_dict = self.data[args.warehouse] |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 289 | previous_sle = get_previous_sle_of_current_voucher(args) |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 290 | warehouse_dict.previous_sle = previous_sle |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 291 | |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 292 | for key in ("qty_after_transaction", "valuation_rate", "stock_value"): |
| 293 | setattr(warehouse_dict, key, flt(previous_sle.get(key))) |
| 294 | |
| 295 | warehouse_dict.update({ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 296 | "prev_stock_value": previous_sle.stock_value or 0.0, |
| 297 | "stock_queue": json.loads(previous_sle.stock_queue or "[]"), |
| 298 | "stock_value_difference": 0.0 |
| 299 | }) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 300 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 301 | def build(self): |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 302 | from erpnext.controllers.stock_controller import future_sle_exists |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 303 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 304 | if self.args.get("sle_id"): |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 305 | self.process_sle_against_current_timestamp() |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 306 | if not future_sle_exists(self.args): |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 307 | self.update_bin() |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 308 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 309 | entries_to_fix = self.get_future_entries_to_fix() |
| 310 | |
| 311 | i = 0 |
| 312 | while i < len(entries_to_fix): |
| 313 | sle = entries_to_fix[i] |
| 314 | i += 1 |
| 315 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 316 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 317 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 318 | if sle.dependant_sle_voucher_detail_no: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 319 | entries_to_fix = self.get_dependent_entries_to_fix(entries_to_fix, sle) |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 320 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 321 | self.update_bin() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 322 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 323 | if self.exceptions: |
| 324 | self.raise_exceptions() |
| 325 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 326 | def process_sle_against_current_timestamp(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 327 | sl_entries = self.get_sle_against_current_voucher() |
| 328 | for sle in sl_entries: |
| 329 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 330 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 331 | def get_sle_against_current_voucher(self): |
Nabin Hait | f2be080 | 2021-02-15 19:27:49 +0530 | [diff] [blame] | 332 | self.args['time_format'] = '%H:%i:%s' |
| 333 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 334 | return frappe.db.sql(""" |
| 335 | select |
| 336 | *, timestamp(posting_date, posting_time) as "timestamp" |
| 337 | from |
| 338 | `tabStock Ledger Entry` |
| 339 | where |
| 340 | item_code = %(item_code)s |
| 341 | and warehouse = %(warehouse)s |
rohitwaghchaure | fe4540d | 2021-08-26 12:52:36 +0530 | [diff] [blame] | 342 | and is_cancelled = 0 |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 343 | and timestamp(posting_date, time_format(posting_time, %(time_format)s)) = timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s)) |
| 344 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 345 | order by |
| 346 | creation ASC |
| 347 | for update |
| 348 | """, self.args, as_dict=1) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 349 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 350 | def get_future_entries_to_fix(self): |
| 351 | # includes current entry! |
| 352 | args = self.data[self.args.warehouse].previous_sle \ |
| 353 | or frappe._dict({"item_code": self.item_code, "warehouse": self.args.warehouse}) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 354 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 355 | return list(self.get_sle_after_datetime(args)) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 356 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 357 | def get_dependent_entries_to_fix(self, entries_to_fix, sle): |
| 358 | dependant_sle = get_sle_by_voucher_detail_no(sle.dependant_sle_voucher_detail_no, |
| 359 | excluded_sle=sle.name) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 360 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 361 | if not dependant_sle: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 362 | return entries_to_fix |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 363 | elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse == self.args.warehouse: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 364 | return entries_to_fix |
| 365 | elif dependant_sle.item_code != self.item_code: |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 366 | self.update_distinct_item_warehouses(dependant_sle) |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 367 | return entries_to_fix |
| 368 | elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse in self.data: |
| 369 | return entries_to_fix |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 370 | else: |
| 371 | return self.append_future_sle_for_dependant(dependant_sle, entries_to_fix) |
| 372 | |
| 373 | def update_distinct_item_warehouses(self, dependant_sle): |
| 374 | key = (dependant_sle.item_code, dependant_sle.warehouse) |
| 375 | val = frappe._dict({ |
| 376 | "sle": dependant_sle |
| 377 | }) |
| 378 | if key not in self.distinct_item_warehouses: |
| 379 | self.distinct_item_warehouses[key] = val |
| 380 | self.new_items_found = True |
| 381 | else: |
| 382 | existing_sle_posting_date = self.distinct_item_warehouses[key].get("sle", {}).get("posting_date") |
| 383 | if getdate(dependant_sle.posting_date) < getdate(existing_sle_posting_date): |
| 384 | val.sle_changed = True |
| 385 | self.distinct_item_warehouses[key] = val |
| 386 | self.new_items_found = True |
| 387 | |
| 388 | def append_future_sle_for_dependant(self, dependant_sle, entries_to_fix): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 389 | self.initialize_previous_data(dependant_sle) |
| 390 | |
| 391 | args = self.data[dependant_sle.warehouse].previous_sle \ |
| 392 | or frappe._dict({"item_code": self.item_code, "warehouse": dependant_sle.warehouse}) |
| 393 | future_sle_for_dependant = list(self.get_sle_after_datetime(args)) |
| 394 | |
| 395 | entries_to_fix.extend(future_sle_for_dependant) |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 396 | return sorted(entries_to_fix, key=lambda k: k['timestamp']) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 397 | |
| 398 | def process_sle(self, sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 399 | # previous sle data for this warehouse |
| 400 | self.wh_data = self.data[sle.warehouse] |
| 401 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 402 | 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] | 403 | # validate negative stock for serialized items, fifo valuation |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 404 | # or when negative stock is not allowed for moving average |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 405 | if not self.validate_negative_stock(sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 406 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 407 | return |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 408 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 409 | # Get dynamic incoming/outgoing rate |
rohitwaghchaure | e6a1ad8 | 2021-09-15 20:42:47 +0530 | [diff] [blame] | 410 | if not self.args.get("sle_id"): |
| 411 | self.get_dynamic_incoming_outgoing_rate(sle) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 412 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 413 | if sle.serial_no: |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 414 | self.get_serialized_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 415 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 416 | if sle.voucher_type == "Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 417 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 418 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 419 | 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] | 420 | else: |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 421 | if sle.voucher_type=="Stock Reconciliation" and not sle.batch_no: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 422 | # assert |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 423 | self.wh_data.valuation_rate = sle.valuation_rate |
| 424 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
| 425 | self.wh_data.stock_queue = [[self.wh_data.qty_after_transaction, self.wh_data.valuation_rate]] |
| 426 | 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] | 427 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 428 | if self.valuation_method == "Moving Average": |
| 429 | self.get_moving_average_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 430 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
| 431 | 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] | 432 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 433 | self.get_fifo_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 434 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
| 435 | 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] | 436 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 437 | # rounding as per precision |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 438 | self.wh_data.stock_value = flt(self.wh_data.stock_value, self.precision) |
| 439 | stock_value_difference = self.wh_data.stock_value - self.wh_data.prev_stock_value |
| 440 | self.wh_data.prev_stock_value = self.wh_data.stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 441 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 442 | # update current sle |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 443 | sle.qty_after_transaction = self.wh_data.qty_after_transaction |
| 444 | sle.valuation_rate = self.wh_data.valuation_rate |
| 445 | sle.stock_value = self.wh_data.stock_value |
| 446 | sle.stock_queue = json.dumps(self.wh_data.stock_queue) |
Rushabh Mehta | 2e0e711 | 2015-02-18 11:38:05 +0530 | [diff] [blame] | 447 | sle.stock_value_difference = stock_value_difference |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 448 | sle.doctype="Stock Ledger Entry" |
| 449 | frappe.get_doc(sle).db_update() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 450 | |
rohitwaghchaure | e6a1ad8 | 2021-09-15 20:42:47 +0530 | [diff] [blame] | 451 | if not self.args.get("sle_id"): |
| 452 | self.update_outgoing_rate_on_transaction(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 453 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 454 | def validate_negative_stock(self, sle): |
| 455 | """ |
| 456 | validate negative stock for entries current datetime onwards |
| 457 | will not consider cancelled entries |
| 458 | """ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 459 | diff = self.wh_data.qty_after_transaction + flt(sle.actual_qty) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 460 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 461 | if diff < 0 and abs(diff) > 0.0001: |
| 462 | # negative stock! |
| 463 | exc = sle.copy().update({"diff": diff}) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 464 | self.exceptions.setdefault(sle.warehouse, []).append(exc) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 465 | return False |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 466 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 467 | return True |
| 468 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 469 | def get_dynamic_incoming_outgoing_rate(self, sle): |
| 470 | # Get updated incoming/outgoing rate from transaction |
| 471 | if sle.recalculate_rate: |
| 472 | rate = self.get_incoming_outgoing_rate_from_transaction(sle) |
| 473 | |
| 474 | if flt(sle.actual_qty) >= 0: |
| 475 | sle.incoming_rate = rate |
| 476 | else: |
| 477 | sle.outgoing_rate = rate |
| 478 | |
| 479 | def get_incoming_outgoing_rate_from_transaction(self, sle): |
| 480 | rate = 0 |
| 481 | # Material Transfer, Repack, Manufacturing |
| 482 | if sle.voucher_type == "Stock Entry": |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 483 | self.recalculate_amounts_in_stock_entry(sle.voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 484 | rate = frappe.db.get_value("Stock Entry Detail", sle.voucher_detail_no, "valuation_rate") |
| 485 | # Sales and Purchase Return |
| 486 | elif sle.voucher_type in ("Purchase Receipt", "Purchase Invoice", "Delivery Note", "Sales Invoice"): |
| 487 | if frappe.get_cached_value(sle.voucher_type, sle.voucher_no, "is_return"): |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 488 | from erpnext.controllers.sales_and_purchase_return import ( |
| 489 | get_rate_for_return, # don't move this import to top |
| 490 | ) |
rohitwaghchaure | ce6c3b5 | 2021-04-13 20:55:52 +0530 | [diff] [blame] | 491 | rate = get_rate_for_return(sle.voucher_type, sle.voucher_no, sle.item_code, |
| 492 | voucher_detail_no=sle.voucher_detail_no, sle = sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 493 | else: |
| 494 | if sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 495 | rate_field = "valuation_rate" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 496 | else: |
| 497 | rate_field = "incoming_rate" |
| 498 | |
| 499 | # check in item table |
| 500 | item_code, incoming_rate = frappe.db.get_value(sle.voucher_type + " Item", |
| 501 | sle.voucher_detail_no, ["item_code", rate_field]) |
| 502 | |
| 503 | if item_code == sle.item_code: |
| 504 | rate = incoming_rate |
| 505 | else: |
| 506 | if sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 507 | ref_doctype = "Packed Item" |
| 508 | else: |
| 509 | ref_doctype = "Purchase Receipt Item Supplied" |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 510 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 511 | rate = frappe.db.get_value(ref_doctype, {"parent_detail_docname": sle.voucher_detail_no, |
| 512 | "item_code": sle.item_code}, rate_field) |
| 513 | |
| 514 | return rate |
| 515 | |
| 516 | def update_outgoing_rate_on_transaction(self, sle): |
| 517 | """ |
| 518 | Update outgoing rate in Stock Entry, Delivery Note, Sales Invoice and Sales Return |
| 519 | In case of Stock Entry, also calculate FG Item rate and total incoming/outgoing amount |
| 520 | """ |
| 521 | if sle.actual_qty and sle.voucher_detail_no: |
| 522 | outgoing_rate = abs(flt(sle.stock_value_difference)) / abs(sle.actual_qty) |
| 523 | |
| 524 | if flt(sle.actual_qty) < 0 and sle.voucher_type == "Stock Entry": |
| 525 | self.update_rate_on_stock_entry(sle, outgoing_rate) |
| 526 | elif sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 527 | self.update_rate_on_delivery_and_sales_return(sle, outgoing_rate) |
| 528 | elif flt(sle.actual_qty) < 0 and sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
| 529 | self.update_rate_on_purchase_receipt(sle, outgoing_rate) |
| 530 | |
| 531 | def update_rate_on_stock_entry(self, sle, outgoing_rate): |
| 532 | frappe.db.set_value("Stock Entry Detail", sle.voucher_detail_no, "basic_rate", outgoing_rate) |
| 533 | |
| 534 | # Update outgoing item's rate, recalculate FG Item's rate and total incoming/outgoing amount |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 535 | if not sle.dependant_sle_voucher_detail_no: |
| 536 | self.recalculate_amounts_in_stock_entry(sle.voucher_no) |
| 537 | |
| 538 | def recalculate_amounts_in_stock_entry(self, voucher_no): |
| 539 | stock_entry = frappe.get_doc("Stock Entry", voucher_no, for_update=True) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 540 | stock_entry.calculate_rate_and_amount(reset_outgoing_rate=False, raise_error_if_no_rate=False) |
| 541 | stock_entry.db_update() |
| 542 | for d in stock_entry.items: |
| 543 | d.db_update() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 544 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 545 | def update_rate_on_delivery_and_sales_return(self, sle, outgoing_rate): |
| 546 | # Update item's incoming rate on transaction |
| 547 | item_code = frappe.db.get_value(sle.voucher_type + " Item", sle.voucher_detail_no, "item_code") |
| 548 | if item_code == sle.item_code: |
| 549 | frappe.db.set_value(sle.voucher_type + " Item", sle.voucher_detail_no, "incoming_rate", outgoing_rate) |
| 550 | else: |
| 551 | # packed item |
| 552 | frappe.db.set_value("Packed Item", |
| 553 | {"parent_detail_docname": sle.voucher_detail_no, "item_code": sle.item_code}, |
| 554 | "incoming_rate", outgoing_rate) |
| 555 | |
| 556 | def update_rate_on_purchase_receipt(self, sle, outgoing_rate): |
| 557 | if frappe.db.exists(sle.voucher_type + " Item", sle.voucher_detail_no): |
| 558 | frappe.db.set_value(sle.voucher_type + " Item", sle.voucher_detail_no, "base_net_rate", outgoing_rate) |
| 559 | else: |
| 560 | frappe.db.set_value("Purchase Receipt Item Supplied", sle.voucher_detail_no, "rate", outgoing_rate) |
| 561 | |
| 562 | # Recalculate subcontracted item's rate in case of subcontracted purchase receipt/invoice |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 563 | if frappe.get_cached_value(sle.voucher_type, sle.voucher_no, "is_subcontracted") == 'Yes': |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 564 | doc = frappe.get_doc(sle.voucher_type, sle.voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 565 | doc.update_valuation_rate(reset_outgoing_rate=False) |
| 566 | for d in (doc.items + doc.supplied_items): |
| 567 | d.db_update() |
| 568 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 569 | def get_serialized_values(self, sle): |
| 570 | incoming_rate = flt(sle.incoming_rate) |
| 571 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 572 | serial_nos = cstr(sle.serial_no).split("\n") |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 573 | |
| 574 | if incoming_rate < 0: |
| 575 | # wrong incoming rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 576 | incoming_rate = self.wh_data.valuation_rate |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 577 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 578 | stock_value_change = 0 |
| 579 | if incoming_rate: |
| 580 | stock_value_change = actual_qty * incoming_rate |
| 581 | elif actual_qty < 0: |
| 582 | # In case of delivery/stock issue, get average purchase rate |
| 583 | # of serial nos of current entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 584 | if not sle.is_cancelled: |
| 585 | outgoing_value = self.get_incoming_value_for_serial_nos(sle, serial_nos) |
| 586 | stock_value_change = -1 * outgoing_value |
| 587 | else: |
| 588 | stock_value_change = actual_qty * sle.outgoing_rate |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 589 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 590 | new_stock_qty = self.wh_data.qty_after_transaction + actual_qty |
rohitwaghchaure | 0fe6ced | 2018-07-27 10:33:30 +0530 | [diff] [blame] | 591 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 592 | if new_stock_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 593 | 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] | 594 | if new_stock_value >= 0: |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 595 | # calculate new valuation rate only if stock value is positive |
| 596 | # else it remains the same as that of previous entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 597 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 598 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 599 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 600 | allow_zero_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 601 | if not allow_zero_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 602 | self.wh_data.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 603 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
| 604 | currency=erpnext.get_company_currency(sle.company)) |
| 605 | |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 606 | def get_incoming_value_for_serial_nos(self, sle, serial_nos): |
| 607 | # get rate from serial nos within same company |
| 608 | all_serial_nos = frappe.get_all("Serial No", |
| 609 | fields=["purchase_rate", "name", "company"], |
| 610 | filters = {'name': ('in', serial_nos)}) |
| 611 | |
Ankush Menat | 9891780 | 2021-06-11 18:40:22 +0530 | [diff] [blame] | 612 | incoming_values = sum(flt(d.purchase_rate) for d in all_serial_nos if d.company==sle.company) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 613 | |
| 614 | # Get rate for serial nos which has been transferred to other company |
| 615 | invalid_serial_nos = [d.name for d in all_serial_nos if d.company!=sle.company] |
| 616 | for serial_no in invalid_serial_nos: |
| 617 | incoming_rate = frappe.db.sql(""" |
| 618 | select incoming_rate |
| 619 | from `tabStock Ledger Entry` |
| 620 | where |
| 621 | company = %s |
| 622 | and actual_qty > 0 |
| 623 | and (serial_no = %s |
| 624 | or serial_no like %s |
| 625 | or serial_no like %s |
| 626 | or serial_no like %s |
| 627 | ) |
| 628 | order by posting_date desc |
| 629 | limit 1 |
| 630 | """, (sle.company, serial_no, serial_no+'\n%', '%\n'+serial_no, '%\n'+serial_no+'\n%')) |
| 631 | |
| 632 | incoming_values += flt(incoming_rate[0][0]) if incoming_rate else 0 |
| 633 | |
| 634 | return incoming_values |
| 635 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 636 | def get_moving_average_values(self, sle): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 637 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 638 | new_stock_qty = flt(self.wh_data.qty_after_transaction) + actual_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 639 | if new_stock_qty >= 0: |
| 640 | if actual_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 641 | if flt(self.wh_data.qty_after_transaction) <= 0: |
| 642 | self.wh_data.valuation_rate = sle.incoming_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 643 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 644 | 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] | 645 | (actual_qty * sle.incoming_rate) |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 646 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 647 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 648 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 649 | elif sle.outgoing_rate: |
| 650 | if new_stock_qty: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 651 | 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] | 652 | (actual_qty * sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 653 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 654 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 655 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 656 | self.wh_data.valuation_rate = sle.outgoing_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 657 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 658 | if flt(self.wh_data.qty_after_transaction) >= 0 and sle.outgoing_rate: |
| 659 | self.wh_data.valuation_rate = sle.outgoing_rate |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 660 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 661 | if not self.wh_data.valuation_rate and actual_qty > 0: |
| 662 | self.wh_data.valuation_rate = sle.incoming_rate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 663 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 664 | # 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] | 665 | # allow zero valuration rate flag set |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 666 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 667 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 668 | if not allow_zero_valuation_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 669 | 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] | 670 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
| 671 | currency=erpnext.get_company_currency(sle.company)) |
| 672 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 673 | def get_fifo_values(self, sle): |
| 674 | incoming_rate = flt(sle.incoming_rate) |
| 675 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 676 | outgoing_rate = flt(sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 677 | |
| 678 | if actual_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 679 | if not self.wh_data.stock_queue: |
| 680 | self.wh_data.stock_queue.append([0, 0]) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 681 | |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 682 | # last row has the same rate, just updated the qty |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 683 | if self.wh_data.stock_queue[-1][1]==incoming_rate: |
| 684 | self.wh_data.stock_queue[-1][0] += actual_qty |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 685 | else: |
Frappe PR Bot | d4d5e27 | 2021-09-15 17:07:58 +0530 | [diff] [blame] | 686 | # Item has a positive balance qty, add new entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 687 | if self.wh_data.stock_queue[-1][0] > 0: |
| 688 | self.wh_data.stock_queue.append([actual_qty, incoming_rate]) |
Frappe PR Bot | d4d5e27 | 2021-09-15 17:07:58 +0530 | [diff] [blame] | 689 | else: # negative balance qty |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 690 | qty = self.wh_data.stock_queue[-1][0] + actual_qty |
Frappe PR Bot | d4d5e27 | 2021-09-15 17:07:58 +0530 | [diff] [blame] | 691 | if qty > 0: # new balance qty is positive |
| 692 | self.wh_data.stock_queue[-1] = [qty, incoming_rate] |
| 693 | else: # new balance qty is still negative, maintain same rate |
| 694 | self.wh_data.stock_queue[-1][0] = qty |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 695 | else: |
| 696 | qty_to_pop = abs(actual_qty) |
| 697 | while qty_to_pop: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 698 | if not self.wh_data.stock_queue: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 699 | # 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] | 700 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 701 | if not allow_zero_valuation_rate: |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 702 | _rate = get_valuation_rate(sle.item_code, sle.warehouse, |
| 703 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
| 704 | currency=erpnext.get_company_currency(sle.company)) |
Nabin Hait | 0a6aaf4 | 2017-02-07 01:23:26 +0530 | [diff] [blame] | 705 | else: |
| 706 | _rate = 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 707 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 708 | self.wh_data.stock_queue.append([0, _rate]) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 709 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 710 | index = None |
| 711 | if outgoing_rate > 0: |
| 712 | # Find the entry where rate matched with outgoing rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 713 | for i, v in enumerate(self.wh_data.stock_queue): |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 714 | if v[1] == outgoing_rate: |
| 715 | index = i |
| 716 | break |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 717 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 718 | # If no entry found with outgoing rate, collapse stack |
Ankush Menat | 4dcac4a | 2021-05-21 13:12:30 +0530 | [diff] [blame] | 719 | if index is None: # nosemgrep |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 720 | new_stock_value = sum((d[0]*d[1] for d in self.wh_data.stock_queue)) - qty_to_pop*outgoing_rate |
| 721 | new_stock_qty = sum((d[0] for d in self.wh_data.stock_queue)) - qty_to_pop |
| 722 | 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] | 723 | break |
| 724 | else: |
| 725 | index = 0 |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 726 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 727 | # select first batch or the batch with same rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 728 | batch = self.wh_data.stock_queue[index] |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 729 | if qty_to_pop >= batch[0]: |
| 730 | # consume current batch |
Ankush Menat | 6a014d1 | 2021-04-12 20:21:27 +0530 | [diff] [blame] | 731 | qty_to_pop = _round_off_if_near_zero(qty_to_pop - batch[0]) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 732 | self.wh_data.stock_queue.pop(index) |
| 733 | if not self.wh_data.stock_queue and qty_to_pop: |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 734 | # stock finished, qty still remains to be withdrawn |
| 735 | # negative stock, keep in as a negative batch |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 736 | 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] | 737 | break |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 738 | |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 739 | else: |
| 740 | # qty found in current batch |
| 741 | # consume it and exit |
| 742 | batch[0] = batch[0] - qty_to_pop |
| 743 | qty_to_pop = 0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 744 | |
Ankush Menat | 6a014d1 | 2021-04-12 20:21:27 +0530 | [diff] [blame] | 745 | stock_value = _round_off_if_near_zero(sum((flt(batch[0]) * flt(batch[1]) for batch in self.wh_data.stock_queue))) |
| 746 | stock_qty = _round_off_if_near_zero(sum((flt(batch[0]) for batch in self.wh_data.stock_queue))) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 747 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 748 | if stock_qty: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 749 | self.wh_data.valuation_rate = stock_value / flt(stock_qty) |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 750 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 751 | if not self.wh_data.stock_queue: |
| 752 | 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] | 753 | |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 754 | def check_if_allow_zero_valuation_rate(self, voucher_type, voucher_detail_no): |
deepeshgarg007 | f9c0ef3 | 2019-07-30 18:49:19 +0530 | [diff] [blame] | 755 | ref_item_dt = "" |
| 756 | |
| 757 | if voucher_type == "Stock Entry": |
| 758 | ref_item_dt = voucher_type + " Detail" |
| 759 | elif voucher_type in ["Purchase Invoice", "Sales Invoice", "Delivery Note", "Purchase Receipt"]: |
| 760 | ref_item_dt = voucher_type + " Item" |
| 761 | |
| 762 | if ref_item_dt: |
| 763 | return frappe.db.get_value(ref_item_dt, voucher_detail_no, "allow_zero_valuation_rate") |
| 764 | else: |
| 765 | return 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 766 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 767 | def get_sle_before_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 768 | """get previous stock ledger entry before current time-bucket""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 769 | sle = get_stock_ledger_entries(args, "<", "desc", "limit 1", for_update=False) |
| 770 | sle = sle[0] if sle else frappe._dict() |
| 771 | return sle |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 772 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 773 | def get_sle_after_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 774 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 775 | 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] | 776 | |
| 777 | def raise_exceptions(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 778 | msg_list = [] |
| 779 | for warehouse, exceptions in iteritems(self.exceptions): |
| 780 | deficiency = min(e["diff"] for e in exceptions) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 781 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 782 | if ((exceptions[0]["voucher_type"], exceptions[0]["voucher_no"]) in |
| 783 | frappe.local.flags.currently_saving): |
Nabin Hait | 3edefb1 | 2016-07-20 16:13:18 +0530 | [diff] [blame] | 784 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 785 | msg = _("{0} units of {1} needed in {2} to complete this transaction.").format( |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 786 | abs(deficiency), frappe.get_desk_link('Item', exceptions[0]["item_code"]), |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 787 | frappe.get_desk_link('Warehouse', warehouse)) |
| 788 | else: |
| 789 | msg = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 790 | abs(deficiency), frappe.get_desk_link('Item', exceptions[0]["item_code"]), |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 791 | frappe.get_desk_link('Warehouse', warehouse), |
| 792 | exceptions[0]["posting_date"], exceptions[0]["posting_time"], |
| 793 | frappe.get_desk_link(exceptions[0]["voucher_type"], exceptions[0]["voucher_no"])) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 794 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 795 | if msg: |
| 796 | msg_list.append(msg) |
| 797 | |
| 798 | if msg_list: |
| 799 | message = "\n\n".join(msg_list) |
| 800 | if self.verbose: |
| 801 | frappe.throw(message, NegativeStockError, title='Insufficient Stock') |
| 802 | else: |
| 803 | raise NegativeStockError(message) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 804 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 805 | def update_bin(self): |
| 806 | # update bin for each warehouse |
| 807 | for warehouse, data in iteritems(self.data): |
| 808 | bin_doc = get_bin(self.item_code, warehouse) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 809 | bin_doc.update({ |
| 810 | "valuation_rate": data.valuation_rate, |
| 811 | "actual_qty": data.qty_after_transaction, |
| 812 | "stock_value": data.stock_value |
| 813 | }) |
| 814 | bin_doc.flags.via_stock_ledger_entry = True |
| 815 | bin_doc.save(ignore_permissions=True) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 816 | |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 817 | |
| 818 | def get_previous_sle_of_current_voucher(args, exclude_current_voucher=False): |
| 819 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 820 | |
| 821 | args['time_format'] = '%H:%i:%s' |
| 822 | if not args.get("posting_date"): |
| 823 | args["posting_date"] = "1900-01-01" |
| 824 | if not args.get("posting_time"): |
| 825 | args["posting_time"] = "00:00" |
| 826 | |
| 827 | voucher_condition = "" |
| 828 | if exclude_current_voucher: |
| 829 | voucher_no = args.get("voucher_no") |
| 830 | voucher_condition = f"and voucher_no != '{voucher_no}'" |
| 831 | |
| 832 | sle = frappe.db.sql(""" |
| 833 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 834 | from `tabStock Ledger Entry` |
| 835 | where item_code = %(item_code)s |
| 836 | and warehouse = %(warehouse)s |
| 837 | and is_cancelled = 0 |
| 838 | {voucher_condition} |
| 839 | and timestamp(posting_date, time_format(posting_time, %(time_format)s)) < timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s)) |
| 840 | order by timestamp(posting_date, posting_time) desc, creation desc |
| 841 | limit 1 |
| 842 | for update""".format(voucher_condition=voucher_condition), args, as_dict=1) |
| 843 | |
| 844 | return sle[0] if sle else frappe._dict() |
| 845 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 846 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 847 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 848 | get the last sle on or before the current time-bucket, |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 849 | to get actual qty before transaction, this function |
| 850 | is called from various transaction like stock entry, reco etc |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 851 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 852 | args = { |
| 853 | "item_code": "ABC", |
| 854 | "warehouse": "XYZ", |
| 855 | "posting_date": "2012-12-12", |
| 856 | "posting_time": "12:00", |
| 857 | "sle": "name of reference Stock Ledger Entry" |
| 858 | } |
| 859 | """ |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 860 | args["name"] = args.get("sle", None) or "" |
| 861 | 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] | 862 | return sle and sle[0] or {} |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 863 | |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 864 | def get_stock_ledger_entries(previous_sle, operator=None, |
| 865 | 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] | 866 | """get stock ledger entries filtered by specific posting datetime conditions""" |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 867 | conditions = " and timestamp(posting_date, posting_time) {0} timestamp(%(posting_date)s, %(posting_time)s)".format(operator) |
| 868 | if previous_sle.get("warehouse"): |
| 869 | conditions += " and warehouse = %(warehouse)s" |
| 870 | elif previous_sle.get("warehouse_condition"): |
| 871 | conditions += " and " + previous_sle.get("warehouse_condition") |
| 872 | |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 873 | if check_serial_no and previous_sle.get("serial_no"): |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 874 | # conditions += " and serial_no like {}".format(frappe.db.escape('%{0}%'.format(previous_sle.get("serial_no")))) |
| 875 | serial_no = previous_sle.get("serial_no") |
| 876 | conditions += (""" and |
| 877 | ( |
| 878 | serial_no = {0} |
| 879 | or serial_no like {1} |
| 880 | or serial_no like {2} |
| 881 | or serial_no like {3} |
| 882 | ) |
| 883 | """).format(frappe.db.escape(serial_no), frappe.db.escape('{}\n%'.format(serial_no)), |
| 884 | frappe.db.escape('%\n{}'.format(serial_no)), frappe.db.escape('%\n{}\n%'.format(serial_no))) |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 885 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 886 | if not previous_sle.get("posting_date"): |
| 887 | previous_sle["posting_date"] = "1900-01-01" |
| 888 | if not previous_sle.get("posting_time"): |
| 889 | previous_sle["posting_time"] = "00:00" |
| 890 | |
| 891 | if operator in (">", "<=") and previous_sle.get("name"): |
| 892 | conditions += " and name!=%(name)s" |
| 893 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 894 | return frappe.db.sql(""" |
| 895 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 896 | from `tabStock Ledger Entry` |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 897 | where item_code = %%(item_code)s |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 898 | and is_cancelled = 0 |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 899 | %(conditions)s |
Aditya Hase | 0c16424 | 2019-01-07 22:07:13 +0530 | [diff] [blame] | 900 | order by timestamp(posting_date, posting_time) %(order)s, creation %(order)s |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 901 | %(limit)s %(for_update)s""" % { |
| 902 | "conditions": conditions, |
| 903 | "limit": limit or "", |
| 904 | "for_update": for_update and "for update" or "", |
| 905 | "order": order |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 906 | }, previous_sle, as_dict=1, debug=debug) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 907 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 908 | def get_sle_by_voucher_detail_no(voucher_detail_no, excluded_sle=None): |
| 909 | return frappe.db.get_value('Stock Ledger Entry', |
| 910 | {'voucher_detail_no': voucher_detail_no, 'name': ['!=', excluded_sle]}, |
| 911 | ['item_code', 'warehouse', 'posting_date', 'posting_time', 'timestamp(posting_date, posting_time) as timestamp'], |
| 912 | as_dict=1) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 913 | |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 914 | def get_valuation_rate(item_code, warehouse, voucher_type, voucher_no, |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 915 | 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] | 916 | # Get valuation rate from last sle for the same item and warehouse |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 917 | if not company: |
| 918 | company = erpnext.get_default_company() |
| 919 | |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 920 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 921 | from `tabStock Ledger Entry` |
Mangesh-Khairnar | 0df5134 | 2019-08-19 10:04:52 +0530 | [diff] [blame] | 922 | where |
| 923 | item_code = %s |
| 924 | AND warehouse = %s |
| 925 | AND valuation_rate >= 0 |
| 926 | AND NOT (voucher_no = %s AND voucher_type = %s) |
| 927 | 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] | 928 | |
| 929 | if not last_valuation_rate: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 930 | # Get valuation rate from last sle for the item against any warehouse |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 931 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 932 | from `tabStock Ledger Entry` |
Mangesh-Khairnar | 0df5134 | 2019-08-19 10:04:52 +0530 | [diff] [blame] | 933 | where |
| 934 | item_code = %s |
| 935 | AND valuation_rate > 0 |
| 936 | AND NOT(voucher_no = %s AND voucher_type = %s) |
| 937 | 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] | 938 | |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 939 | if last_valuation_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 940 | return flt(last_valuation_rate[0][0]) |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 941 | |
| 942 | # If negative stock allowed, and item delivered without any incoming entry, |
| 943 | # system does not found any SLE, then take valuation rate from Item |
| 944 | valuation_rate = frappe.db.get_value("Item", item_code, "valuation_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 945 | |
| 946 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 947 | # try Item Standard rate |
| 948 | valuation_rate = frappe.db.get_value("Item", item_code, "standard_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 949 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 950 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 951 | # try in price list |
| 952 | valuation_rate = frappe.db.get_value('Item Price', |
| 953 | dict(item_code=item_code, buying=1, currency=currency), |
| 954 | 'price_list_rate') |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 955 | |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 956 | 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] | 957 | and cint(erpnext.is_perpetual_inventory_enabled(company)): |
Neil Trini Lasrado | 193c891 | 2017-03-28 17:39:34 +0530 | [diff] [blame] | 958 | frappe.local.message_log = [] |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 959 | form_link = get_link_to_form("Item", item_code) |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 960 | |
| 961 | message = _("Valuation Rate for the Item {0}, is required to do accounting entries for {1} {2}.").format(form_link, voucher_type, voucher_no) |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 962 | message += "<br><br>" + _("Here are the options to proceed:") |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 963 | 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>" |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 964 | solutions += "<li>" + _("If not, you can Cancel / Submit this entry") + " {0} ".format(frappe.bold("after")) + _("performing either one below:") + "</li>" |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 965 | sub_solutions = "<ul><li>" + _("Create an incoming stock transaction for the Item.") + "</li>" |
| 966 | sub_solutions += "<li>" + _("Mention Valuation Rate in the Item master.") + "</li></ul>" |
| 967 | msg = message + solutions + sub_solutions + "</li>" |
| 968 | |
| 969 | frappe.throw(msg=msg, title=_("Valuation Rate Missing")) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 970 | |
| 971 | return valuation_rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 972 | |
Ankush Menat | e7109c1 | 2021-08-26 16:40:45 +0530 | [diff] [blame] | 973 | def update_qty_in_future_sle(args, allow_negative_stock=False): |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 974 | """Recalculate Qty after Transaction in future SLEs based on current SLE.""" |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 975 | datetime_limit_condition = "" |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 976 | qty_shift = args.actual_qty |
| 977 | |
| 978 | # find difference/shift in qty caused by stock reconciliation |
| 979 | if args.voucher_type == "Stock Reconciliation": |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 980 | qty_shift = get_stock_reco_qty_shift(args) |
| 981 | |
| 982 | # find the next nearest stock reco so that we only recalculate SLEs till that point |
| 983 | next_stock_reco_detail = get_next_stock_reco(args) |
| 984 | if next_stock_reco_detail: |
| 985 | detail = next_stock_reco_detail[0] |
| 986 | # add condition to update SLEs before this date & time |
| 987 | datetime_limit_condition = get_datetime_limit_condition(detail) |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 988 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 989 | frappe.db.sql(""" |
| 990 | update `tabStock Ledger Entry` |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 991 | set qty_after_transaction = qty_after_transaction + {qty_shift} |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 992 | where |
| 993 | item_code = %(item_code)s |
| 994 | and warehouse = %(warehouse)s |
| 995 | and voucher_no != %(voucher_no)s |
| 996 | and is_cancelled = 0 |
| 997 | and (timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s) |
| 998 | or ( |
| 999 | timestamp(posting_date, posting_time) = timestamp(%(posting_date)s, %(posting_time)s) |
| 1000 | and creation > %(creation)s |
| 1001 | ) |
| 1002 | ) |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1003 | {datetime_limit_condition} |
| 1004 | """.format(qty_shift=qty_shift, datetime_limit_condition=datetime_limit_condition), args) |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1005 | |
| 1006 | validate_negative_qty_in_future_sle(args, allow_negative_stock) |
| 1007 | |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1008 | def get_stock_reco_qty_shift(args): |
| 1009 | stock_reco_qty_shift = 0 |
| 1010 | if args.get("is_cancelled"): |
| 1011 | if args.get("previous_qty_after_transaction"): |
| 1012 | # get qty (balance) that was set at submission |
| 1013 | last_balance = args.get("previous_qty_after_transaction") |
| 1014 | stock_reco_qty_shift = flt(args.qty_after_transaction) - flt(last_balance) |
| 1015 | else: |
| 1016 | stock_reco_qty_shift = flt(args.actual_qty) |
| 1017 | else: |
| 1018 | # reco is being submitted |
| 1019 | last_balance = get_previous_sle_of_current_voucher(args, |
| 1020 | exclude_current_voucher=True).get("qty_after_transaction") |
| 1021 | |
| 1022 | if last_balance is not None: |
| 1023 | stock_reco_qty_shift = flt(args.qty_after_transaction) - flt(last_balance) |
| 1024 | else: |
| 1025 | stock_reco_qty_shift = args.qty_after_transaction |
| 1026 | |
| 1027 | return stock_reco_qty_shift |
| 1028 | |
| 1029 | def get_next_stock_reco(args): |
| 1030 | """Returns next nearest stock reconciliaton's details.""" |
| 1031 | |
| 1032 | return frappe.db.sql(""" |
| 1033 | select |
| 1034 | name, posting_date, posting_time, creation, voucher_no |
| 1035 | from |
marination | 8c44126 | 2021-07-02 17:46:05 +0530 | [diff] [blame] | 1036 | `tabStock Ledger Entry` |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1037 | where |
| 1038 | item_code = %(item_code)s |
| 1039 | and warehouse = %(warehouse)s |
| 1040 | and voucher_type = 'Stock Reconciliation' |
| 1041 | and voucher_no != %(voucher_no)s |
| 1042 | and is_cancelled = 0 |
| 1043 | and (timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s) |
| 1044 | or ( |
| 1045 | timestamp(posting_date, posting_time) = timestamp(%(posting_date)s, %(posting_time)s) |
| 1046 | and creation > %(creation)s |
| 1047 | ) |
| 1048 | ) |
| 1049 | limit 1 |
| 1050 | """, args, as_dict=1) |
| 1051 | |
| 1052 | def get_datetime_limit_condition(detail): |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1053 | return f""" |
| 1054 | and |
| 1055 | (timestamp(posting_date, posting_time) < timestamp('{detail.posting_date}', '{detail.posting_time}') |
| 1056 | or ( |
| 1057 | timestamp(posting_date, posting_time) = timestamp('{detail.posting_date}', '{detail.posting_time}') |
| 1058 | and creation < '{detail.creation}' |
| 1059 | ) |
| 1060 | )""" |
| 1061 | |
Ankush Menat | e7109c1 | 2021-08-26 16:40:45 +0530 | [diff] [blame] | 1062 | def validate_negative_qty_in_future_sle(args, allow_negative_stock=False): |
| 1063 | allow_negative_stock = cint(allow_negative_stock) \ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1064 | or cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock")) |
| 1065 | |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1066 | if (args.actual_qty < 0 or args.voucher_type == "Stock Reconciliation") and not allow_negative_stock: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1067 | sle = get_future_sle_with_negative_qty(args) |
| 1068 | if sle: |
| 1069 | message = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( |
| 1070 | abs(sle[0]["qty_after_transaction"]), |
| 1071 | frappe.get_desk_link('Item', args.item_code), |
| 1072 | frappe.get_desk_link('Warehouse', args.warehouse), |
| 1073 | sle[0]["posting_date"], sle[0]["posting_time"], |
| 1074 | frappe.get_desk_link(sle[0]["voucher_type"], sle[0]["voucher_no"])) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1075 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1076 | frappe.throw(message, NegativeStockError, title='Insufficient Stock') |
| 1077 | |
| 1078 | def get_future_sle_with_negative_qty(args): |
| 1079 | return frappe.db.sql(""" |
| 1080 | select |
| 1081 | qty_after_transaction, posting_date, posting_time, |
| 1082 | voucher_type, voucher_no |
| 1083 | from `tabStock Ledger Entry` |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1084 | where |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1085 | item_code = %(item_code)s |
| 1086 | and warehouse = %(warehouse)s |
| 1087 | and voucher_no != %(voucher_no)s |
| 1088 | and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) |
| 1089 | and is_cancelled = 0 |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1090 | and qty_after_transaction < 0 |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 1091 | order by timestamp(posting_date, posting_time) asc |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1092 | limit 1 |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 1093 | """, args, as_dict=1) |
Ankush Menat | 6a014d1 | 2021-04-12 20:21:27 +0530 | [diff] [blame] | 1094 | |
| 1095 | def _round_off_if_near_zero(number: float, precision: int = 6) -> float: |
| 1096 | """ Rounds off the number to zero only if number is close to zero for decimal |
| 1097 | specified in precision. Precision defaults to 6. |
| 1098 | """ |
| 1099 | if flt(number) < (1.0 / (10**precision)): |
| 1100 | return 0 |
| 1101 | |
| 1102 | return flt(number) |