blob: 8384973d4882710eef6a287c415d162c3ef1faea [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 Hait787c02e2013-03-29 16:42:33 +05306from webnotes.utils import cint
7import webnotes.defaults
Nabin Haitc3afb252013-03-19 12:01:24 +05308from controllers.accounts_controller import AccountsController
9
10class StockController(AccountsController):
Nabin Haita0e7c152013-03-21 18:37:06 +053011 def get_gl_entries_for_stock(self, against_stock_account, amount,
12 stock_in_hand_account=None, cost_center=None):
13 if not stock_in_hand_account:
Nabin Hait0fc24542013-03-25 11:06:00 +053014 stock_in_hand_account = self.get_company_default("stock_in_hand_account")
15 if not cost_center:
16 cost_center = self.get_company_default("stock_adjustment_cost_center")
Nabin Haitc3afb252013-03-19 12:01:24 +053017
18 if amount:
19 gl_entries = [
20 # stock in hand account
21 self.get_gl_dict({
22 "account": stock_in_hand_account,
23 "against": against_stock_account,
24 "debit": amount,
25 "remarks": self.doc.remarks or "Accounting Entry for Stock",
26 }, self.doc.docstatus == 2),
27
28 # account against stock in hand
29 self.get_gl_dict({
30 "account": against_stock_account,
31 "against": stock_in_hand_account,
32 "credit": amount,
33 "cost_center": cost_center or None,
34 "remarks": self.doc.remarks or "Accounting Entry for Stock",
35 }, self.doc.docstatus == 2),
36 ]
Nabin Haitc3afb252013-03-19 12:01:24 +053037
Nabin Haita0e7c152013-03-21 18:37:06 +053038 return gl_entries
Nabin Haitc3afb252013-03-19 12:01:24 +053039
40 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
41 if not (item_list and warehouse_list):
42 item_list, warehouse_list = self.get_distinct_item_warehouse()
43
44 if item_list and warehouse_list:
45 return webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
46 voucher_detail_no, posting_date, posting_time, stock_value,
47 warehouse, actual_qty as qty from `tabStock Ledger Entry`
48 where ifnull(`is_cancelled`, "No") = "No" and company = %s
49 and item_code in (%s) and warehouse in (%s)
50 order by item_code desc, warehouse desc, posting_date desc,
51 posting_time desc, name desc""" %
52 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
53 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
54
55 def get_distinct_item_warehouse(self):
56 item_list = []
57 warehouse_list = []
58 for item in self.doclist.get({"parentfield": self.fname}) \
59 + self.doclist.get({"parentfield": "packing_details"}):
60 item_list.append(item.item_code)
61 warehouse_list.append(item.warehouse)
62
Nabin Hait787c02e2013-03-29 16:42:33 +053063 return list(set(item_list)), list(set(warehouse_list))
64
65 def make_cancel_gl_entries(self):
66 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
67 and voucher_no=%s and ifnull(is_cancelled, 'No')='No'""",
68 (self.doc.doctype, self.doc.name)):
69 self.make_gl_entries()