public void SaveItem(Pricelist pricelist, Service service, double?price) { if (price == null) { _db.PricelistItems .Where(p => p.PricelistID == pricelist.ID) .Where(p => p.ServiceID == service.ID) .Delete(); } else { var pricelistItem = _db.PricelistItems .Where(p => p.PricelistID == pricelist.ID) .FirstOrDefault(p => p.ServiceID == service.ID); if (pricelistItem == null) { pricelistItem = new PricelistItem() { PricelistID = pricelist.ID, ServiceID = service.ID, Price = price } } ; else { pricelistItem.Price = price; } InsertOrUpdateToDb(pricelistItem); } }
public IHttpActionResult GetPricelistItem(int id) { PricelistItem pricelistItem = db.PricelistItems.Get(id); if (pricelistItem == null) { return(NotFound()); } return(Ok(pricelistItem)); }
public IHttpActionResult PostPricelistItem(PricelistItem pricelistItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.PricelistItems.Add(pricelistItem); db.Complete(); return(CreatedAtRoute("DefaultApi", new { id = pricelistItem.Id }, pricelistItem)); }
public async Task <bool> BuyTicketAsync(string ticketType, int userId = -1, string email = null) { if (userId == -1 && email != null) { PricelistItem prInfo = await _pricelistItemRepository.GetPriceListItemForTicketType(ticketType); Ticket newTicket = new Ticket() { IsValid = true, TicketType = ticketType, PriceInfo = prInfo, DateOfIssue = DateTime.Now }; Add(newTicket); var result = await SaveAll(); if (result) { EmailService.SendEmail("You have successfuly bought hourly ticket.", email); return(true); } return(false); } if (userId != -1) { var userFromDatabase = await _userManager.GetUserById(userId); if (userFromDatabase.Verified == true) { PricelistItem prInfo = await _pricelistItemRepository.GetPriceListItemForTicketType(ticketType); Ticket newTicket = new Ticket() { User = userFromDatabase, IsValid = true, TicketType = ticketType, PriceInfo = prInfo, DateOfIssue = DateTime.Now }; Add(newTicket); return(await SaveAll()); } } return(false); }
public IHttpActionResult DeletePricelistItem(int id) { PricelistItem pricelistItem = db.PricelistItems.Get(id); if (pricelistItem == null) { return(NotFound()); } db.PricelistItems.Remove(pricelistItem); db.Complete(); return(Ok(pricelistItem)); }
public async Task <ServiceResponse <int> > GetPrice(string ticketType) { ServiceResponse <int> response = new ServiceResponse <int>(); ServiceResponse <int> rpcResponse = new ServiceResponse <int>(); int userId = GetUserId(); Enums.UserType userType = Enums.UserType.RegulerUser; try { if (userId != -1) { // RPC call to account service to get user type var res = await _rpcClient.CallAsync(userId.ToString()); Console.WriteLine("test1"); rpcResponse = System.Text.Json.JsonSerializer.Deserialize <ServiceResponse <int> >(res); Console.WriteLine("test2"); if (rpcResponse.Success) { Console.WriteLine("test3"); userType = (Enums.UserType)rpcResponse.Data; } Console.WriteLine("test4"); } } catch (Exception) { response.Message = "Possible discount couldn't be processed. Price shown is without discount.\n"; } try { Enums.TicketType type = (Enums.TicketType)Enum.Parse(typeof(Enums.TicketType), ticketType); PricelistItem pricelistItem = await _context.PricelistItems.FirstOrDefaultAsync(pi => pi.Pricelist.Active == true && pi.Item.TicketType == type); Coefficient coef = await _context.Coefficients.FirstOrDefaultAsync(c => c.UserType == userType); response.Data = (int)Math.Round(pricelistItem.Price * coef.Value); } catch (Exception e) { response.Success = false; response.Message = e.Message; } return(response); }
public IHttpActionResult PutPricelistItem(int id, PricelistItem pricelistItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != pricelistItem.Id) { return(BadRequest()); } db.PricelistItems.Update(pricelistItem); db.Complete(); return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <ServiceResponse <AddedTicketDTO> > CreateTicket(string ticketType, int ticketPrice, string email) { ServiceResponse <AddedTicketDTO> response = new ServiceResponse <AddedTicketDTO>(); Enums.TicketType type = (Enums.TicketType)Enum.Parse(typeof(Enums.TicketType), ticketType); PricelistItem pricelistItem = await _context.PricelistItems.FirstOrDefaultAsync(pi => pi.Pricelist.Active == true && pi.Item.TicketType == type); Ticket ticket = new Ticket() { IssueTime = DateTime.Now, PricelistItemId = pricelistItem.Id, Valid = true, Price = (double)ticketPrice }; int userId = GetUserId(); if (userId != -1) { ticket.UserId = userId; } try { await _context.Tickets.AddAsync(ticket); await _context.SaveChangesAsync(); response.Data = new AddedTicketDTO() { Id = ticket.Id, Price = ticket.Price }; Utility.SendEmail(email, "Ticket purchase confirmation", "You have successfully purchased a ticket. Ticket id: " + ticket.Id); } catch (Exception e) { response.Success = false; response.Message = e.Message; } return(response); }
public void UpdateCompatibleItems(PricelistItem item) { string[] rawReferences = item.CompatibleModels.Split('|'); //add new references to the CompatibleItems list foreach (string reference in rawReferences) { var isCreated = FindByReference(reference); if (isCreated != null && !item.CompatibleItems.Contains(isCreated.PricelistItemID)) { item.AddCompatibleItem(isCreated); } } var linkedReferences = item.CompatibleItems; //remove old references from the CompatibleItems list foreach (var linkedReference in linkedReferences) { if (!rawReferences.Contains(_db.PricelistItems.Find(linkedReference).Reference)) { item.RemoveCompatibleItem(_db.PricelistItems.Find(linkedReference)); } } }
private void InitialDBAdding(WebApp.Persistence.ApplicationDbContext context) { if (!context.Coordinates.Any(c => c.CoordinatesId == 1)) // kada budemo napravili server ovo ce biti nepotrebno { Coordinates c = new Coordinates() { CoordX = 1, CoordY = 1 }; context.Coordinates.Add(c); context.SaveChanges(); } if (!context.Coordinates.Any(c => c.CoordinatesId == 2)) // kada budemo napravili server ovo ce biti nepotrebno { Coordinates c = new Coordinates() { CoordX = 10, CoordY = 10 }; context.Coordinates.Add(c); context.SaveChanges(); } if (!context.DriveLines.Any(d => d.Number == 4)) { var drLine = new Driveline() { Number = 4 }; context.DriveLines.Add(drLine); context.SaveChanges(); } if (!context.DriveLines.Any(d => d.Number == 7)) { var drLine = new Driveline() { Number = 7 }; context.DriveLines.Add(drLine); context.SaveChanges(); } if (!context.DrivingPlans.Any(p => p.Departures.Equals("4: 50 ; 10:30"))) { DrivingPlan drPlan = new DrivingPlan() { Day = Models.Enums.WeekDays.Monday, Type = Models.Enums.DriveType.City, Departures = "4: 50 ; 10:30", DrivelineId = context.DriveLines.Where(l => l.Number == 4).FirstOrDefault().Id }; context.DrivingPlans.Add(drPlan); context.SaveChanges(); } if (!context.DrivingPlans.Any(p => p.Departures.Equals("10:00 ; 11:00 ; 12:00"))) { DrivingPlan drPlan = new DrivingPlan() { Day = Models.Enums.WeekDays.Monday, Type = Models.Enums.DriveType.City, Departures = "10:00 ; 11:00 ; 12:00", DrivelineId = context.DriveLines.Where(l => l.Number == 7).FirstOrDefault().Id }; context.DrivingPlans.Add(drPlan); context.SaveChanges(); } if (!context.Stations.Any(s => s.Name == "FirstStation")) { Station s = new Station() { Name = "FirstStation", Address = "Bulevar Oslobodjenja 1" }; s.CoordinatesId = context.Coordinates.Where(c => c.CoordinatesId == 1).FirstOrDefault().CoordinatesId; context.Stations.Add(s); context.SaveChanges(); } if (!context.Stations.Any(s => s.Name == "SecondStation")) { Station s = new Station() { Name = "SecondStation", Address = "Bulevar Oslobodjenja 10" }; s.CoordinatesId = context.Coordinates.Where(c => c.CoordinatesId == 2).FirstOrDefault().CoordinatesId; context.Stations.Add(s); context.SaveChanges(); } if (!context.Pricelists.Any(p => p.PricelistId == 1)) //necemo po ID-u , ali posto je samo test onda je ok { Pricelist pr = new Pricelist() { ValidFrom = DateTime.Now, ValidUntil = DateTime.Now.AddDays(2) }; context.Pricelists.Add(pr); context.SaveChanges(); } if (!context.PricelistItems.Any(p => p.TicketType == Models.Enums.TicketType.Daily && p.PassengerType == Models.Enums.PassengerType.Regular)) { PricelistItem prI = new PricelistItem() { TicketType = Models.Enums.TicketType.Daily, Price = 200, PassengerType = Models.Enums.PassengerType.Regular }; Pricelist pr = context.Pricelists.Where(p => p.PricelistId == 1).FirstOrDefault(); prI.PricelistId = pr.PricelistId; context.PricelistItems.Add(prI); context.SaveChanges(); pr.PricelistItems.Add(prI); context.PricelistItems.AddOrUpdate(prI); context.SaveChanges(); } if (!context.PassengerTypeCoefficients.Any(p => p.PassengerType == Models.Enums.PassengerType.Regular)) { PassengerTypeCoefficient pas = new PassengerTypeCoefficient() { Coefficient = 1F, PassengerType = Models.Enums.PassengerType.Regular }; context.PassengerTypeCoefficients.Add(pas); context.SaveChanges(); } if (!context.PassengerTypeCoefficients.Any(p => p.PassengerType == Models.Enums.PassengerType.Pensioner)) { PassengerTypeCoefficient pas = new PassengerTypeCoefficient() { Coefficient = 0.9F, PassengerType = Models.Enums.PassengerType.Pensioner }; context.PassengerTypeCoefficients.Add(pas); context.SaveChanges(); } if (!context.PassengerTypeCoefficients.Any(p => p.PassengerType == Models.Enums.PassengerType.Student)) { PassengerTypeCoefficient pas = new PassengerTypeCoefficient() { Coefficient = 0.8F, PassengerType = Models.Enums.PassengerType.Student }; context.PassengerTypeCoefficients.Add(pas); context.SaveChanges(); } }
public void SeedStations() { List <Station> stations8 = new List <Station>(); List <Station> stations4 = new List <Station>(); List <StationLine> stlines8 = new List <StationLine>(); List <StationLine> stlines4 = new List <StationLine>(); Line lines8 = new Line(); Line lines4 = new Line(); if (!_context.Stations.Any()) { Station station = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Jovana Ducica" }, Location = new Location() { X = 45.248643, Y = 19.792551 }, Name = "Okretnica 8-ce" }; _context.Add(station); stations8.Add(station); Station station2 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.250305, Y = 19.791516 }, Name = "Okretnica 8-ce" }; _context.Add(station2); stations8.Add(station2); Station station3 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.253440, Y = 19.789697 }, Name = "Okretnica 8-ce" }; _context.Add(station3); stations8.Add(station3); Station station4 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.255003, Y = 19.794144 }, Name = "Okretnica 8-ce" }; _context.Add(station4); stations8.Add(station4); Station station5 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.251204, Y = 19.796821 }, Name = "Okretnica 8-ce" }; _context.Add(station5); stations8.Add(station5); Station station6 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.251023, Y = 19.798561 }, Name = "Okretnica 8-ce" }; _context.Add(station6); stations8.Add(station6); Station station7 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.254104, Y = 19.807508 }, Name = "Okretnica 8-ce" }; _context.Add(station7); stations8.Add(station7); Station station8 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.257155, Y = 19.816241 }, Name = "Okretnica 8-ce" }; _context.Add(station8); stations8.Add(station8); Station station9 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.258862, Y = 19.822775 }, Name = "Okretnica 8-ce" }; _context.Add(station9); stations8.Add(station9); Station station10 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.260678, Y = 19.833057 }, Name = "Okretnica 8-ce" }; _context.Add(station10); stations8.Add(station10); Station station11 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.262600, Y = 19.839689 }, Name = "Okretnica 8-ce" }; _context.Add(station11); stations8.Add(station11); Station station12 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Jovana Subotica" }, Location = new Location() { X = 45.260318, Y = 19.842785 }, Name = "Okretnica 8-ce" }; _context.Add(station12); stations8.Add(station12); Station station13 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.256089, Y = 19.841208 }, Name = "Okretnica 8-ce" }; _context.Add(station13); stations8.Add(station13); Station station14 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.253385, Y = 19.843214 }, Name = "Okretnica 8-ce" }; _context.Add(station14); stations8.Add(station14); Station station15 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.253521, Y = 19.847516 }, Name = "Okretnica 8-ce" }; _context.Add(station15); stations8.Add(station15); Station station16 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.251270, Y = 19.846840 }, Name = "Okretnica 8-ce" }; _context.Add(station16); stations8.Add(station16); Station station17 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.247750, Y = 19.849104 }, Name = "Okretnica 8-ce" }; _context.Add(station17); stations8.Add(station17); Station station18 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.245620, Y = 19.846819 }, Name = "Okretnica 8-ce" }; _context.Add(station18); stations8.Add(station18); Station station19 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21000", Street = "Bulevar Kneza Milosa" }, Location = new Location() { X = 45.238989, Y = 19.847967 }, Name = "Okretnica 8-ce" }; _context.Add(station19); stations8.Add(station19); //stations 4 Station station20 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Bulevar Jase Tomica" }, Location = new Location() { X = 45.26446733929329, Y = 19.829593939230563 }, Name = "Zeleznicka stanica" }; _context.Add(station20); stations4.Add(station20); Station station21 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Bulevar oslobodjenja 30" }, Location = new Location() { X = 45.26001187717833, Y = 19.832297605917574 }, Name = "Lutrija" }; _context.Add(station21); stations4.Add(station21); Station station22 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Bulevar oslobodjenja 66a" }, Location = new Location() { X = 45.25462227539901, Y = 19.83525958562143 }, Name = "Aleksandar zgrada" }; _context.Add(station22); stations4.Add(station22); Station station23 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Jevrejska 31" }, Location = new Location() { X = 45.25158042895487, Y = 19.83682571728548 }, Name = "Futoska pijaca" }; _context.Add(station23); stations4.Add(station23); Station station24 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Centar" }, Location = new Location() { X = 45.25390672021402, Y = 19.842361796692217 }, Name = "Lili" }; _context.Add(station24); stations4.Add(station24); Station station25 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Centar" }, Location = new Location() { X = 45.25318165264042, Y = 19.84401403744539 }, Name = "Bazar" }; _context.Add(station25); stations4.Add(station25); Station station26 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Bulevar Jase Tomica" }, Location = new Location() { X = 45.2534938719465, Y = 19.84743306299697 }, Name = "Zeleznicka stanica" }; _context.Add(station26); stations4.Add(station26); Station station27 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Bulevar Jase Tomica" }, Location = new Location() { X = 45.25122798857049, Y = 19.846853705849753 }, Name = "Zeleznicka stanica" }; _context.Add(station27); stations4.Add(station27); Station station28 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Strazilovska" }, Location = new Location() { X = 45.247753458479686, Y = 19.84917113443862 }, Name = "Strazilovska" }; _context.Add(station28); stations4.Add(station28); Station station29 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Izvor, Bulevar Cara Lazara" }, Location = new Location() { X = 45.24551257269078, Y = 19.846830790917465 }, Name = "Izvor" }; _context.Add(station29); stations4.Add(station29); Station station30 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Narodnog fronta 1" }, Location = new Location() { X = 45.24253627573835, Y = 19.84749597875316 }, Name = "Zeleznicka stanica" }; _context.Add(station30); stations4.Add(station30); Station station31 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Narodnog fronta" }, Location = new Location() { X = 45.24037140396024, Y = 19.837623919236535 }, Name = "Narodnog fronta" }; _context.Add(station31); stations4.Add(station31); Station station32 = new Station() { Address = new Address() { City = "Novi Sad", Number = "21101", Street = "Narodnog fronta" }, Location = new Location() { X = 45.237289068480194, Y = 19.826809252488488 }, Name = "Narodnog fronta" }; _context.Add(station32); stations4.Add(station32); } if (!_context.StationLines.Any()) { foreach (Station s in stations8) { StationLine stline = new StationLine() { Station = s }; _context.Add(stline); stlines8.Add(stline); } foreach (Station s in stations4) { StationLine stline = new StationLine() { Station = s }; _context.Add(stline); stlines4.Add(stline); } } if (!_context.Lines.Any()) { Line line8 = new Line() { Name = "NOVO NASELJE - CENTAR - LIMAN 4", LineNumber = 8, Buses = new List <Bus>() { new Bus() { BusNumber = 244, InUse = true, }, new Bus() { BusNumber = 222, InUse = true, }, new Bus() { BusNumber = 234, InUse = true, } }, Stations = stlines8 }; _context.Add(line8); lines8 = line8; Line line4 = new Line() { Name = "ZELEZNICKA STANICA - CENTAR - LIMAN 4", LineNumber = 4, Buses = new List <Bus>() { new Bus() { BusNumber = 135, InUse = true, }, new Bus() { BusNumber = 789, InUse = true, }, new Bus() { BusNumber = 354, InUse = true, } }, Stations = stlines4 }; _context.Add(line4); lines4 = line4; } if (!_context.TimeTables.Any()) { TimeTable timeTable1 = new TimeTable() { Day = "Working day", Departures = "00:12-22-25-34-40-45-58-/01:30-/02:30-/03:30-/04:30-/05:30-/06:00-20-40-/07:03-12-23-30-45-58-/08:13-22-25-30-45-58-/09:03-12-23-30-45-58-/10:03-12-23-30-45-58-/11:03-12-23-30-45-58-/12:03-12-23-30-45-58-/13:03-12-23-30-45-58-/14:03-12-23-30-45-58-/15:03-12-23-30-45-58-/16:03-12-23-30-45-58-/17:00-23-30-40-58-/18:00-23-30-40-58-/19:00-23-30-40-58-/20:00-23-40-58-/21:00-23-40-58-/22:00-23-40-58-/23:00-30-58-/", Type = "In City", Line = lines8, LineId = lines8.Id }; _context.Add(timeTable1); TimeTable timeTable2 = new TimeTable() { Day = "Working day", Departures = "00:12-25-34-40-45-58-/01:30-/02:30-/03:30-/04:30-/05:30-/06:00-25-40-/07:03-14-23-37-45-52-/08:13-22-25-30-45-58-/09:03-12-30-45-58-/10:03-12-27-30-41-58-/11:03-12-23-30-45-58-/12:03-12-23-30-45-58-/13:03-12-23-30-45-58-/14:03-12-23-30-45-58-/15:03-12-23-30-45-58-/16:03-12-23-30-45-58-/17:00-23-30-40-58-/18:00-23-30-40-58-/19:00-23-30-40-58-/20:00-23-40-58-/21:00-23-40-58-/22:00-23-40-58-/23:00-30-58-/", Type = "In City", Line = lines4, LineId = lines4.Id }; _context.Add(timeTable2); } if (!_context.PricelistItems.Any()) { UserDiscount ud1 = new UserDiscount() { Type = "Student", Value = 20 }; _context.Add(ud1); UserDiscount ud2 = new UserDiscount() { Type = "Regular", Value = 0 }; _context.Add(ud2); UserDiscount ud3 = new UserDiscount() { Type = "Senior", Value = 35 }; _context.Add(ud3); Item it1 = new Item() { Type = "Hourly", Description = "This ticket is valid just for one hour" }; _context.Add(it1); Item it2 = new Item() { Type = "Daily", Description = "This ticket is valid until 00:00 next day" }; _context.Add(it2); Item it3 = new Item() { Type = "Monthly", Description = "This ticket is valid for one month" }; _context.Add(it3); Item it4 = new Item() { Type = "Annual", Description = "This ticket is valid for one year" }; _context.Add(it4); _context.SaveChanges(); Pricelist pr = new Pricelist() { Active = true, From = DateTime.Now, To = DateTime.Now.AddMonths(4) }; _context.Add(pr); _context.SaveChanges(); PricelistItem prit1 = new PricelistItem() { Pricelist = pr, Price = 150, Item = it1 }; _context.Add(prit1); PricelistItem prit2 = new PricelistItem() { Pricelist = pr, Price = 390, Item = it2 }; _context.Add(prit2); PricelistItem prit3 = new PricelistItem() { Pricelist = pr, Price = 3450, Item = it3 }; _context.Add(prit3); PricelistItem prit4 = new PricelistItem() { Pricelist = pr, Price = 12050, Item = it4 }; _context.Add(prit4); } _context.SaveChanges(); }
public async Task <PricelistItem> AddPricelist(NewPricelistDto pricelist) { var prl = new Pricelist() { From = pricelist.From, To = pricelist.To, Active = pricelist.Active }; _context.Pricelists.Add(prl); await SaveAll(); var hourItem = await _context.Items.FirstOrDefaultAsync(i => i.Type == "Hourly"); var dayItem = await _context.Items.FirstOrDefaultAsync(i => i.Type == "Daily"); var monthItem = await _context.Items.FirstOrDefaultAsync(i => i.Type == "Monthly"); var annualItem = await _context.Items.FirstOrDefaultAsync(i => i.Type == "Annual"); var priceHourly = new PricelistItem(); var priceDaily = new PricelistItem(); var priceMonthly = new PricelistItem(); var priceAnnual = new PricelistItem(); _mapper.Map(pricelist, priceHourly); _mapper.Map(pricelist, priceDaily); _mapper.Map(pricelist, priceMonthly); _mapper.Map(pricelist, priceAnnual); priceHourly.Item = hourItem; priceHourly.Price = pricelist.PriceHourly; priceHourly.Pricelist = prl; priceDaily.Item = dayItem; priceDaily.Price = pricelist.PriceDaily; priceDaily.Pricelist = prl; priceMonthly.Item = monthItem; priceMonthly.Price = pricelist.PriceMonthly; priceMonthly.Pricelist = prl; priceAnnual.Item = annualItem; priceAnnual.Price = pricelist.PriceAnnual; priceAnnual.Pricelist = prl; if (pricelist.Active) { var pricelistFromDb = await _context.PricelistItems.Include(pr => pr.Item).Include(pr => pr.Pricelist) .Where(pr => pr.Pricelist.Active == true).ToListAsync(); foreach (var pr in pricelistFromDb) { pr.Pricelist.Active = false; } } _context.PricelistItems.Add(priceHourly); _context.PricelistItems.Add(priceDaily); _context.PricelistItems.Add(priceMonthly); _context.PricelistItems.Add(priceAnnual); if (await SaveAll()) { return(priceAnnual); } else { return(null); } }
public bool UpdatePricelistItem(PricelistItem pricelistItem, int?pricelistNumber, int?customerNumber) { //Log(" -> " + pricelistItem.ProductCode + " (" + pricelistItem.Active.ToString() + " " + pricelistItem.GetIdValue().ToString() + ") " + pricelistItem.BatchSize.ToString() + "ks " + pricelistItem.ContractPrice.ToString()); decimal price = pricelistItem.ContractPrice; string articleCode = pricelistItem.ProductCode; int? quantity = (int)pricelistItem.BatchSize; if (quantity <= 1) { quantity = null; } Article article = visma.GetArticleByCode(articleCode); // If pricelist has Discount value, use it to calculate the final price from currect Article price if ((pricelistItem.ContractDiscount > 0) && (pricelistItem.ContractDiscount < 100) && (article != null)) { price = Math.Round(article.Price1 * ((100 - pricelistItem.ContractDiscount) / 100), 2); } // Check if pricelist is valid by looking at the PVM and VOIMASSA fields bool datesValid = true; DateTime validFromDate; DateTime validToDate; DateTime.TryParse(pricelistItem.PricelistDate, out validFromDate); DateTime.TryParse(pricelistItem.Valid, out validToDate); datesValid = ((validFromDate == DateTime.MinValue) || (validFromDate <= DateTime.Now)) && ((validToDate == DateTime.MinValue) || (validToDate >= DateTime.Now)); bool currencyMatch = true; if (!string.IsNullOrEmpty(article.Currency) && !string.IsNullOrEmpty(pricelistItem.Currency)) { if (article.Currency != pricelistItem.Currency) { currencyMatch = false; } } if ((article != null) && (datesValid) && (currencyMatch)) // && (article.ArticleCode == "A0112") { string pricelistItemName = article.ArticleCode; pricelistItemName += pricelistNumber.HasValue ? string.Format(" P{0}", pricelistNumber) : string.Format(" C{0}", customerNumber); if (quantity != null) { pricelistItemName += string.Format(" Q{0}", quantity); } string famiyCode = article.FamilyCode; string articleName = null; if (!string.IsNullOrEmpty(article.FamilyCode)) { articleName = visma.GetCommonArticleNameForFamilyCode(article.FamilyCode); } DataSet.ProductDataTable genericProducts = productTA.GetDataByArticleCode(shop.ID, articleCode); if (genericProducts.Count > 0) { long?shopifyProductID = genericProducts[0].ShopifyProductID; long?shopifyVariantID = null; long?shopifyVariantVatID = null; DataSet.ProductDataTable specificProducts = productTA.GetDataByArticleCodeSpecific(shop.ID, articleCode, pricelistNumber, customerNumber, quantity); if (specificProducts.Count == 0) { Log(" - Create specific price"); try { shop.CreateUpdateProductVariants(article, shopifyProductID.Value, ref shopifyVariantID, ref shopifyVariantVatID, pricelistNumber, customerNumber, quantity, price, articleName); Log(string.Format(" - Shopify Product Variant [{0}] created for specific price {1}.", shopifyVariantID, pricelistItemName)); productTA.InsertProduct(shop.ID, shopifyProductID, shopifyVariantID, shopifyVariantVatID, articleCode, pricelistNumber, customerNumber, quantity, price, famiyCode, null, null); } catch (System.Net.WebException ex) { LogError(string.Format("Unable to create specific price {0}.", pricelistItemName), ex); } } else { shopifyProductID = specificProducts[0].ShopifyProductID; shopifyVariantID = specificProducts[0].ShopifyVariantID; shopifyVariantVatID = specificProducts[0].ShopifyVariantVatID; try { shop.CreateUpdateProductVariants(article, shopifyProductID.Value, ref shopifyVariantID, ref shopifyVariantVatID, pricelistNumber, customerNumber, quantity, price, articleName); Log(string.Format(" - Shopify Product Variant [{0}] updated for specific price {1}.", shopifyVariantID, pricelistItemName)); } catch (System.Net.WebException ex) { LogError(string.Format("Unable to update specific price {0}.", pricelistItemName), ex); } } } else { //Log(" - Product not in Shopify"); } } return(true); }