Exemplo n.º 1
0
        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("");
        }
Exemplo n.º 2
0
        public string SaveCompanyBudgetDistributes(E_VoucherBudgetDTO model)
        {
            using (var ts = this._unitOfWork.BeginTransaction())
            {
                try
                {
                    var budget = this._repo.SelectWhere(x => x.Id == model.Id && x.CompanyId == model.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ổ");
                        }

                        // update trang thai thanh da phan bo
                        var properties = new List <Expression <Func <E_VoucherBudget, object> > >
                        {
                            (x => x.Status),
                            (x => x.StatusName),
                        };
                        budget.Status     = (int?)BudgetStatus.Distributed;
                        budget.StatusName = "Đã phân bổ";
                        this._repo.UpdateByProperties(budget, properties);
                        _unitOfWork.SaveChanges();

                        #region Them dong menh gia cua cac nhan vien
                        foreach (var empData in model.BudgetDetails)
                        {
                            var budDetail = _detailRepo.SelectWhere(x => x.Id == empData.Id && x.OrgCompanyId == model.CompanyId).FirstOrDefault();
                            if (budDetail != null)
                            {
                                if (empData.DistributeLines != null && empData.DistributeLines.Count > 0)
                                {
                                    // update trang thai thanh da phan bo va so luong
                                    var detailProps = new List <Expression <Func <E_VoucherBudgetDetail, object> > >
                                    {
                                        (x => x.Status),
                                        (x => x.StatusName),
                                    };
                                    budDetail.Status     = (int?)BudgetStatus.Distributed;
                                    budDetail.StatusName = "Đã phân bổ";
                                    this._detailRepo.UpdateByProperties(budDetail, detailProps);
                                    _unitOfWork.SaveChanges();

                                    if (empData.DistributeLines.Sum(x => x.TotalValues) > empData.Budget)
                                    {
                                        return("Dữ liệu không khớp: [" + empData.CodeUser + "] Tổng tiền voucher lớn hơn ngân sách");
                                    }

                                    foreach (var demino in empData.DistributeLines)
                                    {
                                        // tim dong co cung menh gia cua nhan vien nay
                                        var demi = _detailLineRepo.SelectWhere(x => x.VoucherTypeCode == demino.VoucherTypeCode && x.VoucherBudgetDetailId == empData.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?)empData.Id,
                                                VoucherTypeCode       = demino.VoucherTypeCode,
                                                VoucherTypeName       = demino.VoucherTypeName
                                            };

                                            _detailLineRepo.Add(demi1);
                                            _unitOfWork.SaveChanges();
                                        }
                                    }
                                }
                            }
                        }
                        #endregion


                        #region Them dong menh gia cua ngay
                        foreach (var budDeno in model.BudgetDenominations)
                        {
                            // tim dong co cung menh gia cua nhan vien nay
                            var tBudDenomi = _denomiRepo.SelectWhere(x => x.VoucherTypeCode == budDeno.VoucherTypeCode && x.VoucherBudgetId == model.Id).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 = model.Id,
                                    VoucherTypeCode = budDeno.VoucherTypeCode,
                                    VoucherTypeName = budDeno.VoucherTypeName,
                                };

                                _denomiRepo.Add(demi1);
                                _unitOfWork.SaveChanges();
                            }
                        }
                        #endregion

                        ts.Commit();
                    }
                }
                catch (Exception ex)
                {
                    return(ex.Message);
                }
            }
            return("");
        }