blob: 03bdc98d929ac3643d82c57557a5f0268ea8bfe9 [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 Hait1e2f20a2013-08-02 11:42:11 +053019from webnotes.utils import cint, flt, cstr
Nabin Hait787c02e2013-03-29 16:42:33 +053020import webnotes.defaults
Nabin Haitc3afb252013-03-19 12:01:24 +053021from controllers.accounts_controller import AccountsController
22
23class StockController(AccountsController):
Nabin Haita0e7c152013-03-21 18:37:06 +053024 def get_gl_entries_for_stock(self, against_stock_account, amount,
25 stock_in_hand_account=None, cost_center=None):
26 if not stock_in_hand_account:
Nabin Hait0fc24542013-03-25 11:06:00 +053027 stock_in_hand_account = self.get_company_default("stock_in_hand_account")
28 if not cost_center:
29 cost_center = self.get_company_default("stock_adjustment_cost_center")
Nabin Haitc3afb252013-03-19 12:01:24 +053030
31 if amount:
32 gl_entries = [
33 # stock in hand account
34 self.get_gl_dict({
35 "account": stock_in_hand_account,
36 "against": against_stock_account,
37 "debit": amount,
38 "remarks": self.doc.remarks or "Accounting Entry for Stock",
39 }, self.doc.docstatus == 2),
40
41 # account against stock in hand
42 self.get_gl_dict({
43 "account": against_stock_account,
44 "against": stock_in_hand_account,
45 "credit": amount,
46 "cost_center": cost_center or None,
47 "remarks": self.doc.remarks or "Accounting Entry for Stock",
48 }, self.doc.docstatus == 2),
49 ]
Nabin Haitc3afb252013-03-19 12:01:24 +053050
Nabin Haita0e7c152013-03-21 18:37:06 +053051 return gl_entries
Nabin Hait1e2f20a2013-08-02 11:42:11 +053052
53
54 def get_sl_entries(self, d, args):
55 sl_dict = {
56 "item_code": d.item_code,
57 "warehouse": d.warehouse,
58 "posting_date": self.doc.posting_date,
59 "posting_time": self.doc.posting_time,
60 "voucher_type": self.doc.doctype,
61 "voucher_no": self.doc.name,
62 "voucher_detail_no": d.name,
63 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
64 "stock_uom": d.stock_uom,
65 "incoming_rate": 0,
66 "company": self.doc.company,
67 "fiscal_year": self.doc.fiscal_year,
68 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No",
69 "batch_no": cstr(d.batch_no).strip(),
70 "serial_no": d.serial_no,
71 "project": d.project_name
72 }
73
74 sl_dict.update(args)
75 return sl_dict
76
77 def make_sl_entries(self, sl_entries, is_amended=None):
78 if sl_entries:
79 from webnotes.model.code import get_obj
80 get_obj('Stock Ledger').update_stock(sl_entries, is_amended)
Nabin Haitc3afb252013-03-19 12:01:24 +053081
82 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
83 if not (item_list and warehouse_list):
84 item_list, warehouse_list = self.get_distinct_item_warehouse()
85
86 if item_list and warehouse_list:
87 return webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
88 voucher_detail_no, posting_date, posting_time, stock_value,
89 warehouse, actual_qty as qty from `tabStock Ledger Entry`
90 where ifnull(`is_cancelled`, "No") = "No" and company = %s
91 and item_code in (%s) and warehouse in (%s)
92 order by item_code desc, warehouse desc, posting_date desc,
93 posting_time desc, name desc""" %
94 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
95 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
96
97 def get_distinct_item_warehouse(self):
98 item_list = []
99 warehouse_list = []
100 for item in self.doclist.get({"parentfield": self.fname}) \
101 + self.doclist.get({"parentfield": "packing_details"}):
102 item_list.append(item.item_code)
103 warehouse_list.append(item.warehouse)
104
Nabin Hait787c02e2013-03-29 16:42:33 +0530105 return list(set(item_list)), list(set(warehouse_list))
106
107 def make_cancel_gl_entries(self):
108 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
109 and voucher_no=%s and ifnull(is_cancelled, 'No')='No'""",
110 (self.doc.doctype, self.doc.name)):
111 self.make_gl_entries()