blob: 359dc9e86ff3e6f43d6b8f2533ccea09df72f645 [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Nabin Haitc3afb252013-03-19 12:01:24 +05303
4from __future__ import unicode_literals
5import webnotes
Nabin Hait1e2f20a2013-08-02 11:42:11 +05306from webnotes.utils import cint, flt, cstr
Nabin Haitd4741942013-08-06 15:57:25 +05307from webnotes import msgprint, _
Nabin Hait787c02e2013-03-29 16:42:33 +05308import webnotes.defaults
Nabin Haitd4741942013-08-06 15:57:25 +05309
Nabin Haitc3afb252013-03-19 12:01:24 +053010from controllers.accounts_controller import AccountsController
Nabin Hait2e296fa2013-08-28 18:53:11 +053011from accounts.general_ledger import make_gl_entries, delete_gl_entries
Nabin Haitc3afb252013-03-19 12:01:24 +053012
13class StockController(AccountsController):
Nabin Hait27994c22013-08-26 16:53:30 +053014 def make_gl_entries(self):
Nabin Hait145e5e22013-10-22 23:51:41 +053015 if self.doc.docstatus == 2:
Nabin Hait2e296fa2013-08-28 18:53:11 +053016 delete_gl_entries(voucher_type=self.doc.doctype, voucher_no=self.doc.name)
Nabin Hait145e5e22013-10-22 23:51:41 +053017
18 if cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")):
19 warehouse_account = self.get_warehouse_account()
Nabin Hait2e296fa2013-08-28 18:53:11 +053020
Nabin Hait145e5e22013-10-22 23:51:41 +053021 if self.doc.docstatus==1:
22 gl_entries = self.get_gl_entries_for_stock(warehouse_account)
23 make_gl_entries(gl_entries)
24
25 self.update_gl_entries_after(warehouse_account)
Nabin Hait2e296fa2013-08-28 18:53:11 +053026
Nabin Hait142007a2013-09-17 15:15:16 +053027 def get_gl_entries_for_stock(self, warehouse_account=None, default_expense_account=None,
28 default_cost_center=None):
Nabin Hait27994c22013-08-26 16:53:30 +053029 from accounts.general_ledger import process_gl_map
Nabin Hait142007a2013-09-17 15:15:16 +053030 if not warehouse_account:
31 warehouse_account = self.get_warehouse_account()
32
Nabin Hait2e296fa2013-08-28 18:53:11 +053033 stock_ledger = self.get_stock_ledger_details()
34 voucher_details = self.get_voucher_details(stock_ledger, default_expense_account,
35 default_cost_center)
36
37 gl_list = []
Nabin Hait7a75e102013-09-17 10:21:20 +053038 warehouse_with_no_account = []
Nabin Hait2e296fa2013-08-28 18:53:11 +053039 for detail in voucher_details:
40 sle_list = stock_ledger.get(detail.name)
41 if sle_list:
42 for sle in sle_list:
43 if warehouse_account.get(sle.warehouse):
Nabin Hait2e296fa2013-08-28 18:53:11 +053044 # from warehouse account
45 gl_list.append(self.get_gl_dict({
46 "account": warehouse_account[sle.warehouse],
47 "against": detail.expense_account,
48 "cost_center": detail.cost_center,
49 "remarks": self.doc.remarks or "Accounting Entry for Stock",
50 "debit": sle.stock_value_difference
51 }))
Nabin Hait27994c22013-08-26 16:53:30 +053052
Nabin Hait2e296fa2013-08-28 18:53:11 +053053 # to target warehouse / expense account
54 gl_list.append(self.get_gl_dict({
55 "account": detail.expense_account,
56 "against": warehouse_account[sle.warehouse],
57 "cost_center": detail.cost_center,
Nabin Hait27994c22013-08-26 16:53:30 +053058 "remarks": self.doc.remarks or "Accounting Entry for Stock",
Nabin Hait2e296fa2013-08-28 18:53:11 +053059 "credit": sle.stock_value_difference
60 }))
Nabin Hait7a75e102013-09-17 10:21:20 +053061 elif sle.warehouse not in warehouse_with_no_account:
62 warehouse_with_no_account.append(sle.warehouse)
63
64 if warehouse_with_no_account:
65 msgprint(_("No accounting entries for following warehouses") + ": \n" +
66 "\n".join(warehouse_with_no_account))
Nabin Hait27994c22013-08-26 16:53:30 +053067
Nabin Hait2e296fa2013-08-28 18:53:11 +053068 return process_gl_map(gl_list)
Nabin Hait27994c22013-08-26 16:53:30 +053069
Nabin Hait2e296fa2013-08-28 18:53:11 +053070 def get_voucher_details(self, stock_ledger, default_expense_account, default_cost_center):
71 if not default_expense_account:
72 details = self.doclist.get({"parentfield": self.fname})
73 for d in details:
74 self.check_expense_account(d)
75 else:
76 details = [webnotes._dict({
77 "name":d,
78 "expense_account": default_expense_account,
79 "cost_center": default_cost_center
80 }) for d in stock_ledger.keys()]
81
82 return details
83
84 def get_stock_ledger_details(self):
85 stock_ledger = {}
Nabin Hait27994c22013-08-26 16:53:30 +053086 for sle in webnotes.conn.sql("""select warehouse, stock_value_difference, voucher_detail_no
87 from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""",
88 (self.doc.doctype, self.doc.name), as_dict=True):
Nabin Hait2e296fa2013-08-28 18:53:11 +053089 stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
90 return stock_ledger
Nabin Hait27994c22013-08-26 16:53:30 +053091
Nabin Hait2e296fa2013-08-28 18:53:11 +053092 def get_warehouse_account(self):
Nabin Hait7a75e102013-09-17 10:21:20 +053093 warehouse_account = dict(webnotes.conn.sql("""select master_name, name from tabAccount
94 where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
Nabin Hait2e296fa2013-08-28 18:53:11 +053095 return warehouse_account
Nabin Hait27994c22013-08-26 16:53:30 +053096
Nabin Hait142007a2013-09-17 15:15:16 +053097 def update_gl_entries_after(self, warehouse_account=None):
Nabin Hait27994c22013-08-26 16:53:30 +053098 future_stock_vouchers = self.get_future_stock_vouchers()
99 gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
Nabin Hait142007a2013-09-17 15:15:16 +0530100 if not warehouse_account:
101 warehouse_account = self.get_warehouse_account()
102
Nabin Hait27994c22013-08-26 16:53:30 +0530103 for voucher_type, voucher_no in future_stock_vouchers:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530104 existing_gle = gle.get((voucher_type, voucher_no), [])
105 voucher_obj = webnotes.get_obj(voucher_type, voucher_no)
Nabin Hait142007a2013-09-17 15:15:16 +0530106 expected_gle = voucher_obj.get_gl_entries_for_stock(warehouse_account)
Nabin Hait2e296fa2013-08-28 18:53:11 +0530107
Nabin Hait27994c22013-08-26 16:53:30 +0530108 if expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530109 matched = True
Nabin Hait27994c22013-08-26 16:53:30 +0530110 if existing_gle:
Nabin Hait27994c22013-08-26 16:53:30 +0530111 for entry in expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530112 for e in existing_gle:
113 if entry.account==e.account \
114 and entry.against_account==e.against_account\
115 and entry.cost_center==e.cost_center:
116 if entry.debit != e.debit or entry.credit != e.credit:
117 matched = False
118 break
Nabin Hait27994c22013-08-26 16:53:30 +0530119 else:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530120 matched = False
121
122 if not matched:
123 self.delete_gl_entries(voucher_type, voucher_no)
124 make_gl_entries(expected_gle)
125 else:
126 self.delete_gl_entries(voucher_type, voucher_no)
Nabin Hait27994c22013-08-26 16:53:30 +0530127
128
129 def get_future_stock_vouchers(self):
130 future_stock_vouchers = []
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530131
132 if hasattr(self, "fname"):
133 item_list = [d.item_code for d in self.doclist.get({"parentfield": self.fname})]
134 condition = ''.join(['and item_code in (\'', '\', \''.join(item_list) ,'\')'])
135 else:
136 condition = ""
Nabin Hait2e296fa2013-08-28 18:53:11 +0530137
138 for d in webnotes.conn.sql("""select distinct sle.voucher_type, sle.voucher_no
139 from `tabStock Ledger Entry` sle
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530140 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
141 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
142 ('%s', '%s', condition), (self.doc.posting_date, self.doc.posting_time),
143 as_dict=True):
Nabin Hait27994c22013-08-26 16:53:30 +0530144 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Nabin Hait2e296fa2013-08-28 18:53:11 +0530145
Nabin Hait27994c22013-08-26 16:53:30 +0530146 return future_stock_vouchers
147
148 def get_voucherwise_gl_entries(self, future_stock_vouchers):
149 gl_entries = {}
150 if future_stock_vouchers:
151 for d in webnotes.conn.sql("""select * from `tabGL Entry`
152 where posting_date >= %s and voucher_no in (%s)""" %
153 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
154 tuple([self.doc.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530155 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Nabin Hait27994c22013-08-26 16:53:30 +0530156
157 return gl_entries
Nabin Hait2e296fa2013-08-28 18:53:11 +0530158
159 def delete_gl_entries(self, voucher_type, voucher_no):
160 webnotes.conn.sql("""delete from `tabGL Entry`
161 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Nabin Hait27994c22013-08-26 16:53:30 +0530162
Nabin Hait2e296fa2013-08-28 18:53:11 +0530163 def make_adjustment_entry(self, expected_gle, voucher_obj):
Nabin Haitd4741942013-08-06 15:57:25 +0530164 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530165 account_list = [d.account for d in expected_gle]
166 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
167
168 cost_center = self.get_company_default("cost_center")
169 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
170
Nabin Haitd4741942013-08-06 15:57:25 +0530171 gl_entries = []
172 for account, diff in acc_diff.items():
173 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530174 gl_entries.append([
175 # stock in hand account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530176 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530177 "account": account,
178 "against": stock_adjustment_account,
179 "debit": diff,
180 "remarks": "Adjustment Accounting Entry for Stock",
181 }),
182
183 # account against stock in hand
Nabin Hait2e296fa2013-08-28 18:53:11 +0530184 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530185 "account": stock_adjustment_account,
186 "against": account,
187 "credit": diff,
188 "cost_center": cost_center or None,
189 "remarks": "Adjustment Accounting Entry for Stock",
190 }),
191 ])
192
Nabin Haitd4741942013-08-06 15:57:25 +0530193 if gl_entries:
194 from accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530195 make_gl_entries(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530196
197 def check_expense_account(self, item):
198 if item.fields.has_key("expense_account") and not item.expense_account:
Nabin Hait678130f2013-09-02 18:10:36 +0530199 msgprint(_("""Expense/Difference account is mandatory for item: """) + item.item_code,
Nabin Hait27994c22013-08-26 16:53:30 +0530200 raise_exception=1)
201
202 if item.fields.has_key("expense_account") and not item.cost_center:
203 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
204 raise_exception=1)
Nabin Haitd4741942013-08-06 15:57:25 +0530205
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530206 def get_sl_entries(self, d, args):
207 sl_dict = {
208 "item_code": d.item_code,
209 "warehouse": d.warehouse,
210 "posting_date": self.doc.posting_date,
211 "posting_time": self.doc.posting_time,
212 "voucher_type": self.doc.doctype,
213 "voucher_no": self.doc.name,
214 "voucher_detail_no": d.name,
215 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
216 "stock_uom": d.stock_uom,
217 "incoming_rate": 0,
218 "company": self.doc.company,
219 "fiscal_year": self.doc.fiscal_year,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530220 "batch_no": cstr(d.batch_no).strip(),
221 "serial_no": d.serial_no,
Nabin Hait74c281c2013-08-19 16:17:18 +0530222 "project": d.project_name,
Nabin Hait9653f602013-08-20 15:37:33 +0530223 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530224 }
225
226 sl_dict.update(args)
227 return sl_dict
228
229 def make_sl_entries(self, sl_entries, is_amended=None):
Nabin Hait74c281c2013-08-19 16:17:18 +0530230 from stock.stock_ledger import make_sl_entries
231 make_sl_entries(sl_entries, is_amended)
232
Nabin Haitc3afb252013-03-19 12:01:24 +0530233 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530234 out = {}
235
Nabin Haitc3afb252013-03-19 12:01:24 +0530236 if not (item_list and warehouse_list):
237 item_list, warehouse_list = self.get_distinct_item_warehouse()
238
239 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530240 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +0530241 voucher_detail_no, posting_date, posting_time, stock_value,
242 warehouse, actual_qty as qty from `tabStock Ledger Entry`
Nabin Hait4ae729b2013-08-20 12:04:46 +0530243 where company = %s and item_code in (%s) and warehouse in (%s)
Nabin Haitc3afb252013-03-19 12:01:24 +0530244 order by item_code desc, warehouse desc, posting_date desc,
245 posting_time desc, name desc""" %
246 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
247 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530248
249 for r in res:
250 if (r.item_code, r.warehouse) not in out:
251 out[(r.item_code, r.warehouse)] = []
252
253 out[(r.item_code, r.warehouse)].append(r)
254
255 return out
Nabin Haitc3afb252013-03-19 12:01:24 +0530256
257 def get_distinct_item_warehouse(self):
258 item_list = []
259 warehouse_list = []
260 for item in self.doclist.get({"parentfield": self.fname}) \
261 + self.doclist.get({"parentfield": "packing_details"}):
262 item_list.append(item.item_code)
263 warehouse_list.append(item.warehouse)
264
Nabin Hait787c02e2013-03-29 16:42:33 +0530265 return list(set(item_list)), list(set(warehouse_list))
266
267 def make_cancel_gl_entries(self):
268 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
Nabin Hait4ae729b2013-08-20 12:04:46 +0530269 and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
Nabin Hait787c02e2013-03-29 16:42:33 +0530270 self.make_gl_entries()