using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Configuration;
using MySql.Data.MySqlClient;
using MySql.Data;
using CLS_BL;
using CLS_DL;
using CLS_PL;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Web.UI.DataVisualization.Charting;

namespace SUNWEB
{
    public partial class Inventoycreation : System.Web.UI.Page
    {

        BL_SELECT ObjSel = new BL_SELECT();
        BLCompany ObjBL = new BLCompany();
        PLCOMMON ObjPL = new PLCOMMON();
        BL_USER ObjUserBL = new BL_USER(); //This Object for All User and Page Related fuctions included 
        PLCUSTOMER ObjCustPL = new PLCUSTOMER();
        Int32 K = 0;
        Int32 L = 0;
        Int32 U = 0;
        string ConStr = ConfigurationManager.ConnectionStrings["ConMysql"].ConnectionString;
        //string ConStr_Rds = ConfigurationManager.ConnectionStrings["ConRDS"].ConnectionString;
        string Curr_Date = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        string Curr_DateOnly = System.DateTime.Now.ToString("yyyy-MM-dd");
        
        DataSet ds = new DataSet();
        MYSQLDB ObjMain = new MYSQLDB();
        DataTable DT = new DataTable();
        DataTable Dt2 = new DataTable();
        DataTable DtLicense = new DataTable();
        DataTable DtSuperAdmin = new DataTable();
        DataTable DtMultiBranch = new DataTable();
        DataTable DtNormalUser = new DataTable();
        DataTable DTProductSale = new DataTable();
        string CatValue;
        string Grp1Value;
        string Grp2Value;
        string Grp3Value;
        string Grp4Value;
        string Grp5Value;
        string Grp1_Maxid = string.Empty;
        string Grp2_Maxid = string.Empty;
        string Grp3_Maxid = string.Empty;
        string Grp4_Maxid = string.Empty;
        string Grp5_Maxid = string.Empty;

        string BranchAlias = "saffron1";
        private  string SearchTxt_G1;
        private string SearchTxt_G2;
        private string SearchTxt_G3;
        private string SearchTxt_G4;
        private string SearchTxt_G5;
       
        protected void Page_Load(object sender, EventArgs e)
        {
             txtEffectiveDt.Text = Curr_DateOnly;
             if (!IsPostBack)
             {
                 LoadCategoryList();
                 // Load GroupValues -
                 LoadGrp1();
                 LoadGrp2();
                 LoadGrp3();
                 LoadGrp4();
                 LoadGrp5();
                 
             }
        
        }
        protected void btnSaveCategory_Click(object sender, EventArgs e)
        {

            MySqlConnection Cnn = new MySqlConnection(ConStr);
            Cnn.Open();
            string CateQuery = "select CategoryName from " + BranchAlias + ".inventorycategories where  CategoryName  like '" + txtCategoryName.Text + "%'";
            MySqlCommand Cmd = new MySqlCommand(CateQuery, Cnn);
            CatValue = Convert.ToString(Cmd.ExecuteScalar());
            Grp1_Maxid = ObjSel.GetMax_CatID(BranchAlias);

            if (CatValue != "")
            {

            }
            else
            {
                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                string Flag1 = "00000100";
                string Flag2 = "01000001";
                string Fireindex = "0";
                string Ins_CatQuery = "Insert into " + BranchAlias + ".inventorycategoriesdl(CategoryID,CategoryNumber,CategoryName,Description,DateCreated,Active,Updated,BranchUpdated,Flag1,Flag2,FireIndex)values(" + Grp1_Maxid + "," + Grp1_Maxid + " ,  '" + txtCategoryName.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ",'" + Flag1 + "','" + Flag2 + "','" + Fireindex + "') ";
                K = ObjMain.ExecuteNonQuery(Ins_CatQuery);
                LoadCategoryList();
                txtCategoryName.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Category Name Created ')", true);
            }

        }
       protected void btnSave1_Click(object sender, EventArgs e)
        {

            MySqlConnection Cnn = new MySqlConnection(ConStr);
            Cnn.Open();
            string RdQuery = "select GroupName from " + BranchAlias + ".group1 where  GroupName  like '" + txtGroupName.Text + "%'";
            MySqlCommand Cmd = new MySqlCommand(RdQuery, Cnn);
            Grp1Value = Convert.ToString(Cmd.ExecuteScalar());
            Grp1_Maxid = ObjSel.GetMax_GrpID1(BranchAlias);
            
            if (Grp1Value != "")
            {

            }
            else
            {
                string Active = "-1";
                string Updated = "0";
                string BranchUpdated = "0";
                string Ins_Query = "Insert into " + BranchAlias + ".group1dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp1_Maxid + ",'" + txtGroupName.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," +BranchUpdated +") ";
                LoadGrp1();
                txtGroupName.Text = "";
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);
           }

        }

       protected void btnSave2_Click(object sender, EventArgs e)
       {

           MySqlConnection Cnn = new MySqlConnection(ConStr);
           Cnn.Open();
           string RdQuery1 = "select GroupName from " + BranchAlias + ".group2 where  GroupName  like '" + txtGroupName1 + "%'";
           MySqlCommand Cmd = new MySqlCommand(RdQuery1, Cnn);
           Grp2Value = Convert.ToString(Cmd.ExecuteScalar());
           Grp2_Maxid = ObjSel.GetMax_GrpID2(BranchAlias);

           if (Grp2Value != "")
           {

           }
           else
           {
               string Active = "-1";
               string Updated = "0";
               string BranchUpdated = "0";
               string Ins_Query1 = "Insert into " + BranchAlias + ".group2dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp2_Maxid + ",'" + txtGroupName1.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
               K =  ObjMain.ExecuteNonQuery(Ins_Query1);
               LoadGrp2();
               txtGroupName1.Text = "";
               ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);

           }
       }

       protected void btnSave3_Click(object sender, EventArgs e)
       {

           MySqlConnection Cnn = new MySqlConnection(ConStr);
           Cnn.Open();
           string RdQuery2 = "select GroupName from " + BranchAlias + ".group3 where  GroupName  like '" + txtGroupName3.Text+ "%'";
           MySqlCommand Cmd = new MySqlCommand(RdQuery2, Cnn);
           Grp3Value = Convert.ToString(Cmd.ExecuteScalar());
           Grp3_Maxid = ObjSel.GetMax_GrpID3(BranchAlias);

           if (Grp3Value != "")
           {

           }
           else
           {

               string Active = "-1";
               string Updated = "0";
               string BranchUpdated = "0";
               string Ins_Query2 = "Insert into " + BranchAlias + ".group3dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp3_Maxid + ",'" + txtGroupName3.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
               K = ObjMain.ExecuteNonQuery(Ins_Query2);
               LoadGrp3();
               txtGroupName3.Text = "";
               ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);

           }
       }

       protected void btnSave4_Click(object sender, EventArgs e)
       {

           MySqlConnection Cnn = new MySqlConnection(ConStr);
           Cnn.Open();
           string RdQuery3= "select GroupName from " + BranchAlias + ".group4 where  GroupName  like '" + txtGroupName4.Text + "%'";
           MySqlCommand Cmd = new MySqlCommand(RdQuery3, Cnn);
           Grp4Value = Convert.ToString(Cmd.ExecuteScalar());
           Grp4_Maxid = ObjSel.GetMax_GrpID4(BranchAlias);

           if (Grp4Value != "")
           {

           }
           else
           {

               string Active = "-1";
               string Updated = "0";
               string BranchUpdated = "0";
               string Ins_Query3 = "Insert into " + BranchAlias + ".group4dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp4_Maxid + ",'" + txtGroupName4.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
               K = ObjMain.ExecuteNonQuery(Ins_Query3);
               LoadGrp4();
               txtGroupName4.Text = "";
               ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);

           }
       }

       protected void btnSave5_Click(object sender, EventArgs e)
       {

           MySqlConnection Cnn = new MySqlConnection(ConStr);
           Cnn.Open();
           string RdQuery4 = "select GroupName from " + BranchAlias + ".group5 where  GroupName  like '" + txtGroupName5.Text + "%'";
           MySqlCommand Cmd = new MySqlCommand(RdQuery4, Cnn);
           Grp5Value = Convert.ToString(Cmd.ExecuteScalar());
           Grp5_Maxid = ObjSel.GetMax_GrpID5(BranchAlias);

           if (Grp5Value != "")
           {

           }
           else
           {

               string Active = "-1";
               string Updated = "0";
               string BranchUpdated = "0";
               string Ins_Query4 = "Insert into " + BranchAlias + ".group5dl(GroupID,GroupName,Description,DateCreated,Active,Updated,BranchUpdated)values(" + Grp5_Maxid + ",'" + txtGroupName5.Text + "','" + txtDescription.Text + "','" + Curr_DateOnly + "'," + Active + "," + Updated + "," + BranchUpdated + ") ";
               K = ObjMain.ExecuteNonQuery(Ins_Query4);
               LoadGrp5();
               txtGroupName5.Text = "";
               ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Group Name Created ')", true);
           }
       }

        public void LoadCategoryList()
        {
            cmbCategoryName.DataSource = ObjSel.Inv_CategoryList(BranchAlias);
            cmbCategoryName.DataTextField = "CategoryName";
            cmbCategoryName.DataValueField="CategoryID";
            cmbCategoryName.DataBind();
            cmbCategoryName.Items.Insert(0, "Select Category");
            
        }

        public void LoadGrp1()
        {
            cmbGroup1.DataSource = ObjSel.Inv_Grp1List(BranchAlias);
            cmbGroup1.DataValueField = "Groupid";
            cmbGroup1.DataTextField = "GroupName1";
            cmbGroup1.DataBind();
            cmbGroup1.Items.Insert(0, "");
        
        }

        public void LoadGrp2()
        {
            cmbGroup2.DataSource = ObjSel.Inv_Grp2List(BranchAlias);
            cmbGroup2.DataValueField = "GroupName2";
            cmbGroup2.DataTextField = "Groupid";
            cmbGroup2.DataBind();
            cmbGroup2.Items.Insert(0, "");

        }
        public void LoadGrp3()
        {
            cmbGroup3.DataSource = ObjSel.Inv_Grp3List(BranchAlias);
            cmbGroup3.DataValueField = "GroupName3";
            cmbGroup3.DataTextField = "Groupid";
            cmbGroup3.DataBind();
            cmbGroup3.Items.Insert(0, "");

        }
        public void LoadGrp4()
        {
            cmbGroup4.DataSource = ObjSel.Inv_Grp4List(BranchAlias);
            cmbGroup4.DataValueField = "GroupName4";
            cmbGroup4.DataTextField = "Groupid";
            cmbGroup4.DataBind();
            cmbGroup4.Items.Insert(0, "");
        }
        public void LoadGrp5()
        {
            cmbGroup5.DataSource = ObjSel.Inv_Grp5List(BranchAlias);
            cmbGroup5.DataValueField = "GroupName5";
            cmbGroup5.DataTextField = "Groupid";
            cmbGroup5.DataBind();
            cmbGroup5.Items.Insert(0, "");
        }

        public void SaveInvoice( )
        { 
           MySqlConnection Cnn=new MySqlConnection(ConStr);
            Cnn.Open();
            ObjPL.InventoryId=ObjSel.GetMax_RunningNo(BranchAlias);
            ObjPL.Units="PCS";

            if (txtPrice1.Text == "")
            {
                ObjPL.Price1 = 0;

            }
            else
            {
                ObjPL.Price1 =Convert.ToDecimal(txtPrice1.Text);
            }
            if (txtPrice2.Text == "")
            {
                ObjPL.Price2 = 0;
            }
            else
            {
                ObjPL.Price2 = Convert.ToDecimal(txtPrice2.Text);
            }

            if (txtPrice3.Text == "")
            {
                ObjPL.Price3 = 0;
            }
            else
            {
                ObjPL.Price3 = Convert.ToDecimal(txtPrice3.Text);
            }

            if (txtPrice4.Text == "")
            {
                ObjPL.Price4 = 0;
            }
            else
            {
                ObjPL.Price4 = Convert.ToDecimal(txtPrice4.Text);
            }

            string Query="insert into "+BranchAlias+".inventorydl(InventoryID,BarCode,InventoryName,Description,CategoryID,Class,Unit,Price,"+
           "Price2,Price3,Price4,DateCreated,Group1,Group2,Group3,Group4,Group5,NoPriceShift,EffectiveDate,InvFlag,Active,Updated," +
           "Notes,BranchUpdated,StockCode,Flag1)"+
           "values ("+ObjPL.InventoryId +"  ,'"+txtBarcode.Text+"', '"+txtInventoryName.Text+"','"+txtDescription.Text+"',"+cmbCategoryName.SelectedValue+","+
            "0,'" + ObjPL.Units + "'," + ObjPL.Price1 + "," + ObjPL.Price2 + "," + ObjPL.Price3 + "," + ObjPL.Price4 + ",'" + Curr_DateOnly + "','" + cmbGroup1.SelectedItem.Text + "','" + cmbGroup2.SelectedItem.Text + "'," +
           "'"+cmbGroup3.SelectedItem.Text+"','"+cmbGroup4.SelectedItem.Text+"','"+cmbGroup5.SelectedItem.Text+"',0,'"+txtEffectiveDt.Text+"',0,1,0,'',0,'',0);";
            K=ObjMain.ExecuteNonQuery(Query);  
            // Insert  CostAvg Table 
            string Query1="INSERT INTO "+BranchAlias+".inventorycostaveragedl(`InventoryID`, `PQuantity`, `PAmount`, `CutQuantity`, `CutAmount`, `UnitCost`, `RetailQty`, `RetailAmount`)"+
		   "VALUES ("+ObjPL.InventoryId +",'0.0000','0.0000','0.0000','0.0000','0.0000','0','0.0000');" ;
            L = ObjMain.ExecuteNonQuery(Query1);
            string QueryUPT = "update " + BranchAlias + ".runningnumber set RunningNo=" + ObjPL.InventoryId + "   where ModuleName='Inventory';";
            U = ObjMain.ExecuteNonQuery(QueryUPT);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SaveInvoice();
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "err_msg", "alert(' Inventory Created Successfully ');location.href='Inventoycreation.aspx' ", true);

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Inventoycreation.aspx");
        }

        protected void txtInventoryName_TextChanged(object sender, EventArgs e)
        {

            txtDescription.Text = txtInventoryName.Text;
        }
         
    }
}