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