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): |
| 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 Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 157 | each.save(1) |
| 158 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 159 | for t in data[1:]: |
| 160 | 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] | 161 | print "Stock Entry Created" |
| 162 | |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 163 | |
| 164 | #=========================================================================== |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 165 | def submit_stock_entry(self, ste): |
| 166 | ste.validate() |
| 167 | ste.on_submit() |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 168 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 169 | ste.doc.docstatus = 1 |
| 170 | ste.doc.save() |
| 171 | |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 172 | print "Stock Entry Submitted" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 173 | return ste |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 174 | |
| 175 | #=========================================================================== |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 176 | def cancel_stock_entry(self, ste): |
| 177 | ste = self.submit_stock_entry(ste) |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 178 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 179 | ste.on_cancel() |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 180 | |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 181 | ste.doc.cancel_reason = "testing" |
| 182 | ste.doc.docstatus = 2 |
| 183 | ste.doc.save() |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 184 | |
| 185 | print "Stock Entry Cancelled" |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 186 | return ste |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 187 | |
| 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 Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 199 | |
Nabin Hait | 31665e5 | 2011-09-29 10:41:02 +0530 | [diff] [blame] | 200 | #=========================================================================== |
| 201 | def tearDown(self): |
| 202 | webnotes.conn.rollback() |
Nabin Hait | 88f9cb5 | 2011-09-30 17:20:06 +0530 | [diff] [blame^] | 203 | |
| 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] |