From f8924bcf6febd806b4daf214057e371265b67e2c Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 15 Oct 2013 12:19:25 +0530 Subject: [PATCH 1/2] [fix] [minor] update serial no in sle while creating auto serial no --- .../sales_invoice/test_sales_invoice.py | 10 +++--- .../purchase_order/test_purchase_order.py | 4 +-- controllers/buying_controller.py | 2 -- .../delivery_note/test_delivery_note.py | 6 ++-- .../material_request/test_material_request.py | 4 +-- .../purchase_receipt/purchase_receipt.py | 4 +-- stock/doctype/serial_no/serial_no.py | 36 +++++++++++-------- stock/doctype/stock_entry/stock_entry.py | 2 +- stock/doctype/stock_entry/test_stock_entry.py | 7 ++-- .../stock_ledger_entry/stock_ledger_entry.py | 5 +-- 10 files changed, 42 insertions(+), 38 deletions(-) diff --git a/accounts/doctype/sales_invoice/test_sales_invoice.py b/accounts/doctype/sales_invoice/test_sales_invoice.py index dee098a3e4..fbb344ec94 100644 --- a/accounts/doctype/sales_invoice/test_sales_invoice.py +++ b/accounts/doctype/sales_invoice/test_sales_invoice.py @@ -641,8 +641,8 @@ class TestSalesInvoice(unittest.TestCase): return new_si - # if yearly, test 3 repetitions, else test 13 repetitions - count = 3 if no_of_months == 12 else 13 + # if yearly, test 3 repetitions, else test 5 repetitions + count = 1 if (no_of_months == 12) else 5 for i in xrange(count): base_si = _test(i) @@ -653,7 +653,7 @@ class TestSalesInvoice(unittest.TestCase): def test_serialized(self): from stock.doctype.stock_entry.test_stock_entry import make_serialized_item - from stock.doctype.stock_ledger_entry.stock_ledger_entry import get_serial_nos + from stock.doctype.serial_no.serial_no import get_serial_nos se = make_serialized_item() serial_nos = get_serial_nos(se.doclist[1].serial_no) @@ -674,7 +674,7 @@ class TestSalesInvoice(unittest.TestCase): return si def test_serialized_cancel(self): - from stock.doctype.stock_ledger_entry.stock_ledger_entry import get_serial_nos + from stock.doctype.serial_no.serial_no import get_serial_nos si = self.test_serialized() si.cancel() @@ -686,7 +686,7 @@ class TestSalesInvoice(unittest.TestCase): "delivery_document_no")) def test_serialize_status(self): - from stock.doctype.stock_ledger_entry.stock_ledger_entry import SerialNoStatusError, get_serial_nos + from stock.doctype.serial_no.serial_no import SerialNoStatusError, get_serial_nos from stock.doctype.stock_entry.test_stock_entry import make_serialized_item se = make_serialized_item() diff --git a/buying/doctype/purchase_order/test_purchase_order.py b/buying/doctype/purchase_order/test_purchase_order.py index cef5e4a207..bd5f410f6e 100644 --- a/buying/doctype/purchase_order/test_purchase_order.py +++ b/buying/doctype/purchase_order/test_purchase_order.py @@ -98,11 +98,11 @@ class TestPurchaseOrder(unittest.TestCase): self.assertEquals(len(po.doclist.get({"parentfield": "po_raw_material_details"})), 2) def test_warehouse_company_validation(self): - from controllers.buying_controller import WrongWarehouseCompany + from stock.utils import InvalidWarehouseCompany po = webnotes.bean(copy=test_records[0]) po.doc.company = "_Test Company 1" po.doc.conversion_rate = 0.0167 - self.assertRaises(WrongWarehouseCompany, po.insert) + self.assertRaises(InvalidWarehouseCompany, po.insert) def test_uom_integer_validation(self): from utilities.transaction_base import UOMMustBeIntegerError diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py index 25d76aa66d..fd3428e6ad 100644 --- a/controllers/buying_controller.py +++ b/controllers/buying_controller.py @@ -11,8 +11,6 @@ from setup.utils import get_company_currency from controllers.stock_controller import StockController -class WrongWarehouseCompany(Exception): pass - class BuyingController(StockController): def onload_post_render(self): # contact, address, item details diff --git a/stock/doctype/delivery_note/test_delivery_note.py b/stock/doctype/delivery_note/test_delivery_note.py index 7c525504ae..ca95a50602 100644 --- a/stock/doctype/delivery_note/test_delivery_note.py +++ b/stock/doctype/delivery_note/test_delivery_note.py @@ -160,7 +160,7 @@ class TestDeliveryNote(unittest.TestCase): def test_serialized(self): from stock.doctype.stock_entry.test_stock_entry import make_serialized_item - from stock.doctype.stock_ledger_entry.stock_ledger_entry import get_serial_nos + from stock.doctype.serial_no.serial_no import get_serial_nos se = make_serialized_item() serial_nos = get_serial_nos(se.doclist[1].serial_no) @@ -180,7 +180,7 @@ class TestDeliveryNote(unittest.TestCase): return dn def test_serialized_cancel(self): - from stock.doctype.stock_ledger_entry.stock_ledger_entry import get_serial_nos + from stock.doctype.serial_no.serial_no import get_serial_nos dn = self.test_serialized() dn.cancel() @@ -192,7 +192,7 @@ class TestDeliveryNote(unittest.TestCase): "delivery_document_no")) def test_serialize_status(self): - from stock.doctype.stock_ledger_entry.stock_ledger_entry import SerialNoStatusError, get_serial_nos + from stock.doctype.serial_no.serial_no import SerialNoStatusError, get_serial_nos from stock.doctype.stock_entry.test_stock_entry import make_serialized_item se = make_serialized_item() diff --git a/stock/doctype/material_request/test_material_request.py b/stock/doctype/material_request/test_material_request.py index 8ebad15810..13fdd02c3c 100644 --- a/stock/doctype/material_request/test_material_request.py +++ b/stock/doctype/material_request/test_material_request.py @@ -315,10 +315,10 @@ class TestMaterialRequest(unittest.TestCase): self.assertRaises(webnotes.MappingMismatchError, se.insert) def test_warehouse_company_validation(self): - from controllers.buying_controller import WrongWarehouseCompany + from stock.utils import InvalidWarehouseCompany mr = webnotes.bean(copy=test_records[0]) mr.doc.company = "_Test Company 1" - self.assertRaises(WrongWarehouseCompany, mr.insert) + self.assertRaises(InvalidWarehouseCompany, mr.insert) test_dependencies = ["Currency Exchange"] test_records = [ diff --git a/stock/doctype/purchase_receipt/purchase_receipt.py b/stock/doctype/purchase_receipt/purchase_receipt.py index 6169b1d916..6d4320fe18 100644 --- a/stock/doctype/purchase_receipt/purchase_receipt.py +++ b/stock/doctype/purchase_receipt/purchase_receipt.py @@ -7,7 +7,7 @@ import webnotes from webnotes.utils import cstr, flt, cint from webnotes.model.bean import getlist from webnotes.model.code import get_obj -from webnotes import msgprint +from webnotes import msgprint, _ import webnotes.defaults from stock.utils import update_bin @@ -247,7 +247,7 @@ class DocType(BuyingController): self.update_stock() from stock.doctype.serial_no.serial_no import update_serial_nos_after_submit - update_serial_nos_after_submit(self, "Purchase Receipt", "purchase_receipt_details") + update_serial_nos_after_submit(self, "purchase_receipt_details") purchase_controller.update_last_purchase_rate(self, 1) diff --git a/stock/doctype/serial_no/serial_no.py b/stock/doctype/serial_no/serial_no.py index d806287ecb..f665abc46f 100644 --- a/stock/doctype/serial_no/serial_no.py +++ b/stock/doctype/serial_no/serial_no.py @@ -37,10 +37,7 @@ class DocType(StockController): self.validate_warehouse() self.validate_item() - if self.via_stock_ledger: - self.set_status() - self.set_purchase_details() - self.set_sales_details() + self.on_stock_ledger_entry() def validate_amc_status(self): """ @@ -181,6 +178,12 @@ class DocType(StockController): webnotes.conn.sql("""update `tab%s` set serial_no = %s where name=%s""" % (dt[0], '%s', '%s'), ('\n'.join(serial_nos), item[0])) + + def on_stock_ledger_entry(self): + if self.via_stock_ledger and not self.doc.fields.get("__islocal"): + self.set_status() + self.set_purchase_details() + self.set_sales_details() def process_serial_no(sle): item_det = get_item_details(sle.item_code) @@ -233,6 +236,14 @@ def validate_serial_no(sle, item_det): + sle.item_code), SerialNoRequiredError) def update_serial_nos(sle, item_det): + if not sle.serial_no and sle.actual_qty > 0 and item_det.serial_no_series: + from webnotes.model.doc import make_autoname + serial_nos = [] + for i in xrange(cint(sle.actual_qty)): + serial_nos.append(make_autoname(item_det.serial_no_series)) + + webnotes.conn.set(sle, "serial_no", "\n".join(serial_nos)) + if sle.serial_no: serial_nos = get_serial_nos(sle.serial_no) for serial_no in serial_nos: @@ -243,12 +254,6 @@ def update_serial_nos(sle, item_det): sr.save() elif sle.actual_qty > 0: make_serial_no(serial_no, sle) - elif sle.actual_qty > 0 and item_det.serial_no_series: - from webnotes.model.doc import make_autoname - serial_nos = [] - for i in xrange(cint(sle.actual_qty)): - serial_nos.append(make_serial_no(make_autoname(item_det.serial_no_series), sle)) - sle.serial_no = "\n".join(serial_nos) def get_item_details(item_code): return webnotes.conn.sql("""select name, has_batch_no, docstatus, @@ -270,13 +275,16 @@ def make_serial_no(serial_no, sle): webnotes.msgprint(_("Serial No created") + ": " + sr.doc.name) return sr.doc.name -def update_serial_nos_after_submit(controller, parenttype, parentfield): - if not hasattr(webnotes, "new_stock_ledger_entries"): - return +def update_serial_nos_after_submit(controller, parentfield): + stock_ledger_entries = webnotes.conn.sql("""select voucher_detail_no, serial_no + from `tabStock Ledger Entry` where voucher_type=%s and voucher_no=%s""", + (controller.doc.doctype, controller.doc.name), as_dict=True) + if not stock_ledger_entries: return + for d in controller.doclist.get({"parentfield": parentfield}): serial_no = None - for sle in webnotes.new_stock_ledger_entries: + for sle in stock_ledger_entries: if sle.voucher_detail_no==d.name: serial_no = sle.serial_no break diff --git a/stock/doctype/stock_entry/stock_entry.py b/stock/doctype/stock_entry/stock_entry.py index 7d8130cc85..c620bd9c8e 100644 --- a/stock/doctype/stock_entry/stock_entry.py +++ b/stock/doctype/stock_entry/stock_entry.py @@ -55,7 +55,7 @@ class DocType(StockController): self.update_stock_ledger() from stock.doctype.serial_no.serial_no import update_serial_nos_after_submit - update_serial_nos_after_submit(self, "Stock Entry", "mtn_details") + update_serial_nos_after_submit(self, "mtn_details") self.update_production_order() self.make_gl_entries() diff --git a/stock/doctype/stock_entry/test_stock_entry.py b/stock/doctype/stock_entry/test_stock_entry.py index b41a6269ad..2882072852 100644 --- a/stock/doctype/stock_entry/test_stock_entry.py +++ b/stock/doctype/stock_entry/test_stock_entry.py @@ -44,9 +44,10 @@ class TestStockEntry(unittest.TestCase): def test_warehouse_company_validation(self): self._clear_stock_account_balance() - webnotes.session.user = "test2@example.com" webnotes.bean("Profile", "test2@example.com").get_controller()\ .add_roles("Sales User", "Sales Manager", "Material User", "Material Manager") + + webnotes.session.user = "test2@example.com" from stock.utils import InvalidWarehouseCompany st1 = webnotes.bean(copy=test_records[0]) @@ -65,14 +66,14 @@ class TestStockEntry(unittest.TestCase): webnotes.bean("Profile", "test2@example.com").get_controller()\ .add_roles("Sales User", "Sales Manager", "Material User", "Material Manager") - + webnotes.session.user = "test@example.com" + st1 = webnotes.bean(copy=test_records[0]) st1.doc.company = "_Test Company 1" st1.doclist[1].t_warehouse="_Test Warehouse 2 - _TC1" st1.insert() self.assertRaises(UserNotAllowedForWarehouse, st1.submit) - webnotes.session.user = "test2@example.com" st1 = webnotes.bean(copy=test_records[0]) st1.doc.company = "_Test Company 1" st1.doclist[1].t_warehouse="_Test Warehouse 2 - _TC1" diff --git a/stock/doctype/stock_ledger_entry/stock_ledger_entry.py b/stock/doctype/stock_ledger_entry/stock_ledger_entry.py index bfb0f0aa92..1738efcf3f 100644 --- a/stock/doctype/stock_ledger_entry/stock_ledger_entry.py +++ b/stock/doctype/stock_ledger_entry/stock_ledger_entry.py @@ -14,10 +14,7 @@ class DocType(DocListController): def validate(self): from stock.utils import validate_warehouse_user, validate_warehouse_company - if not hasattr(webnotes, "new_stock_ledger_entries"): - webnotes.new_stock_ledger_entries = [] - - webnotes.new_stock_ledger_entries.append(self.doc) + self.validate_mandatory() self.validate_item() validate_warehouse_user(self.doc.warehouse) From 5101098020fe18d31a965e4a249d682bda2224dc Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Tue, 15 Oct 2013 13:00:53 +0530 Subject: [PATCH 2/2] [fix] [minor] fixes in testcases --- stock/doctype/stock_entry/test_stock_entry.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/stock/doctype/stock_entry/test_stock_entry.py b/stock/doctype/stock_entry/test_stock_entry.py index 2882072852..3a18c69693 100644 --- a/stock/doctype/stock_entry/test_stock_entry.py +++ b/stock/doctype/stock_entry/test_stock_entry.py @@ -43,6 +43,7 @@ class TestStockEntry(unittest.TestCase): webnotes.conn.set_default("company", self.old_default_company) def test_warehouse_company_validation(self): + set_perpetual_inventory(0) self._clear_stock_account_balance() webnotes.bean("Profile", "test2@example.com").get_controller()\ .add_roles("Sales User", "Sales Manager", "Material User", "Material Manager") @@ -58,22 +59,23 @@ class TestStockEntry(unittest.TestCase): webnotes.session.user = "Administrator" def test_warehouse_user(self): + set_perpetual_inventory(0) from stock.utils import UserNotAllowedForWarehouse - webnotes.session.user = "test@example.com" webnotes.bean("Profile", "test@example.com").get_controller()\ .add_roles("Sales User", "Sales Manager", "Material User", "Material Manager") webnotes.bean("Profile", "test2@example.com").get_controller()\ .add_roles("Sales User", "Sales Manager", "Material User", "Material Manager") - webnotes.session.user = "test@example.com" + webnotes.session.user = "test@example.com" st1 = webnotes.bean(copy=test_records[0]) st1.doc.company = "_Test Company 1" st1.doclist[1].t_warehouse="_Test Warehouse 2 - _TC1" st1.insert() self.assertRaises(UserNotAllowedForWarehouse, st1.submit) + webnotes.session.user = "test2@example.com" st1 = webnotes.bean(copy=test_records[0]) st1.doc.company = "_Test Company 1" st1.doclist[1].t_warehouse="_Test Warehouse 2 - _TC1"