private void btnOpen_Click(object sender, EventArgs e)
        {
            List <string> copiedImagesList =
                FileManager.FileDialogeAndCopy(ActiveSelectedPatient.Person_CU_ID.ToString(),
                                               FileManager.GetServerDirectoryPath(DB_ServerDirectory.ScanDirectory), true,
                                               FileSelectionFilter.Images, ref errorMessage,
                                               ActiveSelectedPatient.Person_CU_ID + "_" + (int)ImageType_P_ID + "_" + DateTime.Now.Date.Day +
                                               "_" + DateTime.Now.Date.Month + "_" + DateTime.Now.Date.Year);

            if (!string.IsNullOrEmpty(errorMessage))
            {
                XtraMessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error,
                                    MessageBoxDefaultButton.Button1, DefaultBoolean.Default);
                errorMessage = string.Empty;
                return;
            }

            if (copiedImagesList == null || copiedImagesList.Count == 0)
            {
                return;
            }

            if (ImagesList == null)
            {
                ImagesList = new List <PatientAttachment_cu>();
            }

            foreach (string fileName in copiedImagesList)
            {
                PatientAttachment_cu patientAttachment = MerkDBBusinessLogicEngine.CreateNewPatientAttachement(
                    ActiveSelectedPatient.Person_CU_ID,
                    Path.GetFileName(fileName), fileName, (DB_ImageType)ImageType_P_ID, Description,
                    ApplicationStaticConfiguration.ActiveLoginUser.Person_CU_ID);

                switch (ActiveScanningMode)
                {
                case ScanningMode.Regular:
                    if (MerkDBBusinessLogicEngine.SavePatientAttachement(patientAttachment))
                    {
                        ImagesList.Add(patientAttachment);
                    }
                    break;

                case ScanningMode.MedicalVisit:
                    VisitTiming_Attachment visitTimingAttachement =
                        PEMRBusinessLogic.CreateNew_VisitTiming_Attachment(
                            patientAttachment, ApplicationStaticConfiguration.ActiveLoginUser.Person_CU_ID);
                    if (visitTimingAttachement != null)
                    {
                        if (PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_Attachment == null)
                        {
                            PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_Attachment = new List <VisitTiming_Attachment>();
                        }
                    }
                    PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_Attachment.Add(visitTimingAttachement);
                    ImagesList.Add(patientAttachment);

                    break;
                }
            }

            txtDescription.EditValue = null;
            txtPictureName.EditValue = null;
            CommonViewsActions.FillListBoxControl(lstImageNames, ImagesList, "ImageName");
            lstImageNames.Refresh();
        }
        private void btnScanner_Click(object sender, EventArgs e)
        {
            if (txtPictureName.EditValue == null)
            {
                XtraMessageBox.Show("يجــب كتابــة إســم الصـــورة", "Note",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error,
                                    MessageBoxDefaultButton.Button1, DefaultBoolean.Default);
                return;
            }

            string savedPath = "";
            Image  image     = WIAScannerEngine.ScanFile(ActiveSelectedPatient.Person_CU_ID.ToString(),
                                                         ActiveSelectedPatient.Person_CU_ID + "_" + (int)ImageType_P_ID + "_" + DateTime.Now.Date.Day + "_" +
                                                         DateTime.Now.Date.Month + "_" + DateTime.Now.Date.Year + "_" + txtPictureName.Text, ImageFormat.JPEG,
                                                         false, ref savedPath, ref errorMessage);

            if (image == null)
            {
                if (!string.IsNullOrEmpty(errorMessage) && !string.IsNullOrWhiteSpace(errorMessage))
                {
                    XtraMessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error,
                                        MessageBoxDefaultButton.Button1, DefaultBoolean.Default);
                    errorMessage = string.Empty;
                    return;
                }
            }

            if (!string.IsNullOrEmpty(errorMessage) && !string.IsNullOrWhiteSpace(errorMessage))
            {
                XtraMessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error,
                                    MessageBoxDefaultButton.Button1, DefaultBoolean.Default);
                errorMessage = string.Empty;
                return;
            }

            pictureEdit1.Image = image;
            if (ImagesList == null)
            {
                ImagesList = new List <PatientAttachment_cu>();
            }

            PatientAttachment_cu patientAttachment = MerkDBBusinessLogicEngine.CreateNewPatientAttachement(
                ActiveSelectedPatient.Person_CU_ID,
                txtPictureName.Text, savedPath, (DB_ImageType)ImageType_P_ID, Description,
                ApplicationStaticConfiguration.ActiveLoginUser.Person_CU_ID);
            VisitTiming_InvestigationResult investigationResult    = null;
            VisitTiming_Attachment          visitTimingAttachement = null;

            switch (ActiveScanningMode)
            {
            case ScanningMode.Regular:
                if (MerkDBBusinessLogicEngine.SavePatientAttachement(patientAttachment))
                {
                    ImagesList.Add(patientAttachment);
                }
                break;

            case ScanningMode.MedicalVisit:
                visitTimingAttachement =
                    PEMRBusinessLogic.CreateNew_VisitTiming_Attachment(
                        patientAttachment, ApplicationStaticConfiguration.ActiveLoginUser.Person_CU_ID);
                if (visitTimingAttachement != null)
                {
                    if (PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_Attachment == null)
                    {
                        PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_Attachment = new List <VisitTiming_Attachment>();
                    }
                    PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_Attachment.Add(visitTimingAttachement);
                    ImagesList.Add(patientAttachment);

                    switch (MedicalType)
                    {
                    case MedicalType.InvestigationResult:
                        investigationResult =
                            PEMRBusinessLogic.CreateNew_VisitTiming_InvestigationResult(visitTimingAttachement,
                                                                                        Active_VisitTiming_InvestigationReservation,
                                                                                        ApplicationStaticConfiguration.ActiveLoginUser.Person_CU_ID);
                        if (investigationResult != null)
                        {
                            if (PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_InvestigationResult == null)
                            {
                                PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_InvestigationResult =
                                    new List <VisitTiming_InvestigationResult>();
                            }
                        }
                        PEMRBusinessLogic.ActivePEMRObject.List_VisitTiming_InvestigationResult.Add(
                            investigationResult);
                        break;
                    }
                }

                break;
            }

            txtDescription.EditValue = null;
            txtPictureName.EditValue = null;
            CommonViewsActions.FillListBoxControl(lstImageNames, ImagesList, "ImageName");
            lstImageNames.Refresh();

            if (investigationResult != null && visitTimingAttachement != null &&
                PEMRBusinessLogic.ActiveVisitTimming != null && SaveImmediately)
            {
                visitTimingAttachement.VisitTimingID = PEMRBusinessLogic.ActiveVisitTimming.ID;
                investigationResult.VisitTiming_InvestigationReservationID =
                    Active_VisitTiming_InvestigationReservation.ID;
                if (investigationResult.SaveChanges())
                {
                    PatientAttachment_cu.ItemsList.Add(patientAttachment);
                }
            }
        }