blob: deb687cf9538d54c8af976fe4c474d54080d16e3 [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
11
12class StockController(AccountsController):
Nabin Hait27994c22013-08-26 16:53:30 +053013 def make_gl_entries(self):
14 if not cint(webnotes.defaults.get_global_default("perpetual_accounting")):
15 return
Nabin Haitd4741942013-08-06 15:57:25 +053016
Nabin Hait27994c22013-08-26 16:53:30 +053017 from accounts.general_ledger import make_gl_entries, delete_gl_entries
18 gl_entries = self.get_gl_entries_for_stock()
19
20 if gl_entries and self.doc.docstatus==1:
21 make_gl_entries(gl_entries)
22 elif self.doc.docstatus==2:
23 webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s
24 and voucher_no=%s""", (self.doc.doctype, self.doc.name))
25
26 self.update_gl_entries_after()
27
28
29 def get_gl_entries_for_stock(self, item_acc_map=None, expense_account=None, cost_center=None):
30 from accounts.general_ledger import process_gl_map
31
32 if not (expense_account or cost_center or item_acc_map):
33 item_acc_map = {}
34 for item in self.doclist.get({"parentfield": self.fname}):
35 self.check_expense_account(item)
36 item_acc_map.setdefault(item.name, [item.expense_account, item.cost_center])
37
38 gl_entries = []
39 stock_value_diff = self.get_stock_value_diff_from_sle(item_acc_map, expense_account,
40 cost_center)
41 for stock_in_hand_account, against_stock_account_dict in stock_value_diff.items():
42 for against_stock_account, cost_center_dict in against_stock_account_dict.items():
43 for cost_center, value_diff in cost_center_dict.items():
44 gl_entries += [
45 # stock in hand account
46 self.get_gl_dict({
47 "account": stock_in_hand_account,
48 "against": against_stock_account,
49 "debit": value_diff,
50 "remarks": self.doc.remarks or "Accounting Entry for Stock",
51 }),
52
53 # account against stock in hand
54 self.get_gl_dict({
55 "account": against_stock_account,
56 "against": stock_in_hand_account,
57 "credit": value_diff,
58 "cost_center": cost_center != "No Cost Center" and cost_center or None,
59 "remarks": self.doc.remarks or "Accounting Entry for Stock",
60 }),
61 ]
62 gl_entries = process_gl_map(gl_entries)
63 return gl_entries
64
65
66 def get_stock_value_diff_from_sle(self, item_acc_map, expense_account, cost_center):
67 wh_acc_map = self.get_warehouse_account_map()
68 stock_value_diff = {}
69 for sle in webnotes.conn.sql("""select warehouse, stock_value_difference, voucher_detail_no
70 from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""",
71 (self.doc.doctype, self.doc.name), as_dict=True):
72 account = wh_acc_map[sle.warehouse]
73 against_account = expense_account or item_acc_map[sle.voucher_detail_no][0]
74 cost_center = cost_center or item_acc_map[sle.voucher_detail_no][1] or \
75 "No Cost Center"
Nabin Haitc3afb252013-03-19 12:01:24 +053076
Nabin Hait27994c22013-08-26 16:53:30 +053077 stock_value_diff.setdefault(account, {}).setdefault(against_account, {})\
78 .setdefault(cost_center, 0)
79 stock_value_diff[account][against_account][cost_center] += \
80 flt(sle.stock_value_difference)
81
82 return stock_value_diff
83
84 def get_warehouse_account_map(self):
85 wh_acc_map = {}
86 warehouse_with_no_account = []
87 for d in webnotes.conn.sql("""select name, account from `tabWarehouse`""", as_dict=True):
88 if not d.account: warehouse_with_no_account.append(d.name)
89 wh_acc_map.setdefault(d.name, d.account)
Nabin Haitc3afb252013-03-19 12:01:24 +053090
Nabin Hait27994c22013-08-26 16:53:30 +053091 if warehouse_with_no_account:
92 webnotes.throw(_("Please mention Perpetual Account in warehouse master for \
93 following warehouses") + ": " + '\n'.join(warehouse_with_no_account))
94
95 return wh_acc_map
96
97 def update_gl_entries_after(self):
98 future_stock_vouchers = self.get_future_stock_vouchers()
99 gle = self.get_voucherwise_gl_entries(future_stock_vouchers)
100 for voucher_type, voucher_no in future_stock_vouchers:
101 existing_gle = gle.get((voucher_type, voucher_no), {})
102 voucher_bean = webnotes.bean(voucher_type, voucher_no)
103 expected_gle = voucher_bean.run_method("get_gl_entries_for_stock")
104 if expected_gle:
105 if existing_gle:
106 matched = True
107 for entry in expected_gle:
108 entry_amount = existing_gle.get(entry.account, {}).get(entry.cost_center \
109 or "No Cost Center", [0, 0])
110
111 if [entry.debit, entry.credit] != entry_amount:
112 matched = False
113 break
114
115 if not matched:
116 # make updated entry
117 webnotes.conn.sql("""delete from `tabGL Entry`
118 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
119
120 voucher_bean.run_method("make_gl_entries")
121 else:
122 # make adjustment entry on that date
123 self.make_adjustment_entry(expected_gle, voucher_bean)
124
125
126 def get_future_stock_vouchers(self):
127 future_stock_vouchers = []
128 for d in webnotes.conn.sql("""select distinct voucher_type, voucher_no
129 from `tabStock Ledger Entry`
130 where timestamp(posting_date, posting_time) >= timestamp(%s, %s)
131 order by timestamp(posting_date, posting_time) asc, name asc""",
132 (self.doc.posting_date, self.doc.posting_time), as_dict=True):
133 future_stock_vouchers.append([d.voucher_type, d.voucher_no])
134
135 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):
144 gl_entries.setdefault((d.voucher_type, d.voucher_no), {})\
145 .setdefault(d.account, {})\
146 .setdefault(d.cost_center, [d.debit, d.credit])
147
148 return gl_entries
149
150 def make_adjustment_entry(self, expected_gle, voucher_bean):
Nabin Haitd4741942013-08-06 15:57:25 +0530151 from accounts.utils import get_stock_and_account_difference
Nabin Hait27994c22013-08-26 16:53:30 +0530152 account_list = [d.account for d in expected_gle]
153 acc_diff = get_stock_and_account_difference(account_list, expected_gle[0].posting_date)
154
155 cost_center = self.get_company_default("cost_center")
156 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
157
Nabin Haitd4741942013-08-06 15:57:25 +0530158 gl_entries = []
159 for account, diff in acc_diff.items():
160 if diff:
Nabin Hait27994c22013-08-26 16:53:30 +0530161 gl_entries.append([
162 # stock in hand account
163 voucher_bean.get_gl_dict({
164 "account": account,
165 "against": stock_adjustment_account,
166 "debit": diff,
167 "remarks": "Adjustment Accounting Entry for Stock",
168 }),
169
170 # account against stock in hand
171 voucher_bean.get_gl_dict({
172 "account": stock_adjustment_account,
173 "against": account,
174 "credit": diff,
175 "cost_center": cost_center or None,
176 "remarks": "Adjustment Accounting Entry for Stock",
177 }),
178 ])
179
Nabin Haitd4741942013-08-06 15:57:25 +0530180 if gl_entries:
181 from accounts.general_ledger import make_gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +0530182 make_gl_entries(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530183
184 def check_expense_account(self, item):
185 if item.fields.has_key("expense_account") and not item.expense_account:
186 msgprint(_("""Expense account is mandatory for item: """) + item.item_code,
187 raise_exception=1)
188
189 if item.fields.has_key("expense_account") and not item.cost_center:
190 msgprint(_("""Cost Center is mandatory for item: """) + item.item_code,
191 raise_exception=1)
Nabin Haitd4741942013-08-06 15:57:25 +0530192
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530193 def get_sl_entries(self, d, args):
194 sl_dict = {
195 "item_code": d.item_code,
196 "warehouse": d.warehouse,
197 "posting_date": self.doc.posting_date,
198 "posting_time": self.doc.posting_time,
199 "voucher_type": self.doc.doctype,
200 "voucher_no": self.doc.name,
201 "voucher_detail_no": d.name,
202 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
203 "stock_uom": d.stock_uom,
204 "incoming_rate": 0,
205 "company": self.doc.company,
206 "fiscal_year": self.doc.fiscal_year,
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530207 "batch_no": cstr(d.batch_no).strip(),
208 "serial_no": d.serial_no,
Nabin Hait74c281c2013-08-19 16:17:18 +0530209 "project": d.project_name,
Nabin Hait9653f602013-08-20 15:37:33 +0530210 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +0530211 }
212
213 sl_dict.update(args)
214 return sl_dict
215
216 def make_sl_entries(self, sl_entries, is_amended=None):
Nabin Hait74c281c2013-08-19 16:17:18 +0530217 from stock.stock_ledger import make_sl_entries
218 make_sl_entries(sl_entries, is_amended)
219
Nabin Haitc3afb252013-03-19 12:01:24 +0530220 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530221 out = {}
222
Nabin Haitc3afb252013-03-19 12:01:24 +0530223 if not (item_list and warehouse_list):
224 item_list, warehouse_list = self.get_distinct_item_warehouse()
225
226 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530227 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +0530228 voucher_detail_no, posting_date, posting_time, stock_value,
229 warehouse, actual_qty as qty from `tabStock Ledger Entry`
Nabin Hait4ae729b2013-08-20 12:04:46 +0530230 where company = %s and item_code in (%s) and warehouse in (%s)
Nabin Haitc3afb252013-03-19 12:01:24 +0530231 order by item_code desc, warehouse desc, posting_date desc,
232 posting_time desc, name desc""" %
233 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
234 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530235
236 for r in res:
237 if (r.item_code, r.warehouse) not in out:
238 out[(r.item_code, r.warehouse)] = []
239
240 out[(r.item_code, r.warehouse)].append(r)
241
242 return out
Nabin Haitc3afb252013-03-19 12:01:24 +0530243
244 def get_distinct_item_warehouse(self):
245 item_list = []
246 warehouse_list = []
247 for item in self.doclist.get({"parentfield": self.fname}) \
248 + self.doclist.get({"parentfield": "packing_details"}):
249 item_list.append(item.item_code)
250 warehouse_list.append(item.warehouse)
251
Nabin Hait787c02e2013-03-29 16:42:33 +0530252 return list(set(item_list)), list(set(warehouse_list))
253
254 def make_cancel_gl_entries(self):
255 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
Nabin Hait4ae729b2013-08-20 12:04:46 +0530256 and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
Nabin Hait787c02e2013-03-29 16:42:33 +0530257 self.make_gl_entries()