Example #1
0
        public async Task <IActionResult> Create(DocumentTemplateViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                await BindControlsAsync(model);

                Title     = DocumentResources.DocumentTemplate;
                ReturnUrl = returnUrl;
                return(View(model));
            }

            var template = new DocumentTemplate
            {
                Name      = model.Name,
                CreatorId = UserId,
                DocumentTemplateProducts = model.Products.Where(p => p.Checked).Select(m => new DocumentTemplateProduct
                {
                    ProductId = m.Id
                }).ToList()
            };

            Uow.DocumentTemplates.Add(template);
            await Uow.SaveAsync();

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #2
0
        public async Task <IActionResult> Edit(JobTitleViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                await BindControls(model);

                Title     = HrResources.JobTitle;
                ReturnUrl = returnUrl;
                return(View(model));
            }


            if (model.Id.GetValueOrDefault() <= 0)
            {
                return(NotFound());
            }

            var jobTitle = await Uow.JobTitles.Where(t => t.Id == model.Id && !t.IsDeleted).SingleOrDefaultAsync();

            if (jobTitle == null)
            {
                return(NotFound());
            }

            jobTitle.DepartmentId = model.DepartmentId.GetValueOrDefault();
            jobTitle.ModifierId   = UserId;
            jobTitle.ModifiedDate = DateTime.Now;


            var translates = await Uow.JobTitleTranslates.Where(t => t.Id == model.Id).ToListAsync();

            for (var i = 0; i < model.Texts.Count; i++)
            {
                var translate = new JobTitleTranslate {
                    Id = model.Id.GetValueOrDefault(), CultureId = model.Texts[i].Key, Text = model.Texts[i].Value
                };
                if (await Uow.JobTitleTranslates.Where(t => t.Text == translate.Text && t.Id != model.Id).AnyAsync())
                {
                    ModelState.AddModelError($"{nameof(JobTitleViewModel.Texts)}[{i}].Value", string.Format(ValidationResources.DuplicatedValidationError, "ტექსტი"));
                    continue;
                }

                var dbTranslate = translates.SingleOrDefault(t => t.CultureId == translate.CultureId);
                if (dbTranslate == null)
                {
                    dbTranslate = translate;
                    Uow.JobTitleTranslates.Add(dbTranslate);
                }
                else
                {
                    dbTranslate.Text = translate.Text;
                }
            }

            await Uow.SaveAsync();

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #3
0
        public async Task <IActionResult> Create(JobTitleViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                await BindControls(model);

                Title     = HrResources.JobTitle;
                ReturnUrl = returnUrl;
                return(View(model));
            }
            var jobTitle = new JobTitle
            {
                DepartmentId = model.DepartmentId.GetValueOrDefault(),
                CreatorId    = UserId
            };



            var translates = new List <JobTitleTranslate>();//  model.Texts.Select(x => );

            for (var i = 0; i < model.Texts.Count; i++)
            {
                var translate = new JobTitleTranslate {
                    Dictionary = jobTitle, CultureId = model.Texts[i].Key, Text = model.Texts[i].Value
                };
                if (await Uow.JobTitleTranslates.Where(t => t.Text == translate.Text).AnyAsync())
                {
                    ModelState.AddModelError($"{nameof(JobTitleViewModel.Texts)}[{i}].Value", string.Format(ValidationResources.DuplicatedValidationError, "ტექსტი"));
                    continue;
                }

                translates.Add(translate);
            }

            if (!ModelState.IsValid)
            {
                await BindControls(model);

                Title     = HrResources.JobTitle;
                ReturnUrl = returnUrl;
                return(View(model));
            }

            Uow.JobTitles.Add(jobTitle);
            Uow.JobTitleTranslates.AddRange(translates);
            await Uow.SaveAsync();

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #4
0
        public async Task <IActionResult> Edit(DocumentTemplateViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                await BindControlsAsync(model);

                Title     = DocumentResources.DocumentTemplate;
                ReturnUrl = returnUrl;
                return(View(model));
            }


            var template = await Uow.DocumentTemplates.Where(t => t.Id == model.Id && !t.IsDeleted).SingleOrDefaultAsync();

            if (template == null)
            {
                return(NotFound());
            }

            template.Name         = model.Name;
            template.ModifierId   = UserId;
            template.ModifiedDate = DateTime.Now;


            var dbProducts = await Uow.DocumentTemplateProducts.Where(p => p.DocumentTemplateId == model.Id).ToListAsync();

            var dbIds               = new HashSet <int>(dbProducts.Select(t => t.ProductId));
            var checkedProducts     = model.Products.Where(t => t.Checked).ToList();
            var checkedIds          = new HashSet <int>(checkedProducts.Select(t => t.Id));
            var toBeDeletedProducts = dbProducts.Where(t => !checkedIds.Contains(t.ProductId)).ToList();

            Uow.DocumentTemplateProducts.RemoveRange(toBeDeletedProducts);

            Uow.DocumentTemplateProducts.AddRange(checkedProducts.Where(t => !dbIds.Contains(t.Id))
                                                  .Select(t => new DocumentTemplateProduct
            {
                DocumentTemplateId = template.Id,
                ProductId          = t.Id
            }));


            await Uow.SaveAsync();


            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #5
0
        public async Task <IActionResult> Create(ProductViewModel model, string returnUrl = null)
        {
            ReturnUrl = returnUrl;

            if (ModelState.IsValid)
            {
                if (await Uow.Products.Where(p => p.ProductNumber == model.ProductNumber).AnyAsync())
                {
                    ModelState.AddModelError(nameof(ProductViewModel.ProductNumber), ValidationResources.DuplicatedValidationError);
                }

                if (await Uow.Products.Where(p => p.Name == model.Name).AnyAsync())
                {
                    ModelState.AddModelError(nameof(ProductViewModel.Name), ValidationResources.DuplicatedValidationError);
                }
            }

            if (!ModelState.IsValid)
            {
                BindControls(model);
                Title     = ProductResources.ProductNumber;
                ReturnUrl = returnUrl;
                return(View(model));
            }



            var product = new Product
            {
                Name          = model.Name,
                ProductNumber = model.ProductNumber,
                CategoryId    = ArrayHelper.Coalesce(model.SubCategoryId, model.CategoryId),
                CurrencyId    = ISO4217.GEL.ToInt32(),
                CreatorId     = UserId
            };

            Uow.Products.Add(product);
            await Uow.SaveAsync();

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #6
0
        public async Task <IActionResult> Create(DocumentViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                await BindControlsAsync(model);

                ReturnUrl = returnUrl;
                return(View(model));
            }

            var template = await Uow.DocumentTemplates.Where(t => t.Id == model.TemplateId && !t.IsDeleted).Select(t => new { t.Name }).SingleOrDefaultAsync();

            if (template == null)
            {
                return(BadRequest());
            }

            var document = new Document
            {
                CreatorId  = UserId,
                TemplateId = model.TemplateId.GetValueOrDefault()
            };

            await UpdateEmployeeInfoAsync(document);

            var dbIds = new HashSet <int>(await Uow.DocumentTemplateProducts.Where(t => t.DocumentTemplateId == model.TemplateId && !t.Product.IsDeleted).Select(t => t.ProductId).ToListAsync());

            document.DocumentProducts = model.Products.Where(t => t.Quantity > 0 && dbIds.Contains(t.Id)).Select(t => new DocumentProduct
            {
                ProductId = t.Id,
                Quantity  = t.Quantity.GetValueOrDefault()
            }).ToList();


            Uow.Documents.Add(document);
            await Uow.SaveAsync();

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #7
0
        public async Task <IActionResult> Restore(int id)
        {
            if (id <= 0)
            {
                return(NotFound());
            }

            var product = await Uow.Products.Where(p => p.Id == id && p.IsDeleted).SingleOrDefaultAsync();

            if (product == null)
            {
                return(NotFound());
            }

            product.IsDeleted    = false;
            product.ModifiedDate = DateTime.Now;
            product.ModifierId   = UserId;
            await Uow.SaveAsync();

            return(Ok());
        }
Example #8
0
        public async Task <IActionResult> Approve(int id)
        {
            if (id <= 0)
            {
                return(NotFound());
            }

            var document = await Uow.Documents.Where(d => d.Id == id && !d.IsDeleted && !d.IsApproved).SingleOrDefaultAsync();

            if (document == null)
            {
                return(NotFound());
            }

            document.IsApproved   = true;
            document.ModifiedDate = DateTime.Now;
            document.ApproverId   = UserId;
            await Uow.SaveAsync();

            return(Ok());
        }
Example #9
0
        private async Task ImageUploadAsync(IFormFile image, Employee employee)
        {
            if (image?.Length > 0 && _options.ImageContentTypes.Contains(image.ContentType, StringComparer.OrdinalIgnoreCase))
            {
                var fileName = Path.GetFileName(image.FileName);

                if (fileName != null)
                {
                    var extension = Path.GetExtension(image.FileName);
                    var file      = $"{employee.Id}{extension}";
                    var path      = Path.Combine(_environment.WebRootPath, $@"images\employees\{file}");
                    FileHelper.CreateDirectoryIfNotExists(path);
                    using (var fileStream = new FileStream(path, FileMode.Create))
                    {
                        await image.CopyToAsync(fileStream);
                    }
                    employee.Image = file;
                    await Uow.SaveAsync();
                }
            }
        }
Example #10
0
        public async Task <IActionResult> Delete(int id)
        {
            if (id <= 0)
            {
                return(NotFound());
            }

            var person = await Uow.Persons.Where(p => p.Id == id && !p.IsDeleted).SingleOrDefaultAsync();

            if (person == null)
            {
                return(NotFound());
            }

            person.IsDeleted    = true;
            person.ModifiedDate = DateTime.Now;
            person.ModifierId   = UserId;
            await Uow.SaveAsync();

            return(Ok());
        }
Example #11
0
        public async Task <IActionResult> Edit(DocumentViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                await BindControlsAsync(model);

                Title = await Uow.DocumentTemplates.Where(t => t.Id == model.TemplateId).Select(t => t.Name).SingleOrDefaultAsync();

                ReturnUrl = returnUrl;
                return(View(model));
            }



            var template = await Uow.DocumentTemplates.Where(t => t.Id == model.TemplateId && !t.IsDeleted).Select(t => new { t.Name }).SingleOrDefaultAsync();

            if (template == null)
            {
                return(BadRequest());
            }


            var query = Uow.Documents.Where(t => t.Id == model.Id && !t.IsDeleted);

            if (User.IsInAnyRole())
            {
                query = query.Where(t => t.CreatorId == UserId);
            }
            var document = await query.SingleOrDefaultAsync();

            if (document == null)
            {
                return(NotFound());
            }

            await UpdateEmployeeInfoAsync(document);

            document.ModifierId   = UserId;
            document.ModifiedDate = DateTime.Now;

            var templateProductIds = new HashSet <int>(await Uow.DocumentTemplateProducts.Where(t => t.DocumentTemplateId == model.TemplateId && !t.Product.IsDeleted).Select(t => t.ProductId).ToListAsync());
            var dbProducts         = await Uow.DocumentProducts.Where(t => t.DocumentId == model.Id).ToListAsync();

            var checkedProducts     = model.Products.Where(t => t.Quantity > 0 && templateProductIds.Contains(t.Id)).ToList();
            var checkedIds          = new HashSet <int>(checkedProducts.Select(t => t.Id));
            var toBeDeletedProducts = dbProducts.Where(t => !checkedIds.Contains(t.ProductId)).ToList();

            Uow.DocumentProducts.RemoveRange(toBeDeletedProducts);
            foreach (var checkedProduct in checkedProducts)
            {
                var product = dbProducts.SingleOrDefault(p => p.ProductId == checkedProduct.Id);
                if (product == null)
                {
                    product = new DocumentProduct
                    {
                        DocumentId = model.Id.GetValueOrDefault(),
                        ProductId  = checkedProduct.Id,
                        Quantity   = checkedProduct.Quantity.GetValueOrDefault()
                    };
                    Uow.DocumentProducts.Add(product);
                }
                else
                {
                    product.Quantity = checkedProduct.Quantity.GetValueOrDefault();
                }
            }


            await Uow.SaveAsync();

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #12
0
        public async Task <IActionResult> Edit(EmployeeViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                BindControls(model);
                Title     = HrResources.Employee;
                ReturnUrl = returnUrl;
                return(View(model));
            }


            var employee = await Uow.Employees.Where(h => h.Id == model.Id).SingleOrDefaultAsync();

            if (employee == null)
            {
                return(NotFound());
            }

            employee.DepartmentId = model.DepartmentId;
            employee.JobTitleId   = model.JobTitleId;
            employee.BranchId     = model.BranchId;
            employee.StartDate    = model.StartDate;
            employee.EndDate      = model.EndDate;

            var person = await Uow.Persons.Where(h => h.Id == model.Id)
                         .Include(h => h.Address)
                         .Include(h => h.Contact)
                         .SingleOrDefaultAsync();

            person.ModifierId   = UserId;
            person.ModifiedDate = DateTime.Now;

            person.FirstName      = model.FirstName;
            person.LastName       = model.LastName;
            person.FirstNameEn    = string.Empty;
            person.LastNameEn     = string.Empty;
            person.PersonalNumber = model.PersonalNumber.IfNullEmpty();
            person.Passport       = model.Passport.IfNullEmpty();
            person.BirthDate      = model.BirthDate;
            person.GenderId       = model.GenderId;



            if (model.Address != null)
            {
                person.Address.City        = model.Address.City;
                person.Address.Street      = model.Address.Street;
                person.Address.HouseNumber = model.Address.HouseNumber;
                person.Address.PostalCode  = model.Address.PostalCode;
            }


            if (model.Contact != null)
            {
                person.Contact.Phone1  = model.Contact.Phone1;
                person.Contact.Phone2  = model.Contact.Phone2;
                person.Contact.Phone3  = model.Contact.Phone3;
                person.Contact.Fax1    = model.Contact.Fax1;
                person.Contact.Fax2    = model.Contact.Fax2;
                person.Contact.Fax3    = model.Contact.Fax3;
                person.Contact.Mobile1 = model.Contact.Mobile1;
                person.Contact.Mobile2 = model.Contact.Mobile2;
                person.Contact.Mobile3 = model.Contact.Mobile3;
                person.Contact.Email1  = model.Contact.Email1;
                person.Contact.Email2  = model.Contact.Email2;
                person.Contact.Email3  = model.Contact.Email3;
                person.Contact.Url     = model.Contact.Url;
            }
            await Uow.SaveAsync();

            if (model.ImageDelete && !string.IsNullOrEmpty(employee.Image))
            {
                var extension = Path.GetExtension(employee.Image);
                var file      = $"{employee.Id}{extension}";
                var path      = Path.Combine(_environment.WebRootPath, $@"images\employees\{file}");
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                    employee.Image = null;
                    await Uow.SaveAsync();
                }
            }
            await ImageUploadAsync(model.Image, employee);

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }
Example #13
0
        public async Task <IActionResult> Create(EmployeeViewModel model, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                BindControls(model);
                Title     = HrResources.Employee;
                ReturnUrl = returnUrl;
                return(View(model));
            }


            var person = new Person
            {
                FirstName      = model.FirstName,
                LastName       = model.LastName,
                FirstNameEn    = string.Empty,
                LastNameEn     = string.Empty,
                PersonalNumber = model.PersonalNumber.IfNullEmpty(),
                Passport       = model.Passport.IfNullEmpty(),
                BirthDate      = model.BirthDate,
                GenderId       = model.GenderId,//.NullIfDefault(Gender.None),
                CreatorId      = UserId,

                Address = new Address
                {
                    City        = model.Address.City,
                    Street      = model.Address.Street,
                    HouseNumber = model.Address.HouseNumber,
                    PostalCode  = model.Address.PostalCode,
                },
                Contact = new Contact
                {
                    Phone1  = model.Contact.Phone1,
                    Phone2  = model.Contact.Phone2,
                    Phone3  = model.Contact.Phone3,
                    Fax1    = model.Contact.Fax1,
                    Fax2    = model.Contact.Fax2,
                    Fax3    = model.Contact.Fax3,
                    Mobile1 = model.Contact.Mobile1,
                    Mobile2 = model.Contact.Mobile2,
                    Mobile3 = model.Contact.Mobile3,
                    Email1  = model.Contact.Email1,
                    Email2  = model.Contact.Email2,
                    Email3  = model.Contact.Email3,
                    Url     = model.Contact.Url,
                }
            };


            var employee = new Employee
            {
                DepartmentId = model.DepartmentId,
                JobTitleId   = model.JobTitleId,
                BranchId     = model.BranchId,
                StartDate    = model.StartDate,
                EndDate      = model.EndDate,

                Person = person
            };

            Uow.Employees.Add(employee);
            await Uow.SaveAsync();

            await ImageUploadAsync(model.Image, employee);

            return(!string.IsNullOrEmpty(returnUrl)
                ? RedirectToLocal(returnUrl)
                : RedirectToAction(nameof(Index)));
        }