コード例 #1
0
ファイル: ViewModel.cs プロジェクト: LhomNoir/Client
        /// <summary>
        /// -- Open admisnitration page after check if userID is grant --
        /// </summary>
        public void GetAdministration()
        {
            if (!string.IsNullOrWhiteSpace(userLogin))
            {
                Task.Run(() =>
                {
                    // -- Show loading page --
                    IsLoadingWindowVisible = Visibility.Visible;

                    // -- Check if user connect is admin or not --
                    if (allowedAccessToAdminSet.Contains(Environment.UserName.ToUpper()) == false)
                    {
                        DisplayErrorMessage("Vous n'avez pas le droit d'acceder à cette page !");
                    }
                    else
                    {
                        #region -- Populate Historic dataGrid --
                        //History = server.GetAllSimulations();
                        History = server.GetAllSimulations();

                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            // -- Clear List of simulations --
                            SimulationsList.Clear();

                            // -- Populate local list --
                            foreach (Historic s in History)
                            {
                                SimulationsList.Add(s);
                            }

                            MachinesView = CollectionViewSource.GetDefaultView(SimulationsList);
                            // -- Clear collection view --
                            MachinesView.GroupDescriptions.Clear();
                            MachinesView.GroupDescriptions.Add(new PropertyGroupDescription("IsStolen"));
                        }));
                        #endregion

                        #region -- Manage visibility --
                        IsCheckCustomerVisible = Visibility.Collapsed;
                        IsCustomerParkVisible  = Visibility.Collapsed;
                        IsStolenMachineVisible = Visibility.Collapsed;
                        IsAdminVisible         = Visibility.Visible;
                        // -- Hide Loading page --
                        IsLoadingWindowVisible = Visibility.Collapsed;
                        #endregion
                    }
                });
            }
        }
コード例 #2
0
ファイル: ViewModel.cs プロジェクト: LhomNoir/Client
        public ViewModel()
        {
            #region -- Test Zone !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! --


            #endregion -- http://www.aspsnippets.com/Articles/Save-and-Retrieve-Files-from-SQL-Server-Database-using-ASPNet.aspx --

            // -- Set default déclartion tpe --
            SelectedReportType = "Déclaration de vol";

            // --  --
            reportSelectType = SelectedReportType == "Déclaration de vol" ? "Volées" : "Perdues";

            // -- Get Username connect --
            userLogin = Environment.UserName.ToUpper();

            // -- define user email --
            userMail = userLogin.Replace("HILTI\\", string.Empty) + "@hilti.com";

            // -- Define all Admin allowed for Simulator adminitration page --
            allowedAccessToAdminSet = new HashSet <string>()
            {
                "MABOMIC", "RODRIGB"
            };

            // -- Clear --
            CustomerStolenToolsList.Clear();

            #region -- Set Window's visibility --
            //IsCheckCustomerCommandVisible = Visibility.Collapsed;

            IsCheckCustomerVisible       = Visibility.Visible;
            IsCustomerParkVisible        = Visibility.Collapsed;
            IsAdminVisible               = Visibility.Collapsed;
            IsStolenMachineVisible       = Visibility.Collapsed;
            isSendMailVisible            = Visibility.Collapsed;
            IsLoadingWindowVisible       = Visibility.Collapsed;
            IsSendSimulToCustomerVisible = Visibility.Collapsed;
            #endregion

            #region -- Commande qui permet de vérifier le customerID et de passer à la fenêtre qui affiche son park machine --
            checkCustomerCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                GetCustomer();
            }));
            #endregion

            #region -- Commande qui permet de retourner à l'accuiel à partir de la deuxième page --
            backToCheckCustomerCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                IsCheckCustomerVisible = Visibility.Visible;
                IsCustomerParkVisible  = Visibility.Collapsed;
            }));
            #endregion

            #region -- Commande qui permet de continuer vers la simulation et qui affiche les machines volées --
            goToSimulationCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                // -- Clear Contact list --
                CustomerStolenToolsList.Clear();

                #region -- populate Customer stolen machines list --

                foreach (var item in CustomerParkList.Where(x => x.IsStolen == true))
                {
                    CustomerStolenToolsList.Add(item);
                }

                // -- Display Customer stolen machine list in CollectionViewSource --
                MachinesView = CollectionViewSource.GetDefaultView(CustomerStolenToolsList);
                // -- Clear collection view --
                MachinesView.GroupDescriptions.Clear();
                MachinesView.GroupDescriptions.Add(new PropertyGroupDescription("Designation"));

                //List<string> materialList = new List<string>() { "76341", "123456", "7564532", "A10001896", "545018749" };
                // --  --
                Task.Run(() =>
                {
                    // -- Show loading page --
                    IsLoadingWindowVisible = Visibility.Visible;

                    SimulationResult simulationResult = server.GetRemaingCost(CustomerStolenToolsList.Select(p => p.Serial).ToList(), "11190312", "Demande du client", SelectedReportType);

                    //for (int i = 0; i < 10000; i++)
                    //{
                    //    Console.WriteLine(i.ToString());
                    //}

                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        IsLoadingWindowVisible = Visibility.Collapsed;
                        IsStolenMachineVisible = Visibility.Visible;
                        IsCustomerParkVisible  = Visibility.Collapsed;
                    }));

                    AmountToBePaid   = simulationResult.Amount;
                    SimulationNumber = simulationResult.NumAvis;
                    selectContact    = simulationResult.Contact;
                });

                // -- Set Message on GridView Top --
                SecondReport = SelectedReportType == "Déclaration de vol" ? "Ci - dessous la liste des machines déclarées volées par ce client" :
                               "Ci - dessous la liste des machines déclarées perdues par ce client";
                #endregion
            }));
            #endregion

            #region -- Commande qui permet de retourner à l'accuiel --
            backToHomeCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                IsCheckCustomerVisible = Visibility.Visible;
                IsCustomerParkVisible  = Visibility.Collapsed;
                IsStolenMachineVisible = Visibility.Collapsed;
                isSendMailVisible      = Visibility.Collapsed;
            }));
            #endregion

            #region -- Commande qui permet de passer de la page des machines volées à celle du par machine --
            backToSimulationCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                IsCustomerParkVisible  = Visibility.Visible;
                IsStolenMachineVisible = Visibility.Collapsed;
            }));
            #endregion

            #region -- Commande qui permet d'aller sur la fenêtre d'administration --
            goToAdminCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                // -- Clear search string --
                SearchString = string.Empty;
                GetAdministration();
            }));
            #endregion

            #region -- Commande qui permet à l'admin d'aller rentrer à l'accueil --
            adminBackToHome = new DelegateCommand(new System.Action <object>((arg) =>
            {
                IsAdminVisible         = Visibility.Collapsed;
                IsCheckCustomerVisible = Visibility.Visible;
            }));
            #endregion

            #region -- Commande qui permet d'envoyer un email --
            sendByMailCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                if (!string.IsNullOrWhiteSpace(BcFileName) && !string.IsNullOrWhiteSpace(PvFileName) && !string.IsNullOrWhiteSpace(selectContact))
                {
                    Task.Run(() =>
                    {
                        // -- Display loading page --
                        IsLoadingWindowVisible = Visibility.Visible;

                        #region -- Export Data to Excel --
                        excelUtility = new ExcelUtlity();
                        // -- Convert Customer stolen tool to dataTable --
                        convertDataTable = ConvertToDataTable(CustomerStolenToolsList, new string[] { "Code_Article", "Designation", "Duree_Contrat", "Quantite", "Date_Debut_Contrat", "Serial" });

                        // -- Create temp path for our excel file --
                        excelFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ExcelData.xlsx");

                        // -- Define dynamic Excel report title --
                        string reportType = SelectedReportType == "Simulation du montant de la franchise à payer suite à un vol" ?
                                            "Liste des machines déclarées volées" : "Liste des machines déclarées perdues";

                        excelUtility.WriteDataTableToExcel(convertDataTable, excelFileName, "Customer Stolen Tools", reportType, AmountToBePaid, CustomerName, userMail);
                        #endregion

                        #region -- Send mail --
                        // -- Create template --
                        string template     = @"T:\MKI-SOFTWARE\Project\Simulateur de vol\Template\MailTemplate.htm";
                        string htmlTemplate = File.ReadAllText(template, System.Text.Encoding.Default);
                        htmlTemplate        = htmlTemplate.Replace("{A}", SelectedReportType);
                        htmlTemplate        = htmlTemplate.Replace("{B}", userLogin);
                        htmlTemplate        = htmlTemplate.Replace("{C}", CustomerName);
                        htmlTemplate        = htmlTemplate.Replace("{E}", Comments);

                        // -- Call Send mailfunction --
                        sendMail.SendMailToFleet(fleetEmail, excelFileName, Suject, userMail, "*****@*****.**", htmlTemplate, new string[] { PvFileName, BcFileName });
                        #endregion

                        #region -- Save Customer stolen tools in database --
                        // -- Set custom excel file name --
                        string fName = excelFileName.Substring(excelFileName.LastIndexOf('\\') + 1);

                        // -- Set send date --
                        DateTime receiptDate = DateTime.Now;

                        // Après avoir récupérer le code clt, la déisgnation, data debut, durée contrat, quantité, serial, custID, custName, daterecption, Amount to be paid, filename,

                        #endregion

                        #region -- Send to server --


                        #endregion -- http://stackoverflow.com/questions/34586733/sending-a-value-from-server-to-client-with-sockets --

                        // -- Clear form --
                        BcFileName = string.Empty;
                        PvFileName = string.Empty;
                        Comments   = string.Empty;
                        IsFormOk   = false;

                        DisplayMessage("Le dossier complet du client " + CustomerName + " a été envoyée au service fleet pour traitement.\n Vous avez également reçu une copie.");

                        // To do...
                        isSendMailVisible = Visibility.Collapsed;
                        // -- Display loading page --
                        IsLoadingWindowVisible = Visibility.Collapsed;
                        IsCheckCustomerVisible = Visibility.Visible;
                    });
                }
                else
                {
                    DisplayErrorMessage("Le bon de commande et le procès verbal sont obligatoires !");
                }
            }));
            #endregion

            #region -- Commande qui permet de valider la simulation --
            validateCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                listContacts.Clear();

                //// -- Populate local list --
                //foreach (Contact contact in ListOfContacts)
                //    listContacts.Add(contact.FullName);

                IsStolenMachineVisible = Visibility.Collapsed;
                isSendMailVisible      = Visibility.Visible;
            }));
            #endregion

            #region -- Commande qui permet de parcourir pour chercher un Bon de commande --
            broserBcCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                BroswerBcFileName();
            }));
            #endregion

            #region -- Commande qui permet de parcourir pour chercher un proces verbal --
            broserPvCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                BroswerPvFileName();
            }));
            #endregion

            #region -- Commande qui permet d'effectuer les recherches --
            adminSearchCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                if (string.IsNullOrWhiteSpace(SearchString))
                {
                    SimulationsList.Clear();
                    foreach (var item in History)
                    {
                        SimulationsList.Add(item);
                    }
                }
                else
                {
                    var uSearch = SearchString.ToUpper();

                    List <Historic> searchResults = History.Where(t =>
                                                                  t.Customer_ID.Contains(uSearch) ||
                                                                  t.Customer_Name.ToUpper().Contains(uSearch) ||
                                                                  t.DateReception.ToString("dd/MM/yyyy").Contains(uSearch)).ToList();

                    SimulationsList.Clear();
                    foreach (var item in searchResults)
                    {
                        SimulationsList.Add(item);
                    }
                }
            }));
            #endregion

            #region -- Commande qui permet aux TS d'effectuer les recherches --
            customerSearchCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                if (string.IsNullOrWhiteSpace(SearchString))
                {
                    CustomerParkList.Clear();
                    foreach (var item in machineList)
                    {
                        CustomerParkList.Add(item);
                    }
                    // -- Display Customer park list in CollectionViewSource --
                    MachinesView = CollectionViewSource.GetDefaultView(CustomerParkList);
                    // -- Clear collection view --
                    MachinesView.GroupDescriptions.Clear();
                    MachinesView.GroupDescriptions.Add(new PropertyGroupDescription("Designation"));
                }
                else
                {
                    var uSearch = SearchString.ToUpper();
                    List <Machine> searchResults = CustomerParkList.Where(t =>
                                                                          t.Code_Article.Contains(uSearch) ||
                                                                          t.Designation.ToUpper().Contains(uSearch) ||
                                                                          t.Serial.Contains(uSearch)).ToList();

                    CustomerParkList.Clear();
                    foreach (var item in searchResults)
                    {
                        CustomerParkList.Add(item);
                    }

                    // -- Display Customer park list in CollectionViewSource --
                    MachinesView = CollectionViewSource.GetDefaultView(CustomerParkList);
                    // -- Clear collection view --
                    MachinesView.GroupDescriptions.Clear();
                    MachinesView.GroupDescriptions.Add(new PropertyGroupDescription("Designation"));
                }
            }));
            #endregion

            #region -- Commande qui permet de passer de la page envoi du mail à celle qui resume et donne le montant dû --
            backToResume = new DelegateCommand(new System.Action <object>((arg) =>
            {
                // -- Manage window's visibility --
                isSendMailVisible = Visibility.Collapsed;
                IsCustomerStolensMachinesVisible = Visibility.Visible;
                IsStolenMachineVisible           = Visibility.Visible;

                // -- Clear form --
                BcFileName = string.Empty;
                PvFileName = string.Empty;
                Comments   = string.Empty;
                IsFormOk   = false;
            }));
            #endregion

            #region -- Admin Select customer machine in SimulationList --
            SelectedCommand = new DelegateCommand((x) =>
            {
                var val          = x as Historic;
                int idHistorique = val.SimulationId;

                // -- récupérer documents du serveur !!!!!! --
                Historic simulation = SimulationsList.Where(t => t.SimulationId.Equals(idHistorique)).FirstOrDefault();

                if (simulation.SimulationMachineList != null)                  // && simulation.SimulationMachineList != null
                {
                    convertDataTable = ConvertToDataTable(simulation.SimulationMachineList, new string[] { "Code_Article", "Designation", "Duree_Contrat", "Quantite", "Date_Debut_Contrat", "Serial" });

                    #region -- EXCEL --
                    excelUtility = new ExcelUtlity();

                    // -- Choose excel directory --
                    System.Windows.Forms.FolderBrowserDialog FBD = new System.Windows.Forms.FolderBrowserDialog();
                    FBD.Description         = "Choississez le dossier de destination ! ";
                    FBD.ShowNewFolderButton = false;
                    if (FBD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        //string f = FBD.SelectedPath;
                        excelFileName = Path.Combine(FBD.SelectedPath, "ExcelData.xlsx");

                        // -- Define dynamic Excel report title --
                        string reportType = "List de machines du client : " + simulation.Customer_Name;

                        Task.Run(() =>
                        {
                            // -- Show loading page --
                            IsLoadingWindowVisible = Visibility.Visible;
                            // -- Export la liste des machines de cette simulation au format excel --
                            excelUtility.WriteDataTableToExcel(convertDataTable, excelFileName, "Customer Tools", reportType, simulation.OffrePrix, simulation.Customer_Name, userMail);
                            DisplayMessage("La liste des machines de cette simulations a été exportée ici : " + excelFileName);
                            // -- Show loading page --
                            IsLoadingWindowVisible = Visibility.Collapsed;
                        });
                    }
                    #endregion
                }
            });
            #endregion

            #region -- Open send list to customer form --
            openSendToCustomerForm = new DelegateCommand((x) =>
            {
                // -- Clear search string --
                SearchString = string.Empty;

                // -- Manage visibility --
                IsSendSimulToCustomerVisible     = Visibility.Visible;
                IsCustomerStolensMachinesVisible = Visibility.Collapsed;

                //// Bruno !!
                //Task.Run(() =>
                //{
                //    // - Récupérer montant restant -
                //    // To do...
                //    IsLoadingWindowVisible = Visibility.Visible;
                //    //System.Threading.Thread.Sleep(3000);

                //    IsLoadingWindowVisible = Visibility.Collapsed;
                //    IsSendSimulToCustomerVisible = Visibility.Visible;
                //    IsCustomerStolensMachinesVisible = Visibility.Collapsed;
                //});
            });
            #endregion

            #region -- Commande qui permet de passer d'envoyer la liste des machine et le montant restant dû au client --
            sendToCustomerCommand = new DelegateCommand(new System.Action <object>((arg) =>
            {
                //--  --.
                pdfUtility = new PdfUtitlity();
                // -- Export PDf file and sent to custmer --
                convertDataTable = ConvertToDataTable(CustomerStolenToolsList, new string[] { "Code_Article", "Designation", "Serial", "Duree_Contrat" });

                // -- Send to customer --
                if (Regex.IsMatch(ContactEmail.Trim(), @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"))
                {
                    if (pdfUtility.ConvertToPDF(convertDataTable, userMail, CustomerName, SelectedReportType, AmountToBePaid))
                    {
                        Task.Run(() =>
                        {
                            #region -- Create template and send --
                            string template             = @"T:\MKI-SOFTWARE\Project\Simulateur de vol\Template\MailTemplate.htm";
                            string CustomerHtmlTemplate = File.ReadAllText(template, Encoding.Default);
                            CustomerHtmlTemplate        = CustomerHtmlTemplate.Replace("{A}", SelectedReportType);
                            CustomerHtmlTemplate        = CustomerHtmlTemplate.Replace("{B}", userLogin);
                            CustomerHtmlTemplate        = CustomerHtmlTemplate.Replace("{C}", CustomerName);
                            CustomerHtmlTemplate        = CustomerHtmlTemplate.Replace("{E}", CommentForCustomer);

                            string sujet   = "Simulation vous permettant d'établir un bon de commande.";
                            string pdfFile = AppDomain.CurrentDomain.BaseDirectory + "\\CustomerStolenMachines.pdf";

                            // -- Call send mail function --
                            sendMail.SendMailToCustomer(ContactEmail, pdfFile, sujet, userMail, userMail, CustomerHtmlTemplate);
                            #endregion

                            DisplayMessage("La liste des machines déclarées " + reportSelectType + "\n par le client : " + CustomerName + " lui a été envoyée !");

                            // -- Manage window's Visibility --
                            IsSendSimulToCustomerVisible     = Visibility.Collapsed;
                            IsCustomerStolensMachinesVisible = Visibility.Visible;

                            // -- Clear form --
                            ContactEmail       = string.Empty;
                            CommentForCustomer = string.Empty;
                            IsFormOk           = false;
                        });
                    }
                }
                else
                {
                    DisplayErrorMessage("Vous devez saisir une adresse email valide !");
                }
            }));
            #endregion

            #region -- Commande qui permet de passer de la page envoi du mail à celle qui resume et donne le montant dû --
            backStolenLMachines = new DelegateCommand(new Action <object>((arg) =>
            {
                IsCustomerStolensMachinesVisible = Visibility.Visible;
                IsSendSimulToCustomerVisible     = Visibility.Collapsed;

                // -- Clear form --
                ContactEmail       = string.Empty;
                CommentForCustomer = string.Empty;
                IsFormOk           = false;
            }));
            #endregion
        }