Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 1 | import unittest |
| 2 | |
| 3 | import webnotes |
| 4 | import webnotes.profile |
| 5 | webnotes.user = webnotes.profile.Profile() |
| 6 | |
| 7 | |
| 8 | from webnotes.model.doc import Document |
| 9 | from webnotes.model.code import get_obj |
| 10 | from webnotes.utils import cstr, flt |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 11 | from webnotes.model.doclist import getlist |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 12 | sql = webnotes.conn.sql |
| 13 | |
| 14 | from sandbox.testdata.masters import * |
| 15 | from sandbox.testdata import stock_entry |
| 16 | #---------------------------------------------------------- |
| 17 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 18 | |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 19 | class TestStockEntry(unittest.TestCase): |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 20 | #=========================================================================== |
| 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 Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 45 | def setUp(self): |
| 46 | print "=====================================" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 47 | webnotes.conn.begin() |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 48 | create_master_records() |
| 49 | print 'Master Data Created' |
| 50 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 51 | #=========================================================================== |
| 52 | # Purpose: Material Receipt |
| 53 | #=========================================================================== |
| 54 | def test_mr_onsubmit(self): |
Nabin Hait | a970b11 | 2011-09-30 18:05:08 +0530 | [diff] [blame] | 55 | print "Test Case: Material Receipt submission" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 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): |
Nabin Hait | a970b11 | 2011-09-30 18:05:08 +0530 | [diff] [blame] | 75 | print "Test Case: Material Receipt Cancellation" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 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): |
Nabin Hait | a970b11 | 2011-09-30 18:05:08 +0530 | [diff] [blame] | 96 | print "Test Case: Material Transfer Note submission" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 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): |
Nabin Hait | a970b11 | 2011-09-30 18:05:08 +0530 | [diff] [blame] | 125 | print "Test Case: Material Transfer Note Cancellation" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 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]]) |
Nabin Hait | a970b11 | 2011-09-30 18:05:08 +0530 | [diff] [blame] | 148 | |
| 149 | #=========================================================================== |
| 150 | # Purpose: Material Issue |
| 151 | #=========================================================================== |
| 152 | def test_mi_onsubmit(self): |
| 153 | print "Test Case: Material Issue submission" |
| 154 | |
| 155 | self.save_stock_entry('Material Receipt') |
| 156 | mr = get_obj('Stock Entry', stock_entry.mr[0].name, with_children=1) |
| 157 | mr = self.submit_stock_entry(mr) |
| 158 | |
| 159 | self.save_stock_entry('Material Issue') |
| 160 | mi = get_obj('Stock Entry', stock_entry.mi[0].name, with_children=1) |
| 161 | mi = self.submit_stock_entry(mi) |
| 162 | |
| 163 | # stock ledger entry |
| 164 | print "Checking stock ledger entry........." |
| 165 | self.assertDoc(self.get_expected_sle('mi_submit')) |
| 166 | |
| 167 | # bin qty |
| 168 | print "Checking Bin qty........." |
| 169 | self.assertDoc([ |
| 170 | {'doctype':'Bin', 'actual_qty':6, 'item_code':'it', 'warehouse':'wh1'} |
| 171 | ]) |
| 172 | |
| 173 | # serial no |
| 174 | self.assertCount([ |
| 175 | [{'doctype': 'Serial No', 'item_code': 'it', 'warehouse': 'wh1', 'status': 'In Store', 'docstatus': 0}, 6] |
| 176 | ]) |
| 177 | |
| 178 | #=========================================================================== |
| 179 | def test_mi_oncancel(self): |
| 180 | print "Test Case: Material Issue Cancellation" |
| 181 | |
| 182 | self.save_stock_entry('Material Receipt') |
| 183 | mr = get_obj('Stock Entry', stock_entry.mr[0].name, with_children=1) |
| 184 | mr = self.submit_stock_entry(mr) |
| 185 | |
| 186 | self.save_stock_entry('Material Issue') |
| 187 | mi = get_obj('Stock Entry', stock_entry.mi[0].name, with_children=1) |
| 188 | self.cancel_stock_entry(mi) |
| 189 | |
| 190 | # stock ledger entry |
| 191 | print "Checking stock ledger entry........." |
| 192 | self.assertDoc(self.get_expected_sle('mi_cancel')) |
| 193 | |
| 194 | # bin qty |
| 195 | print "Checking Bin qty........." |
| 196 | self.assertDoc([ |
| 197 | {'doctype':'Bin', 'actual_qty':10, 'item_code':'it', 'warehouse':'wh1'} |
| 198 | ]) |
| 199 | |
| 200 | # serial no |
| 201 | self.assertCount([ |
| 202 | [{'doctype': 'Serial No', 'item_code': 'it', 'warehouse': 'wh1', 'status': 'In Store', 'docstatus': 0}, 10] |
| 203 | ]) |
| 204 | |
| 205 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 206 | |
| 207 | #=========================================================================== |
| 208 | def save_stock_entry(self, t): |
| 209 | if t == 'Material Receipt': |
| 210 | data = stock_entry.mr |
| 211 | elif t == 'Material Transfer': |
| 212 | data = stock_entry.mtn |
Nabin Hait | a970b11 | 2011-09-30 18:05:08 +0530 | [diff] [blame] | 213 | elif t == 'Material Issue': |
| 214 | data = stock_entry.mi |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 215 | |
| 216 | for each in data: |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 217 | each.save(1) |
| 218 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 219 | for t in data[1:]: |
| 220 | sql("update `tabStock Entry Detail` set parent = '%s' where name = '%s'" % (data[0].name, t.name)) |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 221 | print "Stock Entry Created" |
| 222 | |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 223 | |
| 224 | #=========================================================================== |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 225 | def submit_stock_entry(self, ste): |
| 226 | ste.validate() |
| 227 | ste.on_submit() |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 228 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 229 | ste.doc.docstatus = 1 |
| 230 | ste.doc.save() |
| 231 | |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 232 | print "Stock Entry Submitted" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 233 | return ste |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 234 | |
| 235 | #=========================================================================== |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 236 | def cancel_stock_entry(self, ste): |
| 237 | ste = self.submit_stock_entry(ste) |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 238 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 239 | ste.on_cancel() |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 240 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 241 | ste.doc.cancel_reason = "testing" |
| 242 | ste.doc.docstatus = 2 |
| 243 | ste.doc.save() |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 244 | |
| 245 | print "Stock Entry Cancelled" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 246 | return ste |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 247 | |
| 248 | #=========================================================================== |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 249 | def tearDown(self): |
| 250 | webnotes.conn.rollback() |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 251 | |
| 252 | |
| 253 | # Expected Result Set |
| 254 | #=================================================================================================== |
| 255 | def get_expected_sle(self, action): |
| 256 | expected_sle = { |
| 257 | 'mr_submit': [{ |
| 258 | 'doctype': 'Stock Ledger Entry', |
| 259 | 'item_code':'it', |
| 260 | 'warehouse':'wh1', |
| 261 | 'voucher_type': 'Stock Entry', |
| 262 | 'voucher_no': stock_entry.mr[0].name, |
| 263 | 'actual_qty': 10, |
| 264 | 'bin_aqat': 10, |
| 265 | 'valuation_rate': 100, |
| 266 | 'is_cancelled': 'No' |
| 267 | }], |
| 268 | 'mr_cancel': [{ |
| 269 | 'doctype': 'Stock Ledger Entry', |
| 270 | 'item_code':'it', |
| 271 | 'warehouse':'wh1', |
| 272 | 'voucher_type': 'Stock Entry', |
| 273 | 'voucher_no': stock_entry.mr[0].name, |
| 274 | 'actual_qty': 10, |
| 275 | 'bin_aqat': 10, |
| 276 | 'valuation_rate': 100, |
| 277 | 'is_cancelled': 'Yes' |
| 278 | },{ |
| 279 | 'doctype': 'Stock Ledger Entry', |
| 280 | 'item_code':'it', |
| 281 | 'warehouse':'wh1', |
| 282 | 'voucher_type': 'Stock Entry', |
| 283 | 'voucher_no': stock_entry.mr[0].name, |
| 284 | 'actual_qty': -10, |
| 285 | 'ifnull(bin_aqat, 0)': 0, |
| 286 | 'ifnull(valuation_rate, 0)': 0, |
| 287 | "ifnull(is_cancelled, 'No')": 'Yes' |
| 288 | }], |
| 289 | 'mtn_submit': [{ |
| 290 | 'doctype': 'Stock Ledger Entry', |
| 291 | 'item_code':'it', |
| 292 | 'warehouse':'wh1', |
| 293 | 'voucher_type': 'Stock Entry', |
| 294 | 'voucher_no': stock_entry.mtn[0].name, |
| 295 | 'actual_qty': -5, |
| 296 | 'bin_aqat': 5, |
| 297 | 'valuation_rate': 100, |
| 298 | 'is_cancelled': 'No' |
| 299 | }, { |
| 300 | 'doctype': 'Stock Ledger Entry', |
| 301 | 'item_code':'it', |
| 302 | 'warehouse':'wh2', |
| 303 | 'voucher_type': 'Stock Entry', |
| 304 | 'voucher_no': stock_entry.mtn[0].name, |
| 305 | 'actual_qty': 5, |
| 306 | 'bin_aqat': 5, |
| 307 | 'valuation_rate': 100, |
| 308 | 'is_cancelled': 'No' |
| 309 | }], |
| 310 | 'mtn_cancel': [{ |
| 311 | 'doctype': 'Stock Ledger Entry', |
| 312 | 'item_code':'it', |
| 313 | 'warehouse':'wh1', |
| 314 | 'voucher_type': 'Stock Entry', |
| 315 | 'voucher_no': stock_entry.mtn[0].name, |
| 316 | 'actual_qty': -5, |
| 317 | 'bin_aqat': 5, |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 318 | 'is_cancelled': 'Yes' |
| 319 | }, { |
| 320 | 'doctype': 'Stock Ledger Entry', |
| 321 | 'item_code':'it', |
| 322 | 'warehouse':'wh2', |
| 323 | 'voucher_type': 'Stock Entry', |
| 324 | 'voucher_no': stock_entry.mtn[0].name, |
| 325 | 'actual_qty': 5, |
| 326 | 'bin_aqat': 5, |
| 327 | 'valuation_rate': 100, |
| 328 | 'is_cancelled': 'Yes' |
| 329 | }, { |
| 330 | 'doctype': 'Stock Ledger Entry', |
| 331 | 'item_code':'it', |
| 332 | 'warehouse':'wh1', |
| 333 | 'voucher_type': 'Stock Entry', |
| 334 | 'voucher_no': stock_entry.mtn[0].name, |
| 335 | 'actual_qty': 5, |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 336 | 'is_cancelled': 'Yes' |
| 337 | }, { |
| 338 | 'doctype': 'Stock Ledger Entry', |
| 339 | 'item_code':'it', |
| 340 | 'warehouse':'wh2', |
| 341 | 'voucher_type': 'Stock Entry', |
| 342 | 'voucher_no': stock_entry.mtn[0].name, |
| 343 | 'actual_qty': -5, |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 344 | 'is_cancelled': 'Yes' |
Nabin Hait | a970b11 | 2011-09-30 18:05:08 +0530 | [diff] [blame] | 345 | }], |
| 346 | 'mi_submit': [{'doctype': 'Stock Ledger Entry', |
| 347 | 'item_code':'it', |
| 348 | 'warehouse':'wh1', |
| 349 | 'voucher_type': 'Stock Entry', |
| 350 | 'voucher_no': stock_entry.mi[0].name, |
| 351 | 'actual_qty': -4, |
| 352 | 'bin_aqat': 6, |
| 353 | 'valuation_rate': 100, |
| 354 | 'is_cancelled': 'No' |
| 355 | }], |
| 356 | 'mi_cancel': [{ |
| 357 | 'doctype': 'Stock Ledger Entry', |
| 358 | 'item_code':'it', |
| 359 | 'warehouse':'wh1', |
| 360 | 'voucher_type': 'Stock Entry', |
| 361 | 'voucher_no': stock_entry.mi[0].name, |
| 362 | 'actual_qty': -4, |
| 363 | 'bin_aqat': 6, |
| 364 | 'valuation_rate': 100, |
| 365 | 'is_cancelled': 'Yes' |
| 366 | },{ |
| 367 | 'doctype': 'Stock Ledger Entry', |
| 368 | 'item_code':'it', |
| 369 | 'warehouse':'wh1', |
| 370 | 'voucher_type': 'Stock Entry', |
| 371 | 'voucher_no': stock_entry.mi[0].name, |
| 372 | 'actual_qty': 4, |
| 373 | 'ifnull(bin_aqat, 0)': 0, |
| 374 | 'ifnull(valuation_rate, 0)': 0, |
| 375 | "ifnull(is_cancelled, 'No')": 'Yes' |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 376 | }] |
Nabin Hait | a970b11 | 2011-09-30 18:05:08 +0530 | [diff] [blame] | 377 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | return expected_sle[action] |