示例#1
0
        public HandleState UpdateCharge(CatChargeAddOrUpdateModel model)
        {
            using (var trans = DataContext.DC.Database.BeginTransaction())
            {
                try
                {
                    model.Charge.UserModified     = currentUser.UserID;
                    model.Charge.DatetimeModified = DateTime.Now;
                    model.Charge.GroupId          = currentUser.GroupId;
                    model.Charge.DepartmentId     = currentUser.DepartmentId;
                    model.Charge.OfficeId         = currentUser.OfficeID;
                    model.Charge.CompanyId        = currentUser.CompanyID;
                    var hs = DataContext.Update(model.Charge, x => x.Id == model.Charge.Id, false);
                    if (hs.Success == false)
                    {
                        return(hs);
                    }

                    foreach (var item in model.ListChargeDefaultAccount)
                    {
                        if (item.Id == 0)
                        {
                            item.ChargeId        = model.Charge.Id;
                            item.UserCreated     = item.UserModified = currentUser.UserID;
                            item.DatetimeCreated = item.DatetimeModified = DateTime.Now;
                            chargeDefaultRepository.Add(item, false);
                        }
                        else
                        {
                            chargeDefaultRepository.Delete(x => x.ChargeId == item.ChargeId);
                            item.UserModified     = currentUser.UserID;
                            item.DatetimeModified = DateTime.Now;
                            item.Id = 0;
                            chargeDefaultRepository.Add(item, false);
                        }
                    }
                    chargeDefaultRepository.SubmitChanges();
                    DataContext.SubmitChanges();
                    trans.Commit();
                    ClearCache();
                    Get();
                    return(hs);
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    return(new HandleState(ex.Message));
                }
                finally
                {
                    trans.Dispose();
                }
            }
        }
 public HandleState Delete(Guid id)
 {
     try
     {
         var hs = DataContext.Delete(x => x.Id == id, false);
         if (hs.Success)
         {
             var perGenerals = permissioSampleGeneralRepository.Get(x => x.PermissionId == id);
             foreach (var item in perGenerals)
             {
                 permissioSampleGeneralRepository.Delete(x => x.Id == item.Id, false);
             }
             var perSpecials = permissioSampleSpecialRepository.Get(x => x.PermissionId == id);
             foreach (var item in perSpecials)
             {
                 permissioSampleSpecialRepository.Delete(x => x.Id == item.Id, false);
             }
             DataContext.SubmitChanges();
             permissioSampleGeneralRepository.SubmitChanges();
             permissioSampleSpecialRepository.SubmitChanges();
         }
         return(hs);
     }
     catch (Exception ex)
     {
         var hs = new HandleState(ex.Message);
         return(hs);
     }
 }
        public HandleState Update(CatPartnerModel model)
        {
            var          listSalemans    = salemanRepository.Get(x => x.PartnerId == model.Id).ToList();
            ICurrentUser _user           = PermissionExtention.GetUserMenuPermission(currentUser, Menu.catPartnerdata);//Set default
            var          permissionRange = PermissionExtention.GetPermissionRange(_user.UserMenuPermission.Write);

            int code = GetPermissionToUpdate(new ModelUpdate {
                UserCreator = model.UserCreated, Salemans = listSalemans, PartnerGroup = model.PartnerGroup
            }, permissionRange, null);

            if (code == 403)
            {
                return(new HandleState(403, ""));
            }
            var entity = mapper.Map <CatPartner>(model);

            entity.DatetimeModified = DateTime.Now;
            entity.UserModified     = currentUser.UserID;
            entity.GroupId          = currentUser.GroupId;
            entity.CompanyId        = currentUser.CompanyID;
            entity.OfficeId         = currentUser.OfficeID;
            entity.DepartmentId     = currentUser.DepartmentId;

            if (entity.Active == false)
            {
                entity.InactiveOn = DateTime.Now;
            }
            if (model.SaleMans.Count > 0)
            {
                entity.SalePersonId = model.SaleMans.FirstOrDefault().SaleManId.ToString();
            }
            var hs = DataContext.Update(entity, x => x.Id == model.Id);

            if (hs.Success)
            {
                var hsoldman = salemanRepository.Delete(x => x.PartnerId == model.Id && !model.SaleMans.Any(sale => sale.Id == x.Id));
                var salemans = mapper.Map <List <CatSaleman> >(model.SaleMans);

                foreach (var item in model.SaleMans)
                {
                    if (item.Id == Guid.Empty)
                    {
                        item.Id          = Guid.NewGuid();
                        item.PartnerId   = entity.Id;
                        item.CreateDate  = DateTime.Now;
                        item.UserCreated = currentUser.UserID;
                        //item.CompanyId = currentUser.CompanyID;
                        //item.OfficeId = currentUser.OfficeID;
                        //item.GroupId = currentUser.GroupId;
                        //item.DepartmentId = currentUser.DepartmentId;
                        salemanRepository.Add(item);
                    }
                    else
                    {
                        item.ModifiedDate = DateTime.Now;
                        item.UserModified = currentUser.UserID;
                        salemanRepository.Update(item, x => x.Id == item.Id);
                    }
                }
                salemanRepository.SubmitChanges();
                ClearCache();
                Get();
            }
            return(hs);
        }
        /// <summary>
        /// Update tariff & list tariff model
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public HandleState UpdateTariff(TariffModel model)
        {
            try
            {
                var today = DateTime.Now;
                //Update SetTariff
                var tariff = mapper.Map <SetTariff>(model.setTariff);
                //var tariffCurrent = DataContext.Get(x => x.Id == tariff.Id).FirstOrDefault();
                //tariff.UserCreated = tariffCurrent.UserCreated;
                //tariff.DatetimeCreated = tariffCurrent.DatetimeCreated;
                tariff.DatetimeModified = today;

                var hs = DataContext.Update(tariff, x => x.Id == tariff.Id);
                if (hs.Success)
                {
                    var tariffDetails = mapper.Map <List <SetTariffDetail> >(model.setTariffDetails);

                    //Remove các tariff mà user đã gỡ bỏ
                    var listIdTariffDetail           = tariffDetails.Select(s => s.Id);
                    var listIdTariffDetailNeedRemove = setTariffDetailRepo.Get(x => x.TariffId == tariff.Id &&
                                                                               !listIdTariffDetail.Contains(x.Id)).Select(x => x.Id);
                    if (listIdTariffDetailNeedRemove.Count() > 0)
                    {
                        var hsTariffDetailDel = setTariffDetailRepo.Delete(x => listIdTariffDetailNeedRemove.Contains(x.Id));
                    }

                    //Update các tariff detail cũ
                    var tariffDetailOld = tariffDetails.Where(x => x.Id != Guid.Empty);
                    //&& setTariffDetailRepo.Get(g => g.TariffId == tariff.Id).Select(s => s.Id).Contains(x.Id));
                    if (tariffDetailOld.Count() > 0)
                    {
                        foreach (var item in tariffDetailOld)
                        {
                            //item.UserCreated = setTariffDetailRepo.Get(x => x.Id == item.Id).FirstOrDefault().UserCreated;
                            item.UserModified = model.setTariff.UserModified;
                            //item.DatetimeCreated = setTariffDetailRepo.Get(x => x.Id == item.Id).FirstOrDefault().DatetimeCreated;
                            item.DatetimeModified = DateTime.Now;
                            var hsTariffDetailUpdate = setTariffDetailRepo.Update(item, x => x.Id == item.Id);
                        }
                    }

                    //Add các tariff detail mới
                    var tariffDetailNew = tariffDetails.Where(x => x.Id == Guid.Empty).ToList();
                    if (tariffDetailNew.Count > 0)
                    {
                        tariffDetailNew.ForEach(r =>
                        {
                            r.Id              = Guid.NewGuid();
                            r.TariffId        = tariff.Id;
                            r.UserCreated     = r.UserModified = model.setTariff.UserModified;
                            r.DatetimeCreated = r.DatetimeModified = DateTime.Now;
                        });
                        var hsTariffDetailAdd = setTariffDetailRepo.Add(tariffDetailNew);
                    }
                }

                return(hs);
            }
            catch (Exception ex)
            {
                return(new HandleState(ex.Message));
            }
        }