private void payMonetaryOfferTax(MonetaryOffer offer, ref MonetaryTransaction transaction)
        {
            var tax = CalculateTax((double)offer.ValueOfSold, getTaxRate(offer));

            addTaxToTransaction(offer, transaction, tax);
            var currency = GetUsedCurrency(offer);

            var countryEntity = Persistent.Countries.FirstOrDefault(c => c.CurrencyID == currency.ID)?.Entity;
            var payingEntity  = offer.Entity;
            var money         = new Money()
            {
                Amount   = (decimal)tax,
                Currency = currency
            };

            var sociatisTransaction = new structs.Transaction()
            {
                Arg1  = "Monetary tax",
                Arg2  = string.Format("{0}({1}) paid monetary tax in {2}({3})", payingEntity.Name, payingEntity.EntityID, countryEntity?.Name, countryEntity?.EntityID),
                Money = money,
                DestinationEntityID = countryEntity?.EntityID,
                TransactionType     = TransactionTypeEnum.MonetaryTax
            };

            offer.TaxReservedMoney -= (decimal)tax;
            transactionService.MakeTransaction(sociatisTransaction, useSqlTransaction: false);
        }
 private static void addTaxToTransaction(MonetaryOffer offer, MonetaryTransaction transaction, double tax)
 {
     if (offer.OfferTypeID == (int)MonetaryOfferTypeEnum.Buy)
     {
         transaction.BuyerTax += (decimal)tax;
     }
     else
     {
         transaction.SellerTax += (decimal)tax;
     }
 }
 public void RemoveOffer(MonetaryOffer offer, ref MonetaryTransaction monetaryTransaction)
 {
     using (var transaction = transactionScopeProvider.CreateTransactionScope())
     {
         payMonetaryOfferTax(offer, ref monetaryTransaction);
         payBackReservedMoney(offer);
         offer.LastTransaction = null;
         monetaryOfferRepository.Remove(offer);
         monetaryOfferRepository.SaveChanges();
         transaction?.Complete();
     }
 }
        private void tryToBuySell(MonetaryOffer offer)
        {
            var entity = entityRepository.GetById(offer.SellerID);

            MonetaryTransaction lastTransaction = null;

            if (offer.OfferTypeID == (int)MonetaryOfferTypeEnum.Buy)
            {
                var suitableSellOffers = monetaryOfferRepository.
                                         Where(o => o.Rate <= offer.Rate &&
                                               o.OfferTypeID == (int)MonetaryOfferTypeEnum.Sell &&
                                               o.SellCurrencyID == offer.SellCurrencyID &&
                                               o.BuyCurrencyID == offer.BuyCurrencyID
                                               );

                if (suitableSellOffers.Any())
                {
                    var offers = suitableSellOffers.OrderBy(o => o.Rate).ToList();
                    foreach (var sellOffer in offers)
                    {
                        int boughtAmount = makeTransaction(offer, entity, sellOffer);

                        MonetaryTransaction transaction = new MonetaryTransaction()
                        {
                            Date             = DateTime.Now,
                            Day              = GameHelper.CurrentDay,
                            Amount           = boughtAmount,
                            Rate             = sellOffer.Rate,
                            BuyerCurrencyID  = offer.BuyCurrencyID,
                            SellerCurrencyID = offer.SellCurrencyID,
                            SellerID         = sellOffer.SellerID,
                            BuyerID          = offer.SellerID,
                            SellerTax        = 0,
                            BuyerTax         = 0
                        };

                        lastTransaction = transaction;


                        if (sellOffer.Amount == 0)
                        {
                            RemoveOffer(sellOffer, ref transaction);
                        }
                        else
                        {
                            transaction.MonetaryOffers.Add(sellOffer);
                        }

                        if (offer.Amount == 0)
                        {
                            RemoveOffer(offer, ref transaction);
                            monetaryTransactionRepository.Add(transaction);
                            offer = null;
                            break;
                        }
                        monetaryTransactionRepository.Add(transaction);
                    }
                }
            }
            else
            {
                var suitableSellOffers = monetaryOfferRepository.
                                         Where(o => o.Rate >= offer.Rate &&
                                               o.OfferTypeID == (int)MonetaryOfferTypeEnum.Buy &&
                                               o.SellCurrencyID == offer.SellCurrencyID &&
                                               o.BuyCurrencyID == offer.BuyCurrencyID
                                               );

                if (suitableSellOffers.Any())
                {
                    var offers = suitableSellOffers.OrderBy(o => o.Rate).ToList();
                    foreach (var buyOffer in offers)
                    {
                        int boughtAmount = makeTransaction(buyOffer, buyOffer.Entity, offer);

                        MonetaryTransaction transaction = new MonetaryTransaction()
                        {
                            Date             = DateTime.Now,
                            Day              = GameHelper.CurrentDay,
                            Amount           = boughtAmount,
                            BuyerID          = buyOffer.SellerID,
                            Rate             = offer.Rate,
                            BuyerCurrencyID  = offer.BuyCurrencyID,
                            SellerCurrencyID = offer.SellCurrencyID,
                            SellerID         = offer.SellerID,
                            SellerTax        = 0,
                            BuyerTax         = 0
                        };

                        lastTransaction = transaction;

                        if (buyOffer.Amount == 0)
                        {
                            RemoveOffer(buyOffer, ref transaction);
                        }
                        else
                        {
                            transaction.MonetaryOffers.Add(buyOffer);
                        }

                        if (offer.Amount == 0)
                        {
                            RemoveOffer(offer, ref transaction);
                            monetaryTransactionRepository.Add(transaction);
                            offer = null;
                            break;
                        }

                        monetaryTransactionRepository.Add(transaction);
                    }
                }
            }

            if (offer != null)
            {
                offer.LastTransaction = lastTransaction;
            }
        }