public void Remove(SalaryVar salaryVar)
 {
     if (Context.Entry(salaryVar).State == EntityState.Detached)
     {
         context.SalaryVar.Attach(salaryVar);
     }
     context.SalaryVar.Remove(salaryVar);
 }
        public ActionResult SaveSalaryVar(SalaryVarFormViewModel model, OptionsViewModel moreInfo, SalaryEmpVarGridViewModel grid1)
        {
            List <Error> errors          = new List <Error>();
            var          SalaryVarRecord = _hrUnitOfWork.PayrollRepository.GetSalVar(model.Reference);

            if (ModelState.IsValid)
            {
                if (ServerValidationEnabled)
                {
                    errors = _hrUnitOfWork.PositionRepository.CheckForm(new CheckParm
                    {
                        CompanyId    = CompanyId,
                        ObjectName   = "SalaryVar",
                        TableName    = "SalaryVars",
                        Columns      = Models.Utils.GetColumnViews(ModelState.Where(a => !a.Key.Contains('.'))),
                        ParentColumn = "PayPeriodId",
                        Culture      = Language
                    });

                    if (errors.Count() > 0)
                    {
                        foreach (var e in errors)
                        {
                            foreach (var errorMsg in e.errors)
                            {
                                ModelState.AddModelError(errorMsg.field, errorMsg.message);
                            }
                        }

                        return(Json(Models.Utils.ParseFormErrors(ModelState)));
                    }
                }
            }
            else
            {
                return(Json(Models.Utils.ParseFormErrors(ModelState)));
            }
            var multi = _hrUnitOfWork.Repository <SalaryItem>().Where(a => a.Id == model.SalItemId).Select(a => a.Multiple).FirstOrDefault();
            List <SalaryVar> ListOfSalaryVar = new List <SalaryVar>();

            if (SalaryVarRecord == null) // New
            {
                if (grid1.inserted != null)
                {
                    Guid newGUID = Guid.NewGuid();
                    foreach (var item in grid1.inserted)
                    {
                        SalaryVarRecord             = new SalaryVar();
                        SalaryVarRecord.Approvedby  = UserName;
                        SalaryVarRecord.Curr        = model.Curr;
                        SalaryVarRecord.PayPeriodId = model.PayPeriodId;
                        SalaryVarRecord.PayrollId   = model.PayrollId;
                        SalaryVarRecord.SalItemId   = model.SalItemId;
                        SalaryVarRecord.CreatedTime = DateTime.Now;
                        SalaryVarRecord.CreatedUser = UserName;
                        SalaryVarRecord.Reference   = newGUID;
                        SalaryVarRecord.EmpId       = item.EmpId;
                        SalaryVarRecord.Amount      = item.Amount;
                        SalaryVarRecord.Status      = item.Status;
                        ListOfSalaryVar.Add(SalaryVarRecord);
                    }
                    _hrUnitOfWork.PayrollRepository.AddRange(ListOfSalaryVar);

                    if (multi == false)
                    {
                        var empIds = ListOfSalaryVar.Select(a => a.EmpId).ToList();
                        if (empIds.Count != empIds.Distinct().Count())
                        {
                            ModelState.AddModelError("", MsgUtils.Instance.Trls("CantAcceptMultiInPeriod"));
                            return(Json(Models.Utils.ParseFormErrors(ModelState)));
                        }
                    }
                }
                else
                {
                    SalaryVarRecord = new SalaryVar();

                    AutoMapper(new Models.AutoMapperParm
                    {
                        Destination = SalaryVarRecord,
                        Source      = model,
                        ObjectName  = "SalaryVar",
                        Options     = moreInfo
                    });
                    _hrUnitOfWork.PayrollRepository.Add(SalaryVarRecord);
                }
            }
            else // Edit
            {
                //if form is updated only
                if (grid1.inserted == null && grid1.updated == null)
                {
                    var listOfSalaryVar = _hrUnitOfWork.Repository <SalaryVar>().Where(a => a.Reference == model.Reference).ToList();
                    foreach (var salary in listOfSalaryVar)
                    {
                        AutoMapper(new Models.AutoMapperParm
                        {
                            Destination = salary,
                            Source      = model,
                            ObjectName  = "SalaryVar",
                            Options     = moreInfo,
                            Transtype   = TransType.Update
                        });
                        SalaryVarRecord.ModifiedTime = DateTime.Now;
                        SalaryVarRecord.ModifiedUser = UserName;
                        _hrUnitOfWork.PayrollRepository.Attach(SalaryVarRecord);
                        _hrUnitOfWork.PayrollRepository.Entry(SalaryVarRecord).State = EntityState.Modified;
                    }
                }
                // if insert in grid
                if (grid1.inserted != null)
                {
                    foreach (var item in grid1.inserted)
                    {
                        SalaryVarRecord             = new SalaryVar();
                        SalaryVarRecord.Approvedby  = UserName;
                        SalaryVarRecord.Curr        = model.Curr;
                        SalaryVarRecord.PayPeriodId = model.PayPeriodId;
                        SalaryVarRecord.PayrollId   = model.PayrollId;
                        SalaryVarRecord.SalItemId   = model.SalItemId;
                        SalaryVarRecord.CreatedTime = DateTime.Now;
                        SalaryVarRecord.CreatedUser = UserName;
                        SalaryVarRecord.Reference   = model.Reference;
                        SalaryVarRecord.EmpId       = item.EmpId;
                        SalaryVarRecord.Amount      = item.Amount;
                        SalaryVarRecord.Status      = item.Status;
                        ListOfSalaryVar.Add(SalaryVarRecord);
                    }
                    _hrUnitOfWork.PayrollRepository.AddRange(ListOfSalaryVar);
                    if (multi == false)
                    {
                        var        dbEmpIds = _hrUnitOfWork.Repository <SalaryVar>().Where(a => a.Reference == model.Reference).Select(s => s.EmpId).ToList();
                        var        empIds   = ListOfSalaryVar.Select(a => a.EmpId).ToList();
                        List <int> chkList  = new List <int>();
                        chkList.AddRange(dbEmpIds);
                        chkList.AddRange(empIds);
                        if (chkList.Count != chkList.Distinct().Count())
                        {
                            ModelState.AddModelError("", MsgUtils.Instance.Trls("CantAcceptMultiInPeriod"));
                            return(Json(Models.Utils.ParseFormErrors(ModelState)));
                        }
                    }
                }
                //if update in grid and form
                if (grid1.updated != null)
                {
                    foreach (var item in grid1.updated)
                    {
                        SalaryVarRecord              = _hrUnitOfWork.Repository <SalaryVar>().Where(a => a.Id == item.Id).FirstOrDefault();
                        SalaryVarRecord.Approvedby   = UserName;
                        SalaryVarRecord.Curr         = model.Curr;
                        SalaryVarRecord.PayPeriodId  = model.PayPeriodId;
                        SalaryVarRecord.PayrollId    = model.PayrollId;
                        SalaryVarRecord.SalItemId    = model.SalItemId;
                        SalaryVarRecord.ModifiedTime = DateTime.Now;
                        SalaryVarRecord.ModifiedUser = UserName;
                        SalaryVarRecord.Reference    = model.Reference;
                        SalaryVarRecord.EmpId        = item.EmpId;
                        SalaryVarRecord.Amount       = item.Amount;
                        SalaryVarRecord.Status       = item.Status;
                        _hrUnitOfWork.PayrollRepository.Attach(SalaryVarRecord);
                        _hrUnitOfWork.PayrollRepository.Entry(SalaryVarRecord).State = EntityState.Modified;
                    }
                }
                if (grid1.deleted != null)
                {
                    foreach (var item in grid1.deleted)
                    {
                        SalaryVarRecord        = _hrUnitOfWork.Repository <SalaryVar>().Where(a => a.Id == item.Id).FirstOrDefault();
                        SalaryVarRecord.Status = 2;
                        _hrUnitOfWork.PayrollRepository.Attach(SalaryVarRecord);
                        _hrUnitOfWork.PayrollRepository.Entry(SalaryVarRecord).State = EntityState.Modified;
                    }
                }
                if (model.submit == true)
                {
                    var listOfSalary = _hrUnitOfWork.Repository <SalaryVar>().Where(a => a.Reference == model.Reference && a.Status == 0).ToList();
                    if (listOfSalary.Count() > 0)
                    {
                        for (int i = 0; i < listOfSalary.Count; i++)
                        {
                            listOfSalary[i].Status = 1;
                            _hrUnitOfWork.PayrollRepository.Attach(listOfSalary[i]);
                            _hrUnitOfWork.PayrollRepository.Entry(listOfSalary[i]).State = EntityState.Modified;
                        }
                    }
                }
            }
            if (errors.Count > 0)
            {
                return(Json(errors.First().errors.First().message));
            }
            try
            {
                _hrUnitOfWork.Save();
            }
            catch (Exception ex)
            {
                var msg = _hrUnitOfWork.HandleDbExceptions(ex, Language);
                if (msg.Length > 0)
                {
                    return(Json(msg));
                }
            }

            return(Json("OK," + ((new JavaScriptSerializer()).Serialize(SalaryVarRecord))));
        }
 public void Attach(SalaryVar salaryvar)
 {
     context.SalaryVar.Attach(salaryvar);
 }
 public DbEntityEntry <SalaryVar> Entry(SalaryVar salaryVar)
 {
     return(Context.Entry(salaryVar));
 }
 public void Add(SalaryVar salaryvar)
 {
     context.SalaryVar.Add(salaryvar);
 }