I have a viewcontroller code below and it computes basic payroll computation, owever it is only displayed on a window. How do I persist the results to a table in sql server? Hope you can help me on this. Thanks! namespace Piask.Module.BusinessObjects.PayrollCalculation { using System; using System.Collections.Generic; using System.ComponentModel; using DevExpress.ExpressApp; using DevExpress.ExpressApp.DC; using DevExpress.Persistent.Base; [DomainComponent]
public class PayrollResultView : INotifyPropertyChanged { public PayrollResultView(DateTime startDate, DateTime endDate, List employees) { this.StartDate = startDate; this.EndDate = endDate; this.Employees = employees; this.CalculateResults(); } [Browsable(false)]
public int Id; private DateTime _StartDate; private DateTime _EndDate; [ImmediatePostData]
public DateTime StartDate { get { return _StartDate; } set { _StartDate = value; CalculateResults(); OnPropertyChanged("StartDate"); } } [ImmediatePostData]
public DateTime EndDate { get { return _EndDate; } set { _EndDate = value; CalculateResults(); OnPropertyChanged("EndDate"); } } [Browsable(false)]
public List Employees { get; set; } private IList _Results; public IList Results { get { return this._Results ?? (this._Results = new BindingList()); } } private void CalculateResults() { if (StartDate >= EndDate) return; this.Results.Clear(); if (this.StartDate != DateTime.MinValue && this.EndDate != DateTime.MinValue && this.EndDate > this.StartDate && this.Employees != null) foreach (var employee in this.Employees) this.Results.Add(new PayrollResult(this.StartDate.Date, this.EndDate.Date, employee)); OnPropertyChanged("Results"); } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { if (PropertyChanged == null) return; PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } and the Payresult controller // -------------------------------------------------------------------------------------------------------------------- // // // // -------------------------------------------------------------------------------------------------------------------- namespace Piask.Module.BusinessObjects.PayrollCalculation { using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using DevExpress.ExpressApp.DC; using DevExpress.ExpressApp.Model; /// /// The payroll result. /// [DomainComponent]
public class PayrollResult { /// /// Initializes a new instance of the class. /// /// /// The start date. /// /// /// The end date. /// /// /// The employee. /// public PayrollResult(DateTime startDate, DateTime endDate, Employee employee) { this.DatesRange = new DatesRange(startDate, endDate); this.Employee = employee; } /// /// The id. /// [Browsable(false)]
public int Id; /// /// Gets or sets the employee. /// [ModelDefault("AllowEdit", "False")]
public Employee Employee { get; set; } /// /// Gets or sets the dates range. /// [ModelDefault("AllowEdit", "False")]
public DatesRange DatesRange { get; set; } /// /// The base hours details. /// private List baseHoursDetails; /// /// Gets the base hours details. /// public List BaseHoursDetails { get { if (baseHoursDetails == null) { baseHoursDetails = new List(); foreach (var schedule in this.Employee.Schedules) { // Get or create hours detail var classTypeHours = baseHoursDetails.FirstOrDefault(x => x.ClassType == schedule.ClassType); if (classTypeHours == null) { classTypeHours = new ClassTypeHours(schedule, schedule.ClassType); baseHoursDetails.Add(classTypeHours); } classTypeHours.Hours += schedule.GetHours(this.DatesRange); } } return baseHoursDetails; } } /// /// The absent hours details. /// private List absentHoursDetails; /// /// Gets the absent hours details. /// public List AbsentHoursDetails { get { if (absentHoursDetails == null) absentHoursDetails = Employee.GetAbsentHours(DatesRange); return absentHoursDetails; } } /// /// The over time hours details. /// private List overTimeHoursDetails; /// /// Gets the over time hours details. /// public List OverTimeHoursDetails { get { if (overTimeHoursDetails == null) { overTimeHoursDetails = new List(); foreach (var overtime in Employee.Overtimes.Where(x => x.StartDate >= this.DatesRange.StartDate && x.EndDate <= this.DatesRange.EndDate)) { overTimeHoursDetails.Add( new ClassTypeHours(overtime, overtime.ClassType) { Hours = overtime.EndDate.Subtract(overtime.StartDate).TotalHours }); } } return overTimeHoursDetails; } } /// /// Gets the work hours. /// public double WorkHours { get { double workHours = 0; foreach (var hoursDetail in BaseHoursDetails) workHours += hoursDetail.Hours; return workHours; } } /// /// Gets the absent hours. /// public double AbsentHours { get { double absentHours = 0; foreach (var absent in AbsentHoursDetails) absentHours += absent.Hours; return absentHours; } } /// /// Gets the overtime hours. /// public double OvertimeHours { get { double result = 0; foreach (var overtime in OverTimeHoursDetails) result += overtime.Hours; return result; } } /// /// Gets the total hours. /// public double TotalHours { get { return this.WorkHours + this.OvertimeHours - this.AbsentHours; } } /// /// Gets the total basic. /// public decimal TotalBasic { get { decimal totalBasic = 0; foreach (var baseHoursDetail in this.BaseHoursDetails) { // Add base hours totalBasic += baseHoursDetail.TotalBasic; // Subtract absent hours by schedule foreach (var absent in AbsentHoursDetails.Where(x => x.Payable == baseHoursDetail.Payable)) totalBasic -= absent.TotalBasic; } return totalBasic; } } /// /// Gets the total allowance. /// public decimal TotalAllowance { get { decimal totalAllowance = 0; foreach (var baseHoursDetail in this.BaseHoursDetails) { // Add base hours totalAllowance += baseHoursDetail.TotalAllowance; // Subtract absent hours by schedule foreach (var absent in AbsentHoursDetails.Where(x => x.Payable == baseHoursDetail.Payable)) totalAllowance -= absent.TotalAllowance; } return totalAllowance; } } /// /// Gets the total amount. /// public decimal TotalAmount { get { return this.TotalBasic + this.TotalAllowance; } } public decimal TotalOvertime { get { decimal result = 0; foreach (var overtime in OverTimeHoursDetails) result += overtime.TotalBasic + overtime.TotalAllowance; return result; } } public decimal TotalEarnings { get { decimal result = 0; foreach (var earning in Employee.GetEarnings(this.DatesRange)) result += earning.Total; return result; } } public decimal TotalDeductions { get { decimal result = 0; foreach (var earning in Employee.GetDeductions(this.DatesRange)) result += earning.Total; return result; } } public decimal NetPay { get { return TotalAmount + TotalOvertime + TotalEarnings - TotalDeductions; } } } } |