blob: 7cfb68c4ef803925db3dfd9e4169addc9d211fca [file] [log] [blame]
Nabin Haitc3afb252013-03-19 12:01:24 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18import webnotes
Nabin Hait787c02e2013-03-29 16:42:33 +053019from webnotes.utils import cint
20import webnotes.defaults
Nabin Haitc3afb252013-03-19 12:01:24 +053021from controllers.accounts_controller import AccountsController
22
23class StockController(AccountsController):
Nabin Haita0e7c152013-03-21 18:37:06 +053024 def get_gl_entries_for_stock(self, against_stock_account, amount,
25 stock_in_hand_account=None, cost_center=None):
26 if not stock_in_hand_account:
Nabin Hait0fc24542013-03-25 11:06:00 +053027 stock_in_hand_account = self.get_company_default("stock_in_hand_account")
28 if not cost_center:
29 cost_center = self.get_company_default("stock_adjustment_cost_center")
Nabin Haitc3afb252013-03-19 12:01:24 +053030
31 if amount:
32 gl_entries = [
33 # stock in hand account
34 self.get_gl_dict({
35 "account": stock_in_hand_account,
36 "against": against_stock_account,
37 "debit": amount,
38 "remarks": self.doc.remarks or "Accounting Entry for Stock",
39 }, self.doc.docstatus == 2),
40
41 # account against stock in hand
42 self.get_gl_dict({
43 "account": against_stock_account,
44 "against": stock_in_hand_account,
45 "credit": amount,
46 "cost_center": cost_center or None,
47 "remarks": self.doc.remarks or "Accounting Entry for Stock",
48 }, self.doc.docstatus == 2),
49 ]
Nabin Haitc3afb252013-03-19 12:01:24 +053050
Nabin Haita0e7c152013-03-21 18:37:06 +053051 return gl_entries
Nabin Haitc3afb252013-03-19 12:01:24 +053052
53 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
54 if not (item_list and warehouse_list):
55 item_list, warehouse_list = self.get_distinct_item_warehouse()
56
57 if item_list and warehouse_list:
58 return webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
59 voucher_detail_no, posting_date, posting_time, stock_value,
60 warehouse, actual_qty as qty from `tabStock Ledger Entry`
61 where ifnull(`is_cancelled`, "No") = "No" and company = %s
62 and item_code in (%s) and warehouse in (%s)
63 order by item_code desc, warehouse desc, posting_date desc,
64 posting_time desc, name desc""" %
65 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
66 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
67
68 def get_distinct_item_warehouse(self):
69 item_list = []
70 warehouse_list = []
71 for item in self.doclist.get({"parentfield": self.fname}) \
72 + self.doclist.get({"parentfield": "packing_details"}):
73 item_list.append(item.item_code)
74 warehouse_list.append(item.warehouse)
75
Nabin Hait787c02e2013-03-29 16:42:33 +053076 return list(set(item_list)), list(set(warehouse_list))
77
78 def make_cancel_gl_entries(self):
79 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
80 and voucher_no=%s and ifnull(is_cancelled, 'No')='No'""",
81 (self.doc.doctype, self.doc.name)):
82 self.make_gl_entries()