blob: c76865d3717d6332bd37f241c47f441e16f7ea3c [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:
26 stock_in_hand_account = self.get_default_account("stock_in_hand_account")
Nabin Haitc3afb252013-03-19 12:01:24 +053027
28 if amount:
29 gl_entries = [
30 # stock in hand account
31 self.get_gl_dict({
32 "account": stock_in_hand_account,
33 "against": against_stock_account,
34 "debit": amount,
35 "remarks": self.doc.remarks or "Accounting Entry for Stock",
36 }, self.doc.docstatus == 2),
37
38 # account against stock in hand
39 self.get_gl_dict({
40 "account": against_stock_account,
41 "against": stock_in_hand_account,
42 "credit": amount,
43 "cost_center": cost_center or None,
44 "remarks": self.doc.remarks or "Accounting Entry for Stock",
45 }, self.doc.docstatus == 2),
46 ]
Nabin Haitc3afb252013-03-19 12:01:24 +053047
Nabin Haita0e7c152013-03-21 18:37:06 +053048 return gl_entries
49
50
51 def check_expense_account(self, item):
52 if not item.expense_account:
53 msgprint(_("""Expense account is mandatory for item: """) + item.item_code,
54 raise_exception=1)
Nabin Haitc3afb252013-03-19 12:01:24 +053055
56 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
57 if not (item_list and warehouse_list):
58 item_list, warehouse_list = self.get_distinct_item_warehouse()
59
60 if item_list and warehouse_list:
61 return webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
62 voucher_detail_no, posting_date, posting_time, stock_value,
63 warehouse, actual_qty as qty from `tabStock Ledger Entry`
64 where ifnull(`is_cancelled`, "No") = "No" and company = %s
65 and item_code in (%s) and warehouse in (%s)
66 order by item_code desc, warehouse desc, posting_date desc,
67 posting_time desc, name desc""" %
68 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
69 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
70
71 def get_distinct_item_warehouse(self):
72 item_list = []
73 warehouse_list = []
74 for item in self.doclist.get({"parentfield": self.fname}) \
75 + self.doclist.get({"parentfield": "packing_details"}):
76 item_list.append(item.item_code)
77 warehouse_list.append(item.warehouse)
78
79 return list(set(item_list)), list(set(warehouse_list))