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 |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 6 | |
| 7 | import frappe |
| 8 | from frappe import _ |
| 9 | from frappe.model.meta import get_field_precision |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 10 | from frappe.utils import cint, cstr, flt, get_link_to_form, getdate, now, nowdate |
Achilles Rasquinha | 361366e | 2018-02-14 17:08:59 +0530 | [diff] [blame] | 11 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 12 | import erpnext |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 13 | 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] | 14 | from erpnext.stock.utils import ( |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 15 | get_incoming_outgoing_rate_for_cancel, |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 16 | get_or_make_bin, |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 17 | get_valuation_method, |
| 18 | ) |
| 19 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 20 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 21 | class NegativeStockError(frappe.ValidationError): pass |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 22 | class SerialNoExistsInFutureTransaction(frappe.ValidationError): |
| 23 | pass |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 24 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 25 | _exceptions = frappe.local('stockledger_exceptions') |
Anand Doshi | 5b004ff | 2013-09-25 19:55:41 +0530 | [diff] [blame] | 26 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 27 | def make_sl_entries(sl_entries, allow_negative_stock=False, via_landed_cost_voucher=False): |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 28 | from erpnext.controllers.stock_controller import future_sle_exists |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 29 | if sl_entries: |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 30 | cancel = sl_entries[0].get("is_cancelled") |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 31 | if cancel: |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 32 | validate_cancellation(sl_entries) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 33 | 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] | 34 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 35 | args = get_args_for_future_sle(sl_entries[0]) |
| 36 | future_sle_exists(args, sl_entries) |
| 37 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 38 | for sle in sl_entries: |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 39 | if sle.serial_no: |
| 40 | validate_serial_no(sle) |
| 41 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 42 | if cancel: |
| 43 | sle['actual_qty'] = -flt(sle.get('actual_qty')) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 44 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 45 | if sle['actual_qty'] < 0 and not sle.get('outgoing_rate'): |
| 46 | sle['outgoing_rate'] = get_incoming_outgoing_rate_for_cancel(sle.item_code, |
| 47 | sle.voucher_type, sle.voucher_no, sle.voucher_detail_no) |
| 48 | sle['incoming_rate'] = 0.0 |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 49 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 50 | if sle['actual_qty'] > 0 and not sle.get('incoming_rate'): |
| 51 | sle['incoming_rate'] = get_incoming_outgoing_rate_for_cancel(sle.item_code, |
| 52 | sle.voucher_type, sle.voucher_no, sle.voucher_detail_no) |
| 53 | sle['outgoing_rate'] = 0.0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 54 | |
Nabin Hait | 5288bde | 2014-11-03 15:08:21 +0530 | [diff] [blame] | 55 | if sle.get("actual_qty") or sle.get("voucher_type")=="Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 56 | sle_doc = make_entry(sle, allow_negative_stock, via_landed_cost_voucher) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 57 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 58 | args = sle_doc.as_dict() |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 59 | |
| 60 | if sle.get("voucher_type") == "Stock Reconciliation": |
| 61 | # preserve previous_qty_after_transaction for qty reposting |
| 62 | args.previous_qty_after_transaction = sle.get("previous_qty_after_transaction") |
| 63 | |
Ankush Menat | cef84c2 | 2021-12-03 12:18:59 +0530 | [diff] [blame] | 64 | is_stock_item = frappe.get_cached_value('Item', args.get("item_code"), 'is_stock_item') |
| 65 | if is_stock_item: |
| 66 | bin_name = get_or_make_bin(args.get("item_code"), args.get("warehouse")) |
| 67 | update_bin_qty(bin_name, args) |
| 68 | repost_current_voucher(args, allow_negative_stock, via_landed_cost_voucher) |
| 69 | else: |
| 70 | frappe.msgprint(_("Item {0} ignored since it is not a stock item").format(args.get("item_code"))) |
| 71 | |
| 72 | def repost_current_voucher(args, allow_negative_stock=False, via_landed_cost_voucher=False): |
| 73 | if args.get("actual_qty") or args.get("voucher_type") == "Stock Reconciliation": |
| 74 | if not args.get("posting_date"): |
| 75 | args["posting_date"] = nowdate() |
| 76 | |
| 77 | if args.get("is_cancelled") and via_landed_cost_voucher: |
| 78 | return |
| 79 | |
| 80 | # Reposts only current voucher SL Entries |
| 81 | # Updates valuation rate, stock value, stock queue for current transaction |
| 82 | update_entries_after({ |
| 83 | "item_code": args.get('item_code'), |
| 84 | "warehouse": args.get('warehouse'), |
| 85 | "posting_date": args.get("posting_date"), |
| 86 | "posting_time": args.get("posting_time"), |
| 87 | "voucher_type": args.get("voucher_type"), |
| 88 | "voucher_no": args.get("voucher_no"), |
| 89 | "sle_id": args.get('name'), |
| 90 | "creation": args.get('creation') |
| 91 | }, allow_negative_stock=allow_negative_stock, via_landed_cost_voucher=via_landed_cost_voucher) |
| 92 | |
| 93 | # update qty in future sle and Validate negative qty |
| 94 | update_qty_in_future_sle(args, allow_negative_stock) |
| 95 | |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 96 | |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 97 | def get_args_for_future_sle(row): |
| 98 | return frappe._dict({ |
| 99 | 'voucher_type': row.get('voucher_type'), |
| 100 | 'voucher_no': row.get('voucher_no'), |
| 101 | 'posting_date': row.get('posting_date'), |
| 102 | 'posting_time': row.get('posting_time') |
| 103 | }) |
| 104 | |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 105 | def validate_serial_no(sle): |
| 106 | from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos |
| 107 | for sn in get_serial_nos(sle.serial_no): |
| 108 | args = copy.deepcopy(sle) |
| 109 | args.serial_no = sn |
| 110 | args.warehouse = '' |
| 111 | |
| 112 | vouchers = [] |
| 113 | for row in get_stock_ledger_entries(args, '>'): |
| 114 | voucher_type = frappe.bold(row.voucher_type) |
| 115 | voucher_no = frappe.bold(get_link_to_form(row.voucher_type, row.voucher_no)) |
| 116 | vouchers.append(f'{voucher_type} {voucher_no}') |
| 117 | |
| 118 | if vouchers: |
| 119 | serial_no = frappe.bold(sn) |
| 120 | msg = (f'''The serial no {serial_no} has been used in the future transactions so you need to cancel them first. |
| 121 | The list of the transactions are as below.''' + '<br><br><ul><li>') |
| 122 | |
| 123 | msg += '</li><li>'.join(vouchers) |
| 124 | msg += '</li></ul>' |
| 125 | |
| 126 | title = 'Cannot Submit' if not sle.get('is_cancelled') else 'Cannot Cancel' |
| 127 | frappe.throw(_(msg), title=_(title), exc=SerialNoExistsInFutureTransaction) |
| 128 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 129 | def validate_cancellation(args): |
| 130 | if args[0].get("is_cancelled"): |
| 131 | repost_entry = frappe.db.get_value("Repost Item Valuation", { |
| 132 | 'voucher_type': args[0].voucher_type, |
| 133 | 'voucher_no': args[0].voucher_no, |
| 134 | 'docstatus': 1 |
| 135 | }, ['name', 'status'], as_dict=1) |
| 136 | |
| 137 | if repost_entry: |
| 138 | if repost_entry.status == 'In Progress': |
| 139 | frappe.throw(_("Cannot cancel the transaction. Reposting of item valuation on submission is not completed yet.")) |
| 140 | if repost_entry.status == 'Queued': |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 141 | doc = frappe.get_doc("Repost Item Valuation", repost_entry.name) |
Ankush Menat | aa024fc | 2021-11-18 12:51:26 +0530 | [diff] [blame] | 142 | doc.flags.ignore_permissions = True |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 143 | doc.cancel() |
| 144 | doc.delete() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 145 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 146 | def set_as_cancel(voucher_type, voucher_no): |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 147 | frappe.db.sql("""update `tabStock Ledger Entry` set is_cancelled=1, |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 148 | modified=%s, modified_by=%s |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 149 | where voucher_type=%s and voucher_no=%s and is_cancelled = 0""", |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 150 | (now(), frappe.session.user, voucher_type, voucher_no)) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 151 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 152 | 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] | 153 | args["doctype"] = "Stock Ledger Entry" |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 154 | sle = frappe.get_doc(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 155 | sle.flags.ignore_permissions = 1 |
Nabin Hait | 4ccd8d3 | 2015-01-23 12:18:01 +0530 | [diff] [blame] | 156 | sle.allow_negative_stock=allow_negative_stock |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 157 | sle.via_landed_cost_voucher = via_landed_cost_voucher |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 158 | sle.submit() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 159 | return sle |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 160 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 161 | def repost_future_sle(args=None, voucher_type=None, voucher_no=None, allow_negative_stock=None, via_landed_cost_voucher=False, doc=None): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 162 | if not args and voucher_type and voucher_no: |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 163 | args = get_items_to_be_repost(voucher_type, voucher_no, doc) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 164 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 165 | distinct_item_warehouses = get_distinct_item_warehouse(args, doc) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 166 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 167 | i = get_current_index(doc) or 0 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 168 | while i < len(args): |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 169 | validate_item_warehouse(args[i]) |
| 170 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 171 | obj = update_entries_after({ |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 172 | 'item_code': args[i].get('item_code'), |
| 173 | 'warehouse': args[i].get('warehouse'), |
| 174 | 'posting_date': args[i].get('posting_date'), |
| 175 | 'posting_time': args[i].get('posting_time'), |
| 176 | 'creation': args[i].get('creation'), |
| 177 | 'distinct_item_warehouses': distinct_item_warehouses |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 178 | }, allow_negative_stock=allow_negative_stock, via_landed_cost_voucher=via_landed_cost_voucher) |
| 179 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 180 | distinct_item_warehouses[(args[i].get('item_code'), args[i].get('warehouse'))].reposting_status = True |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 181 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 182 | if obj.new_items_found: |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 183 | for item_wh, data in distinct_item_warehouses.items(): |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 184 | if ('args_idx' not in data and not data.reposting_status) or (data.sle_changed and data.reposting_status): |
| 185 | data.args_idx = len(args) |
| 186 | args.append(data.sle) |
| 187 | elif data.sle_changed and not data.reposting_status: |
| 188 | args[data.args_idx] = data.sle |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 189 | |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 190 | data.sle_changed = False |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 191 | i += 1 |
| 192 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 193 | if doc and i % 2 == 0: |
| 194 | update_args_in_repost_item_valuation(doc, i, args, distinct_item_warehouses) |
| 195 | |
| 196 | if doc and args: |
| 197 | update_args_in_repost_item_valuation(doc, i, args, distinct_item_warehouses) |
| 198 | |
| 199 | def validate_item_warehouse(args): |
| 200 | for field in ['item_code', 'warehouse', 'posting_date', 'posting_time']: |
| 201 | if not args.get(field): |
| 202 | validation_msg = f'The field {frappe.unscrub(args.get(field))} is required for the reposting' |
| 203 | frappe.throw(_(validation_msg)) |
| 204 | |
| 205 | def update_args_in_repost_item_valuation(doc, index, args, distinct_item_warehouses): |
| 206 | frappe.db.set_value(doc.doctype, doc.name, { |
| 207 | 'items_to_be_repost': json.dumps(args, default=str), |
| 208 | 'distinct_item_and_warehouse': json.dumps({str(k): v for k,v in distinct_item_warehouses.items()}, default=str), |
| 209 | 'current_index': index |
| 210 | }) |
| 211 | |
| 212 | frappe.db.commit() |
| 213 | |
| 214 | frappe.publish_realtime('item_reposting_progress', { |
| 215 | 'name': doc.name, |
| 216 | 'items_to_be_repost': json.dumps(args, default=str), |
| 217 | 'current_index': index |
| 218 | }) |
| 219 | |
| 220 | def get_items_to_be_repost(voucher_type, voucher_no, doc=None): |
| 221 | if doc and doc.items_to_be_repost: |
| 222 | return json.loads(doc.items_to_be_repost) or [] |
| 223 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 224 | return frappe.db.get_all("Stock Ledger Entry", |
| 225 | filters={"voucher_type": voucher_type, "voucher_no": voucher_no}, |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 226 | fields=["item_code", "warehouse", "posting_date", "posting_time", "creation"], |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 227 | order_by="creation asc", |
| 228 | group_by="item_code, warehouse" |
| 229 | ) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 230 | |
rohitwaghchaure | 31fe5f5 | 2021-08-02 11:01:30 +0530 | [diff] [blame] | 231 | def get_distinct_item_warehouse(args=None, doc=None): |
| 232 | distinct_item_warehouses = {} |
| 233 | if doc and doc.distinct_item_and_warehouse: |
| 234 | distinct_item_warehouses = json.loads(doc.distinct_item_and_warehouse) |
| 235 | distinct_item_warehouses = {frappe.safe_eval(k): frappe._dict(v) for k, v in distinct_item_warehouses.items()} |
| 236 | else: |
| 237 | for i, d in enumerate(args): |
| 238 | distinct_item_warehouses.setdefault((d.item_code, d.warehouse), frappe._dict({ |
| 239 | "reposting_status": False, |
| 240 | "sle": d, |
| 241 | "args_idx": i |
| 242 | })) |
| 243 | |
| 244 | return distinct_item_warehouses |
| 245 | |
| 246 | def get_current_index(doc=None): |
| 247 | if doc and doc.current_index: |
| 248 | return doc.current_index |
| 249 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 250 | class update_entries_after(object): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 251 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 252 | update valution rate and qty after transaction |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 253 | from the current time-bucket onwards |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 254 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 255 | :param args: args as dict |
| 256 | |
| 257 | args = { |
| 258 | "item_code": "ABC", |
| 259 | "warehouse": "XYZ", |
| 260 | "posting_date": "2012-12-12", |
| 261 | "posting_time": "12:00" |
| 262 | } |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 263 | """ |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 264 | def __init__(self, args, allow_zero_rate=False, allow_negative_stock=None, via_landed_cost_voucher=False, verbose=1): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 265 | self.exceptions = {} |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 266 | self.verbose = verbose |
| 267 | self.allow_zero_rate = allow_zero_rate |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 268 | self.via_landed_cost_voucher = via_landed_cost_voucher |
Ankush Menat | 7bafa11 | 2021-10-12 20:39:10 +0530 | [diff] [blame] | 269 | self.allow_negative_stock = allow_negative_stock \ |
| 270 | or cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock")) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 271 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 272 | self.args = frappe._dict(args) |
| 273 | self.item_code = args.get("item_code") |
| 274 | if self.args.sle_id: |
| 275 | self.args['name'] = self.args.sle_id |
Nabin Hait | d46b236 | 2021-02-23 16:38:52 +0530 | [diff] [blame] | 276 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 277 | self.company = frappe.get_cached_value("Warehouse", self.args.warehouse, "company") |
| 278 | self.get_precision() |
| 279 | self.valuation_method = get_valuation_method(self.item_code) |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 280 | |
| 281 | self.new_items_found = False |
| 282 | self.distinct_item_warehouses = args.get("distinct_item_warehouses", frappe._dict()) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 283 | |
| 284 | self.data = frappe._dict() |
| 285 | self.initialize_previous_data(self.args) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 286 | self.build() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 287 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 288 | def get_precision(self): |
| 289 | company_base_currency = frappe.get_cached_value('Company', self.company, "default_currency") |
| 290 | self.precision = get_field_precision(frappe.get_meta("Stock Ledger Entry").get_field("stock_value"), |
| 291 | currency=company_base_currency) |
| 292 | |
| 293 | def initialize_previous_data(self, args): |
| 294 | """ |
| 295 | Get previous sl entries for current item for each related warehouse |
| 296 | and assigns into self.data dict |
| 297 | |
| 298 | :Data Structure: |
| 299 | |
| 300 | self.data = { |
| 301 | warehouse1: { |
| 302 | 'previus_sle': {}, |
| 303 | 'qty_after_transaction': 10, |
| 304 | 'valuation_rate': 100, |
| 305 | 'stock_value': 1000, |
| 306 | 'prev_stock_value': 1000, |
| 307 | 'stock_queue': '[[10, 100]]', |
| 308 | 'stock_value_difference': 1000 |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | """ |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 313 | self.data.setdefault(args.warehouse, frappe._dict()) |
| 314 | warehouse_dict = self.data[args.warehouse] |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 315 | previous_sle = get_previous_sle_of_current_voucher(args) |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 316 | warehouse_dict.previous_sle = previous_sle |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 317 | |
Ankush Menat | c1d986a | 2021-08-31 19:43:42 +0530 | [diff] [blame] | 318 | for key in ("qty_after_transaction", "valuation_rate", "stock_value"): |
| 319 | setattr(warehouse_dict, key, flt(previous_sle.get(key))) |
| 320 | |
| 321 | warehouse_dict.update({ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 322 | "prev_stock_value": previous_sle.stock_value or 0.0, |
| 323 | "stock_queue": json.loads(previous_sle.stock_queue or "[]"), |
| 324 | "stock_value_difference": 0.0 |
| 325 | }) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 326 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 327 | def build(self): |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 328 | from erpnext.controllers.stock_controller import future_sle_exists |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 329 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 330 | if self.args.get("sle_id"): |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 331 | self.process_sle_against_current_timestamp() |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 332 | if not future_sle_exists(self.args): |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 333 | self.update_bin() |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 334 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 335 | entries_to_fix = self.get_future_entries_to_fix() |
| 336 | |
| 337 | i = 0 |
| 338 | while i < len(entries_to_fix): |
| 339 | sle = entries_to_fix[i] |
| 340 | i += 1 |
| 341 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 342 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 343 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 344 | if sle.dependant_sle_voucher_detail_no: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 345 | 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] | 346 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 347 | self.update_bin() |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 348 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 349 | if self.exceptions: |
| 350 | self.raise_exceptions() |
| 351 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 352 | def process_sle_against_current_timestamp(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 353 | sl_entries = self.get_sle_against_current_voucher() |
| 354 | for sle in sl_entries: |
| 355 | self.process_sle(sle) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 356 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 357 | def get_sle_against_current_voucher(self): |
Nabin Hait | f2be080 | 2021-02-15 19:27:49 +0530 | [diff] [blame] | 358 | self.args['time_format'] = '%H:%i:%s' |
| 359 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 360 | return frappe.db.sql(""" |
| 361 | select |
| 362 | *, timestamp(posting_date, posting_time) as "timestamp" |
| 363 | from |
| 364 | `tabStock Ledger Entry` |
| 365 | where |
| 366 | item_code = %(item_code)s |
| 367 | and warehouse = %(warehouse)s |
rohitwaghchaure | fe4540d | 2021-08-26 12:52:36 +0530 | [diff] [blame] | 368 | and is_cancelled = 0 |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 369 | and timestamp(posting_date, time_format(posting_time, %(time_format)s)) = timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s)) |
| 370 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 371 | order by |
| 372 | creation ASC |
| 373 | for update |
| 374 | """, self.args, as_dict=1) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 375 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 376 | def get_future_entries_to_fix(self): |
| 377 | # includes current entry! |
| 378 | args = self.data[self.args.warehouse].previous_sle \ |
| 379 | or frappe._dict({"item_code": self.item_code, "warehouse": self.args.warehouse}) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 380 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 381 | return list(self.get_sle_after_datetime(args)) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 382 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 383 | def get_dependent_entries_to_fix(self, entries_to_fix, sle): |
| 384 | dependant_sle = get_sle_by_voucher_detail_no(sle.dependant_sle_voucher_detail_no, |
| 385 | excluded_sle=sle.name) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 386 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 387 | if not dependant_sle: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 388 | return entries_to_fix |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 389 | elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse == self.args.warehouse: |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 390 | return entries_to_fix |
| 391 | elif dependant_sle.item_code != self.item_code: |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 392 | self.update_distinct_item_warehouses(dependant_sle) |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 393 | return entries_to_fix |
| 394 | elif dependant_sle.item_code == self.item_code and dependant_sle.warehouse in self.data: |
| 395 | return entries_to_fix |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 396 | else: |
| 397 | return self.append_future_sle_for_dependant(dependant_sle, entries_to_fix) |
| 398 | |
| 399 | def update_distinct_item_warehouses(self, dependant_sle): |
| 400 | key = (dependant_sle.item_code, dependant_sle.warehouse) |
| 401 | val = frappe._dict({ |
| 402 | "sle": dependant_sle |
| 403 | }) |
| 404 | if key not in self.distinct_item_warehouses: |
| 405 | self.distinct_item_warehouses[key] = val |
| 406 | self.new_items_found = True |
| 407 | else: |
| 408 | existing_sle_posting_date = self.distinct_item_warehouses[key].get("sle", {}).get("posting_date") |
| 409 | if getdate(dependant_sle.posting_date) < getdate(existing_sle_posting_date): |
| 410 | val.sle_changed = True |
| 411 | self.distinct_item_warehouses[key] = val |
| 412 | self.new_items_found = True |
| 413 | |
| 414 | def append_future_sle_for_dependant(self, dependant_sle, entries_to_fix): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 415 | self.initialize_previous_data(dependant_sle) |
| 416 | |
| 417 | args = self.data[dependant_sle.warehouse].previous_sle \ |
| 418 | or frappe._dict({"item_code": self.item_code, "warehouse": dependant_sle.warehouse}) |
| 419 | future_sle_for_dependant = list(self.get_sle_after_datetime(args)) |
| 420 | |
| 421 | entries_to_fix.extend(future_sle_for_dependant) |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 422 | return sorted(entries_to_fix, key=lambda k: k['timestamp']) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 423 | |
| 424 | def process_sle(self, sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 425 | # previous sle data for this warehouse |
| 426 | self.wh_data = self.data[sle.warehouse] |
| 427 | |
Anand Doshi | 0dc79f4 | 2015-04-06 12:59:34 +0530 | [diff] [blame] | 428 | 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] | 429 | # validate negative stock for serialized items, fifo valuation |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 430 | # or when negative stock is not allowed for moving average |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 431 | if not self.validate_negative_stock(sle): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 432 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 433 | return |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 434 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 435 | # Get dynamic incoming/outgoing rate |
rohitwaghchaure | e6a1ad8 | 2021-09-15 20:42:47 +0530 | [diff] [blame] | 436 | if not self.args.get("sle_id"): |
| 437 | self.get_dynamic_incoming_outgoing_rate(sle) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 438 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 439 | if sle.serial_no: |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 440 | self.get_serialized_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 441 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 442 | if sle.voucher_type == "Stock Reconciliation": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 443 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 444 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 445 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt(self.wh_data.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 446 | else: |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 447 | if sle.voucher_type=="Stock Reconciliation" and not sle.batch_no: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 448 | # assert |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 449 | self.wh_data.valuation_rate = sle.valuation_rate |
| 450 | self.wh_data.qty_after_transaction = sle.qty_after_transaction |
| 451 | self.wh_data.stock_queue = [[self.wh_data.qty_after_transaction, self.wh_data.valuation_rate]] |
| 452 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt(self.wh_data.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 453 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 454 | if self.valuation_method == "Moving Average": |
| 455 | self.get_moving_average_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 456 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
| 457 | self.wh_data.stock_value = flt(self.wh_data.qty_after_transaction) * flt(self.wh_data.valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 458 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 459 | self.get_fifo_values(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 460 | self.wh_data.qty_after_transaction += flt(sle.actual_qty) |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 461 | self.wh_data.stock_value = sum(flt(batch[0]) * flt(batch[1]) for batch in self.wh_data.stock_queue) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 462 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 463 | # rounding as per precision |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 464 | self.wh_data.stock_value = flt(self.wh_data.stock_value, self.precision) |
| 465 | stock_value_difference = self.wh_data.stock_value - self.wh_data.prev_stock_value |
| 466 | self.wh_data.prev_stock_value = self.wh_data.stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 467 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 468 | # update current sle |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 469 | sle.qty_after_transaction = self.wh_data.qty_after_transaction |
| 470 | sle.valuation_rate = self.wh_data.valuation_rate |
| 471 | sle.stock_value = self.wh_data.stock_value |
| 472 | sle.stock_queue = json.dumps(self.wh_data.stock_queue) |
Rushabh Mehta | 2e0e711 | 2015-02-18 11:38:05 +0530 | [diff] [blame] | 473 | sle.stock_value_difference = stock_value_difference |
Rushabh Mehta | 8bb6e53 | 2015-02-18 20:22:59 +0530 | [diff] [blame] | 474 | sle.doctype="Stock Ledger Entry" |
| 475 | frappe.get_doc(sle).db_update() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 476 | |
rohitwaghchaure | e6a1ad8 | 2021-09-15 20:42:47 +0530 | [diff] [blame] | 477 | if not self.args.get("sle_id"): |
| 478 | self.update_outgoing_rate_on_transaction(sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 479 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 480 | def validate_negative_stock(self, sle): |
| 481 | """ |
| 482 | validate negative stock for entries current datetime onwards |
| 483 | will not consider cancelled entries |
| 484 | """ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 485 | diff = self.wh_data.qty_after_transaction + flt(sle.actual_qty) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 486 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 487 | if diff < 0 and abs(diff) > 0.0001: |
| 488 | # negative stock! |
| 489 | exc = sle.copy().update({"diff": diff}) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 490 | self.exceptions.setdefault(sle.warehouse, []).append(exc) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 491 | return False |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 492 | else: |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 493 | return True |
| 494 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 495 | def get_dynamic_incoming_outgoing_rate(self, sle): |
| 496 | # Get updated incoming/outgoing rate from transaction |
| 497 | if sle.recalculate_rate: |
| 498 | rate = self.get_incoming_outgoing_rate_from_transaction(sle) |
| 499 | |
| 500 | if flt(sle.actual_qty) >= 0: |
| 501 | sle.incoming_rate = rate |
| 502 | else: |
| 503 | sle.outgoing_rate = rate |
| 504 | |
| 505 | def get_incoming_outgoing_rate_from_transaction(self, sle): |
| 506 | rate = 0 |
| 507 | # Material Transfer, Repack, Manufacturing |
| 508 | if sle.voucher_type == "Stock Entry": |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 509 | self.recalculate_amounts_in_stock_entry(sle.voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 510 | rate = frappe.db.get_value("Stock Entry Detail", sle.voucher_detail_no, "valuation_rate") |
| 511 | # Sales and Purchase Return |
| 512 | elif sle.voucher_type in ("Purchase Receipt", "Purchase Invoice", "Delivery Note", "Sales Invoice"): |
| 513 | 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] | 514 | from erpnext.controllers.sales_and_purchase_return import ( |
| 515 | get_rate_for_return, # don't move this import to top |
| 516 | ) |
rohitwaghchaure | ce6c3b5 | 2021-04-13 20:55:52 +0530 | [diff] [blame] | 517 | rate = get_rate_for_return(sle.voucher_type, sle.voucher_no, sle.item_code, |
| 518 | voucher_detail_no=sle.voucher_detail_no, sle = sle) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 519 | else: |
| 520 | if sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 521 | rate_field = "valuation_rate" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 522 | else: |
| 523 | rate_field = "incoming_rate" |
| 524 | |
| 525 | # check in item table |
| 526 | item_code, incoming_rate = frappe.db.get_value(sle.voucher_type + " Item", |
| 527 | sle.voucher_detail_no, ["item_code", rate_field]) |
| 528 | |
| 529 | if item_code == sle.item_code: |
| 530 | rate = incoming_rate |
| 531 | else: |
| 532 | if sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 533 | ref_doctype = "Packed Item" |
| 534 | else: |
| 535 | ref_doctype = "Purchase Receipt Item Supplied" |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 536 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 537 | rate = frappe.db.get_value(ref_doctype, {"parent_detail_docname": sle.voucher_detail_no, |
| 538 | "item_code": sle.item_code}, rate_field) |
| 539 | |
| 540 | return rate |
| 541 | |
| 542 | def update_outgoing_rate_on_transaction(self, sle): |
| 543 | """ |
| 544 | Update outgoing rate in Stock Entry, Delivery Note, Sales Invoice and Sales Return |
| 545 | In case of Stock Entry, also calculate FG Item rate and total incoming/outgoing amount |
| 546 | """ |
| 547 | if sle.actual_qty and sle.voucher_detail_no: |
| 548 | outgoing_rate = abs(flt(sle.stock_value_difference)) / abs(sle.actual_qty) |
| 549 | |
| 550 | if flt(sle.actual_qty) < 0 and sle.voucher_type == "Stock Entry": |
| 551 | self.update_rate_on_stock_entry(sle, outgoing_rate) |
| 552 | elif sle.voucher_type in ("Delivery Note", "Sales Invoice"): |
| 553 | self.update_rate_on_delivery_and_sales_return(sle, outgoing_rate) |
| 554 | elif flt(sle.actual_qty) < 0 and sle.voucher_type in ("Purchase Receipt", "Purchase Invoice"): |
| 555 | self.update_rate_on_purchase_receipt(sle, outgoing_rate) |
| 556 | |
| 557 | def update_rate_on_stock_entry(self, sle, outgoing_rate): |
| 558 | frappe.db.set_value("Stock Entry Detail", sle.voucher_detail_no, "basic_rate", outgoing_rate) |
| 559 | |
| 560 | # Update outgoing item's rate, recalculate FG Item's rate and total incoming/outgoing amount |
Nabin Hait | 97bce3a | 2021-07-12 13:24:43 +0530 | [diff] [blame] | 561 | if not sle.dependant_sle_voucher_detail_no: |
| 562 | self.recalculate_amounts_in_stock_entry(sle.voucher_no) |
| 563 | |
| 564 | def recalculate_amounts_in_stock_entry(self, voucher_no): |
| 565 | stock_entry = frappe.get_doc("Stock Entry", voucher_no, for_update=True) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 566 | stock_entry.calculate_rate_and_amount(reset_outgoing_rate=False, raise_error_if_no_rate=False) |
| 567 | stock_entry.db_update() |
| 568 | for d in stock_entry.items: |
| 569 | d.db_update() |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 570 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 571 | def update_rate_on_delivery_and_sales_return(self, sle, outgoing_rate): |
| 572 | # Update item's incoming rate on transaction |
| 573 | item_code = frappe.db.get_value(sle.voucher_type + " Item", sle.voucher_detail_no, "item_code") |
| 574 | if item_code == sle.item_code: |
| 575 | frappe.db.set_value(sle.voucher_type + " Item", sle.voucher_detail_no, "incoming_rate", outgoing_rate) |
| 576 | else: |
| 577 | # packed item |
| 578 | frappe.db.set_value("Packed Item", |
| 579 | {"parent_detail_docname": sle.voucher_detail_no, "item_code": sle.item_code}, |
| 580 | "incoming_rate", outgoing_rate) |
| 581 | |
| 582 | def update_rate_on_purchase_receipt(self, sle, outgoing_rate): |
| 583 | if frappe.db.exists(sle.voucher_type + " Item", sle.voucher_detail_no): |
| 584 | frappe.db.set_value(sle.voucher_type + " Item", sle.voucher_detail_no, "base_net_rate", outgoing_rate) |
| 585 | else: |
| 586 | frappe.db.set_value("Purchase Receipt Item Supplied", sle.voucher_detail_no, "rate", outgoing_rate) |
| 587 | |
| 588 | # Recalculate subcontracted item's rate in case of subcontracted purchase receipt/invoice |
Rohit Waghchaure | 4d81d45 | 2021-06-15 10:21:44 +0530 | [diff] [blame] | 589 | if frappe.get_cached_value(sle.voucher_type, sle.voucher_no, "is_subcontracted") == 'Yes': |
Rohit Waghchaure | e5fb239 | 2021-06-18 20:37:42 +0530 | [diff] [blame] | 590 | doc = frappe.get_doc(sle.voucher_type, sle.voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 591 | doc.update_valuation_rate(reset_outgoing_rate=False) |
| 592 | for d in (doc.items + doc.supplied_items): |
| 593 | d.db_update() |
| 594 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 595 | def get_serialized_values(self, sle): |
| 596 | incoming_rate = flt(sle.incoming_rate) |
| 597 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 598 | serial_nos = cstr(sle.serial_no).split("\n") |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 599 | |
| 600 | if incoming_rate < 0: |
| 601 | # wrong incoming rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 602 | incoming_rate = self.wh_data.valuation_rate |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 603 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 604 | stock_value_change = 0 |
| 605 | if incoming_rate: |
| 606 | stock_value_change = actual_qty * incoming_rate |
| 607 | elif actual_qty < 0: |
| 608 | # In case of delivery/stock issue, get average purchase rate |
| 609 | # of serial nos of current entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 610 | if not sle.is_cancelled: |
| 611 | outgoing_value = self.get_incoming_value_for_serial_nos(sle, serial_nos) |
| 612 | stock_value_change = -1 * outgoing_value |
| 613 | else: |
| 614 | stock_value_change = actual_qty * sle.outgoing_rate |
Rushabh Mehta | 2a21bc9 | 2015-02-25 15:08:42 +0530 | [diff] [blame] | 615 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 616 | new_stock_qty = self.wh_data.qty_after_transaction + actual_qty |
rohitwaghchaure | 0fe6ced | 2018-07-27 10:33:30 +0530 | [diff] [blame] | 617 | |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 618 | if new_stock_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 619 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + stock_value_change |
rohitwaghchaure | 0fe6ced | 2018-07-27 10:33:30 +0530 | [diff] [blame] | 620 | if new_stock_value >= 0: |
Nabin Hait | 2620bf4 | 2016-02-29 11:30:27 +0530 | [diff] [blame] | 621 | # calculate new valuation rate only if stock value is positive |
| 622 | # else it remains the same as that of previous entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 623 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 624 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 625 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 626 | allow_zero_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 627 | if not allow_zero_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 628 | self.wh_data.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 629 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
Ankush Menat | a0727b2 | 2021-11-01 13:17:40 +0530 | [diff] [blame] | 630 | currency=erpnext.get_company_currency(sle.company), company=sle.company) |
rohitwaghchaure | b1ac979 | 2017-12-01 16:09:02 +0530 | [diff] [blame] | 631 | |
Nabin Hait | 328c4f9 | 2020-01-02 19:00:32 +0530 | [diff] [blame] | 632 | def get_incoming_value_for_serial_nos(self, sle, serial_nos): |
| 633 | # get rate from serial nos within same company |
| 634 | all_serial_nos = frappe.get_all("Serial No", |
| 635 | fields=["purchase_rate", "name", "company"], |
| 636 | filters = {'name': ('in', serial_nos)}) |
| 637 | |
Ankush Menat | 9891780 | 2021-06-11 18:40:22 +0530 | [diff] [blame] | 638 | 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] | 639 | |
| 640 | # Get rate for serial nos which has been transferred to other company |
| 641 | invalid_serial_nos = [d.name for d in all_serial_nos if d.company!=sle.company] |
| 642 | for serial_no in invalid_serial_nos: |
| 643 | incoming_rate = frappe.db.sql(""" |
| 644 | select incoming_rate |
| 645 | from `tabStock Ledger Entry` |
| 646 | where |
| 647 | company = %s |
| 648 | and actual_qty > 0 |
| 649 | and (serial_no = %s |
| 650 | or serial_no like %s |
| 651 | or serial_no like %s |
| 652 | or serial_no like %s |
| 653 | ) |
| 654 | order by posting_date desc |
| 655 | limit 1 |
| 656 | """, (sle.company, serial_no, serial_no+'\n%', '%\n'+serial_no, '%\n'+serial_no+'\n%')) |
| 657 | |
| 658 | incoming_values += flt(incoming_rate[0][0]) if incoming_rate else 0 |
| 659 | |
| 660 | return incoming_values |
| 661 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 662 | def get_moving_average_values(self, sle): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 663 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 664 | new_stock_qty = flt(self.wh_data.qty_after_transaction) + actual_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 665 | if new_stock_qty >= 0: |
| 666 | if actual_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 667 | if flt(self.wh_data.qty_after_transaction) <= 0: |
| 668 | self.wh_data.valuation_rate = sle.incoming_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 669 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 670 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + \ |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 671 | (actual_qty * sle.incoming_rate) |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 672 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 673 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 674 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 675 | elif sle.outgoing_rate: |
| 676 | if new_stock_qty: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 677 | new_stock_value = (self.wh_data.qty_after_transaction * self.wh_data.valuation_rate) + \ |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 678 | (actual_qty * sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 679 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 680 | self.wh_data.valuation_rate = new_stock_value / new_stock_qty |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 681 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 682 | self.wh_data.valuation_rate = sle.outgoing_rate |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 683 | else: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 684 | if flt(self.wh_data.qty_after_transaction) >= 0 and sle.outgoing_rate: |
| 685 | self.wh_data.valuation_rate = sle.outgoing_rate |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 686 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 687 | if not self.wh_data.valuation_rate and actual_qty > 0: |
| 688 | self.wh_data.valuation_rate = sle.incoming_rate |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 689 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 690 | # 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] | 691 | # allow zero valuration rate flag set |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 692 | if not self.wh_data.valuation_rate and sle.voucher_detail_no: |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 693 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 694 | if not allow_zero_valuation_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 695 | self.wh_data.valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 696 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
Ankush Menat | a0727b2 | 2021-11-01 13:17:40 +0530 | [diff] [blame] | 697 | currency=erpnext.get_company_currency(sle.company), company=sle.company) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 698 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 699 | def get_fifo_values(self, sle): |
| 700 | incoming_rate = flt(sle.incoming_rate) |
| 701 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 702 | outgoing_rate = flt(sle.outgoing_rate) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 703 | |
| 704 | if actual_qty > 0: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 705 | if not self.wh_data.stock_queue: |
| 706 | self.wh_data.stock_queue.append([0, 0]) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 707 | |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 708 | # last row has the same rate, just updated the qty |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 709 | if self.wh_data.stock_queue[-1][1]==incoming_rate: |
| 710 | self.wh_data.stock_queue[-1][0] += actual_qty |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 711 | else: |
Frappe PR Bot | d4d5e27 | 2021-09-15 17:07:58 +0530 | [diff] [blame] | 712 | # Item has a positive balance qty, add new entry |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 713 | if self.wh_data.stock_queue[-1][0] > 0: |
| 714 | self.wh_data.stock_queue.append([actual_qty, incoming_rate]) |
Frappe PR Bot | d4d5e27 | 2021-09-15 17:07:58 +0530 | [diff] [blame] | 715 | else: # negative balance qty |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 716 | qty = self.wh_data.stock_queue[-1][0] + actual_qty |
Frappe PR Bot | d4d5e27 | 2021-09-15 17:07:58 +0530 | [diff] [blame] | 717 | if qty > 0: # new balance qty is positive |
| 718 | self.wh_data.stock_queue[-1] = [qty, incoming_rate] |
| 719 | else: # new balance qty is still negative, maintain same rate |
| 720 | self.wh_data.stock_queue[-1][0] = qty |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 721 | else: |
| 722 | qty_to_pop = abs(actual_qty) |
| 723 | while qty_to_pop: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 724 | if not self.wh_data.stock_queue: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 725 | # Get valuation rate from last sle if exists or from valuation rate field in item master |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 726 | allow_zero_valuation_rate = self.check_if_allow_zero_valuation_rate(sle.voucher_type, sle.voucher_detail_no) |
| 727 | if not allow_zero_valuation_rate: |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 728 | _rate = get_valuation_rate(sle.item_code, sle.warehouse, |
| 729 | sle.voucher_type, sle.voucher_no, self.allow_zero_rate, |
Ankush Menat | a0727b2 | 2021-11-01 13:17:40 +0530 | [diff] [blame] | 730 | currency=erpnext.get_company_currency(sle.company), company=sle.company) |
Nabin Hait | 0a6aaf4 | 2017-02-07 01:23:26 +0530 | [diff] [blame] | 731 | else: |
| 732 | _rate = 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 733 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 734 | self.wh_data.stock_queue.append([0, _rate]) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 735 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 736 | index = None |
| 737 | if outgoing_rate > 0: |
| 738 | # Find the entry where rate matched with outgoing rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 739 | for i, v in enumerate(self.wh_data.stock_queue): |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 740 | if v[1] == outgoing_rate: |
| 741 | index = i |
| 742 | break |
Rushabh Mehta | 14a908b | 2015-10-15 12:28:20 +0530 | [diff] [blame] | 743 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 744 | # If no entry found with outgoing rate, collapse stack |
Ankush Menat | 4dcac4a | 2021-05-21 13:12:30 +0530 | [diff] [blame] | 745 | if index is None: # nosemgrep |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 746 | new_stock_value = sum(d[0]*d[1] for d in self.wh_data.stock_queue) - qty_to_pop*outgoing_rate |
| 747 | new_stock_qty = sum(d[0] for d in self.wh_data.stock_queue) - qty_to_pop |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 748 | self.wh_data.stock_queue = [[new_stock_qty, new_stock_value/new_stock_qty if new_stock_qty > 0 else outgoing_rate]] |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 749 | break |
| 750 | else: |
| 751 | index = 0 |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 752 | |
Nabin Hait | ada485f | 2015-07-17 15:09:56 +0530 | [diff] [blame] | 753 | # select first batch or the batch with same rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 754 | batch = self.wh_data.stock_queue[index] |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 755 | if qty_to_pop >= batch[0]: |
| 756 | # consume current batch |
Ankush Menat | 6a014d1 | 2021-04-12 20:21:27 +0530 | [diff] [blame] | 757 | qty_to_pop = _round_off_if_near_zero(qty_to_pop - batch[0]) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 758 | self.wh_data.stock_queue.pop(index) |
| 759 | if not self.wh_data.stock_queue and qty_to_pop: |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 760 | # stock finished, qty still remains to be withdrawn |
| 761 | # negative stock, keep in as a negative batch |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 762 | self.wh_data.stock_queue.append([-qty_to_pop, outgoing_rate or batch[1]]) |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 763 | break |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 764 | |
Nabin Hait | 8142cd2 | 2015-08-05 18:57:26 +0530 | [diff] [blame] | 765 | else: |
| 766 | # qty found in current batch |
| 767 | # consume it and exit |
| 768 | batch[0] = batch[0] - qty_to_pop |
| 769 | qty_to_pop = 0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 770 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 771 | stock_value = _round_off_if_near_zero(sum(flt(batch[0]) * flt(batch[1]) for batch in self.wh_data.stock_queue)) |
| 772 | stock_qty = _round_off_if_near_zero(sum(flt(batch[0]) for batch in self.wh_data.stock_queue)) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 773 | |
Nabin Hait | 6dfc78b | 2016-06-24 12:28:55 +0530 | [diff] [blame] | 774 | if stock_qty: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 775 | self.wh_data.valuation_rate = stock_value / flt(stock_qty) |
Rushabh Mehta | cca33b2 | 2016-07-08 18:24:46 +0530 | [diff] [blame] | 776 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 777 | if not self.wh_data.stock_queue: |
| 778 | self.wh_data.stock_queue.append([0, sle.incoming_rate or sle.outgoing_rate or self.wh_data.valuation_rate]) |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 779 | |
Javier Wong | 9b11d9b | 2017-04-14 18:24:04 +0800 | [diff] [blame] | 780 | def check_if_allow_zero_valuation_rate(self, voucher_type, voucher_detail_no): |
deepeshgarg007 | f9c0ef3 | 2019-07-30 18:49:19 +0530 | [diff] [blame] | 781 | ref_item_dt = "" |
| 782 | |
| 783 | if voucher_type == "Stock Entry": |
| 784 | ref_item_dt = voucher_type + " Detail" |
| 785 | elif voucher_type in ["Purchase Invoice", "Sales Invoice", "Delivery Note", "Purchase Receipt"]: |
| 786 | ref_item_dt = voucher_type + " Item" |
| 787 | |
| 788 | if ref_item_dt: |
| 789 | return frappe.db.get_value(ref_item_dt, voucher_detail_no, "allow_zero_valuation_rate") |
| 790 | else: |
| 791 | return 0 |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 792 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 793 | def get_sle_before_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 794 | """get previous stock ledger entry before current time-bucket""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 795 | sle = get_stock_ledger_entries(args, "<", "desc", "limit 1", for_update=False) |
| 796 | sle = sle[0] if sle else frappe._dict() |
| 797 | return sle |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 798 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 799 | def get_sle_after_datetime(self, args): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 800 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 801 | 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] | 802 | |
| 803 | def raise_exceptions(self): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 804 | msg_list = [] |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 805 | for warehouse, exceptions in self.exceptions.items(): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 806 | deficiency = min(e["diff"] for e in exceptions) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 807 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 808 | if ((exceptions[0]["voucher_type"], exceptions[0]["voucher_no"]) in |
| 809 | frappe.local.flags.currently_saving): |
Nabin Hait | 3edefb1 | 2016-07-20 16:13:18 +0530 | [diff] [blame] | 810 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 811 | msg = _("{0} units of {1} needed in {2} to complete this transaction.").format( |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 812 | abs(deficiency), frappe.get_desk_link('Item', exceptions[0]["item_code"]), |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 813 | frappe.get_desk_link('Warehouse', warehouse)) |
| 814 | else: |
| 815 | msg = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 816 | abs(deficiency), frappe.get_desk_link('Item', exceptions[0]["item_code"]), |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 817 | frappe.get_desk_link('Warehouse', warehouse), |
| 818 | exceptions[0]["posting_date"], exceptions[0]["posting_time"], |
| 819 | frappe.get_desk_link(exceptions[0]["voucher_type"], exceptions[0]["voucher_no"])) |
Rushabh Mehta | 538607e | 2016-06-12 11:03:00 +0530 | [diff] [blame] | 820 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 821 | if msg: |
| 822 | msg_list.append(msg) |
| 823 | |
| 824 | if msg_list: |
| 825 | message = "\n\n".join(msg_list) |
| 826 | if self.verbose: |
| 827 | frappe.throw(message, NegativeStockError, title='Insufficient Stock') |
| 828 | else: |
| 829 | raise NegativeStockError(message) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 830 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 831 | def update_bin(self): |
| 832 | # update bin for each warehouse |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 833 | for warehouse, data in self.data.items(): |
Ankush Menat | 97060c4 | 2021-12-03 11:50:38 +0530 | [diff] [blame] | 834 | bin_name = get_or_make_bin(self.item_code, warehouse) |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 835 | |
Ankush Menat | 97060c4 | 2021-12-03 11:50:38 +0530 | [diff] [blame] | 836 | frappe.db.set_value('Bin', bin_name, { |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 837 | "valuation_rate": data.valuation_rate, |
| 838 | "actual_qty": data.qty_after_transaction, |
| 839 | "stock_value": data.stock_value |
| 840 | }) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 841 | |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 842 | |
| 843 | def get_previous_sle_of_current_voucher(args, exclude_current_voucher=False): |
| 844 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 845 | |
| 846 | args['time_format'] = '%H:%i:%s' |
| 847 | if not args.get("posting_date"): |
| 848 | args["posting_date"] = "1900-01-01" |
| 849 | if not args.get("posting_time"): |
| 850 | args["posting_time"] = "00:00" |
| 851 | |
| 852 | voucher_condition = "" |
| 853 | if exclude_current_voucher: |
| 854 | voucher_no = args.get("voucher_no") |
| 855 | voucher_condition = f"and voucher_no != '{voucher_no}'" |
| 856 | |
| 857 | sle = frappe.db.sql(""" |
| 858 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 859 | from `tabStock Ledger Entry` |
| 860 | where item_code = %(item_code)s |
| 861 | and warehouse = %(warehouse)s |
| 862 | and is_cancelled = 0 |
| 863 | {voucher_condition} |
| 864 | and timestamp(posting_date, time_format(posting_time, %(time_format)s)) < timestamp(%(posting_date)s, time_format(%(posting_time)s, %(time_format)s)) |
| 865 | order by timestamp(posting_date, posting_time) desc, creation desc |
| 866 | limit 1 |
| 867 | for update""".format(voucher_condition=voucher_condition), args, as_dict=1) |
| 868 | |
| 869 | return sle[0] if sle else frappe._dict() |
| 870 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 871 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 872 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 873 | get the last sle on or before the current time-bucket, |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 874 | to get actual qty before transaction, this function |
| 875 | is called from various transaction like stock entry, reco etc |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 876 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 877 | args = { |
| 878 | "item_code": "ABC", |
| 879 | "warehouse": "XYZ", |
| 880 | "posting_date": "2012-12-12", |
| 881 | "posting_time": "12:00", |
| 882 | "sle": "name of reference Stock Ledger Entry" |
| 883 | } |
| 884 | """ |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 885 | args["name"] = args.get("sle", None) or "" |
| 886 | 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] | 887 | return sle and sle[0] or {} |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 888 | |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 889 | def get_stock_ledger_entries(previous_sle, operator=None, |
| 890 | order="desc", limit=None, for_update=False, debug=False, check_serial_no=True): |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 891 | """get stock ledger entries filtered by specific posting datetime conditions""" |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 892 | conditions = " and timestamp(posting_date, posting_time) {0} timestamp(%(posting_date)s, %(posting_time)s)".format(operator) |
| 893 | if previous_sle.get("warehouse"): |
| 894 | conditions += " and warehouse = %(warehouse)s" |
| 895 | elif previous_sle.get("warehouse_condition"): |
| 896 | conditions += " and " + previous_sle.get("warehouse_condition") |
| 897 | |
Rohit Waghchaure | 66aa37f | 2019-05-24 16:53:51 +0530 | [diff] [blame] | 898 | if check_serial_no and previous_sle.get("serial_no"): |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 899 | # conditions += " and serial_no like {}".format(frappe.db.escape('%{0}%'.format(previous_sle.get("serial_no")))) |
| 900 | serial_no = previous_sle.get("serial_no") |
| 901 | conditions += (""" and |
| 902 | ( |
| 903 | serial_no = {0} |
| 904 | or serial_no like {1} |
| 905 | or serial_no like {2} |
| 906 | or serial_no like {3} |
| 907 | ) |
| 908 | """).format(frappe.db.escape(serial_no), frappe.db.escape('{}\n%'.format(serial_no)), |
| 909 | frappe.db.escape('%\n{}'.format(serial_no)), frappe.db.escape('%\n{}\n%'.format(serial_no))) |
Rohit Waghchaure | 05d3bcb | 2019-04-28 18:39:18 +0530 | [diff] [blame] | 910 | |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 911 | if not previous_sle.get("posting_date"): |
| 912 | previous_sle["posting_date"] = "1900-01-01" |
| 913 | if not previous_sle.get("posting_time"): |
| 914 | previous_sle["posting_time"] = "00:00" |
| 915 | |
| 916 | if operator in (">", "<=") and previous_sle.get("name"): |
| 917 | conditions += " and name!=%(name)s" |
| 918 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 919 | return frappe.db.sql(""" |
| 920 | select *, timestamp(posting_date, posting_time) as "timestamp" |
| 921 | from `tabStock Ledger Entry` |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 922 | where item_code = %%(item_code)s |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 923 | and is_cancelled = 0 |
Nabin Hait | b9ce104 | 2018-02-01 14:58:50 +0530 | [diff] [blame] | 924 | %(conditions)s |
Aditya Hase | 0c16424 | 2019-01-07 22:07:13 +0530 | [diff] [blame] | 925 | order by timestamp(posting_date, posting_time) %(order)s, creation %(order)s |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 926 | %(limit)s %(for_update)s""" % { |
| 927 | "conditions": conditions, |
| 928 | "limit": limit or "", |
| 929 | "for_update": for_update and "for update" or "", |
| 930 | "order": order |
Rushabh Mehta | 50dc4e9 | 2015-02-19 20:05:45 +0530 | [diff] [blame] | 931 | }, previous_sle, as_dict=1, debug=debug) |
Rushabh Mehta | df9e80c | 2015-02-17 19:55:17 +0530 | [diff] [blame] | 932 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 933 | def get_sle_by_voucher_detail_no(voucher_detail_no, excluded_sle=None): |
| 934 | return frappe.db.get_value('Stock Ledger Entry', |
| 935 | {'voucher_detail_no': voucher_detail_no, 'name': ['!=', excluded_sle]}, |
| 936 | ['item_code', 'warehouse', 'posting_date', 'posting_time', 'timestamp(posting_date, posting_time) as timestamp'], |
| 937 | as_dict=1) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 938 | |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 939 | def get_valuation_rate(item_code, warehouse, voucher_type, voucher_no, |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 940 | allow_zero_rate=False, currency=None, company=None, raise_error_if_no_rate=True): |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 941 | |
Ankush Menat | f7ffe04 | 2021-11-01 13:21:14 +0530 | [diff] [blame] | 942 | if not company: |
| 943 | company = frappe.get_cached_value("Warehouse", warehouse, "company") |
| 944 | |
| 945 | # Get valuation rate from last sle for the same item and warehouse |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 946 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 947 | from `tabStock Ledger Entry` force index (item_warehouse) |
Mangesh-Khairnar | 0df5134 | 2019-08-19 10:04:52 +0530 | [diff] [blame] | 948 | where |
| 949 | item_code = %s |
| 950 | AND warehouse = %s |
| 951 | AND valuation_rate >= 0 |
| 952 | AND NOT (voucher_no = %s AND voucher_type = %s) |
| 953 | order by posting_date desc, posting_time desc, name desc limit 1""", (item_code, warehouse, voucher_no, voucher_type)) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 954 | |
| 955 | if not last_valuation_rate: |
Nabin Hait | a0b967f | 2017-01-18 18:35:58 +0530 | [diff] [blame] | 956 | # Get valuation rate from last sle for the item against any warehouse |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 957 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
Deepesh Garg | 6f107da | 2021-10-12 20:15:55 +0530 | [diff] [blame] | 958 | from `tabStock Ledger Entry` force index (item_code) |
Mangesh-Khairnar | 0df5134 | 2019-08-19 10:04:52 +0530 | [diff] [blame] | 959 | where |
| 960 | item_code = %s |
| 961 | AND valuation_rate > 0 |
| 962 | AND NOT(voucher_no = %s AND voucher_type = %s) |
| 963 | order by posting_date desc, posting_time desc, name desc limit 1""", (item_code, voucher_no, voucher_type)) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 964 | |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 965 | if last_valuation_rate: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 966 | return flt(last_valuation_rate[0][0]) |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 967 | |
| 968 | # If negative stock allowed, and item delivered without any incoming entry, |
| 969 | # system does not found any SLE, then take valuation rate from Item |
| 970 | valuation_rate = frappe.db.get_value("Item", item_code, "valuation_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 971 | |
| 972 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 973 | # try Item Standard rate |
| 974 | valuation_rate = frappe.db.get_value("Item", item_code, "standard_rate") |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 975 | |
Rushabh Mehta | aedaac6 | 2017-05-04 09:35:19 +0530 | [diff] [blame] | 976 | if not valuation_rate: |
Nabin Hait | a645f36 | 2018-03-01 10:31:24 +0530 | [diff] [blame] | 977 | # try in price list |
| 978 | valuation_rate = frappe.db.get_value('Item Price', |
| 979 | dict(item_code=item_code, buying=1, currency=currency), |
| 980 | 'price_list_rate') |
Rushabh Mehta | cc8b2b2 | 2017-03-31 12:44:29 +0530 | [diff] [blame] | 981 | |
Nabin Hait | 7ba092e | 2018-02-01 10:51:27 +0530 | [diff] [blame] | 982 | if not allow_zero_rate and not valuation_rate and raise_error_if_no_rate \ |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 983 | and cint(erpnext.is_perpetual_inventory_enabled(company)): |
Neil Trini Lasrado | 193c891 | 2017-03-28 17:39:34 +0530 | [diff] [blame] | 984 | frappe.local.message_log = [] |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 985 | form_link = get_link_to_form("Item", item_code) |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 986 | |
| 987 | message = _("Valuation Rate for the Item {0}, is required to do accounting entries for {1} {2}.").format(form_link, voucher_type, voucher_no) |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 988 | message += "<br><br>" + _("Here are the options to proceed:") |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 989 | solutions = "<li>" + _("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.").format(voucher_type) + "</li>" |
Rohit Waghchaure | bb3e5d0 | 2021-04-24 17:28:33 +0530 | [diff] [blame] | 990 | solutions += "<li>" + _("If not, you can Cancel / Submit this entry") + " {0} ".format(frappe.bold("after")) + _("performing either one below:") + "</li>" |
Marica | 97715f2 | 2020-05-11 20:45:37 +0530 | [diff] [blame] | 991 | sub_solutions = "<ul><li>" + _("Create an incoming stock transaction for the Item.") + "</li>" |
| 992 | sub_solutions += "<li>" + _("Mention Valuation Rate in the Item master.") + "</li></ul>" |
| 993 | msg = message + solutions + sub_solutions + "</li>" |
| 994 | |
| 995 | frappe.throw(msg=msg, title=_("Valuation Rate Missing")) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 996 | |
| 997 | return valuation_rate |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 998 | |
Ankush Menat | e7109c1 | 2021-08-26 16:40:45 +0530 | [diff] [blame] | 999 | def update_qty_in_future_sle(args, allow_negative_stock=False): |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1000 | """Recalculate Qty after Transaction in future SLEs based on current SLE.""" |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1001 | datetime_limit_condition = "" |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1002 | qty_shift = args.actual_qty |
| 1003 | |
| 1004 | # find difference/shift in qty caused by stock reconciliation |
| 1005 | if args.voucher_type == "Stock Reconciliation": |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1006 | qty_shift = get_stock_reco_qty_shift(args) |
| 1007 | |
| 1008 | # find the next nearest stock reco so that we only recalculate SLEs till that point |
| 1009 | next_stock_reco_detail = get_next_stock_reco(args) |
| 1010 | if next_stock_reco_detail: |
| 1011 | detail = next_stock_reco_detail[0] |
| 1012 | # add condition to update SLEs before this date & time |
| 1013 | datetime_limit_condition = get_datetime_limit_condition(detail) |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1014 | |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1015 | frappe.db.sql(""" |
| 1016 | update `tabStock Ledger Entry` |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1017 | set qty_after_transaction = qty_after_transaction + {qty_shift} |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1018 | where |
| 1019 | item_code = %(item_code)s |
| 1020 | and warehouse = %(warehouse)s |
| 1021 | and voucher_no != %(voucher_no)s |
| 1022 | and is_cancelled = 0 |
| 1023 | and (timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s) |
| 1024 | or ( |
| 1025 | timestamp(posting_date, posting_time) = timestamp(%(posting_date)s, %(posting_time)s) |
| 1026 | and creation > %(creation)s |
| 1027 | ) |
| 1028 | ) |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1029 | {datetime_limit_condition} |
| 1030 | """.format(qty_shift=qty_shift, datetime_limit_condition=datetime_limit_condition), args) |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1031 | |
| 1032 | validate_negative_qty_in_future_sle(args, allow_negative_stock) |
| 1033 | |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1034 | def get_stock_reco_qty_shift(args): |
| 1035 | stock_reco_qty_shift = 0 |
| 1036 | if args.get("is_cancelled"): |
| 1037 | if args.get("previous_qty_after_transaction"): |
| 1038 | # get qty (balance) that was set at submission |
| 1039 | last_balance = args.get("previous_qty_after_transaction") |
| 1040 | stock_reco_qty_shift = flt(args.qty_after_transaction) - flt(last_balance) |
| 1041 | else: |
| 1042 | stock_reco_qty_shift = flt(args.actual_qty) |
| 1043 | else: |
| 1044 | # reco is being submitted |
| 1045 | last_balance = get_previous_sle_of_current_voucher(args, |
| 1046 | exclude_current_voucher=True).get("qty_after_transaction") |
| 1047 | |
| 1048 | if last_balance is not None: |
| 1049 | stock_reco_qty_shift = flt(args.qty_after_transaction) - flt(last_balance) |
| 1050 | else: |
| 1051 | stock_reco_qty_shift = args.qty_after_transaction |
| 1052 | |
| 1053 | return stock_reco_qty_shift |
| 1054 | |
| 1055 | def get_next_stock_reco(args): |
| 1056 | """Returns next nearest stock reconciliaton's details.""" |
| 1057 | |
| 1058 | return frappe.db.sql(""" |
| 1059 | select |
| 1060 | name, posting_date, posting_time, creation, voucher_no |
| 1061 | from |
marination | 8c44126 | 2021-07-02 17:46:05 +0530 | [diff] [blame] | 1062 | `tabStock Ledger Entry` |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1063 | where |
| 1064 | item_code = %(item_code)s |
| 1065 | and warehouse = %(warehouse)s |
| 1066 | and voucher_type = 'Stock Reconciliation' |
| 1067 | and voucher_no != %(voucher_no)s |
| 1068 | and is_cancelled = 0 |
| 1069 | and (timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s) |
| 1070 | or ( |
| 1071 | timestamp(posting_date, posting_time) = timestamp(%(posting_date)s, %(posting_time)s) |
| 1072 | and creation > %(creation)s |
| 1073 | ) |
| 1074 | ) |
| 1075 | limit 1 |
| 1076 | """, args, as_dict=1) |
| 1077 | |
| 1078 | def get_datetime_limit_condition(detail): |
marination | 4038977 | 2021-07-02 17:13:45 +0530 | [diff] [blame] | 1079 | return f""" |
| 1080 | and |
| 1081 | (timestamp(posting_date, posting_time) < timestamp('{detail.posting_date}', '{detail.posting_time}') |
| 1082 | or ( |
| 1083 | timestamp(posting_date, posting_time) = timestamp('{detail.posting_date}', '{detail.posting_time}') |
| 1084 | and creation < '{detail.creation}' |
| 1085 | ) |
| 1086 | )""" |
| 1087 | |
Ankush Menat | e7109c1 | 2021-08-26 16:40:45 +0530 | [diff] [blame] | 1088 | def validate_negative_qty_in_future_sle(args, allow_negative_stock=False): |
| 1089 | allow_negative_stock = cint(allow_negative_stock) \ |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1090 | or cint(frappe.db.get_single_value("Stock Settings", "allow_negative_stock")) |
| 1091 | |
marination | 8418c4b | 2021-06-22 21:35:25 +0530 | [diff] [blame] | 1092 | if (args.actual_qty < 0 or args.voucher_type == "Stock Reconciliation") and not allow_negative_stock: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1093 | sle = get_future_sle_with_negative_qty(args) |
| 1094 | if sle: |
| 1095 | message = _("{0} units of {1} needed in {2} on {3} {4} for {5} to complete this transaction.").format( |
| 1096 | abs(sle[0]["qty_after_transaction"]), |
| 1097 | frappe.get_desk_link('Item', args.item_code), |
| 1098 | frappe.get_desk_link('Warehouse', args.warehouse), |
| 1099 | sle[0]["posting_date"], sle[0]["posting_time"], |
| 1100 | frappe.get_desk_link(sle[0]["voucher_type"], sle[0]["voucher_no"])) |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1101 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1102 | frappe.throw(message, NegativeStockError, title='Insufficient Stock') |
| 1103 | |
| 1104 | def get_future_sle_with_negative_qty(args): |
| 1105 | return frappe.db.sql(""" |
| 1106 | select |
| 1107 | qty_after_transaction, posting_date, posting_time, |
| 1108 | voucher_type, voucher_no |
| 1109 | from `tabStock Ledger Entry` |
Deepesh Garg | b4be292 | 2021-01-28 13:09:56 +0530 | [diff] [blame] | 1110 | where |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1111 | item_code = %(item_code)s |
| 1112 | and warehouse = %(warehouse)s |
| 1113 | and voucher_no != %(voucher_no)s |
| 1114 | and timestamp(posting_date, posting_time) >= timestamp(%(posting_date)s, %(posting_time)s) |
| 1115 | and is_cancelled = 0 |
Nabin Hait | 186a045 | 2021-02-18 14:14:21 +0530 | [diff] [blame] | 1116 | and qty_after_transaction < 0 |
Nabin Hait | 243d59b | 2021-02-02 16:55:13 +0530 | [diff] [blame] | 1117 | order by timestamp(posting_date, posting_time) asc |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1118 | limit 1 |
Sagar Vora | e50324a | 2021-03-31 12:44:03 +0530 | [diff] [blame] | 1119 | """, args, as_dict=1) |
Ankush Menat | 6a014d1 | 2021-04-12 20:21:27 +0530 | [diff] [blame] | 1120 | |
| 1121 | def _round_off_if_near_zero(number: float, precision: int = 6) -> float: |
| 1122 | """ Rounds off the number to zero only if number is close to zero for decimal |
| 1123 | specified in precision. Precision defaults to 6. |
| 1124 | """ |
| 1125 | if flt(number) < (1.0 / (10**precision)): |
| 1126 | return 0 |
| 1127 | |
| 1128 | return flt(number) |