blob: 89f62aacf3cd306edbf53d6e34f30a2016c7f187 [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 Haita0e7c152013-03-21 18:37:06 +053019from webnotes import msgprint, _
Nabin Haitc3afb252013-03-19 12:01:24 +053020from controllers.accounts_controller import AccountsController
21
22class StockController(AccountsController):
Nabin Haita0e7c152013-03-21 18:37:06 +053023 def get_gl_entries_for_stock(self, against_stock_account, amount,
24 stock_in_hand_account=None, cost_center=None):
25 if not stock_in_hand_account:
Nabin Hait0fc24542013-03-25 11:06:00 +053026 stock_in_hand_account = self.get_company_default("stock_in_hand_account")
27 if not cost_center:
28 cost_center = self.get_company_default("stock_adjustment_cost_center")
Nabin Haitc3afb252013-03-19 12:01:24 +053029
30 if amount:
31 gl_entries = [
32 # stock in hand account
33 self.get_gl_dict({
34 "account": stock_in_hand_account,
35 "against": against_stock_account,
36 "debit": amount,
37 "remarks": self.doc.remarks or "Accounting Entry for Stock",
38 }, self.doc.docstatus == 2),
39
40 # account against stock in hand
41 self.get_gl_dict({
42 "account": against_stock_account,
43 "against": stock_in_hand_account,
44 "credit": amount,
45 "cost_center": cost_center or None,
46 "remarks": self.doc.remarks or "Accounting Entry for Stock",
47 }, self.doc.docstatus == 2),
48 ]
Nabin Haitc3afb252013-03-19 12:01:24 +053049
Nabin Haita0e7c152013-03-21 18:37:06 +053050 return gl_entries
Nabin Haitc3afb252013-03-19 12:01:24 +053051
52 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
53 if not (item_list and warehouse_list):
54 item_list, warehouse_list = self.get_distinct_item_warehouse()
55
56 if item_list and warehouse_list:
57 return webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
58 voucher_detail_no, posting_date, posting_time, stock_value,
59 warehouse, actual_qty as qty from `tabStock Ledger Entry`
60 where ifnull(`is_cancelled`, "No") = "No" and company = %s
61 and item_code in (%s) and warehouse in (%s)
62 order by item_code desc, warehouse desc, posting_date desc,
63 posting_time desc, name desc""" %
64 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
65 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
66
67 def get_distinct_item_warehouse(self):
68 item_list = []
69 warehouse_list = []
70 for item in self.doclist.get({"parentfield": self.fname}) \
71 + self.doclist.get({"parentfield": "packing_details"}):
72 item_list.append(item.item_code)
73 warehouse_list.append(item.warehouse)
74
75 return list(set(item_list)), list(set(warehouse_list))