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