public virtual bool IsEnoughFuelForTrade(int?fuelInInventory, int neededFuel, MarketOffer offer) { fuelInInventory = fuelInInventory ?? 0; if (neededFuel > 0 == false) { return(true); } if (offer.GetProductType() == ProductTypeEnum.Fuel) { neededFuel -= offer.Amount; } return(fuelInInventory >= neededFuel); }
public MethodResult CanBuyOffer(MarketOffer offer, int amount, Entity entity, int walletID) { if (offer == null) { return(new MethodResult(MarketOfferErrors.OfferNotExist)); } var productType = ((ProductTypeEnum)offer.ProductID); if (offer.Amount < amount) { return(new MethodResult(MarketOfferErrors.NotEnoughProducts)); } if (amount <= 0) { return(new MethodResult("Are you crazy?")); } if (amount > 1 && offer.GetProductType() == ProductTypeEnum.House) { return(new MethodResult("You cannot buy more than 1 house at same moment!")); } if (equipmentService.IsAllowedItemFor(entity, productType) == false) { return(new MethodResult(MarketOfferErrors.NotAllowedProduct)); } var result = CanBuy(offer, entity, offer.Company.Entity); if (result.IsError) { return(result); } var cost = GetOfferCost(offer, entity, amount); if (cost == null) { return(new MethodResult("You cannot buy that offer!")); } if (walletService.HaveMoney(walletID, cost.ClientPriceMoney) == false) { return(new MethodResult(MarketOfferErrors.NotEnoughMoney)); } if (offer.Company.CompanyTypeID == (int)CompanyTypeEnum.Shop && (equipmentRepository.GetEquipmentItem(offer.Company.Entity.EquipmentID.Value, (int)ProductTypeEnum.SellingPower, 1)?.Amount ?? 0) < amount) { return(new MethodResult("Shop does not have enough selling power!")); } if (entity.EntityTypeID != (int)EntityTypeEnum.Citizen && cost.FuelCost > 0) { var fuel = equipmentRepository.GetEquipmentItem(entity.EquipmentID.Value, (int)ProductTypeEnum.Fuel, 1); if (IsEnoughFuelForTrade(fuel?.Amount, cost.IntegerRealCost, offer) == false) { return(new MethodResult(MarketOfferErrors.NotEngouhFuel)); } } var equipment = entity.Equipment; if (equipment.CanAddNewItem(amount) == false) { return(new MethodResult("You do not have enough space in inventory")); } if (offer.GetProductType() == ProductTypeEnum.House) { var houseService = DependencyResolver.Current.GetService <IHouseService>(); if (houseService.CanCreateHouseForCitizen(entity.Citizen).IsError) { return(new MethodResult("You already have house in this region!")); } } return(MethodResult.Success); }
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); } }