blob: 54b6d9f983a6cafab3983227a73ab5f91a0b07db [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 warehouse_account = dict(webnotes.conn.sql("""select master_name, name from tabAccount
95 where account_type = 'Warehouse' and ifnull(master_name, '') != ''"""))
Nabin Hait2e296fa2013-08-28 18:53:11 +053096 return warehouse_account
Nabin Hait27994c22013-08-26 16:53:30 +053097
Nabin Hait142007a2013-09-17 15:15:16 +053098 def update_gl_entries_after(self, warehouse_account=None):
Nabin Hait2e296fa2013-08-28 18:53:11 +053099 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530100 future_stock_vouchers = self.get_future_stock_vouchers()
101 gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
Nabin Hait142007a2013-09-17 15:15:16 +0530102 if not warehouse_account:
103 warehouse_account = self.get_warehouse_account()
104
Nabin Hait27994c22013-08-26 16:53:30 +0530105 for voucher_type, voucher_no in future_stock_vouchers:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530106 existing_gle = gle.get((voucher_type, voucher_no), [])
107 voucher_obj = webnotes.get_obj(voucher_type, voucher_no)
Nabin Hait142007a2013-09-17 15:15:16 +0530108 expected_gle = voucher_obj.get_gl_entries_for_stock(warehouse_account)
Nabin Hait2e296fa2013-08-28 18:53:11 +0530109
Nabin Hait27994c22013-08-26 16:53:30 +0530110 if expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530111 matched = True
Nabin Hait27994c22013-08-26 16:53:30 +0530112 if existing_gle:
Nabin Hait27994c22013-08-26 16:53:30 +0530113 for entry in expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530114 for e in existing_gle:
115 if entry.account==e.account \
116 and entry.against_account==e.against_account\
117 and entry.cost_center==e.cost_center:
118 if entry.debit != e.debit or entry.credit != e.credit:
119 matched = False
120 break
Nabin Hait27994c22013-08-26 16:53:30 +0530121 else:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530122 matched = False
123
124 if not matched:
125 self.delete_gl_entries(voucher_type, voucher_no)
126 make_gl_entries(expected_gle)
127 else:
128 self.delete_gl_entries(voucher_type, voucher_no)
Nabin Hait27994c22013-08-26 16:53:30 +0530129
130
131 def get_future_stock_vouchers(self):
132 future_stock_vouchers = []
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530133
134 if hasattr(self, "fname"):
135 item_list = [d.item_code for d in self.doclist.get({"parentfield": self.fname})]
136 condition = ''.join(['and item_code in (\'', '\', \''.join(item_list) ,'\')'])
137 else:
138 condition = ""
Nabin Hait2e296fa2013-08-28 18:53:11 +0530139
140 for d in webnotes.conn.sql("""select distinct sle.voucher_type, sle.voucher_no
141 from `tabStock Ledger Entry` sle
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530142 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
143 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
144 ('%s', '%s', condition), (self.doc.posting_date, self.doc.posting_time),
145 as_dict=True):
Nabin Hait27994c22013-08-26 16:53:30 +0530146 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Nabin Hait2e296fa2013-08-28 18:53:11 +0530147
Nabin Hait27994c22013-08-26 16:53:30 +0530148 return future_stock_vouchers
149
150 def get_voucherwise_gl_entries(self, future_stock_vouchers):
151 gl_entries = {}
152 if future_stock_vouchers:
153 for d in webnotes.conn.sql("""select * from `tabGL Entry`
154 where posting_date >= %s and voucher_no in (%s)""" %
155 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
156 tuple([self.doc.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530157 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Nabin Hait27994c22013-08-26 16:53:30 +0530158
159 return gl_entries
Nabin Hait2e296fa2013-08-28 18:53:11 +0530160
161 def delete_gl_entries(self, voucher_type, voucher_no):
162 webnotes.conn.sql("""delete from `tabGL Entry`
163 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Nabin Hait27994c22013-08-26 16:53:30 +0530164
Nabin Hait2e296fa2013-08-28 18:53:11 +0530165 def make_adjustment_entry(self, expected_gle, voucher_obj):
Nabin Haitd4741942013-08-06 15:57:25 +0530166 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530167 account_list = [d.account for d in expected_gle]
168 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
169
170 cost_center = self.get_company_default("cost_center")
171 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
172
Nabin Haitd4741942013-08-06 15:57:25 +0530173 gl_entries = []
174 for account, diff in acc_diff.items():
175 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530176 gl_entries.append([
177 # stock in hand account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530178 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530179 "account": account,
180 "against": stock_adjustment_account,
181 "debit": diff,
182 "remarks": "Adjustment Accounting Entry for Stock",
183 }),
184
185 # account against stock in hand
Nabin Hait2e296fa2013-08-28 18:53:11 +0530186 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530187 "account": stock_adjustment_account,
188 "against": account,
189 "credit": diff,
190 "cost_center": cost_center or None,
191 "remarks": "Adjustment Accounting Entry for Stock",
192 }),
193 ])
194
Nabin Haitd4741942013-08-06 15:57:25 +0530195 if gl_entries:
196 from accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530197 make_gl_entries(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530198
199 def check_expense_account(self, item):
200 if item.fields.has_key("expense_account") and not item.expense_account:
Nabin Hait678130f2013-09-02 18:10:36 +0530201 msgprint(_("""Expense/Difference account is mandatory for item: """) + item.item_code,
Nabin Hait27994c22013-08-26 16:53:30 +0530202 raise_exception=1)
203
204 if item.fields.has_key("expense_account") and not item.cost_center:
205 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
206 raise_exception=1)
Nabin Haitd4741942013-08-06 15:57:25 +0530207
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530208 def get_sl_entries(self, d, args):
209 sl_dict = {
210 "item_code": d.item_code,
211 "warehouse": d.warehouse,
212 "posting_date": self.doc.posting_date,
213 "posting_time": self.doc.posting_time,
214 "voucher_type": self.doc.doctype,
215 "voucher_no": self.doc.name,
216 "voucher_detail_no": d.name,
217 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
218 "stock_uom": d.stock_uom,
219 "incoming_rate": 0,
220 "company": self.doc.company,
221 "fiscal_year": self.doc.fiscal_year,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530222 "batch_no": cstr(d.batch_no).strip(),
223 "serial_no": d.serial_no,
Nabin Hait74c281c2013-08-19 16:17:18 +0530224 "project": d.project_name,
Nabin Hait9653f602013-08-20 15:37:33 +0530225 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530226 }
227
228 sl_dict.update(args)
229 return sl_dict
230
231 def make_sl_entries(self, sl_entries, is_amended=None):
Nabin Hait74c281c2013-08-19 16:17:18 +0530232 from stock.stock_ledger import make_sl_entries
233 make_sl_entries(sl_entries, is_amended)
234
Nabin Haitc3afb252013-03-19 12:01:24 +0530235 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530236 out = {}
237
Nabin Haitc3afb252013-03-19 12:01:24 +0530238 if not (item_list and warehouse_list):
239 item_list, warehouse_list = self.get_distinct_item_warehouse()
240
241 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530242 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +0530243 voucher_detail_no, posting_date, posting_time, stock_value,
244 warehouse, actual_qty as qty from `tabStock Ledger Entry`
Nabin Hait4ae729b2013-08-20 12:04:46 +0530245 where company = %s and item_code in (%s) and warehouse in (%s)
Nabin Haitc3afb252013-03-19 12:01:24 +0530246 order by item_code desc, warehouse desc, posting_date desc,
247 posting_time desc, name desc""" %
248 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
249 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530250
251 for r in res:
252 if (r.item_code, r.warehouse) not in out:
253 out[(r.item_code, r.warehouse)] = []
254
255 out[(r.item_code, r.warehouse)].append(r)
256
257 return out
Nabin Haitc3afb252013-03-19 12:01:24 +0530258
259 def get_distinct_item_warehouse(self):
260 item_list = []
261 warehouse_list = []
262 for item in self.doclist.get({"parentfield": self.fname}) \
263 + self.doclist.get({"parentfield": "packing_details"}):
264 item_list.append(item.item_code)
265 warehouse_list.append(item.warehouse)
266
Nabin Hait787c02e2013-03-29 16:42:33 +0530267 return list(set(item_list)), list(set(warehouse_list))
268
269 def make_cancel_gl_entries(self):
270 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
Nabin Hait4ae729b2013-08-20 12:04:46 +0530271 and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
Nabin Hait787c02e2013-03-29 16:42:33 +0530272 self.make_gl_entries()