public async Task <ReturnItemOutput> Execute(ReturnItemInput input)
        {
            var partInstance = await this._partInstanceRepository.GetEntityAsync(e => e.Id == input.PartInstanceId);

            var location = await this._locationProvider.GetEntityAsync(e => e.Id == input.LocationId);

            var refTransaction = await this._transactionRepository.GetEntityAsync(e => e.Id == input.ReferenceTransactionId);

            //var bubblerParam
            if (partInstance != null && location != null && refTransaction != null)
            {
                if (input.IsBubbler)
                {
                    return(await this.ExecuteBubbler(partInstance, location, refTransaction, input));
                }
                else
                {
                    return(await this.ExecuteStandard(partInstance, location, refTransaction, input));
                }
            }
            else
            {
                return(new ReturnItemOutput(null, false, "PartInstance or Location Not Found"));
            }
        }
        private async Task <ReturnItemOutput> ExecuteStandard(PartInstance partInstance, Location location, Transaction refTransaction, ReturnItemInput item)
        {
            partInstance.UpdateQuantity(item.Quantity);

            partInstance.LocationId = location.Id;
            if (item.ConditionId != 0)
            {
                var condition = await this._categoryRepository.GetEntityAsync(e => e.Id == item.ConditionId);

                if (condition != null)
                {
                    partInstance.ConditionId = condition.Id;
                }
            }

            if (!partInstance.StockType.IsDefault)
            {
                var stockType = (StockType)await this._categoryRepository.GetEntityAsync(e => e.Id == partInstance.StockTypeId);

                if (stockType != null)
                {
                    stockType.Quantity += partInstance.Quantity;
                    var updated = await this._categoryRepository.UpdateAsync(stockType);

                    if (updated == null)
                    {
                        await this._unitOfWork.Undo();

                        return(new ReturnItemOutput(null, false, "Error: Could not adjust stock, Please contact administrator"));
                    }
                }
                else
                {
                    await this._unitOfWork.Undo();

                    return(new ReturnItemOutput(null, false, "Error: Could not adjust stock, Please contact administrator"));
                }
            }

            Transaction transaction = new Transaction(partInstance, InventoryAction.RETURNING,
                                                      0, 0, location, item.TimeStamp);

            transaction.Quantity = item.Quantity;

            if (partInstance.CostReported)
            {
                transaction.UnitCost  = refTransaction.UnitCost;
                transaction.TotalCost = refTransaction.TotalCost;
            }
            else
            {
                transaction.UnitCost  = 0;
                transaction.TotalCost = 0;
            }

            transaction.ReferenceTransactionId = item.ReferenceTransactionId;
            transaction.SessionId = this._userService.CurrentSessionId.Value;

            var instance = await this._partInstanceRepository.UpdateAsync(partInstance);

            var trans = await this._transactionRepository.AddAsync(transaction);

            StringBuilder builder = new StringBuilder();

            if (instance != null && trans != null)
            {
                var val = await this._unitOfWork.Save();

                builder.AppendFormat("Instance: {0} Quantity:{1} LinesUpdated: {2}", instance.Name, transaction.Quantity, val).AppendLine();
                return(new ReturnItemOutput(trans, true, builder.ToString()));
            }
            else
            {
                await this._unitOfWork.Undo();

                builder.AppendFormat("Instance: {0}", partInstance.Name).AppendLine();
                return(new ReturnItemOutput(transaction, false, builder.ToString()));
            }
        }