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