public NvVatTuChungTu InsertPhieu(NvXuatBanLeVm.Dto instance) { var _ParentUnitCode = GetParentUnitCode(); instance.Calc(); //Tinh lại cac thuộc tính thứ sinh var item = Mapper.Map <NvXuatBanLeVm.Dto, NvVatTuChungTu>(instance); item.Id = Guid.NewGuid().ToString(); var result = AddUnit(item); result.MaChungTu = SaveCode(); item.GenerateMaChungTuPk(); result = Insert(result); var detailData = AutoMapper.Mapper.Map <List <NvXuatBanLeVm.DtoDetail>, List <NvVatTuChungTuChiTiet> >(instance.DataDetails); var importWareHouse = UnitOfWork.Repository <MdWareHouse>().DbSet.FirstOrDefault(x => x.MaKho == result.MaKhoNhap); var merchandiseCollection = UnitOfWork.Repository <MdMerchandise>().DbSet; detailData.ForEach(x => { var hang = merchandiseCollection.FirstOrDefault(u => u.MaVatTu == x.MaHang && u.UnitCode.StartsWith(_ParentUnitCode)); x.TenHang = hang != null ? hang.TenHang : ""; x.Id = Guid.NewGuid().ToString(); x.MaChungTu = result.MaChungTu; x.MaChungTuPk = result.MaChungTuPk; x.SoLuongCT = x.SoLuong; x.SoLuongBaoCT = x.SoLuongBao; }); InsertGeneralLedger(instance.DataClauseDetails, result); UnitOfWork.Repository <NvVatTuChungTuChiTiet>().InsertRange(detailData); return(result); }
public NvVatTuChungTu UpdatePhieu(NvXuatBanLeVm.Dto instance) { var _ParentUnitCode = GetParentUnitCode(); instance.Calc(); var exsitItem = FindById(instance.Id); if (exsitItem.TrangThai == (int)ApprovalState.IsComplete) { return(null); } var masterData = Mapper.Map <NvXuatBanLeVm.Dto, NvVatTuChungTu>(instance); var detailData = Mapper.Map <List <NvXuatBanLeVm.DtoDetail>, List <NvVatTuChungTuChiTiet> >(instance.DataDetails); var merchandiseCollection = UnitOfWork.Repository <MdMerchandise>().DbSet; { var detailCollection = UnitOfWork.Repository <NvVatTuChungTuChiTiet>().DbSet.Where(x => x.MaChungTuPk == exsitItem.MaChungTuPk); detailCollection.ToList().ForEach(x => x.ObjectState = ObjectState.Deleted); } detailData.ForEach(x => { var hang = merchandiseCollection.FirstOrDefault(u => u.MaVatTu == x.MaHang && u.UnitCode.StartsWith(_ParentUnitCode)); x.TenHang = hang != null ? hang.TenHang : ""; x.Id = Guid.NewGuid().ToString(); x.MaChungTuPk = exsitItem.MaChungTuPk; x.MaChungTu = exsitItem.MaChungTu; x.SoLuongCT = x.SoLuong; x.SoLuongBaoCT = x.SoLuongBao; }); UnitOfWork.Repository <NvVatTuChungTuChiTiet>().InsertRange(detailData); UpdateGeneralLedger(instance.DataClauseDetails, exsitItem); var result = Update(masterData); return(result); }