Exemple #1
0
        private void InitializeMyComponents()
        {
            database = new Database(this);
            vinnprodukt = new Vinnprodukt(this);
            openXml = new OpenXML(this);
            processing = new FormProcessing(this);
            salesCodes = new SalesCodes(this);
            tools = new KgsaTools(this);

            worker = new BackgroundWorker();

            webService.ObjectForScripting = new ScriptInterface();
            UpdateServicePage.OnBrowseServicePage += UpdateServicePage_OnRun;
            Log.LogAdded += new EventHandler(LogMessage_LogAdded);

            timerAutoRanking.Tick += timer_Tick;
            timerAutoQuick.Tick += timer_TickQuick;
            timerAutoService.Tick += timerService_Tick;
            timerAutoStore.Tick += timerStore_Tick;
            timerMsgClear.Tick += timer;

            bwSendEmail.DoWork += new DoWorkEventHandler(bwSendEmail_DoWork);
            bwSendEmail.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwSendEmail_RunWorkerCompleted);

            bwPopulateSk.DoWork += new DoWorkEventHandler(bwPopulateSk_DoWork);
            bwPopulateSk.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwPopulateSk_Completed);

            bwImport.DoWork += new DoWorkEventHandler(bwImport_DoWork);
            bwImport.ProgressChanged += new ProgressChangedEventHandler(bwProgressReport_ProgressChanged);
            bwImport.WorkerReportsProgress = true;
            bwImport.WorkerSupportsCancellation = true;
            bwImport.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwImport_Completed);

            bwImportObsolete.DoWork += new DoWorkEventHandler(bwImportObsolete_DoWork);
            bwImportObsolete.ProgressChanged += new ProgressChangedEventHandler(bwProgressReport_ProgressChanged);
            bwImportObsolete.WorkerReportsProgress = true;
            bwImportObsolete.WorkerSupportsCancellation = true;
            bwImportObsolete.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwImportObsolete_Completed);

            bwPDF.DoWork += new DoWorkEventHandler(bwPDF_DoWork);
            bwPDF.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwPDF.WorkerReportsProgress = true;
            bwPDF.WorkerSupportsCancellation = true;
            bwPDF.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwPDF_Completed);

            bwBudgetPDF.DoWork += new DoWorkEventHandler(bwBudgetPDF_DoWork);
            bwBudgetPDF.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwBudgetPDF.WorkerReportsProgress = true;
            bwBudgetPDF.WorkerSupportsCancellation = true;
            bwBudgetPDF.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwPDF_Completed);

            bwOpenPDF.DoWork += new DoWorkEventHandler(bwOpenPDF_DoWork);
            bwOpenPDF.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwOpenPDF.WorkerReportsProgress = true;
            bwOpenPDF.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwOpenPDF_Completed);

            bwAutoRanking.DoWork += new DoWorkEventHandler(bwAutoRanking_DoWork);
            bwAutoRanking.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwAutoRanking.WorkerReportsProgress = true;
            bwAutoRanking.WorkerSupportsCancellation = true;
            bwAutoRanking.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwAutoRanking_Completed);

            bwMacroRanking.DoWork += new DoWorkEventHandler(bwMacroRanking_DoWork);
            bwMacroRanking.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwMacroRanking.WorkerReportsProgress = true;
            bwMacroRanking.WorkerSupportsCancellation = true;
            bwMacroRanking.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwMacroRanking_Completed);

            bwAutoStore.DoWork += new DoWorkEventHandler(bwAutoStore_DoWork);
            bwAutoStore.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwAutoStore.WorkerReportsProgress = true;
            bwAutoStore.WorkerSupportsCancellation = true;
            bwAutoStore.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwAutoStore_Completed);

            bwRanking.DoWork += new DoWorkEventHandler(bwRanking_DoWork);
            bwRanking.ProgressChanged += new ProgressChangedEventHandler(bwProgressReport_ProgressChanged);
            bwRanking.WorkerReportsProgress = true;
            bwRanking.WorkerSupportsCancellation = true;
            bwRanking.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwRanking_Completed);

            bwVinnSelger.DoWork += new DoWorkEventHandler(bwVinnSelger_DoWork);
            bwVinnSelger.ProgressChanged += new ProgressChangedEventHandler(bwProgressReport_ProgressChanged);
            bwVinnSelger.WorkerReportsProgress = true;
            bwVinnSelger.WorkerSupportsCancellation = true;
            bwVinnSelger.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwRanking_Completed);

            bwBudget.DoWork += new DoWorkEventHandler(bwBudget_DoWork);
            bwBudget.ProgressChanged += new ProgressChangedEventHandler(bwProgressReport_ProgressChanged);
            bwBudget.WorkerReportsProgress = true;
            bwBudget.WorkerSupportsCancellation = true;
            bwBudget.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwBudget_Completed);

            bwStore.DoWork += new DoWorkEventHandler(bwStore_DoWork);
            bwStore.ProgressChanged += new ProgressChangedEventHandler(bwProgressReport_ProgressChanged);
            bwStore.WorkerReportsProgress = true;
            bwStore.WorkerSupportsCancellation = true;
            bwStore.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwStore_Completed);

            bwReport.DoWork += new DoWorkEventHandler(bwReport_DoWork);
            bwReport.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwReport.WorkerReportsProgress = true;
            bwReport.WorkerSupportsCancellation = true;
            bwReport.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwReport_Completed);

            bwQuickAuto.DoWork += new DoWorkEventHandler(bwQuickAuto_DoWork);
            bwQuickAuto.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwQuickAuto.WorkerReportsProgress = true;
            bwQuickAuto.WorkerSupportsCancellation = true;
            bwQuickAuto.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwQuickAuto_Completed);

            bwAutoImportService.DoWork += new DoWorkEventHandler(bwAutoImportService_DoWork);
            bwAutoImportService.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwAutoImportService.WorkerReportsProgress = true;
            bwAutoImportService.WorkerSupportsCancellation = true;
            bwAutoImportService.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwAutoImportService_Completed);

            bwImportService.DoWork += new DoWorkEventHandler(bwImportService_DoWork);
            bwImportService.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwImportService.WorkerReportsProgress = true;
            bwImportService.WorkerSupportsCancellation = true;
            bwImportService.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwImportService_Completed);

            bwGraph.DoWork += new DoWorkEventHandler(bwGraph_DoWork);
            bwGraph.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwGraph.WorkerReportsProgress = true;
            bwGraph.WorkerSupportsCancellation = true;
            bwGraph.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwGraph_Completed);

            bwService.DoWork += new DoWorkEventHandler(bwService_DoWork);
            bwService.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwServiceGraph_Completed);

            bwServiceReport.DoWork += new DoWorkEventHandler(bwServiceReport_DoWork);
            bwServiceReport.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwServiceReport_Completed);

            bwUpdateTopGraph.DoWork += new DoWorkEventHandler(bwUpdateTopGraph_DoWork);
            bwUpdateTopGraph.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwUpdateTopGraph_Completed);

            bwUpdateBigGraph.DoWork += new DoWorkEventHandler(bwUpdateBigGraph_DoWork);
            bwUpdateBigGraph.ProgressChanged += new ProgressChangedEventHandler(bwProgressCustom_ProgressChanged);
            bwUpdateBigGraph.WorkerSupportsCancellation = true;
            bwUpdateBigGraph.WorkerReportsProgress = true;
            bwUpdateBigGraph.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwUpdateBigGraph_Completed);

            bwCreateHtml.DoWork += new DoWorkEventHandler(bwCreateHtml_DoWork);
            bwCreateHtml.ProgressChanged += new ProgressChangedEventHandler(bwProgressReport_ProgressChanged);
            bwCreateHtml.WorkerReportsProgress = true;
            bwCreateHtml.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwCreateHtml_Completed);

            bwHentAvdelinger.DoWork += new DoWorkEventHandler(bwHentAvdelinger_DoWork);
            bwHentAvdelinger.WorkerSupportsCancellation = true;
            bwHentAvdelinger.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bwHentAvdelinger_Completed);
        }
Exemple #2
0
        public bool Import(string filename, FormProcessing processing, BackgroundWorker bw = null)
        {
            try
            {
                main.processing = processing;
                if (!File.Exists(filename))
                {
                    Log.n("Service Import: Fant ikke CSV service fil eller ble nektet tilgang. (" + filename + ")", Color.Red);
                    return false;
                }

                var engine = new FileHelperEngine(typeof(csvService));
                engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue;

                processing.SetProgressStyle = ProgressBarStyle.Marquee;
                engine.SetProgressHandler(new ProgressChangeHandler(ReadProgressCSV));
                var resCSV = engine.ReadFile(filename) as csvService[];

                if (engine.ErrorManager.HasErrors)
                    foreach (ErrorInfo err in engine.ErrorManager.Errors)
                    {
                        importReadErrors++;
                        Log.n("Import: Klarte ikke lese linje " + err.LineNumber + ": " + err.RecordString, Color.Red);
                        Log.d("Exception: " + err.ExceptionInfo.ToString());

                        if (importReadErrors > 100)
                        {
                            Log.n("Feil: CSV er ikke en service eksportering eller filen er skadet. (" + filename + ")", Color.Red);
                            return false;
                        }

                    }
                processing.SetProgressStyle = ProgressBarStyle.Continuous;
                int count = resCSV.Length;

                if (count > 0)
                {
                    DateTime dtFirst = DateTime.MaxValue;
                    DateTime dtLast = DateTime.MinValue;

                    string sqlStrAvd = "";
                    for (int i = 0; i < count; i++)
                    {
                        if (main.appConfig.importSetting != "FullFavoritt")
                            if (!sqlStrAvd.Contains(resCSV[i].Avd.ToString())) // Bygg SQL streng av avdelinger som skal slettes.
                                sqlStrAvd = sqlStrAvd + "Avdeling = '" + resCSV[i].Avd.ToString() + "' OR ";

                        DateTime dtTemp = Convert.ToDateTime(resCSV[i].DatoMottatt.ToString());
                        if (DateTime.Compare(dtTemp, dtFirst) < 0)
                            dtFirst = dtTemp;

                        if (DateTime.Compare(dtTemp, dtLast) > 0)
                            dtLast = dtTemp;
                    }

                    int dager = (dtLast - dtFirst).Days;
                    if (dager < 60)
                    {
                        Log.n("Feil: CSV inneholder for kort periode. Velg minst 60 dager perioder.", Color.Red);
                        return false;
                    }
                    if (dtLast < dbServiceDatoTil)
                    {
                        Log.n("Feil: CSV inneholder bare servicer som er eldre enn eksisterende servicer.", Color.Red);
                        return false;
                    }

                    if (main.appConfig.importSetting == "FullFavoritt")
                        foreach (string avdel in FormMain.Favoritter)
                            sqlStrAvd = sqlStrAvd + "Avdeling = '" + avdel + "' OR ";
                    if (sqlStrAvd.Length > 3) // Remove the last "OR"
                        sqlStrAvd = sqlStrAvd.Remove(sqlStrAvd.Length - 3);

                    var command = new SqlCeCommand("DELETE FROM tblService WHERE (DatoMottat >= '" + dtFirst.ToString("yyy-MM-dd") + "') AND (DatoMottat <= '" + dtLast.ToString("yyy-MM-dd") + "') AND (" + sqlStrAvd + ")", main.connection);
                    var result = command.ExecuteNonQuery();
                    Log.d("Slettet " + result + " servicer.");

                    command = new SqlCeCommand("DELETE FROM tblServiceLogg", main.connection);
                    result = command.ExecuteNonQuery();
                    Log.d("Slettet " + result + " logg oppføringer.");

                    Log.n("Prosesserer " + count.ToString("#,##0") + " service oppføringer.. (" + filename + ")");

                    if (main.appConfig.importSetting == "FullFavoritt")
                        foreach (string avdel in FormMain.Favoritter)
                            sqlStrAvd = sqlStrAvd + "Avdeling = '" + avdel + "' OR ";
                    if (sqlStrAvd.Length > 3) // Remove the last "OR"
                        sqlStrAvd = sqlStrAvd.Remove(sqlStrAvd.Length - 3);

                    int ordreNr = 0;
                    int currentRecord = 0;
                    int currentLog = 0;
                    int id = 0;
                    string sql = "INSERT INTO tblService (Avdeling, Ordrenr, Navn, DatoMottat, DatoIarbeid, DatoFerdig, DatoUtlevert, Status, Selgerkode, Verksted) " +
                        "VALUES (@Avdeling, @Ordrenr, @Navn, @DatoMottat, @DatoIarbeid, @DatoFerdig, @DatoUtlevert, @Status, @Selgerkode, @Verksted)";

                    for (int i = 0; i < count; i++)
                    {
                        if (FormMain.Favoritter.Contains(resCSV[i].Avd.ToString()))
                        {
                            if ((int)resCSV[i].Ordrenr != ordreNr)
                            {
                                ordreNr = (int)resCSV[i].Ordrenr;

                                currentRecord++;
                                processing.SetText = "Lagrer servicer " + currentRecord.ToString("#,##0") + "..";

                                if (bw != null)
                                {
                                    if (bw.WorkerReportsProgress)
                                        bw.ReportProgress(i, new StatusProgress(count, "Prosessere servicer oppføringer..", 0, 100));
                                    if (bw.CancellationPending)
                                    {
                                        Log.n("Service importering avbrutt av bruker!", Color.Red);
                                        return false;
                                    }
                                }

                                using (SqlCeCommand cmd = new SqlCeCommand(sql, main.connection))
                                {
                                    cmd.Parameters.AddWithValue("@Avdeling", (int)resCSV[i].Avd);
                                    cmd.Parameters.AddWithValue("@Ordrenr", (int)resCSV[i].Ordrenr);
                                    cmd.Parameters.AddWithValue("@Navn", resCSV[i].Navn);
                                    cmd.Parameters.Add("@DatoMottat", SqlDbType.DateTime).Value = Convert.ToDateTime(resCSV[i].DatoMottatt);
                                    var date = Convert.ToDateTime(resCSV[i].DatoIarbeid);
                                    if (date > FormMain.rangeMin)
                                        cmd.Parameters.Add("@DatoIarbeid", SqlDbType.DateTime).Value = date;
                                    else
                                        cmd.Parameters.Add("@DatoIarbeid", SqlDbType.DateTime).Value = FormMain.rangeMin;
                                    date = Convert.ToDateTime(resCSV[i].DatoFerdig);
                                    if (date > FormMain.rangeMin)
                                        cmd.Parameters.Add("@DatoFerdig", SqlDbType.DateTime).Value = date;
                                    else
                                        cmd.Parameters.Add("@DatoFerdig", SqlDbType.DateTime).Value = FormMain.rangeMin;
                                    date = Convert.ToDateTime(resCSV[i].DatoUtlevert);
                                    if (date > FormMain.rangeMin)
                                        cmd.Parameters.Add("@DatoUtlevert", SqlDbType.DateTime).Value = date;
                                    else
                                        cmd.Parameters.Add("@DatoUtlevert", SqlDbType.DateTime).Value = FormMain.rangeMin;
                                    cmd.Parameters.AddWithValue("@Status", resCSV[i].Status);
                                    cmd.Parameters.AddWithValue("@Selgerkode", resCSV[i].Selgerkode);
                                    cmd.Parameters.AddWithValue("@Verksted", resCSV[i].Verksted);
                                    cmd.CommandType = System.Data.CommandType.Text;
                                    cmd.ExecuteNonQuery();

                                    cmd.CommandText = "SELECT @@IDENTITY";
                                    id = Convert.ToInt32(cmd.ExecuteScalar());
                                }
                                currentLog = 1;
                                ImportAddLog(Convert.ToDateTime(resCSV[i].LoggDato), Convert.ToDateTime(resCSV[i].LoggTid), resCSV[i].LoggKode, resCSV[i].LoggTekst, id, currentRecord, currentLog, main.connection);
                            }
                            else
                            {
                                currentLog++;
                                ImportAddLog(Convert.ToDateTime(resCSV[i].LoggDato), Convert.ToDateTime(resCSV[i].LoggTid), resCSV[i].LoggKode, resCSV[i].LoggTekst, id, currentRecord, currentLog, main.connection);
                                continue;
                            }
                        }
                    }
                }
                return true;
            }
            catch (IOException ex)
            {
                Log.n("CSV var låst for lesing. Forleng ventetid i makro hvis overføringen ikke ble ferdig i tide.", Color.Red);
                Log.Unhandled(ex);
                return false;
            }
            catch (Exception ex)
            {
                FormError errorMsg = new FormError("Feil ved importering av servicer", ex);
                errorMsg.ShowDialog();
                return false;
            }
        }