public InventoryOperation ManageOrderItemBalance(OrderItemBalance orderItemBalance, int userId)
        {
            using (var dbContext = new InventoryDbContext())
            {
                using (var transaction = dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                {
                    //Finding relevant Receipt Item
                    var receiptReferenceNumber = orderItemBalance.FuelReportDetailId.ToString();

                    var receiptReference = findInventoryOperationReference(
                        dbContext, InventoryOperationType.Receipt,
                        FUEL_REPORT_DETAIL_RECEIVE, receiptReferenceNumber);

                    var receiptReferenceTransactionItem = dbContext.TransactionItems.Single(
                        tip =>
                            tip.TransactionId == (int)receiptReference.OperationId &&
                            tip.GoodId == (int)orderItemBalance.FuelReportDetail.Good.SharedGoodId);

                    var receiptPriceReferenceNumber = generateOrderItemBalancePricingReferenceNumber(orderItemBalance);

                    var receiptRegisteredPriceReference = findInventoryOperationReference(dbContext, InventoryOperationType.Pricing, FUEL_REPORT_DETAIL_RECEIPT_INVOICE, receiptPriceReferenceNumber);
                    //There is no mechanism for editing the price of priced receipts portions.

                    var transactionItemPrice = new TransactionItemPrice()
                    {
                        TransactionItemId = receiptReferenceTransactionItem.Id,
                        QuantityUnitId = getMeasurementUnitId(dbContext, orderItemBalance.UnitCode),
                        QuantityAmount = orderItemBalance.QuantityAmountInMainUnit,
                        PriceUnitId = getCurrencyId(dbContext, orderItemBalance.InvoiceItem.Invoice.Currency.Abbreviation),
                        Fee = orderItemBalance.InvoiceItem.Fee,
                        RegistrationDate = DateTime.Now,
                        Description = "Received Good Pricing > " + orderItemBalance.FuelReportDetail.Good.Code,
                        UserCreatorId = userId
                    };

                    string pricingMessage;

                    priceTransactionItemManually(dbContext, transactionItemPrice, userId, out pricingMessage, FUEL_REPORT_DETAIL_RECEIPT_INVOICE, receiptPriceReferenceNumber);

                    var pricingOperationReference = findInventoryOperationReference(dbContext, InventoryOperationType.Pricing, FUEL_REPORT_DETAIL_RECEIPT_INVOICE, receiptPriceReferenceNumber);

                    var result = new InventoryOperation(
                        pricingOperationReference.OperationId,
                           actionNumber: string.Format("{0}/{1}", (InventoryOperationType)pricingOperationReference.OperationType, pricingOperationReference.OperationId),
                           actionDate: DateTime.Now,
                           actionType: InventoryActionType.Pricing,
                           fuelReportDetailId: null,
                           charterId: null);

                    transaction.Commit();

                    return result;
                }
            }
        }
        public List<InventoryOperation> ManageCharterInStart(CharterIn charterInStart, int userId)
        {
            if (charterInStart.CharterType != CharterType.Start)
                throw new InvalidArgument("The given entity is not Charter In Start", "charterInStart");

            using (var dbContext = new InventoryDbContext())
            {
                using (var transaction = dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                {
                    var result = new List<Domain.Model.DomainObjects.InventoryOperation>();

                    var reference = findInventoryOperationReference(dbContext, InventoryOperationType.Receipt, CHARTER_IN_START_RECEIPT, charterInStart.Id.ToString());

                    //if (reference.OperationId == INVALID_ID)
                    if (reference == null)
                    {
                        activateWarehouse(dbContext, (int)charterInStart.VesselInCompany.VesselInInventory.Id, userId);

                        string transactionCode, transactionMessage;

                        var operationReference = receipt(
                                  dbContext,
                                  (int)charterInStart.VesselInCompany.CompanyId,
                                  (int)charterInStart.VesselInCompany.VesselInInventory.Id,
                                  1,
                                  convertCharterInTypeToStoreType(charterInStart),
                                    null,
                                  CHARTER_IN_START_RECEIPT,
                                  charterInStart.Id.ToString(),
                                  userId,
                                  out transactionCode,
                                  out transactionMessage);

                        //TODO: Items
                        string transactionItemMessage;

                        var transactionItems = new List<TransactionItem>();

                        foreach (var charterItem in charterInStart.CharterItems)
                        {

                            transactionItems.Add(new TransactionItem()
                                                 {
                                                     GoodId = (int)charterItem.Good.SharedGoodId,
                                                     CreateDate = DateTime.Now,
                                                     Description = "Charter In Start > " + charterItem.Good.Code,
                                                     QuantityAmount = charterItem.Rob,
                                                     QuantityUnitId = getMeasurementUnitId(dbContext, charterItem.GoodUnit.Abbreviation),
                                                     TransactionId = (int)operationReference.OperationId,
                                                     UserCreatorId = userId
                                                 });
                        }

                        addTransactionItems(dbContext, (int)operationReference.OperationId, transactionItems, userId, out transactionItemMessage);

                        //Manual Items Pricing
                        var registeredTransaction = dbContext.Transactions.Single(t => t.Id == (int)operationReference.OperationId);

                        var transactionItemPrices = new List<TransactionItemPrice>();

                        foreach (var charterItem in charterInStart.CharterItems)
                        {
                            var registeredTransactionItem = registeredTransaction.TransactionItems.Single(ti => ti.GoodId == charterItem.Good.SharedGoodId);

                            var transactionItemPrice = new TransactionItemPrice()
                                {
                                    TransactionItemId = registeredTransactionItem.Id,
                                    QuantityUnitId = getMeasurementUnitId(dbContext, charterItem.GoodUnit.Abbreviation),
                                    QuantityAmount = charterItem.Rob,
                                    PriceUnitId = getCurrencyId(dbContext, charterInStart.Currency.Abbreviation),
                                    Fee = charterItem.Fee,
                                    RegistrationDate = DateTime.Now,
                                    Description = "Charter In Start Pricing > " + charterItem.Good.Code,
                                    UserCreatorId = userId
                                };

                            transactionItemPrices.Add(transactionItemPrice);
                            //priceTransactionItemsManually(dbContext, transactionItemPrice, userId, out pricingMessage, CHARTER_IN_START_RECEIPT_PRICING, charterItem.Id.ToString());
                        }

                        string pricingMessage;
                        priceTransactionItemsManually(dbContext, transactionItemPrices, userId, out pricingMessage, CHARTER_IN_START_RECEIPT_PRICING, charterInStart.Id.ToString());

                        result.Add(new InventoryOperation(
                            inventoryOperationId: operationReference.OperationId,
                            actionNumber: string.Format("{0}/{1}", (InventoryOperationType)operationReference.OperationType, transactionCode),
                            actionDate: DateTime.Now,
                            actionType: InventoryActionType.Receipt,
                            fuelReportDetailId: null,
                            charterId: charterInStart.Id));
                    }
                    else
                    {
                        throw new InvalidOperation("CharterInStart disapprovement", "CharterInStart disapprovement is invalid.");

                        var transactionItems = dbContext.TransactionItems.Where(ti => ti.TransactionId == reference.OperationId);

                    }

                    transaction.Commit();

                    return result;
                }
            }
        }
        //================================================================================
        public List<InventoryOperation> ManageFuelReportDetailIncrementalCorrectionDirectPricing(FuelReportDetail fuelReportDetail, int userId)
        {
            using (var dbContext = new InventoryDbContext())
            {
                using (var transaction = dbContext.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted))
                {
                    var result = new List<Domain.Model.DomainObjects.InventoryOperation>();

                    #region Incremental Correction

                    var transactionReferenceNumber = fuelReportDetail.Id.ToString();

                    var reference = findInventoryOperationReference(dbContext, InventoryOperationType.Receipt, FUEL_REPORT_DETAIL_INCREMENTAL_CORRECTION, transactionReferenceNumber);

                    //if (reference.OperationId == INVALID_ID)
                    if (reference == null)
                    {
                        string transactionCode, transactionMessage;

                        var operationReference =
                            receipt(
                                  dbContext,
                                  (int)fuelReportDetail.FuelReport.VesselInCompany.CompanyId,
                                  (int)fuelReportDetail.FuelReport.VesselInCompany.VesselInInventory.Id,
                                  1,
                                  convertFuelReportCorrectionTypeToStoreType(fuelReportDetail),
                                    null,
                                  FUEL_REPORT_DETAIL_INCREMENTAL_CORRECTION,
                                  transactionReferenceNumber,
                                  userId,
                                  out transactionCode,
                                  out transactionMessage);

                        string transactionItemMessage;

                        var transactionItems = new List<TransactionItem>();
                        transactionItems.Add(new TransactionItem()
                                             {
                                                 GoodId = (int)fuelReportDetail.Good.SharedGoodId,
                                                 CreateDate = DateTime.Now,
                                                 Description = fuelReportDetail.FuelReport.FuelReportType.ToString(),
                                                 QuantityAmount = (decimal?)fuelReportDetail.Correction,
                                                 QuantityUnitId = getMeasurementUnitId(dbContext, fuelReportDetail.MeasuringUnit.Abbreviation),
                                                 TransactionId = (int)operationReference.OperationId,
                                                 UserCreatorId = userId
                                             });

                        var transactionItemIds = addTransactionItems(dbContext, (int)operationReference.OperationId, transactionItems, userId, out transactionItemMessage);

                        //TODO: Items Pricing.

                        var transactionItemPrice = new TransactionItemPrice()
                        {
                            TransactionItemId = transactionItemIds[0],
                            QuantityUnitId = getMeasurementUnitId(dbContext, fuelReportDetail.MeasuringUnit.Abbreviation),
                            QuantityAmount = (decimal?)fuelReportDetail.Correction,
                            PriceUnitId = getCurrencyId(dbContext, fuelReportDetail.CorrectionPriceCurrency.Abbreviation),
                            Fee = fuelReportDetail.CorrectionPrice,
                            RegistrationDate = DateTime.Now,
                            Description = "Incremental Correction Direct Pricing > " + fuelReportDetail.Good.Code,
                            UserCreatorId = userId
                        };

                        string pricingMessage;

                        priceTransactionItemManually(dbContext, transactionItemPrice, userId, out pricingMessage, FUEL_REPORT_DETAIL_INCREMENTAL_CORRECTION_PRICING, transactionReferenceNumber);

                        result.Add(new InventoryOperation(
                                       inventoryOperationId: operationReference.OperationId,
                                       actionNumber: string.Format("{0}/{1}", (InventoryOperationType)operationReference.OperationType, transactionCode),
                                       actionDate: DateTime.Now,
                                       actionType: InventoryActionType.Receipt,
                                       fuelReportDetailId: fuelReportDetail.Id,
                                       charterId: null));
                    }
                    else
                    {
                        throw new InvalidOperation("FR Incremental Correction Edit", "FueReport  Incremental Correction edit is invalid");
                        var transactionItems = dbContext.TransactionItems.Where(ti => ti.TransactionId == reference.OperationId);
                    }

                    #endregion

                    transaction.Commit();

                    return result;
                }
            }
        }
        //================================================================================
        private void priceTransactionItemManually(InventoryDbContext dbContext, TransactionItemPrice transactionItemPrice,
            int userId, out string message, string pricingReferenceType, string pricingReferenceNumber)
        {
            var transactionItemsPrices = new List<TransactionItemPrice>
                                         {
                                             transactionItemPrice
                                         };

            priceTransactionItemsManually(dbContext, transactionItemsPrices, userId, out message, pricingReferenceType, pricingReferenceNumber);
        }