public IActionResult CreateOrUpdatePayrollNoneTax(string month, [FromBody] PayrollMonthNoneTax monthTax) { try { payrollService.CreatePayroll(monthTax, payrollService.OrganisationId, month); return(Ok(monthTax)); } catch (Exception ex) { return(BadRequest(new Error(ex.Message))); } }
public void CreatePayroll(PayrollMonthNoneTax payrollNoneTax, string organisationId, string month) { using var context = Context(); if (!context.Organisation.FirstOrDefault(u => u.Id == organisationId).DeclareTax) { throw new Exception("This organisation is declare tax"); } var payroll = context.PayrollMonth.FirstOrDefault(u => u.OrganisationId == organisationId && u.Month == month); var date = CurrentCambodiaTime; if (date.Year.ToString() + date.Month.ToString("00") != month) { date = new DateTime(int.Parse(month.Substring(0, 4)), int.Parse(month.Substring(4, 2)), 1, 20, 0, 0, 0); } if (payroll == null) { payroll = new Dal.Models.PayrollMonth { Month = month, EndDate = CurrentCambodiaTime, StartDate = CurrentCambodiaTime, ExchangeRate = 1, OrganisationId = organisationId, Total = payrollNoneTax.Payrolls.Sum(u => u.Salary + (u.OtherBenefit == null ? 0 : u.OtherBenefit.Value)) }; payroll.PayrollEmployee = payrollNoneTax.Payrolls.Select(u => new Dal.Models.PayrollEmployee { EmployeeId = u.EmployeeId, Date = date, Total = u.Salary + (u.OtherBenefit == null ? 0 : u.OtherBenefit.Value), TransactionDate = CurrentCambodiaTime, PayrollEmployeeNoneTax = new Dal.Models.PayrollEmployeeNoneTax { Salary = u.Salary, OtherBenefit = u.OtherBenefit } }).ToHashSet(); context.PayrollMonth.Add(payroll); context.SaveChanges(); payrollNoneTax.Id = payroll.Id; payrollNoneTax.Payrolls = context.PayrollEmployeeNoneTax.Where(u => u.IdNavigation.PayrollMonthId == payrollNoneTax.Id).Select(u => new PayrollNoneTax { Date = u.IdNavigation.Date, EmployeeId = u.IdNavigation.EmployeeId, Employee = new Employee { Id = u.IdNavigation.EmployeeId, DisplayName = u.IdNavigation.Employee.DisplayName, JobTitle = u.IdNavigation.Employee.JobTitle, Country = new Country { Id = u.IdNavigation.Employee.CountryId, Alpha2Code = u.IdNavigation.Employee.Country.Alpha2Code, Alpha3Code = u.IdNavigation.Employee.Country.Alpha3Code, Name = u.IdNavigation.Employee.Country.Name, Nationality = u.IdNavigation.Employee.Country.Nationality } }, OtherBenefit = u.OtherBenefit, Id = u.Id, Salary = u.Salary, Total = u.IdNavigation.Total, TransactionDate = u.IdNavigation.TransactionDate }).ToList(); return; } payroll.EndDate = CurrentCambodiaTime; payroll.Total = payrollNoneTax.Payrolls.Sum(u => u.Salary + (u.OtherBenefit == null ? 0 : u.OtherBenefit.Value)); foreach (var p in payrollNoneTax.Payrolls) { var updatePayroll = context.PayrollEmployee.FirstOrDefault(u => u.Id == p.Id); updatePayroll.Total = p.Salary + (p.OtherBenefit == null ? 0 : p.OtherBenefit.Value); updatePayroll.PayrollEmployeeNoneTax.OtherBenefit = p.OtherBenefit; updatePayroll.PayrollEmployeeNoneTax.Salary = p.Salary; } context.SaveChanges(); }
public PayrollMonthNoneTax GetPayrollMonth(string organisationId, string month) { using var context = Context(); var organisation = context.Organisation.FirstOrDefault(u => u.Id == organisationId); if (!context.PayrollMonth.Any(u => u.Month == month && u.OrganisationId == organisationId)) { var payroll = new PayrollMonthNoneTax { Month = month, OrganisationId = organisationId, Currency = new Currency { Id = organisation.OrganisationBaseCurrency.BaseCurrencyId, Code = organisation.OrganisationBaseCurrency.BaseCurrency.Code, Name = organisation.OrganisationBaseCurrency.BaseCurrency.Name, Symbole = organisation.OrganisationBaseCurrency.BaseCurrency.Symbole }, }; var employees = (from u in context.Employee.Where(u => u.IsActive && u.OrganisationId == organisationId).ToList() let numberOfChildAndSpouse = (u.IsConfederationThatHosts ? 1 : 0) + u.NumberOfChild select new PayrollNoneTax { Employee = new Employee { Id = u.Id, DisplayName = u.DisplayName, JobTitle = u.JobTitle, Country = new Country { Id = u.CountryId, Alpha2Code = u.Country.Alpha2Code, Alpha3Code = u.Country.Alpha3Code, Name = u.Country.Name, Nationality = u.Country.Nationality }, IsConfederationThatHosts = u.IsConfederationThatHosts, NumberOfChild = u.NumberOfChild, IsResidentEmployee = u.IsResidentEmployee }, EmployeeId = u.Id, OtherBenefit = null, Date = DateTime.Now, TransactionDate = DateTime.Now, Salary = u.Salary, }).ToList(); payroll.Payrolls = employees; return(payroll); } var payrollMonth = context.PayrollMonth.FirstOrDefault(u => u.Month == month && u.OrganisationId == organisationId); var result = new PayrollMonthNoneTax { Month = month, OrganisationId = organisationId, Id = payrollMonth.Id, EndDate = payrollMonth.EndDate, StartDate = payrollMonth.StartDate, Total = payrollMonth.Total, Currency = new Currency { Id = organisation.OrganisationBaseCurrency.BaseCurrencyId, Code = organisation.OrganisationBaseCurrency.BaseCurrency.Code, Name = organisation.OrganisationBaseCurrency.BaseCurrency.Name, Symbole = organisation.OrganisationBaseCurrency.BaseCurrency.Symbole }, ExchangeRate = (decimal)payrollMonth.ExchangeRate, Payrolls = payrollMonth.PayrollEmployee.Select(u => new PayrollNoneTax { Date = u.Date, Employee = new Employee { Id = u.Id, DisplayName = u.Employee.DisplayName, JobTitle = u.Employee.JobTitle, Country = new Country { Id = u.Employee.CountryId, Alpha2Code = u.Employee.Country.Alpha2Code, Alpha3Code = u.Employee.Country.Alpha3Code, Name = u.Employee.Country.Name, Nationality = u.Employee.Country.Nationality }, }, EmployeeId = u.EmployeeId, OtherBenefit = u.PayrollEmployeeNoneTax.OtherBenefit, Salary = u.PayrollEmployeeNoneTax.Salary, TransactionDate = u.TransactionDate, Total = u.Total, Id = u.Id }).ToList() }; return(result); }