Torna al Thread
private void btnElabora_Click(object sender, EventArgs e)
{
SqlCommand TmpQrySelect = new SqlCommand();
SqlCommand TmpQryUpdate = new SqlCommand();
SqlDataReader QryReader ;
int updOk;
int Processed;
if (MessageBox.Show("Procedere con il ricalcolo sconti?", "CkSconti", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
TmpQrySelect.Connection = scontiTableAdapter.Connection;
TmpQryUpdate.Connection = scontiTableAdapter.Connection;
TmpQrySelect.Connection.Open();
try
{
TmpQrySelect.CommandText = "Select Sum(Fatrig.Qta3*Fatrig.lordo) TotKg, Fatrig.IdBam, Fattes.IdCliente," +
"Fattes.Anndoc, Fattes.numdoc, Cast(Fattes.DatDoc as DateTime) as DatDoc, " +
"Persone.Descr1 Cliente "+
"From " + Settings.Default.DBRif + ".Dbo.Fatrig Fatrig " +
"Inner join " + Settings.Default.DBRif + ".dbo.Fattes Fattes on fattes.id=Fatrig.idfat " +
"Inner join " + Settings.Default.DBRif + ".dbo.Clienti Clienti on fattes.idCliente=Clienti.id " +
"Inner join " + Settings.Default.DBRif + ".dbo.Persone Persone on Persone.id=Clienti.idPersona " +
"Where Fattes.Stato='P' " +
"and Fatrig.IdBam is not null " +
"and (Cast(Fattes.DatDoc as DateTime) " +
"Between Cast('" + DatIniPicker.Value.ToShortDateString() + "' as DateTime) " +
"And Cast('" + DatFinPicker.Value.ToShortDateString() + "' as DateTime) " +
") " +
"and exists(Select id from clienti Clienti2 where clienti2.idcliente=Fattes.idcliente) " +
"Group By fatrig.IdBam, Fattes.Anndoc, Fattes.numdoc, Fattes.DatDoc, Persone.Descr1, Fattes.IdCliente " +
"Order By Fattes.Anndoc, Fattes.numdoc, Fattes.DatDoc";
TmpQryUpdate.CommandText = "Update " + Settings.Default.DBRif + ".dbo.Fatrig " +
"set Sconto1 = (Case When (Select max(Sconto) " +
"From Sconti " +
"Where SogliaKg <= @TotKg and IdCliente=@IdCliente "+
") is null Then 0 "+
"else (Select max(Sconto) " +
"From Sconti " +
"Where SogliaKg <= @TotKg and IdCliente=@IdCliente " +
") "+
"End ), " +
"Sconto2 = 0, Sconto3 = 0, Sconto4 = 0, "+
"PrezzoN = (Case When (Select max(Sconto) " +
"From Sconti " +
"Where SogliaKg <= @TotKg and IdCliente=@IdCliente " +
") is null Then Prezzo "+
"else Round( Prezzo * ( 1 - (Select max(Sconto)/100 " +
"From Sconti " +
"Where SogliaKg <= @TotKg and IdCliente=@IdCliente " +
") "+
"), 3 "+
") "+
"End ), " +
"ImpTot = Round( "+
"Qta1 * (Case When (Select max(Sconto) " +
"From Sconti " +
"Where SogliaKg <= @TotKg and IdCliente=@IdCliente " +
") is null Then Prezzo " +
"else Round( Prezzo * ( 1 - (Select max(Sconto)/100 " +
"From Sconti " +
"Where SogliaKg <= @TotKg and IdCliente=@IdCliente " +
") " +
"), 2 " +
") " +
"End ), 2 " +
") " +
"Where IdBam = @idbam";
TmpQryUpdate.Parameters.Add("@idbam", SqlDbType.Int);
TmpQryUpdate.Parameters.Add("@IdCliente", SqlDbType.Int);
TmpQryUpdate.Parameters.Add("@TotKg", SqlDbType.Float);
TmpQrySelect.Transaction = TmpQrySelect.Connection.BeginTransaction();
TmpQryUpdate.Transaction = TmpQrySelect.Transaction;
MessageBox.Show("Step 1");
QryReader = TmpQrySelect.ExecuteReader();
MessageBox.Show("Step 2");
try
{
Processed = 0;
dataGridView1.Rows.Clear();
MessageBox.Show("Step 3");
while (QryReader.Read())
{
MessageBox.Show("Step 4");
if ( dataGridView1.Rows.Count == 0 || dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[1].Value.ToString() != QryReader["NumDoc"].ToString() )
{
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[0].Value = QryReader["AnnDoc"].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[1].Value = QryReader["NumDoc"].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[2].Value = QryReader["DatDoc"].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[3].Value = QryReader["Cliente"].ToString();
dataGridView1.Refresh();
}
TmpQryUpdate.Parameters["@idbam"].Value = QryReader["IdBam"].ToString();
TmpQryUpdate.Parameters["@idCliente"].Value = QryReader["IdCliente"].ToString();
TmpQryUpdate.Parameters["@TotKg"].Value = QryReader["TotKg"].ToString();
Processed++;
QryReader.Close();
MessageBox.Show("Step 5");
updOk = TmpQryUpdate.ExecuteNonQuery();
MessageBox.Show("Step 6");
QryReader = TmpQrySelect.ExecuteReader();
MessageBox.Show("Step 7");
for (int UpdOk = 0; UpdOk < Processed; UpdOk++)
{
QryReader.Read();
}
MessageBox.Show("Step 8");
}
QryReader.Close();
TmpQrySelect.Transaction.Commit();
}
catch (Exception err)
{
TmpQryUpdate.Transaction.Rollback();
MessageBox.Show("Si è verificato un errore durante l'aggiornamento dei dati.\r\nVerrà ripristinata la situazione precedente\r\n("+err.Message+")");
}
}
catch (Exception err)
{
MessageBox.Show("Si è verificato un errore nella lettura dei dati.\r\nVerificare la correttezza dei filtri indicati\r\n("+err.Message+")");
}
finally
{
TmpQrySelect.Connection.Close();
MessageBox.Show("Elaborazione terminata");
}
}
}