using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using System.Configuration;
using CLS_DL;
using CLS_PL;


namespace CLS_BL
{
    public class BLCompany
    {
        MYSQLDB ObjMain = new MYSQLDB();
        BL_ErrorLog ObjLog = new BL_ErrorLog();
        DataTable _dt = new DataTable();
        int _INS = 0;
        Int32 K = 0;
        Int32 L = 0;
        string Constr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;




        public int TestMaster(PLCOMMON ObjPL)
        {
            try
            {
                MySqlParameter[] _param = { 
                                              //new MySqlParameter("@cm_id ",ObjPL.Comp_ID),
                                              new MySqlParameter("ID",ObjPL.TID),
                                              new MySqlParameter("NAME",ObjPL.TNAME),
                                             new MySqlParameter("PLACE",ObjPL.TPLACE), 
                                       };

                _INS = ObjMain.ExecuteProcedure("SP_TESTMASTER_Insert", _param);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _INS;
        }
        

        /// <summary>
        ///  This funcation uses to Insert the Company Master Record of Company Creation   
        /// </summary>
        ///  cm_id - Primary key  
        /// <param name="ObjPL"></param>
        /// <returns></returns>
     public int InsertCompanyMaster(PLCOMMON ObjPL)
        {
            try
            {
             MySqlParameter[] _param = { 
                                              //new MySqlParameter("@cm_id ",ObjPL.Comp_ID),
                                              new MySqlParameter("@cm_no ", ObjPL.Cm_no),
                                              new MySqlParameter("@cm_name", ObjPL.Comp_Name),
                                              new MySqlParameter("@cm_address ", ObjPL.Comp_Address),
                                              new MySqlParameter("@cm_contactperson ", ObjPL.Comp_ContactPerson),
                                              new MySqlParameter("@cm_contactno ", ObjPL.Cm_ContacNumber),
                                              new MySqlParameter("@cm_email ", ObjPL.Cm_Email),
                                              new MySqlParameter("@cm_alias", ObjPL.Comp_Alias), 
                                                                                   


                                          };
             _INS = ObjMain.ExecuteProcedure("SP_CompanyMaster_Insert", _param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _INS;
        }






        /// <summary>
        ///  
        /// </summary>
        /// <param name="att_dt"></param>
        /// <param name="UserCode"></param>
     public void InsertCompanyMaster1(PLCOMMON ObjPL)
     {


         MySqlConnection con = new MySqlConnection(Constr);
         MySqlCommand sqlcmd = new MySqlCommand("SP_CompanyMaster_Insert", con);
         sqlcmd.CommandType = CommandType.StoredProcedure;
              
         sqlcmd.Parameters.Add(new MySqlParameter("@cm_no ", ObjPL.Cm_no));
         sqlcmd.Parameters.Add(new MySqlParameter("@cm_name",ObjPL.Comp_Name));
         sqlcmd.Parameters.Add(new MySqlParameter("@cm_address ", ObjPL.Comp_Address));
         sqlcmd.Parameters.Add(new MySqlParameter("@cm_contactperson ", ObjPL.Comp_ContactPerson));
         sqlcmd.Parameters.Add(new MySqlParameter("@cm_contactno ",ObjPL.Cm_ContacNumber));
         sqlcmd.Parameters.Add(new MySqlParameter("@cm_email ", ObjPL.Cm_Email));
         sqlcmd.Parameters.Add(new MySqlParameter("@cm_alias", ObjPL.Comp_Alias));
         
         con.Open();
         sqlcmd.ExecuteNonQuery();
         con.Close();

     }




     /// <summary>
     ///  This Funcation Used to Insert the Company Details while we creating the Company 
     /// </summary>
     ///  Here cm_id is the Foreign key reference to the CompanyMaster Table 
     /// <param name="ObjPL"></param>
     /// <returns></returns>
     public int InsertCompanyDetails(PLCOMMON ObjPL)
     {
         try
         {
             MySqlParameter[] _param = { 
                                              new MySqlParameter("@cm_id ", ObjPL.Comp_ID),
                                              new MySqlParameter("@cd_branchname  ", ObjPL.Cd_BranchName),
                                              new MySqlParameter("@cd_branchaddress ", ObjPL.Cd_BranchAddr),
                                              new MySqlParameter("@cd_regdate  ", ObjPL.cd_regdate),
                                              new MySqlParameter("@cd_expdate  ", ObjPL.cd_expdate),
                                              new MySqlParameter("@cd_activate  ", ObjPL.cd_activate),
                                              new MySqlParameter("@cd_branchAlias  ", ObjPL.cd_branchAlias),
                                              new MySqlParameter("@cd_hq ", ObjPL.cd_hq),
                                              new MySqlParameter("@cd_branchid  ", ObjPL.cd_branchid),


                                          };
             _INS = ObjMain.ExecuteProcedure("SP_CustomerDetails_Insert", _param);
         }
         catch (Exception ex)
         {
             throw ex;
         }
         return _INS;
     }



        public DataTable GetCompanyList(string cm_id)
        {
            DataTable DT = new DataTable();

            try
            {
                DT = ObjMain.GetData_DT("SELECT distinct CM.Cm_id ,CM.cm_name FROM portaldb.companymaster as CM inner join portaldb.companydetails as CD ON CM.cm_id=CD.cm_id where CD.status='A'  AND CM.cm_id="+ cm_id + "");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }

            return DT;

        }



        public DataTable BranchHeader(string cm_id)
        {
            DataTable DT = new DataTable();

            try
            {
                DT = ObjMain.GetData_DT("select CD.cd_id as cd_id, CD.cd_Branchname as cd_branchname from  Portaldb.companydetails as CD where CD.cd_hq=1 and CD.cm_id=" + cm_id+";");
            }
            catch (Exception ex)
            {
                ObjLog.Write(ex);
            }

            return DT;

        }




    }
}