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