blob: 9adefb0d0b33aafabd02cff38935ac294a16a18c [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 Haitbf4d27e2013-09-04 17:55:45 +053017
Nabin Hait2e296fa2013-08-28 18:53:11 +053018 if self.doc.docstatus==1:
Nabin Haitbf4d27e2013-09-04 17:55:45 +053019 gl_entries = self.get_gl_entries_for_stock()
Nabin Hait27994c22013-08-26 16:53:30 +053020 make_gl_entries(gl_entries)
Nabin Haitbf4d27e2013-09-04 17:55:45 +053021 else:
Nabin Hait2e296fa2013-08-28 18:53:11 +053022 delete_gl_entries(voucher_type=self.doc.doctype, voucher_no=self.doc.name)
23
Nabin Hait27994c22013-08-26 16:53:30 +053024 self.update_gl_entries_after()
Nabin Hait2e296fa2013-08-28 18:53:11 +053025
26 def get_gl_entries_for_stock(self, default_expense_account=None, default_cost_center=None):
Nabin Hait27994c22013-08-26 16:53:30 +053027 from accounts.general_ledger import process_gl_map
Nabin Hait2e296fa2013-08-28 18:53:11 +053028 warehouse_account = self.get_warehouse_account()
29 stock_ledger = self.get_stock_ledger_details()
30 voucher_details = self.get_voucher_details(stock_ledger, default_expense_account,
31 default_cost_center)
32
33 gl_list = []
34 for detail in voucher_details:
35 sle_list = stock_ledger.get(detail.name)
36 if sle_list:
37 for sle in sle_list:
38 if warehouse_account.get(sle.warehouse):
39
40 # from warehouse account
41 gl_list.append(self.get_gl_dict({
42 "account": warehouse_account[sle.warehouse],
43 "against": detail.expense_account,
44 "cost_center": detail.cost_center,
45 "remarks": self.doc.remarks or "Accounting Entry for Stock",
46 "debit": sle.stock_value_difference
47 }))
Nabin Hait27994c22013-08-26 16:53:30 +053048
Nabin Hait2e296fa2013-08-28 18:53:11 +053049 # to target warehouse / expense account
50 gl_list.append(self.get_gl_dict({
51 "account": detail.expense_account,
52 "against": warehouse_account[sle.warehouse],
53 "cost_center": detail.cost_center,
Nabin Hait27994c22013-08-26 16:53:30 +053054 "remarks": self.doc.remarks or "Accounting Entry for Stock",
Nabin Hait2e296fa2013-08-28 18:53:11 +053055 "credit": sle.stock_value_difference
56 }))
Nabin Hait27994c22013-08-26 16:53:30 +053057
Nabin Hait2e296fa2013-08-28 18:53:11 +053058 return process_gl_map(gl_list)
Nabin Hait27994c22013-08-26 16:53:30 +053059
Nabin Hait2e296fa2013-08-28 18:53:11 +053060 def get_voucher_details(self, stock_ledger, default_expense_account, default_cost_center):
61 if not default_expense_account:
62 details = self.doclist.get({"parentfield": self.fname})
63 for d in details:
64 self.check_expense_account(d)
65 else:
66 details = [webnotes._dict({
67 "name":d,
68 "expense_account": default_expense_account,
69 "cost_center": default_cost_center
70 }) for d in stock_ledger.keys()]
71
72 return details
73
74 def get_stock_ledger_details(self):
75 stock_ledger = {}
Nabin Hait27994c22013-08-26 16:53:30 +053076 for sle in webnotes.conn.sql("""select warehouse, stock_value_difference, voucher_detail_no
77 from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""",
78 (self.doc.doctype, self.doc.name), as_dict=True):
Nabin Hait2e296fa2013-08-28 18:53:11 +053079 stock_ledger.setdefault(sle.voucher_detail_no, []).append(sle)
80 return stock_ledger
Nabin Hait27994c22013-08-26 16:53:30 +053081
Nabin Hait2e296fa2013-08-28 18:53:11 +053082 def get_warehouse_account(self):
83 warehouse_account = dict(webnotes.conn.sql("""select name, account from `tabWarehouse`
84 where ifnull(account, '') != ''"""))
Nabin Hait27994c22013-08-26 16:53:30 +053085
Nabin Hait2e296fa2013-08-28 18:53:11 +053086 return warehouse_account
Nabin Hait27994c22013-08-26 16:53:30 +053087
88 def update_gl_entries_after(self):
Nabin Hait2e296fa2013-08-28 18:53:11 +053089 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +053090 future_stock_vouchers = self.get_future_stock_vouchers()
91 gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
92 for voucher_type, voucher_no in future_stock_vouchers:
Nabin Hait2e296fa2013-08-28 18:53:11 +053093 existing_gle = gle.get((voucher_type, voucher_no), [])
94 voucher_obj = webnotes.get_obj(voucher_type, voucher_no)
95 expected_gle = voucher_obj.get_gl_entries_for_stock()
96
Nabin Hait27994c22013-08-26 16:53:30 +053097 if expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +053098 matched = True
Nabin Hait27994c22013-08-26 16:53:30 +053099 if existing_gle:
Nabin Hait27994c22013-08-26 16:53:30 +0530100 for entry in expected_gle:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530101 for e in existing_gle:
102 if entry.account==e.account \
103 and entry.against_account==e.against_account\
104 and entry.cost_center==e.cost_center:
105 if entry.debit != e.debit or entry.credit != e.credit:
106 matched = False
107 break
Nabin Hait27994c22013-08-26 16:53:30 +0530108 else:
Nabin Hait2e296fa2013-08-28 18:53:11 +0530109 matched = False
110
111 if not matched:
112 self.delete_gl_entries(voucher_type, voucher_no)
113 make_gl_entries(expected_gle)
114 else:
115 self.delete_gl_entries(voucher_type, voucher_no)
Nabin Hait27994c22013-08-26 16:53:30 +0530116
117
118 def get_future_stock_vouchers(self):
119 future_stock_vouchers = []
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530120
121 if hasattr(self, "fname"):
122 item_list = [d.item_code for d in self.doclist.get({"parentfield": self.fname})]
123 condition = ''.join(['and item_code in (\'', '\', \''.join(item_list) ,'\')'])
124 else:
125 condition = ""
Nabin Hait2e296fa2013-08-28 18:53:11 +0530126
127 for d in webnotes.conn.sql("""select distinct sle.voucher_type, sle.voucher_no
128 from `tabStock Ledger Entry` sle
Nabin Haitbf4d27e2013-09-04 17:55:45 +0530129 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) %s
130 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""" %
131 ('%s', '%s', condition), (self.doc.posting_date, self.doc.posting_time),
132 as_dict=True):
Nabin Hait27994c22013-08-26 16:53:30 +0530133 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Nabin Hait2e296fa2013-08-28 18:53:11 +0530134
Nabin Hait27994c22013-08-26 16:53:30 +0530135 return future_stock_vouchers
136
137 def get_voucherwise_gl_entries(self, future_stock_vouchers):
138 gl_entries = {}
139 if future_stock_vouchers:
140 for d in webnotes.conn.sql("""select * from `tabGL Entry`
141 where posting_date >= %s and voucher_no in (%s)""" %
142 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
143 tuple([self.doc.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530144 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Nabin Hait27994c22013-08-26 16:53:30 +0530145
146 return gl_entries
Nabin Hait2e296fa2013-08-28 18:53:11 +0530147
148 def delete_gl_entries(self, voucher_type, voucher_no):
149 webnotes.conn.sql("""delete from `tabGL Entry`
150 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Nabin Hait27994c22013-08-26 16:53:30 +0530151
Nabin Hait2e296fa2013-08-28 18:53:11 +0530152 def make_adjustment_entry(self, expected_gle, voucher_obj):
Nabin Haitd4741942013-08-06 15:57:25 +0530153 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530154 account_list = [d.account for d in expected_gle]
155 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
156
157 cost_center = self.get_company_default("cost_center")
158 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
159
Nabin Haitd4741942013-08-06 15:57:25 +0530160 gl_entries = []
161 for account, diff in acc_diff.items():
162 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530163 gl_entries.append([
164 # stock in hand account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530165 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530166 "account": account,
167 "against": stock_adjustment_account,
168 "debit": diff,
169 "remarks": "Adjustment Accounting Entry for Stock",
170 }),
171
172 # account against stock in hand
Nabin Hait2e296fa2013-08-28 18:53:11 +0530173 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530174 "account": stock_adjustment_account,
175 "against": account,
176 "credit": diff,
177 "cost_center": cost_center or None,
178 "remarks": "Adjustment Accounting Entry for Stock",
179 }),
180 ])
181
Nabin Haitd4741942013-08-06 15:57:25 +0530182 if gl_entries:
183 from accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530184 make_gl_entries(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530185
186 def check_expense_account(self, item):
187 if item.fields.has_key("expense_account") and not item.expense_account:
Nabin Hait678130f2013-09-02 18:10:36 +0530188 msgprint(_("""Expense/Difference account is mandatory for item: """) + item.item_code,
Nabin Hait27994c22013-08-26 16:53:30 +0530189 raise_exception=1)
190
191 if item.fields.has_key("expense_account") and not item.cost_center:
192 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
193 raise_exception=1)
Nabin Haitd4741942013-08-06 15:57:25 +0530194
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530195 def get_sl_entries(self, d, args):
196 sl_dict = {
197 "item_code": d.item_code,
198 "warehouse": d.warehouse,
199 "posting_date": self.doc.posting_date,
200 "posting_time": self.doc.posting_time,
201 "voucher_type": self.doc.doctype,
202 "voucher_no": self.doc.name,
203 "voucher_detail_no": d.name,
204 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
205 "stock_uom": d.stock_uom,
206 "incoming_rate": 0,
207 "company": self.doc.company,
208 "fiscal_year": self.doc.fiscal_year,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530209 "batch_no": cstr(d.batch_no).strip(),
210 "serial_no": d.serial_no,
Nabin Hait74c281c2013-08-19 16:17:18 +0530211 "project": d.project_name,
Nabin Hait9653f602013-08-20 15:37:33 +0530212 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530213 }
214
215 sl_dict.update(args)
216 return sl_dict
217
218 def make_sl_entries(self, sl_entries, is_amended=None):
Nabin Hait74c281c2013-08-19 16:17:18 +0530219 from stock.stock_ledger import make_sl_entries
220 make_sl_entries(sl_entries, is_amended)
221
Nabin Haitc3afb252013-03-19 12:01:24 +0530222 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530223 out = {}
224
Nabin Haitc3afb252013-03-19 12:01:24 +0530225 if not (item_list and warehouse_list):
226 item_list, warehouse_list = self.get_distinct_item_warehouse()
227
228 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530229 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +0530230 voucher_detail_no, posting_date, posting_time, stock_value,
231 warehouse, actual_qty as qty from `tabStock Ledger Entry`
Nabin Hait4ae729b2013-08-20 12:04:46 +0530232 where company = %s and item_code in (%s) and warehouse in (%s)
Nabin Haitc3afb252013-03-19 12:01:24 +0530233 order by item_code desc, warehouse desc, posting_date desc,
234 posting_time desc, name desc""" %
235 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
236 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530237
238 for r in res:
239 if (r.item_code, r.warehouse) not in out:
240 out[(r.item_code, r.warehouse)] = []
241
242 out[(r.item_code, r.warehouse)].append(r)
243
244 return out
Nabin Haitc3afb252013-03-19 12:01:24 +0530245
246 def get_distinct_item_warehouse(self):
247 item_list = []
248 warehouse_list = []
249 for item in self.doclist.get({"parentfield": self.fname}) \
250 + self.doclist.get({"parentfield": "packing_details"}):
251 item_list.append(item.item_code)
252 warehouse_list.append(item.warehouse)
253
Nabin Hait787c02e2013-03-29 16:42:33 +0530254 return list(set(item_list)), list(set(warehouse_list))
255
256 def make_cancel_gl_entries(self):
257 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
Nabin Hait4ae729b2013-08-20 12:04:46 +0530258 and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
Nabin Hait787c02e2013-03-29 16:42:33 +0530259 self.make_gl_entries()