Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
Nabin Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 6 | from webnotes.utils import cint |
| 7 | import webnotes.defaults |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 8 | from controllers.accounts_controller import AccountsController |
| 9 | |
| 10 | class StockController(AccountsController): |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 11 | 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 Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 14 | 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 Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 17 | |
| 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 Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 37 | |
Nabin Hait | a0e7c15 | 2013-03-21 18:37:06 +0530 | [diff] [blame] | 38 | return gl_entries |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 39 | |
| 40 | def get_stock_ledger_entries(self, item_list=None, warehouse_list=None): |
Anand Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 41 | out = {} |
| 42 | |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 43 | 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 Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 47 | res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no, |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 48 | 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 Doshi | 5dd6b1d | 2013-08-07 19:27:30 +0530 | [diff] [blame] | 56 | |
| 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 Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 64 | |
| 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 Hait | 787c02e | 2013-03-29 16:42:33 +0530 | [diff] [blame] | 73 | 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() |