async public Task <IHttpActionResult> Get(int Id) { var LastToDate = Db.BillingMonthly.ToList().LastOrDefault(e => e.ProjectId == Id); if (LastToDate.To.Date <= DateTime.Now.Date) { LastToDate.Editable = false; var projectAmountToPay = (await Db.Projects.SingleOrDefaultAsync(p => p.Id == Id)).BillingAmount; var billingMonthly = new BillingMonthly() { From = LastToDate.To.Date, To = LastToDate.To.Date.AddMonths(1), ProjectId = Id, Arrears = LastToDate.RemainingArrears, MonthlyAmount = projectAmountToPay, AmountPaid = 0, Editable = true, TotalAmountToPay = LastToDate.RemainingArrears + projectAmountToPay, RemainingArrears = LastToDate.RemainingArrears + projectAmountToPay - 0 }; var comissionEmployee = (await Db.ProjectComission.FirstOrDefaultAsync(p => p.ProjectId == Id)).EmployeeId; var comissionMonthly = new ProjectComission() { EmployeeId = comissionEmployee, Date = LastToDate.To.Date, ComissionAmount = 0, ProjectId = Id }; Db.BillingMonthly.Add(billingMonthly); Db.ProjectComission.Add(comissionMonthly); await Db.SaveChangesAsync(); } var monthlyHistory = await Db.BillingMonthly.Where(i => i.ProjectId == Id).Include(p => p.Project).ToListAsync(); return(Ok(monthlyHistory)); }
async public Task <IHttpActionResult> New(ProjectRegisterPostVM projectPost) { Db.Projects.Add(projectPost.Project); Db.ProjectComission.Add(projectPost.ProjectComission); var emp = await Db.Employees.SingleOrDefaultAsync(e => e.Id == projectPost.ProjectComission.EmployeeId); if (projectPost.Project.ProjectBillingTypeId == 1) { var billOneTime = new BillingOneTime() { OneTimeAmount = projectPost.Project.BillingAmount, AmountPaid = projectPost.Project.Arrears, Tax = projectPost.Tax, RemainingArrears = projectPost.Project.BillingAmount - projectPost.Project.Arrears - projectPost.Tax }; var billHistory = new OneTimeHistoryDetails() { BillingOneTimeId = billOneTime.Id, AmountAdded = projectPost.Project.Arrears, TaxAdded = Convert.ToInt32(projectPost.Tax), Date = projectPost.Project.StartingDate }; Db.BillingOneTime.Add(billOneTime); Db.OneTimeHistoryDetails.Add(billHistory); } else if (projectPost.Project.ProjectBillingTypeId == 2) { var billMonthly = new BillingMonthly() { From = projectPost.Project.StartingDate, To = projectPost.Project.StartingDate.AddMonths(1), MonthlyAmount = projectPost.Project.BillingAmount, Arrears = 0, Tax = projectPost.Tax, Editable = true, TotalAmountToPay = projectPost.Project.BillingAmount + 0, AmountPaid = projectPost.Project.Arrears, RemainingArrears = projectPost.Project.BillingAmount + 0 - projectPost.Project.Arrears - projectPost.Tax }; var billHistory = new MonthlyHistoryDetails() { BillingMonthlyId = billMonthly.Id, AmountAdded = projectPost.Project.Arrears, TaxAdded = Convert.ToInt32(projectPost.Tax), Date = projectPost.Project.StartingDate }; Db.BillingMonthly.Add(billMonthly); Db.MonthlyHistoryDetails.Add(billHistory); } else if (projectPost.Project.ProjectBillingTypeId == 3) { var billQuaterly = new BillingQuaterly() { From = projectPost.Project.StartingDate, To = projectPost.Project.StartingDate.AddMonths(3), QuaterlyAmount = projectPost.Project.BillingAmount, Arrears = 0, Tax = projectPost.Tax, Editable = true, TotalAmountToPay = projectPost.Project.BillingAmount + 0, AmountPaid = projectPost.Project.Arrears, RemainingArrears = projectPost.Project.BillingAmount + 0 - projectPost.Project.Arrears - projectPost.Tax }; var billHistory = new QuaterlyHistoryDetails() { BillingQuaterlyId = billQuaterly.Id, AmountAdded = projectPost.Project.Arrears, TaxAdded = Convert.ToInt32(projectPost.Tax), Date = projectPost.Project.StartingDate }; Db.BillingQuaterly.Add(billQuaterly); Db.QuaterlyHistoryDetails.Add(billHistory); } else if (projectPost.Project.ProjectBillingTypeId == 4) { var billyearly = new BillingYearly() { From = projectPost.Project.StartingDate, To = projectPost.Project.StartingDate.AddYears(1), YearlyAmount = projectPost.Project.BillingAmount, Arrears = 0, Tax = projectPost.Tax, Editable = true, TotalAmountToPay = projectPost.Project.BillingAmount + 0, AmountPaid = projectPost.Project.Arrears, RemainingArrears = projectPost.Project.BillingAmount + 0 - projectPost.Project.Arrears - projectPost.Tax }; var billHistory = new YearlyHistoryDetails() { BillingYearlyId = billyearly.Id, AmountAdded = projectPost.Project.Arrears, TaxAdded = Convert.ToInt32(projectPost.Tax), Date = projectPost.Project.StartingDate }; Db.BillingYearly.Add(billyearly); Db.YearlyHistoryDetails.Add(billHistory); } await Db.SaveChangesAsync(); var IdData = (await Db.Projects.SingleOrDefaultAsync(p => p.Name == projectPost.Project.Name && p.ClientId == projectPost.Project.ClientId)).Id; return(Ok(IdData)); }