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