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