Marica | d6078aa | 2022-06-17 15:13:13 +0530 | [diff] [blame] | 1 | # Copyright (c) 2022, 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 |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 9 | from frappe import _, scrub |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 10 | from frappe.model.meta import get_field_precision |
Rohit Waghchaure | bb95451 | 2023-06-02 00:11:43 +0530 | [diff] [blame] | 11 | from frappe.query_builder import Case |
Ankush Menat | e1c1687 | 2022-04-21 20:01:48 +0530 | [diff] [blame] | 12 | from frappe.query_builder.functions import CombineDatetime, Sum |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 13 | from frappe.utils import ( |
| 14 | cint, |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 15 | flt, |
| 16 | get_link_to_form, |
| 17 | getdate, |
| 18 | gzip_compress, |
| 19 | gzip_decompress, |
| 20 | now, |
| 21 | nowdate, |
| 22 | parse_json, |
| 23 | ) |
Achilles Rasquinha | 361366e | 2018-02-14 17:08:59 +0530 | [diff] [blame] | 24 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 25 | import erpnext |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 26 | from erpnext.stock.doctype.bin.bin import update_qty as update_bin_qty |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 27 | from erpnext.stock.doctype.inventory_dimension.inventory_dimension import get_inventory_dimensions |
s-aga-r | f0acb20 | 2023-04-12 14:13:54 +0530 | [diff] [blame] | 28 | from erpnext.stock.doctype.stock_reservation_entry.stock_reservation_entry import ( |
| 29 | get_sre_reserved_qty_for_item_and_warehouse as get_reserved_stock, |
| 30 | ) |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 31 | from erpnext.stock.utils import ( |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 32 | get_incoming_outgoing_rate_for_cancel, |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 33 | get_or_make_bin, |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 34 | get_valuation_method, |
| 35 | ) |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 36 | from erpnext.stock.valuation import FIFOValuation, LIFOValuation, round_off_if_near_zero |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 37 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 38 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 39 | class NegativeStockError(frappe.ValidationError): |
| 40 | pass |
| 41 | |
| 42 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 43 | class SerialNoExistsInFutureTransaction(frappe.ValidationError): |
| 44 | pass |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 45 | |
Anand Doshi | 5b004ff | 2013-09-25 19:55:41 +0530 | [diff] [blame] | 46 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 47 | 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] | 48 | """Create SL entries from SL entry dicts |
Ankush Menat | eb8495a | 2022-03-02 12:01:51 +0530 | [diff] [blame] | 49 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 50 | args: |
| 51 | - allow_negative_stock: disable negative stock valiations if true |
| 52 | - via_landed_cost_voucher: landed cost voucher cancels and reposts |
| 53 | entries of purchase document. This flag is used to identify if |
| 54 | cancellation and repost is happening via landed cost voucher, in |
| 55 | such cases certain validations need to be ignored (like negative |
| 56 | stock) |
Ankush Menat | eb8495a | 2022-03-02 12:01:51 +0530 | [diff] [blame] | 57 | """ |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 58 | from erpnext.controllers.stock_controller import future_sle_exists |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 59 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 60 | if sl_entries: |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 61 | cancel = sl_entries[0].get("is_cancelled") |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 62 | if cancel: |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 63 | validate_cancellation(sl_entries) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 64 | 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] | 65 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 66 | args = get_args_for_future_sle(sl_entries[0]) |
| 67 | future_sle_exists(args, sl_entries) |
| 68 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 69 | for sle in sl_entries: |
Ankush Menat | efc4b94 | 2022-03-02 11:19:12 +0530 | [diff] [blame] | 70 | if sle.serial_no and not via_landed_cost_voucher: |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 71 | validate_serial_no(sle) |
| 72 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 73 | if cancel: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 74 | sle["actual_qty"] = -flt(sle.get("actual_qty")) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 75 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 76 | if sle["actual_qty"] < 0 and not sle.get("outgoing_rate"): |
| 77 | sle["outgoing_rate"] = get_incoming_outgoing_rate_for_cancel( |
| 78 | sle.item_code, sle.voucher_type, sle.voucher_no, sle.voucher_detail_no |
| 79 | ) |
| 80 | sle["incoming_rate"] = 0.0 |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 81 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 82 | if sle["actual_qty"] > 0 and not sle.get("incoming_rate"): |
| 83 | sle["incoming_rate"] = get_incoming_outgoing_rate_for_cancel( |
| 84 | sle.item_code, sle.voucher_type, sle.voucher_no, sle.voucher_detail_no |
| 85 | ) |
| 86 | sle["outgoing_rate"] = 0.0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 87 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 88 | if sle.get("actual_qty") or sle.get("voucher_type") == "Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 89 | sle_doc = make_entry(sle, allow_negative_stock, via_landed_cost_voucher) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 90 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 91 | args = sle_doc.as_dict() |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 92 | |
| 93 | if sle.get("voucher_type") == "Stock Reconciliation": |
| 94 | # preserve previous_qty_after_transaction for qty reposting |
| 95 | args.previous_qty_after_transaction = sle.get("previous_qty_after_transaction") |
| 96 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 97 | 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] | 98 | if is_stock_item: |
| 99 | 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] | 100 | repost_current_voucher(args, allow_negative_stock, via_landed_cost_voucher) |
Ankush Menat | ff9a6e8 | 2021-12-20 15:07:41 +0530 | [diff] [blame] | 101 | update_bin_qty(bin_name, args) |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 102 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 103 | frappe.msgprint( |
| 104 | _("Item {0} ignored since it is not a stock item").format(args.get("item_code")) |
| 105 | ) |
| 106 | |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 107 | |
| 108 | def repost_current_voucher(args, allow_negative_stock=False, via_landed_cost_voucher=False): |
| 109 | if args.get("actual_qty") or args.get("voucher_type") == "Stock Reconciliation": |
| 110 | if not args.get("posting_date"): |
| 111 | args["posting_date"] = nowdate() |
| 112 | |
marination | 7a5fd71 | 2022-07-04 17:46:54 +0530 | [diff] [blame] | 113 | if not (args.get("is_cancelled") and via_landed_cost_voucher): |
| 114 | # Reposts only current voucher SL Entries |
| 115 | # Updates valuation rate, stock value, stock queue for current transaction |
| 116 | update_entries_after( |
| 117 | { |
| 118 | "item_code": args.get("item_code"), |
| 119 | "warehouse": args.get("warehouse"), |
| 120 | "posting_date": args.get("posting_date"), |
| 121 | "posting_time": args.get("posting_time"), |
| 122 | "voucher_type": args.get("voucher_type"), |
| 123 | "voucher_no": args.get("voucher_no"), |
| 124 | "sle_id": args.get("name"), |
| 125 | "creation": args.get("creation"), |
| 126 | }, |
| 127 | allow_negative_stock=allow_negative_stock, |
| 128 | via_landed_cost_voucher=via_landed_cost_voucher, |
| 129 | ) |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 130 | |
| 131 | # update qty in future sle and Validate negative qty |
marination | 7a5fd71 | 2022-07-04 17:46:54 +0530 | [diff] [blame] | 132 | # For LCV: update future balances with -ve LCV SLE, which will be balanced by +ve LCV SLE |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 133 | update_qty_in_future_sle(args, allow_negative_stock) |
| 134 | |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 135 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 136 | def get_args_for_future_sle(row): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 137 | return frappe._dict( |
| 138 | { |
| 139 | "voucher_type": row.get("voucher_type"), |
| 140 | "voucher_no": row.get("voucher_no"), |
| 141 | "posting_date": row.get("posting_date"), |
| 142 | "posting_time": row.get("posting_time"), |
| 143 | } |
| 144 | ) |
| 145 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 146 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 147 | def validate_serial_no(sle): |
| 148 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
Ankush Menat | 66bf21f | 2022-01-16 20:45:59 +0530 | [diff] [blame] | 149 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 150 | for sn in get_serial_nos(sle.serial_no): |
| 151 | args = copy.deepcopy(sle) |
| 152 | args.serial_no = sn |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 153 | args.warehouse = "" |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 154 | |
| 155 | vouchers = [] |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 156 | for row in get_stock_ledger_entries(args, ">"): |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 157 | voucher_type = frappe.bold(row.voucher_type) |
| 158 | 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] | 159 | vouchers.append(f"{voucher_type} {voucher_no}") |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 160 | |
| 161 | if vouchers: |
| 162 | serial_no = frappe.bold(sn) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 163 | msg = ( |
| 164 | f"""The serial no {serial_no} has been used in the future transactions so you need to cancel them first. |
| 165 | The list of the transactions are as below.""" |
| 166 | + "<br><br><ul><li>" |
| 167 | ) |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 168 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 169 | msg += "</li><li>".join(vouchers) |
| 170 | msg += "</li></ul>" |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 171 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 172 | title = "Cannot Submit" if not sle.get("is_cancelled") else "Cannot Cancel" |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 173 | frappe.throw(_(msg), title=_(title), exc=SerialNoExistsInFutureTransaction) |
| 174 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 175 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 176 | def validate_cancellation(args): |
| 177 | if args[0].get("is_cancelled"): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 178 | repost_entry = frappe.db.get_value( |
| 179 | "Repost Item Valuation", |
| 180 | {"voucher_type": args[0].voucher_type, "voucher_no": args[0].voucher_no, "docstatus": 1}, |
| 181 | ["name", "status"], |
| 182 | as_dict=1, |
| 183 | ) |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 184 | |
| 185 | if repost_entry: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 186 | if repost_entry.status == "In Progress": |
| 187 | frappe.throw( |
| 188 | _( |
| 189 | "Cannot cancel the transaction. Reposting of item valuation on submission is not completed yet." |
| 190 | ) |
| 191 | ) |
| 192 | if repost_entry.status == "Queued": |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 193 | doc = frappe.get_doc("Repost Item Valuation", repost_entry.name) |
Ankush Menat | a281998 | 2022-04-08 13:20:25 +0530 | [diff] [blame] | 194 | doc.status = "Skipped" |
Ankush Menat | aa024fc | 2021-11-18 12:51:26 +0530 | [diff] [blame] | 195 | doc.flags.ignore_permissions = True |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 196 | doc.cancel() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 197 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 198 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 199 | def set_as_cancel(voucher_type, voucher_no): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 200 | frappe.db.sql( |
| 201 | """update `tabStock Ledger Entry` set is_cancelled=1, |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 202 | modified=%s, modified_by=%s |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 203 | where voucher_type=%s and voucher_no=%s and is_cancelled = 0""", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 204 | (now(), frappe.session.user, voucher_type, voucher_no), |
| 205 | ) |
| 206 | |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 207 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 208 | 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] | 209 | args["doctype"] = "Stock Ledger Entry" |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 210 | sle = frappe.get_doc(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 211 | sle.flags.ignore_permissions = 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 212 | sle.allow_negative_stock = allow_negative_stock |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 213 | sle.via_landed_cost_voucher = via_landed_cost_voucher |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 214 | sle.submit() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 215 | return sle |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 216 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 217 | |
| 218 | def repost_future_sle( |
| 219 | args=None, |
| 220 | voucher_type=None, |
| 221 | voucher_no=None, |
| 222 | allow_negative_stock=None, |
| 223 | via_landed_cost_voucher=False, |
| 224 | doc=None, |
| 225 | ): |
Nabin Hait | e1fa723 | 2022-07-20 15:19:09 +0530 | [diff] [blame] | 226 | if not args: |
| 227 | args = [] # set args to empty list if None to avoid enumerate error |
Rohit Waghchaure | 1d80d37 | 2022-07-19 16:36:22 +0530 | [diff] [blame] | 228 | |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 229 | reposting_data = {} |
| 230 | if doc and doc.reposting_data_file: |
| 231 | reposting_data = get_reposting_data(doc.reposting_data_file) |
| 232 | |
Rohit Waghchaure | 1d80d37 | 2022-07-19 16:36:22 +0530 | [diff] [blame] | 233 | items_to_be_repost = get_items_to_be_repost( |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 234 | voucher_type=voucher_type, voucher_no=voucher_no, doc=doc, reposting_data=reposting_data |
Rohit Waghchaure | 1d80d37 | 2022-07-19 16:36:22 +0530 | [diff] [blame] | 235 | ) |
| 236 | if items_to_be_repost: |
| 237 | args = items_to_be_repost |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 238 | |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 239 | distinct_item_warehouses = get_distinct_item_warehouse(args, doc, reposting_data=reposting_data) |
| 240 | affected_transactions = get_affected_transactions(doc, reposting_data=reposting_data) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 241 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 242 | i = get_current_index(doc) or 0 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 243 | while i < len(args): |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 244 | validate_item_warehouse(args[i]) |
| 245 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 246 | obj = update_entries_after( |
| 247 | { |
| 248 | "item_code": args[i].get("item_code"), |
| 249 | "warehouse": args[i].get("warehouse"), |
| 250 | "posting_date": args[i].get("posting_date"), |
| 251 | "posting_time": args[i].get("posting_time"), |
| 252 | "creation": args[i].get("creation"), |
| 253 | "distinct_item_warehouses": distinct_item_warehouses, |
| 254 | }, |
| 255 | allow_negative_stock=allow_negative_stock, |
| 256 | via_landed_cost_voucher=via_landed_cost_voucher, |
| 257 | ) |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 258 | affected_transactions.update(obj.affected_transactions) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 259 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 260 | distinct_item_warehouses[ |
| 261 | (args[i].get("item_code"), args[i].get("warehouse")) |
| 262 | ].reposting_status = True |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 263 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 264 | if obj.new_items_found: |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 265 | for item_wh, data in distinct_item_warehouses.items(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 266 | if ("args_idx" not in data and not data.reposting_status) or ( |
| 267 | data.sle_changed and data.reposting_status |
| 268 | ): |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 269 | data.args_idx = len(args) |
| 270 | args.append(data.sle) |
| 271 | elif data.sle_changed and not data.reposting_status: |
| 272 | args[data.args_idx] = data.sle |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 273 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 274 | data.sle_changed = False |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 275 | i += 1 |
| 276 | |
Rohit Waghchaure | 78c8bb2 | 2022-07-04 20:24:18 +0530 | [diff] [blame] | 277 | if doc: |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 278 | update_args_in_repost_item_valuation( |
| 279 | doc, i, args, distinct_item_warehouses, affected_transactions |
| 280 | ) |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 281 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 282 | |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 283 | def get_reposting_data(file_path) -> dict: |
| 284 | file_name = frappe.db.get_value( |
| 285 | "File", |
| 286 | { |
| 287 | "file_url": file_path, |
| 288 | "attached_to_field": "reposting_data_file", |
| 289 | }, |
| 290 | "name", |
| 291 | ) |
| 292 | |
| 293 | if not file_name: |
| 294 | return frappe._dict() |
| 295 | |
| 296 | attached_file = frappe.get_doc("File", file_name) |
| 297 | |
| 298 | data = gzip_decompress(attached_file.get_content()) |
| 299 | if data := json.loads(data.decode("utf-8")): |
| 300 | data = data |
| 301 | |
| 302 | return parse_json(data) |
| 303 | |
| 304 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 305 | def validate_item_warehouse(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 306 | for field in ["item_code", "warehouse", "posting_date", "posting_time"]: |
s-aga-r | eeda264 | 2022-01-12 20:55:30 +0530 | [diff] [blame] | 307 | if args.get(field) in [None, ""]: |
s-aga-r | ba77da0 | 2022-11-28 18:01:30 +0530 | [diff] [blame] | 308 | validation_msg = f"The field {frappe.unscrub(field)} is required for the reposting" |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 309 | frappe.throw(_(validation_msg)) |
| 310 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 311 | |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 312 | def update_args_in_repost_item_valuation( |
| 313 | doc, index, args, distinct_item_warehouses, affected_transactions |
| 314 | ): |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 315 | if not doc.items_to_be_repost: |
| 316 | file_name = "" |
| 317 | if doc.reposting_data_file: |
| 318 | file_name = get_reposting_file_name(doc.doctype, doc.name) |
| 319 | # frappe.delete_doc("File", file_name, ignore_permissions=True, delete_permanently=True) |
| 320 | |
| 321 | doc.reposting_data_file = create_json_gz_file( |
| 322 | { |
| 323 | "items_to_be_repost": args, |
| 324 | "distinct_item_and_warehouse": {str(k): v for k, v in distinct_item_warehouses.items()}, |
| 325 | "affected_transactions": affected_transactions, |
| 326 | }, |
| 327 | doc, |
| 328 | file_name, |
| 329 | ) |
| 330 | |
| 331 | doc.db_set( |
| 332 | { |
| 333 | "current_index": index, |
| 334 | "total_reposting_count": len(args), |
| 335 | "reposting_data_file": doc.reposting_data_file, |
| 336 | } |
| 337 | ) |
| 338 | |
| 339 | else: |
| 340 | doc.db_set( |
| 341 | { |
| 342 | "items_to_be_repost": json.dumps(args, default=str), |
| 343 | "distinct_item_and_warehouse": json.dumps( |
| 344 | {str(k): v for k, v in distinct_item_warehouses.items()}, default=str |
| 345 | ), |
| 346 | "current_index": index, |
| 347 | "affected_transactions": frappe.as_json(affected_transactions), |
| 348 | } |
| 349 | ) |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 350 | |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 351 | if not frappe.flags.in_test: |
| 352 | frappe.db.commit() |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 353 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 354 | frappe.publish_realtime( |
| 355 | "item_reposting_progress", |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 356 | { |
| 357 | "name": doc.name, |
| 358 | "items_to_be_repost": json.dumps(args, default=str), |
| 359 | "current_index": index, |
| 360 | "total_reposting_count": len(args), |
| 361 | }, |
Ankush Menat | c0642cf | 2023-07-29 15:02:11 +0530 | [diff] [blame] | 362 | doctype=doc.doctype, |
| 363 | docname=doc.name, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 364 | ) |
| 365 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 366 | |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 367 | def get_reposting_file_name(dt, dn): |
| 368 | return frappe.db.get_value( |
| 369 | "File", |
| 370 | { |
| 371 | "attached_to_doctype": dt, |
| 372 | "attached_to_name": dn, |
| 373 | "attached_to_field": "reposting_data_file", |
| 374 | }, |
| 375 | "name", |
| 376 | ) |
| 377 | |
| 378 | |
| 379 | def create_json_gz_file(data, doc, file_name=None) -> str: |
| 380 | encoded_content = frappe.safe_encode(frappe.as_json(data)) |
| 381 | compressed_content = gzip_compress(encoded_content) |
| 382 | |
| 383 | if not file_name: |
| 384 | json_filename = f"{scrub(doc.doctype)}-{scrub(doc.name)}.json.gz" |
| 385 | _file = frappe.get_doc( |
| 386 | { |
| 387 | "doctype": "File", |
| 388 | "file_name": json_filename, |
| 389 | "attached_to_doctype": doc.doctype, |
| 390 | "attached_to_name": doc.name, |
| 391 | "attached_to_field": "reposting_data_file", |
| 392 | "content": compressed_content, |
| 393 | "is_private": 1, |
| 394 | } |
| 395 | ) |
| 396 | _file.save(ignore_permissions=True) |
| 397 | |
| 398 | return _file.file_url |
| 399 | else: |
| 400 | file_doc = frappe.get_doc("File", file_name) |
| 401 | path = file_doc.get_full_path() |
| 402 | |
| 403 | with open(path, "wb") as f: |
| 404 | f.write(compressed_content) |
| 405 | |
| 406 | return doc.reposting_data_file |
| 407 | |
| 408 | |
| 409 | def get_items_to_be_repost(voucher_type=None, voucher_no=None, doc=None, reposting_data=None): |
| 410 | if not reposting_data and doc and doc.reposting_data_file: |
| 411 | reposting_data = get_reposting_data(doc.reposting_data_file) |
| 412 | |
| 413 | if reposting_data and reposting_data.items_to_be_repost: |
| 414 | return reposting_data.items_to_be_repost |
| 415 | |
Rohit Waghchaure | 1d80d37 | 2022-07-19 16:36:22 +0530 | [diff] [blame] | 416 | items_to_be_repost = [] |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 417 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 418 | if doc and doc.items_to_be_repost: |
Rohit Waghchaure | 1d80d37 | 2022-07-19 16:36:22 +0530 | [diff] [blame] | 419 | items_to_be_repost = json.loads(doc.items_to_be_repost) or [] |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 420 | |
Rohit Waghchaure | 1d80d37 | 2022-07-19 16:36:22 +0530 | [diff] [blame] | 421 | if not items_to_be_repost and voucher_type and voucher_no: |
| 422 | items_to_be_repost = frappe.db.get_all( |
| 423 | "Stock Ledger Entry", |
| 424 | filters={"voucher_type": voucher_type, "voucher_no": voucher_no}, |
| 425 | fields=["item_code", "warehouse", "posting_date", "posting_time", "creation"], |
| 426 | order_by="creation asc", |
| 427 | group_by="item_code, warehouse", |
| 428 | ) |
| 429 | |
Nabin Hait | e1fa723 | 2022-07-20 15:19:09 +0530 | [diff] [blame] | 430 | return items_to_be_repost or [] |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 431 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 432 | |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 433 | def get_distinct_item_warehouse(args=None, doc=None, reposting_data=None): |
| 434 | if not reposting_data and doc and doc.reposting_data_file: |
| 435 | reposting_data = get_reposting_data(doc.reposting_data_file) |
| 436 | |
| 437 | if reposting_data and reposting_data.distinct_item_and_warehouse: |
| 438 | return reposting_data.distinct_item_and_warehouse |
| 439 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 440 | distinct_item_warehouses = {} |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 441 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 442 | if doc and doc.distinct_item_and_warehouse: |
| 443 | distinct_item_warehouses = json.loads(doc.distinct_item_and_warehouse) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 444 | distinct_item_warehouses = { |
| 445 | frappe.safe_eval(k): frappe._dict(v) for k, v in distinct_item_warehouses.items() |
| 446 | } |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 447 | else: |
| 448 | for i, d in enumerate(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 449 | distinct_item_warehouses.setdefault( |
| 450 | (d.item_code, d.warehouse), frappe._dict({"reposting_status": False, "sle": d, "args_idx": i}) |
| 451 | ) |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 452 | |
| 453 | return distinct_item_warehouses |
| 454 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 455 | |
Rohit Waghchaure | fb1a40c | 2023-05-31 14:11:15 +0530 | [diff] [blame] | 456 | def get_affected_transactions(doc, reposting_data=None) -> Set[Tuple[str, str]]: |
| 457 | if not reposting_data and doc and doc.reposting_data_file: |
| 458 | reposting_data = get_reposting_data(doc.reposting_data_file) |
| 459 | |
| 460 | if reposting_data and reposting_data.affected_transactions: |
| 461 | return {tuple(transaction) for transaction in reposting_data.affected_transactions} |
| 462 | |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 463 | if not doc.affected_transactions: |
| 464 | return set() |
| 465 | |
| 466 | transactions = frappe.parse_json(doc.affected_transactions) |
| 467 | return {tuple(transaction) for transaction in transactions} |
| 468 | |
| 469 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 470 | def get_current_index(doc=None): |
| 471 | if doc and doc.current_index: |
| 472 | return doc.current_index |
| 473 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 474 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 475 | class update_entries_after(object): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 476 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 477 | update valution rate and qty after transaction |
| 478 | from the current time-bucket onwards |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 479 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 480 | :param args: args as dict |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 481 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 482 | args = { |
| 483 | "item_code": "ABC", |
| 484 | "warehouse": "XYZ", |
| 485 | "posting_date": "2012-12-12", |
| 486 | "posting_time": "12:00" |
| 487 | } |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 488 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 489 | |
| 490 | def __init__( |
| 491 | self, |
| 492 | args, |
| 493 | allow_zero_rate=False, |
| 494 | allow_negative_stock=None, |
| 495 | via_landed_cost_voucher=False, |
| 496 | verbose=1, |
| 497 | ): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 498 | self.exceptions = {} |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 499 | self.verbose = verbose |
| 500 | self.allow_zero_rate = allow_zero_rate |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 501 | self.via_landed_cost_voucher = via_landed_cost_voucher |
Ankush Menat | eb8b424 | 2022-02-12 13:08:28 +0530 | [diff] [blame] | 502 | self.item_code = args.get("item_code") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 503 | self.allow_negative_stock = allow_negative_stock or is_negative_stock_allowed( |
| 504 | item_code=self.item_code |
| 505 | ) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 506 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 507 | self.args = frappe._dict(args) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 508 | if self.args.sle_id: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 509 | self.args["name"] = self.args.sle_id |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 510 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 511 | self.company = frappe.get_cached_value("Warehouse", self.args.warehouse, "company") |
Marica | d6078aa | 2022-06-17 15:13:13 +0530 | [diff] [blame] | 512 | self.set_precision() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 513 | self.valuation_method = get_valuation_method(self.item_code) |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 514 | |
| 515 | self.new_items_found = False |
| 516 | self.distinct_item_warehouses = args.get("distinct_item_warehouses", frappe._dict()) |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 517 | self.affected_transactions: Set[Tuple[str, str]] = set() |
s-aga-r | f0acb20 | 2023-04-12 14:13:54 +0530 | [diff] [blame] | 518 | self.reserved_stock = get_reserved_stock(self.args.item_code, self.args.warehouse) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 519 | |
| 520 | self.data = frappe._dict() |
| 521 | self.initialize_previous_data(self.args) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 522 | self.build() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 523 | |
Marica | d6078aa | 2022-06-17 15:13:13 +0530 | [diff] [blame] | 524 | def set_precision(self): |
| 525 | self.flt_precision = cint(frappe.db.get_default("float_precision")) or 2 |
| 526 | self.currency_precision = get_field_precision( |
| 527 | frappe.get_meta("Stock Ledger Entry").get_field("stock_value") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 528 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 529 | |
| 530 | def initialize_previous_data(self, args): |
| 531 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 532 | Get previous sl entries for current item for each related warehouse |
| 533 | and assigns into self.data dict |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 534 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 535 | :Data Structure: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 536 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 537 | self.data = { |
| 538 | warehouse1: { |
| 539 | 'previus_sle': {}, |
| 540 | 'qty_after_transaction': 10, |
| 541 | 'valuation_rate': 100, |
| 542 | 'stock_value': 1000, |
| 543 | 'prev_stock_value': 1000, |
| 544 | 'stock_queue': '[[10, 100]]', |
| 545 | 'stock_value_difference': 1000 |
| 546 | } |
| 547 | } |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 548 | |
| 549 | """ |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 550 | self.data.setdefault(args.warehouse, frappe._dict()) |
| 551 | warehouse_dict = self.data[args.warehouse] |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 552 | previous_sle = get_previous_sle_of_current_voucher(args) |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 553 | warehouse_dict.previous_sle = previous_sle |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 554 | |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 555 | for key in ("qty_after_transaction", "valuation_rate", "stock_value"): |
| 556 | setattr(warehouse_dict, key, flt(previous_sle.get(key))) |
| 557 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 558 | warehouse_dict.update( |
| 559 | { |
| 560 | "prev_stock_value": previous_sle.stock_value or 0.0, |
| 561 | "stock_queue": json.loads(previous_sle.stock_queue or "[]"), |
| 562 | "stock_value_difference": 0.0, |
| 563 | } |
| 564 | ) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 565 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 566 | def build(self): |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 567 | from erpnext.controllers.stock_controller import future_sle_exists |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 568 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 569 | if self.args.get("sle_id"): |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 570 | self.process_sle_against_current_timestamp() |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 571 | if not future_sle_exists(self.args): |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 572 | self.update_bin() |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 573 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 574 | entries_to_fix = self.get_future_entries_to_fix() |
| 575 | |
| 576 | i = 0 |
| 577 | while i < len(entries_to_fix): |
| 578 | sle = entries_to_fix[i] |
| 579 | i += 1 |
| 580 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 581 | self.process_sle(sle) |
Rohit Waghchaure | 9e5e2de | 2023-05-25 23:41:56 +0530 | [diff] [blame] | 582 | self.update_bin_data(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 583 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 584 | if sle.dependant_sle_voucher_detail_no: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 585 | 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] | 586 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 587 | if self.exceptions: |
| 588 | self.raise_exceptions() |
| 589 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 590 | def process_sle_against_current_timestamp(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 591 | sl_entries = self.get_sle_against_current_voucher() |
| 592 | for sle in sl_entries: |
| 593 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 594 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 595 | def get_sle_against_current_voucher(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 596 | self.args["time_format"] = "%H:%i:%s" |
Nabin Hait | f2be080 | 2021-02-15 19:27:49 +0530 | [diff] [blame] | 597 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 598 | return frappe.db.sql( |
| 599 | """ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 600 | select |
| 601 | *, timestamp(posting_date, posting_time) as "timestamp" |
| 602 | from |
| 603 | `tabStock Ledger Entry` |
| 604 | where |
| 605 | item_code = %(item_code)s |
| 606 | and warehouse = %(warehouse)s |
rohitwaghchaure | fe4540d | 2021-08-26 12:52:36 +0530 | [diff] [blame] | 607 | and is_cancelled = 0 |
Rohit Waghchaure | a05c47e | 2022-12-22 10:24:04 +0530 | [diff] [blame] | 608 | and ( |
| 609 | posting_date = %(posting_date)s and |
| 610 | time_format(posting_time, %(time_format)s) = time_format(%(posting_time)s, %(time_format)s) |
| 611 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 612 | order by |
| 613 | creation ASC |
| 614 | for update |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 615 | """, |
| 616 | self.args, |
| 617 | as_dict=1, |
| 618 | ) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 619 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 620 | def get_future_entries_to_fix(self): |
| 621 | # includes current entry! |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 622 | args = self.data[self.args.warehouse].previous_sle or frappe._dict( |
| 623 | {"item_code": self.item_code, "warehouse": self.args.warehouse} |
| 624 | ) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 625 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 626 | return list(self.get_sle_after_datetime(args)) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 627 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 628 | def get_dependent_entries_to_fix(self, entries_to_fix, sle): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 629 | dependant_sle = get_sle_by_voucher_detail_no( |
| 630 | sle.dependant_sle_voucher_detail_no, excluded_sle=sle.name |
| 631 | ) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 632 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 633 | if not dependant_sle: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 634 | return entries_to_fix |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 635 | elif ( |
| 636 | dependant_sle.item_code == self.item_code and dependant_sle.warehouse == self.args.warehouse |
| 637 | ): |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 638 | return entries_to_fix |
| 639 | elif dependant_sle.item_code != self.item_code: |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 640 | self.update_distinct_item_warehouses(dependant_sle) |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 641 | return entries_to_fix |
| 642 | elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse in self.data: |
| 643 | return entries_to_fix |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 644 | else: |
Rohit Waghchaure | 1d80d37 | 2022-07-19 16:36:22 +0530 | [diff] [blame] | 645 | self.initialize_previous_data(dependant_sle) |
| 646 | self.update_distinct_item_warehouses(dependant_sle) |
Rohit Waghchaure | 78c8bb2 | 2022-07-04 20:24:18 +0530 | [diff] [blame] | 647 | return entries_to_fix |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 648 | |
| 649 | def update_distinct_item_warehouses(self, dependant_sle): |
| 650 | key = (dependant_sle.item_code, dependant_sle.warehouse) |
mergify[bot] | 87d0251 | 2023-08-19 15:37:33 +0530 | [diff] [blame] | 651 | val = frappe._dict({"sle": dependant_sle}) |
Rohit Waghchaure | b77a808 | 2023-07-01 11:30:46 +0530 | [diff] [blame] | 652 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 653 | if key not in self.distinct_item_warehouses: |
| 654 | self.distinct_item_warehouses[key] = val |
| 655 | self.new_items_found = True |
| 656 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 657 | existing_sle_posting_date = ( |
| 658 | self.distinct_item_warehouses[key].get("sle", {}).get("posting_date") |
| 659 | ) |
Rohit Waghchaure | c16a581 | 2023-07-11 17:51:27 +0530 | [diff] [blame] | 660 | |
| 661 | dependent_voucher_detail_nos = self.get_dependent_voucher_detail_nos(key) |
| 662 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 663 | if getdate(dependant_sle.posting_date) < getdate(existing_sle_posting_date): |
| 664 | val.sle_changed = True |
mergify[bot] | 87d0251 | 2023-08-19 15:37:33 +0530 | [diff] [blame] | 665 | dependent_voucher_detail_nos.append(dependant_sle.voucher_detail_no) |
| 666 | val.dependent_voucher_detail_nos = dependent_voucher_detail_nos |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 667 | self.distinct_item_warehouses[key] = val |
| 668 | self.new_items_found = True |
Rohit Waghchaure | c16a581 | 2023-07-11 17:51:27 +0530 | [diff] [blame] | 669 | elif dependant_sle.voucher_detail_no not in set(dependent_voucher_detail_nos): |
| 670 | # Future dependent voucher needs to be repost to get the correct stock value |
| 671 | # If dependent voucher has not reposted, then add it to the list |
| 672 | dependent_voucher_detail_nos.append(dependant_sle.voucher_detail_no) |
Rohit Waghchaure | b77a808 | 2023-07-01 11:30:46 +0530 | [diff] [blame] | 673 | self.new_items_found = True |
Rohit Waghchaure | c16a581 | 2023-07-11 17:51:27 +0530 | [diff] [blame] | 674 | val.dependent_voucher_detail_nos = dependent_voucher_detail_nos |
| 675 | self.distinct_item_warehouses[key] = val |
| 676 | |
| 677 | def get_dependent_voucher_detail_nos(self, key): |
| 678 | if "dependent_voucher_detail_nos" not in self.distinct_item_warehouses[key]: |
| 679 | self.distinct_item_warehouses[key].dependent_voucher_detail_nos = [] |
| 680 | |
| 681 | return self.distinct_item_warehouses[key].dependent_voucher_detail_nos |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 682 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 683 | def process_sle(self, sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 684 | # previous sle data for this warehouse |
| 685 | self.wh_data = self.data[sle.warehouse] |
Ankush Menat | ecdb493 | 2022-04-17 19:06:13 +0530 | [diff] [blame] | 686 | self.affected_transactions.add((sle.voucher_type, sle.voucher_no)) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 687 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 688 | 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] | 689 | # validate negative stock for serialized items, fifo valuation |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 690 | # or when negative stock is not allowed for moving average |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 691 | if not self.validate_negative_stock(sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 692 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 693 | return |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 694 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 695 | # Get dynamic incoming/outgoing rate |
Ankush Menat | 701878f | 2022-03-01 18:08:29 +0530 | [diff] [blame] | 696 | if not self.args.get("sle_id"): |
| 697 | self.get_dynamic_incoming_outgoing_rate(sle) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 698 | |
Rohit Waghchaure | 3266e54 | 2022-10-12 15:09:50 +0530 | [diff] [blame] | 699 | if ( |
Rohit Waghchaure | 7bfc8f1 | 2023-04-14 12:22:19 +0530 | [diff] [blame] | 700 | sle.voucher_type == "Stock Reconciliation" |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 701 | and (sle.batch_no or (sle.has_batch_no and sle.serial_and_batch_bundle)) |
Rohit Waghchaure | 7bfc8f1 | 2023-04-14 12:22:19 +0530 | [diff] [blame] | 702 | and sle.voucher_detail_no |
| 703 | and sle.actual_qty < 0 |
| 704 | ): |
| 705 | self.reset_actual_qty_for_stock_reco(sle) |
| 706 | |
| 707 | if ( |
Rohit Waghchaure | 3266e54 | 2022-10-12 15:09:50 +0530 | [diff] [blame] | 708 | sle.voucher_type in ["Purchase Receipt", "Purchase Invoice"] |
| 709 | and sle.voucher_detail_no |
| 710 | and sle.actual_qty < 0 |
Rohit Waghchaure | b5a2ccf | 2023-05-04 15:38:35 +0530 | [diff] [blame] | 711 | and is_internal_transfer(sle) |
Rohit Waghchaure | 3266e54 | 2022-10-12 15:09:50 +0530 | [diff] [blame] | 712 | ): |
| 713 | sle.outgoing_rate = get_incoming_rate_for_inter_company_transfer(sle) |
| 714 | |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 715 | dimensions = get_inventory_dimensions() |
| 716 | has_dimensions = False |
| 717 | if dimensions: |
| 718 | for dimension in dimensions: |
| 719 | if sle.get(dimension.get("fieldname")): |
| 720 | has_dimensions = True |
| 721 | |
Rohit Waghchaure | e6143ab | 2023-03-13 14:51:43 +0530 | [diff] [blame] | 722 | if sle.serial_and_batch_bundle: |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 723 | self.calculate_valuation_for_serial_batch_bundle(sle) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 724 | else: |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 725 | if sle.voucher_type == "Stock Reconciliation" and not sle.batch_no and not has_dimensions: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 726 | # assert |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 727 | self.wh_data.valuation_rate = sle.valuation_rate |
| 728 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 729 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt( |
| 730 | self.wh_data.valuation_rate |
| 731 | ) |
Ankush Menat | b0cf619 | 2022-01-16 13:02:23 +0530 | [diff] [blame] | 732 | if self.valuation_method != "Moving Average": |
| 733 | 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] | 734 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 735 | if self.valuation_method == "Moving Average": |
| 736 | self.get_moving_average_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 737 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 738 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt( |
| 739 | self.wh_data.valuation_rate |
| 740 | ) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 741 | else: |
Ankush Menat | f089d39 | 2022-02-02 12:51:21 +0530 | [diff] [blame] | 742 | self.update_queue_values(sle) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 743 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 744 | # rounding as per precision |
Marica | d6078aa | 2022-06-17 15:13:13 +0530 | [diff] [blame] | 745 | self.wh_data.stock_value = flt(self.wh_data.stock_value, self.currency_precision) |
Ankush Menat | 609d2fc | 2022-02-20 11:35:53 +0530 | [diff] [blame] | 746 | if not self.wh_data.qty_after_transaction: |
| 747 | self.wh_data.stock_value = 0.0 |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 748 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 749 | stock_value_difference = self.wh_data.stock_value - self.wh_data.prev_stock_value |
| 750 | self.wh_data.prev_stock_value = self.wh_data.stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 751 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 752 | # update current sle |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 753 | sle.qty_after_transaction = self.wh_data.qty_after_transaction |
| 754 | sle.valuation_rate = self.wh_data.valuation_rate |
| 755 | sle.stock_value = self.wh_data.stock_value |
| 756 | sle.stock_queue = json.dumps(self.wh_data.stock_queue) |
Rushabh Mehta | 2e0e711 | 2015-02-18 11:38:05 +0530 | [diff] [blame] | 757 | sle.stock_value_difference = stock_value_difference |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 758 | sle.doctype = "Stock Ledger Entry" |
Rohit Waghchaure | 683a47f | 2022-10-11 15:11:39 +0530 | [diff] [blame] | 759 | |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 760 | frappe.get_doc(sle).db_update() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 761 | |
Ankush Menat | 701878f | 2022-03-01 18:08:29 +0530 | [diff] [blame] | 762 | if not self.args.get("sle_id"): |
| 763 | self.update_outgoing_rate_on_transaction(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 764 | |
Rohit Waghchaure | 7bfc8f1 | 2023-04-14 12:22:19 +0530 | [diff] [blame] | 765 | def reset_actual_qty_for_stock_reco(self, sle): |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 766 | if sle.serial_and_batch_bundle: |
| 767 | current_qty = frappe.get_cached_value( |
| 768 | "Serial and Batch Bundle", sle.serial_and_batch_bundle, "total_qty" |
| 769 | ) |
| 770 | |
| 771 | if current_qty is not None: |
| 772 | current_qty = abs(current_qty) |
| 773 | else: |
| 774 | current_qty = frappe.get_cached_value( |
| 775 | "Stock Reconciliation Item", sle.voucher_detail_no, "current_qty" |
| 776 | ) |
Rohit Waghchaure | 7bfc8f1 | 2023-04-14 12:22:19 +0530 | [diff] [blame] | 777 | |
| 778 | if current_qty: |
| 779 | sle.actual_qty = current_qty * -1 |
| 780 | elif current_qty == 0: |
| 781 | sle.is_cancelled = 1 |
| 782 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 783 | def calculate_valuation_for_serial_batch_bundle(self, sle): |
| 784 | doc = frappe.get_cached_doc("Serial and Batch Bundle", sle.serial_and_batch_bundle) |
| 785 | |
| 786 | doc.set_incoming_rate(save=True) |
| 787 | doc.calculate_qty_and_amount(save=True) |
| 788 | |
| 789 | self.wh_data.stock_value = round_off_if_near_zero(self.wh_data.stock_value + doc.total_amount) |
| 790 | |
| 791 | self.wh_data.qty_after_transaction += doc.total_qty |
| 792 | if self.wh_data.qty_after_transaction: |
| 793 | self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction |
| 794 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 795 | def validate_negative_stock(self, sle): |
| 796 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 797 | validate negative stock for entries current datetime onwards |
| 798 | will not consider cancelled entries |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 799 | """ |
s-aga-r | f0acb20 | 2023-04-12 14:13:54 +0530 | [diff] [blame] | 800 | diff = self.wh_data.qty_after_transaction + flt(sle.actual_qty) - flt(self.reserved_stock) |
Marica | d6078aa | 2022-06-17 15:13:13 +0530 | [diff] [blame] | 801 | diff = flt(diff, self.flt_precision) # respect system precision |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 802 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 803 | if diff < 0 and abs(diff) > 0.0001: |
| 804 | # negative stock! |
| 805 | exc = sle.copy().update({"diff": diff}) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 806 | self.exceptions.setdefault(sle.warehouse, []).append(exc) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 807 | return False |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 808 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 809 | return True |
| 810 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 811 | def get_dynamic_incoming_outgoing_rate(self, sle): |
| 812 | # Get updated incoming/outgoing rate from transaction |
| 813 | if sle.recalculate_rate: |
| 814 | rate = self.get_incoming_outgoing_rate_from_transaction(sle) |
| 815 | |
| 816 | if flt(sle.actual_qty) >= 0: |
| 817 | sle.incoming_rate = rate |
| 818 | else: |
| 819 | sle.outgoing_rate = rate |
| 820 | |
| 821 | def get_incoming_outgoing_rate_from_transaction(self, sle): |
| 822 | rate = 0 |
| 823 | # Material Transfer, Repack, Manufacturing |
| 824 | if sle.voucher_type == "Stock Entry": |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 825 | self.recalculate_amounts_in_stock_entry(sle.voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 826 | rate = frappe.db.get_value("Stock Entry Detail", sle.voucher_detail_no, "valuation_rate") |
| 827 | # Sales and Purchase Return |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 828 | elif sle.voucher_type in ( |
| 829 | "Purchase Receipt", |
| 830 | "Purchase Invoice", |
| 831 | "Delivery Note", |
| 832 | "Sales Invoice", |
Sagar Sharma | 323bdf8 | 2022-05-17 15:14:07 +0530 | [diff] [blame] | 833 | "Subcontracting Receipt", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 834 | ): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 835 | 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] | 836 | from erpnext.controllers.sales_and_purchase_return import ( |
| 837 | get_rate_for_return, # don't move this import to top |
| 838 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 839 | |
| 840 | rate = get_rate_for_return( |
| 841 | sle.voucher_type, |
| 842 | sle.voucher_no, |
| 843 | sle.item_code, |
| 844 | voucher_detail_no=sle.voucher_detail_no, |
| 845 | sle=sle, |
| 846 | ) |
Rohit Waghchaure | ddd24ea | 2022-08-09 14:50:20 +0530 | [diff] [blame] | 847 | |
| 848 | elif ( |
| 849 | sle.voucher_type in ["Purchase Receipt", "Purchase Invoice"] |
Rohit Waghchaure | a03b4ce | 2022-09-08 19:16:00 +0530 | [diff] [blame] | 850 | and sle.voucher_detail_no |
Rohit Waghchaure | b5a2ccf | 2023-05-04 15:38:35 +0530 | [diff] [blame] | 851 | and is_internal_transfer(sle) |
Rohit Waghchaure | ddd24ea | 2022-08-09 14:50:20 +0530 | [diff] [blame] | 852 | ): |
Rohit Waghchaure | 683a47f | 2022-10-11 15:11:39 +0530 | [diff] [blame] | 853 | rate = get_incoming_rate_for_inter_company_transfer(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 854 | else: |
| 855 | if sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 856 | rate_field = "valuation_rate" |
Sagar Sharma | 323bdf8 | 2022-05-17 15:14:07 +0530 | [diff] [blame] | 857 | elif sle.voucher_type == "Subcontracting Receipt": |
| 858 | rate_field = "rate" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 859 | else: |
| 860 | rate_field = "incoming_rate" |
| 861 | |
| 862 | # check in item table |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 863 | item_code, incoming_rate = frappe.db.get_value( |
| 864 | sle.voucher_type + " Item", sle.voucher_detail_no, ["item_code", rate_field] |
| 865 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 866 | |
| 867 | if item_code == sle.item_code: |
| 868 | rate = incoming_rate |
| 869 | else: |
| 870 | if sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 871 | ref_doctype = "Packed Item" |
Sagar Sharma | 323bdf8 | 2022-05-17 15:14:07 +0530 | [diff] [blame] | 872 | elif sle == "Subcontracting Receipt": |
| 873 | ref_doctype = "Subcontracting Receipt Supplied Item" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 874 | else: |
| 875 | ref_doctype = "Purchase Receipt Item Supplied" |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 876 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 877 | rate = frappe.db.get_value( |
| 878 | ref_doctype, |
| 879 | {"parent_detail_docname": sle.voucher_detail_no, "item_code": sle.item_code}, |
| 880 | rate_field, |
| 881 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 882 | |
| 883 | return rate |
| 884 | |
| 885 | def update_outgoing_rate_on_transaction(self, sle): |
| 886 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 887 | Update outgoing rate in Stock Entry, Delivery Note, Sales Invoice and Sales Return |
| 888 | 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] | 889 | """ |
| 890 | if sle.actual_qty and sle.voucher_detail_no: |
| 891 | outgoing_rate = abs(flt(sle.stock_value_difference)) / abs(sle.actual_qty) |
| 892 | |
| 893 | if flt(sle.actual_qty) < 0 and sle.voucher_type == "Stock Entry": |
| 894 | self.update_rate_on_stock_entry(sle, outgoing_rate) |
| 895 | elif sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 896 | self.update_rate_on_delivery_and_sales_return(sle, outgoing_rate) |
| 897 | elif flt(sle.actual_qty) < 0 and sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
| 898 | self.update_rate_on_purchase_receipt(sle, outgoing_rate) |
Sagar Sharma | 323bdf8 | 2022-05-17 15:14:07 +0530 | [diff] [blame] | 899 | elif flt(sle.actual_qty) < 0 and sle.voucher_type == "Subcontracting Receipt": |
| 900 | self.update_rate_on_subcontracting_receipt(sle, outgoing_rate) |
s-aga-r | 88a3f65 | 2023-05-30 16:54:28 +0530 | [diff] [blame] | 901 | elif sle.voucher_type == "Stock Reconciliation": |
| 902 | self.update_rate_on_stock_reconciliation(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 903 | |
| 904 | def update_rate_on_stock_entry(self, sle, outgoing_rate): |
| 905 | frappe.db.set_value("Stock Entry Detail", sle.voucher_detail_no, "basic_rate", outgoing_rate) |
| 906 | |
Ankush Menat | 701878f | 2022-03-01 18:08:29 +0530 | [diff] [blame] | 907 | # Update outgoing item's rate, recalculate FG Item's rate and total incoming/outgoing amount |
| 908 | if not sle.dependant_sle_voucher_detail_no: |
| 909 | self.recalculate_amounts_in_stock_entry(sle.voucher_no) |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 910 | |
| 911 | def recalculate_amounts_in_stock_entry(self, voucher_no): |
| 912 | stock_entry = frappe.get_doc("Stock Entry", voucher_no, for_update=True) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 913 | stock_entry.calculate_rate_and_amount(reset_outgoing_rate=False, raise_error_if_no_rate=False) |
| 914 | stock_entry.db_update() |
| 915 | for d in stock_entry.items: |
| 916 | d.db_update() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 917 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 918 | def update_rate_on_delivery_and_sales_return(self, sle, outgoing_rate): |
| 919 | # Update item's incoming rate on transaction |
| 920 | item_code = frappe.db.get_value(sle.voucher_type + " Item", sle.voucher_detail_no, "item_code") |
| 921 | if item_code == sle.item_code: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 922 | frappe.db.set_value( |
| 923 | sle.voucher_type + " Item", sle.voucher_detail_no, "incoming_rate", outgoing_rate |
| 924 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 925 | else: |
| 926 | # packed item |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 927 | frappe.db.set_value( |
| 928 | "Packed Item", |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 929 | {"parent_detail_docname": sle.voucher_detail_no, "item_code": sle.item_code}, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 930 | "incoming_rate", |
| 931 | outgoing_rate, |
| 932 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 933 | |
| 934 | def update_rate_on_purchase_receipt(self, sle, outgoing_rate): |
| 935 | if frappe.db.exists(sle.voucher_type + " Item", sle.voucher_detail_no): |
Rohit Waghchaure | 683a47f | 2022-10-11 15:11:39 +0530 | [diff] [blame] | 936 | if sle.voucher_type in ["Purchase Receipt", "Purchase Invoice"] and frappe.get_cached_value( |
| 937 | sle.voucher_type, sle.voucher_no, "is_internal_supplier" |
| 938 | ): |
| 939 | frappe.db.set_value( |
| 940 | f"{sle.voucher_type} Item", sle.voucher_detail_no, "valuation_rate", sle.outgoing_rate |
| 941 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 942 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 943 | frappe.db.set_value( |
| 944 | "Purchase Receipt Item Supplied", sle.voucher_detail_no, "rate", outgoing_rate |
| 945 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 946 | |
| 947 | # Recalculate subcontracted item's rate in case of subcontracted purchase receipt/invoice |
Sagar Sharma | d074c93 | 2022-03-31 19:57:42 +0530 | [diff] [blame] | 948 | 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] | 949 | doc = frappe.get_doc(sle.voucher_type, sle.voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 950 | doc.update_valuation_rate(reset_outgoing_rate=False) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 951 | for d in doc.items + doc.supplied_items: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 952 | d.db_update() |
| 953 | |
Sagar Sharma | 323bdf8 | 2022-05-17 15:14:07 +0530 | [diff] [blame] | 954 | def update_rate_on_subcontracting_receipt(self, sle, outgoing_rate): |
s-aga-r | a6cb6c6 | 2023-05-03 09:51:58 +0530 | [diff] [blame] | 955 | if frappe.db.exists("Subcontracting Receipt Item", sle.voucher_detail_no): |
| 956 | frappe.db.set_value("Subcontracting Receipt Item", sle.voucher_detail_no, "rate", outgoing_rate) |
Sagar Sharma | 323bdf8 | 2022-05-17 15:14:07 +0530 | [diff] [blame] | 957 | else: |
| 958 | frappe.db.set_value( |
Sagar Sharma | 9c72c2a | 2023-05-12 11:46:32 +0530 | [diff] [blame] | 959 | "Subcontracting Receipt Supplied Item", |
| 960 | sle.voucher_detail_no, |
| 961 | {"rate": outgoing_rate, "amount": abs(sle.actual_qty) * outgoing_rate}, |
Sagar Sharma | 323bdf8 | 2022-05-17 15:14:07 +0530 | [diff] [blame] | 962 | ) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 963 | |
s-aga-r | a6cb6c6 | 2023-05-03 09:51:58 +0530 | [diff] [blame] | 964 | scr = frappe.get_doc("Subcontracting Receipt", sle.voucher_no, for_update=True) |
Sagar Sharma | 9c72c2a | 2023-05-12 11:46:32 +0530 | [diff] [blame] | 965 | scr.calculate_items_qty_and_amount() |
s-aga-r | a6cb6c6 | 2023-05-03 09:51:58 +0530 | [diff] [blame] | 966 | scr.db_update() |
Sagar Sharma | 9c72c2a | 2023-05-12 11:46:32 +0530 | [diff] [blame] | 967 | for d in scr.items: |
s-aga-r | a6cb6c6 | 2023-05-03 09:51:58 +0530 | [diff] [blame] | 968 | d.db_update() |
| 969 | |
s-aga-r | 88a3f65 | 2023-05-30 16:54:28 +0530 | [diff] [blame] | 970 | def update_rate_on_stock_reconciliation(self, sle): |
| 971 | if not sle.serial_no and not sle.batch_no: |
| 972 | sr = frappe.get_doc("Stock Reconciliation", sle.voucher_no, for_update=True) |
| 973 | |
| 974 | for item in sr.items: |
| 975 | # Skip for Serial and Batch Items |
s-aga-r | db159dd | 2023-06-12 18:28:16 +0530 | [diff] [blame] | 976 | if item.name != sle.voucher_detail_no or item.serial_no or item.batch_no: |
s-aga-r | 88a3f65 | 2023-05-30 16:54:28 +0530 | [diff] [blame] | 977 | continue |
| 978 | |
| 979 | previous_sle = get_previous_sle( |
| 980 | { |
| 981 | "item_code": item.item_code, |
| 982 | "warehouse": item.warehouse, |
| 983 | "posting_date": sr.posting_date, |
| 984 | "posting_time": sr.posting_time, |
| 985 | "sle": sle.name, |
| 986 | } |
| 987 | ) |
| 988 | |
| 989 | item.current_qty = previous_sle.get("qty_after_transaction") or 0.0 |
| 990 | item.current_valuation_rate = previous_sle.get("valuation_rate") or 0.0 |
| 991 | item.current_amount = flt(item.current_qty) * flt(item.current_valuation_rate) |
| 992 | |
| 993 | item.amount = flt(item.qty) * flt(item.valuation_rate) |
s-aga-r | 6a1b0a2 | 2023-06-15 11:39:22 +0530 | [diff] [blame] | 994 | item.quantity_difference = item.qty - item.current_qty |
s-aga-r | 88a3f65 | 2023-05-30 16:54:28 +0530 | [diff] [blame] | 995 | item.amount_difference = item.amount - item.current_amount |
| 996 | else: |
| 997 | sr.difference_amount = sum([item.amount_difference for item in sr.items]) |
| 998 | sr.db_update() |
| 999 | |
| 1000 | for item in sr.items: |
| 1001 | item.db_update() |
| 1002 | |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 1003 | def get_incoming_value_for_serial_nos(self, sle, serial_nos): |
| 1004 | # get rate from serial nos within same company |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1005 | all_serial_nos = frappe.get_all( |
| 1006 | "Serial No", fields=["purchase_rate", "name", "company"], filters={"name": ("in", serial_nos)} |
| 1007 | ) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 1008 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1009 | 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] | 1010 | |
| 1011 | # Get rate for serial nos which has been transferred to other company |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1012 | 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] | 1013 | for serial_no in invalid_serial_nos: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1014 | incoming_rate = frappe.db.sql( |
| 1015 | """ |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 1016 | select incoming_rate |
| 1017 | from `tabStock Ledger Entry` |
| 1018 | where |
| 1019 | company = %s |
| 1020 | and actual_qty > 0 |
Ankush Menat | 82ea958 | 2022-01-16 20:19:04 +0530 | [diff] [blame] | 1021 | and is_cancelled = 0 |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 1022 | and (serial_no = %s |
| 1023 | or serial_no like %s |
| 1024 | or serial_no like %s |
| 1025 | or serial_no like %s |
| 1026 | ) |
| 1027 | order by posting_date desc |
| 1028 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1029 | """, |
| 1030 | (sle.company, serial_no, serial_no + "\n%", "%\n" + serial_no, "%\n" + serial_no + "\n%"), |
| 1031 | ) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 1032 | |
| 1033 | incoming_values += flt(incoming_rate[0][0]) if incoming_rate else 0 |
| 1034 | |
| 1035 | return incoming_values |
| 1036 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1037 | def get_moving_average_values(self, sle): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1038 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1039 | new_stock_qty = flt(self.wh_data.qty_after_transaction) + actual_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 1040 | if new_stock_qty >= 0: |
| 1041 | if actual_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1042 | if flt(self.wh_data.qty_after_transaction) <= 0: |
| 1043 | self.wh_data.valuation_rate = sle.incoming_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 1044 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1045 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + ( |
| 1046 | actual_qty * sle.incoming_rate |
| 1047 | ) |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 1048 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1049 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 1050 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 1051 | elif sle.outgoing_rate: |
| 1052 | if new_stock_qty: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1053 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + ( |
| 1054 | actual_qty * sle.outgoing_rate |
| 1055 | ) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1056 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1057 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 1058 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1059 | self.wh_data.valuation_rate = sle.outgoing_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 1060 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1061 | if flt(self.wh_data.qty_after_transaction) >= 0 and sle.outgoing_rate: |
| 1062 | self.wh_data.valuation_rate = sle.outgoing_rate |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 1063 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1064 | if not self.wh_data.valuation_rate and actual_qty > 0: |
| 1065 | self.wh_data.valuation_rate = sle.incoming_rate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 1066 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 1067 | # 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] | 1068 | # allow zero valuration rate flag set |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1069 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1070 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate( |
| 1071 | sle.voucher_type, sle.voucher_detail_no |
| 1072 | ) |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 1073 | if not allow_zero_valuation_rate: |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 1074 | self.wh_data.valuation_rate = self.get_fallback_rate(sle) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 1075 | |
Ankush Menat | f089d39 | 2022-02-02 12:51:21 +0530 | [diff] [blame] | 1076 | def update_queue_values(self, sle): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1077 | incoming_rate = flt(sle.incoming_rate) |
| 1078 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 1079 | outgoing_rate = flt(sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1080 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1081 | self.wh_data.qty_after_transaction = round_off_if_near_zero( |
| 1082 | self.wh_data.qty_after_transaction + actual_qty |
| 1083 | ) |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 1084 | |
Ankush Menat | 97e18a1 | 2022-01-15 17:42:25 +0530 | [diff] [blame] | 1085 | if self.valuation_method == "LIFO": |
| 1086 | stock_queue = LIFOValuation(self.wh_data.stock_queue) |
| 1087 | else: |
| 1088 | stock_queue = FIFOValuation(self.wh_data.stock_queue) |
| 1089 | |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 1090 | _prev_qty, prev_stock_value = stock_queue.get_total_stock_and_value() |
| 1091 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1092 | if actual_qty > 0: |
Ankush Menat | 97e18a1 | 2022-01-15 17:42:25 +0530 | [diff] [blame] | 1093 | stock_queue.add_stock(qty=actual_qty, rate=incoming_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1094 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1095 | |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 1096 | def rate_generator() -> float: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1097 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate( |
| 1098 | sle.voucher_type, sle.voucher_detail_no |
| 1099 | ) |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 1100 | if not allow_zero_valuation_rate: |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 1101 | return self.get_fallback_rate(sle) |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 1102 | else: |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 1103 | return 0.0 |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 1104 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1105 | stock_queue.remove_stock( |
| 1106 | qty=abs(actual_qty), outgoing_rate=outgoing_rate, rate_generator=rate_generator |
| 1107 | ) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 1108 | |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 1109 | _qty, stock_value = stock_queue.get_total_stock_and_value() |
| 1110 | |
| 1111 | stock_value_difference = stock_value - prev_stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 1112 | |
Ankush Menat | 97e18a1 | 2022-01-15 17:42:25 +0530 | [diff] [blame] | 1113 | self.wh_data.stock_queue = stock_queue.state |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1114 | self.wh_data.stock_value = round_off_if_near_zero( |
| 1115 | self.wh_data.stock_value + stock_value_difference |
| 1116 | ) |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 1117 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1118 | if not self.wh_data.stock_queue: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1119 | self.wh_data.stock_queue.append( |
| 1120 | [0, sle.incoming_rate or sle.outgoing_rate or self.wh_data.valuation_rate] |
| 1121 | ) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 1122 | |
Ankush Menat | b534fee | 2022-02-19 20:58:36 +0530 | [diff] [blame] | 1123 | if self.wh_data.qty_after_transaction: |
| 1124 | self.wh_data.valuation_rate = self.wh_data.stock_value / self.wh_data.qty_after_transaction |
| 1125 | |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1126 | def update_batched_values(self, sle): |
| 1127 | incoming_rate = flt(sle.incoming_rate) |
| 1128 | actual_qty = flt(sle.actual_qty) |
Ankush Menat | 4b29fb6 | 2021-12-18 18:40:22 +0530 | [diff] [blame] | 1129 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1130 | self.wh_data.qty_after_transaction = round_off_if_near_zero( |
| 1131 | self.wh_data.qty_after_transaction + actual_qty |
| 1132 | ) |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1133 | |
| 1134 | if actual_qty > 0: |
| 1135 | stock_value_difference = incoming_rate * actual_qty |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1136 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1137 | outgoing_rate = get_batch_incoming_rate( |
| 1138 | item_code=sle.item_code, |
| 1139 | warehouse=sle.warehouse, |
Rohit Waghchaure | 6c9b212 | 2022-12-05 14:48:18 +0530 | [diff] [blame] | 1140 | serial_and_batch_bundle=sle.serial_and_batch_bundle, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1141 | posting_date=sle.posting_date, |
| 1142 | posting_time=sle.posting_time, |
| 1143 | creation=sle.creation, |
| 1144 | ) |
Ankush Menat | aba7a7c | 2022-02-19 19:36:28 +0530 | [diff] [blame] | 1145 | if outgoing_rate is None: |
| 1146 | # This can *only* happen if qty available for the batch is zero. |
| 1147 | # in such case fall back various other rates. |
| 1148 | # future entries will correct the overall accounting as each |
| 1149 | # batch individually uses moving average rates. |
| 1150 | outgoing_rate = self.get_fallback_rate(sle) |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1151 | stock_value_difference = outgoing_rate * actual_qty |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1152 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1153 | self.wh_data.stock_value = round_off_if_near_zero( |
| 1154 | self.wh_data.stock_value + stock_value_difference |
| 1155 | ) |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1156 | if self.wh_data.qty_after_transaction: |
| 1157 | 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] | 1158 | |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 1159 | def check_if_allow_zero_valuation_rate(self, voucher_type, voucher_detail_no): |
deepeshgarg007 | f9c0ef3 | 2019-07-30 18:49:19 +0530 | [diff] [blame] | 1160 | ref_item_dt = "" |
| 1161 | |
| 1162 | if voucher_type == "Stock Entry": |
| 1163 | ref_item_dt = voucher_type + " Detail" |
| 1164 | elif voucher_type in ["Purchase Invoice", "Sales Invoice", "Delivery Note", "Purchase Receipt"]: |
| 1165 | ref_item_dt = voucher_type + " Item" |
| 1166 | |
| 1167 | if ref_item_dt: |
| 1168 | return frappe.db.get_value(ref_item_dt, voucher_detail_no, "allow_zero_valuation_rate") |
| 1169 | else: |
| 1170 | return 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 1171 | |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 1172 | def get_fallback_rate(self, sle) -> float: |
| 1173 | """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] | 1174 | This should only get used for negative stock.""" |
| 1175 | return get_valuation_rate( |
| 1176 | sle.item_code, |
| 1177 | sle.warehouse, |
| 1178 | sle.voucher_type, |
| 1179 | sle.voucher_no, |
| 1180 | self.allow_zero_rate, |
| 1181 | currency=erpnext.get_company_currency(sle.company), |
| 1182 | company=sle.company, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1183 | ) |
Ankush Menat | d7ca83e | 2022-02-19 19:35:33 +0530 | [diff] [blame] | 1184 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1185 | def get_sle_before_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1186 | """get previous stock ledger entry before current time-bucket""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1187 | sle = get_stock_ledger_entries(args, "<", "desc", "limit 1", for_update=False) |
| 1188 | sle = sle[0] if sle else frappe._dict() |
| 1189 | return sle |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1190 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1191 | def get_sle_after_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1192 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1193 | 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] | 1194 | |
| 1195 | def raise_exceptions(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1196 | msg_list = [] |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 1197 | for warehouse, exceptions in self.exceptions.items(): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1198 | deficiency = min(e["diff"] for e in exceptions) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 1199 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1200 | if ( |
| 1201 | exceptions[0]["voucher_type"], |
| 1202 | exceptions[0]["voucher_no"], |
| 1203 | ) in frappe.local.flags.currently_saving: |
Nabin Hait | 3edefb1 | 2016-07-20 16:13:18 +0530 | [diff] [blame] | 1204 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1205 | msg = _("{0} units of {1} needed in {2} to complete this transaction.").format( |
s-aga-r | f0acb20 | 2023-04-12 14:13:54 +0530 | [diff] [blame] | 1206 | frappe.bold(abs(deficiency)), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1207 | frappe.get_desk_link("Item", exceptions[0]["item_code"]), |
| 1208 | frappe.get_desk_link("Warehouse", warehouse), |
| 1209 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1210 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1211 | msg = _( |
| 1212 | "{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction." |
| 1213 | ).format( |
s-aga-r | f0acb20 | 2023-04-12 14:13:54 +0530 | [diff] [blame] | 1214 | frappe.bold(abs(deficiency)), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1215 | frappe.get_desk_link("Item", exceptions[0]["item_code"]), |
| 1216 | frappe.get_desk_link("Warehouse", warehouse), |
| 1217 | exceptions[0]["posting_date"], |
| 1218 | exceptions[0]["posting_time"], |
| 1219 | frappe.get_desk_link(exceptions[0]["voucher_type"], exceptions[0]["voucher_no"]), |
| 1220 | ) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 1221 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1222 | if msg: |
s-aga-r | f0acb20 | 2023-04-12 14:13:54 +0530 | [diff] [blame] | 1223 | if self.reserved_stock: |
s-aga-r | 7e8fd8f | 2023-04-21 17:44:44 +0530 | [diff] [blame] | 1224 | allowed_qty = abs(exceptions[0]["actual_qty"]) - abs(exceptions[0]["diff"]) |
s-aga-r | 2d8363a | 2023-09-02 11:02:24 +0530 | [diff] [blame] | 1225 | |
| 1226 | if allowed_qty > 0: |
| 1227 | msg = "{0} As {1} units are reserved for other sales orders, you are allowed to consume only {2} units.".format( |
| 1228 | msg, frappe.bold(self.reserved_stock), frappe.bold(allowed_qty) |
| 1229 | ) |
| 1230 | else: |
| 1231 | msg = "{0} As the full stock is reserved for other sales orders, you're not allowed to consume the stock.".format( |
| 1232 | msg, |
| 1233 | ) |
s-aga-r | f0acb20 | 2023-04-12 14:13:54 +0530 | [diff] [blame] | 1234 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1235 | msg_list.append(msg) |
| 1236 | |
| 1237 | if msg_list: |
| 1238 | message = "\n\n".join(msg_list) |
| 1239 | if self.verbose: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1240 | frappe.throw(message, NegativeStockError, title=_("Insufficient Stock")) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1241 | else: |
| 1242 | raise NegativeStockError(message) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1243 | |
Rohit Waghchaure | 9e5e2de | 2023-05-25 23:41:56 +0530 | [diff] [blame] | 1244 | def update_bin_data(self, sle): |
| 1245 | bin_name = get_or_make_bin(sle.item_code, sle.warehouse) |
Rohit Waghchaure | 718ad3f | 2023-05-26 11:29:22 +0530 | [diff] [blame] | 1246 | values_to_update = { |
| 1247 | "actual_qty": sle.qty_after_transaction, |
| 1248 | "stock_value": sle.stock_value, |
| 1249 | } |
| 1250 | |
| 1251 | if sle.valuation_rate is not None: |
| 1252 | values_to_update["valuation_rate"] = sle.valuation_rate |
| 1253 | |
| 1254 | frappe.db.set_value("Bin", bin_name, values_to_update) |
Rohit Waghchaure | 9e5e2de | 2023-05-25 23:41:56 +0530 | [diff] [blame] | 1255 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1256 | def update_bin(self): |
| 1257 | # update bin for each warehouse |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 1258 | for warehouse, data in self.data.items(): |
Ankush Menat | 97060c4 | 2021-12-03 11:50:38 +0530 | [diff] [blame] | 1259 | bin_name = get_or_make_bin(self.item_code, warehouse) |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 1260 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1261 | 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] | 1262 | if data.valuation_rate is not None: |
| 1263 | updated_values["valuation_rate"] = data.valuation_rate |
Ankush Menat | 8376fbc | 2022-10-06 20:35:33 +0530 | [diff] [blame] | 1264 | frappe.db.set_value("Bin", bin_name, updated_values, update_modified=True) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 1265 | |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1266 | |
Rohit Waghchaure | 6d513e2 | 2023-02-02 18:40:15 +0530 | [diff] [blame] | 1267 | def get_previous_sle_of_current_voucher(args, operator="<", exclude_current_voucher=False): |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1268 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 1269 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1270 | args["time_format"] = "%H:%i:%s" |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1271 | if not args.get("posting_date"): |
| 1272 | args["posting_date"] = "1900-01-01" |
| 1273 | if not args.get("posting_time"): |
| 1274 | args["posting_time"] = "00:00" |
| 1275 | |
| 1276 | voucher_condition = "" |
| 1277 | if exclude_current_voucher: |
| 1278 | voucher_no = args.get("voucher_no") |
| 1279 | voucher_condition = f"and voucher_no != '{voucher_no}'" |
| 1280 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1281 | sle = frappe.db.sql( |
| 1282 | """ |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1283 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 1284 | from `tabStock Ledger Entry` |
| 1285 | where item_code = %(item_code)s |
| 1286 | and warehouse = %(warehouse)s |
| 1287 | and is_cancelled = 0 |
| 1288 | {voucher_condition} |
Rohit Waghchaure | a05c47e | 2022-12-22 10:24:04 +0530 | [diff] [blame] | 1289 | and ( |
| 1290 | posting_date < %(posting_date)s or |
| 1291 | ( |
| 1292 | posting_date = %(posting_date)s and |
Rohit Waghchaure | 6d513e2 | 2023-02-02 18:40:15 +0530 | [diff] [blame] | 1293 | time_format(posting_time, %(time_format)s) {operator} time_format(%(posting_time)s, %(time_format)s) |
Rohit Waghchaure | a05c47e | 2022-12-22 10:24:04 +0530 | [diff] [blame] | 1294 | ) |
| 1295 | ) |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1296 | order by timestamp(posting_date, posting_time) desc, creation desc |
| 1297 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1298 | for update""".format( |
Rohit Waghchaure | 6d513e2 | 2023-02-02 18:40:15 +0530 | [diff] [blame] | 1299 | operator=operator, voucher_condition=voucher_condition |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1300 | ), |
| 1301 | args, |
| 1302 | as_dict=1, |
| 1303 | ) |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1304 | |
| 1305 | return sle[0] if sle else frappe._dict() |
| 1306 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1307 | |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 1308 | def get_previous_sle(args, for_update=False, extra_cond=None): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 1309 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1310 | get the last sle on or before the current time-bucket, |
| 1311 | to get actual qty before transaction, this function |
| 1312 | is called from various transaction like stock entry, reco etc |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 1313 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1314 | args = { |
| 1315 | "item_code": "ABC", |
| 1316 | "warehouse": "XYZ", |
| 1317 | "posting_date": "2012-12-12", |
| 1318 | "posting_time": "12:00", |
| 1319 | "sle": "name of reference Stock Ledger Entry" |
| 1320 | } |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 1321 | """ |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1322 | args["name"] = args.get("sle", None) or "" |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 1323 | sle = get_stock_ledger_entries( |
| 1324 | args, "<=", "desc", "limit 1", for_update=for_update, extra_cond=extra_cond |
| 1325 | ) |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 1326 | return sle and sle[0] or {} |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1327 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1328 | |
| 1329 | def get_stock_ledger_entries( |
| 1330 | previous_sle, |
| 1331 | operator=None, |
| 1332 | order="desc", |
| 1333 | limit=None, |
| 1334 | for_update=False, |
| 1335 | debug=False, |
| 1336 | check_serial_no=True, |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 1337 | extra_cond=None, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1338 | ): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1339 | """get stock ledger entries filtered by specific posting datetime conditions""" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1340 | conditions = " and timestamp(posting_date, posting_time) {0} timestamp(%(posting_date)s, %(posting_time)s)".format( |
| 1341 | operator |
| 1342 | ) |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 1343 | if previous_sle.get("warehouse"): |
| 1344 | conditions += " and warehouse = %(warehouse)s" |
| 1345 | elif previous_sle.get("warehouse_condition"): |
| 1346 | conditions += " and " + previous_sle.get("warehouse_condition") |
| 1347 | |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 1348 | if check_serial_no and previous_sle.get("serial_no"): |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 1349 | # conditions += " and serial_no like {}".format(frappe.db.escape('%{0}%'.format(previous_sle.get("serial_no")))) |
| 1350 | serial_no = previous_sle.get("serial_no") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1351 | conditions += ( |
| 1352 | """ and |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 1353 | ( |
| 1354 | serial_no = {0} |
| 1355 | or serial_no like {1} |
| 1356 | or serial_no like {2} |
| 1357 | or serial_no like {3} |
| 1358 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1359 | """ |
| 1360 | ).format( |
| 1361 | frappe.db.escape(serial_no), |
| 1362 | frappe.db.escape("{}\n%".format(serial_no)), |
| 1363 | frappe.db.escape("%\n{}".format(serial_no)), |
| 1364 | frappe.db.escape("%\n{}\n%".format(serial_no)), |
| 1365 | ) |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 1366 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1367 | if not previous_sle.get("posting_date"): |
| 1368 | previous_sle["posting_date"] = "1900-01-01" |
| 1369 | if not previous_sle.get("posting_time"): |
| 1370 | previous_sle["posting_time"] = "00:00" |
| 1371 | |
| 1372 | if operator in (">", "<=") and previous_sle.get("name"): |
| 1373 | conditions += " and name!=%(name)s" |
| 1374 | |
mergify[bot] | 27a1e3b | 2023-10-16 19:15:18 +0530 | [diff] [blame] | 1375 | if extra_cond: |
| 1376 | conditions += f"{extra_cond}" |
| 1377 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1378 | return frappe.db.sql( |
| 1379 | """ |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1380 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 1381 | from `tabStock Ledger Entry` |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1382 | where item_code = %%(item_code)s |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1383 | and is_cancelled = 0 |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 1384 | %(conditions)s |
Aditya Hase | 0c16424 | 2019-01-07 22:07:13 +0530 | [diff] [blame] | 1385 | order by timestamp(posting_date, posting_time) %(order)s, creation %(order)s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1386 | %(limit)s %(for_update)s""" |
| 1387 | % { |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1388 | "conditions": conditions, |
| 1389 | "limit": limit or "", |
| 1390 | "for_update": for_update and "for update" or "", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1391 | "order": order, |
| 1392 | }, |
| 1393 | previous_sle, |
| 1394 | as_dict=1, |
| 1395 | debug=debug, |
| 1396 | ) |
| 1397 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 1398 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1399 | 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] | 1400 | return frappe.db.get_value( |
| 1401 | "Stock Ledger Entry", |
Rohit Waghchaure | f8c852c | 2023-02-01 15:38:12 +0530 | [diff] [blame] | 1402 | {"voucher_detail_no": voucher_detail_no, "name": ["!=", excluded_sle], "is_cancelled": 0}, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1403 | [ |
| 1404 | "item_code", |
| 1405 | "warehouse", |
Rohit Waghchaure | b77a808 | 2023-07-01 11:30:46 +0530 | [diff] [blame] | 1406 | "actual_qty", |
| 1407 | "qty_after_transaction", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1408 | "posting_date", |
| 1409 | "posting_time", |
Rohit Waghchaure | c16a581 | 2023-07-11 17:51:27 +0530 | [diff] [blame] | 1410 | "voucher_detail_no", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1411 | "timestamp(posting_date, posting_time) as timestamp", |
| 1412 | ], |
| 1413 | as_dict=1, |
| 1414 | ) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1415 | |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1416 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1417 | def get_batch_incoming_rate( |
Rohit Waghchaure | 6c9b212 | 2022-12-05 14:48:18 +0530 | [diff] [blame] | 1418 | item_code, warehouse, serial_and_batch_bundle, posting_date, posting_time, creation=None |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1419 | ): |
| 1420 | |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1421 | sle = frappe.qb.DocType("Stock Ledger Entry") |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 1422 | batch_ledger = frappe.qb.DocType("Serial and Batch Entry") |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1423 | |
Ankush Menat | e1c1687 | 2022-04-21 20:01:48 +0530 | [diff] [blame] | 1424 | timestamp_condition = CombineDatetime(sle.posting_date, sle.posting_time) < CombineDatetime( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1425 | posting_date, posting_time |
| 1426 | ) |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1427 | if creation: |
| 1428 | timestamp_condition |= ( |
Ankush Menat | e1c1687 | 2022-04-21 20:01:48 +0530 | [diff] [blame] | 1429 | CombineDatetime(sle.posting_date, sle.posting_time) |
| 1430 | == CombineDatetime(posting_date, posting_time) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1431 | ) & (sle.creation < creation) |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1432 | |
Rohit Waghchaure | 6c9b212 | 2022-12-05 14:48:18 +0530 | [diff] [blame] | 1433 | batches = frappe.get_all( |
Rohit Waghchaure | 5bb3173 | 2023-03-21 10:54:41 +0530 | [diff] [blame] | 1434 | "Serial and Batch Entry", fields=["batch_no"], filters={"parent": serial_and_batch_bundle} |
Rohit Waghchaure | 6c9b212 | 2022-12-05 14:48:18 +0530 | [diff] [blame] | 1435 | ) |
| 1436 | |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1437 | batch_details = ( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1438 | frappe.qb.from_(sle) |
Rohit Waghchaure | 6c9b212 | 2022-12-05 14:48:18 +0530 | [diff] [blame] | 1439 | .inner_join(batch_ledger) |
| 1440 | .on(sle.serial_and_batch_bundle == batch_ledger.parent) |
| 1441 | .select( |
| 1442 | Sum( |
| 1443 | Case() |
| 1444 | .when(sle.actual_qty > 0, batch_ledger.qty * batch_ledger.incoming_rate) |
| 1445 | .else_(batch_ledger.qty * batch_ledger.outgoing_rate * -1) |
| 1446 | ).as_("batch_value"), |
| 1447 | Sum(Case().when(sle.actual_qty > 0, batch_ledger.qty).else_(batch_ledger.qty * -1)).as_( |
| 1448 | "batch_qty" |
| 1449 | ), |
| 1450 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1451 | .where( |
| 1452 | (sle.item_code == item_code) |
| 1453 | & (sle.warehouse == warehouse) |
Rohit Waghchaure | 6c9b212 | 2022-12-05 14:48:18 +0530 | [diff] [blame] | 1454 | & (batch_ledger.batch_no.isin([row.batch_no for row in batches])) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1455 | & (sle.is_cancelled == 0) |
| 1456 | ) |
| 1457 | .where(timestamp_condition) |
Ankush Menat | 102fff2 | 2022-02-19 15:51:04 +0530 | [diff] [blame] | 1458 | ).run(as_dict=True) |
Ankush Menat | ce0514c | 2022-02-15 11:41:41 +0530 | [diff] [blame] | 1459 | |
| 1460 | if batch_details and batch_details[0].batch_qty: |
| 1461 | return batch_details[0].batch_value / batch_details[0].batch_qty |
| 1462 | |
| 1463 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1464 | def get_valuation_rate( |
| 1465 | item_code, |
| 1466 | warehouse, |
| 1467 | voucher_type, |
| 1468 | voucher_no, |
| 1469 | allow_zero_rate=False, |
| 1470 | currency=None, |
| 1471 | company=None, |
| 1472 | raise_error_if_no_rate=True, |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 1473 | serial_and_batch_bundle=None, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1474 | ): |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 1475 | |
Rohit Waghchaure | d3ceb07 | 2023-03-31 09:03:54 +0530 | [diff] [blame] | 1476 | from erpnext.stock.serial_batch_bundle import BatchNoValuation |
| 1477 | |
Ankush Menat | f7ffe04 | 2021-11-01 13:21:14 +0530 | [diff] [blame] | 1478 | if not company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1479 | company = frappe.get_cached_value("Warehouse", warehouse, "company") |
Ankush Menat | f7ffe04 | 2021-11-01 13:21:14 +0530 | [diff] [blame] | 1480 | |
Ankush Menat | 342d09a | 2022-02-19 14:28:51 +0530 | [diff] [blame] | 1481 | # Get moving average rate of a specific batch number |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 1482 | if warehouse and serial_and_batch_bundle: |
| 1483 | batch_obj = BatchNoValuation( |
| 1484 | sle=frappe._dict( |
| 1485 | { |
| 1486 | "item_code": item_code, |
| 1487 | "warehouse": warehouse, |
| 1488 | "actual_qty": -1, |
| 1489 | "serial_and_batch_bundle": serial_and_batch_bundle, |
| 1490 | } |
| 1491 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1492 | ) |
Ankush Menat | 342d09a | 2022-02-19 14:28:51 +0530 | [diff] [blame] | 1493 | |
Rohit Waghchaure | c2d7461 | 2023-03-29 11:40:36 +0530 | [diff] [blame] | 1494 | return batch_obj.get_incoming_rate() |
| 1495 | |
Ankush Menat | f7ffe04 | 2021-11-01 13:21:14 +0530 | [diff] [blame] | 1496 | # Get valuation rate from last sle for the same item and warehouse |
Akhil Narang | dd911aa | 2023-09-26 13:45:39 +0530 | [diff] [blame] | 1497 | if last_valuation_rate := frappe.db.sql( |
| 1498 | """select valuation_rate |
| 1499 | from `tabStock Ledger Entry` force index (item_warehouse) |
| 1500 | where |
| 1501 | item_code = %s |
| 1502 | AND warehouse = %s |
| 1503 | AND valuation_rate >= 0 |
| 1504 | AND is_cancelled = 0 |
| 1505 | AND NOT (voucher_no = %s AND voucher_type = %s) |
| 1506 | order by posting_date desc, posting_time desc, name desc limit 1""", |
| 1507 | (item_code, warehouse, voucher_no, voucher_type), |
| 1508 | ): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1509 | return flt(last_valuation_rate[0][0]) |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 1510 | |
| 1511 | # If negative stock allowed, and item delivered without any incoming entry, |
| 1512 | # system does not found any SLE, then take valuation rate from Item |
| 1513 | valuation_rate = frappe.db.get_value("Item", item_code, "valuation_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1514 | |
| 1515 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 1516 | # try Item Standard rate |
| 1517 | valuation_rate = frappe.db.get_value("Item", item_code, "standard_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1518 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 1519 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 1520 | # try in price list |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1521 | valuation_rate = frappe.db.get_value( |
| 1522 | "Item Price", dict(item_code=item_code, buying=1, currency=currency), "price_list_rate" |
| 1523 | ) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 1524 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1525 | if ( |
| 1526 | not allow_zero_rate |
| 1527 | and not valuation_rate |
| 1528 | and raise_error_if_no_rate |
| 1529 | and cint(erpnext.is_perpetual_inventory_enabled(company)) |
| 1530 | ): |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 1531 | form_link = get_link_to_form("Item", item_code) |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 1532 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1533 | message = _( |
| 1534 | "Valuation Rate for the Item {0}, is required to do accounting entries for {1} {2}." |
| 1535 | ).format(form_link, voucher_type, voucher_no) |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 1536 | message += "<br><br>" + _("Here are the options to proceed:") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1537 | solutions = ( |
| 1538 | "<li>" |
| 1539 | + _( |
| 1540 | "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." |
| 1541 | ).format(voucher_type) |
| 1542 | + "</li>" |
| 1543 | ) |
| 1544 | solutions += ( |
| 1545 | "<li>" |
| 1546 | + _("If not, you can Cancel / Submit this entry") |
| 1547 | + " {0} ".format(frappe.bold("after")) |
| 1548 | + _("performing either one below:") |
| 1549 | + "</li>" |
| 1550 | ) |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 1551 | sub_solutions = "<ul><li>" + _("Create an incoming stock transaction for the Item.") + "</li>" |
| 1552 | sub_solutions += "<li>" + _("Mention Valuation Rate in the Item master.") + "</li></ul>" |
| 1553 | msg = message + solutions + sub_solutions + "</li>" |
| 1554 | |
| 1555 | frappe.throw(msg=msg, title=_("Valuation Rate Missing")) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 1556 | |
| 1557 | return valuation_rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1558 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1559 | |
Ankush Menat | e7109c1 | 2021-08-26 16:40:45 +0530 | [diff] [blame] | 1560 | def update_qty_in_future_sle(args, allow_negative_stock=False): |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1561 | """Recalculate Qty after Transaction in future SLEs based on current SLE.""" |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1562 | datetime_limit_condition = "" |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1563 | qty_shift = args.actual_qty |
| 1564 | |
Ankush Menat | 7c839c4 | 2022-05-06 12:09:08 +0530 | [diff] [blame] | 1565 | args["time_format"] = "%H:%i:%s" |
| 1566 | |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1567 | # find difference/shift in qty caused by stock reconciliation |
| 1568 | if args.voucher_type == "Stock Reconciliation": |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1569 | qty_shift = get_stock_reco_qty_shift(args) |
| 1570 | |
| 1571 | # find the next nearest stock reco so that we only recalculate SLEs till that point |
| 1572 | next_stock_reco_detail = get_next_stock_reco(args) |
| 1573 | if next_stock_reco_detail: |
| 1574 | detail = next_stock_reco_detail[0] |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 1575 | if detail.batch_no or (detail.serial_and_batch_bundle and detail.has_batch_no): |
Rohit Waghchaure | ef4bd77 | 2023-04-04 23:56:57 +0530 | [diff] [blame] | 1576 | regenerate_sle_for_batch_stock_reco(detail) |
| 1577 | |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1578 | # add condition to update SLEs before this date & time |
| 1579 | datetime_limit_condition = get_datetime_limit_condition(detail) |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1580 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1581 | frappe.db.sql( |
Ankush Menat | 7e2fbc0 | 2022-05-09 11:13:31 +0530 | [diff] [blame] | 1582 | f""" |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1583 | update `tabStock Ledger Entry` |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1584 | set qty_after_transaction = qty_after_transaction + {qty_shift} |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1585 | where |
| 1586 | item_code = %(item_code)s |
| 1587 | and warehouse = %(warehouse)s |
| 1588 | and voucher_no != %(voucher_no)s |
| 1589 | and is_cancelled = 0 |
Rohit Waghchaure | a05c47e | 2022-12-22 10:24:04 +0530 | [diff] [blame] | 1590 | and ( |
| 1591 | posting_date > %(posting_date)s or |
| 1592 | ( |
| 1593 | posting_date = %(posting_date)s and |
| 1594 | time_format(posting_time, %(time_format)s) > time_format(%(posting_time)s, %(time_format)s) |
| 1595 | ) |
| 1596 | ) |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1597 | {datetime_limit_condition} |
Ankush Menat | 7e2fbc0 | 2022-05-09 11:13:31 +0530 | [diff] [blame] | 1598 | """, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1599 | args, |
| 1600 | ) |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1601 | |
| 1602 | validate_negative_qty_in_future_sle(args, allow_negative_stock) |
| 1603 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1604 | |
Rohit Waghchaure | ef4bd77 | 2023-04-04 23:56:57 +0530 | [diff] [blame] | 1605 | def regenerate_sle_for_batch_stock_reco(detail): |
| 1606 | doc = frappe.get_cached_doc("Stock Reconciliation", detail.voucher_no) |
Rohit Waghchaure | ef4bd77 | 2023-04-04 23:56:57 +0530 | [diff] [blame] | 1607 | doc.recalculate_current_qty(detail.item_code, detail.batch_no) |
Rohit Waghchaure | f2253dd | 2023-04-22 11:16:12 +0530 | [diff] [blame] | 1608 | |
| 1609 | if not frappe.db.exists( |
| 1610 | "Repost Item Valuation", {"voucher_no": doc.name, "status": "Queued", "docstatus": "1"} |
| 1611 | ): |
Rohit Waghchaure | 2d5ccc0 | 2023-05-01 21:17:18 +0530 | [diff] [blame] | 1612 | doc.repost_future_sle_and_gle(force=True) |
Rohit Waghchaure | ef4bd77 | 2023-04-04 23:56:57 +0530 | [diff] [blame] | 1613 | |
| 1614 | |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1615 | def get_stock_reco_qty_shift(args): |
| 1616 | stock_reco_qty_shift = 0 |
| 1617 | if args.get("is_cancelled"): |
| 1618 | if args.get("previous_qty_after_transaction"): |
| 1619 | # get qty (balance) that was set at submission |
| 1620 | last_balance = args.get("previous_qty_after_transaction") |
| 1621 | stock_reco_qty_shift = flt(args.qty_after_transaction) - flt(last_balance) |
| 1622 | else: |
| 1623 | stock_reco_qty_shift = flt(args.actual_qty) |
| 1624 | else: |
| 1625 | # reco is being submitted |
Rohit Waghchaure | 6d513e2 | 2023-02-02 18:40:15 +0530 | [diff] [blame] | 1626 | last_balance = get_previous_sle_of_current_voucher(args, "<=", exclude_current_voucher=True).get( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1627 | "qty_after_transaction" |
| 1628 | ) |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1629 | |
| 1630 | if last_balance is not None: |
| 1631 | stock_reco_qty_shift = flt(args.qty_after_transaction) - flt(last_balance) |
| 1632 | else: |
| 1633 | stock_reco_qty_shift = args.qty_after_transaction |
| 1634 | |
| 1635 | return stock_reco_qty_shift |
| 1636 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1637 | |
Rohit Waghchaure | d9dd64b | 2023-04-14 13:00:12 +0530 | [diff] [blame] | 1638 | def get_next_stock_reco(kwargs): |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1639 | """Returns next nearest stock reconciliaton's details.""" |
| 1640 | |
Rohit Waghchaure | d9dd64b | 2023-04-14 13:00:12 +0530 | [diff] [blame] | 1641 | sle = frappe.qb.DocType("Stock Ledger Entry") |
| 1642 | |
| 1643 | query = ( |
| 1644 | frappe.qb.from_(sle) |
| 1645 | .select( |
| 1646 | sle.name, |
| 1647 | sle.posting_date, |
| 1648 | sle.posting_time, |
| 1649 | sle.creation, |
| 1650 | sle.voucher_no, |
| 1651 | sle.item_code, |
| 1652 | sle.batch_no, |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 1653 | sle.serial_and_batch_bundle, |
Rohit Waghchaure | d9dd64b | 2023-04-14 13:00:12 +0530 | [diff] [blame] | 1654 | sle.actual_qty, |
Rohit Waghchaure | 42b2294 | 2023-05-27 19:18:03 +0530 | [diff] [blame] | 1655 | sle.has_batch_no, |
Rohit Waghchaure | d9dd64b | 2023-04-14 13:00:12 +0530 | [diff] [blame] | 1656 | ) |
| 1657 | .where( |
| 1658 | (sle.item_code == kwargs.get("item_code")) |
| 1659 | & (sle.warehouse == kwargs.get("warehouse")) |
| 1660 | & (sle.voucher_type == "Stock Reconciliation") |
| 1661 | & (sle.voucher_no != kwargs.get("voucher_no")) |
| 1662 | & (sle.is_cancelled == 0) |
| 1663 | & ( |
| 1664 | ( |
| 1665 | CombineDatetime(sle.posting_date, sle.posting_time) |
| 1666 | > CombineDatetime(kwargs.get("posting_date"), kwargs.get("posting_time")) |
Rohit Waghchaure | 379b215 | 2023-04-24 17:32:32 +0530 | [diff] [blame] | 1667 | ) |
| 1668 | | ( |
| 1669 | ( |
| 1670 | CombineDatetime(sle.posting_date, sle.posting_time) |
| 1671 | == CombineDatetime(kwargs.get("posting_date"), kwargs.get("posting_time")) |
Rohit Waghchaure | d9dd64b | 2023-04-14 13:00:12 +0530 | [diff] [blame] | 1672 | ) |
Rohit Waghchaure | 379b215 | 2023-04-24 17:32:32 +0530 | [diff] [blame] | 1673 | & (sle.creation > kwargs.get("creation")) |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1674 | ) |
| 1675 | ) |
Rohit Waghchaure | d9dd64b | 2023-04-14 13:00:12 +0530 | [diff] [blame] | 1676 | ) |
Rohit Waghchaure | 6bccd86 | 2023-04-17 14:22:27 +0530 | [diff] [blame] | 1677 | .orderby(CombineDatetime(sle.posting_date, sle.posting_time)) |
| 1678 | .orderby(sle.creation) |
Rohit Waghchaure | fcfa884 | 2023-04-20 09:48:15 +0530 | [diff] [blame] | 1679 | .limit(1) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1680 | ) |
| 1681 | |
Rohit Waghchaure | d9dd64b | 2023-04-14 13:00:12 +0530 | [diff] [blame] | 1682 | if kwargs.get("batch_no"): |
Deepesh Garg | ea6eeac | 2023-04-20 12:48:44 +0530 | [diff] [blame] | 1683 | query = query.where(sle.batch_no == kwargs.get("batch_no")) |
Rohit Waghchaure | d9dd64b | 2023-04-14 13:00:12 +0530 | [diff] [blame] | 1684 | |
| 1685 | return query.run(as_dict=True) |
| 1686 | |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1687 | |
| 1688 | def get_datetime_limit_condition(detail): |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1689 | return f""" |
| 1690 | and |
| 1691 | (timestamp(posting_date, posting_time) < timestamp('{detail.posting_date}', '{detail.posting_time}') |
| 1692 | or ( |
| 1693 | timestamp(posting_date, posting_time) = timestamp('{detail.posting_date}', '{detail.posting_time}') |
| 1694 | and creation < '{detail.creation}' |
| 1695 | ) |
| 1696 | )""" |
| 1697 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1698 | |
Ankush Menat | e7109c1 | 2021-08-26 16:40:45 +0530 | [diff] [blame] | 1699 | def validate_negative_qty_in_future_sle(args, allow_negative_stock=False): |
Ankush Menat | eb8b424 | 2022-02-12 13:08:28 +0530 | [diff] [blame] | 1700 | 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] | 1701 | return |
| 1702 | if not (args.actual_qty < 0 or args.voucher_type == "Stock Reconciliation"): |
| 1703 | return |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1704 | |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1705 | neg_sle = get_future_sle_with_negative_qty(args) |
Marica | d6078aa | 2022-06-17 15:13:13 +0530 | [diff] [blame] | 1706 | |
| 1707 | if is_negative_with_precision(neg_sle): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1708 | message = _( |
| 1709 | "{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction." |
| 1710 | ).format( |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1711 | abs(neg_sle[0]["qty_after_transaction"]), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1712 | frappe.get_desk_link("Item", args.item_code), |
| 1713 | frappe.get_desk_link("Warehouse", args.warehouse), |
| 1714 | neg_sle[0]["posting_date"], |
| 1715 | neg_sle[0]["posting_time"], |
| 1716 | frappe.get_desk_link(neg_sle[0]["voucher_type"], neg_sle[0]["voucher_no"]), |
| 1717 | ) |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1718 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1719 | frappe.throw(message, NegativeStockError, title=_("Insufficient Stock")) |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1720 | |
| 1721 | if not args.batch_no: |
| 1722 | return |
| 1723 | |
| 1724 | neg_batch_sle = get_future_sle_with_negative_batch_qty(args) |
Marica | d6078aa | 2022-06-17 15:13:13 +0530 | [diff] [blame] | 1725 | if is_negative_with_precision(neg_batch_sle, is_batch=True): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1726 | message = _( |
| 1727 | "{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction." |
| 1728 | ).format( |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1729 | abs(neg_batch_sle[0]["cumulative_total"]), |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1730 | frappe.get_desk_link("Batch", args.batch_no), |
| 1731 | frappe.get_desk_link("Warehouse", args.warehouse), |
| 1732 | neg_batch_sle[0]["posting_date"], |
| 1733 | neg_batch_sle[0]["posting_time"], |
| 1734 | frappe.get_desk_link(neg_batch_sle[0]["voucher_type"], neg_batch_sle[0]["voucher_no"]), |
| 1735 | ) |
mergify[bot] | 7a3d301 | 2022-03-08 10:42:30 +0530 | [diff] [blame] | 1736 | frappe.throw(message, NegativeStockError, title=_("Insufficient Stock for Batch")) |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1737 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1738 | |
Marica | d6078aa | 2022-06-17 15:13:13 +0530 | [diff] [blame] | 1739 | def is_negative_with_precision(neg_sle, is_batch=False): |
| 1740 | """ |
| 1741 | Returns whether system precision rounded qty is insufficient. |
| 1742 | E.g: -0.0003 in precision 3 (0.000) is sufficient for the user. |
| 1743 | """ |
| 1744 | |
| 1745 | if not neg_sle: |
| 1746 | return False |
| 1747 | |
| 1748 | field = "cumulative_total" if is_batch else "qty_after_transaction" |
| 1749 | precision = cint(frappe.db.get_default("float_precision")) or 2 |
| 1750 | qty_deficit = flt(neg_sle[0][field], precision) |
| 1751 | |
| 1752 | return qty_deficit < 0 and abs(qty_deficit) > 0.0001 |
| 1753 | |
| 1754 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1755 | def get_future_sle_with_negative_qty(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1756 | return frappe.db.sql( |
| 1757 | """ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1758 | select |
| 1759 | qty_after_transaction, posting_date, posting_time, |
| 1760 | voucher_type, voucher_no |
| 1761 | from `tabStock Ledger Entry` |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1762 | where |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1763 | item_code = %(item_code)s |
| 1764 | and warehouse = %(warehouse)s |
| 1765 | and voucher_no != %(voucher_no)s |
| 1766 | and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) |
| 1767 | and is_cancelled = 0 |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1768 | and qty_after_transaction < 0 |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 1769 | order by timestamp(posting_date, posting_time) asc |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1770 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1771 | """, |
| 1772 | args, |
| 1773 | as_dict=1, |
| 1774 | ) |
Ankush Menat | 6a014d1 | 2021-04-12 20:21:27 +0530 | [diff] [blame] | 1775 | |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1776 | |
| 1777 | def get_future_sle_with_negative_batch_qty(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1778 | return frappe.db.sql( |
| 1779 | """ |
Ankush Menat | 5eba575 | 2021-12-07 23:03:52 +0530 | [diff] [blame] | 1780 | with batch_ledger as ( |
| 1781 | select |
| 1782 | posting_date, posting_time, voucher_type, voucher_no, |
| 1783 | sum(actual_qty) over (order by posting_date, posting_time, creation) as cumulative_total |
| 1784 | from `tabStock Ledger Entry` |
| 1785 | where |
| 1786 | item_code = %(item_code)s |
| 1787 | and warehouse = %(warehouse)s |
| 1788 | and batch_no=%(batch_no)s |
| 1789 | and is_cancelled = 0 |
| 1790 | order by posting_date, posting_time, creation |
| 1791 | ) |
| 1792 | select * from batch_ledger |
| 1793 | where |
| 1794 | cumulative_total < 0.0 |
| 1795 | and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) |
| 1796 | limit 1 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1797 | """, |
| 1798 | args, |
| 1799 | as_dict=1, |
| 1800 | ) |
Ankush Menat | eb8b424 | 2022-02-12 13:08:28 +0530 | [diff] [blame] | 1801 | |
| 1802 | |
| 1803 | def is_negative_stock_allowed(*, item_code: Optional[str] = None) -> bool: |
| 1804 | if cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock", cache=True)): |
| 1805 | return True |
| 1806 | if item_code and cint(frappe.db.get_value("Item", item_code, "allow_negative_stock", cache=True)): |
| 1807 | return True |
| 1808 | return False |
Rohit Waghchaure | 683a47f | 2022-10-11 15:11:39 +0530 | [diff] [blame] | 1809 | |
| 1810 | |
| 1811 | def get_incoming_rate_for_inter_company_transfer(sle) -> float: |
| 1812 | """ |
| 1813 | For inter company transfer, incoming rate is the average of the outgoing rate |
| 1814 | """ |
| 1815 | rate = 0.0 |
| 1816 | |
| 1817 | field = "delivery_note_item" if sle.voucher_type == "Purchase Receipt" else "sales_invoice_item" |
| 1818 | |
| 1819 | doctype = "Delivery Note Item" if sle.voucher_type == "Purchase Receipt" else "Sales Invoice Item" |
| 1820 | |
| 1821 | reference_name = frappe.get_cached_value(sle.voucher_type + " Item", sle.voucher_detail_no, field) |
| 1822 | |
| 1823 | if reference_name: |
| 1824 | rate = frappe.get_cached_value( |
| 1825 | doctype, |
| 1826 | reference_name, |
| 1827 | "incoming_rate", |
| 1828 | ) |
| 1829 | |
| 1830 | return rate |
Rohit Waghchaure | b5a2ccf | 2023-05-04 15:38:35 +0530 | [diff] [blame] | 1831 | |
| 1832 | |
| 1833 | def is_internal_transfer(sle): |
| 1834 | data = frappe.get_cached_value( |
| 1835 | sle.voucher_type, |
| 1836 | sle.voucher_no, |
| 1837 | ["is_internal_supplier", "represents_company", "company"], |
| 1838 | as_dict=True, |
| 1839 | ) |
| 1840 | |
| 1841 | if data.is_internal_supplier and data.represents_company == data.company: |
| 1842 | return True |