public AddOrderResponse AddOrder(Order order) { AddOrderResponse response = new AddOrderResponse(); //Validation if (order.OrderDate < DateTime.Now) { response.Success = false; response.Message = "Order date MUST be in the future!"; return(response); } if (order.Area < 100) { response.Success = false; response.Message = "Order area MUST be greater than 100sqft!"; return(response); } ProductManager productManager = ProductManagerFactory.Create(); ProductLookupResponse productResponse = productManager.GetProduct(order.ProductType); if (!productResponse.Success) { response.Success = false; response.Message = $"Product type: {order.ProductType} does not exist!"; return(response); } StateTaxManager stateTaxManager = StateTaxManagerFactory.Create(); StateLookupResponse taxResponse = stateTaxManager.GetStateTax(order.State); if (!taxResponse.Success) { response.Success = false; response.Message = $"No state tax data found for: {order.State}"; return(response); } bool duplicateOrder = _orderRepository.List(order.OrderDate) .Any(o => o.CustomerName == order.CustomerName && o.State == order.State && o.ProductType == order.ProductType && o.Area == order.Area ); if (duplicateOrder) { response.Success = false; response.Message = "Order entered already exists!"; } else { _orderRepository.AddOrder(order); response.Success = true; response.Order = order; } return(response); }
public SaveOrderResponse SaveOrder(Order order) { SaveOrderResponse response = new SaveOrderResponse(); //Validation if (order.Area < 100) { response.Success = false; response.Message = "Order area MUST be greater than 100sqft!"; return(response); } ProductManager productManager = ProductManagerFactory.Create(); ProductLookupResponse productResponse = productManager.GetProduct(order.ProductType); if (!productResponse.Success) { response.Success = false; response.Message = $"Product type: {order.ProductType} does not exist!"; return(response); } StateTaxManager stateTaxManager = StateTaxManagerFactory.Create(); StateLookupResponse taxResponse = stateTaxManager.GetStateTax(order.State); if (!taxResponse.Success) { response.Success = false; response.Message = $"No state tax data found for: {order.State}"; return(response); } //Save order _orderRepository.SaveOrder(order); if (!order.AreEqualOrders(order, _orderRepository.LoadOrder(order.OrderNumber, order.OrderDate))) { response.Success = false; response.Message = "Failed to save order!"; } else { response.Success = true; } return(response); }
public AddOrderResponse CheckProduct(string productType) { AddOrderResponse response = new AddOrderResponse(); ProductManager productRepo = ProductManagerFactory.Create(); AddOrderRules rules = new AddOrderRules(); response.Product = _productRepository.GetProduct(productType.ToLower()); if (response.Product == null) { response.Success = false; response.Message = $"We do not sell product type \"{productType}\"."; return(response); } else { response.Success = true; } return(response); }
//Takes in an original order, makes a copy, assigns user edits from "editOrder" to the copy and returns copy to be saved public EditOrderResponse EditOrder(Order originalOrder, Order editOrder) { Order order = new Order(originalOrder); EditOrderResponse response = new EditOrderResponse(); //Assign order properties if not blank or different than the original order if (order.CustomerName != editOrder.CustomerName && editOrder.CustomerName != "") { order.CustomerName = editOrder.CustomerName; } if (order.State != editOrder.State && editOrder.State != null) { order.State = editOrder.State; order.TaxRate = editOrder.TaxRate; } if (order.Area != editOrder.Area && editOrder.Area != decimal.MinValue) { order.Area = editOrder.Area; } if (order.ProductType != editOrder.ProductType && editOrder.ProductType != null) { order.ProductType = editOrder.ProductType; order.CostPerSquareFoot = editOrder.CostPerSquareFoot; order.LaborCostPerSquareFoot = editOrder.LaborCostPerSquareFoot; } //Validation or the updated order if (order.OrderDate < DateTime.Now) { response.Success = false; response.Message = "Order date MUST be in the future!"; return(response); } if (order.Area < 100) { response.Success = false; response.Message = "Order area MUST be greater than 100sqft!"; return(response); } ProductManager productManager = ProductManagerFactory.Create(); ProductLookupResponse productResponse = productManager.GetProduct(order.ProductType); if (!productResponse.Success) { response.Success = false; response.Message = $"Product type: {order.ProductType} does not exist!"; return(response); } StateTaxManager stateTaxManager = StateTaxManagerFactory.Create(); StateLookupResponse taxResponse = stateTaxManager.GetStateTax(order.State); if (!taxResponse.Success) { response.Success = false; response.Message = $"No state tax data found for: {order.State}"; return(response); } //lookup order and check for changes OrderLookupResponse lookupResponse = LookupOrder(order.OrderNumber, order.OrderDate); if (lookupResponse.Success) { if (order.AreEqualOrders(lookupResponse.Order, order)) { response.Success = false; response.Message = $"No changes entered for Order: {order.OrderNumber} - {order.OrderDate:MM/dd/yyyy}."; } else { response.Success = true; response.Order = order; } } else { response.Success = false; response.Message = $"{lookupResponse.Message}\nOrder Number and Order Date cannot be changed!"; } return(response); }
private static Response ValidOrder(Order order, bool isNewOrder) { Response response = new Response(); //Setting Success true by default, if errors are found it will be set to false then... response.Success = true; //Validate that each element in the order object (except calculated fields, and OrderStateTax.StateName) contains a value other than the object's default value. response.Message = "The order could not be processed due to the following problems: \n"; foreach (PropertyInfo propertyInfo in order.GetType().GetProperties()) { switch (propertyInfo.Name) { case "OrderDate": if (isNewOrder && order.OrderDate < DateTime.Now.Date) { response.Message += "* OrderDate invalid! \n"; response.Success = false; } break; case "OrderNumber": if (!isNewOrder && order.OrderNumber == 0) { response.Message += "* OrderNumber invalid! \n"; response.Success = false; } break; case "CustomerName": if (order.CustomerName == "" || order.CustomerName.Length > _maxCustNameLength || !(order.CustomerName.All(c => char.IsLetterOrDigit(c) || c == ' ' || c == '.' || c == ','))) { response.Message += "* CustomerName invalid! \n"; response.Success = false; } ; break; case "OrderStateTax": TaxManager myTM = TaxManagerFactory.Create(); TaxResponse myTR = new TaxResponse(); myTR = myTM.GetTaxByState(order.OrderStateTax.StateCode); //if ((!myTR.Success) && (!(order.OrderStateTax.StateCode == myTR.StateTax.StateCode && order.OrderStateTax.TaxRate == myTR.StateTax.TaxRate)) ) //{ // response.Message += "* One or more of the StateTax values was invalid! \n"; // response.Success = false; //} if (!myTR.Success) { response.Message += "* " + myTR.Message + " \nContact IT! \n"; response.Success = false; } else if (!(order.OrderStateTax.StateCode == myTR.StateTax.StateCode && order.OrderStateTax.TaxRate == myTR.StateTax.TaxRate)) { response.Message += "* One or more of the StateTax values was invalid! \n"; response.Success = false; } break; case "OrderProduct": ProductManager myPM = ProductManagerFactory.Create(); ProductResponse myPR = new ProductResponse(); myPR = myPM.GetProductByType(order.OrderProduct.ProductType); if (!myPR.Success && !(order.OrderProduct.ProductType == myPR.Product.ProductType && order.OrderProduct.CostPerSquareFoot == myPR.Product.CostPerSquareFoot && order.OrderProduct.LaborCostPerSquareFoot == myPR.Product.LaborCostPerSquareFoot)) { response.Message += "* One or more of the ProductType values was invalid! \n"; response.Success = false; } break; case "Area": if (order.Area < _minArea || order.Area > _maxArea) { response.Message += "* The Area is invalid! \n"; response.Success = false; } break; case "FileLaborCost": if (order.FileLaborCost != order.CalcLaborCost) //Math.Round(order.Area * order.OrderProduct.LaborCostPerSquareFoot, MidpointRounding.AwayFromZero)) { response.Message += "* The LaborCost is invalid! \n"; response.Success = false; } break; case "FileMaterialCost": if (order.FileMaterialCost != order.CalcMaterialCost) // Math.Round(order.Area * order.OrderProduct.CostPerSquareFoot, MidpointRounding.AwayFromZero)) { response.Message += "* The MaterialCost is invalid! \n"; response.Success = false; } break; case "FileTax": if (order.FileTax != order.CalcTax) //Math.Round((((order.Area * order.OrderProduct.CostPerSquareFoot) + (order.Area * order.OrderProduct.LaborCostPerSquareFoot)) * order.OrderStateTax.TaxRate/100),2, MidpointRounding.AwayFromZero)) { response.Message += "* The TaxCost is invalid! \n"; response.Success = false; } break; case "FileTotal": if (order.FileTotal != order.CalcTotal) //Math.Round(order.FileMaterialCost + order.FileLaborCost + order.FileTax, 2, MidpointRounding.AwayFromZero)) { response.Message += "* The TotalCost is invalid! \n"; response.Success = false; } break; default: break; } } if (response.Success) { response.Message = ""; } else { response.Message = response.Message.Substring(0, response.Message.Length - 1); //Remove final '\n' - counts as 1 char... } return(response); }