2013-11-20 07:29:58 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2013-03-19 06:31:24 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import cint, flt, cstr
|
|
|
|
from frappe import msgprint, _
|
|
|
|
import frappe.defaults
|
2013-08-06 10:27:25 +00:00
|
|
|
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.controllers.accounts_controller import AccountsController
|
|
|
|
from erpnext.accounts.general_ledger import make_gl_entries, delete_gl_entries
|
2013-03-19 06:31:24 +00:00
|
|
|
|
|
|
|
class StockController(AccountsController):
|
2014-03-27 11:48:29 +00:00
|
|
|
def make_gl_entries(self, repost_future_gle=True):
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.docstatus == 2:
|
|
|
|
delete_gl_entries(voucher_type=self.doctype, voucher_no=self.name)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
|
2013-10-22 18:21:41 +00:00
|
|
|
warehouse_account = self.get_warehouse_account()
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-28 08:25:00 +00:00
|
|
|
if self.docstatus==1:
|
2013-11-14 13:10:08 +00:00
|
|
|
gl_entries = self.get_gl_entries(warehouse_account)
|
2013-10-22 18:21:41 +00:00
|
|
|
make_gl_entries(gl_entries)
|
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
if repost_future_gle:
|
|
|
|
items, warehouse_account = self.get_items_and_warehouse_accounts(warehouse_account)
|
2014-04-07 13:21:58 +00:00
|
|
|
update_gl_entries_after(self.posting_date, self.posting_time,
|
2014-03-27 11:48:29 +00:00
|
|
|
warehouse_account, items)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-11-14 13:10:08 +00:00
|
|
|
def get_gl_entries(self, warehouse_account=None, default_expense_account=None,
|
2013-09-17 09:45:16 +00:00
|
|
|
default_cost_center=None):
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.accounts.general_ledger import process_gl_map
|
2013-09-17 09:45:16 +00:00
|
|
|
if not warehouse_account:
|
2014-03-27 11:48:29 +00:00
|
|
|
warehouse_account = get_warehouse_account()
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
stock_ledger = self.get_stock_ledger_details()
|
2014-04-07 13:21:58 +00:00
|
|
|
voucher_details = self.get_voucher_details(stock_ledger, default_expense_account,
|
2013-08-28 13:23:11 +00:00
|
|
|
default_cost_center)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
gl_list = []
|
2013-09-17 04:51:20 +00:00
|
|
|
warehouse_with_no_account = []
|
2013-08-28 13:23:11 +00:00
|
|
|
for detail in voucher_details:
|
|
|
|
sle_list = stock_ledger.get(detail.name)
|
|
|
|
if sle_list:
|
|
|
|
for sle in sle_list:
|
|
|
|
if warehouse_account.get(sle.warehouse):
|
|
|
|
# from warehouse account
|
2014-04-16 11:51:25 +00:00
|
|
|
self.check_expense_account(detail)
|
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
gl_list.append(self.get_gl_dict({
|
|
|
|
"account": warehouse_account[sle.warehouse],
|
|
|
|
"against": detail.expense_account,
|
|
|
|
"cost_center": detail.cost_center,
|
2014-04-07 06:32:57 +00:00
|
|
|
"remarks": self.get("remarks") or "Accounting Entry for Stock",
|
2013-11-18 07:12:55 +00:00
|
|
|
"debit": flt(sle.stock_value_difference, 2)
|
2013-08-28 13:23:11 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
# to target warehouse / expense account
|
|
|
|
gl_list.append(self.get_gl_dict({
|
|
|
|
"account": detail.expense_account,
|
|
|
|
"against": warehouse_account[sle.warehouse],
|
|
|
|
"cost_center": detail.cost_center,
|
2014-04-07 06:32:57 +00:00
|
|
|
"remarks": self.get("remarks") or "Accounting Entry for Stock",
|
2013-11-18 07:12:55 +00:00
|
|
|
"credit": flt(sle.stock_value_difference, 2)
|
2013-08-28 13:23:11 +00:00
|
|
|
}))
|
2013-09-17 04:51:20 +00:00
|
|
|
elif sle.warehouse not in warehouse_with_no_account:
|
|
|
|
warehouse_with_no_account.append(sle.warehouse)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
|
|
|
if warehouse_with_no_account:
|
2014-04-14 13:50:45 +00:00
|
|
|
msgprint(_("No accounting entries for the following warehouses") + ": \n" +
|
2013-09-17 04:51:20 +00:00
|
|
|
"\n".join(warehouse_with_no_account))
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
return process_gl_map(gl_list)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
def get_voucher_details(self, stock_ledger, default_expense_account, default_cost_center):
|
|
|
|
if not default_expense_account:
|
2014-03-27 10:42:56 +00:00
|
|
|
details = self.get(self.fname)
|
2013-08-28 13:23:11 +00:00
|
|
|
else:
|
2014-02-14 10:17:51 +00:00
|
|
|
details = [frappe._dict({
|
2014-04-07 13:21:58 +00:00
|
|
|
"name":d,
|
|
|
|
"expense_account": default_expense_account,
|
2013-08-28 13:23:11 +00:00
|
|
|
"cost_center": default_cost_center
|
|
|
|
}) for d in stock_ledger.keys()]
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
return details
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
def get_items_and_warehouse_accounts(self, warehouse_account=None):
|
|
|
|
items, warehouses = [], []
|
|
|
|
if not warehouse_account:
|
|
|
|
warehouse_account = get_warehouse_account()
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
if hasattr(self, "fname"):
|
2014-04-02 12:39:34 +00:00
|
|
|
item_doclist = self.get(self.fname)
|
2014-03-28 08:25:00 +00:00
|
|
|
elif self.doctype == "Stock Reconciliation":
|
2014-03-27 11:48:29 +00:00
|
|
|
import json
|
|
|
|
item_doclist = []
|
2014-03-28 08:25:00 +00:00
|
|
|
data = json.loads(self.reconciliation_json)
|
2014-03-27 11:48:29 +00:00
|
|
|
for row in data[data.index(self.head_row)+1:]:
|
|
|
|
d = frappe._dict(zip(["item_code", "warehouse", "qty", "valuation_rate"], row))
|
|
|
|
item_doclist.append(d)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
if item_doclist:
|
|
|
|
for d in item_doclist:
|
|
|
|
if d.item_code and d.item_code not in items:
|
|
|
|
items.append(d.item_code)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-04-07 06:32:57 +00:00
|
|
|
if d.get("warehouse") and d.warehouse not in warehouses:
|
2014-03-27 11:48:29 +00:00
|
|
|
warehouses.append(d.warehouse)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-04-07 06:32:57 +00:00
|
|
|
if self.doctype == "Stock Entry":
|
|
|
|
if d.get("s_warehouse") and d.s_warehouse not in warehouses:
|
|
|
|
warehouses.append(d.s_warehouse)
|
|
|
|
if d.get("t_warehouse") and d.t_warehouse not in warehouses:
|
|
|
|
warehouses.append(d.t_warehouse)
|
2014-03-27 11:48:29 +00:00
|
|
|
|
2014-04-07 13:21:58 +00:00
|
|
|
warehouse_account = {wh: warehouse_account[wh] for wh in warehouses
|
2014-03-27 11:48:29 +00:00
|
|
|
if warehouse_account.get(wh)}
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
return items, warehouse_account
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
def get_stock_ledger_details(self):
|
|
|
|
stock_ledger = {}
|
2014-02-26 07:05:33 +00:00
|
|
|
for sle in frappe.db.sql("""select warehouse, stock_value_difference, voucher_detail_no
|
2013-08-26 11:23:30 +00:00
|
|
|
from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""",
|
2014-03-28 08:25:00 +00:00
|
|
|
(self.doctype, self.name), as_dict=True):
|
2013-08-28 13:23:11 +00:00
|
|
|
stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
|
|
|
|
return stock_ledger
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
def get_warehouse_account(self):
|
2014-04-07 13:21:58 +00:00
|
|
|
warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount
|
2013-09-17 04:51:20 +00:00
|
|
|
where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
|
2013-08-28 13:23:11 +00:00
|
|
|
return warehouse_account
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-09-17 09:45:16 +00:00
|
|
|
def update_gl_entries_after(self, warehouse_account=None):
|
2013-08-26 11:23:30 +00:00
|
|
|
future_stock_vouchers = self.get_future_stock_vouchers()
|
|
|
|
gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
|
2013-09-17 09:45:16 +00:00
|
|
|
if not warehouse_account:
|
|
|
|
warehouse_account = self.get_warehouse_account()
|
2013-08-26 11:23:30 +00:00
|
|
|
for voucher_type, voucher_no in future_stock_vouchers:
|
2013-08-28 13:23:11 +00:00
|
|
|
existing_gle = gle.get((voucher_type, voucher_no), [])
|
2014-03-31 11:57:06 +00:00
|
|
|
voucher_obj = frappe.get_doc(voucher_type, voucher_no)
|
2013-11-14 13:10:08 +00:00
|
|
|
expected_gle = voucher_obj.get_gl_entries(warehouse_account)
|
2013-08-26 11:23:30 +00:00
|
|
|
if expected_gle:
|
2013-08-28 13:23:11 +00:00
|
|
|
matched = True
|
2013-08-26 11:23:30 +00:00
|
|
|
if existing_gle:
|
|
|
|
for entry in expected_gle:
|
2013-08-28 13:23:11 +00:00
|
|
|
for e in existing_gle:
|
|
|
|
if entry.account==e.account \
|
|
|
|
and entry.against_account==e.against_account\
|
|
|
|
and entry.cost_center==e.cost_center:
|
|
|
|
if entry.debit != e.debit or entry.credit != e.credit:
|
|
|
|
matched = False
|
|
|
|
break
|
2013-08-26 11:23:30 +00:00
|
|
|
else:
|
2013-08-28 13:23:11 +00:00
|
|
|
matched = False
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
if not matched:
|
|
|
|
self.delete_gl_entries(voucher_type, voucher_no)
|
2014-03-27 11:48:29 +00:00
|
|
|
voucher_obj.make_gl_entries(repost_future_gle=False)
|
2013-08-28 13:23:11 +00:00
|
|
|
else:
|
|
|
|
self.delete_gl_entries(voucher_type, voucher_no)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
def get_future_stock_vouchers(self):
|
|
|
|
future_stock_vouchers = []
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-09-04 12:25:45 +00:00
|
|
|
if hasattr(self, "fname"):
|
2014-03-27 10:42:56 +00:00
|
|
|
item_list = [d.item_code for d in self.get(self.fname)]
|
2013-09-04 12:25:45 +00:00
|
|
|
condition = ''.join(['and item_code in (\'', '\', \''.join(item_list) ,'\')'])
|
|
|
|
else:
|
|
|
|
condition = ""
|
2014-04-07 13:21:58 +00:00
|
|
|
|
|
|
|
for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
|
2013-08-28 13:23:11 +00:00
|
|
|
from `tabStock Ledger Entry` sle
|
2013-09-04 12:25:45 +00:00
|
|
|
where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
|
2014-04-07 13:21:58 +00:00
|
|
|
order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
|
|
|
|
('%s', '%s', condition), (self.posting_date, self.posting_time),
|
2013-09-04 12:25:45 +00:00
|
|
|
as_dict=True):
|
2013-08-26 11:23:30 +00:00
|
|
|
future_stock_vouchers.append([d.voucher_type, d.voucher_no])
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
return future_stock_vouchers
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
def get_voucherwise_gl_entries(self, future_stock_vouchers):
|
|
|
|
gl_entries = {}
|
|
|
|
if future_stock_vouchers:
|
2014-04-07 13:21:58 +00:00
|
|
|
for d in frappe.db.sql("""select * from `tabGL Entry`
|
|
|
|
where posting_date >= %s and voucher_no in (%s)""" %
|
|
|
|
('%s', ', '.join(['%s']*len(future_stock_vouchers))),
|
2014-03-28 08:25:00 +00:00
|
|
|
tuple([self.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
|
2013-08-28 13:23:11 +00:00
|
|
|
gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
return gl_entries
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
def delete_gl_entries(self, voucher_type, voucher_no):
|
2014-04-07 13:21:58 +00:00
|
|
|
frappe.db.sql("""delete from `tabGL Entry`
|
2013-08-28 13:23:11 +00:00
|
|
|
where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-28 13:23:11 +00:00
|
|
|
def make_adjustment_entry(self, expected_gle, voucher_obj):
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.accounts.utils import get_stock_and_account_difference
|
2013-08-26 11:23:30 +00:00
|
|
|
account_list = [d.account for d in expected_gle]
|
|
|
|
acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
cost_center = self.get_company_default("cost_center")
|
|
|
|
stock_adjustment_account = self.get_company_default("stock_adjustment_account")
|
|
|
|
|
2013-08-06 10:27:25 +00:00
|
|
|
gl_entries = []
|
|
|
|
for account, diff in acc_diff.items():
|
|
|
|
if diff:
|
2013-08-26 11:23:30 +00:00
|
|
|
gl_entries.append([
|
|
|
|
# stock in hand account
|
2013-08-28 13:23:11 +00:00
|
|
|
voucher_obj.get_gl_dict({
|
2013-08-26 11:23:30 +00:00
|
|
|
"account": account,
|
|
|
|
"against": stock_adjustment_account,
|
|
|
|
"debit": diff,
|
|
|
|
"remarks": "Adjustment Accounting Entry for Stock",
|
|
|
|
}),
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
# account against stock in hand
|
2013-08-28 13:23:11 +00:00
|
|
|
voucher_obj.get_gl_dict({
|
2013-08-26 11:23:30 +00:00
|
|
|
"account": stock_adjustment_account,
|
|
|
|
"against": account,
|
|
|
|
"credit": diff,
|
|
|
|
"cost_center": cost_center or None,
|
|
|
|
"remarks": "Adjustment Accounting Entry for Stock",
|
|
|
|
}),
|
|
|
|
])
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-06 10:27:25 +00:00
|
|
|
if gl_entries:
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.accounts.general_ledger import make_gl_entries
|
2013-08-06 10:27:25 +00:00
|
|
|
make_gl_entries(gl_entries)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-26 11:23:30 +00:00
|
|
|
def check_expense_account(self, item):
|
2014-04-03 12:08:54 +00:00
|
|
|
if item.meta.get_field("expense_account") and not item.expense_account:
|
2014-04-16 11:51:25 +00:00
|
|
|
frappe.throw(_("Expense or Difference account is mandatory for Item {0} as there is difference in value").format(item.item_code))
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-04-16 11:51:25 +00:00
|
|
|
if getattr(item, "expense_account", None) and not item.cost_center:
|
|
|
|
frappe.throw(_("""Cost Center is mandatory for Item {0}""").format(item.item_code))
|
2014-04-07 13:21:58 +00:00
|
|
|
|
|
|
|
def get_sl_entries(self, d, args):
|
2013-08-02 06:12:11 +00:00
|
|
|
sl_dict = {
|
|
|
|
"item_code": d.item_code,
|
2014-04-07 06:32:57 +00:00
|
|
|
"warehouse": d.get("warehouse", None),
|
2014-03-28 08:25:00 +00:00
|
|
|
"posting_date": self.posting_date,
|
|
|
|
"posting_time": self.posting_time,
|
|
|
|
"voucher_type": self.doctype,
|
|
|
|
"voucher_no": self.name,
|
2013-08-02 06:12:11 +00:00
|
|
|
"voucher_detail_no": d.name,
|
2014-04-07 06:32:57 +00:00
|
|
|
"actual_qty": (self.docstatus==1 and 1 or -1)*flt(d.get("stock_qty")),
|
2013-08-02 06:12:11 +00:00
|
|
|
"stock_uom": d.stock_uom,
|
|
|
|
"incoming_rate": 0,
|
2014-03-28 08:25:00 +00:00
|
|
|
"company": self.company,
|
|
|
|
"fiscal_year": self.fiscal_year,
|
2013-08-02 06:12:11 +00:00
|
|
|
"batch_no": cstr(d.batch_no).strip(),
|
|
|
|
"serial_no": d.serial_no,
|
2014-04-07 06:32:57 +00:00
|
|
|
"project": d.get("project_name"),
|
2014-03-28 08:25:00 +00:00
|
|
|
"is_cancelled": self.docstatus==2 and "Yes" or "No"
|
2013-08-02 06:12:11 +00:00
|
|
|
}
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-02 06:12:11 +00:00
|
|
|
sl_dict.update(args)
|
|
|
|
return sl_dict
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2013-08-02 06:12:11 +00:00
|
|
|
def make_sl_entries(self, sl_entries, is_amended=None):
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.stock.stock_ledger import make_sl_entries
|
2013-08-19 10:47:18 +00:00
|
|
|
make_sl_entries(sl_entries, is_amended)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
def make_cancel_gl_entries(self):
|
2014-04-07 13:21:58 +00:00
|
|
|
if frappe.db.sql("""select name from `tabGL Entry` where voucher_type=%s
|
2014-03-28 08:25:00 +00:00
|
|
|
and voucher_no=%s""", (self.doctype, self.name)):
|
2014-03-27 11:48:29 +00:00
|
|
|
self.make_gl_entries()
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
def update_gl_entries_after(posting_date, posting_time, warehouse_account=None, for_items=None):
|
|
|
|
def _delete_gl_entries(voucher_type, voucher_no):
|
2014-04-07 13:21:58 +00:00
|
|
|
frappe.db.sql("""delete from `tabGL Entry`
|
2014-03-27 11:48:29 +00:00
|
|
|
where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
if not warehouse_account:
|
|
|
|
warehouse_account = get_warehouse_account()
|
2014-04-07 13:21:58 +00:00
|
|
|
future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time,
|
2014-03-27 11:48:29 +00:00
|
|
|
warehouse_account, for_items)
|
|
|
|
gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date)
|
2013-08-07 13:57:30 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
for voucher_type, voucher_no in future_stock_vouchers:
|
|
|
|
existing_gle = gle.get((voucher_type, voucher_no), [])
|
2014-03-31 11:57:06 +00:00
|
|
|
voucher_obj = frappe.get_doc(voucher_type, voucher_no)
|
2014-03-27 11:48:29 +00:00
|
|
|
expected_gle = voucher_obj.get_gl_entries(warehouse_account)
|
|
|
|
if expected_gle:
|
2014-04-07 13:21:58 +00:00
|
|
|
if not existing_gle or not compare_existing_and_expected_gle(existing_gle,
|
2014-03-27 11:48:29 +00:00
|
|
|
expected_gle):
|
|
|
|
_delete_gl_entries(voucher_type, voucher_no)
|
|
|
|
voucher_obj.make_gl_entries(repost_future_gle=False)
|
|
|
|
else:
|
|
|
|
_delete_gl_entries(voucher_type, voucher_no)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
def compare_existing_and_expected_gle(existing_gle, expected_gle):
|
|
|
|
matched = True
|
|
|
|
for entry in expected_gle:
|
|
|
|
for e in existing_gle:
|
|
|
|
if entry.account==e.account and entry.against_account==e.against_account \
|
|
|
|
and entry.cost_center==e.cost_center \
|
|
|
|
and (entry.debit != e.debit or entry.credit != e.credit):
|
|
|
|
matched = False
|
|
|
|
break
|
|
|
|
return matched
|
2013-03-19 06:31:24 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
def get_future_stock_vouchers(posting_date, posting_time, warehouse_account=None, for_items=None):
|
|
|
|
future_stock_vouchers = []
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
condition = ""
|
|
|
|
if for_items:
|
|
|
|
condition = ''.join([' and item_code in (\'', '\', \''.join(for_items) ,'\')'])
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
if warehouse_account:
|
|
|
|
condition += ''.join([' and warehouse in (\'', '\', \''.join(warehouse_account.keys()) ,'\')'])
|
2014-04-07 13:21:58 +00:00
|
|
|
|
|
|
|
for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
|
2014-03-27 11:48:29 +00:00
|
|
|
from `tabStock Ledger Entry` sle
|
|
|
|
where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
|
2014-04-07 13:21:58 +00:00
|
|
|
order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
|
|
|
|
('%s', '%s', condition), (posting_date, posting_time),
|
2014-03-27 11:48:29 +00:00
|
|
|
as_dict=True):
|
|
|
|
future_stock_vouchers.append([d.voucher_type, d.voucher_no])
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
return future_stock_vouchers
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
def get_voucherwise_gl_entries(future_stock_vouchers, posting_date):
|
|
|
|
gl_entries = {}
|
|
|
|
if future_stock_vouchers:
|
2014-04-07 13:21:58 +00:00
|
|
|
for d in frappe.db.sql("""select * from `tabGL Entry`
|
|
|
|
where posting_date >= %s and voucher_no in (%s)""" %
|
|
|
|
('%s', ', '.join(['%s']*len(future_stock_vouchers))),
|
2014-03-27 11:48:29 +00:00
|
|
|
tuple([posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
|
|
|
|
gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
|
2014-04-07 13:21:58 +00:00
|
|
|
|
2014-03-27 11:48:29 +00:00
|
|
|
return gl_entries
|
|
|
|
|
|
|
|
def get_warehouse_account():
|
2014-04-07 13:21:58 +00:00
|
|
|
warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount
|
2014-03-27 11:48:29 +00:00
|
|
|
where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
|
2014-03-28 08:11:59 +00:00
|
|
|
return warehouse_account
|