Пример #1
0
        public static CheckResult ValidateSave(DSModel db, DriverModel model)
        {
            CheckResult res = new CheckResult(model);
            if (string.IsNullOrWhiteSpace(model.FirstName))
                res.AddError("Please enter a First Name of the driver!", model.GetName(p => p.FirstName));
            if (string.IsNullOrWhiteSpace(model.SecondName))
                res.AddWarning("Missing Second Name of the driver!", model.GetName(p => p.SecondName));
            if (string.IsNullOrWhiteSpace(model.LastName))
                res.AddError("Please enter a Last Name of the driver!", model.GetName(p => p.LastName));
            //if (model.LicenseID == 0)
            //    res.AddError("Please select a license!", model.GetName(p => p.LicenseID));
            //if (!model.LicenseExpirationDate.HasValue)
            //    res.AddError("Please enter a license expiration date", model.GetName(p => p.LicenseExpirationDate));
            if (model.PayRateOverride < 0.0m)
                res.AddError("Please enter a positive number for Pay Rate Override!", model.GetName(p => p.PayRateOverride));
            if (model.DriverCode != string.Empty)
            {
                var check = db.Drivers.Where(d => d.DriverCode == model.DriverCode && d.DriverID != model.DriverID).FirstOrDefault();
                if (check != null)
                    res.AddError("Another driver already uses this Driver Code! Use Peek or leave blank to autogenerate!", model.GetName(p => p.DriverCode));
            }
            foreach (var l in model.Locations)
            {
                if (l.LocationID == 0)
                    res.AddError("Please choose a Location for hte travel pay!", "LocationID");
                if (l.TravelPay == 0.0m)
                    res.AddError("Please enter a positive non-zero number for Travel Pay!", "TravelPay");
            }

            return res;
        }
        public static CheckResult ValidateSave(DSModel db, DriverLicenseModel model)
        {
            CheckResult res = new CheckResult(model);

            if (model.DriverID == 0)
                res.AddError("Driver cannot be empty!", model.GetName(p => p.DriverID));
            if (model.LicenseID == 0)
                res.AddError("License type cannot be empty!", model.GetName(p => p.LicenseID));
            if (model.IssueDate == DateTime.MinValue)
                res.AddError("Issue date cannot be empty!", model.GetName(p => p.IssueDate));
            if (model.ExpirationDate == DateTime.MinValue)
                res.AddError("Expiration date cannot be empty!", model.GetName(p => p.ExpirationDate));
            if (model.MVRReviewDate.HasValue && model.MVRReviewDate.Value.Date < model.IssueDate.Date)
                res.AddError("MVR review date cannot be earlier than Issue date!", model.GetName(p => p.MVRReviewDate));
            if (model.IssueDate.Date > model.ExpirationDate.Date)
                res.AddError("Expiration date cannot be earlier than Issue date!", model.GetName(p => p.ExpirationDate));
            if (model.Permits.Count == 0)
                res.AddError("At least one permit must be selected!", model.GetName(p => p.Permits));

            //overlap check
            var check = db.DriversLicenses
                .Where(d =>
                    d.DriverLicenseID != model.DriverLicenseID &&
                    d.DriverID == model.DriverID &&
                    d.IssueDate < model.ExpirationDate &&
                    d.ExpirationDate > model.IssueDate)
                .FirstOrDefault();
            if (check != null)
                res.AddError("There is an overlapping license already!");

            return res;
        }
        public static CheckResult ValidateSaveView(DSModel db, FileBlobViewModel model)
        {
            CheckResult res = new CheckResult(model);

            if (string.IsNullOrWhiteSpace(model.BlobName))
                res.AddError("File name cannot bet empty!", model.GetName(p => p.BlobName));
            if (string.IsNullOrWhiteSpace(model.BlobExtension))
                res.AddError("File extension cannot be empty!", model.GetName(p => p.BlobExtension));

            return res;
        }
        public static CheckResult ValidateDelete(DSModel db, DriverLicenseModel model)
        {
            CheckResult res = new CheckResult(model);

            var checkDispatch = db.Dispatches
                .Where(d => d.DriverID == model.DriverID &&
                    d.FromDateTime.Date >= model.IssueDate &&
                    d.ToDateTime.Date <= model.ExpirationDate)
                .FirstOrDefault();
            if (checkDispatch != null)
                res.AddError("Cannot delete license, a dispatch uses the license!");

            return res;
        }
        public static CheckResult ValidateSave(DSModel db, InvoiceModel model)
        {
            CheckResult res = new CheckResult(model);
            if (model.CompanyID == 0)
                res.AddError("Please choose a Company!", model.GetName(p => p.CompanyID));
            if (model.LocationID == 0)
                res.AddError("Please choose a Location!", model.GetName(p => p.LocationID));
            if (model.InvoicePeriodFrom == DateTime.MinValue)
                res.AddError("Please enter a date for Period From!", model.GetName(p => p.InvoicePeriodFrom));
            if (model.InvoicePeriodTo == DateTime.MinValue)
                res.AddError("Please enter a date for Period To!", model.GetName(p => p.InvoicePeriodTo));
            if (model.InvoicePeriodTo < model.InvoicePeriodFrom)
                res.AddError("The date for Pertiod To cannot be earlier than Period From!", model.GetName(p => p.InvoicePeriodTo));

            return res;
        }
Пример #6
0
        public static CheckResult ValidateSave(DSModel db, UserModel user)
        {
            CheckResult res = new CheckResult();
            if (string.IsNullOrWhiteSpace(user.Username))
                res.AddError("Please enter a Username!", user.GetName(p => p.Username));
            if (string.IsNullOrWhiteSpace(user.FirstName))
                res.AddError("Please enter a First Name!", user.GetName(p => p.FirstName));
            if (string.IsNullOrWhiteSpace(user.LastName))
                res.AddError("Please enter a Last Name!", user.GetName(p => p.LastName));

            var check = db.Users.Where(u => u.Username == user.Username && u.UserID != user.UserID).FirstOrDefault();
            if (check != null)
                res.AddError("Another user already uses this username!", user.GetName(p => p.Username));

            return res;
        }
        public static CheckResult ValidateInvoiceRates(DSModel db, List<CompanyInvoicingPayRateModel> rates)
        {
            var dic = db.ConstLicenses.ToDictionary(d => d.LicenseID, d => d.LicenseName);
            CheckResult res = new CheckResult();
            for (int i = 0; i < rates.Count; i++)
            {
                var cur = rates[i];
                if (cur.ToDate.HasValue && cur.ToDate.Value.Date < cur.FromDate.Date)
                {
                    res.AddError(
                        string.Format("To date cannot be earlier than From date! License: {0} From: {1} To: {2}",
                        dic[cur.LicenseID],
                        cur.FromDate.ToString(GLOB.Formats.Date),
                        cur.ToDate.GetValueOrDefault().ToString(GLOB.Formats.Date)),
                        cur.GetName(p => p.ToDate));
                    return res;
                }

                if (rates.Count > 1)
                {
                    for (int q = 0; q < rates.Count; q++)
                    {
                        if (q == i)
                            continue;

                        //overlap = ! ( (end2 < start1) || (start2 > end1) ); 
                        var next = rates[q];
                        if (!((next.ToDate.HasValue && next.ToDate.Value.Date < cur.FromDate.Date) || (cur.ToDate.HasValue && next.FromDate.Date > cur.ToDate.Value.Date))
                            && next.LicenseID == cur.LicenseID)
                        {
                            res.AddError(
                                string.Format("Overlapping invoice dates detected!\r\n\r\nLicense: {0} From: {1} To: {2}\r\nLicense: {3} From: {4} To: {5}",
                                dic[cur.LicenseID],
                                cur.FromDate.ToString(GLOB.Formats.Date),
                                (cur.ToDate.HasValue ? cur.ToDate.GetValueOrDefault().ToString(GLOB.Formats.Date) : "NOW"),
                                dic[next.LicenseID],
                                next.FromDate.ToString(GLOB.Formats.Date),
                                (next.ToDate.HasValue ? next.ToDate.GetValueOrDefault().ToString(GLOB.Formats.Date) : "NOW"))
                                , cur.GetName(p => p.ToDate));
                            return res;
                        }
                    }
                }
            }

            return res;
        }
        public static CheckResult ValidateSave(DSModel db, DriverMedicalModel model)
        {
            CheckResult res = new CheckResult(model);

            if (model.DriverID == 0)
                res.AddError("Driver cannot be empty!", model.GetName(p => p.DriverID));
            if (model.MedTypeID == 0)
                res.AddError("Medical type cannot be empty!", model.GetName(p => p.MedTypeID));
            if (model.ExaminationDate == DateTime.MinValue)
                res.AddError("Examination date cannot be empty!", model.GetName(p => p.ExaminationDate));
            if (model.ValidityDate == DateTime.MinValue)
                res.AddError("Validity date cannot be empty!", model.GetName(p => p.ValidityDate));
            if (model.ValidityDate.Date <= model.ExaminationDate.Date)
                res.AddError("Validity date cannot be earlier than Examination date!", model.GetName(p => p.ValidityDate));

            return res;
        }
Пример #9
0
        public CheckResult DeleteDriver(DriverModel model)
        {
            try
            {
                using (var db = DB.GetContext())
                {
                    var check = DriverValidator.ValidateDelete(db, model);
                    if (check.Failed)
                        return check;

                    DriverRepository.DeleteDriver(db, model);
                    db.SaveChanges();
                    return check;
                }
            }
            catch (Exception ex)
            {
                CheckResult res = new CheckResult();
                res.AddError("The following driver has records in the database and cannot be deleted!", "DriverID");
                return res;
            }
        }
Пример #10
0
        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;
        }
        public static CheckResult ValidateDelete(DSModel db, FileBlobModel model)
        {
            CheckResult res = new CheckResult(model);

            return res;
        }
        public static CheckResult ValidateDelete(DSModel db, DriverMedicalModel model)
        {
            CheckResult res = new CheckResult(model);

            return res;
        }
Пример #13
0
        public CheckResult PreviewFile(FileBlobViewModel model)
        {
            CheckResult res = new CheckResult(model);
            using (var db = DB.GetContext())
            {
                var file = FileBlobRepository.GetBlob(db, model.BlobID);
                if (file == null)
                {
                    res.AddError("No such file!", string.Empty);
                    return res;
                }

                string directory = Path.Combine(Environment.CurrentDirectory, "files");
                if (!Directory.Exists(directory))
                    Directory.CreateDirectory(directory);

                string fileName = Path.Combine(directory, ExtensionMethods.SafeFileName(file.BlobName) + file.BlobExtension);
                if (File.Exists(fileName))
                {
                    try
                    {
                        File.Delete(fileName);
                    }
                    catch (Exception ex)
                    {
                        return new CheckResult(ex);
                    }
                }

                try
                {
                    File.WriteAllBytes(fileName, file.BlobData);
                    ProcessStartInfo info = new ProcessStartInfo(fileName);
                    info.UseShellExecute = true;
                    Process.Start(info);
                    return res;
                }
                catch (Exception ex)
                {
                    return new CheckResult(ex);
                }
            }
        }