public NvChuongTrinhKhuyenMai UpdatePhieu(NvKhuyenMaiBuy1Get1Vm.Dto instance)
        {
            string _unitCode = GetCurrentUnitCode();
            NvChuongTrinhKhuyenMai exsitItem = FindById(instance.Id);

            if (exsitItem.TrangThai == (int)ApprovalState.IsComplete)
            {
                return(null);
            }
            NvChuongTrinhKhuyenMai masterData = Mapper.Map <NvKhuyenMaiBuy1Get1Vm.Dto, NvChuongTrinhKhuyenMai>(instance);
            List <NvChuongTrinhKhuyenMaiChiTiet> detailData = Mapper.Map <List <NvKhuyenMaiBuy1Get1Vm.DtoDetail>, List <NvChuongTrinhKhuyenMaiChiTiet> >(instance.DataDetails);
            DbSet <MdMerchandise> merchandiseCollection     = UnitOfWork.Repository <MdMerchandise>().DbSet;

            {
                IQueryable <NvChuongTrinhKhuyenMaiChiTiet> detailCollection = UnitOfWork.Repository <NvChuongTrinhKhuyenMaiChiTiet>().DbSet.Where(x => x.MaChuongTrinh == exsitItem.MaChuongTrinh);
                detailCollection.ToList().ForEach(x => x.ObjectState = ObjectState.Deleted);
            }
            detailData.ForEach(x =>
            {
                x.MaChuongTrinh = masterData.MaChuongTrinh;
                x.Id            = Guid.NewGuid().ToString();
            });
            masterData.IState      = string.Format("{0}", (int)SyncEntityState.IsWaitingForSync);
            masterData.IUpdateBy   = GetClaimsPrincipal().Identity.Name;
            masterData.IUpdateDate = DateTime.Now;
            masterData.UnitCode    = _unitCode;
            UnitOfWork.Repository <NvChuongTrinhKhuyenMaiChiTiet>().InsertRange(detailData);
            NvChuongTrinhKhuyenMai result = Update(masterData);

            return(result);
        }
        public NvChuongTrinhKhuyenMai InsertPhieu(NvKhuyenMaiBuy1Get1Vm.Dto instance)
        {
            NvChuongTrinhKhuyenMai item = AutoMapper.Mapper.Map <NvKhuyenMaiBuy1Get1Vm.Dto, NvChuongTrinhKhuyenMai>(instance);

            item.Id            = Guid.NewGuid().ToString();
            item.MaChuongTrinh = SaveCode();
            NvChuongTrinhKhuyenMai result = Insert(item);
            List <NvChuongTrinhKhuyenMaiChiTiet> detailData = AutoMapper.Mapper.Map <List <NvKhuyenMaiBuy1Get1Vm.DtoDetail>, List <NvChuongTrinhKhuyenMaiChiTiet> >(instance.DataDetails);

            detailData.ForEach(x => {
                x.Id            = Guid.NewGuid().ToString();
                x.MaChuongTrinh = item.MaChuongTrinh;
            });
            item.IState      = string.Format("{0}", (int)SyncEntityState.IsWaitingForSync);
            item.ICreateBy   = GetClaimsPrincipal().Identity.Name;
            item.ICreateDate = DateTime.Now;
            UnitOfWork.Repository <NvChuongTrinhKhuyenMaiChiTiet>().InsertRange(detailData);
            return(result);
        }