public object CreateSalaryStructure([FromBody] SalaryStructureVM model)
        {
            object result    = null;
            string message   = "";
            string errorcode = "";
            string excp      = "";

            if (model == null)
            {
                return(BadRequest());
            }
            using (_context)
            {
                using (var _ctxTransaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        SalaryStructure objSalaryStructure = new SalaryStructure();
                        objSalaryStructure.Name        = model.Name;
                        objSalaryStructure.Description = model.Description;
                        objSalaryStructure.IsActive    = model.IsActive;
                        _context.SalaryStructure.Add(objSalaryStructure);
                        //await _ctx.SaveChangesAsync();
                        _context.SaveChanges();

                        if (model.DepartmentIds.Count > 0)
                        {
                            foreach (int DepartmentId in model.DepartmentIds)
                            {
                                SalaryStructureDepartmentMapping objMapping = null;
                                objMapping = new SalaryStructureDepartmentMapping {
                                    SalaryStructureId = objSalaryStructure.Id, DepartmentId = DepartmentId
                                };
                                _context.Add(objMapping);
                            }
                            _context.SaveChanges();
                        }

                        if (model.PayrollCategoryIds.Count > 0)
                        {
                            foreach (int PayrollCategoryId in model.PayrollCategoryIds)
                            {
                                SalaryStructurePayrollCategoryMapping objMapping = null;
                                objMapping = new SalaryStructurePayrollCategoryMapping {
                                    SalaryStructureId = objSalaryStructure.Id, PayrollCategoryId = PayrollCategoryId
                                };
                                _context.Add(objMapping);
                            }
                            _context.SaveChanges();
                        }

                        _ctxTransaction.Commit();
                        message   = "Saved Successfully";
                        errorcode = "0";
                    }
                    catch (Exception e)
                    {
                        _ctxTransaction.Rollback();
                        e.ToString();
                        message   = "Saved Error";
                        errorcode = "1";
                        excp      = e.ToString();
                    }

                    result = new
                    {
                        error = errorcode,
                        msg   = message,
                        excp  = excp
                    };
                }
            }
            return(result);
        }
        public object CreateUpdateSalaryStructure(int id, [FromBody] SalaryStructureVM model)
        {
            object result    = null;
            string message   = "";
            string errorcode = "";
            string excp      = "";

            if (model == null)
            {
                return(BadRequest());
            }
            using (_context)
            {
                using (var _ctxTransaction = _context.Database.BeginTransaction())
                {
                    try
                    {
                        if (id != 0)
                        {
                            var entityUpdate = _context.SalaryStructure.FirstOrDefault(x => x.Id == id);

                            if (entityUpdate != null)
                            {
                                entityUpdate.Name        = model.Name;
                                entityUpdate.Description = model.Description;
                                entityUpdate.IsActive    = model.IsActive;

                                _context.SaveChanges();
                            }

                            List <SalaryStructureDepartmentMapping> RemoveDepartmentList = _context.SalaryStructureDepartmentMapping.Where(y => y.SalaryStructureId == id).ToList();
                            if (RemoveDepartmentList.Count > 0)
                            {
                                _context.SalaryStructureDepartmentMapping.RemoveRange(RemoveDepartmentList);
                            }
                            if (model.DepartmentIds.Count > 0)
                            {
                                foreach (int DepartmentId in model.DepartmentIds)
                                {
                                    SalaryStructureDepartmentMapping objMapping = null;
                                    objMapping = new SalaryStructureDepartmentMapping {
                                        SalaryStructureId = id, DepartmentId = DepartmentId
                                    };
                                    _context.Add(objMapping);
                                }
                                _context.SaveChanges();
                            }
                            List <SalaryStructurePayrollCategoryMapping> RemovePayrollCategoryList = _context.SalaryStructurePayrollCategoryMapping.Where(y => y.SalaryStructureId == id).ToList();
                            if (RemovePayrollCategoryList.Count > 0)
                            {
                                _context.SalaryStructurePayrollCategoryMapping.RemoveRange(RemovePayrollCategoryList);
                            }
                            if (model.PayrollCategoryIds.Count > 0)
                            {
                                foreach (int PayrollCategoryId in model.PayrollCategoryIds)
                                {
                                    SalaryStructurePayrollCategoryMapping objMapping = null;
                                    objMapping = new SalaryStructurePayrollCategoryMapping {
                                        SalaryStructureId = id, PayrollCategoryId = PayrollCategoryId
                                    };
                                    _context.Add(objMapping);
                                }
                                _context.SaveChanges();
                            }

                            _ctxTransaction.Commit();
                            message   = "Entry Updated";
                            errorcode = "0";
                        }
                        else
                        {
                            SalaryStructure objSalaryStructure = new SalaryStructure();
                            objSalaryStructure.Name        = model.Name;
                            objSalaryStructure.Description = model.Description;
                            objSalaryStructure.IsActive    = model.IsActive;
                            _context.SalaryStructure.Add(objSalaryStructure);
                            //await _ctx.SaveChangesAsync();
                            _context.SaveChanges();

                            if (model.DepartmentIds.Count > 0)
                            {
                                foreach (int DepartmentId in model.DepartmentIds)
                                {
                                    SalaryStructureDepartmentMapping objMapping = null;
                                    objMapping = new SalaryStructureDepartmentMapping {
                                        SalaryStructureId = objSalaryStructure.Id, DepartmentId = DepartmentId
                                    };
                                    _context.Add(objMapping);
                                }
                                _context.SaveChanges();
                            }

                            if (model.PayrollCategoryIds.Count > 0)
                            {
                                foreach (int PayrollCategoryId in model.PayrollCategoryIds)
                                {
                                    SalaryStructurePayrollCategoryMapping objMapping = null;
                                    objMapping = new SalaryStructurePayrollCategoryMapping {
                                        SalaryStructureId = objSalaryStructure.Id, PayrollCategoryId = PayrollCategoryId
                                    };
                                    _context.Add(objMapping);
                                }
                                _context.SaveChanges();
                            }

                            _ctxTransaction.Commit();
                            message   = "Saved Successfully";
                            errorcode = "0";
                        }
                    }
                    catch (Exception e)
                    {
                        _ctxTransaction.Rollback();
                        e.ToString();
                        message   = "Saved Error";
                        errorcode = "1";
                        excp      = e.ToString();
                    }

                    result = new
                    {
                        error = errorcode,
                        msg   = message,
                        excp  = excp
                    };
                }
            }
            return(result);
        }
예제 #3
0
        public object GetEmployeeSalaryDetailsList()
        {
            object result = null;
            List <EmployeeSalaryDetailsListVM> list1 = new List <EmployeeSalaryDetailsListVM>();
            List <EmployeeSalaryDetailsListVM> list  = new List <EmployeeSalaryDetailsListVM>();

            try
            {
                using (_context)
                {
                    var abc = _context.EmployeePayrollSalaryDetail.Where(y => y.EmployeeId == 1).FirstOrDefault();
                    list1 = _context.EmployeeDetails.Select(x => new
                    {
                        Id             = x.EmployeeId,
                        EmployeeId     = x.EmployeeId,
                        EmployeeCode   = x.F1,
                        FullName       = x.F2 + " " + x.F3,
                        JoiningDate    = x.F17,
                        Department     = x.F12,
                        DepartmentName = x.F12 != null ? _context.LookupData.Where(z => z.RowId == int.Parse(x.F12) && z.FieldName == "DepartmentName").FirstOrDefault().Value : null,
                        Position       = x.F19,
                        //Ctc = _context.EmployeePayrollSalaryDetail.Where(y => y.EmployeeId == x.EmployeeId).FirstOrDefault() != null ? _context.EmployeePayrollSalaryDetail.Where(y => y.EmployeeId == x.EmployeeId).FirstOrDefault().GrossSalary : null
                        //Ctc = _context.EmployeePayrollSalaryDetail.Where(y => y.EmployeeId == x.EmployeeId).FirstOrDefault() != null ? _context.EmployeePayrollSalaryDetail.Where(y => y.EmployeeId == x.EmployeeId).FirstOrDefault().GrossSalary : null,
                        //Ctc = _context.EmployeePayrollCategory.Where(q => _context.SalaryStructurePayrollCategoryMapping.Where(z => z.SalaryStructureId == _context.SalaryStructureDepartmentMapping.Where(y => y.DepartmentId == int.Parse(x.F12)).FirstOrDefault().SalaryStructureId).Select(p => p.PayrollCategoryId).ToList().Contains(q.Id)).ToList().Sum(r => r.Amount),
                        //ProfessionalTax
                    }).OrderBy(x => x.Id).ToList().Select(x => new EmployeeSalaryDetailsListVM()
                    {
                        Id             = x.Id,
                        EmployeeId     = x.EmployeeId,
                        EmployeeCode   = x.EmployeeCode,
                        FullName       = x.FullName,
                        JoiningDate    = x.JoiningDate,
                        Department     = x.Department,
                        DepartmentName = x.DepartmentName,
                        Position       = x.Position,
                        //Ctc = x.Ctc,
                        //ProfessionalTax
                    }).OrderBy(x => x.Id).ToList();

                    foreach (var SalaryDetail in list1)
                    {
                        Decimal?CTCAmount = _context.EmployeePayrollSalaryDetail.Where(y => y.EmployeeId == SalaryDetail.EmployeeId).FirstOrDefault() != null?_context.EmployeePayrollSalaryDetail.Where(y => y.EmployeeId == SalaryDetail.EmployeeId).FirstOrDefault().GrossSalary : null;

                        if (CTCAmount != null)
                        {
                            SalaryDetail.Ctc = CTCAmount;
                        }
                        else
                        {
                            if (SalaryDetail.Department != null && SalaryDetail.Department != "")
                            {
                                var SalaryStructureId = _context.SalaryStructureDepartmentMapping.Where(y => y.DepartmentId == int.Parse(SalaryDetail.Department)).FirstOrDefault() == null ? null : _context.SalaryStructureDepartmentMapping.Where(y => y.DepartmentId == int.Parse(SalaryDetail.Department)).FirstOrDefault().SalaryStructureId;
                                if (SalaryStructureId != null)
                                {
                                    List <int?> PayrollCategoryIds = _context.SalaryStructurePayrollCategoryMapping.Where(z => z.SalaryStructureId == SalaryStructureId).Select(p => p.PayrollCategoryId).ToList();
                                    var         TotalAmount        = _context.EmployeePayrollCategory.Where(q => PayrollCategoryIds.Contains(q.Id)).ToList().Sum(r => r.Amount);
                                    SalaryDetail.Ctc = TotalAmount;
                                }
                            }
                        }
                    }

                    foreach (var SalaryDetail in list1)
                    {
                        if (SalaryDetail.Department != null && SalaryDetail.Department != "")
                        {
                            SalaryStructureDepartmentMapping SalaryStructureDMapp = _context.SalaryStructureDepartmentMapping.Where(y => y.DepartmentId == int.Parse(SalaryDetail.Department)).FirstOrDefault();
                            if (SalaryStructureDMapp != null)
                            {
                                list.Add(SalaryDetail);
                            }
                        }
                    }

                    result = new
                    {
                        list,
                        error = "0",
                        msg   = "Success"
                    };
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
                result = new
                {
                    list,
                    error = "1",
                    msg   = ex.ToString()
                };
            }
            return(result);
        }