public RespondData SaveEmployeeBudgetDistributes(E_VoucherBudgetDetailDTO model) { var result = _service.SaveEmployeeBudgetDistributes(model, GetOrg()); if (string.IsNullOrWhiteSpace(result)) { return(Success(null)); } return(Fail(result)); }
public string SaveEmployeeBudgetDistributes(E_VoucherBudgetDetailDTO filter, int?companyId) { using (var ts = this._unitOfWork.BeginTransaction()) { try { var budget = this._repo.SelectWhere(x => x.Id == filter.VoucherBudgetId && x.CompanyId == companyId).FirstOrDefault(); if (budget != null) { // da hoan thanh phan bo if (budget.Status >= (int?)BudgetStatus.DistributedCompleted) { return("Ngân sách của công ty ngày này đã hoàn thành phân bổ"); } var budDetail = _detailRepo.SelectWhere(x => x.Id == filter.Id).FirstOrDefault(); if (budDetail != null) { #region xoa line cu var oldLines = _detailLineRepo.SelectWhere(x => x.VoucherBudgetDetailId == budDetail.Id).ToList(); var deleteLines = new List <E_VoucherBudgetDetailLine>(); // danh sach detail line se xoa if (filter.DistributeLines != null && filter.DistributeLines.Count > 0) { foreach (var item in oldLines) { if (!filter.DistributeLines.Exists(x => x.VoucherTypeCode == item.VoucherTypeCode)) { deleteLines.Add(item); } } } else { deleteLines = oldLines; } foreach (var item in deleteLines) { _detailLineRepo.Delete(item); _unitOfWork.SaveChanges(); } #endregion #region them moi hoac update foreach (var demino in filter.DistributeLines) { // tim dong co cung menh gia cua nhan vien nay var demi = _detailLineRepo.SelectWhere(x => x.VoucherTypeCode == demino.VoucherTypeCode && x.VoucherBudgetDetailId == filter.Id).FirstOrDefault(); if (demi != null) // update { demi.CountNumberPage = demino.CountNumberPage; demi.Denominations = demino.Denominations; demi.TotalValues = demino.TotalValues; var detailLineProps = new List <Expression <Func <E_VoucherBudgetDetailLine, object> > > { (x => x.CountNumberPage), (x => x.Denominations), (x => x.TotalValues), }; this._detailLineRepo.UpdateByProperties(demi, detailLineProps); _unitOfWork.SaveChanges(); } else // add { var demi1 = new E_VoucherBudgetDetailLine() { CountNumberPage = demino.CountNumberPage, Denominations = demino.Denominations, TotalValues = demino.TotalValues, VoucherBudgetDetailId = (int?)filter.Id, VoucherTypeCode = demino.VoucherTypeCode, VoucherTypeName = demino.VoucherTypeName }; _detailLineRepo.Add(demi1); _unitOfWork.SaveChanges(); } } #endregion #region update bang thong ke menh gia cua ngay // danh sach nhan vien var details = _detailRepo.SelectWhere(x => x.VoucherBudgetId == filter.VoucherBudgetId).ToList(); // danh sach menh gia var oldBudgetDenomis = _denomiRepo.SelectWhere(x => x.VoucherBudgetId == filter.VoucherBudgetId).ToList(); // danh sach thong ke menh gia cua tung nhan vien var currBudgetDenomis = new List <E_VoucherBudgetDenominationsDTO>(); if (details != null && details.Count > 0) { foreach (var item in details) { var detailLines = _detailLineRepo.SelectWhere(x => x.VoucherBudgetDetailId == item.Id).ToList(); if (detailLines != null && detailLines.Count > 0) { foreach (var line in detailLines) { var budDeno = currBudgetDenomis.FirstOrDefault(x => x.VoucherTypeCode == line.VoucherTypeCode); // neu co trong danh sach if (budDeno != null) { budDeno.CountNumber += line.CountNumberPage; budDeno.TotalValues = budDeno.CountNumber * budDeno.Denominations; } else // chua co { currBudgetDenomis.Add(new E_VoucherBudgetDenominationsDTO() { CountNumber = line.CountNumberPage, Denominations = line.Denominations, TotalValues = line.TotalValues, VoucherTypeCode = line.VoucherTypeCode, VoucherTypeName = line.VoucherTypeName }); } } } } // cap nhat danh sach #region danh sach menh gia bang 0 var setToZeroDenomis = new List <E_VoucherBudgetDenominations>(); if (currBudgetDenomis != null && currBudgetDenomis.Count > 0) { foreach (var item in oldBudgetDenomis) { if (!currBudgetDenomis.Exists(x => x.VoucherTypeCode == item.VoucherTypeCode)) { setToZeroDenomis.Add(item); } } } else { setToZeroDenomis = oldBudgetDenomis; } foreach (var item in setToZeroDenomis) { var setToZeroDenomisProps = new List <Expression <Func <E_VoucherBudgetDenominations, object> > > { (x => x.CountNumber), (x => x.Denominations), (x => x.TotalValues), }; this._denomiRepo.UpdateByProperties(item, setToZeroDenomisProps); _unitOfWork.SaveChanges(); } #endregion #region cap nhat danh sach foreach (var budDeno in currBudgetDenomis) { // tim dong co cung menh gia cua nhan vien nay var tBudDenomi = _denomiRepo.SelectWhere(x => x.VoucherTypeCode == budDeno.VoucherTypeCode && x.VoucherBudgetId == filter.VoucherBudgetId).FirstOrDefault(); if (tBudDenomi != null) // update { tBudDenomi.CountNumber = budDeno.CountNumber; tBudDenomi.Denominations = budDeno.Denominations; tBudDenomi.TotalValues = budDeno.TotalValues; var budDenoProps = new List <Expression <Func <E_VoucherBudgetDenominations, object> > > { (x => x.CountNumber), (x => x.Denominations), (x => x.TotalValues), }; this._denomiRepo.UpdateByProperties(tBudDenomi, budDenoProps); _unitOfWork.SaveChanges(); } else // add { var demi1 = new E_VoucherBudgetDenominations() { CountNumber = budDeno.CountNumber, Denominations = budDeno.Denominations, TotalValues = budDeno.TotalValues, VoucherBudgetId = (long)filter.VoucherBudgetId, VoucherTypeCode = budDeno.VoucherTypeCode, VoucherTypeName = budDeno.VoucherTypeName, }; _denomiRepo.Add(demi1); _unitOfWork.SaveChanges(); } } #endregion } #endregion } ts.Commit(); } } catch (Exception ex) { FileLog.WriteLog(ex.ToString()); return(ex.Message); } } return(""); }
public string SaveEmployeeBudgetDistributes(E_VoucherBudgetDetailDTO model, int?companyId) { return(_repo.SaveEmployeeBudgetDistributes(model, companyId)); }