public List <Bill> CalculateChange(Pdv pdv) { FillBillsData(pdv.Id); decimal price = pdv.Price; decimal amount = pdv.AmountPaid; decimal change = amount - price; List <Bill> lBills = (from b in this._DbContext.Bills select b) .OrderByDescending(x => x.Value) .ToList(); foreach (Bill bill in lBills) { if (change >= bill.Value) { bill.Quantity = (int)(change / bill.Value); change -= bill.Quantity * bill.Value; } if (change == 0) { break; } } return(lBills); }
public static void Initialize(AppDbContext context) { context.Database.EnsureCreated(); if (context.Pdvs.Any()) { return; //DB has been seeded } var rawObj = JObject.Parse(System.IO.File.ReadAllText(@"pdvs.json")); var pdvs = (JArray)rawObj["pdvs"]; foreach (var item in pdvs) { var pdv = new Pdv() { Id = 0, // Avoid explict id inserting in identity column TradingName = (string)item["tradingName"], OwnerName = (string)item["ownerName"], Document = (string)item["document"], CoverageArea = (string)item["coverageArea"], Address = (string)item["address"], }; context.Pdvs.Add(pdv); } context.SaveChanges(); }
public static Pdv SearchClosestPDV(Pdv result, double lat, double lon, Point testPoint, Pdv pdv) { var coverageAreaList = JsonConvert.DeserializeObject <CoverageArea>(pdv.CoverageArea); var pdvPoints = new List <PdvPoint>(); foreach (var ptsList in coverageAreaList.coordinates[0]) { foreach (var pts in ptsList) { pdvPoints.Add(new PdvPoint { X = pts[1], Y = pts[0] }); } } PolyGon myRoute = new PolyGon(pdvPoints); bool PdvIsInMultiPolygon = myRoute.FindPoint(lat, lon); //true if (PdvIsInMultiPolygon == true) { var address = JsonConvert.DeserializeObject <Point>(pdv.Address); pdv.Distance = GeoUtils.GetDistance(address, testPoint); result = pdv; } return(result); }
public Pdv Insert(Pdv pdv) { pdv.Id = _pdvs.Count() + 1; _pdvs.Add(pdv); return(Select(pdv.Id)); }
public Pdv Closest() { Pdv result = null; string latitude = Request.Query["lat"]; string longitude = Request.Query["lon"]; if (string.IsNullOrEmpty(latitude) || string.IsNullOrEmpty(longitude)) { return(result); } double lat, lon; lat = double.Parse(latitude); lon = double.Parse(longitude); var testPoint = new Point(new Position(lat, lon)); //Query to find the closest PDV var pdvs = CachePDVS(); foreach (var pdv in pdvs) { result = PdvRepository.SearchClosestPDV(result, lat, lon, testPoint, pdv); } return(result); }
public void Compute_With_Insufficient_Pdv_Balance() { Pdv _pdv = new Pdv(); _pdv.InitiatePdv(GenerateTestBankNotesInsufficient); Assert.Throws <InsufficientPdvBalanceError>(() => _pdv.Compute(80, 100)); }
public void Insert_InvalidPdvRequest_ThrowsDbUpdateException() { Pdv invalidPdv = NewInvalidPdv; TestDelegate testDelegate = new TestDelegate(() => _repository.Insert(invalidPdv)); Assert.Throws <DbUpdateException>(testDelegate); }
public void Insert_DuplicateDocument_ThrowsDbUpdateException() { Pdv duplicate = NewDuplicatePdv; TestDelegate testDelegate = new TestDelegate(() => _repository.Insert(duplicate)); Assert.Throws <DbUpdateException>(testDelegate); }
public void InitiatePdv_With_Positive_Balance_For_BankCoins() { Pdv _pdv = new Pdv(); _pdv.InitiatePdv(null, GenerateTestBankCoins); Assert.True(_pdv.IsValid); }
public void Compute_With_Suficient_Pdv_Balance() { Pdv _pdv = new Pdv(); _pdv.InitiatePdv(GenerateTestBankNotes); _pdv.Compute(80, 100); Assert.True(_pdv.IsClosed); }
public PdvResponse(Pdv pdv) { this.Id = pdv.Id.ToString(); this.TradingName = pdv.TradingName; this.OwnerName = pdv.OwnerName; this.Document = pdv.Document; this.CoverageArea = Serialize(pdv.CoverageArea); this.Address = Serialize(pdv.Address); }
public void CheckBalance_OK() { Pdv pdv = new Pdv(); pdv.InitiatePdv(); var check = pdv.CheckBalance(70); Assert.True(check); }
public void CheckBalance_Not_OK() { Pdv pdv = new Pdv(); pdv.InitiatePdv(); var check = pdv.CheckBalance(1500); Assert.False(check); }
public PdvResponse Create(PdvRequest request) { _validator.ValidateAndThrow(request); //TODO: Implementar handler validação Pdv pdv = _repository.Insert(new Pdv(request)); return(new PdvResponse(pdv)); }
public PdvResponse Get(int id) { Pdv pdv = _repository.Select(id); if (pdv == null) { return(null); } return(new PdvResponse(pdv)); }
public Pdv Insert(Pdv obj) { if (obj == null || !obj.CoverageArea.IsValid || !obj.Address.IsValid) { throw new DbUpdateException("Invalid Entity", new ArgumentException("Invalid Geometry")); } _context.Set <Pdv>().Add(obj); _context.SaveChanges(); return(this.Select(obj.Id)); }
public void InitiatePdv_With_Invalid_Balance_For_BankCoins() { List <int> coins = new List <int> { 0, -01, -05 }; Pdv _pdv = new Pdv(); Assert.Throws <PdvIniatializationError>(() => _pdv.InitiatePdv(coins, null)); }
public void InitiatePdv_With_Invalid_Balance_For_BankNotes() { List <int> notes = new List <int> { 0, -10, -50 }; Pdv _pdv = new Pdv(); Assert.Throws <PdvIniatializationError>(() => _pdv.InitiatePdv(notes, null)); }
public void Create_WhenGetPdvReturnAnyPdv_ShouldReturnBadRequest( Pdv pdvRequest, PdvDto pdvDto, PdvController sut) { sut.PdvQueryRepository.GetPdv(Arg.Any <Guid>()).Returns(pdvDto); var response = sut.Create(pdvRequest); sut.PdvCommandRepository.DidNotReceive().CreatePdv(Arg.Any <PdvDto>()); response.Should().BeOfType <BadRequestObjectResult>(); }
public void Get_ShouldReturnPdv( Guid pdvId, PdvDto pdvDto, Pdv pdv, PdvController sut) { sut.PdvQueryRepository.GetPdv(pdvId).Returns(pdvDto); sut.Mapper.Map <Pdv>(pdvDto).Returns(pdv); var response = sut.Get(pdvId); response.Should().BeOfType <OkObjectResult>(); ((OkObjectResult)response).Value.Should().Be(pdv); }
public void Create_ShouldReturnOkay( Pdv pdvRequest, PdvDto pdvDto, PdvController sut) { sut.PdvQueryRepository.GetPdv(Arg.Any <Guid>()).Returns((PdvDto)null); sut.Mapper.Map <PdvDto>(Arg.Any <Pdv>()).Returns(pdvDto); var response = sut.Create(pdvRequest); sut.PdvCommandRepository.Received().CreatePdv(pdvDto); response.Should().BeOfType <CreatedResult>(); }
public void Insert_ValidPdv_ReturnsPdv() { Pdv validPdv = NewValidPdv; Pdv response = _repository.Insert(validPdv); Assert.IsNotNull(response); Assert.AreSame(typeof(Pdv), response.GetType()); Assert.AreEqual(validPdv.TradingName, response.TradingName); Assert.AreEqual(validPdv.OwnerName, response.OwnerName); Assert.AreEqual(validPdv.Document, response.Document); Assert.AreEqual(validPdv.CoverageArea, response.CoverageArea); Assert.AreEqual(validPdv.Address, response.Address); }
public PdvResponse Search(double lng, double lat) { IPoint location = new Point(lng, lat); location.SRID = 4326; Pdv pdv = _repository.Search(location); if (pdv == null) { return(null); } return(new PdvResponse(pdv)); }
public IActionResult Create(Pdv pdvRequest) { var pdv = PdvQueryRepository.GetPdv(pdvRequest.Document); if (pdv != null) { return(BadRequest("This Pdv already exists")); } pdvRequest.Id = Guid.NewGuid(); var pdvDto = Mapper.Map <PdvDto>(pdvRequest); PdvCommandRepository.CreatePdv(pdvDto); Logger.Information("Creating a PDV {pdvRequest}", pdvRequest); return(Created("", pdvRequest)); }
public IActionResult DodajPV(PorezDodajVM input) { if (!ModelState.IsValid) { return(PartialView(input)); } Pdv v = new Pdv() { Drzava = input.Drzava, IznosPdv = Convert.ToDecimal(input.IznosPoreza) / 100 }; _db.Add(v); _db.SaveChanges(); _db.Dispose(); return(RedirectToAction(nameof(Index))); }
public Pdv PayBill(double payment, double total) { try { var _pdv = new Pdv(); _pdv.InitiatePdv(); _pdv.Compute(total, payment); // A gravação e tratativas do Histórico também poderia ser controlada // por um evento e um controlador deste evento // permitindo desacoplar mais a lógica deste código if (_pdv.IsClosed.HasValue && _pdv.IsClosed.Value) { var historyIn = new PdvHistory { Amount = payment, CreatedAt = DateTime.Now, OperationType = OperationType.In }; _unitOfWork.PdvHistoryRepository.Insert(historyIn); if (_pdv.BankCoinsToReturn.Count > 0 || _pdv.BankNotesToReturn.Count > 0) { var historyOut = new PdvHistory { Amount = _pdv.BankNotesToReturn.Sum(b => b) + _pdv.BankCoinsToReturn.Sum(b => b), CreatedAt = DateTime.Now, OperationType = OperationType.Out }; _unitOfWork.PdvHistoryRepository.Insert(historyOut); } } return(_pdv); } catch (Exception) { throw; } }
public Pdv Closest() { Pdv result = null; //Read data from querystring string latitude = Request.Query["lat"]; string longitude = Request.Query["lon"]; if (string.IsNullOrEmpty(latitude) || string.IsNullOrEmpty(longitude)) { return(result); //TODO: better error handling } double lat, lon; lat = double.Parse(latitude); lon = double.Parse(longitude); var testPoint = new Point(new Position(lat, lon)); //Query PDV's to find the closest var pdvs = CachedGetPdvs(); foreach (var pdv in pdvs) { var address = JsonConvert.DeserializeObject <Point>(pdv.Address); var distance = GeoUtils.GetDistance(address, testPoint); if (result == null || result.Distance > distance) { var coverageArea = JsonConvert.DeserializeObject <MultiPolygon>(pdv.CoverageArea); if (GeoUtils.IsPointInMultiPolygon(testPoint, coverageArea)) { pdv.Distance = distance; result = pdv; } } } return(result); }
public async Task <ActionResult <List <Bill> > > Post( [FromServices] DataContext context, [FromBody] Pdv model) { BillRepository billRepository = new BillRepository(context); decimal change = model.AmountPaid - model.Price; //Change limit if (change >= 10000) { return(null); } context.Pdvs.Add(model); List <Bill> lBills = billRepository.CalculateChange(model); //context.Bills.UpdateRange(lBills); context.SaveChanges(); return(lBills); }
private bool HasValidPdv() { return(Pdv.Equals(0) == false); }
public void Compute_With_Insufficient_Amout_Payed() { Pdv _pdv = new Pdv(); Assert.Throws <PaymentInsufficientError>(() => _pdv.Compute(100, 90)); }