using System;
using System.Data;
using System.Data.SqlClient;

namespace TinyHeels.App_Code
{
    class Orders
    {
        DBConnection strcon = new DBConnection();
        public string orderCode { get; set; }
        public DateTime date { get; set; }
        public DateTime date1 { get; set; }
        public DateTime date2 { get; set; }
        public string time { get; set; }
        public double amount { get; set; }
        public double discount { get; set; }
        public double totalAmount { get; set; }
        public double received { get; set; }
        public double change { get; set; }
        public string salesMan { get; set; }
        public string paymentType { get; set; }
        public string note { get; set; }
        //-------------------------------------------
        public void newItem()
        {
            try
            {
                SqlConnection connection = strcon.connect();
                string sql = "INSERT INTO orders(orderCode,date,time,amount,discount,totalAmount,received,change,salesMan,paymentType,note)"
                + "VALUES(@orderCode,@date,@time,@amount,@discount,@totalAmount,@received,@change,@salesMan,@paymentType,@note)";
                SqlCommand cmd = new SqlCommand(sql, connection);
                cmd.Parameters.AddWithValue("@orderCode", this.orderCode);
                cmd.Parameters.AddWithValue("@date", this.date);
                cmd.Parameters.AddWithValue("@time", this.time);
                cmd.Parameters.AddWithValue("@amount", this.amount);
                cmd.Parameters.AddWithValue("@discount", this.discount);
                cmd.Parameters.AddWithValue("@totalAmount", this.totalAmount);
                cmd.Parameters.AddWithValue("@received", this.received);
                cmd.Parameters.AddWithValue("@change", this.change);
                cmd.Parameters.AddWithValue("@salesMan", this.salesMan);
                cmd.Parameters.AddWithValue("@paymentType", this.paymentType);
                cmd.Parameters.AddWithValue("@note", this.note);
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        public void updateForExchange()
        {
            try
            {
                SqlConnection connection = strcon.connect();
                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "update orders set amount=@amount,discount=@discount,totalAmount=@totalAmount,received=@received,change=@change,note=@note where orderCode='" + this.orderCode + "'";
                cmd.Parameters.AddWithValue("@amount", this.amount);
                cmd.Parameters.AddWithValue("@discount", this.discount);
                cmd.Parameters.AddWithValue("@totalAmount", this.totalAmount);
                cmd.Parameters.AddWithValue("@received", this.received);
                cmd.Parameters.AddWithValue("@change", this.change);
                cmd.Parameters.AddWithValue("@note", this.note);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception ex)
            {
                throw new Exception("Could not update updateAmount data\nUpdate Manually");
            }
        }
        public DataTable getOrderById()
        {
            try
            {
                SqlConnection connection = strcon.connect();
                using (SqlDataAdapter ds1 = new SqlDataAdapter("Select * from orders where orderCode='" + this.orderCode + "'", connection))
                {
                    DataSet ds = new DataSet();
                    ds1.Fill(ds);
                    DataTable dt = new DataTable();
                    dt = ds.Tables[0];
                    connection.Close();
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
        public DataTable getOrderBetweenDate()
        {
            try
            {
                SqlConnection connection = strcon.connect();
                using (SqlDataAdapter ds1 = new SqlDataAdapter("Select * from orders where date between '" + this.date1 + "' and '"+date2+"'", connection))
                {
                    DataSet ds = new DataSet();
                    ds1.Fill(ds);
                    DataTable dt = new DataTable();
                    dt = ds.Tables[0];
                    connection.Close();
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
    }
}
