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

namespace Transport.DAL
{
    class dal_cost
    {
        public static int SumOfCost(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(cost_price) FROM tbl_cost ");
            }
            else
            {
                SQLQuery = string.Format("SELECT SUM(cost_price) FROM tbl_cost WHERE cost_date >= '{0}' AND cost_date <= '{1}'", start_date, end_date);
            }
            
            com.CommandText = SQLQuery;

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

            object Result = com.ExecuteScalar();

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

            int SumOfCosts = 0;
            try
            {
                SumOfCosts = Convert.ToInt32(Result);
            }
            catch (InvalidCastException e)
            {
                FMessegeBox.FarsiMessegeBox.Show("عدد تبدیل نمی شود");
                SumOfCosts = 0;
            }

            return SumOfCosts;
            
        }
    }
}
