예제 #1
0
        private void FrmModificaPrezziMAMI_Load(object sender, EventArgs e)
        {
            using (FlussoFattureBusiness bFlussoFatture = new FlussoFattureBusiness())
            {
                bFlussoFatture.FillMATERIALIMAMI(_ds);
            }

            dgvMateriali.AutoGenerateColumns = false;
            dgvMateriali.DataSource          = _ds;
            dgvMateriali.DataMember          = _ds.MATERIALIMAMI.TableName;
        }
        private void btnCreaFiles_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                List <string> idTestate = new List <string>();
                foreach (DataGridViewRow riga in dgvRisultati.Rows)
                {
                    object selezione = riga.Cells[SELEZIONATA.Index].Value;
                    if (selezione != null)
                    {
                        bool valore = (bool)selezione;
                        if (valore)
                        {
                            string idTestata = (string)riga.Cells[DOCUMENTO.Index].Value;
                            idTestate.Add(idTestata);
                        }
                    }
                }

                if (idTestate.Count == 0)
                {
                    MessageBox.Show("Nessuna bolla selezionata", "ATTENZIONE", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Filter       = "Excel Files (*.xlsx)|*.xlsx";
                sfd.DefaultExt   = "xlsx";
                sfd.AddExtension = true;
                sfd.FileName     = string.Format("Flusso Fatture {0}.xlsx", DateTime.Today.ToString("dd.MM.yyyy"));
                if (sfd.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                FlussoFattureDS ds = new FlussoFattureDS();

                using (FlussoFattureBusiness bFlussoFatture = new FlussoFattureBusiness())
                {
                    bFlussoFatture.FillMATERIALIMAMI(ds);
                    bFlussoFatture.FillBC_FLUSSO_TESTATA(ds, dtDal.Value, dtAl.Value);
                    bFlussoFatture.FillBC_FLUSSO_DETTAGLIO(ds, dtDal.Value, dtAl.Value);
                }

                string        errori;
                StringBuilder senzaPrezzo = new StringBuilder();
                idTestate = idTestate.Distinct().ToList();
                using (FlussoFattureBusiness bFlussoFatture = new FlussoFattureBusiness())
                {
                    foreach (string fullnumdoc in idTestate)
                    {
                        FlussoFattureDS.BC_FLUSSO_TESTATARow testata = ds.BC_FLUSSO_TESTATA.Where(x => x.FULLNUMDOC == fullnumdoc).FirstOrDefault();
                        if (ds.BC_FLUSSO_DETTAGLIO.Any(x => x.FULLNUMDOC == fullnumdoc && x.PREZZOTOT == 0))
                        {
                            senzaPrezzo.AppendLine(string.Format("{0} non esportata perchè SENZA PREZZO", fullnumdoc));
                        }
                        else
                        {
                            bFlussoFatture.BloccaBolla(fullnumdoc);
                        }
                    }
                }

                ExcelHelper hExcel   = new ExcelHelper();
                byte[]      filedata = hExcel.CreaFlussoFatture(idTestate.Distinct().ToList(), ds, out errori);
                FileStream  fs       = new FileStream(sfd.FileName, FileMode.Create);
                fs.Write(filedata, 0, filedata.Length);
                fs.Flush();
                fs.Close();

                errori = errori + string.Format(" {0}", senzaPrezzo);

                if (errori.Trim().Length > 0)
                {
                    MessageBox.Show(errori.Trim(), "Attenzione", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
                else
                {
                    MessageBox.Show("Operazione conclusa con successo", "OERAZIONE TERMINATA", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MostraEccezione(ex, "Errore in fase di creazione dei file");
            }
            finally { Cursor.Current = Cursors.Default; }
        }