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