using System;
using System.Data;
using System.Data.SqlClient;
//using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
///
/// Summary description for TellimusteHaldus
///
public class TellimusteHaldus
{
SqlConnection yhendus = new SqlConnection(
ConfigurationManager.ConnectionStrings[
"yhendusTekst"].ConnectionString);
ToodeteHaldus t = new ToodeteHaldus();
public TellimusteHaldus()
{
yhendus.Open();
}
public void SalvestaTellimus(DataTable ostukorv,
string KasutajaNimi) {
int TellimuseNR = LooTellimus(
new KasutajaProvider().KysiKasutajaID(KasutajaNimi));
foreach (DataRow tooterida in ostukorv.Rows){
SalvestaTellimuseToode(tooterida, TellimuseNR);
}
}
void SalvestaTellimuseToode(DataRow tooterida,
int TellimuseNR) {
int VanaTooteID = t.LeiaVanaToode(
Convert.ToInt32(tooterida["TooteID"]));
SqlCommand cm=new SqlCommand("INSERT INTO TellimusedTooted_tbl "+
" (TellimuseID, VanaTooteID, Kogus) "+
"VALUES(@TellimuseID, @VanaTooteID, @Kogus)",yhendus);
cm.Parameters.AddWithValue("@TellimuseID", TellimuseNR);
cm.Parameters.AddWithValue("@VanaTooteID", VanaTooteID);
cm.Parameters.AddWithValue("@Kogus",
Convert.ToInt32(tooterida["Kogus"]));
cm.ExecuteNonQuery();
}
///
/// Loob kliendile tellimuse
/// Aeg pannakse automaatselt
///
/// Klient, kellele tellimus luuakse
/// Loodud tellimuse id
public int LooTellimus(int KliendiID) {
SqlCommand cm = new SqlCommand("INSERT INTO Tellimused_tbl " +
"(KliendiID) VALUES(@KliendiID)", yhendus);
cm.Parameters.AddWithValue("@KliendiID", KliendiID);
cm.ExecuteNonQuery();
SqlCommand cm2 = new SqlCommand("SELECT MAX(TellimuseID) " +
"FROM Tellimused_tbl "+
"WHERE KliendiID=@KliendiID", yhendus);
cm2.Parameters.AddWithValue("@KliendiID", KliendiID);
return Convert.ToInt32(cm2.ExecuteScalar());
}
public int[] TaitmataTellimusteKoodid() {
SqlCommand cm = new SqlCommand(
"SELECT TellimuseID FROM Tellimused_tbl "+
"WHERE Taidetud=0", yhendus
);
SqlDataReader lugeja = cm.ExecuteReader();
System.Collections.Generic.LinkedList hoidla =
new System.Collections.Generic.LinkedList();
while (lugeja.Read()) {
hoidla.AddLast(lugeja.GetInt32(0));
}
lugeja.Close();
int[] tulemused=new int[hoidla.Count];
hoidla.CopyTo(tulemused, 0);
return tulemused;
}
public string TellimuseAndmed(int TellimuseID) {
SqlCommand cm = new SqlCommand(
"SELECT Nimetus, Hind, Kogus "+
"FROM TellimusedTooted_tbl "+
"INNER JOIN VanadTooted_tbl "+
"ON TellimusedTooted_tbl.VanaTooteID=" +
" VanadTooted_tbl.VanaToodeID "+
"WHERE TellimuseID=@TellimuseID ",yhendus
);
cm.Parameters.AddWithValue("@TellimuseID", TellimuseID);
SqlDataReader lugeja = cm.ExecuteReader();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (lugeja.Read()) {
sb.Append(lugeja.GetString(0) + " ");
sb.Append(lugeja.GetDecimal(1) + "kr, ");
sb.Append(lugeja.GetInt16(2) + "tk\n");
}
lugeja.Close();
return sb.ToString();
}
}