blob: 7d5d0456daa0709729677872278032f9c399c989 [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 Haitd85d63b2013-08-28 19:24:52 +053015 if not cint(webnotes.defaults.get_global_default("auto_accounting_for_stock")):
Nabin Hait27994c22013-08-26 16:53:30 +053016 return
Nabin Hait142007a2013-09-17 15:15:16 +053017
18 warehouse_account = self.get_warehouse_account()
19
Nabin Hait2e296fa2013-08-28 18:53:11 +053020 if self.doc.docstatus==1:
Nabin Hait142007a2013-09-17 15:15:16 +053021 gl_entries = self.get_gl_entries_for_stock(warehouse_account)
Nabin Hait27994c22013-08-26 16:53:30 +053022 make_gl_entries(gl_entries)
Nabin Haitbf4d27e2013-09-04 17:55:45 +053023 else:
Nabin Hait2e296fa2013-08-28 18:53:11 +053024 delete_gl_entries(voucher_type=self.doc.doctype, voucher_no=self.doc.name)
25
Nabin Hait142007a2013-09-17 15:15:16 +053026 self.update_gl_entries_after(warehouse_account)
Nabin Hait2e296fa2013-08-28 18:53:11 +053027
Nabin Hait142007a2013-09-17 15:15:16 +053028 def get_gl_entries_for_stock(self, warehouse_account=None, default_expense_account=None,
29 default_cost_center=None):
Nabin Hait27994c22013-08-26 16:53:30 +053030 from accounts.general_ledger import process_gl_map
Nabin Hait142007a2013-09-17 15:15:16 +053031 if not warehouse_account:
32 warehouse_account = self.get_warehouse_account()
33
Nabin Hait2e296fa2013-08-28 18:53:11 +053034 stock_ledger = self.get_stock_ledger_details()
35 voucher_details = self.get_voucher_details(stock_ledger, default_expense_account,
36 default_cost_center)
37
38 gl_list = []
Nabin Hait7a75e102013-09-17 10:21:20 +053039 warehouse_with_no_account = []
Nabin Hait2e296fa2013-08-28 18:53:11 +053040 for detail in voucher_details:
41 sle_list = stock_ledger.get(detail.name)
42 if sle_list:
43 for sle in sle_list:
44 if warehouse_account.get(sle.warehouse):
Nabin Hait2e296fa2013-08-28 18:53:11 +053045 # from warehouse account
46 gl_list.append(self.get_gl_dict({
47 "account": warehouse_account[sle.warehouse],
48 "against": detail.expense_account,
49 "cost_center": detail.cost_center,
50 "remarks": self.doc.remarks or "Accounting Entry for Stock",
51 "debit": sle.stock_value_difference
52 }))
Nabin Hait27994c22013-08-26 16:53:30 +053053
Nabin Hait2e296fa2013-08-28 18:53:11 +053054 # to target warehouse / expense account
55 gl_list.append(self.get_gl_dict({
56 "account": detail.expense_account,
57 "against": warehouse_account[sle.warehouse],
58 "cost_center": detail.cost_center,
Nabin Hait27994c22013-08-26 16:53:30 +053059 "remarks": self.doc.remarks or "Accounting Entry for Stock",
Nabin Hait2e296fa2013-08-28 18:53:11 +053060 "credit": sle.stock_value_difference
61 }))
Nabin Hait7a75e102013-09-17 10:21:20 +053062 elif sle.warehouse not in warehouse_with_no_account:
63 warehouse_with_no_account.append(sle.warehouse)
64
65 if warehouse_with_no_account:
66 msgprint(_("No accounting entries for following warehouses") + ": \n" +
67 "\n".join(warehouse_with_no_account))
Nabin Hait27994c22013-08-26 16:53:30 +053068
Nabin Hait2e296fa2013-08-28 18:53:11 +053069 return process_gl_map(gl_list)
Nabin Hait27994c22013-08-26 16:53:30 +053070
Nabin Hait2e296fa2013-08-28 18:53:11 +053071 def get_voucher_details(self, stock_ledger, default_expense_account, default_cost_center):
72 if not default_expense_account:
73 details = self.doclist.get({"parentfield": self.fname})
74 for d in details:
75 self.check_expense_account(d)
76 else:
77 details = [webnotes._dict({
78 "name":d,
79 "expense_account": default_expense_account,
80 "cost_center": default_cost_center
81 }) for d in stock_ledger.keys()]
82
83 return details
84
85 def get_stock_ledger_details(self):
86 stock_ledger = {}
Nabin Hait27994c22013-08-26 16:53:30 +053087 for sle in webnotes.conn.sql("""select warehouse, stock_value_difference, voucher_detail_no
88 from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""",
89 (self.doc.doctype, self.doc.name), as_dict=True):
Nabin Hait2e296fa2013-08-28 18:53:11 +053090 stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
91 return stock_ledger
Nabin Hait27994c22013-08-26 16:53:30 +053092
Nabin Hait2e296fa2013-08-28 18:53:11 +053093 def get_warehouse_account(self):
Nabin Hait7a75e102013-09-17 10:21:20 +053094 for d in webnotes.conn.sql("select name from tabWarehouse"):
95 webnotes.bean("Warehouse", d[0]).save()
96
97 warehouse_account = dict(webnotes.conn.sql("""select master_name, name from tabAccount
98 where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
Nabin Hait2e296fa2013-08-28 18:53:11 +053099 return warehouse_account
Nabin Hait27994c22013-08-26 16:53:30 +0530100
Nabin Hait142007a2013-09-17 15:15:16 +0530101 def update_gl_entries_after(self, warehouse_account=None):
Nabin Hait27994c22013-08-26 16:53:30 +0530102 future_stock_vouchers = self.get_future_stock_vouchers()
103 gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
Nabin Hait142007a2013-09-17 15:15:16 +0530104 if not warehouse_account:
105 warehouse_account = self.get_warehouse_account()
106
Nabin Hait27994c22013-08-26 16:53:30 +0530107 for voucher_type, voucher_no in future_stock_vouchers:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530108 existing_gle = gle.get((voucher_type, voucher_no), [])
109 voucher_obj = webnotes.get_obj(voucher_type, voucher_no)
Nabin Hait142007a2013-09-17 15:15:16 +0530110 expected_gle = voucher_obj.get_gl_entries_for_stock(warehouse_account)
Nabin Hait2e296fa2013-08-28 18:53:11 +0530111
Nabin Hait27994c22013-08-26 16:53:30 +0530112 if expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530113 matched = True
Nabin Hait27994c22013-08-26 16:53:30 +0530114 if existing_gle:
Nabin Hait27994c22013-08-26 16:53:30 +0530115 for entry in expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530116 for e in existing_gle:
117 if entry.account==e.account \
118 and entry.against_account==e.against_account\
119 and entry.cost_center==e.cost_center:
120 if entry.debit != e.debit or entry.credit != e.credit:
121 matched = False
122 break
Nabin Hait27994c22013-08-26 16:53:30 +0530123 else:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530124 matched = False
125
126 if not matched:
127 self.delete_gl_entries(voucher_type, voucher_no)
128 make_gl_entries(expected_gle)
129 else:
130 self.delete_gl_entries(voucher_type, voucher_no)
Nabin Hait27994c22013-08-26 16:53:30 +0530131
132
133 def get_future_stock_vouchers(self):
134 future_stock_vouchers = []
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530135
136 if hasattr(self, "fname"):
137 item_list = [d.item_code for d in self.doclist.get({"parentfield": self.fname})]
138 condition = ''.join(['and item_code in (\'', '\', \''.join(item_list) ,'\')'])
139 else:
140 condition = ""
Nabin Hait2e296fa2013-08-28 18:53:11 +0530141
142 for d in webnotes.conn.sql("""select distinct sle.voucher_type, sle.voucher_no
143 from `tabStock Ledger Entry` sle
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530144 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
145 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
146 ('%s', '%s', condition), (self.doc.posting_date, self.doc.posting_time),
147 as_dict=True):
Nabin Hait27994c22013-08-26 16:53:30 +0530148 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Nabin Hait2e296fa2013-08-28 18:53:11 +0530149
Nabin Hait27994c22013-08-26 16:53:30 +0530150 return future_stock_vouchers
151
152 def get_voucherwise_gl_entries(self, future_stock_vouchers):
153 gl_entries = {}
154 if future_stock_vouchers:
155 for d in webnotes.conn.sql("""select * from `tabGL Entry`
156 where posting_date >= %s and voucher_no in (%s)""" %
157 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
158 tuple([self.doc.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530159 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Nabin Hait27994c22013-08-26 16:53:30 +0530160
161 return gl_entries
Nabin Hait2e296fa2013-08-28 18:53:11 +0530162
163 def delete_gl_entries(self, voucher_type, voucher_no):
164 webnotes.conn.sql("""delete from `tabGL Entry`
165 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Nabin Hait27994c22013-08-26 16:53:30 +0530166
Nabin Hait2e296fa2013-08-28 18:53:11 +0530167 def make_adjustment_entry(self, expected_gle, voucher_obj):
Nabin Haitd4741942013-08-06 15:57:25 +0530168 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530169 account_list = [d.account for d in expected_gle]
170 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
171
172 cost_center = self.get_company_default("cost_center")
173 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
174
Nabin Haitd4741942013-08-06 15:57:25 +0530175 gl_entries = []
176 for account, diff in acc_diff.items():
177 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530178 gl_entries.append([
179 # stock in hand account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530180 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530181 "account": account,
182 "against": stock_adjustment_account,
183 "debit": diff,
184 "remarks": "Adjustment Accounting Entry for Stock",
185 }),
186
187 # account against stock in hand
Nabin Hait2e296fa2013-08-28 18:53:11 +0530188 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530189 "account": stock_adjustment_account,
190 "against": account,
191 "credit": diff,
192 "cost_center": cost_center or None,
193 "remarks": "Adjustment Accounting Entry for Stock",
194 }),
195 ])
196
Nabin Haitd4741942013-08-06 15:57:25 +0530197 if gl_entries:
198 from accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530199 make_gl_entries(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530200
201 def check_expense_account(self, item):
202 if item.fields.has_key("expense_account") and not item.expense_account:
Nabin Hait678130f2013-09-02 18:10:36 +0530203 msgprint(_("""Expense/Difference account is mandatory for item: """) + item.item_code,
Nabin Hait27994c22013-08-26 16:53:30 +0530204 raise_exception=1)
205
206 if item.fields.has_key("expense_account") and not item.cost_center:
207 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
208 raise_exception=1)
Nabin Haitd4741942013-08-06 15:57:25 +0530209
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530210 def get_sl_entries(self, d, args):
211 sl_dict = {
212 "item_code": d.item_code,
213 "warehouse": d.warehouse,
214 "posting_date": self.doc.posting_date,
215 "posting_time": self.doc.posting_time,
216 "voucher_type": self.doc.doctype,
217 "voucher_no": self.doc.name,
218 "voucher_detail_no": d.name,
219 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
220 "stock_uom": d.stock_uom,
221 "incoming_rate": 0,
222 "company": self.doc.company,
223 "fiscal_year": self.doc.fiscal_year,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530224 "batch_no": cstr(d.batch_no).strip(),
225 "serial_no": d.serial_no,
Nabin Hait74c281c2013-08-19 16:17:18 +0530226 "project": d.project_name,
Nabin Hait9653f602013-08-20 15:37:33 +0530227 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530228 }
229
230 sl_dict.update(args)
231 return sl_dict
232
233 def make_sl_entries(self, sl_entries, is_amended=None):
Nabin Hait74c281c2013-08-19 16:17:18 +0530234 from stock.stock_ledger import make_sl_entries
235 make_sl_entries(sl_entries, is_amended)
236
Nabin Haitc3afb252013-03-19 12:01:24 +0530237 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530238 out = {}
239
Nabin Haitc3afb252013-03-19 12:01:24 +0530240 if not (item_list and warehouse_list):
241 item_list, warehouse_list = self.get_distinct_item_warehouse()
242
243 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530244 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +0530245 voucher_detail_no, posting_date, posting_time, stock_value,
246 warehouse, actual_qty as qty from `tabStock Ledger Entry`
Nabin Hait4ae729b2013-08-20 12:04:46 +0530247 where company = %s and item_code in (%s) and warehouse in (%s)
Nabin Haitc3afb252013-03-19 12:01:24 +0530248 order by item_code desc, warehouse desc, posting_date desc,
249 posting_time desc, name desc""" %
250 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
251 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530252
253 for r in res:
254 if (r.item_code, r.warehouse) not in out:
255 out[(r.item_code, r.warehouse)] = []
256
257 out[(r.item_code, r.warehouse)].append(r)
258
259 return out
Nabin Haitc3afb252013-03-19 12:01:24 +0530260
261 def get_distinct_item_warehouse(self):
262 item_list = []
263 warehouse_list = []
264 for item in self.doclist.get({"parentfield": self.fname}) \
265 + self.doclist.get({"parentfield": "packing_details"}):
266 item_list.append(item.item_code)
267 warehouse_list.append(item.warehouse)
268
Nabin Hait787c02e2013-03-29 16:42:33 +0530269 return list(set(item_list)), list(set(warehouse_list))
270
271 def make_cancel_gl_entries(self):
272 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
Nabin Hait4ae729b2013-08-20 12:04:46 +0530273 and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
Nabin Hait787c02e2013-03-29 16:42:33 +0530274 self.make_gl_entries()