public virtual void FinishTrade(Trade trade) { using (var trs = transactionScopeProvider.CreateTransactionScope()) { trade.Set(TradeStatusEnum.Success); Entity destination = trade.Destination; Entity source = trade.Source; using (NoSaveChanges) { foreach (var money in trade.TradeMoneys.ToList()) { var secondSide = GetSecondSide(trade, money.Entity); walletService.AddMoney(secondSide.WalletID, new Money(money.CurrencyID, money.Amount)); if (secondSide.Is(EntityTypeEnum.Company)) { var company = secondSide.Company; companyFinanceSummaryService.AddFinances(company, new TradeBalanceFinance(money.Amount, money.CurrencyID)); } if (money.Entity.Is(EntityTypeEnum.Company)) { var company = money.Entity.Company; companyFinanceSummaryService.AddFinances(company, new TradeBalanceFinance(-money.Amount, money.CurrencyID)); } } foreach (var product in trade.TradeProducts.ToList()) { var secondSide = GetSecondSide(trade, product.Entity); equipmentService.GiveItem((ProductTypeEnum)product.ProductID, product.Amount, product.Quality, secondSide.Equipment); } var sourceFuelCost = GetNeededFuel(trade.Source, trade); var destinationFuelCost = GetNeededFuel(trade.Destination, trade); trade.DestinationUsedFuelAmount = destinationFuelCost; trade.SourceUsedFuelAmount = sourceFuelCost; if (sourceFuelCost > 0) { equipmentService.RemoveProductsFromEquipment(ProductTypeEnum.Fuel, sourceFuelCost.Value, 1, trade.Source.Equipment); } if (destinationFuelCost > 0) { equipmentService.RemoveProductsFromEquipment(ProductTypeEnum.Fuel, destinationFuelCost.Value, 1, trade.Destination.Equipment); } var tradeLink = TradeLinkCreator.Create(trade); string msg = $"{tradeLink} has been successfully finished"; warningService.AddWarning(destination.EntityID, msg); warningService.AddWarning(source.EntityID, msg); } ConditionalSaveChanges(tradeRepository); trs.Complete(); } }
public void SendMoneyGift(Entity source, Entity destination, Currency currency, decimal amount) { var money = new Money(currency, amount); using (var trs = transactionScopeProvider.CreateTransactionScope()) { transactionsService.MakeGift(source, destination, money); var sourceLink = EntityLinkCreator.Create(source); string msg = $"You received {money.Amount} {currency.Symbol} from {sourceLink}."; warningService.AddWarning(destination.EntityID, msg); if (destination.Is(EntityTypeEnum.Company)) { var company = destination.Company; companyFinanceSummaryService.AddFinances(company, new GiftBalanceFinance(amount, currency.ID)); } if (source.Is(EntityTypeEnum.Company)) { var company = source.Company; companyFinanceSummaryService.AddFinances(company, new GiftBalanceFinance(-amount, currency.ID)); } trs.Complete(); } }
private MethodResult payCashForWork(Company company, Citizen citizen, CompanyEmployee employee) { var currency = company.Region.Country.Currency; var transaction = new structs.Transaction() { Arg1 = "Job Salary", Arg2 = string.Format("{0} Paid {1}", company.Entity.Name, citizen.Entity.Name), DestinationEntityID = citizen.ID, SourceEntityID = company.ID, Money = new Money() { Amount = employee.TodaySalary.Value, Currency = currency }, TransactionType = TransactionTypeEnum.Salary }; var result = transactionService.MakeTransaction(transaction); if (result == enums.TransactionResult.NotEnoughMoney) { return(new MethodResult("Company does not have enough money")); } companyFinanceSummaryService.AddFinances(company, new SalaryCostFinance(employee.TodaySalary.Value, currency.ID)); return(MethodResult.Success); }
private void PayJobOfferFee(Company company, Country country, JobOfferTypeEnum offerType, double offerAmount, int currencyID, decimal price) { transactionService.PayForJobOffer(company, country, currencyID, price); companyFinanceSummaryService.AddFinances(company, new JobOfferCostFinance(price, currencyID)); }
public MethodResult Buy(MarketOffer offer, Entity buyer, int amount, int walletID) { using (var scope = transactionScopeProvider.CreateTransactionScope()) { var eqID = buyer.EquipmentID.Value; var sellerEntity = offer.Company.Entity; var localCountry = sellerEntity.GetCurrentCountry(); if (offer.GetProductType() == ProductTypeEnum.House) { var houseService = DependencyResolver.Current.GetService <IHouseService>(); houseService.CreateHouseForCitizen(buyer.Citizen); } else { equipmentRepository.AddEquipmentItem(eqID, offer.ProductID, offer.Quality, amount); } var cost = GetOfferCost(offer, buyer, amount); if (sellerEntity.Company.CompanyTypeID == (int)CompanyTypeEnum.Shop) { equipmentRepository.RemoveEquipmentItem(sellerEntity.EquipmentID.Value, (int)ProductTypeEnum.SellingPower, 1, amount); } if (buyer.EntityTypeID == (int)EntityTypeEnum.Company) { UseFuelInBuyProcess(eqID, cost); } var currency = Persistent.Currencies.First(c => c.ID == offer.CurrencyID); if (makeCompanyTransactionForBuy(offer, buyer, sellerEntity, cost, currency, amount, walletID) != TransactionResult.Success) { return(MethodResult.Failure); } if (makeLocalCountryTransactionForBuy(offer, buyer, sellerEntity, localCountry, cost, currency, amount, walletID) != TransactionResult.Success) { return(MethodResult.Failure); } //there is no vat because company is not paying vat (directly). Citizen is paying the vat. ICompanyFinance[] finances = new ICompanyFinance[] { new SellRevenueFinance(cost.BasePrice, currency.ID), }; companyFinanceSummaryService.AddFinances(offer.Company, finances); /*if (cost.ExportTax > 0) * { * makeExportTransactionForBuy(offer, buyer, sellerEntity, cost, amount); * }*/ offer.Amount -= amount; if (offer.Amount <= 0) { marketOfferRepository.Remove(offer.ID); } equipmentRepository.SaveChanges(); scope.Complete(); return(MethodResult.Success); } }