public static void SaveHolidays(DSModel db, KeyBinder key, List<HolidayModel> model)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (key == null)
                throw new ArgumentNullException("key");
            if (model == null)
                throw new ArgumentNullException("model");

            var holidaysToDelete = db.Holidays.ToList();
            foreach (var h in holidaysToDelete.ToList())
            {
                var check = model.Where(hh => hh.HolidayDate == h.HolidayDate).FirstOrDefault();
                if (check == null)
                    db.Delete(h);
                else
                    model.Remove(check);
            }

            foreach (var h in model)
            {
                Holiday poco = new Holiday();
                poco.HolidayDate = h.HolidayDate;
                db.Add(poco);
                key.AddKey(poco, h, h.GetName(p => p.HolidayDate));
            }
        }
        public static void DeleteCompany(DSModel db, CompanyModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");

            if (model.CompanyID != 0)
            {
                var poco = db.Companies.Where(c => c.CompanyID == model.CompanyID).FirstOrDefault();
                if (poco != null)
                {
                    db.Delete(poco);
                }
            }
        }
        public static void DeleteDispatch(DSModel db, DispatchModel disp)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (disp == null)
                throw new ArgumentNullException("disp");

            if (disp.DispatchID != 0)
            {
                var poco = db.Dispatches.Where(d => d.DispatchID == disp.DispatchID).FirstOrDefault();
                if (poco != null)
                    db.Delete(poco);
            }
        }
        private static void UpdateCompany(DSModel db, KeyBinder key, CompanyModel model)
        {
            Company poco = db.Companies.Where(c => c.CompanyID == model.CompanyID).FirstOrDefault();
            if (poco == null)
                throw new ArgumentException("No company with the specified ID!");

            poco.CompanyID = model.CompanyID;
            poco.CompanyName = model.CompanyName;
            poco.CompanyCode = model.CompanyCode;
            poco.CompanyAddress1 = model.CompanyAddress1;
            poco.CompanyAddress2 = model.CompanyAddress2;
            poco.CompanyCity = model.CompanyCity;
            poco.CompanyState = model.CompanyState;
            poco.CompanyPostCode = model.CompanyPostCode;
            poco.CompanyContactName = model.CompanyContactName;
            poco.CompanyFax = model.CompanyFax;
            poco.CompanyPhone = model.CompanyPhone;
            poco.CompanyEmail = model.CompanyEmail;
            poco.LunchTime = model.LunchTime;
            poco.TrainingTime = model.TrainingTime;
            poco.IsEnabled = model.IsEnabled;

            List<Location> locationsToBeDeleted = poco.Locations.Where(dl => !model.Locations.Any(ml => ml.LocationID == dl.LocationID)).ToList();
            foreach (var del in locationsToBeDeleted)
            {
                db.Delete(del);
                poco.Locations.Remove(del);
            }

            foreach (var ins in model.Locations)
            {
                Location loc = LocationRepository.SaveLocation(db, key, ins, poco);
                poco.Locations.Add(loc);
                key.AddKey(loc, ins, ins.GetName(p => p.LocationID));
            }
        }
        public static void DeleteLocation(DSModel db, LocationModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");

            if (model.LocationID != 0)
            {
                var poco = db.Locations.Where(l => l.LocationID == model.LocationID).FirstOrDefault();
                if (poco != null)
                {
                    if (poco.ConfirmationContactID.HasValue)
                        db.Delete(poco.ConfirmationContact);
                    if (poco.InvoiceContactID.HasValue)
                        db.Delete(poco.InvoiceContact);
                    if (poco.DispatchContactID.HasValue)
                        db.Delete(poco.DispatchContact);
                    db.Delete(poco);
                }
            }
        }
        public static void DeleteDriver(DSModel db, DriverModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");

            if (model.DriverID != 0)
            {
                var poco = db.Drivers.Where(d => d.DriverID == model.DriverID).FirstOrDefault();
                if (poco != null)
                    db.Delete(poco);
            }
        }
        private static void UpdateDriver(DSModel db, KeyBinder key, DriverModel model)
        {
            var poco = db.Drivers.Where(d => d.DriverID == model.DriverID).FirstOrDefault();
            if (poco == null)
                throw new ArgumentException("No driver with this ID!");

            poco.DriverCode = model.DriverCode;
            poco.FirstName = model.FirstName;
            poco.SecondName = model.SecondName;
            poco.LastName = model.LastName;
            poco.DateOfBirth = model.DateOfBirth;
            poco.DateOfHire = model.DateOfHire;
            poco.CellPhone = model.CellPhone;
            poco.EmergencyPhone = model.EmergencyPhone;
            poco.Email = model.Email;
            poco.PayRateOverride = model.PayRateOverride;
            poco.IsEnabled = model.IsEnabled;

            foreach (var l in poco.LocationsDrivers.ToList())
            {
                db.Delete(l);
                poco.LocationsDrivers.Remove(l);
            }
            db.FlushChanges();
            foreach (var l in model.Locations)
            {
                poco.LocationsDrivers.Add(
                    new LocationsDriver()
                    {
                        LocationID = l.LocationID,
                        DriverID = l.DriverID,
                        TravelPay = l.TravelPay
                    });
            }
        }
        public static void DeleteInvoice(DSModel db, InvoiceModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (model == null)
                throw new ArgumentNullException("model");

            if (model.InvoiceID != 0)
            {
                var poco = db.Invoices.Where(i => i.InvoiceID == model.InvoiceID).FirstOrDefault();
                if (poco != null)
                    db.Delete(poco);
            }
        }
        private static void UpdateInvoice(DSModel db, KeyBinder key, InvoiceModel model)
        {
            var poco = db.Invoices.Where(i => i.InvoiceID == model.InvoiceID).FirstOrDefault();
            if (poco == null)
                throw new ArgumentException("No invoice with the specified ID!");

            poco.CompanyID = model.CompanyID;
            poco.LocationID = model.LocationID;
            poco.InvoiceTypeID = model.InvoiceTypeID;
            poco.InvoiceNumber = model.InvoiceNumber;
            poco.InvoiceIssueDate = model.InvoiceIssueDate;
            poco.InvoicePeriodFrom = model.InvoicePeriodFrom;
            poco.InvoicePeriodTo = model.InvoicePeriodTo;
            poco.InvoiceNote = model.InvoiceNote;
            poco.LateCharge = model.LateCharge;
            poco.LateChargeDays = model.LateChargeDays;
            poco.IsConfirmed = model.IsConfirmed;
            poco.UserID = model.UserID;
            poco.LastUpdateTime = model.LastUpdateTime;

            foreach (var d in poco.InvoicesDetails.ToList())
            {
                db.Delete(d);
                poco.InvoicesDetails.Remove(d);
            }

            foreach (var d in model.Details)
            {
                var det = new InvoicesDetail();
                det.InvoiceDetailDate = d.InvoiceDetailDate;
                det.InvoiceDetailName = d.InvoiceDetailName;
                det.InvoiceDetailTotalTime = d.InvoiceDetailTotalTime;
                det.InvoiceDetailOverTime = d.InvoiceDetailOverTime;
                det.InvoiceDetailRegularRate = d.InvoiceDetailRegularRate;
                det.InvoiceDetailOverRate = d.InvoiceDetailOverRate;
                det.InvoiceDetailRegularPay = d.InvoiceDetailRegularPay;
                det.InvoiceDetailOvertimePay = d.InvoiceDetailOvertimePay;
                det.InvoiceDetailGroupName = d.InvoiceDetailGroupName;
                det.InvoiceDetailGroupPosition = d.InvoiceDetailGroupPosition;
                det.Invoice = poco;
                det.InvoiceID = poco.InvoiceID;
                key.AddKey(det, d, d.GetName(p => p.InvoiceDetailID));
                poco.InvoicesDetails.Add(det);
                db.Add(det);
            }
        }