/// <summary> /// Update Purchase Order Detail /// </summary> /// <param name="purchaseOrderDetailDataTransformation"></param> /// <returns></returns> public async Task <ResponseModel <PurchaseOrderDetailDataTransformation> > UpdatePurchaseOrderDetail(PurchaseOrderDetailDataTransformation purchaseOrderDetailDataTransformation) { ResponseModel <PurchaseOrderDetailDataTransformation> returnResponse = new ResponseModel <PurchaseOrderDetailDataTransformation>(); PurchaseOrderDetail purchaseOrderDetail = new PurchaseOrderDetail(); try { int accountId = purchaseOrderDetailDataTransformation.AccountId; int purchaseOrderId = purchaseOrderDetailDataTransformation.PurchaseOrderId; int purchaseOrderDetailId = purchaseOrderDetailDataTransformation.PurchaseOrderDetailId; if (purchaseOrderDetailDataTransformation.CurrentReceivedQuantity == 0) { returnResponse.ReturnMessage.Add("Invalid Received Quantity"); returnResponse.ReturnStatus = false; return(returnResponse); } _inventoryManagementDataService.OpenConnection(_connectionStrings.PrimaryDatabaseConnectionString); _inventoryManagementDataService.BeginTransaction((int)IsolationLevel.Serializable); PurchaseOrder purchaseOrder = await _inventoryManagementDataService.GetPurchaseOrderHeader(accountId, purchaseOrderId); if (purchaseOrder == null) { _inventoryManagementDataService.RollbackTransaction(); returnResponse.ReturnMessage.Add("Purchase Order not found"); returnResponse.ReturnStatus = false; return(returnResponse); } purchaseOrderDetail = await _inventoryManagementDataService.GetPurchaseOrderDetailForUpdate(purchaseOrderDetailId); if (purchaseOrderDetail == null) { _inventoryManagementDataService.RollbackTransaction(); returnResponse.ReturnMessage.Add("Purchase Order Detail not found"); returnResponse.ReturnStatus = false; return(returnResponse); } purchaseOrderDetail.ReceivedQuantity = purchaseOrderDetail.ReceivedQuantity + purchaseOrderDetailDataTransformation.CurrentReceivedQuantity; await _inventoryManagementDataService.UpdatePurchaseOrderDetail(purchaseOrderDetail); Product product = await _inventoryManagementDataService.GetProductInformationForUpdate(purchaseOrderDetail.ProductId); if (product == null) { _inventoryManagementDataService.RollbackTransaction(); returnResponse.ReturnMessage.Add("Product not found"); returnResponse.ReturnStatus = false; return(returnResponse); } double newAverageCost = CalculateAverageCost(product.OnHandQuantity, product.AverageCost, purchaseOrderDetailDataTransformation.CurrentReceivedQuantity, purchaseOrderDetail.UnitPrice); if (newAverageCost != 0) { product.AverageCost = newAverageCost; } product.OnHandQuantity = product.OnHandQuantity + purchaseOrderDetailDataTransformation.CurrentReceivedQuantity; await _inventoryManagementDataService.UpdateProduct(product); InventoryTransaction inventoryTransaction = new InventoryTransaction(); inventoryTransaction.EntityId = purchaseOrderDetail.PurchaseOrderDetailId; inventoryTransaction.MasterEntityId = purchaseOrderDetail.MasterPurchaseOrderDetailId; inventoryTransaction.ProductId = purchaseOrderDetail.ProductId; inventoryTransaction.UnitCost = purchaseOrderDetail.UnitPrice; inventoryTransaction.Quantity = purchaseOrderDetailDataTransformation.CurrentReceivedQuantity; inventoryTransaction.TransactionDate = DateTime.UtcNow; await _inventoryManagementDataService.CreateInventoryTransaction(inventoryTransaction); TransactionQueueOutbound transactionQueue = new TransactionQueueOutbound(); transactionQueue.Payload = GenerateInventoryTransactionPayload(inventoryTransaction); transactionQueue.TransactionCode = TransactionQueueTypes.InventoryReceived; transactionQueue.ExchangeName = MessageQueueExchanges.InventoryManagement; await _inventoryManagementDataService.CreateOutboundTransactionQueue(transactionQueue); //await _inventoryManagementDataService.UpdateDatabase(); _inventoryManagementDataService.CommitTransaction(); returnResponse.ReturnStatus = true; } catch (Exception ex) { _inventoryManagementDataService.RollbackTransaction(); returnResponse.ReturnStatus = false; returnResponse.ReturnMessage.Add(ex.Message); } finally { _inventoryManagementDataService.CloseConnection(); } returnResponse.Entity = purchaseOrderDetailDataTransformation; return(returnResponse); }