blob: 0ed2e2e0b2c1f1ae848944f4aec6155575c0d273 [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):
41 from accounts.utils import get_stock_and_account_difference
42 acc_diff = get_stock_and_account_difference(warehouse_list)
Nabin Haitd4741942013-08-06 15:57:25 +053043 if not cost_center:
44 cost_center = self.get_company_default("cost_center")
Nabin Haitd4741942013-08-06 15:57:25 +053045 gl_entries = []
46 for account, diff in acc_diff.items():
47 if diff:
48 stock_adjustment_account = self.get_company_default("stock_adjustment_account")
49 gl_entries += self.get_gl_entries_for_stock(stock_adjustment_account, diff,
50 stock_in_hand_account=account, cost_center=cost_center)
51
52 if gl_entries:
53 from accounts.general_ledger import make_gl_entries
54
55 if posting_date:
56 for entries in gl_entries:
57 entries["posting_date"] = posting_date
Nabin Hait815a49e2013-08-07 17:00:01 +053058
Nabin Haitd4741942013-08-06 15:57:25 +053059 make_gl_entries(gl_entries)
60
Nabin Hait1e2f20a2013-08-02 11:42:11 +053061 def get_sl_entries(self, d, args):
62 sl_dict = {
63 "item_code": d.item_code,
64 "warehouse": d.warehouse,
65 "posting_date": self.doc.posting_date,
66 "posting_time": self.doc.posting_time,
67 "voucher_type": self.doc.doctype,
68 "voucher_no": self.doc.name,
69 "voucher_detail_no": d.name,
70 "actual_qty": (self.doc.docstatus==1 and 1 or -1)*flt(d.stock_qty),
71 "stock_uom": d.stock_uom,
72 "incoming_rate": 0,
73 "company": self.doc.company,
74 "fiscal_year": self.doc.fiscal_year,
Nabin Hait1e2f20a2013-08-02 11:42:11 +053075 "batch_no": cstr(d.batch_no).strip(),
76 "serial_no": d.serial_no,
Nabin Hait74c281c2013-08-19 16:17:18 +053077 "project": d.project_name,
Nabin Hait9653f602013-08-20 15:37:33 +053078 "is_cancelled": self.doc.docstatus==2 and "Yes" or "No"
Nabin Hait1e2f20a2013-08-02 11:42:11 +053079 }
80
81 sl_dict.update(args)
82 return sl_dict
83
84 def make_sl_entries(self, sl_entries, is_amended=None):
Nabin Hait74c281c2013-08-19 16:17:18 +053085 from stock.stock_ledger import make_sl_entries
86 make_sl_entries(sl_entries, is_amended)
87
Nabin Haitc3afb252013-03-19 12:01:24 +053088 def get_stock_ledger_entries(self, item_list=None, warehouse_list=None):
Anand Doshi5dd6b1d2013-08-07 19:27:30 +053089 out = {}
90
Nabin Haitc3afb252013-03-19 12:01:24 +053091 if not (item_list and warehouse_list):
92 item_list, warehouse_list = self.get_distinct_item_warehouse()
93
94 if item_list and warehouse_list:
Anand Doshi5dd6b1d2013-08-07 19:27:30 +053095 res = webnotes.conn.sql("""select item_code, voucher_type, voucher_no,
Nabin Haitc3afb252013-03-19 12:01:24 +053096 voucher_detail_no, posting_date, posting_time, stock_value,
97 warehouse, actual_qty as qty from `tabStock Ledger Entry`
Nabin Hait4ae729b2013-08-20 12:04:46 +053098 where company = %s and item_code in (%s) and warehouse in (%s)
Nabin Haitc3afb252013-03-19 12:01:24 +053099 order by item_code desc, warehouse desc, posting_date desc,
100 posting_time desc, name desc""" %
101 ('%s', ', '.join(['%s']*len(item_list)), ', '.join(['%s']*len(warehouse_list))),
102 tuple([self.doc.company] + item_list + warehouse_list), as_dict=1)
Anand Doshi5dd6b1d2013-08-07 19:27:30 +0530103
104 for r in res:
105 if (r.item_code, r.warehouse) not in out:
106 out[(r.item_code, r.warehouse)] = []
107
108 out[(r.item_code, r.warehouse)].append(r)
109
110 return out
Nabin Haitc3afb252013-03-19 12:01:24 +0530111
112 def get_distinct_item_warehouse(self):
113 item_list = []
114 warehouse_list = []
115 for item in self.doclist.get({"parentfield": self.fname}) \
116 + self.doclist.get({"parentfield": "packing_details"}):
117 item_list.append(item.item_code)
118 warehouse_list.append(item.warehouse)
119
Nabin Hait787c02e2013-03-29 16:42:33 +0530120 return list(set(item_list)), list(set(warehouse_list))
121
122 def make_cancel_gl_entries(self):
123 if webnotes.conn.sql("""select name from `tabGL Entry` where voucher_type=%s
Nabin Hait4ae729b2013-08-20 12:04:46 +0530124 and voucher_no=%s""", (self.doc.doctype, self.doc.name)):
Nabin Hait787c02e2013-03-29 16:42:33 +0530125 self.make_gl_entries()