private static void InsertCompany(DSModel db, KeyBinder key, CompanyModel model)
        {
            Company poco = new Company();
            poco.CompanyID = model.CompanyID;
            poco.CompanyName = model.CompanyName;
            if (model.CompanyCode == string.Empty)
                poco.CompanyCode = "C" + CompanyRepository.PeekCompanyCode(db, "C");
            else
                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;

            foreach (var loc in model.Locations)
            {
                Location l = LocationRepository.SaveLocation(db, key, loc, poco);
                poco.Locations.Add(l);
                key.AddKey(l, loc, loc.GetName(p => p.LocationID));
                key.AddKey(poco, loc, loc.GetName(p => p.CompanyID));
            }

            db.Add(poco);

            key.AddKey(poco, model, model.GetName(p => p.CompanyID));
        }
        private static Location UpdateLocation(DSModel db, KeyBinder key, LocationModel model, Company company = null)
        {
            var poco = db.Locations.Where(l => l.LocationID == model.LocationID).FirstOrDefault();
            if (poco == null)
                throw new ArgumentException("No Location with the specified ID!");

            poco.LocationName = model.LocationName;
            poco.LocationCode = model.LocationCode;

            if (company == null)
                poco.CompanyID = model.CompanyID;
            else
                poco.Company = company;

            poco.LocationAddress = model.LocationAddress;
            poco.LocationPhone = model.LocationPhone;
            poco.LocationFax = model.LocationFax;

            if (model.ConfirmationContact.IsChanged)
            {
                poco.ConfirmationContact = ContactRepository.SaveContact(db, key, model.ConfirmationContact);
                key.AddKey(poco.ConfirmationContact, model.ConfirmationContact, poco.ConfirmationContact.GetName(p => p.ContactID));
                key.AddKey(poco.ConfirmationContact, model, poco.ConfirmationContact.GetName(p => p.ContactID), model.GetName(p => p.ConfirmationContactID));
            }
            if (model.InvoiceContact.IsChanged)
            {
                poco.InvoiceContact = ContactRepository.SaveContact(db, key, model.InvoiceContact);
                key.AddKey(poco.InvoiceContact, model.InvoiceContact, poco.InvoiceContact.GetName(p => p.ContactID));
                key.AddKey(poco.InvoiceContact, model, poco.InvoiceContact.GetName(p => p.ContactID), model.GetName(p => p.InvoiceContactID));
            }
            if (model.DispatchContact.IsChanged)
            {
                poco.DispatchContact = ContactRepository.SaveContact(db, key, model.DispatchContact);
                key.AddKey(poco.DispatchContact, model.DispatchContact, poco.DispatchContact.GetName(p => p.ContactID));
                key.AddKey(poco.DispatchContact, model, poco.DispatchContact.GetName(p => p.ContactID), model.GetName(p => p.DispatchContactID));
            }

            poco.TravelPay = model.TravelPay;
            poco.TravelPayName = model.TravelPayName;
            poco.LunchTime = model.LunchTime;
            poco.IsEnabled = model.IsEnabled;
            poco.IncludeConfirmation = model.IncludeConfirmation;
            return poco;
        }
        public static Location SaveLocation(DSModel db, KeyBinder key, LocationModel model, Company company = null)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (key == null)
                throw new ArgumentNullException("key");
            if (model == null)
                throw new ArgumentNullException("model");

            if (model.LocationID == 0)
                return InsertLocation(db, key, model, company);
            else
                return UpdateLocation(db, key, model, company);
        }
        private static Location InsertLocation(DSModel db, KeyBinder key, LocationModel model, Company company = null)
        {
            Location poco = new Location();
            poco.LocationName = model.LocationName;
            if (model.LocationCode == string.Empty)
                poco.LocationCode = "L" + LocationRepository.PeekLocationCode(db, "L");
            else
                poco.LocationCode = model.LocationCode;

            if (company == null)
                poco.CompanyID = model.CompanyID;
            else
                poco.Company = company;

            poco.LocationAddress = model.LocationAddress;
            poco.LocationPhone = model.LocationPhone;
            poco.LocationFax = model.LocationFax;

            if (model.ConfirmationContact.IsChanged)
            {
                poco.ConfirmationContact = ContactRepository.SaveContact(db, key, model.ConfirmationContact);
                key.AddKey(poco.ConfirmationContact, model.ConfirmationContact, poco.ConfirmationContact.GetName(p => p.ContactID));
                key.AddKey(poco.ConfirmationContact, model, poco.ConfirmationContact.GetName(p => p.ContactID), model.GetName(p => p.ConfirmationContactID));
            }
            if (model.InvoiceContact.IsChanged)
            {
                poco.InvoiceContact = ContactRepository.SaveContact(db, key, model.InvoiceContact);
                key.AddKey(poco.InvoiceContact, model.InvoiceContact, poco.InvoiceContact.GetName(p => p.ContactID));
                key.AddKey(poco.InvoiceContact, model, poco.InvoiceContact.GetName(p => p.ContactID), model.GetName(p => p.InvoiceContactID));
            }
            if (model.DispatchContact.IsChanged)
            {
                poco.DispatchContact = ContactRepository.SaveContact(db, key, model.DispatchContact);
                key.AddKey(poco.DispatchContact, model.DispatchContact, poco.DispatchContact.GetName(p => p.ContactID));
                key.AddKey(poco.DispatchContact, model, poco.DispatchContact.GetName(p => p.ContactID), model.GetName(p => p.DispatchContactID));
            }

            poco.TravelPay = model.TravelPay;
            poco.TravelPayName = model.TravelPayName;
            poco.LunchTime = model.LunchTime;
            poco.IsEnabled = model.IsEnabled;
            poco.IncludeConfirmation = model.IncludeConfirmation;
            db.Add(poco);
            key.AddKey(poco, model, model.GetName(p => p.LocationID));
            return poco;
        }