using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Linq.Expressions; using Bowin.Common.Linq; using Bowin.Common.Linq.Entity; using EMIS.ViewModel.PaymentManage; using EMIS.ViewModel; using EMIS.DataLogic.PaymentManage; using EMIS.Entities; using EMIS.Utility; namespace EMIS.CommonLogic.PaymentManage { public class PaymentServices : BaseServices, IPaymentServices { public PaymentDAL PaymentDAL { get; set; } public TeacherTypePaymentFormulaDAL TeacherTypePaymentFormulaDAL { get; set; } public IGridResultSet GetPaymentDetailViewList(ConfiguretView paymentConditionView, Guid? schoolyearID, Guid? collegeID, int? titleID, int pageIndex, int pageSize) { Expression> staffExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> schoolyearExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> coursematerialExp = (x => true); Expression> missionExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { schoolyearExp = schoolyearExp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { missionExp = missionExp.And(x => x.CollegeID == collegeID); } if (titleID.HasValue) { staffExp = staffExp.And(x => x.TitleID == titleID); } var query = PaymentDAL.GetPaymentDetailViewQueryable(staffExp, schoolyearExp, missionExp, coursematerialExp); if (!string.IsNullOrEmpty(paymentConditionView.ConditionValue) && !string.IsNullOrEmpty(paymentConditionView.Attribute)) query = query.DynamicWhere(paymentConditionView.Attribute, paymentConditionView.Condition, paymentConditionView.ConditionValue); query = query.OrderBy(x => x.CollegeNo.Length).ThenBy(x => x.CollegeNo).ThenBy(x => x.UserName); var result = query.ToGridResultSet(pageIndex, pageSize); result.rows = this.CalculatePaymentDetailView(result.rows); return result; } public List GetPaymentDetailViewList(ConfiguretView paymentConditionView, Guid? schoolyearID, Guid? collegeID, int? titleID) { Expression> staffExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> schoolyearExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> coursematerialExp = (x => true); Expression> missionExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { schoolyearExp = schoolyearExp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { missionExp = missionExp.And(x => x.CollegeID == collegeID); } if (titleID.HasValue) { staffExp = staffExp.And(x => x.TitleID == titleID); } var query = PaymentDAL.GetPaymentDetailViewQueryable(staffExp, schoolyearExp, missionExp, coursematerialExp); if (!string.IsNullOrEmpty(paymentConditionView.ConditionValue) && !string.IsNullOrEmpty(paymentConditionView.Attribute)) query = query.DynamicWhere(paymentConditionView.Attribute, paymentConditionView.Condition, paymentConditionView.ConditionValue); query = query.OrderBy(x => x.CollegeNo.Length).ThenBy(x => x.CollegeNo).ThenBy(x => x.UserName); var result = query.ToList(); result = this.CalculatePaymentDetailView(result); return result; } public List GetPaymentTotalViewList(ConfiguretView paymentConditionView, Guid? schoolyearID, Guid? collegeID, int? titleID) { Expression> staffExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> schoolyearExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); Expression> coursematerialExp = (x => true); Expression> missionExp = (x => x.RecordStatus > (int)SYS_STATUS.UNUSABLE); if (schoolyearID.HasValue) { schoolyearExp = schoolyearExp.And(x => x.SchoolyearID == schoolyearID); } if (collegeID.HasValue) { missionExp = missionExp.And(x => x.CollegeID == collegeID); } if (titleID.HasValue) { staffExp = staffExp.And(x => x.TitleID == titleID); } var query = PaymentDAL.GetPaymentDetailViewQueryable(staffExp, schoolyearExp, missionExp, coursematerialExp); if (!string.IsNullOrEmpty(paymentConditionView.ConditionValue) && !string.IsNullOrEmpty(paymentConditionView.Attribute)) query = query.DynamicWhere(paymentConditionView.Attribute, paymentConditionView.Condition, paymentConditionView.ConditionValue); query = query.OrderBy(x => x.CollegeNo.Length).ThenBy(x => x.CollegeNo).ThenBy(x => x.UserName); var detailList = query.ToList(); detailList = this.CalculatePaymentDetailView(detailList); var totalList = detailList.GroupBy(x => new { x.CollegeID, x.CollegeNo, x.CollegeName, x.UserID, x.LoginID, x.UserName, x.Account }) .Select(x => new PaymentTotalView { CollegeID = x.Key.CollegeID, CollegeNo = x.Key.CollegeNo, CollegeName = x.Key.CollegeName, Account = x.Key.Account, UserID = x.Key.UserID, LoginID = x.Key.LoginID, UserName = x.Key.UserName, TotalPayment = x.Sum(w => w.TotalPayment ?? 0) }).OrderBy(x => x.CollegeNo.Length).ThenBy(x => x.CollegeNo).ThenBy(x => x.UserName).ToList(); return totalList; } private List CalculatePaymentDetailView(IList paymentDetailViewList) { var teacherTypePaymentFormularList = TeacherTypePaymentFormulaDAL.GetTeacherTypePaymentFormulaViewQueryable(x => true).ToList(); foreach (var paymentDetailView in paymentDetailViewList) { var teacherTypePaymentFormular = teacherTypePaymentFormularList.FirstOrDefault(x => x.TeacherType == paymentDetailView.TeacherTypeID); if (teacherTypePaymentFormular != null) { ReflectorHelper.RunMethod(teacherTypePaymentFormular.StudentCountRateFormunla, paymentDetailView); ReflectorHelper.RunMethod(teacherTypePaymentFormular.PaymentFormula, paymentDetailView); } } return paymentDetailViewList.ToList(); } public void GetNormalStudentCountRate(PaymentDetailView paymentDetailView) { if ((paymentDetailView.StudentCount - paymentDetailView.ExemptionCount) <= paymentDetailView.BaseStudentCount) { paymentDetailView.StudentCountRate = 1; } else { paymentDetailView.StudentCountRate = (decimal?)(paymentDetailView.StudentCount - paymentDetailView.ExemptionCount) / (decimal?)(paymentDetailView.BaseStudentCount ?? 1); } if (paymentDetailView.StudentCountRate > paymentDetailView.StudentCountRateLimit) { paymentDetailView.StudentCountRate = paymentDetailView.StudentCountRateLimit; } } public void GetNormalPayment(PaymentDetailView paymentDetailView) { paymentDetailView.HoursPayment = (paymentDetailView.PaymentStandard ?? 0) * (paymentDetailView.LearningformRate ?? 0) * (paymentDetailView.StudentCountRate ?? 0) * (paymentDetailView.ActualScheduleHours + (2 * ((paymentDetailView.ExaminationModeID == (int)CF_ExaminationMode.WrittenExam && paymentDetailView.LearningformID == (int)CF_Learningform.Correspondence) ? 1 : 0)) ); } public void GetOuterStudentCountRate(PaymentDetailView paymentDetailView) { paymentDetailView.StudentCountRate = 1 + ((paymentDetailView.StudentCount - paymentDetailView.ExemptionCount) - 50) / 100; if (paymentDetailView.StudentCountRate < 1) { paymentDetailView.StudentCountRate = 1; } } public void GetOuterPayment(PaymentDetailView paymentDetailView) { paymentDetailView.HoursPayment = (paymentDetailView.PaymentStandard ?? 0) * (paymentDetailView.StudentCountRate ?? 0) * (paymentDetailView.ActualScheduleHours + (2 * ((paymentDetailView.ExaminationModeID == (int)CF_ExaminationMode.WrittenExam && paymentDetailView.LearningformID == (int)CF_Learningform.Correspondence) ? 1 : 0)) ); } } }