Пример #1
0
 private CompanyManager(DSModel db, CompanyModel company, List<CompanyLicensePayRateModel> license, List<CompanyInvoicingPayRateModel> invoice)
     : base(db)
 {
     this.ActiveModel = company;
     this.LicensePayRates = license;
     this.InvoicePayRates = invoice;
 }
        public static CompanyModel GetCompany(DSModel db, uint companyID)
        {
            if (db == null)
                throw new ArgumentNullException("db");

            var poco = db.Companies.Where(c => c.CompanyID == companyID).FirstOrDefault();
            if (poco == null)
                return null;

            CompanyModel mod = new CompanyModel();
            mod.CompanyID = poco.CompanyID;
            mod.CompanyName = poco.CompanyName;
            mod.CompanyCode = poco.CompanyCode;
            mod.CompanyAddress1 = poco.CompanyAddress1;
            mod.CompanyAddress2 = poco.CompanyAddress2;
            mod.CompanyCity = poco.CompanyCity;
            mod.CompanyState = poco.CompanyState;
            mod.CompanyPostCode = poco.CompanyPostCode;
            mod.CompanyFax = poco.CompanyFax;
            mod.CompanyPhone = poco.CompanyPhone;
            mod.CompanyEmail = poco.CompanyEmail;
            mod.LunchTime = poco.LunchTime;
            mod.TrainingTime = poco.TrainingTime;
            mod.IsEnabled = poco.IsEnabled;
            mod.Locations.AddRange(LocationRepository.GetLocationsByCompany(db, companyID));

            mod.IsChanged = false;

            return mod;
        }
Пример #3
0
        public static CompanyManager CreateEdit(CompanyModel company)
        {
            var db = DB.GetContext();
            var license = CompanyRepository.GetLicensesPayRates(db, company.CompanyID);
            var invoice = CompanyRepository.GetInvoicePayRates(db, company.CompanyID);

            return new CompanyManager(db, company, license, invoice);
        }
        public static CheckResult ValidateSave(DSModel db, CompanyModel model)
        {
            CheckResult res = new CheckResult(model);
            if (string.IsNullOrWhiteSpace(model.CompanyName))
                res.AddError("Please enter a Company Name!", model.GetName(p => p.CompanyName));
            if (model.CompanyCode != string.Empty)
            {
                var check = db.Companies.Where(c => c.CompanyCode == model.CompanyCode && c.CompanyID != model.CompanyID).FirstOrDefault();
                if (check != null)
                    res.AddError("Another company already uses this Company Code! Use Peek or leave blank to autogenerate!", model.GetName(p => p.CompanyCode));
            }
            if (model.TrainingTime < 0m)
                res.AddError("Traning time cannot be negative number!", model.GetName(p => p.TrainingTime));

            var groups = model.Locations.GroupBy(l => l.LocationCode).ToList();
            if (groups.Any(g => g.Count() > 1))
            {
                var gr = groups.Where(g => g.Count() > 1).FirstOrDefault();
                res.AddError(string.Format("Duplicate location codes! Code: {0}", gr.FirstOrDefault().LocationCode));
            }
            return res;
        }
Пример #5
0
        public CheckResult SaveCompany(CompanyModel model, List<CompanyLicensePayRateModel> licenseRates, List<CompanyInvoicingPayRateModel> invoiceRates)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = CompanyValidator.ValidateSave(db, model);
                    if (check.Failed)
                        return check;

                    var checkLicense = CompanyValidator.ValidateDriverRates(db, licenseRates);
                    if (checkLicense.Failed)
                        return checkLicense;

                    var checkInvoice = CompanyValidator.ValidateInvoiceRates(db, invoiceRates);
                    if (checkInvoice.Failed)
                        return checkInvoice;

                    KeyBinder key = new KeyBinder();
                    CompanyRepository.SaveCompany(db, key, model);
                    if (model.CompanyID != 0)
                    {
                        CompanyRepository.SaveLicensesPayRates(db, key, licenseRates);
                        CompanyRepository.SaveInvoicePayRates(db, key, invoiceRates);
                    }
                    db.SaveChanges();
                    key.BindKeys();
                    model.IsChanged = false;

                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
        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);
                }
            }
        }
        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));
            }
        }
        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));
        }
        public static void SaveCompany(DSModel db, KeyBinder key, CompanyModel model)
        {
            if (db == null)
                throw new ArgumentNullException("db");
            if (key == null)
                throw new ArgumentNullException("key");
            if (model == null)
                throw new ArgumentNullException("model");

            if (model.CompanyID == 0)
                InsertCompany(db, key, model);
            else
                UpdateCompany(db, key, model);
        }
        private void BindModel(CompanyModel model)
        {
            BindingSource bsModel = new BindingSource();
            bsModel.DataSource = model;

            CompanyName.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyName), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyCode.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyCode), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyAddress1.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyAddress1), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyAddress2.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyAddress2), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyCity.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyCity), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyState.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyState), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyPostCode.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyPostCode), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyContactName.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyContactName), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyFax.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyFax), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyPhone.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyPhone), true, DataSourceUpdateMode.OnPropertyChanged);
            CompanyEmail.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.CompanyEmail), true, DataSourceUpdateMode.OnPropertyChanged);
            LunchTime.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.LunchTime), true, DataSourceUpdateMode.OnPropertyChanged);
            TrainingTime.DataBindings.Add("EditValue", bsModel, model.GetName(p => p.TrainingTime), true, DataSourceUpdateMode.OnPropertyChanged);
            IsEnabled.DataBindings.Add("Checked", bsModel, model.GetName(p => p.IsEnabled), true, DataSourceUpdateMode.OnPropertyChanged);

            BindingSource bsLocations = new BindingSource();
            bsLocations.DataSource = model.Locations;
            gridControlLocations.DataSource = bsLocations;

            BindingSource bsLicenseRates = new BindingSource();
            bsLicenseRates.DataSource = this.Manager.LicensePayRates;
            gridControlLicense.DataSource = bsLicenseRates;

            BindingSource bsInvoiceRates = new BindingSource();
            bsInvoiceRates.DataSource = this.Manager.InvoicePayRates;
            gridControlInvoice.DataSource = bsInvoiceRates;
        }
Пример #11
0
 public static CheckResult ValidateDelete(DSModel db, CompanyModel model)
 {
     return new CheckResult(model);
 }
Пример #12
0
        public CheckResult DeleteCompany(CompanyModel model)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = CompanyValidator.ValidateDelete(db, model);
                    if (check.Failed)
                        return check;

                    CompanyRepository.DeleteCompany(db, model);
                    db.SaveChanges();
                    return check;
                }
            }
            catch (Exception ex)
            {
                return new CheckResult(ex);
            }
        }
Пример #13
0
 private CompanyManager(DSModel db, CompanyModel company) : this(db, company, new List<CompanyLicensePayRateModel>(), new List<CompanyInvoicingPayRateModel>()) { }