using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data;
using System.Data.SqlClient;

namespace Transport.DAL
{
    class dal_transport
    {
        public static int SumOfTransport(string start_date, string end_date,bool is_all_date)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = dal_config.connection_string;
            SqlCommand com = new SqlCommand();
            com.Connection = con;
            com.CommandType = CommandType.Text;
            string SQLQuery = "";
            if (is_all_date)
            {
                SQLQuery = string.Format("SELECT SUM(agency_portion) FROM tbl_transport");
            }
            else
            {
                SQLQuery = string.Format("SELECT SUM(agency_portion) FROM tbl_transport WHERE transport_date >= '{0}' AND transport_date <= '{1}'", start_date, end_date);
            }
            
            com.CommandText = SQLQuery;

            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }

            int SumOfTransport =0;

            object Result = com.ExecuteScalar();

            if (Result !=null)
            {
                try
                {
                    SumOfTransport = Convert.ToInt32(Result.ToString());
                }
                catch (InvalidCastException e)
                {
                    SumOfTransport = 0;
                }
            }

            if (con.State != ConnectionState.Closed)
            {
                con.Close();
            }

            return SumOfTransport;

        }
    }
}
