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