public NvDatHang InsertPhieu(NvPhieuDatHangVm.Dto instance) { var _ParentUnitCode = GetParentUnitCode(); instance.Calc(); //Tinh lại cac thuộc tính thứ sinh var item = AutoMapper.Mapper.Map <NvPhieuDatHangVm.Dto, NvDatHang>(instance); item.IsBanBuon = instance.IsBuon ? (int)LoaiDonDatHang.BANBUON : (int)LoaiDonDatHang.BANLE; item.Id = Guid.NewGuid().ToString(); var result = AddUnit(item); string _unitCode = GetCurrentUnitCode(); result.SoPhieu = BuildCode_PTNX(TypeVoucher.DH.ToString(), _unitCode, true); result.GenerateMaChungTuPk(); result = Insert(result); var merchandiseCollection = UnitOfWork.Repository <MdMerchandise>().DbSet; var dataFilter = instance.DataDetails.Where(x => x.SoLuong > 0).ToList(); var dataDetails = AutoMapper.Mapper.Map <List <NvPhieuDatHangVm.DtoDetail>, List <NvDatHangChiTiet> >(dataFilter); dataDetails.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.SoPhieu = result.SoPhieu; x.SoPhieuPk = result.SoPhieuPk; }); UnitOfWork.Repository <NvDatHangChiTiet>().InsertRange(dataDetails); return(result); }
public NvDatHang UpdatePhieu(NvPhieuDatHangVm.Dto instance) { var _ParentUnitCode = GetParentUnitCode(); instance.Calc(); var exsitItem = FindById(instance.Id); if (exsitItem.TrangThai == (int)OrderState.IsComplete || exsitItem.TrangThai == (int)OrderState.IsRecieved) { return(null); } var merchandiseCollection = UnitOfWork.Repository <MdMerchandise>().DbSet; var masterData = Mapper.Map <NvPhieuDatHangVm.Dto, NvDatHang>(instance); var contract = UnitOfWork.Repository <MdContract>().DbSet.FirstOrDefault(x => x.MaHd == masterData.MaHd); if (contract != null) { masterData.MaKhachHang = contract.KhachHang; } var dataFilter = instance.DataDetails.Where(x => x.SoLuong > 0).ToList(); var detailData = Mapper.Map <List <NvPhieuDatHangVm.DtoDetail>, List <NvDatHangChiTiet> >(dataFilter); { var detailCollection = UnitOfWork.Repository <NvDatHangChiTiet>().DbSet.Where(x => x.SoPhieuPk == exsitItem.SoPhieuPk); 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.SoPhieu = exsitItem.SoPhieu; x.SoPhieuPk = exsitItem.SoPhieuPk; x.MaHd = exsitItem.MaHd; }); UnitOfWork.Repository <NvDatHangChiTiet>().InsertRange(detailData); var result = Update(masterData); return(result); }