blob: d4c92a90a7ead331f18c31509034ee4d212dc1f9 [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 Hait2e296fa2013-08-28 18:53:11 +0530102 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530103 future_stock_vouchers = self.get_future_stock_vouchers()
104 gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
Nabin Hait142007a2013-09-17 15:15:16 +0530105 if not warehouse_account:
106 warehouse_account = self.get_warehouse_account()
107
Nabin Hait27994c22013-08-26 16:53:30 +0530108 for voucher_type, voucher_no in future_stock_vouchers:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530109 existing_gle = gle.get((voucher_type, voucher_no), [])
110 voucher_obj = webnotes.get_obj(voucher_type, voucher_no)
Nabin Hait142007a2013-09-17 15:15:16 +0530111 expected_gle = voucher_obj.get_gl_entries_for_stock(warehouse_account)
Nabin Hait2e296fa2013-08-28 18:53:11 +0530112
Nabin Hait27994c22013-08-26 16:53:30 +0530113 if expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530114 matched = True
Nabin Hait27994c22013-08-26 16:53:30 +0530115 if existing_gle:
Nabin Hait27994c22013-08-26 16:53:30 +0530116 for entry in expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530117 for e in existing_gle:
118 if entry.account==e.account \
119 and entry.against_account==e.against_account\
120 and entry.cost_center==e.cost_center:
121 if entry.debit != e.debit or entry.credit != e.credit:
122 matched = False
123 break
Nabin Hait27994c22013-08-26 16:53:30 +0530124 else:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530125 matched = False
126
127 if not matched:
128 self.delete_gl_entries(voucher_type, voucher_no)
129 make_gl_entries(expected_gle)
130 else:
131 self.delete_gl_entries(voucher_type, voucher_no)
Nabin Hait27994c22013-08-26 16:53:30 +0530132
133
134 def get_future_stock_vouchers(self):
135 future_stock_vouchers = []
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530136
137 if hasattr(self, "fname"):
138 item_list = [d.item_code for d in self.doclist.get({"parentfield": self.fname})]
139 condition = ''.join(['and item_code in (\'', '\', \''.join(item_list) ,'\')'])
140 else:
141 condition = ""
Nabin Hait2e296fa2013-08-28 18:53:11 +0530142
143 for d in webnotes.conn.sql("""select distinct sle.voucher_type, sle.voucher_no
144 from `tabStock Ledger Entry` sle
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530145 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
146 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
147 ('%s', '%s', condition), (self.doc.posting_date, self.doc.posting_time),
148 as_dict=True):
Nabin Hait27994c22013-08-26 16:53:30 +0530149 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Nabin Hait2e296fa2013-08-28 18:53:11 +0530150
Nabin Hait27994c22013-08-26 16:53:30 +0530151 return future_stock_vouchers
152
153 def get_voucherwise_gl_entries(self, future_stock_vouchers):
154 gl_entries = {}
155 if future_stock_vouchers:
156 for d in webnotes.conn.sql("""select * from `tabGL Entry`
157 where posting_date >= %s and voucher_no in (%s)""" %
158 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
159 tuple([self.doc.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530160 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Nabin Hait27994c22013-08-26 16:53:30 +0530161
162 return gl_entries
Nabin Hait2e296fa2013-08-28 18:53:11 +0530163
164 def delete_gl_entries(self, voucher_type, voucher_no):
165 webnotes.conn.sql("""delete from `tabGL Entry`
166 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Nabin Hait27994c22013-08-26 16:53:30 +0530167
Nabin Hait2e296fa2013-08-28 18:53:11 +0530168 def make_adjustment_entry(self, expected_gle, voucher_obj):
Nabin Haitd4741942013-08-06 15:57:25 +0530169 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530170 account_list = [d.account for d in expected_gle]
171 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
172
173 cost_center = self.get_company_default("cost_center")
174 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
175
Nabin Haitd4741942013-08-06 15:57:25 +0530176 gl_entries = []
177 for account, diff in acc_diff.items():
178 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530179 gl_entries.append([
180 # stock in hand account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530181 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530182 "account": account,
183 "against": stock_adjustment_account,
184 "debit": diff,
185 "remarks": "Adjustment Accounting Entry for Stock",
186 }),
187
188 # account against stock in hand
Nabin Hait2e296fa2013-08-28 18:53:11 +0530189 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530190 "account": stock_adjustment_account,
191 "against": account,
192 "credit": diff,
193 "cost_center": cost_center or None,
194 "remarks": "Adjustment Accounting Entry for Stock",
195 }),
196 ])
197
Nabin Haitd4741942013-08-06 15:57:25 +0530198 if gl_entries:
199 from accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530200 make_gl_entries(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530201
202 def check_expense_account(self, item):
203 if item.fields.has_key("expense_account") and not item.expense_account:
Nabin Hait678130f2013-09-02 18:10:36 +0530204 msgprint(_("""Expense/Difference account is mandatory for item: """) + item.item_code,
Nabin Hait27994c22013-08-26 16:53:30 +0530205 raise_exception=1)
206
207 if item.fields.has_key("expense_account") and not item.cost_center:
208 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
209 raise_exception=1)
Nabin Haitd4741942013-08-06 15:57:25 +0530210
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530211 def get_sl_entries(self, d, args):
212 sl_dict = {
213 "item_code": d.item_code,
214 "warehouse": d.warehouse,
215 "posting_date": self.doc.posting_date,
216 "posting_time": self.doc.posting_time,
217 "voucher_type": self.doc.doctype,
218 "voucher_no": self.doc.name,
219 "voucher_detail_no": d.name,
220 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
221 "stock_uom": d.stock_uom,
222 "incoming_rate": 0,
223 "company": self.doc.company,
224 "fiscal_year": self.doc.fiscal_year,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530225 "batch_no": cstr(d.batch_no).strip(),
226 "serial_no": d.serial_no,
Nabin Hait74c281c2013-08-19 16:17:18 +0530227 "project": d.project_name,
Nabin Hait9653f602013-08-20 15:37:33 +0530228 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530229 }
230
231 sl_dict.update(args)
232 return sl_dict
233
234 def make_sl_entries(self, sl_entries, is_amended=None):
Nabin Hait74c281c2013-08-19 16:17:18 +0530235 from stock.stock_ledger import make_sl_entries
236 make_sl_entries(sl_entries, is_amended)
237
Nabin Haitc3afb252013-03-19 12:01:24 +0530238 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530239 out = {}
240
Nabin Haitc3afb252013-03-19 12:01:24 +0530241 if not (item_list and warehouse_list):
242 item_list, warehouse_list = self.get_distinct_item_warehouse()
243
244 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530245 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +0530246 voucher_detail_no, posting_date, posting_time, stock_value,
247 warehouse, actual_qty as qty from `tabStock Ledger Entry`
Nabin Hait4ae729b2013-08-20 12:04:46 +0530248 where company = %s and item_code in (%s) and warehouse in (%s)
Nabin Haitc3afb252013-03-19 12:01:24 +0530249 order by item_code desc, warehouse desc, posting_date desc,
250 posting_time desc, name desc""" %
251 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
252 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530253
254 for r in res:
255 if (r.item_code, r.warehouse) not in out:
256 out[(r.item_code, r.warehouse)] = []
257
258 out[(r.item_code, r.warehouse)].append(r)
259
260 return out
Nabin Haitc3afb252013-03-19 12:01:24 +0530261
262 def get_distinct_item_warehouse(self):
263 item_list = []
264 warehouse_list = []
265 for item in self.doclist.get({"parentfield": self.fname}) \
266 + self.doclist.get({"parentfield": "packing_details"}):
267 item_list.append(item.item_code)
268 warehouse_list.append(item.warehouse)
269
Nabin Hait787c02e2013-03-29 16:42:33 +0530270 return list(set(item_list)), list(set(warehouse_list))
271
272 def make_cancel_gl_entries(self):
273 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
Nabin Hait4ae729b2013-08-20 12:04:46 +0530274 and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
Nabin Hait787c02e2013-03-29 16:42:33 +0530275 self.make_gl_entries()