blob: 1aeca1bc733c76405f5958ab3be0795d54409340 [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):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +053041 out = {}
42
Nabin Haitc3afb252013-03-19 12:01:24 +053043 if not (item_list and warehouse_list):
44 item_list, warehouse_list = self.get_distinct_item_warehouse()
45
46 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +053047 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +053048 voucher_detail_no, posting_date, posting_time, stock_value,
49 warehouse, actual_qty as qty from `tabStock Ledger Entry`
50 where ifnull(`is_cancelled`, "No") = "No" and company = %s
51 and item_code in (%s) and warehouse in (%s)
52 order by item_code desc, warehouse desc, posting_date desc,
53 posting_time desc, name desc""" %
54 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
55 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +053056
57 for r in res:
58 if (r.item_code, r.warehouse) not in out:
59 out[(r.item_code, r.warehouse)] = []
60
61 out[(r.item_code, r.warehouse)].append(r)
62
63 return out
Nabin Haitc3afb252013-03-19 12:01:24 +053064
65 def get_distinct_item_warehouse(self):
66 item_list = []
67 warehouse_list = []
68 for item in self.doclist.get({"parentfield": self.fname}) \
69 + self.doclist.get({"parentfield": "packing_details"}):
70 item_list.append(item.item_code)
71 warehouse_list.append(item.warehouse)
72
Nabin Hait787c02e2013-03-29 16:42:33 +053073 return list(set(item_list)), list(set(warehouse_list))
74
75 def make_cancel_gl_entries(self):
76 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
77 and voucher_no=%s and ifnull(is_cancelled, 'No')='No'""",
78 (self.doc.doctype, self.doc.name)):
79 self.make_gl_entries()