Rushabh Mehta | ad45e31 | 2013-11-20 12:59:58 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes 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 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 6 | from frappe import _ |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | from frappe.utils import cint, flt, cstr, now |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 8 | from erpnext.stock.utils import get_valuation_method |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 9 | import json |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 10 | |
| 11 | # future reposting |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 12 | class NegativeStockError(frappe.ValidationError): pass |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 13 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 14 | _exceptions = frappe.local('stockledger_exceptions') |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 15 | # _exceptions = [] |
Anand Doshi | 5b004ff | 2013-09-25 19:55:41 +0530 | [diff] [blame] | 16 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 17 | def make_sl_entries(sl_entries, is_amended=None, allow_negative_stock=False, via_landed_cost_voucher=False): |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 18 | if sl_entries: |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 19 | from erpnext.stock.utils import update_bin |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 20 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 21 | cancel = True if sl_entries[0].get("is_cancelled") == "Yes" else False |
| 22 | if cancel: |
| 23 | set_as_cancel(sl_entries[0].get('voucher_no'), sl_entries[0].get('voucher_type')) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 24 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 25 | for sle in sl_entries: |
| 26 | sle_id = None |
| 27 | if sle.get('is_cancelled') == 'Yes': |
| 28 | sle['actual_qty'] = -flt(sle['actual_qty']) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 29 | |
Nabin Hait | 5288bde | 2014-11-03 15:08:21 +0530 | [diff] [blame] | 30 | if sle.get("actual_qty") or sle.get("voucher_type")=="Stock Reconciliation": |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 31 | sle_id = make_entry(sle, allow_negative_stock, via_landed_cost_voucher) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 32 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 33 | args = sle.copy() |
| 34 | args.update({ |
| 35 | "sle_id": sle_id, |
| 36 | "is_amended": is_amended |
| 37 | }) |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 38 | update_bin(args, allow_negative_stock, via_landed_cost_voucher) |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 39 | |
Nabin Hait | ca77574 | 2013-09-26 16:16:44 +0530 | [diff] [blame] | 40 | if cancel: |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 41 | delete_cancelled_entry(sl_entries[0].get('voucher_type'), sl_entries[0].get('voucher_no')) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 42 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 43 | def set_as_cancel(voucher_type, voucher_no): |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 44 | frappe.db.sql("""update `tabStock Ledger Entry` set is_cancelled='Yes', |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 45 | modified=%s, modified_by=%s |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 46 | where voucher_no=%s and voucher_type=%s""", |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 47 | (now(), frappe.session.user, voucher_type, voucher_no)) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 48 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 49 | 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] | 50 | args.update({"doctype": "Stock Ledger Entry"}) |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 51 | sle = frappe.get_doc(args) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 52 | sle.ignore_permissions = 1 |
Nabin Hait | 4ccd8d3 | 2015-01-23 12:18:01 +0530 | [diff] [blame] | 53 | sle.allow_negative_stock=allow_negative_stock |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 54 | sle.via_landed_cost_voucher = via_landed_cost_voucher |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 55 | sle.insert() |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 56 | sle.submit() |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 57 | return sle.name |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 58 | |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 59 | def delete_cancelled_entry(voucher_type, voucher_no): |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 60 | frappe.db.sql("""delete from `tabStock Ledger Entry` |
Nabin Hait | 9653f60 | 2013-08-20 15:37:33 +0530 | [diff] [blame] | 61 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no)) |
Nabin Hait | 74c281c | 2013-08-19 16:17:18 +0530 | [diff] [blame] | 62 | |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 63 | def update_entries_after(args, allow_zero_rate=False, allow_negative_stock=False, |
| 64 | via_landed_cost_voucher=False, verbose=1): |
| 65 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 66 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 67 | update valution rate and qty after transaction |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 68 | from the current time-bucket onwards |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 69 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 70 | args = { |
| 71 | "item_code": "ABC", |
| 72 | "warehouse": "XYZ", |
| 73 | "posting_date": "2012-12-12", |
| 74 | "posting_time": "12:00" |
| 75 | } |
| 76 | """ |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 77 | if not _exceptions: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 78 | frappe.local.stockledger_exceptions = [] |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 79 | |
Nabin Hait | 9c47efb | 2015-01-21 16:22:45 +0530 | [diff] [blame] | 80 | if not allow_negative_stock: |
| 81 | allow_negative_stock = cint(frappe.db.get_default("allow_negative_stock")) |
| 82 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 83 | previous_sle = get_sle_before_datetime(args) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 84 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 85 | qty_after_transaction = flt(previous_sle.get("qty_after_transaction")) |
| 86 | valuation_rate = flt(previous_sle.get("valuation_rate")) |
| 87 | stock_queue = json.loads(previous_sle.get("stock_queue") or "[]") |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 88 | stock_value = flt(previous_sle.get("stock_value")) |
| 89 | prev_stock_value = flt(previous_sle.get("stock_value")) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 90 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 91 | entries_to_fix = get_sle_after_datetime(previous_sle or \ |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 92 | {"item_code": args["item_code"], "warehouse": args["warehouse"]}, for_update=True) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 93 | valuation_method = get_valuation_method(args["item_code"]) |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 94 | stock_value_difference = 0.0 |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 95 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 96 | for sle in entries_to_fix: |
Nabin Hait | 54c865e | 2015-03-27 15:38:31 +0530 | [diff] [blame] | 97 | if (sle.serial_no and not via_landed_cost_voucher) or not allow_negative_stock: |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 98 | # validate negative stock for serialized items, fifo valuation |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 99 | # or when negative stock is not allowed for moving average |
| 100 | if not validate_negative_stock(qty_after_transaction, sle): |
| 101 | qty_after_transaction += flt(sle.actual_qty) |
| 102 | continue |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 103 | |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 104 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 105 | if sle.serial_no: |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 106 | valuation_rate = get_serialized_values(qty_after_transaction, sle, valuation_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 107 | qty_after_transaction += flt(sle.actual_qty) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 108 | |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 109 | else: |
| 110 | if sle.voucher_type=="Stock Reconciliation": |
| 111 | valuation_rate = sle.valuation_rate |
| 112 | qty_after_transaction = sle.qty_after_transaction |
| 113 | stock_queue = [[qty_after_transaction, valuation_rate]] |
| 114 | else: |
| 115 | if valuation_method == "Moving Average": |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 116 | valuation_rate = get_moving_average_values(qty_after_transaction, sle, valuation_rate, allow_zero_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 117 | else: |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 118 | valuation_rate = get_fifo_values(qty_after_transaction, sle, stock_queue, allow_zero_rate) |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 119 | |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 120 | |
Nabin Hait | b96c014 | 2014-10-07 11:25:04 +0530 | [diff] [blame] | 121 | qty_after_transaction += flt(sle.actual_qty) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 122 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 123 | # get stock value |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 124 | if sle.serial_no: |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 125 | stock_value = qty_after_transaction * valuation_rate |
| 126 | elif valuation_method == "Moving Average": |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 127 | stock_value = qty_after_transaction * valuation_rate |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 128 | else: |
| 129 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue)) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 130 | |
Rushabh Mehta | 5404778 | 2013-12-26 11:07:46 +0530 | [diff] [blame] | 131 | # rounding as per precision |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 132 | from frappe.model.meta import get_field_precision |
Rushabh Mehta | e88bc8b | 2014-03-27 17:51:41 +0530 | [diff] [blame] | 133 | meta = frappe.get_meta("Stock Ledger Entry") |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 134 | |
| 135 | stock_value = flt(stock_value, get_field_precision(meta.get_field("stock_value"), |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 136 | frappe._dict({"fields": sle}))) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 137 | |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 138 | stock_value_difference = stock_value - prev_stock_value |
| 139 | prev_stock_value = stock_value |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 140 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 141 | # update current sle |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 142 | frappe.db.sql("""update `tabStock Ledger Entry` |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 143 | set qty_after_transaction=%s, valuation_rate=%s, stock_queue=%s, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 144 | stock_value=%s, stock_value_difference=%s where name=%s""", |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 145 | (qty_after_transaction, valuation_rate, |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 146 | json.dumps(stock_queue), stock_value, stock_value_difference, sle.name)) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 147 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 148 | if _exceptions: |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 149 | _raise_exceptions(args, verbose) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 150 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 151 | # update bin |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 152 | if not frappe.db.exists({"doctype": "Bin", "item_code": args["item_code"], |
Nabin Hait | ac53b11 | 2013-01-11 19:25:46 +0530 | [diff] [blame] | 153 | "warehouse": args["warehouse"]}): |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 154 | bin_wrapper = frappe.get_doc({ |
Nabin Hait | ac53b11 | 2013-01-11 19:25:46 +0530 | [diff] [blame] | 155 | "doctype": "Bin", |
| 156 | "item_code": args["item_code"], |
| 157 | "warehouse": args["warehouse"], |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 158 | }) |
Anand Doshi | c313d66 | 2013-01-14 15:46:17 +0530 | [diff] [blame] | 159 | bin_wrapper.ignore_permissions = 1 |
| 160 | bin_wrapper.insert() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 161 | |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 162 | frappe.db.sql("""update `tabBin` set valuation_rate=%s, actual_qty=%s, |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 163 | stock_value=%s, |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 164 | projected_qty = (actual_qty + indented_qty + ordered_qty + planned_qty - reserved_qty) |
| 165 | where item_code=%s and warehouse=%s""", (valuation_rate, qty_after_transaction, |
| 166 | stock_value, args["item_code"], args["warehouse"])) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 167 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 168 | def get_sle_before_datetime(args, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 169 | """ |
| 170 | get previous stock ledger entry before current time-bucket |
| 171 | |
| 172 | Details: |
| 173 | get the last sle before the current time-bucket, so that all values |
| 174 | are reposted from the current time-bucket onwards. |
| 175 | this is necessary because at the time of cancellation, there may be |
| 176 | entries between the cancelled entries in the same time-bucket |
| 177 | """ |
| 178 | sle = get_stock_ledger_entries(args, |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 179 | ["timestamp(posting_date, posting_time) < timestamp(%(posting_date)s, %(posting_time)s)"], |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 180 | "desc", "limit 1", for_update=for_update) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 181 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 182 | return sle and sle[0] or frappe._dict() |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 183 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 184 | def get_sle_after_datetime(args, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 185 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 186 | # NOTE: using for update of |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 187 | conditions = ["timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s)"] |
| 188 | |
| 189 | # Excluding name: Workaround for MariaDB timestamp() floating microsecond issue |
| 190 | if args.get("name"): |
| 191 | conditions.append("name!=%(name)s") |
| 192 | |
| 193 | return get_stock_ledger_entries(args, conditions, "asc", for_update=for_update) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 194 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 195 | def get_stock_ledger_entries(args, conditions=None, order="desc", limit=None, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 196 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 197 | if not args.get("posting_date"): |
| 198 | args["posting_date"] = "1900-01-01" |
| 199 | if not args.get("posting_time"): |
Anand Doshi | 71bed31 | 2013-03-13 12:57:04 +0530 | [diff] [blame] | 200 | args["posting_time"] = "00:00" |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 201 | |
Anand Doshi | 2ce39cf | 2014-04-07 18:51:58 +0530 | [diff] [blame] | 202 | return frappe.db.sql("""select *, timestamp(posting_date, posting_time) as "timestamp" from `tabStock Ledger Entry` |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 203 | where item_code = %%(item_code)s |
| 204 | and warehouse = %%(warehouse)s |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 205 | and ifnull(is_cancelled, 'No')='No' |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 206 | %(conditions)s |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 207 | order by timestamp(posting_date, posting_time) %(order)s, name %(order)s |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 208 | %(limit)s %(for_update)s""" % { |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 209 | "conditions": conditions and ("and " + " and ".join(conditions)) or "", |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 210 | "limit": limit or "", |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 211 | "for_update": for_update and "for update" or "", |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 212 | "order": order |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 213 | }, args, as_dict=1) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 214 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 215 | def validate_negative_stock(qty_after_transaction, sle): |
| 216 | """ |
| 217 | validate negative stock for entries current datetime onwards |
| 218 | will not consider cancelled entries |
| 219 | """ |
| 220 | diff = qty_after_transaction + flt(sle.actual_qty) |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 221 | |
| 222 | if not _exceptions: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 223 | frappe.local.stockledger_exceptions = [] |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 224 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 225 | if diff < 0 and abs(diff) > 0.0001: |
| 226 | # negative stock! |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 227 | exc = sle.copy().update({"diff": diff}) |
| 228 | _exceptions.append(exc) |
| 229 | return False |
| 230 | else: |
| 231 | return True |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 232 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 233 | def get_serialized_values(qty_after_transaction, sle, valuation_rate): |
| 234 | incoming_rate = flt(sle.incoming_rate) |
| 235 | actual_qty = flt(sle.actual_qty) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 236 | serial_no = cstr(sle.serial_no).split("\n") |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 237 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 238 | if incoming_rate < 0: |
| 239 | # wrong incoming rate |
| 240 | incoming_rate = valuation_rate |
| 241 | elif incoming_rate == 0 or flt(sle.actual_qty) < 0: |
| 242 | # In case of delivery/stock issue, get average purchase rate |
| 243 | # of serial nos of current entry |
Anand Doshi | e9baaa6 | 2014-02-26 12:35:33 +0530 | [diff] [blame] | 244 | incoming_rate = flt(frappe.db.sql("""select avg(ifnull(purchase_rate, 0)) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 245 | from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))), |
| 246 | tuple(serial_no))[0][0]) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 247 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 248 | if incoming_rate and not valuation_rate: |
| 249 | valuation_rate = incoming_rate |
| 250 | else: |
| 251 | new_stock_qty = qty_after_transaction + actual_qty |
| 252 | if new_stock_qty > 0: |
| 253 | new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate |
| 254 | if new_stock_value > 0: |
| 255 | # calculate new valuation rate only if stock value is positive |
| 256 | # else it remains the same as that of previous entry |
| 257 | valuation_rate = new_stock_value / new_stock_qty |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 258 | |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 259 | return valuation_rate |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 260 | |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 261 | def get_moving_average_values(qty_after_transaction, sle, valuation_rate, allow_zero_rate): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 262 | incoming_rate = flt(sle.incoming_rate) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 263 | actual_qty = flt(sle.actual_qty) |
| 264 | |
Nabin Hait | 7c6f990 | 2014-10-10 18:03:27 +0530 | [diff] [blame] | 265 | if flt(sle.actual_qty) > 0: |
| 266 | if qty_after_transaction < 0 and not valuation_rate: |
| 267 | # if negative stock, take current valuation rate as incoming rate |
| 268 | valuation_rate = incoming_rate |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 269 | |
Nabin Hait | 7c6f990 | 2014-10-10 18:03:27 +0530 | [diff] [blame] | 270 | new_stock_qty = abs(qty_after_transaction) + actual_qty |
| 271 | new_stock_value = (abs(qty_after_transaction) * valuation_rate) + (actual_qty * incoming_rate) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 272 | |
Nabin Hait | 7c6f990 | 2014-10-10 18:03:27 +0530 | [diff] [blame] | 273 | if new_stock_qty: |
| 274 | valuation_rate = new_stock_value / flt(new_stock_qty) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 275 | elif not valuation_rate and qty_after_transaction <= 0: |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 276 | valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, allow_zero_rate) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 277 | |
Nabin Hait | bf49212 | 2014-10-14 16:09:14 +0530 | [diff] [blame] | 278 | return abs(flt(valuation_rate)) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 279 | |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 280 | def get_fifo_values(qty_after_transaction, sle, stock_queue, allow_zero_rate): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 281 | incoming_rate = flt(sle.incoming_rate) |
| 282 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 283 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 284 | if actual_qty > 0: |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 285 | if not stock_queue: |
| 286 | stock_queue.append([0, 0]) |
| 287 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 288 | if stock_queue[-1][0] > 0: |
| 289 | stock_queue.append([actual_qty, incoming_rate]) |
| 290 | else: |
| 291 | qty = stock_queue[-1][0] + actual_qty |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 292 | if qty == 0: |
| 293 | stock_queue.pop(-1) |
| 294 | else: |
| 295 | stock_queue[-1] = [qty, incoming_rate] |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 296 | else: |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 297 | qty_to_pop = abs(actual_qty) |
| 298 | while qty_to_pop: |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 299 | if not stock_queue: |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 300 | stock_queue.append([0, get_valuation_rate(sle.item_code, sle.warehouse, allow_zero_rate) |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 301 | if qty_after_transaction <= 0 else 0]) |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 302 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 303 | batch = stock_queue[0] |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 304 | |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 305 | if qty_to_pop >= batch[0]: |
| 306 | # consume current batch |
| 307 | qty_to_pop = qty_to_pop - batch[0] |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 308 | stock_queue.pop(0) |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 309 | if not stock_queue and qty_to_pop: |
| 310 | # stock finished, qty still remains to be withdrawn |
| 311 | # negative stock, keep in as a negative batch |
| 312 | stock_queue.append([-qty_to_pop, batch[1]]) |
| 313 | break |
| 314 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 315 | else: |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 316 | # qty found in current batch |
| 317 | # consume it and exit |
| 318 | batch[0] = batch[0] - qty_to_pop |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 319 | qty_to_pop = 0 |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 320 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 321 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue)) |
| 322 | stock_qty = sum((flt(batch[0]) for batch in stock_queue)) |
| 323 | |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 324 | valuation_rate = (stock_value / flt(stock_qty)) if stock_qty else 0 |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 325 | |
Nabin Hait | 4d74216 | 2014-10-09 19:25:03 +0530 | [diff] [blame] | 326 | return abs(valuation_rate) |
| 327 | |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 328 | def _raise_exceptions(args, verbose=1): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 329 | deficiency = min(e["diff"] for e in _exceptions) |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 330 | msg = _("Negative Stock Error ({6}) for Item {0} in Warehouse {1} on {2} {3} in {4} {5}").format(args["item_code"], |
| 331 | args.get("warehouse"), _exceptions[0]["posting_date"], _exceptions[0]["posting_time"], |
| 332 | _(_exceptions[0]["voucher_type"]), _exceptions[0]["voucher_no"], deficiency) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 333 | if verbose: |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 334 | frappe.throw(msg, NegativeStockError) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 335 | else: |
Rushabh Mehta | 4c17f94 | 2013-08-12 14:18:09 +0530 | [diff] [blame] | 336 | raise NegativeStockError, msg |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 337 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 338 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 339 | """ |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 340 | get the last sle on or before the current time-bucket, |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 341 | to get actual qty before transaction, this function |
| 342 | is called from various transaction like stock entry, reco etc |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 343 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 344 | args = { |
| 345 | "item_code": "ABC", |
| 346 | "warehouse": "XYZ", |
| 347 | "posting_date": "2012-12-12", |
| 348 | "posting_time": "12:00", |
| 349 | "sle": "name of reference Stock Ledger Entry" |
| 350 | } |
| 351 | """ |
| 352 | if not args.get("sle"): args["sle"] = "" |
Nabin Hait | dc82d4f | 2014-04-07 12:02:57 +0530 | [diff] [blame] | 353 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 354 | sle = get_stock_ledger_entries(args, ["name != %(sle)s", |
| 355 | "timestamp(posting_date, posting_time) <= timestamp(%(posting_date)s, %(posting_time)s)"], |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 356 | "desc", "limit 1", for_update=for_update) |
Pratik Vyas | 16371b7 | 2013-09-18 18:31:03 +0530 | [diff] [blame] | 357 | return sle and sle[0] or {} |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 358 | |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 359 | def get_valuation_rate(item_code, warehouse, allow_zero_rate=False): |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 360 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 361 | from `tabStock Ledger Entry` |
| 362 | where item_code = %s and warehouse = %s |
| 363 | and ifnull(valuation_rate, 0) > 0 |
| 364 | order by posting_date desc, posting_time desc, name desc limit 1""", (item_code, warehouse)) |
| 365 | |
| 366 | if not last_valuation_rate: |
| 367 | last_valuation_rate = frappe.db.sql("""select valuation_rate |
| 368 | from `tabStock Ledger Entry` |
| 369 | where item_code = %s and ifnull(valuation_rate, 0) > 0 |
| 370 | order by posting_date desc, posting_time desc, name desc limit 1""", item_code) |
| 371 | |
| 372 | valuation_rate = flt(last_valuation_rate[0][0]) if last_valuation_rate else 0 |
| 373 | |
| 374 | if not valuation_rate: |
| 375 | valuation_rate = frappe.db.get_value("Item Price", {"item_code": item_code, "buying": 1}, "price_list_rate") |
| 376 | |
Nabin Hait | f1a07ff | 2014-10-15 12:23:35 +0530 | [diff] [blame] | 377 | if not allow_zero_rate and not valuation_rate and cint(frappe.db.get_value("Accounts Settings", None, "auto_accounting_for_stock")): |
Nabin Hait | fb6e434 | 2014-10-15 11:34:40 +0530 | [diff] [blame] | 378 | frappe.throw(_("Purchase rate for item: {0} not found, which is required to book accounting entry (expense). Please mention item price against a buying price list.").format(item_code)) |
| 379 | |
| 380 | return valuation_rate |