/// <summary> /// Generate PhieuXuat based on PhieuKiemKe /// </summary> /// <param name="lstItems"></param> /// <returns></returns> private int GeneratePhieuXuat(IEnumerable <PhieuKiemKeChiTiet> lstItems) { var maNhaThuoc = this.GetNhaThuoc().MaNhaThuoc; var phieuXuat = new PhieuXuat { MaPhieuXuat = 0, NhaThuoc = unitOfWork.NhaThuocRepository.GetById(this.GetNhaThuoc().MaNhaThuoc), CreatedBy = unitOfWork.UserProfileRepository.GetById(WebSecurity.GetCurrentUserId), MaLoaiXuatNhap = 3, // KhachHang = 1, // NgayTao = DateTime.Today, NgayXuat = DateTime.Today, DaTra = 0, VAT = 0, SoPhieuXuat = db.PhieuXuats.Where(x => x.NhaThuoc.MaNhaThuoc == maNhaThuoc).OrderByDescending(x => x.SoPhieuXuat).FirstOrDefault().SoPhieuXuat, PhieuXuatChiTiets = new List <PhieuXuatChiTiet>() }; var thuocsUtil = new ThuocsUtil(db, this.GetNhaThuoc().MaNhaThuoc); foreach (var xuat in lstItems) { //var lstThuoc = thuocsUtil.GetThuocsLeft(xuat.ThuocId, Math.Abs((int)(xuat.TonKho - xuat.ThucTe))).ToList(); var maDonViTinh = db.Thuocs.FirstOrDefault(x => x.ThuocId == xuat.Thuoc.ThuocId) .DonViXuatLe.MaDonViTinh; //foreach (var thuoc in lstThuoc) //{ phieuXuat.PhieuXuatChiTiets.Add(new PhieuXuatChiTiet() { //ThuocId = xuat.ThuocId, // MaNhaThuoc = this.GetNhaThuoc().MaNhaThuoc, // MaDonViTinh = maDonViTinh, //SoLuong = thuoc.SoLuong, SoLuong = Math.Abs((int)(xuat.TonKho - xuat.ThucTe)), //HanDung = thuoc.HanDung, MaPhieuXuatCt = 0 }); //} //if (lstThuoc.Count > 0 && lstThuoc.ElementAt(0).Message != null) //{ // phieuXuat.PhieuXuatChiTiets.Add(new PhieuXuatChiTiet() // { // ThuocId = xuat.ThuocId, // MaNhaThuoc = this.GetNhaThuoc().MaNhaThuoc, // MaDonViTinh = maDonViTinh, // SoLuong = int.Parse(lstThuoc.ElementAt(0).Message), // //HanDung = // // db.PhieuXuatChiTiets.Where(x => x.ThuocId == xuat.ThuocId) // // .OrderByDescending(x => x.HanDung) // // .FirstOrDefault() // // .HanDung, // MaPhieuXuatCt = 0 // }); //} } db.PhieuXuats.Add(phieuXuat); db.SaveChanges(); return(phieuXuat.MaPhieuXuat); }