From ecb36f2cb0486055ea076432878e99a3fddd5cfb Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Thu, 2 May 2013 11:34:37 +0530 Subject: [PATCH] [purchase] [validation] validate warehouse belongs to company if set --- .../doctype/purchase_order/test_purchase_order.py | 6 ++++++ controllers/buying_controller.py | 13 ++++++++++++- .../material_request/test_material_request.py | 6 ++++++ stock/doctype/warehouse/test_warehouse.py | 3 ++- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/buying/doctype/purchase_order/test_purchase_order.py b/buying/doctype/purchase_order/test_purchase_order.py index bd27f07687..be2e2946a9 100644 --- a/buying/doctype/purchase_order/test_purchase_order.py +++ b/buying/doctype/purchase_order/test_purchase_order.py @@ -27,6 +27,12 @@ class TestPurchaseOrder(unittest.TestCase): po.insert() self.assertEquals(len(po.doclist.get({"parentfield": "po_raw_material_details"})), 2) + def test_warehouse_company_validation(self): + from controllers.buying_controller import WrongWarehouseCompany + po = webnotes.bean(copy=test_records[0]) + po.doc.company = "_Test Company 1" + self.assertRaises(WrongWarehouseCompany, po.insert) + test_dependencies = ["BOM"] diff --git a/controllers/buying_controller.py b/controllers/buying_controller.py index e167dc57b7..cd822e6ecc 100644 --- a/controllers/buying_controller.py +++ b/controllers/buying_controller.py @@ -26,10 +26,13 @@ from webnotes.model.utils import round_floats_in_doc from controllers.stock_controller import StockController +class WrongWarehouseCompany(Exception): pass + class BuyingController(StockController): def validate(self): super(BuyingController, self).validate() self.validate_stock_or_nonstock_items() + self.validate_warehouse_belongs_to_company() if self.meta.get_field("currency"): self.company_currency = get_company_currency(self.doc.company) self.validate_conversion_rate("currency", "conversion_rate") @@ -42,7 +45,15 @@ class BuyingController(StockController): # set total in words self.set_total_in_words() - + + def validate_warehouse_belongs_to_company(self): + for d in self.doclist.get({"warehouse": True}): + warehouse_company = webnotes.conn.get_value("Warehouse", d.warehouse, "company") + if warehouse_company and warehouse_company != self.doc.company: + webnotes.msgprint(_("Warehouse must belong to company") + \ + (": %s (%s, %s)" % (d.warehouse, warehouse_company, self.doc.company)), + raise_exception=WrongWarehouseCompany) + def validate_stock_or_nonstock_items(self): items = [d.item_code for d in self.doclist.get({"parentfield": self.fname})] if self.stock_items: diff --git a/stock/doctype/material_request/test_material_request.py b/stock/doctype/material_request/test_material_request.py index 080989f2d9..f5dbb52bbe 100644 --- a/stock/doctype/material_request/test_material_request.py +++ b/stock/doctype/material_request/test_material_request.py @@ -263,6 +263,12 @@ class TestMaterialRequest(unittest.TestCase): se = webnotes.bean(copy=se_doclist) self.assertRaises(webnotes.MappingMismatchError, se.insert) + def test_warehouse_company_validation(self): + from controllers.buying_controller import WrongWarehouseCompany + mr = webnotes.bean(copy=test_records[0]) + mr.doc.company = "_Test Company 1" + self.assertRaises(WrongWarehouseCompany, mr.insert) + test_records = [ [ { diff --git a/stock/doctype/warehouse/test_warehouse.py b/stock/doctype/warehouse/test_warehouse.py index 26501beab9..f3a04581a9 100644 --- a/stock/doctype/warehouse/test_warehouse.py +++ b/stock/doctype/warehouse/test_warehouse.py @@ -2,7 +2,8 @@ test_records = [ [{ "doctype": "Warehouse", "warehouse_name": "_Test Warehouse", - "warehouse_type": "_Test Warehouse Type" + "warehouse_type": "_Test Warehouse Type", + "company": "_Test Company" }], [{ "doctype": "Warehouse",