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