blob: 304ded23902a84e75817f89dbb560744c1e05e85 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Nabin Haitc3afb252013-03-19 12:01:24 +05303
4from __future__ import unicode_literals
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe.utils import cint, flt, cstr
7from frappe import msgprint, _
8import frappe.defaults
Nabin Haitd4741942013-08-06 15:57:25 +05309
Rushabh Mehta1f847992013-12-12 19:12:19 +053010from erpnext.controllers.accounts_controller import AccountsController
Nabin Haitadeb9762014-10-06 11:53:52 +053011from erpnext.accounts.general_ledger import make_gl_entries, delete_gl_entries, process_gl_map
Nabin Haitc3afb252013-03-19 12:01:24 +053012
13class StockController(AccountsController):
Nabin Haitadeb9762014-10-06 11:53:52 +053014 def make_gl_entries(self, repost_future_gle=True, allow_negative_stock=False):
Anand Doshif78d1ae2014-03-28 13:55:00 +053015 if self.docstatus == 2:
16 delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053017
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053018 if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
Nabin Haitb9e04812014-09-26 14:22:18 +053019 warehouse_account = get_warehouse_account()
Anand Doshi2ce39cf2014-04-07 18:51:58 +053020
Anand Doshif78d1ae2014-03-28 13:55:00 +053021 if self.docstatus==1:
Nabin Haitadeb9762014-10-06 11:53:52 +053022 gl_entries = self.get_gl_entries(warehouse_account, allow_negative_stock)
Nabin Hait145e5e22013-10-22 23:51:41 +053023 make_gl_entries(gl_entries)
24
Nabin Haitbecf75d2014-03-27 17:18:29 +053025 if repost_future_gle:
Nabin Haitb9e04812014-09-26 14:22:18 +053026 items, warehouses = self.get_items_and_warehouses()
Nabin Haitadeb9762014-10-06 11:53:52 +053027 update_gl_entries_after(self.posting_date, self.posting_time, warehouses, items,
28 warehouse_account, allow_negative_stock)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053029
Nabin Haite83069c2013-11-14 18:40:08 +053030 def get_gl_entries(self, warehouse_account=None, default_expense_account=None,
Nabin Haitadeb9762014-10-06 11:53:52 +053031 default_cost_center=None, allow_negative_stock=False):
32
33 block_negative_stock(allow_negative_stock)
34
Nabin Hait142007a2013-09-17 15:15:16 +053035 if not warehouse_account:
Nabin Haitbecf75d2014-03-27 17:18:29 +053036 warehouse_account = get_warehouse_account()
Anand Doshi2ce39cf2014-04-07 18:51:58 +053037
Anand Doshide1a97d2014-04-17 11:37:46 +053038 sle_map = self.get_stock_ledger_details()
39 voucher_details = self.get_voucher_details(default_expense_account, default_cost_center, sle_map)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053040
Nabin Hait2e296fa2013-08-28 18:53:11 +053041 gl_list = []
Nabin Hait7a75e102013-09-17 10:21:20 +053042 warehouse_with_no_account = []
Nabin Hait2e296fa2013-08-28 18:53:11 +053043 for detail in voucher_details:
Anand Doshide1a97d2014-04-17 11:37:46 +053044 sle_list = sle_map.get(detail.name)
Nabin Hait2e296fa2013-08-28 18:53:11 +053045 if sle_list:
46 for sle in sle_list:
47 if warehouse_account.get(sle.warehouse):
Nabin Hait2e296fa2013-08-28 18:53:11 +053048 # from warehouse account
Rushabh Mehta052fe822014-04-16 19:20:11 +053049
Rushabh Mehtac38fc712014-04-16 17:21:25 +053050 self.check_expense_account(detail)
51
Nabin Haitadeb9762014-10-06 11:53:52 +053052 stock_value_difference = flt(sle.stock_value_difference, 2)
53 if not stock_value_difference:
54 valuation_rate = get_valuation_rate(sle.item_code, sle.warehouse, sle.posting_date)
55 stock_value_difference = flt(sle.qty)*flt(valuation_rate)
56
Nabin Hait2e296fa2013-08-28 18:53:11 +053057 gl_list.append(self.get_gl_dict({
58 "account": warehouse_account[sle.warehouse],
59 "against": detail.expense_account,
60 "cost_center": detail.cost_center,
Nabin Haitdc82d4f2014-04-07 12:02:57 +053061 "remarks": self.get("remarks") or "Accounting Entry for Stock",
Nabin Haitadeb9762014-10-06 11:53:52 +053062 "debit": stock_value_difference
Nabin Hait2e296fa2013-08-28 18:53:11 +053063 }))
Nabin Hait27994c22013-08-26 16:53:30 +053064
Nabin Hait2e296fa2013-08-28 18:53:11 +053065 # to target warehouse / expense account
66 gl_list.append(self.get_gl_dict({
67 "account": detail.expense_account,
68 "against": warehouse_account[sle.warehouse],
69 "cost_center": detail.cost_center,
Nabin Haitdc82d4f2014-04-07 12:02:57 +053070 "remarks": self.get("remarks") or "Accounting Entry for Stock",
Nabin Haitadeb9762014-10-06 11:53:52 +053071 "credit": stock_value_difference
Nabin Hait2e296fa2013-08-28 18:53:11 +053072 }))
Nabin Hait7a75e102013-09-17 10:21:20 +053073 elif sle.warehouse not in warehouse_with_no_account:
74 warehouse_with_no_account.append(sle.warehouse)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053075
76 if warehouse_with_no_account:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053077 msgprint(_("No accounting entries for the following warehouses") + ": \n" +
Nabin Hait7a75e102013-09-17 10:21:20 +053078 "\n".join(warehouse_with_no_account))
Anand Doshi2ce39cf2014-04-07 18:51:58 +053079
Nabin Hait2e296fa2013-08-28 18:53:11 +053080 return process_gl_map(gl_list)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053081
Anand Doshide1a97d2014-04-17 11:37:46 +053082 def get_voucher_details(self, default_expense_account, default_cost_center, sle_map):
83 if self.doctype == "Stock Reconciliation":
84 return [frappe._dict({ "name": voucher_detail_no, "expense_account": default_expense_account,
85 "cost_center": default_cost_center }) for voucher_detail_no, sle in sle_map.items()]
86 else:
87 details = self.get(self.fname)
Anand Doshi094610d2014-04-16 19:56:53 +053088
Anand Doshide1a97d2014-04-17 11:37:46 +053089 if default_expense_account or default_cost_center:
90 for d in details:
91 if default_expense_account and not d.get("expense_account"):
92 d.expense_account = default_expense_account
93 if default_cost_center and not d.get("cost_center"):
94 d.cost_center = default_cost_center
Anand Doshi2ce39cf2014-04-07 18:51:58 +053095
Anand Doshide1a97d2014-04-17 11:37:46 +053096 return details
Anand Doshi2ce39cf2014-04-07 18:51:58 +053097
Nabin Haitb9e04812014-09-26 14:22:18 +053098 def get_items_and_warehouses(self):
Nabin Haitbecf75d2014-03-27 17:18:29 +053099 items, warehouses = [], []
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530100
Nabin Haitbecf75d2014-03-27 17:18:29 +0530101 if hasattr(self, "fname"):
Rushabh Mehtaf191f852014-04-02 18:09:34 +0530102 item_doclist = self.get(self.fname)
Anand Doshif78d1ae2014-03-28 13:55:00 +0530103 elif self.doctype == "Stock Reconciliation":
Nabin Haitbecf75d2014-03-27 17:18:29 +0530104 import json
105 item_doclist = []
Anand Doshif78d1ae2014-03-28 13:55:00 +0530106 data = json.loads(self.reconciliation_json)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530107 for row in data[data.index(self.head_row)+1:]:
108 d = frappe._dict(zip(["item_code", "warehouse", "qty", "valuation_rate"], row))
109 item_doclist.append(d)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530110
Nabin Haitbecf75d2014-03-27 17:18:29 +0530111 if item_doclist:
112 for d in item_doclist:
113 if d.item_code and d.item_code not in items:
114 items.append(d.item_code)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530115
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530116 if d.get("warehouse") and d.warehouse not in warehouses:
Nabin Haitbecf75d2014-03-27 17:18:29 +0530117 warehouses.append(d.warehouse)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530118
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530119 if self.doctype == "Stock Entry":
120 if d.get("s_warehouse") and d.s_warehouse not in warehouses:
121 warehouses.append(d.s_warehouse)
122 if d.get("t_warehouse") and d.t_warehouse not in warehouses:
123 warehouses.append(d.t_warehouse)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530124
Nabin Haitb9e04812014-09-26 14:22:18 +0530125 return items, warehouses
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530126
Nabin Hait2e296fa2013-08-28 18:53:11 +0530127 def get_stock_ledger_details(self):
128 stock_ledger = {}
Anand Doshie9baaa62014-02-26 12:35:33 +0530129 for sle in frappe.db.sql("""select warehouse, stock_value_difference, voucher_detail_no
Nabin Hait27994c22013-08-26 16:53:30 +0530130 from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""",
Anand Doshif78d1ae2014-03-28 13:55:00 +0530131 (self.doctype, self.name), as_dict=True):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530132 stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
133 return stock_ledger
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530134
Nabin Hait2e296fa2013-08-28 18:53:11 +0530135 def make_adjustment_entry(self, expected_gle, voucher_obj):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530136 from erpnext.accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530137 account_list = [d.account for d in expected_gle]
138 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530139
Nabin Hait27994c22013-08-26 16:53:30 +0530140 cost_center = self.get_company_default("cost_center")
141 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
142
Nabin Haitd4741942013-08-06 15:57:25 +0530143 gl_entries = []
144 for account, diff in acc_diff.items():
145 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530146 gl_entries.append([
147 # stock in hand account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530148 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530149 "account": account,
150 "against": stock_adjustment_account,
151 "debit": diff,
152 "remarks": "Adjustment Accounting Entry for Stock",
153 }),
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530154
Nabin Hait27994c22013-08-26 16:53:30 +0530155 # account against stock in hand
Nabin Hait2e296fa2013-08-28 18:53:11 +0530156 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530157 "account": stock_adjustment_account,
158 "against": account,
159 "credit": diff,
160 "cost_center": cost_center or None,
161 "remarks": "Adjustment Accounting Entry for Stock",
162 }),
163 ])
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530164
Nabin Haitd4741942013-08-06 15:57:25 +0530165 if gl_entries:
Rushabh Mehta1f847992013-12-12 19:12:19 +0530166 from erpnext.accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530167 make_gl_entries(gl_entries)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530168
Nabin Hait27994c22013-08-26 16:53:30 +0530169 def check_expense_account(self, item):
Rushabh Mehta052fe822014-04-16 19:20:11 +0530170 if not item.get("expense_account"):
Nabin Haitd3412542014-05-01 17:42:21 +0530171 frappe.throw(_("Expense or Difference account is mandatory for Item {0} as it impacts overall stock value").format(item.item_code))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530172
Anand Doshi496123a2014-06-19 19:25:19 +0530173 else:
Nabin Hait00778c92014-06-25 19:12:24 +0530174 is_expense_account = frappe.db.get_value("Account",
175 item.get("expense_account"), "report_type")=="Profit and Loss"
nabinhaite6cee7b2014-07-10 19:06:39 +0530176 if self.doctype not in ("Purchase Receipt", "Stock Reconciliation") and not is_expense_account:
Nabin Hait00778c92014-06-25 19:12:24 +0530177 frappe.throw(_("Expense / Difference account ({0}) must be a 'Profit or Loss' account")
178 .format(item.get("expense_account")))
Anand Doshi496123a2014-06-19 19:25:19 +0530179 if is_expense_account and not item.get("cost_center"):
180 frappe.throw(_("{0} {1}: Cost Center is mandatory for Item {2}").format(
181 _(self.doctype), self.name, item.get("item_code")))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530182
183 def get_sl_entries(self, d, args):
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530184 sl_dict = {
Nabin Hait296d6262014-05-02 17:36:13 +0530185 "item_code": d.get("item_code", None),
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530186 "warehouse": d.get("warehouse", None),
Anand Doshif78d1ae2014-03-28 13:55:00 +0530187 "posting_date": self.posting_date,
188 "posting_time": self.posting_time,
189 "voucher_type": self.doctype,
190 "voucher_no": self.name,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530191 "voucher_detail_no": d.name,
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530192 "actual_qty": (self.docstatus==1 and 1 or -1)*flt(d.get("stock_qty")),
Nabin Hait296d6262014-05-02 17:36:13 +0530193 "stock_uom": d.get("stock_uom"),
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530194 "incoming_rate": 0,
Anand Doshif78d1ae2014-03-28 13:55:00 +0530195 "company": self.company,
196 "fiscal_year": self.fiscal_year,
Nabin Hait296d6262014-05-02 17:36:13 +0530197 "batch_no": cstr(d.get("batch_no")).strip(),
198 "serial_no": d.get("serial_no"),
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530199 "project": d.get("project_name"),
Anand Doshif78d1ae2014-03-28 13:55:00 +0530200 "is_cancelled": self.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530201 }
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530202
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530203 sl_dict.update(args)
204 return sl_dict
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530205
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530206 def make_sl_entries(self, sl_entries, is_amended=None):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530207 from erpnext.stock.stock_ledger import make_sl_entries
Nabin Hait74c281c2013-08-19 16:17:18 +0530208 make_sl_entries(sl_entries, is_amended)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530209
nabinhait87f24012014-07-16 19:48:29 +0530210 def make_gl_entries_on_cancel(self):
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530211 if frappe.db.sql("""select name from `tabGL Entry` where voucher_type=%s
Anand Doshif78d1ae2014-03-28 13:55:00 +0530212 and voucher_no=%s""", (self.doctype, self.name)):
Nabin Haitbecf75d2014-03-27 17:18:29 +0530213 self.make_gl_entries()
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530214
Anand Doshia740f752014-06-25 13:31:02 +0530215 def get_serialized_items(self):
216 serialized_items = []
217 item_codes = list(set([d.item_code for d in self.get(self.fname)]))
218 if item_codes:
219 serialized_items = frappe.db.sql_list("""select name from `tabItem`
220 where has_serial_no='Yes' and name in ({})""".format(", ".join(["%s"]*len(item_codes))),
221 tuple(item_codes))
222
223 return serialized_items
224
Nabin Haitadeb9762014-10-06 11:53:52 +0530225def update_gl_entries_after(posting_date, posting_time, for_warehouses=None, for_items=None,
226 warehouse_account=None, allow_negative_stock=False):
Nabin Haitbecf75d2014-03-27 17:18:29 +0530227 def _delete_gl_entries(voucher_type, voucher_no):
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530228 frappe.db.sql("""delete from `tabGL Entry`
Nabin Haitbecf75d2014-03-27 17:18:29 +0530229 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530230
Nabin Haitbecf75d2014-03-27 17:18:29 +0530231 if not warehouse_account:
232 warehouse_account = get_warehouse_account()
Nabin Haitb9e04812014-09-26 14:22:18 +0530233
234 future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time, for_warehouses, for_items)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530235 gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date)
236
237 for voucher_type, voucher_no in future_stock_vouchers:
238 existing_gle = gle.get((voucher_type, voucher_no), [])
Rushabh Mehta0a0f2492014-03-31 17:27:06 +0530239 voucher_obj = frappe.get_doc(voucher_type, voucher_no)
Nabin Haitadeb9762014-10-06 11:53:52 +0530240 expected_gle = voucher_obj.get_gl_entries(warehouse_account, allow_negative_stock)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530241 if expected_gle:
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530242 if not existing_gle or not compare_existing_and_expected_gle(existing_gle,
Nabin Haitbecf75d2014-03-27 17:18:29 +0530243 expected_gle):
244 _delete_gl_entries(voucher_type, voucher_no)
Nabin Haitadeb9762014-10-06 11:53:52 +0530245 voucher_obj.make_gl_entries(repost_future_gle=False, allow_negative_stock=allow_negative_stock)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530246 else:
247 _delete_gl_entries(voucher_type, voucher_no)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530248
Nabin Haitbecf75d2014-03-27 17:18:29 +0530249def compare_existing_and_expected_gle(existing_gle, expected_gle):
250 matched = True
251 for entry in expected_gle:
252 for e in existing_gle:
253 if entry.account==e.account and entry.against_account==e.against_account \
254 and entry.cost_center==e.cost_center \
255 and (entry.debit != e.debit or entry.credit != e.credit):
256 matched = False
257 break
258 return matched
259
Nabin Haitb9e04812014-09-26 14:22:18 +0530260def get_future_stock_vouchers(posting_date, posting_time, for_warehouses=None, for_items=None):
Nabin Haitbecf75d2014-03-27 17:18:29 +0530261 future_stock_vouchers = []
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530262
Anand Doshi913c51b2014-08-27 22:09:03 +0530263 values = []
Nabin Haitbecf75d2014-03-27 17:18:29 +0530264 condition = ""
265 if for_items:
Anand Doshi913c51b2014-08-27 22:09:03 +0530266 condition += " and item_code in ({})".format(", ".join(["%s"] * len(for_items)))
267 values += for_items
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530268
Nabin Haitb9e04812014-09-26 14:22:18 +0530269 if for_warehouses:
270 condition += " and warehouse in ({})".format(", ".join(["%s"] * len(for_warehouses)))
271 values += for_warehouses
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530272
273 for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
Nabin Haitbecf75d2014-03-27 17:18:29 +0530274 from `tabStock Ledger Entry` sle
Anand Doshi913c51b2014-08-27 22:09:03 +0530275 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) {condition}
276 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""".format(condition=condition),
277 tuple([posting_date, posting_time] + values), as_dict=True):
Nabin Haitbecf75d2014-03-27 17:18:29 +0530278 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530279
Nabin Haitbecf75d2014-03-27 17:18:29 +0530280 return future_stock_vouchers
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530281
Nabin Haitbecf75d2014-03-27 17:18:29 +0530282def get_voucherwise_gl_entries(future_stock_vouchers, posting_date):
283 gl_entries = {}
284 if future_stock_vouchers:
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530285 for d in frappe.db.sql("""select * from `tabGL Entry`
286 where posting_date >= %s and voucher_no in (%s)""" %
287 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
Nabin Haitbecf75d2014-03-27 17:18:29 +0530288 tuple([posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
289 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530290
Nabin Haitbecf75d2014-03-27 17:18:29 +0530291 return gl_entries
292
293def get_warehouse_account():
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530294 warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount
Nabin Haitbecf75d2014-03-27 17:18:29 +0530295 where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
Anand Doshi67d6a4e2014-03-28 13:41:59 +0530296 return warehouse_account
Nabin Haitadeb9762014-10-06 11:53:52 +0530297
298def block_negative_stock(allow_negative_stock=False):
299 if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")) and not allow_negative_stock:
300 if cint(frappe.db.get_value("Stock Settings", None, "allow_negative_stock")):
301 frappe.throw(_("Negative stock is not allowed in case of Perpetual Inventory, please disable it from Stock Settings"))
302
303def get_valuation_rate(item_code, warehouse, posting_date):
304 last_valuation_rate = frappe.db.sql("""select valuation_rate
305 from `tabStock Ledger Entry`
306 where item_code = %s and warehouse = %s
307 and ifnull(qty_after_transaction, 0) > 0 and posting_date < %s
308 order by posting_date desc limit 1""", (item_code, warehouse, posting_date))
309
310 valuation_rate = flt(last_valuation_rate[0][0]) if last_valuation_rate else 0
311
312 if not valuation_rate:
313 valuation_rate = frappe.db.get_value("Item Price", {"item_code": item_code, "buying": 1}, "price")
314
315 return valuation_rate