blob: a66e1905afd55d8dea63b4653448271ca5925c8d [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Nabin Haitc3afb252013-03-19 12:01:24 +05303
4from __future__ import unicode_literals
5import webnotes
Nabin Hait1e2f20a2013-08-02 11:42:11 +05306from webnotes.utils import cint, flt, cstr
Nabin Haitd4741942013-08-06 15:57:25 +05307from webnotes import msgprint, _
Nabin Hait787c02e2013-03-29 16:42:33 +05308import webnotes.defaults
Nabin Haitd4741942013-08-06 15:57:25 +05309
Nabin Haitc3afb252013-03-19 12:01:24 +053010from controllers.accounts_controller import AccountsController
11
12class StockController(AccountsController):
Nabin Haitd4741942013-08-06 15:57:25 +053013 def get_gl_entries_for_stock(self, against_stock_account, amount, warehouse=None,
14 stock_in_hand_account=None, cost_center=None):
15 if not stock_in_hand_account and warehouse:
16 stock_in_hand_account = webnotes.conn.get_value("Warehouse", warehouse, "account")
17
18 if amount:
19 gl_entries = [
Nabin Haitc3afb252013-03-19 12:01:24 +053020 # stock in hand account
21 self.get_gl_dict({
Nabin Haitd4741942013-08-06 15:57:25 +053022 "account": stock_in_hand_account,
Nabin Haitc3afb252013-03-19 12:01:24 +053023 "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,
Nabin Haitd4741942013-08-06 15:57:25 +053031 "against": stock_in_hand_account,
Nabin Haitc3afb252013-03-19 12:01:24 +053032 "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 Haitc3afb252013-03-19 12:01:24 +053037
Nabin Haitd4741942013-08-06 15:57:25 +053038 return gl_entries
Nabin Hait1e2f20a2013-08-02 11:42:11 +053039
Nabin Haitd4741942013-08-06 15:57:25 +053040 def sync_stock_account_balance(self, warehouse_list, cost_center=None, posting_date=None):
Nabin Hait469ee712013-08-07 12:33:37 +053041 print "sync_stock_account_balance"
Nabin Haitd4741942013-08-06 15:57:25 +053042 from accounts.utils import get_stock_and_account_difference
43 acc_diff = get_stock_and_account_difference(warehouse_list)
Nabin Haitd4741942013-08-06 15:57:25 +053044 if not cost_center:
45 cost_center = self.get_company_default("cost_center")
Nabin Hait469ee712013-08-07 12:33:37 +053046 print acc_diff
Nabin Haitd4741942013-08-06 15:57:25 +053047 gl_entries = []
48 for account, diff in acc_diff.items():
49 if diff:
50 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
51 gl_entries += self.get_gl_entries_for_stock(stock_adjustment_account, diff,
52 stock_in_hand_account=account, cost_center=cost_center)
53
54 if gl_entries:
55 from accounts.general_ledger import make_gl_entries
56
57 if posting_date:
58 for entries in gl_entries:
59 entries["posting_date"] = posting_date
Nabin Hait469ee712013-08-07 12:33:37 +053060 # print gl_entries
Nabin Haitd4741942013-08-06 15:57:25 +053061 make_gl_entries(gl_entries)
62
Nabin Hait1e2f20a2013-08-02 11:42:11 +053063 def get_sl_entries(self, d, args):
64 sl_dict = {
65 "item_code": d.item_code,
66 "warehouse": d.warehouse,
67 "posting_date": self.doc.posting_date,
68 "posting_time": self.doc.posting_time,
69 "voucher_type": self.doc.doctype,
70 "voucher_no": self.doc.name,
71 "voucher_detail_no": d.name,
72 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
73 "stock_uom": d.stock_uom,
74 "incoming_rate": 0,
75 "company": self.doc.company,
76 "fiscal_year": self.doc.fiscal_year,
77 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No",
78 "batch_no": cstr(d.batch_no).strip(),
79 "serial_no": d.serial_no,
80 "project": d.project_name
81 }
82
83 sl_dict.update(args)
84 return sl_dict
85
86 def make_sl_entries(self, sl_entries, is_amended=None):
87 if sl_entries:
88 from webnotes.model.code import get_obj
89 get_obj('Stock Ledger').update_stock(sl_entries, is_amended)
Nabin Haitc3afb252013-03-19 12:01:24 +053090
91 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
92 if not (item_list and warehouse_list):
93 item_list, warehouse_list = self.get_distinct_item_warehouse()
94
95 if item_list and warehouse_list:
96 return webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
97 voucher_detail_no, posting_date, posting_time, stock_value,
98 warehouse, actual_qty as qty from `tabStock Ledger Entry`
99 where ifnull(`is_cancelled`, "No") = "No" and company = %s
100 and item_code in (%s) and warehouse in (%s)
101 order by item_code desc, warehouse desc, posting_date desc,
102 posting_time desc, name desc""" %
103 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
104 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
105
106 def get_distinct_item_warehouse(self):
107 item_list = []
108 warehouse_list = []
109 for item in self.doclist.get({"parentfield": self.fname}) \
110 + self.doclist.get({"parentfield": "packing_details"}):
111 item_list.append(item.item_code)
112 warehouse_list.append(item.warehouse)
113
Nabin Hait787c02e2013-03-29 16:42:33 +0530114 return list(set(item_list)), list(set(warehouse_list))
115
116 def make_cancel_gl_entries(self):
117 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
118 and voucher_no=%s and ifnull(is_cancelled, 'No')='No'""",
119 (self.doc.doctype, self.doc.name)):
120 self.make_gl_entries()