private Manipulation CreateNewManipulation(IClinicsData data, string currentReportDate, DataRow row, Patient currentPatient)
        {
            var procedureName = row["Procedure"].ToString();
            var specialistUin = row["SpecialistUIN"].ToString();
            var information = row["Information"].ToString();

            var specialist = data
                .Specialists.All()
                .Where(s => s.Uin == specialistUin)
                .FirstOrDefault();

            var procedure = data
                .Procedures.All()
                .Where(pr => pr.Name == procedureName)
                .FirstOrDefault();

            Manipulation currentManipulation = new Manipulation()
            {
                Id = Guid.NewGuid(),
                PatientId = currentPatient.Id,
                SpecialistId = specialist.Id,
                ProcedureId = procedure.Id,
                Information = information,
                Date = DateTime.ParseExact(currentReportDate, "dd-MM-yyyy",  CultureInfo.InvariantCulture)
            };
            return currentManipulation;
        }
        private XElement GenerateReportXml(IClinicsData data, int year, int month)
        {
            string monthStr = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month);
            var root = new XElement("reports", new XAttribute("period", string.Format("{0}-{1}", monthStr, year)));

            foreach (var specialist in data.Specialists.All().OrderBy(s => s.LastName).ThenBy(s => s.FirstName))
            {
                var days = specialist
                    .Manupulations
                    .Where(m => m.Date.Year == year && m.Date.Month == month)
                    .GroupBy(m => m.Date).ToDictionary(gr => gr.Key,
                        gr => new
                        {
                            manipulations = gr.Count(),
                            expense = gr.Sum(m => m.Procedure.Price)
                        })
                    .OrderBy(date => date.Key)
                    .Select(date => new XElement(
                        "day",
                        new XAttribute("date", date.Key.ToShortDateString()),
                        new XElement("expense", date.Value.expense),
                        new XElement("manipulations", date.Value.manipulations)));

                var specialistXml = new XElement(
                    "specialist",
                    new XAttribute("firstName", specialist.FirstName),
                    new XAttribute("lastName", specialist.LastName),
                    days);

                root.Add(specialistXml);
            }

            return root;
        }
 public HomeController(IFloorData floordata,
                       IClinicsData clinicdata,
                       IDoctorsData doctordata,
                       IHttpContextAccessor httpContextAccessor)
 {
     _httpContextAccessor = httpContextAccessor;
     _Floors  = floordata;
     _Clinics = clinicdata;
     _Doctors = doctordata;
 }
        public void Import(IClinicsData data)
        {
            this.ImportTitles(data);
            this.ImportProcedures(data);
            this.ImportSpecialties(data);
            this.ImportSpecialists(data);
            this.ImportClinics(data);

            data.SaveChanges();
        }
        public void Import(IClinicsData data, string fileName)
        {
            string zipPath = fileName;
            string tempFolder = string.Format("{0}{1}", Directory.GetCurrentDirectory(), TempFolderName);
            string currentReportDate = string.Empty;
            ZipArchive archive = ZipFile.OpenRead(zipPath);

            using (archive)
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    if (entry.FullName.EndsWith("/"))
                    {
                        currentReportDate = entry.FullName.TrimEnd('/');
                    }
                    else
                    {
                        if (!Directory.Exists(tempFolder))
                        {
                            Directory.CreateDirectory(tempFolder);
                        }

                        entry.ExtractToFile(Path.Combine(tempFolder, TempFileName), true);

                        DataTable excelData = this.ReadExcelData(string.Format("{0}{1}", tempFolder, TempFileName));

                        foreach (DataRow row in excelData.Rows)
                        {
                            var patientNumber = row["PatientNumber"].ToString();

                            if (patientNumber != string.Empty)
                            {
                                var patient = data
                                    .Patients.All()
                                    .Where(p => p.PatientNumber == patientNumber)
                                    .FirstOrDefault();

                                if (patient == null)
                                {
                                    patient = this.CreateNewPatient(row);
                                    data.Patients.Add(patient);
                                }

                                Manipulation currentManipulation = this.CreateNewManipulation(data, currentReportDate, row, patient);

                                patient.Manipulations.Add(currentManipulation);
                            }
                        }
                    }
                }
            }

            data.SaveChanges();
        }
        private void CreateTable(IClinicsData data, Document doc, int month, int year)
        {
            var dbManipulations = data.Manipulations.All()
                .Where(m => m.Date.Year == year && m.Date.Month == month)
                .OrderBy(m => m.Date)
                .ThenBy(m => m.Specialist.FirstName);

            var dbProcedures = data.Procedures.All().ToList();
            var dbPatients = data.Patients.All().ToList();
            var dbSpecialists = data.Specialists.All().ToList();
            var dbTitles = data.Titles.All().ToList();

            PdfPTable tableBody = new PdfPTable(5);

            foreach (var man in dbManipulations)
            {
                // "Procedure"
                var currentProcedure = dbProcedures.Where(p => p.Id == man.ProcedureId).FirstOrDefault();
                tableBody.AddCell(currentProcedure.Name);

                // "Manipulation"
                tableBody.AddCell(man.Information);

                tableBody.AddCell(man.Date.Day + "-" + man.Date.Month + "-" + man.Date.Year);

                // "Patient"
                var currentPatient = dbPatients.Where(p => p.Id == man.PatientId)
                                                .FirstOrDefault();
                tableBody.AddCell(currentPatient.Abreviature + " " + currentPatient.Age + " yrs");

                // "Specialist"
                var currentSpecialist = dbSpecialists.Where(s => s.Id == man.SpecialistId).FirstOrDefault();
                var currentSpecialistTitles = dbTitles.Where(t => t.Id == currentSpecialist.TitleId).FirstOrDefault();

                tableBody.AddCell(currentSpecialistTitles.TitleName + " " + currentSpecialist.FirstName + " " +
                    currentSpecialist.MiddleName + " " + currentSpecialist.LastName);
            }

            // Create footer
            PdfPTable footer = new PdfPTable(5);

            PdfPCell totalManipulationsTextCell = new PdfPCell(new Phrase(FileFooter));
            totalManipulationsTextCell.Colspan = 4;
            totalManipulationsTextCell.HorizontalAlignment = 2;
            footer.AddCell(totalManipulationsTextCell);

            PdfPCell totalManipulationsCell = new PdfPCell(new Phrase(dbManipulations.ToList().Count.ToString()));
            totalManipulationsCell.HorizontalAlignment = 1;
            footer.AddCell(totalManipulationsCell);

            doc.Add(tableBody);
            doc.Add(footer);
        }
        public void Export(IClinicsData data, int month, int year)
        {
            Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
            PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(this.fileName, FileMode.Create));

            doc.Open();

            this.CreateTitleHeader(doc);
            this.CreateTableHeader(doc, month, year);
            this.CreateTable(data, doc, month, year);

            doc.Close();
        }
        public void Import(IClinicsData data, string fileName)
        {
            this.doc.Load(fileName);
            XmlNode rootNode = this.doc.DocumentElement;

            foreach (XmlNode node in rootNode.ChildNodes)
            {
                var name = this.GetValue(node, "name");
                var price = decimal.Parse(this.GetValue(node, "price"));
                var information = this.GetValue(node, "information");

                this.InportInMongo(name, price, information);
                this.ImportInSql(data, name, price, information);
            }

            data.SaveChanges();
        }
        private void ImportInSql(IClinicsData data, string name, decimal price, string information)
        {
            var exists = data.Procedures.All()
                .Where(p => p.Name.Equals(name))
                .FirstOrDefault();

            if (exists == null)
            {
                Procedure procedure = new Procedure
                {
                    Id = Guid.NewGuid(),
                    Name = name,
                    Price = price,
                    Information = information
                };

                data.Procedures.Add(procedure);
            }
        }
        private void ImportClinics(IClinicsData data)
        {
            var allClinics = mongoDb.GetCollection<BsonDocument>("Clinics").FindAll();

            foreach (var clinic in allClinics)
            {
                var id = this.GetValue(clinic, "Id");
                var idGuid = new Guid(id);

                string name = this.GetValue(clinic, "ClinicName");
                string address = this.GetValue(clinic, "ClinicAddress");
                var phones = this.GetValue(clinic, "ClinicPhones");

                var chieff = this.GetValue(clinic, "ClinicChiefId");
                var chieffdGuid = new Guid(chieff);

                var existingRecord = data.Clinics.All()
                    .Where(c => c.Id.Equals(idGuid))
                    .FirstOrDefault();

                if (existingRecord == null)
                {
                    Clinic newClinic = new Clinic
                    {
                        Id = idGuid,
                        ClinicName = name,
                        ClinicAddress = address,
                        ClinicPhones = phones,
                        ClinicChiefId = chieffdGuid
                    };

                    data.Clinics.Add(newClinic);
                }
                else
                {
                    existingRecord.ClinicName = name;
                    existingRecord.ClinicAddress = address;
                    existingRecord.ClinicPhones = phones;
                    existingRecord.ClinicChiefId = chieffdGuid;
                }
            }
        }
        private void ImportProcedures(IClinicsData data)
        {
            var allProcedures = mongoDb.GetCollection<BsonDocument>("Procedures").FindAll();

            foreach (var procedure in allProcedures)
            {
                var idGuid = procedure["_id"].AsGuid;
                var name = this.GetValue(procedure, "Name");
                var price = decimal.Parse(this.GetValue(procedure, "Price"));
                var information = this.GetValue(procedure, "Information");

                var existingRecord = data.Procedures.All()
                    .Where(p => p.Name.Equals(name))
                    .FirstOrDefault();

                if (existingRecord == null)
                {
                    Procedure newProcedure = new Procedure
                    {
                        Id = idGuid,
                        Name = name,
                        Price = price,
                        Information = information
                    };

                    data.Procedures.Add(newProcedure);
                }
                else
                {
                    existingRecord.Name = name;
                    existingRecord.Price = price;
                    existingRecord.Information = information;
                }
            }
        }
        private void ImportTitles(IClinicsData data)
        {
            var allTitles = mongoDb.GetCollection<BsonDocument>("Titles").FindAll();;

            foreach (var title in allTitles)
            {
                var mongoId = this.GetValue(title, "TitleId");
                var idGuid = new Guid(mongoId);
                string titleName = this.GetValue(title, "Title");

                var existingRecord = data.Titles.All()
                    .Where(t => t.Id.Equals(idGuid))
                    .FirstOrDefault();

                if (existingRecord == null)
                {
                    Title newTitle = new Title
                    {
                        Id = idGuid,
                        TitleName = titleName
                    };

                    data.Titles.Add(newTitle);
                }
                else
                {
                    existingRecord.TitleName = titleName;
                }
            }
        }
        private void ImportSpecialties(IClinicsData data)
        {
            var allSpecialties = mongoDb.GetCollection<BsonDocument>("Specialties").FindAll();

            foreach (var specialty in allSpecialties)
            {
                var id = this.GetValue(specialty, "SpecialtyId");
                var idGuid = new Guid(id);
                var specialtyName = this.GetValue(specialty, "Specialty");

                var existingRecord = data.Specialties.All()
                    .Where(s => s.Id.Equals(idGuid))
                    .FirstOrDefault();

                if (existingRecord == null)
                {
                    Specialty newSpecialty = new Specialty
                    {
                        Id = idGuid,
                        Speciality = specialtyName
                    };

                    data.Specialties.Add(newSpecialty);
                }
                else
                {
                    existingRecord.Speciality = specialtyName;
                }
            }
        }
        private void ImportSpecialists(IClinicsData data)
        {
            var allSpecialists = mongoDb.GetCollection<BsonDocument>("Specialists").FindAll();

            foreach (var specialist in allSpecialists)
            {
                var mongoId = this.GetValue(specialist, "Id");
                var idGuid = new Guid(mongoId);

                var firstName = this.GetValue(specialist, "FirstName");
                var middleName = this.GetValue(specialist, "MiddleName");
                var lastName = this.GetValue(specialist, "LastName");

                var title = this.GetValue(specialist, "TitleId");
                var titleGuid = new Guid(title);

                var specialty = this.GetValue(specialist, "SpecialtyId");
                var specialtyGuid = new Guid(specialty);

                var uin = this.GetValue(specialist, "Uin");

                var existingRecord = data.Specialists.All()
                    .Where(s => s.Id.Equals(idGuid))
                    .FirstOrDefault();

                if (existingRecord == null)
                {
                    Specialist newSpecialist = new Specialist
                    {
                        Id = idGuid,
                        FirstName = firstName,
                        MiddleName = middleName,
                        LastName = lastName,
                        SpecialtyId = specialtyGuid,
                        TitleId = titleGuid,
                        Uin = uin
                    };

                    data.Specialists.Add(newSpecialist);
                }
                else
                {
                    existingRecord.FirstName = firstName;
                    existingRecord.MiddleName = middleName;
                    existingRecord.LastName = lastName;
                    existingRecord.SpecialtyId = specialtyGuid;
                    existingRecord.TitleId = titleGuid;
                    existingRecord.Uin = uin;
                }
            }
        }
 public void Export(IClinicsData data, int month, int year)
 {
     var reportXml = this.GenerateReportXml(data, year, month);
     reportXml.Save(this.fileName);
 }