Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +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 |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 19 | from webnotes.utils import cint |
| 20 | import webnotes.defaults |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 21 | from controllers.accounts_controller import AccountsController |
| 22 | |
| 23 | class StockController(AccountsController): |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 24 | 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 Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 27 | 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 Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 30 | |
| 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 Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 50 | |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 51 | return gl_entries |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 52 | |
| 53 | def get_stock_ledger_entries(self, item_list=None, warehouse_list=None): |
| 54 | if not (item_list and warehouse_list): |
| 55 | item_list, warehouse_list = self.get_distinct_item_warehouse() |
| 56 | |
| 57 | if item_list and warehouse_list: |
| 58 | return webnotes.conn.sql("""select item_code, voucher_type, voucher_no, |
| 59 | voucher_detail_no, posting_date, posting_time, stock_value, |
| 60 | warehouse, actual_qty as qty from `tabStock Ledger Entry` |
| 61 | where ifnull(`is_cancelled`, "No") = "No" and company = %s |
| 62 | and item_code in (%s) and warehouse in (%s) |
| 63 | order by item_code desc, warehouse desc, posting_date desc, |
| 64 | posting_time desc, name desc""" % |
| 65 | ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))), |
| 66 | tuple([self.doc.company] + item_list + warehouse_list), as_dict=1) |
| 67 | |
| 68 | def get_distinct_item_warehouse(self): |
| 69 | item_list = [] |
| 70 | warehouse_list = [] |
| 71 | for item in self.doclist.get({"parentfield": self.fname}) \ |
| 72 | + self.doclist.get({"parentfield": "packing_details"}): |
| 73 | item_list.append(item.item_code) |
| 74 | warehouse_list.append(item.warehouse) |
| 75 | |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 76 | return list(set(item_list)), list(set(warehouse_list)) |
| 77 | |
| 78 | def make_cancel_gl_entries(self): |
| 79 | if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s |
| 80 | and voucher_no=%s and ifnull(is_cancelled, 'No')='No'""", |
| 81 | (self.doc.doctype, self.doc.name)): |
| 82 | self.make_gl_entries() |