private void btnStart_Click(object sender, EventArgs e)
        {
            string   IdTestata     = "PRODOTTIFINITI";
            string   codiceTestata = "*** PRODOTTI FINITI ***";
            DateTime DataFine      = dtDataFine.Value;

            if (btnStart.Text == etichettaStart)
            {
                if (!chkProdottiFiniti.Checked && ddlInventario.SelectedIndex == -1)
                {
                    MessageBox.Show("Selezionare un inventario", "ATTENZIONE", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (!chkProdottiFiniti.Checked)
                {
                    Testata testata = (Testata)ddlInventario.SelectedItem;
                    IdTestata     = testata.IdInventarioT;
                    DataFine      = testata.DataFine;
                    codiceTestata = testata.Codice;
                }
                _start = DateTime.Now;

                btnStart.Text = etichettaStop;

                if (_bgwCosto.IsBusy != true)
                {
                    // Start the asynchronous operation.
                    CostiDTO dto = new CostiDTO();
                    dto.IdDTestata                = IdTestata;
                    dto.DataFine                  = DataFine;
                    dto.consideraTutteLeFasi      = chkConsideraTutteLeFasi.Checked;
                    dto.consideraListiniTopFinish = chkVenditaTopFinish.Checked;
                    dto.usaDiBaNonDiDefault       = chkUsaDiBaNonDefault.Checked;
                    dto.tuttiProdottiFiniti       = chkProdottiFiniti.Checked;
                    dto.consideraInvatrio2020     = chkInventario2020.Checked;

                    _bgwCosto.RunWorkerAsync(dto);
                    return;
                }
            }
            else
            {
                if (_bgwCosto.WorkerSupportsCancellation == true && _bgwCosto.IsBusy)
                {
                    // Cancel the asynchronous operation.
                    _bgwCosto.CancelAsync();
                }
                btnStart.Text = etichettaStart;
            }
        }
        private void _bgwCosto_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            CostiDTO         dto    = (CostiDTO)e.Argument;

            string   IdTestata     = dto.IdDTestata;
            DateTime DataFine      = dto.DataFine;
            string   codiceTestata = dto.CodiceTestata;

            DiBa diba = new DiBa();

            worker.ReportProgress(0, string.Format("Cancella costi vecchi inventario {0} del {1}", codiceTestata, DataFine.ToShortDateString()));

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            diba.DeleteCostiArticoli(IdTestata);

            worker.ReportProgress(0, "Carica anagrafica articoli");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            diba.FillMAGAZZ();

            if (dto.consideraInvatrio2020)
            {
                worker.ReportProgress(0, "Carica inventario 2020");
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                diba.FillINVENTARIO_2020();
            }

            worker.ReportProgress(0, "Carica listini acquisto");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            diba.FillUSR_LIS_ACQ();

            if (dto.consideraListiniTopFinish)
            {
                worker.ReportProgress(0, "Carica listini vendita");
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                diba.FillUSR_LIS_VEN();
            }

            worker.ReportProgress(0, "Carica fasi");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            diba.FillTABFAS();

            worker.ReportProgress(0, "Carica listino fasi");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            diba.FillUSR_LIS_FASE();

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            worker.ReportProgress(0, "Carica TDIBA");
            diba.CaricaTDiba();

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            worker.ReportProgress(0, "Carica TDIBA DEFAULT");
            diba.CaricaTDibaDefaut();

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            worker.ReportProgress(0, "Carica RDIBA");
            diba.CaricaRDiba();

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            if (!dto.tuttiProdottiFiniti)
            {
                worker.ReportProgress(0, string.Format("Carica INVENTARIOD PER L'INVENTARIO {0}", codiceTestata));
                diba.FillUSR_INVENTARIOD(IdTestata);
            }
            else
            {
                string anno = "2020";
                worker.ReportProgress(0, string.Format("Carica VENDITE l'anno {0}", anno));
                diba.FillUSR_VENDITED(anno);
            }

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            worker.ReportProgress(diba.CostiDaCalcolare(dto.tuttiProdottiFiniti), string.Format("Prodotti Finiti: {0}", diba.CostiDaCalcolare(dto.tuttiProdottiFiniti)));

            worker.ReportProgress(0, "Inizio Calcolo Costi");
            diba.CalcolaCostiArticolo(IdTestata, DataFine, worker, e, dto.consideraTutteLeFasi, dto.consideraListiniTopFinish, dto.usaDiBaNonDiDefault, dto.tuttiProdottiFiniti, dto.consideraInvatrio2020);
            worker.ReportProgress(diba.CostiDaCalcolare(dto.tuttiProdottiFiniti), string.Format("Salvataggio dati in corso...", diba.CostiDaCalcolare(dto.tuttiProdottiFiniti)));
            diba.SalvaCostiArticolo();
        }
        private void _bgwCostoGalvanica_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            CostiDTO         dto    = (CostiDTO)e.Argument;

            string   IdTestata     = dto.IdDTestata;
            DateTime DataFine      = dto.DataFine;
            string   codiceTestata = dto.CodiceTestata;

            DiBa diba = new DiBa();

            worker.ReportProgress(0, string.Format("Cancella costi galvanica vecchi "));

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            diba.DeleteCostiGalvanica();

            worker.ReportProgress(0, "Carica anagrafica articoli");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            diba.FillMAGAZZ();

            worker.ReportProgress(0, "Carica listini acquisto");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            diba.FillUSR_LIS_ACQ();

            worker.ReportProgress(0, "Carica listini vendita");
            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            diba.FillUSR_LIS_VEN();


            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            worker.ReportProgress(0, "Carica TDIBA");
            diba.CaricaTDiba();

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            worker.ReportProgress(0, "Carica TDIBA DEFAULT");
            diba.CaricaTDibaDefaut();

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            worker.ReportProgress(0, "Carica RDIBA");
            diba.CaricaRDiba();

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            worker.ReportProgress(0, string.Format("Carica INVENTARIOD PER L'INVENTARIO {0}", codiceTestata));
            diba.FillUSR_INVENTARIOD(IdTestata);

            if (worker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }

            worker.ReportProgress(diba.CostiDaCalcolare(true), string.Format("Prodotti Finiti: {0}", diba.CostiDaCalcolare(true)));

            worker.ReportProgress(0, "Inizio Calcolo Costi Galvanica");
            diba.CalcolaCostiGalvanica(DataFine, worker, e);
            worker.ReportProgress(diba.CostiDaCalcolare(true), string.Format("Salvataggio dati in corso...", diba.CostiDaCalcolare(true)));
            diba.SalvaCostiGalvanica();
        }