blob: 44e1405ac6982e5cdb150cefa1149365b1d779c4 [file] [log] [blame]
Nabin Hait31665e52011-09-29 10:41:02 +05301import unittest
2
3import webnotes
4import webnotes.profile
5webnotes.user = webnotes.profile.Profile()
6
7
8from webnotes.model.doc import Document
9from webnotes.model.code import get_obj
10from webnotes.utils import cstr, flt
Nabin Hait88f9cb52011-09-30 17:20:06 +053011from webnotes.model.doclist import getlist
Nabin Hait31665e52011-09-29 10:41:02 +053012sql = webnotes.conn.sql
13
14from sandbox.testdata.masters import *
15from sandbox.testdata import stock_entry
16#----------------------------------------------------------
17
Nabin Hait88f9cb52011-09-30 17:20:06 +053018
Nabin Hait31665e52011-09-29 10:41:02 +053019class TestStockEntry(unittest.TestCase):
Nabin Hait88f9cb52011-09-30 17:20:06 +053020 #===========================================================================
21 def assertDoc(self, lst):
22 """assert all values"""
23 for d in lst:
24 cl, vl = [], []
25 for k in d.keys():
26 if k!='doctype':
27 cl.append('%s=%s' % (k, '%s'))
28 vl.append(d[k])
29
30 self.assertTrue(sql("select name from `tab%s` where %s limit 1" % (d['doctype'], ' and '.join(cl)), vl))
31
32 #===========================================================================
33 def assertCount(self, lst):
34 """assert all values"""
35 for d in lst:
36 cl, vl = [], []
37 for k in d[0].keys():
38 if k!='doctype':
39 cl.append('%s=%s' % (k, '%s'))
40 vl.append(d[0][k])
41
42 self.assertTrue(sql("select count(name) from `tab%s` where %s limit 1" % (d[0]['doctype'], ' and '.join(cl)), vl)[0][0] == d[1])
43
44 #===========================================================================
Nabin Hait31665e52011-09-29 10:41:02 +053045 def setUp(self):
46 print "====================================="
Nabin Hait88f9cb52011-09-30 17:20:06 +053047 webnotes.conn.begin()
Nabin Hait31665e52011-09-29 10:41:02 +053048 create_master_records()
49 print 'Master Data Created'
50
Nabin Hait88f9cb52011-09-30 17:20:06 +053051 #===========================================================================
52 # Purpose: Material Receipt
53 #===========================================================================
54 def test_mr_onsubmit(self):
55 print "Test Case: Stock Entry submission"
56 self.save_stock_entry('Material Receipt')
57
58 mr = get_obj('Stock Entry', stock_entry.mr[0].name, with_children=1)
59 self.submit_stock_entry(mr)
60
61 # stock ledger entry
62 print "Checking stock ledger entry........."
63 self.assertDoc(self.get_expected_sle('mr_submit'))
64
65 # bin qty
66 print "Checking Bin qty........."
67 self.assertDoc([{'doctype':'Bin', 'actual_qty':10, 'item_code':'it', 'warehouse':'wh1'}])
68
69 # serial no
70 self.assertCount([[{'doctype': 'Serial No', 'item_code': 'it', 'warehouse': 'wh1', 'status': 'In Store', 'docstatus': 0}, 10]])
71
72
73 #===========================================================================
74 def test_mr_oncancel(self):
75 print "Test Case: Stock Entry Cancellation"
76 self.save_stock_entry('Material Receipt')
77
78 mr = get_obj('Stock Entry', stock_entry.mr[0].name, with_children=1)
79 self.cancel_stock_entry(mr)
80
81 # stock ledger entry
82 print "Checking stock ledger entry........."
83 self.assertDoc(self.get_expected_sle('mr_cancel'))
84
85 # bin qty
86 print "Checking Bin qty........."
87 self.assertDoc([{'doctype':'Bin', 'actual_qty':0, 'item_code':'it', 'warehouse':'wh1'}])
88
89 # serial no
90 self.assertCount([[{'doctype': 'Serial No', 'item_code': 'it', 'warehouse': '', 'status': 'Not in Use', 'docstatus': 2}, 10]])
91
92 #===========================================================================
93 # Purpose: Material Transafer
94 #===========================================================================
95 def test_mtn_onsubmit(self):
96 print "Test Case: Stock Entry submission"
97
98 self.save_stock_entry('Material Receipt')
99 mr = get_obj('Stock Entry', stock_entry.mr[0].name, with_children=1)
100 mr = self.submit_stock_entry(mr)
101
102 self.save_stock_entry('Material Transfer')
103 mtn = get_obj('Stock Entry', stock_entry.mtn[0].name, with_children=1)
104 tn = self.submit_stock_entry(mtn)
105
106 # stock ledger entry
107 print "Checking stock ledger entry........."
108 self.assertDoc(self.get_expected_sle('mtn_submit'))
109
110 # bin qty
111 print "Checking Bin qty........."
112 self.assertDoc([
113 {'doctype':'Bin', 'actual_qty':5, 'item_code':'it', 'warehouse':'wh1'},
114 {'doctype':'Bin', 'actual_qty':5, 'item_code':'it', 'warehouse':'wh2'}
115 ])
116
117 # serial no
118 self.assertCount([
119 [{'doctype': 'Serial No', 'item_code': 'it', 'warehouse': 'wh1', 'status': 'In Store', 'docstatus': 0}, 5],
120 [{'doctype': 'Serial No', 'item_code': 'it', 'warehouse': 'wh2', 'status': 'In Store', 'docstatus': 0}, 5]
121 ])
122
123 #===========================================================================
124 def test_mtn_oncancel(self):
125 print "Test Case: Stock Entry Cancellation"
126
127 self.save_stock_entry('Material Receipt')
128 mr = get_obj('Stock Entry', stock_entry.mr[0].name, with_children=1)
129 mr = self.submit_stock_entry(mr)
130
131 self.save_stock_entry('Material Transfer')
132 mtn = get_obj('Stock Entry', stock_entry.mtn[0].name, with_children=1)
133 self.cancel_stock_entry(mtn)
134
135 # stock ledger entry
136 print "Checking stock ledger entry........."
137 self.assertDoc(self.get_expected_sle('mtn_cancel'))
138
139 # bin qty
140 print "Checking Bin qty........."
141 self.assertDoc([
142 {'doctype':'Bin', 'actual_qty':10, 'item_code':'it', 'warehouse':'wh1'},
143 {'doctype':'Bin', 'actual_qty':0, 'item_code':'it', 'warehouse':'wh2'}
144 ])
145
146 # serial no
147 self.assertCount([[{'doctype': 'Serial No', 'item_code': 'it', 'warehouse': 'wh1', 'status': 'In Store', 'docstatus': 0}, 10]])
148
149 #===========================================================================
150 def save_stock_entry(self, t):
151 if t == 'Material Receipt':
152 data = stock_entry.mr
153 elif t == 'Material Transfer':
154 data = stock_entry.mtn
155
156 for each in data:
Nabin Hait31665e52011-09-29 10:41:02 +0530157 each.save(1)
158
Nabin Hait88f9cb52011-09-30 17:20:06 +0530159 for t in data[1:]:
160 sql("update `tabStock Entry Detail` set parent = '%s' where name = '%s'" % (data[0].name, t.name))
Nabin Hait31665e52011-09-29 10:41:02 +0530161 print "Stock Entry Created"
162
Nabin Hait31665e52011-09-29 10:41:02 +0530163
164 #===========================================================================
Nabin Hait88f9cb52011-09-30 17:20:06 +0530165 def submit_stock_entry(self, ste):
166 ste.validate()
167 ste.on_submit()
Nabin Hait31665e52011-09-29 10:41:02 +0530168
Nabin Hait88f9cb52011-09-30 17:20:06 +0530169 ste.doc.docstatus = 1
170 ste.doc.save()
171
Nabin Hait31665e52011-09-29 10:41:02 +0530172 print "Stock Entry Submitted"
Nabin Hait88f9cb52011-09-30 17:20:06 +0530173 return ste
Nabin Hait31665e52011-09-29 10:41:02 +0530174
175 #===========================================================================
Nabin Hait88f9cb52011-09-30 17:20:06 +0530176 def cancel_stock_entry(self, ste):
177 ste = self.submit_stock_entry(ste)
Nabin Hait31665e52011-09-29 10:41:02 +0530178
Nabin Hait88f9cb52011-09-30 17:20:06 +0530179 ste.on_cancel()
Nabin Hait31665e52011-09-29 10:41:02 +0530180
Nabin Hait88f9cb52011-09-30 17:20:06 +0530181 ste.doc.cancel_reason = "testing"
182 ste.doc.docstatus = 2
183 ste.doc.save()
Nabin Hait31665e52011-09-29 10:41:02 +0530184
185 print "Stock Entry Cancelled"
Nabin Hait88f9cb52011-09-30 17:20:06 +0530186 return ste
Nabin Hait31665e52011-09-29 10:41:02 +0530187
188 #===========================================================================
189 def check_serial_no(self, action, cnt):
190 print "Checking serial nos........"
191 if action == 'submit':
192 status, wh, docstatus = 'In Store', 'wh1', 0
193 else:
194 status, wh, docstatus = 'Not in Use', '', 2
195
196 ser = sql("select count(name) from `tabSerial No` where item_code = 'it' and warehouse = '%s' and status = '%s' and docstatus = %s" % (wh, status, docstatus))
197
198 self.assertTrue(ser[0][0] == cnt)
Nabin Hait88f9cb52011-09-30 17:20:06 +0530199
Nabin Hait31665e52011-09-29 10:41:02 +0530200 #===========================================================================
201 def tearDown(self):
202 webnotes.conn.rollback()
Nabin Hait88f9cb52011-09-30 17:20:06 +0530203
204
205 # Expected Result Set
206 #===================================================================================================
207 def get_expected_sle(self, action):
208 expected_sle = {
209 'mr_submit': [{
210 'doctype': 'Stock Ledger Entry',
211 'item_code':'it',
212 'warehouse':'wh1',
213 'voucher_type': 'Stock Entry',
214 'voucher_no': stock_entry.mr[0].name,
215 'actual_qty': 10,
216 'bin_aqat': 10,
217 'valuation_rate': 100,
218 'is_cancelled': 'No'
219 }],
220 'mr_cancel': [{
221 'doctype': 'Stock Ledger Entry',
222 'item_code':'it',
223 'warehouse':'wh1',
224 'voucher_type': 'Stock Entry',
225 'voucher_no': stock_entry.mr[0].name,
226 'actual_qty': 10,
227 'bin_aqat': 10,
228 'valuation_rate': 100,
229 'is_cancelled': 'Yes'
230 },{
231 'doctype': 'Stock Ledger Entry',
232 'item_code':'it',
233 'warehouse':'wh1',
234 'voucher_type': 'Stock Entry',
235 'voucher_no': stock_entry.mr[0].name,
236 'actual_qty': -10,
237 'ifnull(bin_aqat, 0)': 0,
238 'ifnull(valuation_rate, 0)': 0,
239 "ifnull(is_cancelled, 'No')": 'Yes'
240 }],
241 'mtn_submit': [{
242 'doctype': 'Stock Ledger Entry',
243 'item_code':'it',
244 'warehouse':'wh1',
245 'voucher_type': 'Stock Entry',
246 'voucher_no': stock_entry.mtn[0].name,
247 'actual_qty': -5,
248 'bin_aqat': 5,
249 'valuation_rate': 100,
250 'is_cancelled': 'No'
251 }, {
252 'doctype': 'Stock Ledger Entry',
253 'item_code':'it',
254 'warehouse':'wh2',
255 'voucher_type': 'Stock Entry',
256 'voucher_no': stock_entry.mtn[0].name,
257 'actual_qty': 5,
258 'bin_aqat': 5,
259 'valuation_rate': 100,
260 'is_cancelled': 'No'
261 }],
262 'mtn_cancel': [{
263 'doctype': 'Stock Ledger Entry',
264 'item_code':'it',
265 'warehouse':'wh1',
266 'voucher_type': 'Stock Entry',
267 'voucher_no': stock_entry.mtn[0].name,
268 'actual_qty': -5,
269 'bin_aqat': 5,
270 #'valuation_rate': 100,
271 'is_cancelled': 'Yes'
272 }, {
273 'doctype': 'Stock Ledger Entry',
274 'item_code':'it',
275 'warehouse':'wh2',
276 'voucher_type': 'Stock Entry',
277 'voucher_no': stock_entry.mtn[0].name,
278 'actual_qty': 5,
279 'bin_aqat': 5,
280 'valuation_rate': 100,
281 'is_cancelled': 'Yes'
282 }, {
283 'doctype': 'Stock Ledger Entry',
284 'item_code':'it',
285 'warehouse':'wh1',
286 'voucher_type': 'Stock Entry',
287 'voucher_no': stock_entry.mtn[0].name,
288 'actual_qty': 5,
289 #'bin_aqat': 10,
290 #'valuation_rate': 100,
291 'is_cancelled': 'Yes'
292 }, {
293 'doctype': 'Stock Ledger Entry',
294 'item_code':'it',
295 'warehouse':'wh2',
296 'voucher_type': 'Stock Entry',
297 'voucher_no': stock_entry.mtn[0].name,
298 'actual_qty': -5,
299 #'bin_aqat': 0,
300 #'valuation_rate': 100,
301 'is_cancelled': 'Yes'
302 }]
303 }
304
305 return expected_sle[action]