blob: 576921a53035b53fa9f663241efbe1f890436a2f [file] [log] [blame]
Nabin Hait2df4d542013-01-29 11:34:39 +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
Anand Doshia1d4b782013-02-26 18:09:47 +053019from webnotes import msgprint, _
Nabin Haitfb3fd6e2013-01-30 19:16:13 +053020from webnotes.utils import flt
Nabin Haitbf495c92013-01-30 12:49:08 +053021from utilities.transaction_base import TransactionBase
Nabin Hait2df4d542013-01-29 11:34:39 +053022
Nabin Haitbf495c92013-01-30 12:49:08 +053023class AccountsController(TransactionBase):
Nabin Hait8c7234f2013-03-11 16:32:33 +053024 def get_gl_dict(self, args, cancel=None):
Nabin Hait2df4d542013-01-29 11:34:39 +053025 """this method populates the common properties of a gl entry record"""
Anand Doshi1cf73912013-03-15 13:42:31 +053026 if cancel is None:
27 cancel = (self.doc.docstatus == 2)
28
Nabin Hait2df4d542013-01-29 11:34:39 +053029 gl_dict = {
30 'company': self.doc.company,
31 'posting_date': self.doc.posting_date,
32 'voucher_type': self.doc.doctype,
33 'voucher_no': self.doc.name,
34 'aging_date': self.doc.fields.get("aging_date") or self.doc.posting_date,
35 'remarks': self.doc.remarks,
Anand Doshi1cf73912013-03-15 13:42:31 +053036 'is_cancelled': cancel and "Yes" or "No",
Nabin Hait2df4d542013-01-29 11:34:39 +053037 'fiscal_year': self.doc.fiscal_year,
38 'debit': 0,
39 'credit': 0,
40 'is_opening': self.doc.fields.get("is_opening") or "No",
41 }
42 gl_dict.update(args)
43 return gl_dict
Anand Doshia1d4b782013-02-26 18:09:47 +053044
Anand Doshi613cb6a2013-02-06 17:33:46 +053045 def clear_unallocated_advances(self, childtype, parentfield):
46 self.doclist.remove_items({"parentfield": parentfield, "allocated_amount": ["in", [0, None, ""]]})
Nabin Haitfb3fd6e2013-01-30 19:16:13 +053047
Anand Doshi613cb6a2013-02-06 17:33:46 +053048 webnotes.conn.sql("""delete from `tab%s` where parentfield=%s and parent = %s
49 and ifnull(allocated_amount, 0) = 0""" % (childtype, '%s', '%s'), (parentfield, self.doc.name))
Nabin Haitfb3fd6e2013-01-30 19:16:13 +053050
Anand Doshi613cb6a2013-02-06 17:33:46 +053051 def get_advances(self, account_head, child_doctype, parentfield, dr_or_cr):
Nabin Haitfb3fd6e2013-01-30 19:16:13 +053052 res = webnotes.conn.sql("""select t1.name as jv_no, t1.remark,
53 t2.%s as amount, t2.name as jv_detail_no
54 from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2
55 where t1.name = t2.parent and t2.account = %s and t2.is_advance = 'Yes'
56 and (t2.against_voucher is null or t2.against_voucher = '')
57 and (t2.against_invoice is null or t2.against_invoice = '')
58 and (t2.against_jv is null or t2.against_jv = '')
59 and t1.docstatus = 1 order by t1.posting_date""" %
60 (dr_or_cr, '%s'), account_head, as_dict=1)
61
62 self.doclist = self.doc.clear_table(self.doclist, parentfield)
63 for d in res:
Anand Doshi613cb6a2013-02-06 17:33:46 +053064 self.doclist.append({
65 "doctype": child_doctype,
66 "parentfield": parentfield,
67 "journal_voucher": d.jv_no,
68 "jv_detail_no": d.jv_detail_no,
69 "remarks": d.remark,
70 "advance_amount": flt(d.amount),
71 "allocate_amount": 0
Anand Doshia1d4b782013-02-26 18:09:47 +053072 })
73
74 def get_stock_in_hand_account(self):
75 stock_in_hand_account = webnotes.conn.get_value("Company", self.doc.company, "stock_in_hand_account")
76
77 if not stock_in_hand_account:
78 msgprint(_("Missing") + ": "
79 + _(webnotes.get_doctype("company").get_label("stock_in_hand_account")
80 + " " + _("for Company") + " " + self.doc.company), raise_exception=True)
81
82 return stock_in_hand_account
83
84 @property
85 def stock_items(self):
86 if not hasattr(self, "_stock_items"):
87 item_codes = list(set(item.item_code for item in self.doclist.get({"parentfield": self.fname})))
88 self._stock_items = [r[0] for r in webnotes.conn.sql("""select name
89 from `tabItem` where name in (%s) and is_stock_item='Yes'""" % \
90 (", ".join((["%s"]*len(item_codes))),), item_codes)]
91
Anand Doshi4a7248e2013-02-27 18:10:30 +053092 return self._stock_items
93
94 @property
95 def company_abbr(self):
96 if not hasattr(self, "_abbr"):
97 self._abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr")
98
99 return self._abbr