blob: 96b8a6e8019fa646472551fe6d850a8bc00c50b9 [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
11from erpnext.accounts.general_ledger import make_gl_entries, delete_gl_entries
Nabin Haitc3afb252013-03-19 12:01:24 +053012
13class StockController(AccountsController):
Nabin Haitbecf75d2014-03-27 17:18:29 +053014 def make_gl_entries(self, repost_future_gle=True):
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 Hait145e5e22013-10-22 23:51:41 +053019 warehouse_account = self.get_warehouse_account()
Anand Doshi2ce39cf2014-04-07 18:51:58 +053020
Anand Doshif78d1ae2014-03-28 13:55:00 +053021 if self.docstatus==1:
Nabin Haite83069c2013-11-14 18:40:08 +053022 gl_entries = self.get_gl_entries(warehouse_account)
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:
26 items, warehouse_account = self.get_items_and_warehouse_accounts(warehouse_account)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053027 update_gl_entries_after(self.posting_date, self.posting_time,
Nabin Haitbecf75d2014-03-27 17:18:29 +053028 warehouse_account, items)
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 Hait142007a2013-09-17 15:15:16 +053031 default_cost_center=None):
Rushabh Mehta1f847992013-12-12 19:12:19 +053032 from erpnext.accounts.general_ledger import process_gl_map
Nabin Hait142007a2013-09-17 15:15:16 +053033 if not warehouse_account:
Nabin Haitbecf75d2014-03-27 17:18:29 +053034 warehouse_account = get_warehouse_account()
Anand Doshi2ce39cf2014-04-07 18:51:58 +053035
Anand Doshide1a97d2014-04-17 11:37:46 +053036 sle_map = self.get_stock_ledger_details()
37 voucher_details = self.get_voucher_details(default_expense_account, default_cost_center, sle_map)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053038
Nabin Hait2e296fa2013-08-28 18:53:11 +053039 gl_list = []
Nabin Hait7a75e102013-09-17 10:21:20 +053040 warehouse_with_no_account = []
Nabin Hait2e296fa2013-08-28 18:53:11 +053041 for detail in voucher_details:
Anand Doshide1a97d2014-04-17 11:37:46 +053042 sle_list = sle_map.get(detail.name)
Nabin Hait2e296fa2013-08-28 18:53:11 +053043 if sle_list:
44 for sle in sle_list:
45 if warehouse_account.get(sle.warehouse):
Nabin Hait2e296fa2013-08-28 18:53:11 +053046 # from warehouse account
Rushabh Mehta052fe822014-04-16 19:20:11 +053047
Rushabh Mehtac38fc712014-04-16 17:21:25 +053048 self.check_expense_account(detail)
49
Nabin Hait2e296fa2013-08-28 18:53:11 +053050 gl_list.append(self.get_gl_dict({
51 "account": warehouse_account[sle.warehouse],
52 "against": detail.expense_account,
53 "cost_center": detail.cost_center,
Nabin Haitdc82d4f2014-04-07 12:02:57 +053054 "remarks": self.get("remarks") or "Accounting Entry for Stock",
Nabin Hait48bd4a12013-11-18 12:42:55 +053055 "debit": flt(sle.stock_value_difference, 2)
Nabin Hait2e296fa2013-08-28 18:53:11 +053056 }))
Nabin Hait27994c22013-08-26 16:53:30 +053057
Nabin Hait2e296fa2013-08-28 18:53:11 +053058 # to target warehouse / expense account
59 gl_list.append(self.get_gl_dict({
60 "account": detail.expense_account,
61 "against": warehouse_account[sle.warehouse],
62 "cost_center": detail.cost_center,
Nabin Haitdc82d4f2014-04-07 12:02:57 +053063 "remarks": self.get("remarks") or "Accounting Entry for Stock",
Nabin Hait48bd4a12013-11-18 12:42:55 +053064 "credit": flt(sle.stock_value_difference, 2)
Nabin Hait2e296fa2013-08-28 18:53:11 +053065 }))
Nabin Hait7a75e102013-09-17 10:21:20 +053066 elif sle.warehouse not in warehouse_with_no_account:
67 warehouse_with_no_account.append(sle.warehouse)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053068
69 if warehouse_with_no_account:
Rushabh Mehta9f0d6252014-04-14 19:20:45 +053070 msgprint(_("No accounting entries for the following warehouses") + ": \n" +
Nabin Hait7a75e102013-09-17 10:21:20 +053071 "\n".join(warehouse_with_no_account))
Anand Doshi2ce39cf2014-04-07 18:51:58 +053072
Nabin Hait2e296fa2013-08-28 18:53:11 +053073 return process_gl_map(gl_list)
Anand Doshi2ce39cf2014-04-07 18:51:58 +053074
Anand Doshide1a97d2014-04-17 11:37:46 +053075 def get_voucher_details(self, default_expense_account, default_cost_center, sle_map):
76 if self.doctype == "Stock Reconciliation":
77 return [frappe._dict({ "name": voucher_detail_no, "expense_account": default_expense_account,
78 "cost_center": default_cost_center }) for voucher_detail_no, sle in sle_map.items()]
79 else:
80 details = self.get(self.fname)
Anand Doshi094610d2014-04-16 19:56:53 +053081
Anand Doshide1a97d2014-04-17 11:37:46 +053082 if default_expense_account or default_cost_center:
83 for d in details:
84 if default_expense_account and not d.get("expense_account"):
85 d.expense_account = default_expense_account
86 if default_cost_center and not d.get("cost_center"):
87 d.cost_center = default_cost_center
Anand Doshi2ce39cf2014-04-07 18:51:58 +053088
Anand Doshide1a97d2014-04-17 11:37:46 +053089 return details
Anand Doshi2ce39cf2014-04-07 18:51:58 +053090
Nabin Haitbecf75d2014-03-27 17:18:29 +053091 def get_items_and_warehouse_accounts(self, warehouse_account=None):
92 items, warehouses = [], []
93 if not warehouse_account:
94 warehouse_account = get_warehouse_account()
Anand Doshi2ce39cf2014-04-07 18:51:58 +053095
Nabin Haitbecf75d2014-03-27 17:18:29 +053096 if hasattr(self, "fname"):
Rushabh Mehtaf191f852014-04-02 18:09:34 +053097 item_doclist = self.get(self.fname)
Anand Doshif78d1ae2014-03-28 13:55:00 +053098 elif self.doctype == "Stock Reconciliation":
Nabin Haitbecf75d2014-03-27 17:18:29 +053099 import json
100 item_doclist = []
Anand Doshif78d1ae2014-03-28 13:55:00 +0530101 data = json.loads(self.reconciliation_json)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530102 for row in data[data.index(self.head_row)+1:]:
103 d = frappe._dict(zip(["item_code", "warehouse", "qty", "valuation_rate"], row))
104 item_doclist.append(d)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530105
Nabin Haitbecf75d2014-03-27 17:18:29 +0530106 if item_doclist:
107 for d in item_doclist:
108 if d.item_code and d.item_code not in items:
109 items.append(d.item_code)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530110
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530111 if d.get("warehouse") and d.warehouse not in warehouses:
Nabin Haitbecf75d2014-03-27 17:18:29 +0530112 warehouses.append(d.warehouse)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530113
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530114 if self.doctype == "Stock Entry":
115 if d.get("s_warehouse") and d.s_warehouse not in warehouses:
116 warehouses.append(d.s_warehouse)
117 if d.get("t_warehouse") and d.t_warehouse not in warehouses:
118 warehouses.append(d.t_warehouse)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530119
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530120 warehouse_account = {wh: warehouse_account[wh] for wh in warehouses
Nabin Haitbecf75d2014-03-27 17:18:29 +0530121 if warehouse_account.get(wh)}
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530122
Nabin Haitbecf75d2014-03-27 17:18:29 +0530123 return items, warehouse_account
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530124
Nabin Hait2e296fa2013-08-28 18:53:11 +0530125 def get_stock_ledger_details(self):
126 stock_ledger = {}
Anand Doshie9baaa62014-02-26 12:35:33 +0530127 for sle in frappe.db.sql("""select warehouse, stock_value_difference, voucher_detail_no
Nabin Hait27994c22013-08-26 16:53:30 +0530128 from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""",
Anand Doshif78d1ae2014-03-28 13:55:00 +0530129 (self.doctype, self.name), as_dict=True):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530130 stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
131 return stock_ledger
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530132
Nabin Hait2e296fa2013-08-28 18:53:11 +0530133 def get_warehouse_account(self):
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530134 warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount
Nabin Hait7a75e102013-09-17 10:21:20 +0530135 where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
Nabin Hait2e296fa2013-08-28 18:53:11 +0530136 return warehouse_account
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530137
Nabin Hait142007a2013-09-17 15:15:16 +0530138 def update_gl_entries_after(self, warehouse_account=None):
Nabin Hait27994c22013-08-26 16:53:30 +0530139 future_stock_vouchers = self.get_future_stock_vouchers()
140 gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
Nabin Hait142007a2013-09-17 15:15:16 +0530141 if not warehouse_account:
142 warehouse_account = self.get_warehouse_account()
Nabin Hait27994c22013-08-26 16:53:30 +0530143 for voucher_type, voucher_no in future_stock_vouchers:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530144 existing_gle = gle.get((voucher_type, voucher_no), [])
Rushabh Mehta0a0f2492014-03-31 17:27:06 +0530145 voucher_obj = frappe.get_doc(voucher_type, voucher_no)
Nabin Haite83069c2013-11-14 18:40:08 +0530146 expected_gle = voucher_obj.get_gl_entries(warehouse_account)
Nabin Hait27994c22013-08-26 16:53:30 +0530147 if expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530148 matched = True
Nabin Hait27994c22013-08-26 16:53:30 +0530149 if existing_gle:
Nabin Hait27994c22013-08-26 16:53:30 +0530150 for entry in expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530151 for e in existing_gle:
152 if entry.account==e.account \
153 and entry.against_account==e.against_account\
154 and entry.cost_center==e.cost_center:
155 if entry.debit != e.debit or entry.credit != e.credit:
156 matched = False
157 break
Nabin Hait27994c22013-08-26 16:53:30 +0530158 else:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530159 matched = False
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530160
Nabin Hait2e296fa2013-08-28 18:53:11 +0530161 if not matched:
162 self.delete_gl_entries(voucher_type, voucher_no)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530163 voucher_obj.make_gl_entries(repost_future_gle=False)
Nabin Hait2e296fa2013-08-28 18:53:11 +0530164 else:
165 self.delete_gl_entries(voucher_type, voucher_no)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530166
167
Nabin Hait27994c22013-08-26 16:53:30 +0530168 def get_future_stock_vouchers(self):
169 future_stock_vouchers = []
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530170
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530171 if hasattr(self, "fname"):
Rushabh Mehtad2b34dc2014-03-27 16:12:56 +0530172 item_list = [d.item_code for d in self.get(self.fname)]
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530173 condition = ''.join(['and item_code in (\'', '\', \''.join(item_list) ,'\')'])
174 else:
175 condition = ""
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530176
177 for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
Nabin Hait2e296fa2013-08-28 18:53:11 +0530178 from `tabStock Ledger Entry` sle
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530179 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530180 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
181 ('%s', '%s', condition), (self.posting_date, self.posting_time),
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530182 as_dict=True):
Nabin Hait27994c22013-08-26 16:53:30 +0530183 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530184
Nabin Hait27994c22013-08-26 16:53:30 +0530185 return future_stock_vouchers
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530186
Nabin Hait27994c22013-08-26 16:53:30 +0530187 def get_voucherwise_gl_entries(self, future_stock_vouchers):
188 gl_entries = {}
189 if future_stock_vouchers:
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530190 for d in frappe.db.sql("""select * from `tabGL Entry`
191 where posting_date >= %s and voucher_no in (%s)""" %
192 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
Anand Doshif78d1ae2014-03-28 13:55:00 +0530193 tuple([self.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530194 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530195
Nabin Hait27994c22013-08-26 16:53:30 +0530196 return gl_entries
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530197
Nabin Hait2e296fa2013-08-28 18:53:11 +0530198 def delete_gl_entries(self, voucher_type, voucher_no):
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530199 frappe.db.sql("""delete from `tabGL Entry`
Nabin Hait2e296fa2013-08-28 18:53:11 +0530200 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530201
Nabin Hait2e296fa2013-08-28 18:53:11 +0530202 def make_adjustment_entry(self, expected_gle, voucher_obj):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530203 from erpnext.accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530204 account_list = [d.account for d in expected_gle]
205 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530206
Nabin Hait27994c22013-08-26 16:53:30 +0530207 cost_center = self.get_company_default("cost_center")
208 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
209
Nabin Haitd4741942013-08-06 15:57:25 +0530210 gl_entries = []
211 for account, diff in acc_diff.items():
212 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530213 gl_entries.append([
214 # stock in hand account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530215 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530216 "account": account,
217 "against": stock_adjustment_account,
218 "debit": diff,
219 "remarks": "Adjustment Accounting Entry for Stock",
220 }),
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530221
Nabin Hait27994c22013-08-26 16:53:30 +0530222 # account against stock in hand
Nabin Hait2e296fa2013-08-28 18:53:11 +0530223 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530224 "account": stock_adjustment_account,
225 "against": account,
226 "credit": diff,
227 "cost_center": cost_center or None,
228 "remarks": "Adjustment Accounting Entry for Stock",
229 }),
230 ])
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530231
Nabin Haitd4741942013-08-06 15:57:25 +0530232 if gl_entries:
Rushabh Mehta1f847992013-12-12 19:12:19 +0530233 from erpnext.accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530234 make_gl_entries(gl_entries)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530235
Nabin Hait27994c22013-08-26 16:53:30 +0530236 def check_expense_account(self, item):
Rushabh Mehta052fe822014-04-16 19:20:11 +0530237 if not item.get("expense_account"):
Nabin Haitd3412542014-05-01 17:42:21 +0530238 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 +0530239
Rushabh Mehtaeea80222014-04-16 19:38:40 +0530240 if item.get("expense_account") and not item.get("cost_center"):
241 frappe.throw(_("""Cost Center is mandatory for Item {0}""").format(item.get("item_code")))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530242
243 def get_sl_entries(self, d, args):
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530244 sl_dict = {
Nabin Hait296d6262014-05-02 17:36:13 +0530245 "item_code": d.get("item_code", None),
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530246 "warehouse": d.get("warehouse", None),
Anand Doshif78d1ae2014-03-28 13:55:00 +0530247 "posting_date": self.posting_date,
248 "posting_time": self.posting_time,
249 "voucher_type": self.doctype,
250 "voucher_no": self.name,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530251 "voucher_detail_no": d.name,
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530252 "actual_qty": (self.docstatus==1 and 1 or -1)*flt(d.get("stock_qty")),
Nabin Hait296d6262014-05-02 17:36:13 +0530253 "stock_uom": d.get("stock_uom"),
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530254 "incoming_rate": 0,
Anand Doshif78d1ae2014-03-28 13:55:00 +0530255 "company": self.company,
256 "fiscal_year": self.fiscal_year,
Nabin Hait296d6262014-05-02 17:36:13 +0530257 "batch_no": cstr(d.get("batch_no")).strip(),
258 "serial_no": d.get("serial_no"),
Nabin Haitdc82d4f2014-04-07 12:02:57 +0530259 "project": d.get("project_name"),
Anand Doshif78d1ae2014-03-28 13:55:00 +0530260 "is_cancelled": self.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530261 }
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530262
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530263 sl_dict.update(args)
264 return sl_dict
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530265
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530266 def make_sl_entries(self, sl_entries, is_amended=None):
Rushabh Mehta1f847992013-12-12 19:12:19 +0530267 from erpnext.stock.stock_ledger import make_sl_entries
Nabin Hait74c281c2013-08-19 16:17:18 +0530268 make_sl_entries(sl_entries, is_amended)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530269
Nabin Hait787c02e2013-03-29 16:42:33 +0530270 def make_cancel_gl_entries(self):
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530271 if frappe.db.sql("""select name from `tabGL Entry` where voucher_type=%s
Anand Doshif78d1ae2014-03-28 13:55:00 +0530272 and voucher_no=%s""", (self.doctype, self.name)):
Nabin Haitbecf75d2014-03-27 17:18:29 +0530273 self.make_gl_entries()
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530274
Nabin Haitbecf75d2014-03-27 17:18:29 +0530275def update_gl_entries_after(posting_date, posting_time, warehouse_account=None, for_items=None):
276 def _delete_gl_entries(voucher_type, voucher_no):
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530277 frappe.db.sql("""delete from `tabGL Entry`
Nabin Haitbecf75d2014-03-27 17:18:29 +0530278 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530279
Nabin Haitbecf75d2014-03-27 17:18:29 +0530280 if not warehouse_account:
281 warehouse_account = get_warehouse_account()
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530282 future_stock_vouchers = get_future_stock_vouchers(posting_date, posting_time,
Nabin Haitbecf75d2014-03-27 17:18:29 +0530283 warehouse_account, for_items)
284 gle = get_voucherwise_gl_entries(future_stock_vouchers, posting_date)
285
286 for voucher_type, voucher_no in future_stock_vouchers:
287 existing_gle = gle.get((voucher_type, voucher_no), [])
Rushabh Mehta0a0f2492014-03-31 17:27:06 +0530288 voucher_obj = frappe.get_doc(voucher_type, voucher_no)
Nabin Haitbecf75d2014-03-27 17:18:29 +0530289 expected_gle = voucher_obj.get_gl_entries(warehouse_account)
290 if expected_gle:
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530291 if not existing_gle or not compare_existing_and_expected_gle(existing_gle,
Nabin Haitbecf75d2014-03-27 17:18:29 +0530292 expected_gle):
293 _delete_gl_entries(voucher_type, voucher_no)
294 voucher_obj.make_gl_entries(repost_future_gle=False)
295 else:
296 _delete_gl_entries(voucher_type, voucher_no)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530297
Nabin Haitbecf75d2014-03-27 17:18:29 +0530298def compare_existing_and_expected_gle(existing_gle, expected_gle):
299 matched = True
300 for entry in expected_gle:
301 for e in existing_gle:
302 if entry.account==e.account and entry.against_account==e.against_account \
303 and entry.cost_center==e.cost_center \
304 and (entry.debit != e.debit or entry.credit != e.credit):
305 matched = False
306 break
307 return matched
308
309def get_future_stock_vouchers(posting_date, posting_time, warehouse_account=None, for_items=None):
310 future_stock_vouchers = []
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530311
Nabin Haitbecf75d2014-03-27 17:18:29 +0530312 condition = ""
313 if for_items:
314 condition = ''.join([' and item_code in (\'', '\', \''.join(for_items) ,'\')'])
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530315
Nabin Haitbecf75d2014-03-27 17:18:29 +0530316 if warehouse_account:
317 condition += ''.join([' and warehouse in (\'', '\', \''.join(warehouse_account.keys()) ,'\')'])
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530318
319 for d in frappe.db.sql("""select distinct sle.voucher_type, sle.voucher_no
Nabin Haitbecf75d2014-03-27 17:18:29 +0530320 from `tabStock Ledger Entry` sle
321 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530322 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
323 ('%s', '%s', condition), (posting_date, posting_time),
Nabin Haitbecf75d2014-03-27 17:18:29 +0530324 as_dict=True):
325 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530326
Nabin Haitbecf75d2014-03-27 17:18:29 +0530327 return future_stock_vouchers
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530328
Nabin Haitbecf75d2014-03-27 17:18:29 +0530329def get_voucherwise_gl_entries(future_stock_vouchers, posting_date):
330 gl_entries = {}
331 if future_stock_vouchers:
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530332 for d in frappe.db.sql("""select * from `tabGL Entry`
333 where posting_date >= %s and voucher_no in (%s)""" %
334 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
Nabin Haitbecf75d2014-03-27 17:18:29 +0530335 tuple([posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
336 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530337
Nabin Haitbecf75d2014-03-27 17:18:29 +0530338 return gl_entries
339
340def get_warehouse_account():
Anand Doshi2ce39cf2014-04-07 18:51:58 +0530341 warehouse_account = dict(frappe.db.sql("""select master_name, name from tabAccount
Nabin Haitbecf75d2014-03-27 17:18:29 +0530342 where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
Anand Doshi67d6a4e2014-03-28 13:41:59 +0530343 return warehouse_account