Exemplo n.º 1
0
 public List <MdMerchandiseVm.MasterDto> GetAllDataChild(string maVatTu)
 {
     try
     {
         List <MdMerchandiseVm.MasterDto> result = new List <MdMerchandiseVm.MasterDto>();
         var unitCode = GetCurrentUnitCode();
         List <MdMerchandise> instance = Repository.DbSet.Where(x => x.MaCha == maVatTu && x.UnitCode == unitCode).ToList();
         if (instance.Count > 0)
         {
             instance.ForEach(x =>
             {
                 MdMerchandiseVm.MasterDto masterData = new MdMerchandiseVm.MasterDto();
                 masterData             = Mapper.Map <MdMerchandise, MdMerchandiseVm.MasterDto>(x);
                 var detailData         = UnitOfWork.Repository <MdMerchandisePrice>().DbSet.Where(y => y.MaVatTu == x.MaVatTu && x.UnitCode == unitCode).ToList();
                 masterData.DataDetails = Mapper.Map <List <MdMerchandisePrice>, List <MdMerchandiseVm.DtoDetail> >(detailData);
                 result.Add(masterData);
             });
         }
         return(result);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 2
0
        public MdMerchandise UpdateDto(MdMerchandiseVm.MasterDto instance)
        {
            string                    unitCode   = GetCurrentUnitCode();
            MdMerchandise             existItem  = FindById(instance.Id);
            MdMerchandise             masterData = Mapper.Map <MdMerchandiseVm.MasterDto, MdMerchandise>(instance);
            List <MdMerchandisePrice> detailData = Mapper.Map <List <MdMerchandiseVm.DtoDetail>, List <MdMerchandisePrice> >(instance.DataDetails);

            {
                List <MdMerchandisePrice> detail = UnitOfWork.Repository <MdMerchandisePrice>().DbSet.Where(x => x.MaVatTu == masterData.MaVatTu && x.MaDonVi == unitCode).ToList();
                detail.ForEach(x => x.ObjectState = ObjectState.Deleted);
            }
            {
                if (detailData.GroupBy(x => x.MaDonVi).Any(x => x.Count() > 1))
                {
                    throw new Exception("Không thể tồn tại nhiều giá trên cùng đơn vị!");
                }
            }
            {
                detailData.ForEach(x =>
                {
                    x.MaVatTu = masterData.MaVatTu;
                    x.Id      = Guid.NewGuid().ToString();
                });
            }
            masterData.PathImage = "/Upload/MerchandiseImage/" + masterData.MaVatTu + "/";
            string path = GetPhysicalPathImage() + masterData.MaVatTu + "\\";

            if (!string.IsNullOrEmpty(instance.AvatarName))
            {
                FileStream fs        = new FileStream(path + instance.AvatarName, FileMode.Open, FileAccess.Read);
                byte[]     ImageData = new byte[fs.Length];
                fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
                fs.Close();
                masterData.Avatar = ImageData;
            }
            masterData.IState      = string.Format("{0}", (int)SyncEntityState.IsWaitingForSync);
            masterData.ICreateBy   = existItem.ICreateBy;
            masterData.ICreateDate = existItem.ICreateDate;
            masterData.IUpdateBy   = GetClaimsPrincipal().Identity.Name;
            masterData.IUpdateDate = DateTime.Now;
            MdMerchandise result = Update(masterData);

            UnitOfWork.Repository <MdMerchandisePrice>().InsertRange(detailData);
            return(result);
        }
Exemplo n.º 3
0
        public MdMerchandise UpdateDto(MdMerchandiseVm.MasterDto instance)
        {
            string unitCode    = GetCurrentUnitCode();
            var    currentUser = (HttpContext.Current.User as ClaimsPrincipal);

            MdMerchandise             existItem  = FindById(instance.Id);
            MdMerchandise             masterData = Mapper.Map <MdMerchandiseVm.MasterDto, MdMerchandise>(instance);
            List <MdMerchandisePrice> detailData = Mapper.Map <List <MdMerchandiseVm.DtoDetail>, List <MdMerchandisePrice> >(instance.DataDetails);

            {
                List <MdMerchandisePrice> detail = UnitOfWork.Repository <MdMerchandisePrice>().DbSet.Where(x => x.MaVatTu == masterData.MaVatTu && x.MaDonVi == unitCode).ToList();
                detail.ForEach(x => x.ObjectState = ObjectState.Deleted);
            }
            if (detailData.GroupBy(x => x.MaDonVi).Any(x => x.Count() > 1))
            {
                throw new Exception("Không thể tồn tại nhiều giá trên cùng đơn vị!");
            }

            detailData.ForEach(x =>
            {
                x.MaVatTu = masterData.MaVatTu;
                x.Id      = Guid.NewGuid().ToString();
            });

            masterData.PathImage = "/Upload/MerchandiseImage/" + masterData.MaVatTu + "/";
            string path = GetPhysicalPathImage() + masterData.MaVatTu + "\\";

            if (!string.IsNullOrEmpty(instance.AvatarName))
            {
                FileStream fs        = new FileStream(path + instance.AvatarName, FileMode.Open, FileAccess.Read);
                byte[]     ImageData = new byte[fs.Length];
                fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
                fs.Close();
                masterData.Avatar = ImageData;
            }
            masterData.IState      = string.Format("{0}", (int)SyncEntityState.IsWaitingForSync);
            masterData.ICreateBy   = existItem.ICreateBy;
            masterData.ICreateDate = existItem.ICreateDate;
            masterData.IUpdateBy   = GetClaimsPrincipal().Identity.Name;
            masterData.IUpdateDate = DateTime.Now;
            MdMerchandise result = Update(masterData);

            UnitOfWork.Repository <MdMerchandisePrice>().InsertRange(detailData);

            // Update giá hàng hóa bó hàng
            var existsMatHangBoHang = UnitOfWork.Repository <MdBoHangChiTiet>().DbSet.Any(x => x.MaHang == result.MaVatTu && x.UnitCode == unitCode);

            if (!existsMatHangBoHang)
            {
                return(result);
            }
            else
            {
                var listBoHangChuaMa = UnitOfWork.Repository <MdBoHangChiTiet>().DbSet.Where(x => x.MaHang == result.MaVatTu && x.UnitCode == unitCode);
                if (listBoHangChuaMa != null && listBoHangChuaMa.Count() > 0)
                {
                    foreach (var item in listBoHangChuaMa)
                    {
                        var existsBoHang = UnitOfWork.Repository <MdBoHang>().DbSet.Any(x => x.MaBoHang == item.MaBoHang && x.UnitCode == unitCode && x.TrangThai == 10);
                        if (existsBoHang)
                        {
                            var itemPrice = detailData.FirstOrDefault(x => x.MaVatTu == item.MaHang && x.MaDonVi == item.UnitCode);
                            if (itemPrice != null)
                            {
                                item.DonGia = itemPrice.GiaBanLeVat;

                                var TongBanLe = item.SoLuong * (itemPrice.GiaBanLeVat - (itemPrice.GiaBanLeVat * item.TyLeCKLe / 100));
                                decimal.TryParse(TongBanLe.ToString(), out decimal TongLe);
                                item.TongBanLe = Math.Round(TongLe, 2);

                                var TongBanBuon = item.SoLuong * (itemPrice.GiaBanBuonVat - (itemPrice.GiaBanBuonVat * item.TyLeCKBuon / 100));
                                decimal.TryParse(TongBanBuon.ToString(), out decimal TongBuon);
                                item.TongBanBuon = Math.Round(TongBuon, 2);

                                item.IUpdateDate = DateTime.Now;
                                item.IUpdateBy   = currentUser.Identity.Name;

                                UnitOfWork.Repository <MdBoHangChiTiet>().Update(item);
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 4
0
        public MdMerchandise InsertChild(MdMerchandiseVm.MasterDto instance)
        {
            if (instance.UseGenCode)
            {
                instance.MaVatTu = SaveCodeChild(instance.MaCha);
            }
            var strBarcode = isExistBarCode(instance.Barcode);

            if (!string.IsNullOrEmpty(strBarcode))
            {
                throw new Exception("Barcode trùng:" + strBarcode);
            }
            if (!string.IsNullOrEmpty(instance.ItemCode))
            {
                instance.ItemCode = SaveCodeCanDienTu();
            }
            var masterData = Mapper.Map <MdMerchandiseVm.MasterDto, MdMerchandise>(instance);
            var detailData = Mapper.Map <List <MdMerchandiseVm.DtoDetail>, List <MdMerchandisePrice> >(instance.DataDetails);

            if (detailData.Count == 0)
            {
                detailData.Add(new MdMerchandisePrice()
                {
                    GiaBanBuon    = 0,
                    GiaBanBuonVat = 0,
                    GiaBanLe      = 0,
                    GiaBanLeVat   = 0,
                    GiaMua        = 0,
                    GiaMuaVat     = 0,
                    GiaVon        = 0,
                    TyLeLaiBuon   = 0,
                    TyLeVatRa     = 0,
                    TyLeVatVao    = 0,
                    TyLeLaiLe     = 0,
                    SoTonMax      = 0,
                    SoTonMin      = 0,
                });
            }
            if (detailData.GroupBy(x => x.MaDonVi).Any(x => x.Count() > 1))
            {
                throw new Exception("Không thể tồn tại nhiều giá trên cùng đơn vị!");
            }

            detailData.ForEach(x =>
            {
                x.Id      = Guid.NewGuid().ToString();
                x.MaVatTu = masterData.MaVatTu;
                x.MaDonVi = GetCurrentUnitCode();
            });

            string path = GetPhysicalPathImage() + masterData.MaVatTu + "\\";

            masterData.PathImage = "/Upload/MerchandiseImage/" + masterData.MaVatTu + "/";
            if (!string.IsNullOrEmpty(instance.AvatarName))
            {
                FileStream fs        = new FileStream(path + instance.AvatarName, FileMode.Open, FileAccess.Read);
                byte[]     ImageData = new byte[fs.Length];
                fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
                fs.Close();
                masterData.Avatar = ImageData;
            }
            masterData.IState = string.Format("{0}", (int)SyncEntityState.IsWaitingForSync);
            var result = Insert(masterData);

            UnitOfWork.Repository <MdMerchandisePrice>().InsertRange(detailData);
            return(result);
        }
Exemplo n.º 5
0
        public MdMerchandise InsertDto(MdMerchandiseVm.MasterDto instance)
        {
            if (instance.UseGenCode)
            {
                instance.MaVatTu = SaveCode(instance.MaLoaiVatTu);
            }
            var strBarcode = isExistBarCode(instance.Barcode);

            if (!string.IsNullOrEmpty(strBarcode))
            {
                throw new Exception("Barcode trùng:" + strBarcode);
            }
            var mer = UnitOfWork.Repository <MdMerchandise>().DbSet.FirstOrDefault(x => x.MaVatTu == instance.MaCha);

            if (mer != null)
            {
                mer.TrangThaiCon = 10;
                mer.ObjectState  = ObjectState.Modified;
            }
            if (!string.IsNullOrEmpty(instance.ItemCode))
            {
                instance.ItemCode = SaveCodeCanDienTu();
            }
            instance.MaNCC   = instance.MaKhachHang;
            instance.TenHang = instance.TenVatTu;
            instance.MaKhac  = instance.MaVatTu;
            var masterData = Mapper.Map <MdMerchandiseVm.MasterDto, MdMerchandise>(instance);
            MdMerchandisePrice detailData = new MdMerchandisePrice();

            detailData.MaVatTu       = instance.MaVatTu;
            detailData.MaDonVi       = GetCurrentUnitCode();
            detailData.GiaBanBuon    = instance.GiaBanBuon;
            detailData.GiaBanBuonVat = instance.GiaBanBuonVat;
            detailData.GiaBanLe      = instance.GiaBanLe;
            detailData.GiaBanLeVat   = instance.GiaBanLeVat;
            detailData.GiaMua        = instance.GiaMua;
            detailData.GiaMuaVat     = instance.GiaMuaVat;
            detailData.GiaVon        = instance.GiaVon;
            detailData.TyLeLaiBuon   = instance.TyLeLaiBuon;
            detailData.TyLeVatRa     = instance.TyLeVatRa;
            detailData.TyLeVatVao    = instance.TyLeVatVao;
            detailData.TyLeLaiLe     = instance.TyLeLaiLe;
            detailData.SoTonMax      = 0;
            detailData.SoTonMin      = 0;
            detailData.UnitCode      = GetCurrentUnitCode();
            detailData.MaVatRa       = instance.MaVatRa;
            detailData.MaVatVao      = instance.MaVatVao;
            detailData.IState        = "50";
            detailData.Id            = Guid.NewGuid().ToString();
            detailData.ICreateBy     = GetClaimsPrincipal().Identity.Name;
            detailData.ICreateDate   = DateTime.Now;
            detailData.IUpdateBy     = GetClaimsPrincipal().Identity.Name;
            detailData.IUpdateDate   = DateTime.Now;
            string path = GetPhysicalPathImage() + masterData.MaVatTu + "\\";

            masterData.PathImage = "/Upload/MerchandiseImage/" + masterData.MaVatTu + "/";
            if (!string.IsNullOrEmpty(instance.AvatarName))
            {
                FileStream fs        = new FileStream(path + instance.AvatarName, FileMode.Open, FileAccess.Read);
                byte[]     ImageData = new byte[fs.Length];
                fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
                fs.Close();
                masterData.Avatar = ImageData;
            }
            masterData.IState      = string.Format("{0}", (int)SyncEntityState.IsWaitingForSync);
            masterData.ICreateBy   = GetClaimsPrincipal().Identity.Name;
            masterData.ICreateDate = DateTime.Now;
            var result = Insert(masterData);

            UnitOfWork.Repository <MdMerchandisePrice>().Insert(detailData);
            return(result);
        }