예제 #1
0
        /// <summary>
        /// Delete Purchase Order Detail
        /// </summary>
        /// <param name="purchaseOrderDetailDataTransformation"></param>
        /// <returns></returns>
        public async Task <ResponseModel <PurchaseOrderDetailDataTransformation> > DeletePurchaseOrderDetail(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;

                _purchaseOrderManagementDataService.OpenConnection(_connectionStrings.PrimaryDatabaseConnectionString);
                _purchaseOrderManagementDataService.BeginTransaction((int)IsolationLevel.ReadCommitted);

                PurchaseOrder purchaseOrder = await _purchaseOrderManagementDataService.GetPurchaseOrderHeader(accountId, purchaseOrderId);

                if (purchaseOrder == null)
                {
                    _purchaseOrderManagementDataService.RollbackTransaction();

                    returnResponse.ReturnMessage.Add("Purchase Order not found");
                    returnResponse.ReturnStatus = false;

                    return(returnResponse);
                }

                purchaseOrderDetail = await _purchaseOrderManagementDataService.GetPurchaseOrderDetailForUpdate(purchaseOrderDetailId);

                if (purchaseOrderDetail == null)
                {
                    _purchaseOrderManagementDataService.RollbackTransaction();

                    returnResponse.ReturnMessage.Add("Purchase Order Detail not found");
                    returnResponse.ReturnStatus = false;

                    return(returnResponse);
                }

                double originalLineItemAmount = purchaseOrderDetail.OrderQuantity * purchaseOrderDetail.UnitPrice;

                await _purchaseOrderManagementDataService.DeletePurchaseOrderDetail(purchaseOrderDetailId);

                purchaseOrder.OrderTotal = purchaseOrder.OrderTotal - originalLineItemAmount;

                purchaseOrderDetailDataTransformation.OrderTotal = purchaseOrder.OrderTotal;

                await _purchaseOrderManagementDataService.UpdatePurchaseOrderHeader(purchaseOrder);

                purchaseOrderDetail.UnitPrice     = purchaseOrderDetailDataTransformation.UnitPrice;
                purchaseOrderDetail.OrderQuantity = purchaseOrderDetailDataTransformation.OrderQuantity;

                await _purchaseOrderManagementDataService.UpdatePurchaseOrderDetail(purchaseOrderDetail);

                await _purchaseOrderManagementDataService.UpdateDatabase();

                _purchaseOrderManagementDataService.CommitTransaction();

                returnResponse.ReturnStatus = true;
            }
            catch (Exception ex)
            {
                _purchaseOrderManagementDataService.RollbackTransaction();
                returnResponse.ReturnStatus = false;
                returnResponse.ReturnMessage.Add(ex.Message);
            }
            finally
            {
                _purchaseOrderManagementDataService.CloseConnection();
            }

            returnResponse.Entity = purchaseOrderDetailDataTransformation;

            return(returnResponse);
        }