Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 1 | # 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 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 19 | from webnotes import msgprint, _ |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 20 | from webnotes.utils import flt |
Nabin Hait | 89a94d8 | 2013-03-19 12:01:46 +0530 | [diff] [blame] | 21 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 22 | from utilities.transaction_base import TransactionBase |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 23 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 24 | class AccountsController(TransactionBase): |
Saurabh | 6f75318 | 2013-03-20 12:55:28 +0530 | [diff] [blame] | 25 | def validate(self): |
| 26 | if self.meta.get_field("grand_total"): |
| 27 | self.validate_value("grand_total", ">=", 0) |
| 28 | |
Nabin Hait | 8c7234f | 2013-03-11 16:32:33 +0530 | [diff] [blame] | 29 | def get_gl_dict(self, args, cancel=None): |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 30 | """this method populates the common properties of a gl entry record""" |
Anand Doshi | 1cf7391 | 2013-03-15 13:42:31 +0530 | [diff] [blame] | 31 | if cancel is None: |
| 32 | cancel = (self.doc.docstatus == 2) |
| 33 | |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 34 | gl_dict = { |
| 35 | 'company': self.doc.company, |
| 36 | 'posting_date': self.doc.posting_date, |
| 37 | 'voucher_type': self.doc.doctype, |
| 38 | 'voucher_no': self.doc.name, |
| 39 | 'aging_date': self.doc.fields.get("aging_date") or self.doc.posting_date, |
| 40 | 'remarks': self.doc.remarks, |
Anand Doshi | 1cf7391 | 2013-03-15 13:42:31 +0530 | [diff] [blame] | 41 | 'is_cancelled': cancel and "Yes" or "No", |
Nabin Hait | 2df4d54 | 2013-01-29 11:34:39 +0530 | [diff] [blame] | 42 | 'fiscal_year': self.doc.fiscal_year, |
| 43 | 'debit': 0, |
| 44 | 'credit': 0, |
| 45 | 'is_opening': self.doc.fields.get("is_opening") or "No", |
| 46 | } |
| 47 | gl_dict.update(args) |
| 48 | return gl_dict |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 49 | |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 50 | def clear_unallocated_advances(self, childtype, parentfield): |
| 51 | self.doclist.remove_items({"parentfield": parentfield, "allocated_amount": ["in", [0, None, ""]]}) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 52 | |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 53 | webnotes.conn.sql("""delete from `tab%s` where parentfield=%s and parent = %s |
| 54 | and ifnull(allocated_amount, 0) = 0""" % (childtype, '%s', '%s'), (parentfield, self.doc.name)) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 55 | |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 56 | def get_advances(self, account_head, child_doctype, parentfield, dr_or_cr): |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 57 | res = webnotes.conn.sql("""select t1.name as jv_no, t1.remark, |
| 58 | t2.%s as amount, t2.name as jv_detail_no |
| 59 | from `tabJournal Voucher` t1, `tabJournal Voucher Detail` t2 |
| 60 | where t1.name = t2.parent and t2.account = %s and t2.is_advance = 'Yes' |
| 61 | and (t2.against_voucher is null or t2.against_voucher = '') |
| 62 | and (t2.against_invoice is null or t2.against_invoice = '') |
| 63 | and (t2.against_jv is null or t2.against_jv = '') |
| 64 | and t1.docstatus = 1 order by t1.posting_date""" % |
| 65 | (dr_or_cr, '%s'), account_head, as_dict=1) |
| 66 | |
| 67 | self.doclist = self.doc.clear_table(self.doclist, parentfield) |
| 68 | for d in res: |
Anand Doshi | 613cb6a | 2013-02-06 17:33:46 +0530 | [diff] [blame] | 69 | self.doclist.append({ |
| 70 | "doctype": child_doctype, |
| 71 | "parentfield": parentfield, |
| 72 | "journal_voucher": d.jv_no, |
| 73 | "jv_detail_no": d.jv_detail_no, |
| 74 | "remarks": d.remark, |
| 75 | "advance_amount": flt(d.amount), |
| 76 | "allocate_amount": 0 |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 77 | }) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 78 | |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 79 | def get_company_default(self, fieldname): |
| 80 | from accounts.utils import get_company_default |
| 81 | return get_company_default(self.doc.company, fieldname) |
Anand Doshi | a1d4b78 | 2013-02-26 18:09:47 +0530 | [diff] [blame] | 82 | |
| 83 | @property |
| 84 | def stock_items(self): |
| 85 | if not hasattr(self, "_stock_items"): |
| 86 | item_codes = list(set(item.item_code for item in self.doclist.get({"parentfield": self.fname}))) |
| 87 | self._stock_items = [r[0] for r in webnotes.conn.sql("""select name |
| 88 | from `tabItem` where name in (%s) and is_stock_item='Yes'""" % \ |
| 89 | (", ".join((["%s"]*len(item_codes))),), item_codes)] |
Nabin Hait | 80abad2 | 2013-03-19 18:18:52 +0530 | [diff] [blame] | 90 | |
Anand Doshi | 4a7248e | 2013-02-27 18:10:30 +0530 | [diff] [blame] | 91 | return self._stock_items |
| 92 | |
| 93 | @property |
| 94 | def company_abbr(self): |
| 95 | if not hasattr(self, "_abbr"): |
| 96 | self._abbr = webnotes.conn.get_value("Company", self.doc.company, "abbr") |
| 97 | |
Nabin Hait | 80abad2 | 2013-03-19 18:18:52 +0530 | [diff] [blame] | 98 | return self._abbr |