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