public DBResult SaveCustomer(CustomerModel model) { tblCustomer customer = model.Id == 0 ? new tblCustomer() : db.tblCustomers.FirstOrDefault(d => d.CustID == model.Id); customer.Colour = model.Colour; customer.DeliveryAddress = model.DeliveryAddress; customer.Name = model.Name; try { if (model.Id == 0) { db.tblCustomers.Add(customer); db.SaveChanges(); foreach (var journey in model.Journeys) { tblJourney dbJourney = db.tblJourneys.FirstOrDefault(j => j.ID == journey.ID); dbJourney.CustomerId = customer.CustID; } } else { var customerJourneys = db.tblJourneys.Where(j => j.CustomerId == model.Id).ToList(); customerJourneys.ForEach(cj => cj.CustomerId = null); db.SaveChanges(); foreach (var journey in model.Journeys) { tblJourney dbJourney = db.tblJourneys.FirstOrDefault(j => j.ID == journey.ID); dbJourney.CustomerId = model.Id; } } db.SaveChanges(); return(new DBResult { ReturnCode = ReturnCode.Success }); } catch (Exception ex) { return(new DBResult { Message = ex.Message, ReturnCode = ReturnCode.Failed }); } }
public bool SaveJourney(JourneyModel journey) { if (journey != null) { tblJourney dbJourney = journey.ID == 0 ? new tblJourney() : db.tblJourneys.FirstOrDefault(j => j.ID == journey.ID); dbJourney.Journey = journey.Journey; dbJourney.CustomerId = journey.CustomerId; dbJourney.Base = journey.Base; if (dbJourney.ID == 0) { db.tblJourneys.Add(dbJourney); } db.SaveChanges(); } return(false); }
public DBResult AddUser(UserModel user) { var retval = new DBResult { ReturnCode = ReturnCode.Success }; var tblUser = user.Id == 0 ? new tblUser() : db.tblUsers.FirstOrDefault(u => u.Id == user.Id); tblUser.FirstName = user.FirstName; tblUser.LastName = user.LastName; tblUser.UserName = user.UserName; tblUser.Password = user.Password; tblUser.UserType = user.UserType; tblUser.EmailAddress = user.EmailAddress; if (user.Id == 0) { tblUser.DateAdded = DateTime.Now; db.tblUsers.Add(tblUser); } db.SaveChanges(); return(retval); }
public DBResult SaveDriver(DriverModel model) { tblDriver driver = model.Id == 0 ? new tblDriver() : db.tblDrivers.FirstOrDefault(d => d.DriverID == model.Id); driver.Name = model.Name; driver.Surname = model.Surname; try { if (model.Id == 0) { db.tblDrivers.Add(driver); } db.SaveChanges(); return(new DBResult { ReturnCode = ReturnCode.Success }); } catch (Exception ex) { return(new DBResult { Message = ex.Message, ReturnCode = ReturnCode.Failed }); } }
public DBResult SaveDaybook(DaybookModel job) { tblJob tblJob = job.JobID != 0 ? db.tblJobs.FirstOrDefault(j => j.JobID == job.JobID) : new tblJob(); try { tblJob.Address = job.Address; tblJob.Area = job.Area; tblJob.BasePrice = job.BasePrice; tblJob.CustomerID = job.CustomerID; tblJob.CustRef = job.CustRef; tblJob.CustomerName = job.CustomerName; tblJob.DriverName = job.DriverName; tblJob.dtFrom = job.dtFrom; tblJob.dtTo = job.dtTo; tblJob.Notes = job.Notes; tblJob.Time = job.Time; tblJob.Journey = job.Journey; tblJob.Type = job.Type; tblJob.MiscCode = job.MiscCode; tblJob.TruckId = job.TruckId; tblJob.PinNumber = job.PinNumber; tblJob.TrailerId = job.TrailerId; tblJob.UseReturn = job.UseReturn; tblJob.JobRef = job.JobRef; tblJob.InvoiceNo = job.InvoiceNo; if (job.JobID == 0) { tblJob.DriverID = job.DriverID; db.tblJobs.Add(tblJob); if (job.UseReturn) { var dateFrom = Convert.ToDateTime(tblJob.dtFrom).AddDays(1); var dateTo = Convert.ToDateTime(tblJob.dtTo).AddDays(1); var currentDateUsed = dateFrom; while (currentDateUsed < dateTo) { tblJob retJob = new tblJob(); retJob.DriverID = job.DriverID; retJob.Address = job.Address; retJob.Area = job.Area; retJob.BasePrice = job.BasePrice; retJob.CustomerID = job.CustomerID; retJob.CustRef = job.CustRef; retJob.CustomerName = job.CustomerName; retJob.DriverName = job.DriverName; retJob.dtFrom = currentDateUsed; retJob.dtTo = job.dtTo; retJob.Notes = job.Notes; retJob.Time = job.Time; retJob.Journey = job.Journey; retJob.Type = job.Type; retJob.MiscCode = dateFrom == dateTo.AddDays(-2) ? string.Format("{0}-RETURNED", job.MiscCode) : job.MiscCode; retJob.TruckId = job.TruckId; retJob.PinNumber = job.PinNumber; retJob.TrailerId = job.TrailerId; retJob.UseReturn = job.UseReturn; retJob.JobRef = job.JobRef; retJob.dtFrom = currentDateUsed; db.tblJobs.Add(retJob); currentDateUsed = currentDateUsed.AddDays(1); } } } else { if (job.IsDuplicate) { if (tblJob.DriverID == job.DriverID) { tblJob.DuplicateOf = job.JobID; var duplicatesFound = db.tblJobs.Count(j => j.DuplicateOf == job.JobID); tblJob.DriverName = string.Format("{0} ({1})", job.DriverName, (duplicatesFound + 2)); } tblJob.DriverID = job.DriverID; db.tblJobs.Add(tblJob); } } db.SaveChanges(); return(new DBResult { ReturnCode = ReturnCode.Success }); } catch (Exception ex) { return(new DBResult { ReturnCode = ReturnCode.Failed, Message = ex.Message }); } }