blob: fc4997eef6c4fc11d46187120c06b32eb8663221 [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 Haitd4741942013-08-06 15:57:25 +053017
Nabin Hait2e296fa2013-08-28 18:53:11 +053018 if self.doc.docstatus==1:
19 gl_entries = self.get_gl_entries_for_stock()
Nabin Hait27994c22013-08-26 16:53:30 +053020 make_gl_entries(gl_entries)
Nabin Hait2e296fa2013-08-28 18:53:11 +053021 else:
22 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 Hait2e296fa2013-08-28 18:53:11 +0530120 item_codes = webnotes.conn.sql_list("""select distinct item_code
121 from `tabStock Ledger Entry`
122 where voucher_type=%s and voucher_no=%s""", (self.doc.doctype, self.doc.name))
123
124 for d in webnotes.conn.sql("""select distinct sle.voucher_type, sle.voucher_no
125 from `tabStock Ledger Entry` sle
126 where timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s)
127 order by timestamp(sle.posting_date, sle.posting_time) asc, name asc""",
Nabin Hait27994c22013-08-26 16:53:30 +0530128 (self.doc.posting_date, self.doc.posting_time), as_dict=True):
129 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
Nabin Hait2e296fa2013-08-28 18:53:11 +0530130
Nabin Hait27994c22013-08-26 16:53:30 +0530131 return future_stock_vouchers
132
133 def get_voucherwise_gl_entries(self, future_stock_vouchers):
134 gl_entries = {}
135 if future_stock_vouchers:
136 for d in webnotes.conn.sql("""select * from `tabGL Entry`
137 where posting_date >= %s and voucher_no in (%s)""" %
138 ('%s', ', '.join(['%s']*len(future_stock_vouchers))),
139 tuple([self.doc.posting_date] + [d[1] for d in future_stock_vouchers]), as_dict=1):
Nabin Hait2e296fa2013-08-28 18:53:11 +0530140 gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
Nabin Hait27994c22013-08-26 16:53:30 +0530141
142 return gl_entries
Nabin Hait2e296fa2013-08-28 18:53:11 +0530143
144 def delete_gl_entries(self, voucher_type, voucher_no):
145 webnotes.conn.sql("""delete from `tabGL Entry`
146 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
Nabin Hait27994c22013-08-26 16:53:30 +0530147
Nabin Hait2e296fa2013-08-28 18:53:11 +0530148 def make_adjustment_entry(self, expected_gle, voucher_obj):
Nabin Haitd4741942013-08-06 15:57:25 +0530149 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530150 account_list = [d.account for d in expected_gle]
151 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
152
153 cost_center = self.get_company_default("cost_center")
154 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
155
Nabin Haitd4741942013-08-06 15:57:25 +0530156 gl_entries = []
157 for account, diff in acc_diff.items():
158 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530159 gl_entries.append([
160 # stock in hand account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530161 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530162 "account": account,
163 "against": stock_adjustment_account,
164 "debit": diff,
165 "remarks": "Adjustment Accounting Entry for Stock",
166 }),
167
168 # account against stock in hand
Nabin Hait2e296fa2013-08-28 18:53:11 +0530169 voucher_obj.get_gl_dict({
Nabin Hait27994c22013-08-26 16:53:30 +0530170 "account": stock_adjustment_account,
171 "against": account,
172 "credit": diff,
173 "cost_center": cost_center or None,
174 "remarks": "Adjustment Accounting Entry for Stock",
175 }),
176 ])
177
Nabin Haitd4741942013-08-06 15:57:25 +0530178 if gl_entries:
179 from accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530180 make_gl_entries(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530181
182 def check_expense_account(self, item):
183 if item.fields.has_key("expense_account") and not item.expense_account:
Nabin Hait678130f2013-09-02 18:10:36 +0530184 msgprint(_("""Expense/Difference account is mandatory for item: """) + item.item_code,
Nabin Hait27994c22013-08-26 16:53:30 +0530185 raise_exception=1)
186
187 if item.fields.has_key("expense_account") and not item.cost_center:
188 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
189 raise_exception=1)
Nabin Haitd4741942013-08-06 15:57:25 +0530190
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530191 def get_sl_entries(self, d, args):
192 sl_dict = {
193 "item_code": d.item_code,
194 "warehouse": d.warehouse,
195 "posting_date": self.doc.posting_date,
196 "posting_time": self.doc.posting_time,
197 "voucher_type": self.doc.doctype,
198 "voucher_no": self.doc.name,
199 "voucher_detail_no": d.name,
200 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
201 "stock_uom": d.stock_uom,
202 "incoming_rate": 0,
203 "company": self.doc.company,
204 "fiscal_year": self.doc.fiscal_year,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530205 "batch_no": cstr(d.batch_no).strip(),
206 "serial_no": d.serial_no,
Nabin Hait74c281c2013-08-19 16:17:18 +0530207 "project": d.project_name,
Nabin Hait9653f602013-08-20 15:37:33 +0530208 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530209 }
210
211 sl_dict.update(args)
212 return sl_dict
213
214 def make_sl_entries(self, sl_entries, is_amended=None):
Nabin Hait74c281c2013-08-19 16:17:18 +0530215 from stock.stock_ledger import make_sl_entries
216 make_sl_entries(sl_entries, is_amended)
217
Nabin Haitc3afb252013-03-19 12:01:24 +0530218 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530219 out = {}
220
Nabin Haitc3afb252013-03-19 12:01:24 +0530221 if not (item_list and warehouse_list):
222 item_list, warehouse_list = self.get_distinct_item_warehouse()
223
224 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530225 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +0530226 voucher_detail_no, posting_date, posting_time, stock_value,
227 warehouse, actual_qty as qty from `tabStock Ledger Entry`
Nabin Hait4ae729b2013-08-20 12:04:46 +0530228 where company = %s and item_code in (%s) and warehouse in (%s)
Nabin Haitc3afb252013-03-19 12:01:24 +0530229 order by item_code desc, warehouse desc, posting_date desc,
230 posting_time desc, name desc""" %
231 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
232 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530233
234 for r in res:
235 if (r.item_code, r.warehouse) not in out:
236 out[(r.item_code, r.warehouse)] = []
237
238 out[(r.item_code, r.warehouse)].append(r)
239
240 return out
Nabin Haitc3afb252013-03-19 12:01:24 +0530241
242 def get_distinct_item_warehouse(self):
243 item_list = []
244 warehouse_list = []
245 for item in self.doclist.get({"parentfield": self.fname}) \
246 + self.doclist.get({"parentfield": "packing_details"}):
247 item_list.append(item.item_code)
248 warehouse_list.append(item.warehouse)
249
Nabin Hait787c02e2013-03-29 16:42:33 +0530250 return list(set(item_list)), list(set(warehouse_list))
251
252 def make_cancel_gl_entries(self):
253 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
Nabin Hait4ae729b2013-08-20 12:04:46 +0530254 and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
Nabin Hait787c02e2013-03-29 16:42:33 +0530255 self.make_gl_entries()