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 |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 6 | from typing import Optional, Set, Tuple |
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 |
Ankush Menat | e1c1687 | 2022-04-21 20:01:48 +0530 | [diff] [blame] | 11 | from frappe.query_builder.functions import CombineDatetime, Sum |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 12 | 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] | 13 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 14 | import erpnext |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 15 | 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] | 16 | from erpnext.stock.utils import ( |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 17 | get_incoming_outgoing_rate_for_cancel, |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 18 | get_or_make_bin, |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 19 | get_valuation_method, |
| 20 | ) |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 21 | from erpnext.stock.valuation import FIFOValuation, LIFOValuation, round_off_if_near_zero |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 22 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 23 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 24 | class NegativeStockError(frappe.ValidationError): |
| 25 | pass |
| 26 | |
| 27 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 28 | class SerialNoExistsInFutureTransaction(frappe.ValidationError): |
| 29 | pass |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 30 | |
Anand Doshi | 5b004ff | 2013-09-25 19:55:41 +0530 | [diff] [blame] | 31 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 32 | def make_sl_entries(sl_entries, allow_negative_stock=False, via_landed_cost_voucher=False): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 33 | """Create SL entries from SL entry dicts |
Ankush Menat | eb8495a | 2022-03-02 12:01:51 +0530 | [diff] [blame] | 34 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 35 | args: |
| 36 | - allow_negative_stock: disable negative stock valiations if true |
| 37 | - via_landed_cost_voucher: landed cost voucher cancels and reposts |
| 38 | entries of purchase document. This flag is used to identify if |
| 39 | cancellation and repost is happening via landed cost voucher, in |
| 40 | such cases certain validations need to be ignored (like negative |
| 41 | stock) |
Ankush Menat | eb8495a | 2022-03-02 12:01:51 +0530 | [diff] [blame] | 42 | """ |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 43 | from erpnext.controllers.stock_controller import future_sle_exists |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 44 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 45 | if sl_entries: |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 46 | cancel = sl_entries[0].get("is_cancelled") |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 47 | if cancel: |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 48 | validate_cancellation(sl_entries) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | 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] | 50 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 51 | args = get_args_for_future_sle(sl_entries[0]) |
| 52 | future_sle_exists(args, sl_entries) |
| 53 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 54 | for sle in sl_entries: |
Ankush Menat | efc4b94 | 2022-03-02 11:19:12 +0530 | [diff] [blame] | 55 | if sle.serial_no and not via_landed_cost_voucher: |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 56 | validate_serial_no(sle) |
| 57 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 58 | if cancel: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 59 | sle["actual_qty"] = -flt(sle.get("actual_qty")) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 60 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 61 | if sle["actual_qty"] < 0 and not sle.get("outgoing_rate"): |
| 62 | sle["outgoing_rate"] = get_incoming_outgoing_rate_for_cancel( |
| 63 | sle.item_code, sle.voucher_type, sle.voucher_no, sle.voucher_detail_no |
| 64 | ) |
| 65 | sle["incoming_rate"] = 0.0 |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 66 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 67 | if sle["actual_qty"] > 0 and not sle.get("incoming_rate"): |
| 68 | sle["incoming_rate"] = get_incoming_outgoing_rate_for_cancel( |
| 69 | sle.item_code, sle.voucher_type, sle.voucher_no, sle.voucher_detail_no |
| 70 | ) |
| 71 | sle["outgoing_rate"] = 0.0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 72 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 73 | if sle.get("actual_qty") or sle.get("voucher_type") == "Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 74 | sle_doc = make_entry(sle, allow_negative_stock, via_landed_cost_voucher) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 75 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 76 | args = sle_doc.as_dict() |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 77 | |
| 78 | if sle.get("voucher_type") == "Stock Reconciliation": |
| 79 | # preserve previous_qty_after_transaction for qty reposting |
| 80 | args.previous_qty_after_transaction = sle.get("previous_qty_after_transaction") |
| 81 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 82 | is_stock_item = frappe.get_cached_value("Item", args.get("item_code"), "is_stock_item") |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 83 | if is_stock_item: |
| 84 | 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] | 85 | repost_current_voucher(args, allow_negative_stock, via_landed_cost_voucher) |
Ankush Menat | ff9a6e8 | 2021-12-20 15:07:41 +0530 | [diff] [blame] | 86 | update_bin_qty(bin_name, args) |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 87 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 88 | frappe.msgprint( |
| 89 | _("Item {0} ignored since it is not a stock item").format(args.get("item_code")) |
| 90 | ) |
| 91 | |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 92 | |
| 93 | def repost_current_voucher(args, allow_negative_stock=False, via_landed_cost_voucher=False): |
| 94 | if args.get("actual_qty") or args.get("voucher_type") == "Stock Reconciliation": |
| 95 | if not args.get("posting_date"): |
| 96 | args["posting_date"] = nowdate() |
| 97 | |
| 98 | if args.get("is_cancelled") and via_landed_cost_voucher: |
| 99 | return |
| 100 | |
| 101 | # Reposts only current voucher SL Entries |
| 102 | # Updates valuation rate, stock value, stock queue for current transaction |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 103 | update_entries_after( |
| 104 | { |
| 105 | "item_code": args.get("item_code"), |
| 106 | "warehouse": args.get("warehouse"), |
| 107 | "posting_date": args.get("posting_date"), |
| 108 | "posting_time": args.get("posting_time"), |
| 109 | "voucher_type": args.get("voucher_type"), |
| 110 | "voucher_no": args.get("voucher_no"), |
| 111 | "sle_id": args.get("name"), |
| 112 | "creation": args.get("creation"), |
| 113 | }, |
| 114 | allow_negative_stock=allow_negative_stock, |
| 115 | via_landed_cost_voucher=via_landed_cost_voucher, |
| 116 | ) |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 117 | |
| 118 | # update qty in future sle and Validate negative qty |
| 119 | update_qty_in_future_sle(args, allow_negative_stock) |
| 120 | |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 121 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 122 | def get_args_for_future_sle(row): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 123 | return frappe._dict( |
| 124 | { |
| 125 | "voucher_type": row.get("voucher_type"), |
| 126 | "voucher_no": row.get("voucher_no"), |
| 127 | "posting_date": row.get("posting_date"), |
| 128 | "posting_time": row.get("posting_time"), |
| 129 | } |
| 130 | ) |
| 131 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 132 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 133 | def validate_serial_no(sle): |
| 134 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
Ankush Menat | 66bf21f | 2022-01-16 20:45:59 +0530 | [diff] [blame] | 135 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 136 | for sn in get_serial_nos(sle.serial_no): |
| 137 | args = copy.deepcopy(sle) |
| 138 | args.serial_no = sn |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 139 | args.warehouse = "" |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 140 | |
| 141 | vouchers = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 142 | for row in get_stock_ledger_entries(args, ">"): |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 143 | voucher_type = frappe.bold(row.voucher_type) |
| 144 | voucher_no = frappe.bold(get_link_to_form(row.voucher_type, row.voucher_no)) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 145 | vouchers.append(f"{voucher_type} {voucher_no}") |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 146 | |
| 147 | if vouchers: |
| 148 | serial_no = frappe.bold(sn) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 149 | msg = ( |
| 150 | f"""The serial no {serial_no} has been used in the future transactions so you need to cancel them first. |
| 151 | The list of the transactions are as below.""" |
| 152 | + "<br><br><ul><li>" |
| 153 | ) |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 154 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 155 | msg += "</li><li>".join(vouchers) |
| 156 | msg += "</li></ul>" |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 157 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 158 | title = "Cannot Submit" if not sle.get("is_cancelled") else "Cannot Cancel" |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 159 | frappe.throw(_(msg), title=_(title), exc=SerialNoExistsInFutureTransaction) |
| 160 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 161 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 162 | def validate_cancellation(args): |
| 163 | if args[0].get("is_cancelled"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 164 | repost_entry = frappe.db.get_value( |
| 165 | "Repost Item Valuation", |
| 166 | {"voucher_type": args[0].voucher_type, "voucher_no": args[0].voucher_no, "docstatus": 1}, |
| 167 | ["name", "status"], |
| 168 | as_dict=1, |
| 169 | ) |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 170 | |
| 171 | if repost_entry: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 172 | if repost_entry.status == "In Progress": |
| 173 | frappe.throw( |
| 174 | _( |
| 175 | "Cannot cancel the transaction. Reposting of item valuation on submission is not completed yet." |
| 176 | ) |
| 177 | ) |
| 178 | if repost_entry.status == "Queued": |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 179 | doc = frappe.get_doc("Repost Item Valuation", repost_entry.name) |
Ankush Menat | a281998 | 2022-04-08 13:20:25 +0530 | [diff] [blame] | 180 | doc.status = "Skipped" |
Ankush Menat | aa024fc | 2021-11-18 12:51:26 +0530 | [diff] [blame] | 181 | doc.flags.ignore_permissions = True |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 182 | doc.cancel() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 183 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 184 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 185 | def set_as_cancel(voucher_type, voucher_no): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 186 | frappe.db.sql( |
| 187 | """update `tabStock Ledger Entry` set is_cancelled=1, |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 188 | modified=%s, modified_by=%s |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 189 | where voucher_type=%s and voucher_no=%s and is_cancelled = 0""", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 190 | (now(), frappe.session.user, voucher_type, voucher_no), |
| 191 | ) |
| 192 | |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 193 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 194 | 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] | 195 | args["doctype"] = "Stock Ledger Entry" |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 196 | sle = frappe.get_doc(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 197 | sle.flags.ignore_permissions = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 198 | sle.allow_negative_stock = allow_negative_stock |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 199 | sle.via_landed_cost_voucher = via_landed_cost_voucher |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 200 | sle.submit() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 201 | return sle |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 202 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 203 | |
| 204 | def repost_future_sle( |
| 205 | args=None, |
| 206 | voucher_type=None, |
| 207 | voucher_no=None, |
| 208 | allow_negative_stock=None, |
| 209 | via_landed_cost_voucher=False, |
| 210 | doc=None, |
| 211 | ): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 212 | if not args and voucher_type and voucher_no: |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 213 | args = get_items_to_be_repost(voucher_type, voucher_no, doc) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 214 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 215 | distinct_item_warehouses = get_distinct_item_warehouse(args, doc) |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 216 | affected_transactions = get_affected_transactions(doc) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 217 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 218 | i = get_current_index(doc) or 0 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 219 | while i < len(args): |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 220 | validate_item_warehouse(args[i]) |
| 221 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 222 | obj = update_entries_after( |
| 223 | { |
| 224 | "item_code": args[i].get("item_code"), |
| 225 | "warehouse": args[i].get("warehouse"), |
| 226 | "posting_date": args[i].get("posting_date"), |
| 227 | "posting_time": args[i].get("posting_time"), |
| 228 | "creation": args[i].get("creation"), |
| 229 | "distinct_item_warehouses": distinct_item_warehouses, |
| 230 | }, |
| 231 | allow_negative_stock=allow_negative_stock, |
| 232 | via_landed_cost_voucher=via_landed_cost_voucher, |
| 233 | ) |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 234 | affected_transactions.update(obj.affected_transactions) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 235 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 236 | distinct_item_warehouses[ |
| 237 | (args[i].get("item_code"), args[i].get("warehouse")) |
| 238 | ].reposting_status = True |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 239 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 240 | if obj.new_items_found: |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 241 | for item_wh, data in distinct_item_warehouses.items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 242 | if ("args_idx" not in data and not data.reposting_status) or ( |
| 243 | data.sle_changed and data.reposting_status |
| 244 | ): |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 245 | data.args_idx = len(args) |
| 246 | args.append(data.sle) |
| 247 | elif data.sle_changed and not data.reposting_status: |
| 248 | args[data.args_idx] = data.sle |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 249 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 250 | data.sle_changed = False |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 251 | i += 1 |
| 252 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 253 | if doc and i % 2 == 0: |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 254 | update_args_in_repost_item_valuation( |
| 255 | doc, i, args, distinct_item_warehouses, affected_transactions |
| 256 | ) |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 257 | |
| 258 | if doc and args: |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 259 | update_args_in_repost_item_valuation( |
| 260 | doc, i, args, distinct_item_warehouses, affected_transactions |
| 261 | ) |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 262 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 263 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 264 | def validate_item_warehouse(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 265 | for field in ["item_code", "warehouse", "posting_date", "posting_time"]: |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 266 | if not args.get(field): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 267 | validation_msg = f"The field {frappe.unscrub(args.get(field))} is required for the reposting" |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 268 | frappe.throw(_(validation_msg)) |
| 269 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 270 | |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 271 | def update_args_in_repost_item_valuation( |
| 272 | doc, index, args, distinct_item_warehouses, affected_transactions |
| 273 | ): |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 274 | doc.db_set( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 275 | { |
| 276 | "items_to_be_repost": json.dumps(args, default=str), |
| 277 | "distinct_item_and_warehouse": json.dumps( |
| 278 | {str(k): v for k, v in distinct_item_warehouses.items()}, default=str |
| 279 | ), |
| 280 | "current_index": index, |
Ankush Menat | 8f51954 | 2022-04-19 01:26:16 +0530 | [diff] [blame] | 281 | "affected_transactions": frappe.as_json(affected_transactions), |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 282 | } |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 283 | ) |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 284 | |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 285 | if not frappe.flags.in_test: |
| 286 | frappe.db.commit() |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 287 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 288 | frappe.publish_realtime( |
| 289 | "item_reposting_progress", |
| 290 | {"name": doc.name, "items_to_be_repost": json.dumps(args, default=str), "current_index": index}, |
| 291 | ) |
| 292 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 293 | |
| 294 | def get_items_to_be_repost(voucher_type, voucher_no, doc=None): |
| 295 | if doc and doc.items_to_be_repost: |
| 296 | return json.loads(doc.items_to_be_repost) or [] |
| 297 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 298 | return frappe.db.get_all( |
| 299 | "Stock Ledger Entry", |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 300 | filters={"voucher_type": voucher_type, "voucher_no": voucher_no}, |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 301 | fields=["item_code", "warehouse", "posting_date", "posting_time", "creation"], |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 302 | order_by="creation asc", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 303 | group_by="item_code, warehouse", |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 304 | ) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 305 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 306 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 307 | def get_distinct_item_warehouse(args=None, doc=None): |
| 308 | distinct_item_warehouses = {} |
| 309 | if doc and doc.distinct_item_and_warehouse: |
| 310 | distinct_item_warehouses = json.loads(doc.distinct_item_and_warehouse) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 311 | distinct_item_warehouses = { |
| 312 | frappe.safe_eval(k): frappe._dict(v) for k, v in distinct_item_warehouses.items() |
| 313 | } |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 314 | else: |
| 315 | for i, d in enumerate(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 316 | distinct_item_warehouses.setdefault( |
| 317 | (d.item_code, d.warehouse), frappe._dict({"reposting_status": False, "sle": d, "args_idx": i}) |
| 318 | ) |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 319 | |
| 320 | return distinct_item_warehouses |
| 321 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 322 | |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 323 | def get_affected_transactions(doc) -> Set[Tuple[str, str]]: |
| 324 | if not doc.affected_transactions: |
| 325 | return set() |
| 326 | |
| 327 | transactions = frappe.parse_json(doc.affected_transactions) |
| 328 | return {tuple(transaction) for transaction in transactions} |
| 329 | |
| 330 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 331 | def get_current_index(doc=None): |
| 332 | if doc and doc.current_index: |
| 333 | return doc.current_index |
| 334 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 335 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 336 | class update_entries_after(object): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 337 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 338 | update valution rate and qty after transaction |
| 339 | from the current time-bucket onwards |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 340 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 341 | :param args: args as dict |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 342 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 343 | args = { |
| 344 | "item_code": "ABC", |
| 345 | "warehouse": "XYZ", |
| 346 | "posting_date": "2012-12-12", |
| 347 | "posting_time": "12:00" |
| 348 | } |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 349 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 350 | |
| 351 | def __init__( |
| 352 | self, |
| 353 | args, |
| 354 | allow_zero_rate=False, |
| 355 | allow_negative_stock=None, |
| 356 | via_landed_cost_voucher=False, |
| 357 | verbose=1, |
| 358 | ): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 359 | self.exceptions = {} |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 360 | self.verbose = verbose |
| 361 | self.allow_zero_rate = allow_zero_rate |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 362 | self.via_landed_cost_voucher = via_landed_cost_voucher |
Ankush Menat | eb8b424 | 2022-02-12 13:08:28 +0530 | [diff] [blame] | 363 | self.item_code = args.get("item_code") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 364 | self.allow_negative_stock = allow_negative_stock or is_negative_stock_allowed( |
| 365 | item_code=self.item_code |
| 366 | ) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 367 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 368 | self.args = frappe._dict(args) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 369 | if self.args.sle_id: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 370 | self.args["name"] = self.args.sle_id |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 371 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 372 | self.company = frappe.get_cached_value("Warehouse", self.args.warehouse, "company") |
| 373 | self.get_precision() |
| 374 | self.valuation_method = get_valuation_method(self.item_code) |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 375 | |
| 376 | self.new_items_found = False |
| 377 | self.distinct_item_warehouses = args.get("distinct_item_warehouses", frappe._dict()) |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 378 | self.affected_transactions: Set[Tuple[str, str]] = set() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 379 | |
| 380 | self.data = frappe._dict() |
| 381 | self.initialize_previous_data(self.args) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 382 | self.build() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 383 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 384 | def get_precision(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 385 | company_base_currency = frappe.get_cached_value("Company", self.company, "default_currency") |
| 386 | self.precision = get_field_precision( |
| 387 | frappe.get_meta("Stock Ledger Entry").get_field("stock_value"), currency=company_base_currency |
| 388 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 389 | |
| 390 | def initialize_previous_data(self, args): |
| 391 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 392 | Get previous sl entries for current item for each related warehouse |
| 393 | and assigns into self.data dict |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 394 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 395 | :Data Structure: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 396 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 397 | self.data = { |
| 398 | warehouse1: { |
| 399 | 'previus_sle': {}, |
| 400 | 'qty_after_transaction': 10, |
| 401 | 'valuation_rate': 100, |
| 402 | 'stock_value': 1000, |
| 403 | 'prev_stock_value': 1000, |
| 404 | 'stock_queue': '[[10, 100]]', |
| 405 | 'stock_value_difference': 1000 |
| 406 | } |
| 407 | } |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 408 | |
| 409 | """ |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 410 | self.data.setdefault(args.warehouse, frappe._dict()) |
| 411 | warehouse_dict = self.data[args.warehouse] |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 412 | previous_sle = get_previous_sle_of_current_voucher(args) |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 413 | warehouse_dict.previous_sle = previous_sle |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 414 | |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 415 | for key in ("qty_after_transaction", "valuation_rate", "stock_value"): |
| 416 | setattr(warehouse_dict, key, flt(previous_sle.get(key))) |
| 417 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 418 | warehouse_dict.update( |
| 419 | { |
| 420 | "prev_stock_value": previous_sle.stock_value or 0.0, |
| 421 | "stock_queue": json.loads(previous_sle.stock_queue or "[]"), |
| 422 | "stock_value_difference": 0.0, |
| 423 | } |
| 424 | ) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 425 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 426 | def build(self): |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 427 | from erpnext.controllers.stock_controller import future_sle_exists |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 428 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 429 | if self.args.get("sle_id"): |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 430 | self.process_sle_against_current_timestamp() |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 431 | if not future_sle_exists(self.args): |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 432 | self.update_bin() |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 433 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 434 | entries_to_fix = self.get_future_entries_to_fix() |
| 435 | |
| 436 | i = 0 |
| 437 | while i < len(entries_to_fix): |
| 438 | sle = entries_to_fix[i] |
| 439 | i += 1 |
| 440 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 441 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 442 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 443 | if sle.dependant_sle_voucher_detail_no: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 444 | 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] | 445 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 446 | self.update_bin() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 447 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 448 | if self.exceptions: |
| 449 | self.raise_exceptions() |
| 450 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 451 | def process_sle_against_current_timestamp(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 452 | sl_entries = self.get_sle_against_current_voucher() |
| 453 | for sle in sl_entries: |
| 454 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 455 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 456 | def get_sle_against_current_voucher(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 457 | self.args["time_format"] = "%H:%i:%s" |
Nabin Hait | f2be080 | 2021-02-15 19:27:49 +0530 | [diff] [blame] | 458 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 459 | return frappe.db.sql( |
| 460 | """ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 461 | select |
| 462 | *, timestamp(posting_date, posting_time) as "timestamp" |
| 463 | from |
| 464 | `tabStock Ledger Entry` |
| 465 | where |
| 466 | item_code = %(item_code)s |
| 467 | and warehouse = %(warehouse)s |
rohitwaghchaure | fe4540d | 2021-08-26 12:52:36 +0530 | [diff] [blame] | 468 | and is_cancelled = 0 |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 469 | and timestamp(posting_date, time_format(posting_time, %(time_format)s)) = timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s)) |
| 470 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 471 | order by |
| 472 | creation ASC |
| 473 | for update |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 474 | """, |
| 475 | self.args, |
| 476 | as_dict=1, |
| 477 | ) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 478 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 479 | def get_future_entries_to_fix(self): |
| 480 | # includes current entry! |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 481 | args = self.data[self.args.warehouse].previous_sle or frappe._dict( |
| 482 | {"item_code": self.item_code, "warehouse": self.args.warehouse} |
| 483 | ) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 484 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 485 | return list(self.get_sle_after_datetime(args)) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 486 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 487 | def get_dependent_entries_to_fix(self, entries_to_fix, sle): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 488 | dependant_sle = get_sle_by_voucher_detail_no( |
| 489 | sle.dependant_sle_voucher_detail_no, excluded_sle=sle.name |
| 490 | ) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 491 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 492 | if not dependant_sle: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 493 | return entries_to_fix |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 494 | elif ( |
| 495 | dependant_sle.item_code == self.item_code and dependant_sle.warehouse == self.args.warehouse |
| 496 | ): |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 497 | return entries_to_fix |
| 498 | elif dependant_sle.item_code != self.item_code: |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 499 | self.update_distinct_item_warehouses(dependant_sle) |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 500 | return entries_to_fix |
| 501 | elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse in self.data: |
| 502 | return entries_to_fix |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 503 | else: |
| 504 | return self.append_future_sle_for_dependant(dependant_sle, entries_to_fix) |
| 505 | |
| 506 | def update_distinct_item_warehouses(self, dependant_sle): |
| 507 | key = (dependant_sle.item_code, dependant_sle.warehouse) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 508 | val = frappe._dict({"sle": dependant_sle}) |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 509 | if key not in self.distinct_item_warehouses: |
| 510 | self.distinct_item_warehouses[key] = val |
| 511 | self.new_items_found = True |
| 512 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 513 | existing_sle_posting_date = ( |
| 514 | self.distinct_item_warehouses[key].get("sle", {}).get("posting_date") |
| 515 | ) |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 516 | if getdate(dependant_sle.posting_date) < getdate(existing_sle_posting_date): |
| 517 | val.sle_changed = True |
| 518 | self.distinct_item_warehouses[key] = val |
| 519 | self.new_items_found = True |
| 520 | |
| 521 | def append_future_sle_for_dependant(self, dependant_sle, entries_to_fix): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 522 | self.initialize_previous_data(dependant_sle) |
| 523 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 524 | args = self.data[dependant_sle.warehouse].previous_sle or frappe._dict( |
| 525 | {"item_code": self.item_code, "warehouse": dependant_sle.warehouse} |
| 526 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 527 | future_sle_for_dependant = list(self.get_sle_after_datetime(args)) |
| 528 | |
| 529 | entries_to_fix.extend(future_sle_for_dependant) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 530 | return sorted(entries_to_fix, key=lambda k: k["timestamp"]) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 531 | |
| 532 | def process_sle(self, sle): |
Ankush Menat | 66bf21f | 2022-01-16 20:45:59 +0530 | [diff] [blame] | 533 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
| 534 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 535 | # previous sle data for this warehouse |
| 536 | self.wh_data = self.data[sle.warehouse] |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 537 | self.affected_transactions.add((sle.voucher_type, sle.voucher_no)) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 538 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 539 | 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] | 540 | # validate negative stock for serialized items, fifo valuation |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 541 | # or when negative stock is not allowed for moving average |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 542 | if not self.validate_negative_stock(sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 543 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 544 | return |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 545 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 546 | # Get dynamic incoming/outgoing rate |
Ankush Menat | 701878f | 2022-03-01 18:08:29 +0530 | [diff] [blame] | 547 | if not self.args.get("sle_id"): |
| 548 | self.get_dynamic_incoming_outgoing_rate(sle) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 549 | |
Ankush Menat | 66bf21f | 2022-01-16 20:45:59 +0530 | [diff] [blame] | 550 | if get_serial_nos(sle.serial_no): |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 551 | self.get_serialized_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 552 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 553 | if sle.voucher_type == "Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 554 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 555 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 556 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt( |
| 557 | self.wh_data.valuation_rate |
| 558 | ) |
| 559 | elif sle.batch_no and frappe.db.get_value( |
| 560 | "Batch", sle.batch_no, "use_batchwise_valuation", cache=True |
| 561 | ): |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 562 | self.update_batched_values(sle) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 563 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 564 | if sle.voucher_type == "Stock Reconciliation" and not sle.batch_no: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 565 | # assert |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 566 | self.wh_data.valuation_rate = sle.valuation_rate |
| 567 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 568 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt( |
| 569 | self.wh_data.valuation_rate |
| 570 | ) |
Ankush Menat | b0cf619 | 2022-01-16 13:02:23 +0530 | [diff] [blame] | 571 | if self.valuation_method != "Moving Average": |
| 572 | 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] | 573 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 574 | if self.valuation_method == "Moving Average": |
| 575 | self.get_moving_average_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 576 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 577 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt( |
| 578 | self.wh_data.valuation_rate |
| 579 | ) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 580 | else: |
Ankush Menat | f089d39 | 2022-02-02 12:51:21 +0530 | [diff] [blame] | 581 | self.update_queue_values(sle) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 582 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 583 | # rounding as per precision |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 584 | self.wh_data.stock_value = flt(self.wh_data.stock_value, self.precision) |
Ankush Menat | 609d2fc | 2022-02-20 11:35:53 +0530 | [diff] [blame] | 585 | if not self.wh_data.qty_after_transaction: |
| 586 | self.wh_data.stock_value = 0.0 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 587 | stock_value_difference = self.wh_data.stock_value - self.wh_data.prev_stock_value |
| 588 | self.wh_data.prev_stock_value = self.wh_data.stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 589 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 590 | # update current sle |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 591 | sle.qty_after_transaction = self.wh_data.qty_after_transaction |
| 592 | sle.valuation_rate = self.wh_data.valuation_rate |
| 593 | sle.stock_value = self.wh_data.stock_value |
| 594 | sle.stock_queue = json.dumps(self.wh_data.stock_queue) |
Rushabh Mehta | 2e0e711 | 2015-02-18 11:38:05 +0530 | [diff] [blame] | 595 | sle.stock_value_difference = stock_value_difference |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 596 | sle.doctype = "Stock Ledger Entry" |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 597 | frappe.get_doc(sle).db_update() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 598 | |
Ankush Menat | 701878f | 2022-03-01 18:08:29 +0530 | [diff] [blame] | 599 | if not self.args.get("sle_id"): |
| 600 | self.update_outgoing_rate_on_transaction(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 601 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 602 | def validate_negative_stock(self, sle): |
| 603 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 604 | validate negative stock for entries current datetime onwards |
| 605 | will not consider cancelled entries |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 606 | """ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 607 | diff = self.wh_data.qty_after_transaction + flt(sle.actual_qty) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 608 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 609 | if diff < 0 and abs(diff) > 0.0001: |
| 610 | # negative stock! |
| 611 | exc = sle.copy().update({"diff": diff}) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 612 | self.exceptions.setdefault(sle.warehouse, []).append(exc) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 613 | return False |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 614 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 615 | return True |
| 616 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 617 | def get_dynamic_incoming_outgoing_rate(self, sle): |
| 618 | # Get updated incoming/outgoing rate from transaction |
| 619 | if sle.recalculate_rate: |
| 620 | rate = self.get_incoming_outgoing_rate_from_transaction(sle) |
| 621 | |
| 622 | if flt(sle.actual_qty) >= 0: |
| 623 | sle.incoming_rate = rate |
| 624 | else: |
| 625 | sle.outgoing_rate = rate |
| 626 | |
| 627 | def get_incoming_outgoing_rate_from_transaction(self, sle): |
| 628 | rate = 0 |
| 629 | # Material Transfer, Repack, Manufacturing |
| 630 | if sle.voucher_type == "Stock Entry": |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 631 | self.recalculate_amounts_in_stock_entry(sle.voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 632 | rate = frappe.db.get_value("Stock Entry Detail", sle.voucher_detail_no, "valuation_rate") |
| 633 | # Sales and Purchase Return |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 634 | elif sle.voucher_type in ( |
| 635 | "Purchase Receipt", |
| 636 | "Purchase Invoice", |
| 637 | "Delivery Note", |
| 638 | "Sales Invoice", |
| 639 | ): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 640 | 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] | 641 | from erpnext.controllers.sales_and_purchase_return import ( |
| 642 | get_rate_for_return, # don't move this import to top |
| 643 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 644 | |
| 645 | rate = get_rate_for_return( |
| 646 | sle.voucher_type, |
| 647 | sle.voucher_no, |
| 648 | sle.item_code, |
| 649 | voucher_detail_no=sle.voucher_detail_no, |
| 650 | sle=sle, |
| 651 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 652 | else: |
| 653 | if sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 654 | rate_field = "valuation_rate" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 655 | else: |
| 656 | rate_field = "incoming_rate" |
| 657 | |
| 658 | # check in item table |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 659 | item_code, incoming_rate = frappe.db.get_value( |
| 660 | sle.voucher_type + " Item", sle.voucher_detail_no, ["item_code", rate_field] |
| 661 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 662 | |
| 663 | if item_code == sle.item_code: |
| 664 | rate = incoming_rate |
| 665 | else: |
| 666 | if sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 667 | ref_doctype = "Packed Item" |
| 668 | else: |
| 669 | ref_doctype = "Purchase Receipt Item Supplied" |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 670 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 671 | rate = frappe.db.get_value( |
| 672 | ref_doctype, |
| 673 | {"parent_detail_docname": sle.voucher_detail_no, "item_code": sle.item_code}, |
| 674 | rate_field, |
| 675 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 676 | |
| 677 | return rate |
| 678 | |
| 679 | def update_outgoing_rate_on_transaction(self, sle): |
| 680 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 681 | Update outgoing rate in Stock Entry, Delivery Note, Sales Invoice and Sales Return |
| 682 | In case of Stock Entry, also calculate FG Item rate and total incoming/outgoing amount |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 683 | """ |
| 684 | if sle.actual_qty and sle.voucher_detail_no: |
| 685 | outgoing_rate = abs(flt(sle.stock_value_difference)) / abs(sle.actual_qty) |
| 686 | |
| 687 | if flt(sle.actual_qty) < 0 and sle.voucher_type == "Stock Entry": |
| 688 | self.update_rate_on_stock_entry(sle, outgoing_rate) |
| 689 | elif sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 690 | self.update_rate_on_delivery_and_sales_return(sle, outgoing_rate) |
| 691 | elif flt(sle.actual_qty) < 0 and sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
| 692 | self.update_rate_on_purchase_receipt(sle, outgoing_rate) |
| 693 | |
| 694 | def update_rate_on_stock_entry(self, sle, outgoing_rate): |
| 695 | frappe.db.set_value("Stock Entry Detail", sle.voucher_detail_no, "basic_rate", outgoing_rate) |
| 696 | |
Ankush Menat | 701878f | 2022-03-01 18:08:29 +0530 | [diff] [blame] | 697 | # Update outgoing item's rate, recalculate FG Item's rate and total incoming/outgoing amount |
| 698 | if not sle.dependant_sle_voucher_detail_no: |
| 699 | self.recalculate_amounts_in_stock_entry(sle.voucher_no) |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 700 | |
| 701 | def recalculate_amounts_in_stock_entry(self, voucher_no): |
| 702 | stock_entry = frappe.get_doc("Stock Entry", voucher_no, for_update=True) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 703 | stock_entry.calculate_rate_and_amount(reset_outgoing_rate=False, raise_error_if_no_rate=False) |
| 704 | stock_entry.db_update() |
| 705 | for d in stock_entry.items: |
| 706 | d.db_update() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 707 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 708 | def update_rate_on_delivery_and_sales_return(self, sle, outgoing_rate): |
| 709 | # Update item's incoming rate on transaction |
| 710 | item_code = frappe.db.get_value(sle.voucher_type + " Item", sle.voucher_detail_no, "item_code") |
| 711 | if item_code == sle.item_code: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 712 | frappe.db.set_value( |
| 713 | sle.voucher_type + " Item", sle.voucher_detail_no, "incoming_rate", outgoing_rate |
| 714 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 715 | else: |
| 716 | # packed item |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 717 | frappe.db.set_value( |
| 718 | "Packed Item", |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 719 | {"parent_detail_docname": sle.voucher_detail_no, "item_code": sle.item_code}, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 720 | "incoming_rate", |
| 721 | outgoing_rate, |
| 722 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 723 | |
| 724 | def update_rate_on_purchase_receipt(self, sle, outgoing_rate): |
| 725 | if frappe.db.exists(sle.voucher_type + " Item", sle.voucher_detail_no): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 726 | frappe.db.set_value( |
| 727 | sle.voucher_type + " Item", sle.voucher_detail_no, "base_net_rate", outgoing_rate |
| 728 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 729 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 730 | frappe.db.set_value( |
| 731 | "Purchase Receipt Item Supplied", sle.voucher_detail_no, "rate", outgoing_rate |
| 732 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 733 | |
| 734 | # Recalculate subcontracted item's rate in case of subcontracted purchase receipt/invoice |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 735 | if frappe.get_cached_value(sle.voucher_type, sle.voucher_no, "is_subcontracted"): |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 736 | doc = frappe.get_doc(sle.voucher_type, sle.voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 737 | doc.update_valuation_rate(reset_outgoing_rate=False) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 738 | for d in doc.items + doc.supplied_items: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 739 | d.db_update() |
| 740 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 741 | def get_serialized_values(self, sle): |
| 742 | incoming_rate = flt(sle.incoming_rate) |
| 743 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 744 | serial_nos = cstr(sle.serial_no).split("\n") |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 745 | |
| 746 | if incoming_rate < 0: |
| 747 | # wrong incoming rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 748 | incoming_rate = self.wh_data.valuation_rate |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 749 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 750 | stock_value_change = 0 |
Ankush Menat | b9642a1 | 2021-12-21 16:49:41 +0530 | [diff] [blame] | 751 | if actual_qty > 0: |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 752 | stock_value_change = actual_qty * incoming_rate |
Ankush Menat | b9642a1 | 2021-12-21 16:49:41 +0530 | [diff] [blame] | 753 | else: |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 754 | # In case of delivery/stock issue, get average purchase rate |
| 755 | # of serial nos of current entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 756 | if not sle.is_cancelled: |
| 757 | outgoing_value = self.get_incoming_value_for_serial_nos(sle, serial_nos) |
| 758 | stock_value_change = -1 * outgoing_value |
| 759 | else: |
| 760 | stock_value_change = actual_qty * sle.outgoing_rate |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 761 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 762 | new_stock_qty = self.wh_data.qty_after_transaction + actual_qty |
rohitwaghchaure | 0fe6ced | 2018-07-27 10:33:30 +0530 | [diff] [blame] | 763 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 764 | if new_stock_qty > 0: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 765 | new_stock_value = ( |
| 766 | self.wh_data.qty_after_transaction * self.wh_data.valuation_rate |
| 767 | ) + stock_value_change |
rohitwaghchaure | 0fe6ced | 2018-07-27 10:33:30 +0530 | [diff] [blame] | 768 | if new_stock_value >= 0: |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 769 | # calculate new valuation rate only if stock value is positive |
| 770 | # else it remains the same as that of previous entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 771 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 772 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 773 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 774 | allow_zero_rate = self.check_if_allow_zero_valuation_rate( |
| 775 | sle.voucher_type, sle.voucher_detail_no |
| 776 | ) |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 777 | if not allow_zero_rate: |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 778 | self.wh_data.valuation_rate = self.get_fallback_rate(sle) |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 779 | |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 780 | def get_incoming_value_for_serial_nos(self, sle, serial_nos): |
| 781 | # get rate from serial nos within same company |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 782 | all_serial_nos = frappe.get_all( |
| 783 | "Serial No", fields=["purchase_rate", "name", "company"], filters={"name": ("in", serial_nos)} |
| 784 | ) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 785 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 786 | 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] | 787 | |
| 788 | # Get rate for serial nos which has been transferred to other company |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 789 | invalid_serial_nos = [d.name for d in all_serial_nos if d.company != sle.company] |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 790 | for serial_no in invalid_serial_nos: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 791 | incoming_rate = frappe.db.sql( |
| 792 | """ |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 793 | select incoming_rate |
| 794 | from `tabStock Ledger Entry` |
| 795 | where |
| 796 | company = %s |
| 797 | and actual_qty > 0 |
Ankush Menat | 82ea958 | 2022-01-16 20:19:04 +0530 | [diff] [blame] | 798 | and is_cancelled = 0 |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 799 | and (serial_no = %s |
| 800 | or serial_no like %s |
| 801 | or serial_no like %s |
| 802 | or serial_no like %s |
| 803 | ) |
| 804 | order by posting_date desc |
| 805 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 806 | """, |
| 807 | (sle.company, serial_no, serial_no + "\n%", "%\n" + serial_no, "%\n" + serial_no + "\n%"), |
| 808 | ) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 809 | |
| 810 | incoming_values += flt(incoming_rate[0][0]) if incoming_rate else 0 |
| 811 | |
| 812 | return incoming_values |
| 813 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 814 | def get_moving_average_values(self, sle): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 815 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 816 | new_stock_qty = flt(self.wh_data.qty_after_transaction) + actual_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 817 | if new_stock_qty >= 0: |
| 818 | if actual_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 819 | if flt(self.wh_data.qty_after_transaction) <= 0: |
| 820 | self.wh_data.valuation_rate = sle.incoming_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 821 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 822 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + ( |
| 823 | actual_qty * sle.incoming_rate |
| 824 | ) |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 825 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 826 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 827 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 828 | elif sle.outgoing_rate: |
| 829 | if new_stock_qty: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 830 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + ( |
| 831 | actual_qty * sle.outgoing_rate |
| 832 | ) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 833 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 834 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 835 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 836 | self.wh_data.valuation_rate = sle.outgoing_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 837 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 838 | if flt(self.wh_data.qty_after_transaction) >= 0 and sle.outgoing_rate: |
| 839 | self.wh_data.valuation_rate = sle.outgoing_rate |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 840 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 841 | if not self.wh_data.valuation_rate and actual_qty > 0: |
| 842 | self.wh_data.valuation_rate = sle.incoming_rate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 843 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 844 | # 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] | 845 | # allow zero valuration rate flag set |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 846 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 847 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate( |
| 848 | sle.voucher_type, sle.voucher_detail_no |
| 849 | ) |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 850 | if not allow_zero_valuation_rate: |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 851 | self.wh_data.valuation_rate = self.get_fallback_rate(sle) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 852 | |
Ankush Menat | f089d39 | 2022-02-02 12:51:21 +0530 | [diff] [blame] | 853 | def update_queue_values(self, sle): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 854 | incoming_rate = flt(sle.incoming_rate) |
| 855 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 856 | outgoing_rate = flt(sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 857 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 858 | self.wh_data.qty_after_transaction = round_off_if_near_zero( |
| 859 | self.wh_data.qty_after_transaction + actual_qty |
| 860 | ) |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 861 | |
Ankush Menat | 97e18a1 | 2022-01-15 17:42:25 +0530 | [diff] [blame] | 862 | if self.valuation_method == "LIFO": |
| 863 | stock_queue = LIFOValuation(self.wh_data.stock_queue) |
| 864 | else: |
| 865 | stock_queue = FIFOValuation(self.wh_data.stock_queue) |
| 866 | |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 867 | _prev_qty, prev_stock_value = stock_queue.get_total_stock_and_value() |
| 868 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 869 | if actual_qty > 0: |
Ankush Menat | 97e18a1 | 2022-01-15 17:42:25 +0530 | [diff] [blame] | 870 | stock_queue.add_stock(qty=actual_qty, rate=incoming_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 871 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 872 | |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 873 | def rate_generator() -> float: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 874 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate( |
| 875 | sle.voucher_type, sle.voucher_detail_no |
| 876 | ) |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 877 | if not allow_zero_valuation_rate: |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 878 | return self.get_fallback_rate(sle) |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 879 | else: |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 880 | return 0.0 |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 881 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 882 | stock_queue.remove_stock( |
| 883 | qty=abs(actual_qty), outgoing_rate=outgoing_rate, rate_generator=rate_generator |
| 884 | ) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 885 | |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 886 | _qty, stock_value = stock_queue.get_total_stock_and_value() |
| 887 | |
| 888 | stock_value_difference = stock_value - prev_stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 889 | |
Ankush Menat | 97e18a1 | 2022-01-15 17:42:25 +0530 | [diff] [blame] | 890 | self.wh_data.stock_queue = stock_queue.state |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 891 | self.wh_data.stock_value = round_off_if_near_zero( |
| 892 | self.wh_data.stock_value + stock_value_difference |
| 893 | ) |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 894 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 895 | if not self.wh_data.stock_queue: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 896 | self.wh_data.stock_queue.append( |
| 897 | [0, sle.incoming_rate or sle.outgoing_rate or self.wh_data.valuation_rate] |
| 898 | ) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 899 | |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 900 | if self.wh_data.qty_after_transaction: |
| 901 | self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction |
| 902 | |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 903 | def update_batched_values(self, sle): |
| 904 | incoming_rate = flt(sle.incoming_rate) |
| 905 | actual_qty = flt(sle.actual_qty) |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 906 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 907 | self.wh_data.qty_after_transaction = round_off_if_near_zero( |
| 908 | self.wh_data.qty_after_transaction + actual_qty |
| 909 | ) |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 910 | |
| 911 | if actual_qty > 0: |
| 912 | stock_value_difference = incoming_rate * actual_qty |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 913 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 914 | outgoing_rate = get_batch_incoming_rate( |
| 915 | item_code=sle.item_code, |
| 916 | warehouse=sle.warehouse, |
| 917 | batch_no=sle.batch_no, |
| 918 | posting_date=sle.posting_date, |
| 919 | posting_time=sle.posting_time, |
| 920 | creation=sle.creation, |
| 921 | ) |
Ankush Menat | aba7a7c | 2022-02-19 19:36:28 +0530 | [diff] [blame] | 922 | if outgoing_rate is None: |
| 923 | # This can *only* happen if qty available for the batch is zero. |
| 924 | # in such case fall back various other rates. |
| 925 | # future entries will correct the overall accounting as each |
| 926 | # batch individually uses moving average rates. |
| 927 | outgoing_rate = self.get_fallback_rate(sle) |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 928 | stock_value_difference = outgoing_rate * actual_qty |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 929 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 930 | self.wh_data.stock_value = round_off_if_near_zero( |
| 931 | self.wh_data.stock_value + stock_value_difference |
| 932 | ) |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 933 | if self.wh_data.qty_after_transaction: |
| 934 | self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 935 | |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 936 | def check_if_allow_zero_valuation_rate(self, voucher_type, voucher_detail_no): |
deepeshgarg007 | f9c0ef3 | 2019-07-30 18:49:19 +0530 | [diff] [blame] | 937 | ref_item_dt = "" |
| 938 | |
| 939 | if voucher_type == "Stock Entry": |
| 940 | ref_item_dt = voucher_type + " Detail" |
| 941 | elif voucher_type in ["Purchase Invoice", "Sales Invoice", "Delivery Note", "Purchase Receipt"]: |
| 942 | ref_item_dt = voucher_type + " Item" |
| 943 | |
| 944 | if ref_item_dt: |
| 945 | return frappe.db.get_value(ref_item_dt, voucher_detail_no, "allow_zero_valuation_rate") |
| 946 | else: |
| 947 | return 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 948 | |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 949 | def get_fallback_rate(self, sle) -> float: |
| 950 | """When exact incoming rate isn't available use any of other "average" rates as fallback. |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 951 | This should only get used for negative stock.""" |
| 952 | return get_valuation_rate( |
| 953 | sle.item_code, |
| 954 | sle.warehouse, |
| 955 | sle.voucher_type, |
| 956 | sle.voucher_no, |
| 957 | self.allow_zero_rate, |
| 958 | currency=erpnext.get_company_currency(sle.company), |
| 959 | company=sle.company, |
| 960 | batch_no=sle.batch_no, |
| 961 | ) |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 962 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 963 | def get_sle_before_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 964 | """get previous stock ledger entry before current time-bucket""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 965 | sle = get_stock_ledger_entries(args, "<", "desc", "limit 1", for_update=False) |
| 966 | sle = sle[0] if sle else frappe._dict() |
| 967 | return sle |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 968 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 969 | def get_sle_after_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 970 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 971 | 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] | 972 | |
| 973 | def raise_exceptions(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 974 | msg_list = [] |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 975 | for warehouse, exceptions in self.exceptions.items(): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 976 | deficiency = min(e["diff"] for e in exceptions) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 977 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 978 | if ( |
| 979 | exceptions[0]["voucher_type"], |
| 980 | exceptions[0]["voucher_no"], |
| 981 | ) in frappe.local.flags.currently_saving: |
Nabin Hait | 3edefb1 | 2016-07-20 16:13:18 +0530 | [diff] [blame] | 982 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 983 | msg = _("{0} units of {1} needed in {2} to complete this transaction.").format( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 984 | abs(deficiency), |
| 985 | frappe.get_desk_link("Item", exceptions[0]["item_code"]), |
| 986 | frappe.get_desk_link("Warehouse", warehouse), |
| 987 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 988 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 989 | msg = _( |
| 990 | "{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction." |
| 991 | ).format( |
| 992 | abs(deficiency), |
| 993 | frappe.get_desk_link("Item", exceptions[0]["item_code"]), |
| 994 | frappe.get_desk_link("Warehouse", warehouse), |
| 995 | exceptions[0]["posting_date"], |
| 996 | exceptions[0]["posting_time"], |
| 997 | frappe.get_desk_link(exceptions[0]["voucher_type"], exceptions[0]["voucher_no"]), |
| 998 | ) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 999 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1000 | if msg: |
| 1001 | msg_list.append(msg) |
| 1002 | |
| 1003 | if msg_list: |
| 1004 | message = "\n\n".join(msg_list) |
| 1005 | if self.verbose: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1006 | frappe.throw(message, NegativeStockError, title=_("Insufficient Stock")) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1007 | else: |
| 1008 | raise NegativeStockError(message) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1009 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1010 | def update_bin(self): |
| 1011 | # update bin for each warehouse |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 1012 | for warehouse, data in self.data.items(): |
Ankush Menat | 97060c4 | 2021-12-03 11:50:38 +0530 | [diff] [blame] | 1013 | bin_name = get_or_make_bin(self.item_code, warehouse) |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 1014 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1015 | updated_values = {"actual_qty": data.qty_after_transaction, "stock_value": data.stock_value} |
Ankush Menat | 7dd1036 | 2022-03-10 17:07:57 +0530 | [diff] [blame] | 1016 | if data.valuation_rate is not None: |
| 1017 | updated_values["valuation_rate"] = data.valuation_rate |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1018 | frappe.db.set_value("Bin", bin_name, updated_values) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 1019 | |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1020 | |
| 1021 | def get_previous_sle_of_current_voucher(args, exclude_current_voucher=False): |
| 1022 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 1023 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1024 | args["time_format"] = "%H:%i:%s" |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1025 | if not args.get("posting_date"): |
| 1026 | args["posting_date"] = "1900-01-01" |
| 1027 | if not args.get("posting_time"): |
| 1028 | args["posting_time"] = "00:00" |
| 1029 | |
| 1030 | voucher_condition = "" |
| 1031 | if exclude_current_voucher: |
| 1032 | voucher_no = args.get("voucher_no") |
| 1033 | voucher_condition = f"and voucher_no != '{voucher_no}'" |
| 1034 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1035 | sle = frappe.db.sql( |
| 1036 | """ |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1037 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 1038 | from `tabStock Ledger Entry` |
| 1039 | where item_code = %(item_code)s |
| 1040 | and warehouse = %(warehouse)s |
| 1041 | and is_cancelled = 0 |
| 1042 | {voucher_condition} |
| 1043 | and timestamp(posting_date, time_format(posting_time, %(time_format)s)) < timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s)) |
| 1044 | order by timestamp(posting_date, posting_time) desc, creation desc |
| 1045 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1046 | for update""".format( |
| 1047 | voucher_condition=voucher_condition |
| 1048 | ), |
| 1049 | args, |
| 1050 | as_dict=1, |
| 1051 | ) |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1052 | |
| 1053 | return sle[0] if sle else frappe._dict() |
| 1054 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1055 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 1056 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 1057 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1058 | get the last sle on or before the current time-bucket, |
| 1059 | to get actual qty before transaction, this function |
| 1060 | is called from various transaction like stock entry, reco etc |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 1061 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1062 | args = { |
| 1063 | "item_code": "ABC", |
| 1064 | "warehouse": "XYZ", |
| 1065 | "posting_date": "2012-12-12", |
| 1066 | "posting_time": "12:00", |
| 1067 | "sle": "name of reference Stock Ledger Entry" |
| 1068 | } |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 1069 | """ |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1070 | args["name"] = args.get("sle", None) or "" |
| 1071 | 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] | 1072 | return sle and sle[0] or {} |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1073 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1074 | |
| 1075 | def get_stock_ledger_entries( |
| 1076 | previous_sle, |
| 1077 | operator=None, |
| 1078 | order="desc", |
| 1079 | limit=None, |
| 1080 | for_update=False, |
| 1081 | debug=False, |
| 1082 | check_serial_no=True, |
| 1083 | ): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1084 | """get stock ledger entries filtered by specific posting datetime conditions""" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1085 | conditions = " and timestamp(posting_date, posting_time) {0} timestamp(%(posting_date)s, %(posting_time)s)".format( |
| 1086 | operator |
| 1087 | ) |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 1088 | if previous_sle.get("warehouse"): |
| 1089 | conditions += " and warehouse = %(warehouse)s" |
| 1090 | elif previous_sle.get("warehouse_condition"): |
| 1091 | conditions += " and " + previous_sle.get("warehouse_condition") |
| 1092 | |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 1093 | if check_serial_no and previous_sle.get("serial_no"): |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 1094 | # conditions += " and serial_no like {}".format(frappe.db.escape('%{0}%'.format(previous_sle.get("serial_no")))) |
| 1095 | serial_no = previous_sle.get("serial_no") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1096 | conditions += ( |
| 1097 | """ and |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 1098 | ( |
| 1099 | serial_no = {0} |
| 1100 | or serial_no like {1} |
| 1101 | or serial_no like {2} |
| 1102 | or serial_no like {3} |
| 1103 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1104 | """ |
| 1105 | ).format( |
| 1106 | frappe.db.escape(serial_no), |
| 1107 | frappe.db.escape("{}\n%".format(serial_no)), |
| 1108 | frappe.db.escape("%\n{}".format(serial_no)), |
| 1109 | frappe.db.escape("%\n{}\n%".format(serial_no)), |
| 1110 | ) |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 1111 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1112 | if not previous_sle.get("posting_date"): |
| 1113 | previous_sle["posting_date"] = "1900-01-01" |
| 1114 | if not previous_sle.get("posting_time"): |
| 1115 | previous_sle["posting_time"] = "00:00" |
| 1116 | |
| 1117 | if operator in (">", "<=") and previous_sle.get("name"): |
| 1118 | conditions += " and name!=%(name)s" |
| 1119 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1120 | return frappe.db.sql( |
| 1121 | """ |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1122 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 1123 | from `tabStock Ledger Entry` |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1124 | where item_code = %%(item_code)s |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1125 | and is_cancelled = 0 |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 1126 | %(conditions)s |
Aditya Hase | 0c16424 | 2019-01-07 22:07:13 +0530 | [diff] [blame] | 1127 | order by timestamp(posting_date, posting_time) %(order)s, creation %(order)s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1128 | %(limit)s %(for_update)s""" |
| 1129 | % { |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1130 | "conditions": conditions, |
| 1131 | "limit": limit or "", |
| 1132 | "for_update": for_update and "for update" or "", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1133 | "order": order, |
| 1134 | }, |
| 1135 | previous_sle, |
| 1136 | as_dict=1, |
| 1137 | debug=debug, |
| 1138 | ) |
| 1139 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1140 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1141 | def get_sle_by_voucher_detail_no(voucher_detail_no, excluded_sle=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1142 | return frappe.db.get_value( |
| 1143 | "Stock Ledger Entry", |
| 1144 | {"voucher_detail_no": voucher_detail_no, "name": ["!=", excluded_sle]}, |
| 1145 | [ |
| 1146 | "item_code", |
| 1147 | "warehouse", |
| 1148 | "posting_date", |
| 1149 | "posting_time", |
| 1150 | "timestamp(posting_date, posting_time) as timestamp", |
| 1151 | ], |
| 1152 | as_dict=1, |
| 1153 | ) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1154 | |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1155 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1156 | def get_batch_incoming_rate( |
| 1157 | item_code, warehouse, batch_no, posting_date, posting_time, creation=None |
| 1158 | ): |
| 1159 | |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1160 | sle = frappe.qb.DocType("Stock Ledger Entry") |
| 1161 | |
Ankush Menat | e1c1687 | 2022-04-21 20:01:48 +0530 | [diff] [blame] | 1162 | timestamp_condition = CombineDatetime(sle.posting_date, sle.posting_time) < CombineDatetime( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1163 | posting_date, posting_time |
| 1164 | ) |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1165 | if creation: |
| 1166 | timestamp_condition |= ( |
Ankush Menat | e1c1687 | 2022-04-21 20:01:48 +0530 | [diff] [blame] | 1167 | CombineDatetime(sle.posting_date, sle.posting_time) |
| 1168 | == CombineDatetime(posting_date, posting_time) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1169 | ) & (sle.creation < creation) |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1170 | |
| 1171 | batch_details = ( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1172 | frappe.qb.from_(sle) |
| 1173 | .select(Sum(sle.stock_value_difference).as_("batch_value"), Sum(sle.actual_qty).as_("batch_qty")) |
| 1174 | .where( |
| 1175 | (sle.item_code == item_code) |
| 1176 | & (sle.warehouse == warehouse) |
| 1177 | & (sle.batch_no == batch_no) |
| 1178 | & (sle.is_cancelled == 0) |
| 1179 | ) |
| 1180 | .where(timestamp_condition) |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1181 | ).run(as_dict=True) |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1182 | |
| 1183 | if batch_details and batch_details[0].batch_qty: |
| 1184 | return batch_details[0].batch_value / batch_details[0].batch_qty |
| 1185 | |
| 1186 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1187 | def get_valuation_rate( |
| 1188 | item_code, |
| 1189 | warehouse, |
| 1190 | voucher_type, |
| 1191 | voucher_no, |
| 1192 | allow_zero_rate=False, |
| 1193 | currency=None, |
| 1194 | company=None, |
| 1195 | raise_error_if_no_rate=True, |
| 1196 | batch_no=None, |
| 1197 | ): |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 1198 | |
Ankush Menat | f7ffe04 | 2021-11-01 13:21:14 +0530 | [diff] [blame] | 1199 | if not company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1200 | company = frappe.get_cached_value("Warehouse", warehouse, "company") |
Ankush Menat | f7ffe04 | 2021-11-01 13:21:14 +0530 | [diff] [blame] | 1201 | |
Ankush Menat | 342d09a | 2022-02-19 14:28:51 +0530 | [diff] [blame] | 1202 | last_valuation_rate = None |
| 1203 | |
| 1204 | # Get moving average rate of a specific batch number |
| 1205 | if warehouse and batch_no and frappe.db.get_value("Batch", batch_no, "use_batchwise_valuation"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1206 | last_valuation_rate = frappe.db.sql( |
| 1207 | """ |
Ankush Menat | 342d09a | 2022-02-19 14:28:51 +0530 | [diff] [blame] | 1208 | select sum(stock_value_difference) / sum(actual_qty) |
| 1209 | from `tabStock Ledger Entry` |
| 1210 | where |
| 1211 | item_code = %s |
| 1212 | AND warehouse = %s |
| 1213 | AND batch_no = %s |
| 1214 | AND is_cancelled = 0 |
| 1215 | AND NOT (voucher_no = %s AND voucher_type = %s) |
| 1216 | """, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1217 | (item_code, warehouse, batch_no, voucher_no, voucher_type), |
| 1218 | ) |
Ankush Menat | 342d09a | 2022-02-19 14:28:51 +0530 | [diff] [blame] | 1219 | |
Ankush Menat | f7ffe04 | 2021-11-01 13:21:14 +0530 | [diff] [blame] | 1220 | # Get valuation rate from last sle for the same item and warehouse |
Ankush Menat | 342d09a | 2022-02-19 14:28:51 +0530 | [diff] [blame] | 1221 | if not last_valuation_rate or last_valuation_rate[0][0] is None: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1222 | last_valuation_rate = frappe.db.sql( |
| 1223 | """select valuation_rate |
Ankush Menat | 342d09a | 2022-02-19 14:28:51 +0530 | [diff] [blame] | 1224 | from `tabStock Ledger Entry` force index (item_warehouse) |
| 1225 | where |
| 1226 | item_code = %s |
| 1227 | AND warehouse = %s |
| 1228 | AND valuation_rate >= 0 |
| 1229 | AND is_cancelled = 0 |
| 1230 | AND NOT (voucher_no = %s AND voucher_type = %s) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1231 | order by posting_date desc, posting_time desc, name desc limit 1""", |
| 1232 | (item_code, warehouse, voucher_no, voucher_type), |
| 1233 | ) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1234 | |
| 1235 | if not last_valuation_rate: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 1236 | # Get valuation rate from last sle for the item against any warehouse |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1237 | last_valuation_rate = frappe.db.sql( |
| 1238 | """select valuation_rate |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 1239 | from `tabStock Ledger Entry` force index (item_code) |
Mangesh-Khairnar | 0df5134 | 2019-08-19 10:04:52 +0530 | [diff] [blame] | 1240 | where |
| 1241 | item_code = %s |
| 1242 | AND valuation_rate > 0 |
Ankush Menat | 82ea958 | 2022-01-16 20:19:04 +0530 | [diff] [blame] | 1243 | AND is_cancelled = 0 |
Mangesh-Khairnar | 0df5134 | 2019-08-19 10:04:52 +0530 | [diff] [blame] | 1244 | AND NOT(voucher_no = %s AND voucher_type = %s) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1245 | order by posting_date desc, posting_time desc, name desc limit 1""", |
| 1246 | (item_code, voucher_no, voucher_type), |
| 1247 | ) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1248 | |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 1249 | if last_valuation_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1250 | return flt(last_valuation_rate[0][0]) |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 1251 | |
| 1252 | # If negative stock allowed, and item delivered without any incoming entry, |
| 1253 | # system does not found any SLE, then take valuation rate from Item |
| 1254 | valuation_rate = frappe.db.get_value("Item", item_code, "valuation_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1255 | |
| 1256 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 1257 | # try Item Standard rate |
| 1258 | valuation_rate = frappe.db.get_value("Item", item_code, "standard_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1259 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 1260 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 1261 | # try in price list |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1262 | valuation_rate = frappe.db.get_value( |
| 1263 | "Item Price", dict(item_code=item_code, buying=1, currency=currency), "price_list_rate" |
| 1264 | ) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 1265 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1266 | if ( |
| 1267 | not allow_zero_rate |
| 1268 | and not valuation_rate |
| 1269 | and raise_error_if_no_rate |
| 1270 | and cint(erpnext.is_perpetual_inventory_enabled(company)) |
| 1271 | ): |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 1272 | form_link = get_link_to_form("Item", item_code) |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 1273 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1274 | message = _( |
| 1275 | "Valuation Rate for the Item {0}, is required to do accounting entries for {1} {2}." |
| 1276 | ).format(form_link, voucher_type, voucher_no) |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 1277 | message += "<br><br>" + _("Here are the options to proceed:") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1278 | solutions = ( |
| 1279 | "<li>" |
| 1280 | + _( |
| 1281 | "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." |
| 1282 | ).format(voucher_type) |
| 1283 | + "</li>" |
| 1284 | ) |
| 1285 | solutions += ( |
| 1286 | "<li>" |
| 1287 | + _("If not, you can Cancel / Submit this entry") |
| 1288 | + " {0} ".format(frappe.bold("after")) |
| 1289 | + _("performing either one below:") |
| 1290 | + "</li>" |
| 1291 | ) |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 1292 | sub_solutions = "<ul><li>" + _("Create an incoming stock transaction for the Item.") + "</li>" |
| 1293 | sub_solutions += "<li>" + _("Mention Valuation Rate in the Item master.") + "</li></ul>" |
| 1294 | msg = message + solutions + sub_solutions + "</li>" |
| 1295 | |
| 1296 | frappe.throw(msg=msg, title=_("Valuation Rate Missing")) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1297 | |
| 1298 | return valuation_rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1299 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1300 | |
Ankush Menat | e7109c1 | 2021-08-26 16:40:45 +0530 | [diff] [blame] | 1301 | def update_qty_in_future_sle(args, allow_negative_stock=False): |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1302 | """Recalculate Qty after Transaction in future SLEs based on current SLE.""" |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1303 | datetime_limit_condition = "" |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1304 | qty_shift = args.actual_qty |
| 1305 | |
Ankush Menat | 7c839c4 | 2022-05-06 12:09:08 +0530 | [diff] [blame] | 1306 | args["time_format"] = "%H:%i:%s" |
| 1307 | |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1308 | # find difference/shift in qty caused by stock reconciliation |
| 1309 | if args.voucher_type == "Stock Reconciliation": |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1310 | qty_shift = get_stock_reco_qty_shift(args) |
| 1311 | |
| 1312 | # find the next nearest stock reco so that we only recalculate SLEs till that point |
| 1313 | next_stock_reco_detail = get_next_stock_reco(args) |
| 1314 | if next_stock_reco_detail: |
| 1315 | detail = next_stock_reco_detail[0] |
| 1316 | # add condition to update SLEs before this date & time |
| 1317 | datetime_limit_condition = get_datetime_limit_condition(detail) |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1318 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1319 | frappe.db.sql( |
Ankush Menat | 7e2fbc0 | 2022-05-09 11:13:31 +0530 | [diff] [blame] | 1320 | f""" |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1321 | update `tabStock Ledger Entry` |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1322 | set qty_after_transaction = qty_after_transaction + {qty_shift} |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1323 | where |
| 1324 | item_code = %(item_code)s |
| 1325 | and warehouse = %(warehouse)s |
| 1326 | and voucher_no != %(voucher_no)s |
| 1327 | and is_cancelled = 0 |
Ankush Menat | 7c839c4 | 2022-05-06 12:09:08 +0530 | [diff] [blame] | 1328 | and timestamp(posting_date, time_format(posting_time, %(time_format)s)) |
| 1329 | > timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s)) |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1330 | {datetime_limit_condition} |
Ankush Menat | 7e2fbc0 | 2022-05-09 11:13:31 +0530 | [diff] [blame] | 1331 | """, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1332 | args, |
| 1333 | ) |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1334 | |
| 1335 | validate_negative_qty_in_future_sle(args, allow_negative_stock) |
| 1336 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1337 | |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1338 | def get_stock_reco_qty_shift(args): |
| 1339 | stock_reco_qty_shift = 0 |
| 1340 | if args.get("is_cancelled"): |
| 1341 | if args.get("previous_qty_after_transaction"): |
| 1342 | # get qty (balance) that was set at submission |
| 1343 | last_balance = args.get("previous_qty_after_transaction") |
| 1344 | stock_reco_qty_shift = flt(args.qty_after_transaction) - flt(last_balance) |
| 1345 | else: |
| 1346 | stock_reco_qty_shift = flt(args.actual_qty) |
| 1347 | else: |
| 1348 | # reco is being submitted |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1349 | last_balance = get_previous_sle_of_current_voucher(args, exclude_current_voucher=True).get( |
| 1350 | "qty_after_transaction" |
| 1351 | ) |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1352 | |
| 1353 | if last_balance is not None: |
| 1354 | stock_reco_qty_shift = flt(args.qty_after_transaction) - flt(last_balance) |
| 1355 | else: |
| 1356 | stock_reco_qty_shift = args.qty_after_transaction |
| 1357 | |
| 1358 | return stock_reco_qty_shift |
| 1359 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1360 | |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1361 | def get_next_stock_reco(args): |
| 1362 | """Returns next nearest stock reconciliaton's details.""" |
| 1363 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1364 | return frappe.db.sql( |
| 1365 | """ |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1366 | select |
| 1367 | name, posting_date, posting_time, creation, voucher_no |
| 1368 | from |
marination | 8c44126 | 2021-07-02 17:46:05 +0530 | [diff] [blame] | 1369 | `tabStock Ledger Entry` |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1370 | where |
| 1371 | item_code = %(item_code)s |
| 1372 | and warehouse = %(warehouse)s |
| 1373 | and voucher_type = 'Stock Reconciliation' |
| 1374 | and voucher_no != %(voucher_no)s |
| 1375 | and is_cancelled = 0 |
| 1376 | and (timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s) |
| 1377 | or ( |
| 1378 | timestamp(posting_date, posting_time) = timestamp(%(posting_date)s, %(posting_time)s) |
| 1379 | and creation > %(creation)s |
| 1380 | ) |
| 1381 | ) |
Ankush Menat | 7e2fbc0 | 2022-05-09 11:13:31 +0530 | [diff] [blame] | 1382 | order by timestamp(posting_date, posting_time) asc, creation asc |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1383 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1384 | """, |
| 1385 | args, |
| 1386 | as_dict=1, |
| 1387 | ) |
| 1388 | |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1389 | |
| 1390 | def get_datetime_limit_condition(detail): |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1391 | return f""" |
| 1392 | and |
| 1393 | (timestamp(posting_date, posting_time) < timestamp('{detail.posting_date}', '{detail.posting_time}') |
| 1394 | or ( |
| 1395 | timestamp(posting_date, posting_time) = timestamp('{detail.posting_date}', '{detail.posting_time}') |
| 1396 | and creation < '{detail.creation}' |
| 1397 | ) |
| 1398 | )""" |
| 1399 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1400 | |
Ankush Menat | e7109c1 | 2021-08-26 16:40:45 +0530 | [diff] [blame] | 1401 | def validate_negative_qty_in_future_sle(args, allow_negative_stock=False): |
Ankush Menat | eb8b424 | 2022-02-12 13:08:28 +0530 | [diff] [blame] | 1402 | if allow_negative_stock or is_negative_stock_allowed(item_code=args.item_code): |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1403 | return |
| 1404 | if not (args.actual_qty < 0 or args.voucher_type == "Stock Reconciliation"): |
| 1405 | return |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1406 | |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1407 | neg_sle = get_future_sle_with_negative_qty(args) |
| 1408 | if neg_sle: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1409 | message = _( |
| 1410 | "{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction." |
| 1411 | ).format( |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1412 | abs(neg_sle[0]["qty_after_transaction"]), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1413 | frappe.get_desk_link("Item", args.item_code), |
| 1414 | frappe.get_desk_link("Warehouse", args.warehouse), |
| 1415 | neg_sle[0]["posting_date"], |
| 1416 | neg_sle[0]["posting_time"], |
| 1417 | frappe.get_desk_link(neg_sle[0]["voucher_type"], neg_sle[0]["voucher_no"]), |
| 1418 | ) |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1419 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1420 | frappe.throw(message, NegativeStockError, title=_("Insufficient Stock")) |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1421 | |
| 1422 | if not args.batch_no: |
| 1423 | return |
| 1424 | |
| 1425 | neg_batch_sle = get_future_sle_with_negative_batch_qty(args) |
| 1426 | if neg_batch_sle: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1427 | message = _( |
| 1428 | "{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction." |
| 1429 | ).format( |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1430 | abs(neg_batch_sle[0]["cumulative_total"]), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1431 | frappe.get_desk_link("Batch", args.batch_no), |
| 1432 | frappe.get_desk_link("Warehouse", args.warehouse), |
| 1433 | neg_batch_sle[0]["posting_date"], |
| 1434 | neg_batch_sle[0]["posting_time"], |
| 1435 | frappe.get_desk_link(neg_batch_sle[0]["voucher_type"], neg_batch_sle[0]["voucher_no"]), |
| 1436 | ) |
mergify[bot] | 7a3d301 | 2022-03-08 10:42:30 +0530 | [diff] [blame] | 1437 | frappe.throw(message, NegativeStockError, title=_("Insufficient Stock for Batch")) |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1438 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1439 | |
| 1440 | def get_future_sle_with_negative_qty(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1441 | return frappe.db.sql( |
| 1442 | """ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1443 | select |
| 1444 | qty_after_transaction, posting_date, posting_time, |
| 1445 | voucher_type, voucher_no |
| 1446 | from `tabStock Ledger Entry` |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1447 | where |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1448 | item_code = %(item_code)s |
| 1449 | and warehouse = %(warehouse)s |
| 1450 | and voucher_no != %(voucher_no)s |
| 1451 | and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) |
| 1452 | and is_cancelled = 0 |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1453 | and qty_after_transaction < 0 |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 1454 | order by timestamp(posting_date, posting_time) asc |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1455 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1456 | """, |
| 1457 | args, |
| 1458 | as_dict=1, |
| 1459 | ) |
Ankush Menat | 6a014d1 | 2021-04-12 20:21:27 +0530 | [diff] [blame] | 1460 | |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1461 | |
| 1462 | def get_future_sle_with_negative_batch_qty(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1463 | return frappe.db.sql( |
| 1464 | """ |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1465 | with batch_ledger as ( |
| 1466 | select |
| 1467 | posting_date, posting_time, voucher_type, voucher_no, |
| 1468 | sum(actual_qty) over (order by posting_date, posting_time, creation) as cumulative_total |
| 1469 | from `tabStock Ledger Entry` |
| 1470 | where |
| 1471 | item_code = %(item_code)s |
| 1472 | and warehouse = %(warehouse)s |
| 1473 | and batch_no=%(batch_no)s |
| 1474 | and is_cancelled = 0 |
| 1475 | order by posting_date, posting_time, creation |
| 1476 | ) |
| 1477 | select * from batch_ledger |
| 1478 | where |
| 1479 | cumulative_total < 0.0 |
| 1480 | and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) |
| 1481 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1482 | """, |
| 1483 | args, |
| 1484 | as_dict=1, |
| 1485 | ) |
Ankush Menat | eb8b424 | 2022-02-12 13:08:28 +0530 | [diff] [blame] | 1486 | |
| 1487 | |
| 1488 | def is_negative_stock_allowed(*, item_code: Optional[str] = None) -> bool: |
| 1489 | if cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock", cache=True)): |
| 1490 | return True |
| 1491 | if item_code and cint(frappe.db.get_value("Item", item_code, "allow_negative_stock", cache=True)): |
| 1492 | return True |
| 1493 | return False |