/// <summary> /// Create Purchase Order Detail /// </summary> /// <param name="purchaseOrderDetailDataTransformation"></param> /// <returns></returns> public async Task <ResponseModel <PurchaseOrderDetailDataTransformation> > CreatePurchaseOrderDetail(PurchaseOrderDetailDataTransformation purchaseOrderDetailDataTransformation) { ResponseModel <PurchaseOrderDetailDataTransformation> returnResponse = new ResponseModel <PurchaseOrderDetailDataTransformation>(); PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail(); try { int accountId = purchaseOrderDetailDataTransformation.AccountId; int purchaseOrderId = purchaseOrderDetailDataTransformation.PurchaseOrderId; _purchaseOrderManagementDataService.OpenConnection(_connectionStrings.PrimaryDatabaseConnectionString); _purchaseOrderManagementDataService.BeginTransaction((int)IsolationLevel.ReadCommitted); PurchaseOrderDetailBusinessRules <PurchaseOrderDetailDataTransformation> purchaseOrderDetailBusinessRules = new PurchaseOrderDetailBusinessRules <PurchaseOrderDetailDataTransformation>(purchaseOrderDetailDataTransformation); ValidationResult validationResult = purchaseOrderDetailBusinessRules.Validate(); if (validationResult.ValidationStatus == false) { _purchaseOrderManagementDataService.RollbackTransaction(); returnResponse.ReturnMessage = validationResult.ValidationMessages; returnResponse.ReturnStatus = validationResult.ValidationStatus; return(returnResponse); } PurchaseOrder purchaseOrder = await _purchaseOrderManagementDataService.GetPurchaseOrderHeader(accountId, purchaseOrderId); if (purchaseOrder == null) { _purchaseOrderManagementDataService.RollbackTransaction(); returnResponse.ReturnMessage.Add("Purchase Order not found"); returnResponse.ReturnStatus = false; return(returnResponse); } double lineItemAmount = purchaseOrderDetailDataTransformation.UnitPrice * purchaseOrderDetailDataTransformation.OrderQuantity; purchaseOrder.OrderTotal = purchaseOrder.OrderTotal + lineItemAmount; purchaseOrderDetailDataTransformation.OrderTotal = purchaseOrder.OrderTotal; await _purchaseOrderManagementDataService.UpdatePurchaseOrderHeader(purchaseOrder); purchaseOrderDetail.ProductId = purchaseOrderDetailDataTransformation.ProductId; purchaseOrderDetail.PurchaseOrderId = purchaseOrderDetailDataTransformation.PurchaseOrderId; purchaseOrderDetail.UnitPrice = purchaseOrderDetailDataTransformation.UnitPrice; purchaseOrderDetail.OrderQuantity = purchaseOrderDetailDataTransformation.OrderQuantity; await _purchaseOrderManagementDataService.CreatePurchaseOrderDetail(purchaseOrderDetail); //await _purchaseOrderManagementDataService.UpdateDatabase(); _purchaseOrderManagementDataService.CommitTransaction(); PurchaseOrderDetail updatedPurchaseOrderDetail = await _purchaseOrderManagementDataService.GetPurchaseOrderDetail(purchaseOrderDetail.PurchaseOrderDetailId); purchaseOrderDetailDataTransformation = new PurchaseOrderDetailDataTransformation(); purchaseOrderDetailDataTransformation.PurchaseOrderDetailId = updatedPurchaseOrderDetail.PurchaseOrderDetailId; purchaseOrderDetailDataTransformation.PurchaseOrderId = updatedPurchaseOrderDetail.PurchaseOrderId; purchaseOrderDetailDataTransformation.ProductId = updatedPurchaseOrderDetail.ProductId; purchaseOrderDetailDataTransformation.ProductMasterId = updatedPurchaseOrderDetail.Product.ProductMasterId; purchaseOrderDetailDataTransformation.ProductNumber = updatedPurchaseOrderDetail.Product.ProductNumber; purchaseOrderDetailDataTransformation.ProductDescription = updatedPurchaseOrderDetail.Product.Description; purchaseOrderDetailDataTransformation.UnitPrice = updatedPurchaseOrderDetail.UnitPrice; purchaseOrderDetailDataTransformation.OrderQuantity = updatedPurchaseOrderDetail.OrderQuantity; purchaseOrderDetailDataTransformation.OrderTotal = purchaseOrder.OrderTotal; returnResponse.ReturnStatus = true; } catch (Exception ex) { _purchaseOrderManagementDataService.RollbackTransaction(); returnResponse.ReturnStatus = false; returnResponse.ReturnMessage.Add(ex.Message); } finally { _purchaseOrderManagementDataService.CloseConnection(); } returnResponse.Entity = purchaseOrderDetailDataTransformation; return(returnResponse); }