private string Validate_Lot(WadaGhata _WadaGhata)
        {
            string message = "";
            bool flag = false;

            // for Lot
            flag = WadaGhata_DA.IsLotBeforeWadaGhata(_WadaGhata);

            if (!flag)
            {
                message = "-1";
            }
            return message;
        }
        public string Update(int Id, int Lot1, int Pcs, float Sqft, int Lot2, int Pcs2, float Sqft2, string Description, string Date)
        {
            WadaGhata _WadaGhata = new WadaGhata();

            _WadaGhata.Id = Id;
            _WadaGhata.Lot_1 = Lot1;
            if (Lot2 != -1)
            {
                _WadaGhata.Lot_2 = Lot2;
                _WadaGhata.Pcs2 = Pcs2;
                _WadaGhata.Sqft2 = Sqft2;
            }
            _WadaGhata.Pcs = Pcs;
            _WadaGhata.Sqft = Sqft;
            _WadaGhata.Date = Convert.ToDateTime(Date);
            _WadaGhata.Description = Description;

            #region Cutting
            Cutting _Cutting = new Cutting();

            _Cutting.Lot_1 = Lot1;
            if (Lot2 != -1)
            {
                _Cutting.Lot_2 = Lot2;
                _Cutting.Pcs2 = Pcs2;
                _Cutting.Sqft2 = Sqft2;
            }
            _Cutting.Pcs = Pcs;
            _Cutting.Sqft = Sqft;

            #endregion

            string msg = Validate_Lot(_WadaGhata);
            if (msg == "")
            {
                DataTable dt = WadaGhata_DA.Get_WadaGhata_By_ID(Id);

                msg = validating_Pcs_Sqft_For_Cutter(ref _Cutting, dt.Rows[0]["Lot_1"].ToString(), dt.Rows[0]["Pcs"].ToString(), dt.Rows[0]["Sqft"].ToString(), dt.Rows[0]["Lot_2"].ToString(), dt.Rows[0]["Pcs2"].ToString(), dt.Rows[0]["Sqft2"].ToString());

                msg += WadaGhata_DA.UpdateWadaGhata(_WadaGhata);

                OldPcsSqftEntry(dt.Rows[0]["Lot_1"].ToString(), dt.Rows[0]["Pcs"].ToString(), dt.Rows[0]["Sqft"].ToString());

                if (dt.Rows[0]["Lot_2"].ToString() != "")
                    OldPcsSqftEntry(dt.Rows[0]["Lot_2"].ToString(), dt.Rows[0]["Pcs2"].ToString(), dt.Rows[0]["Sqft2"].ToString());

                UpdateDeliveryForWadaGhata(Lot1, Pcs, Sqft, Lot2, Pcs2, Sqft2);

                if (Session["User"].ToString().ToUpper() != ("Mustafa Piracha").ToUpper())
                {
                    SendNotification(_WadaGhata, dt);
                }
            }
            return msg;
        }
        private void SendNotification(WadaGhata _WadaGhata, DataTable dt)
        {
            string MessageBody = "Edited By : <b>" + Session["User"].ToString().ToUpper() + "</b><br><br>" + DateTime.UtcNow.AddHours(5) + "<br><h3>Before Edit</h3>";
            string tableStart = "<table style=\" width:100%;border-collapse: collapse;\">";//border: 1px solid #ddd;
            string tableEnd = "</table>";
            string MessageHeader = "<tr>";
            string MessageDataOld = "<tr>";
            string MessageDataNew = "<tr>";

            #region Old Header New

            MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + Convert.ToDateTime(dt.Rows[0]["Date"].ToString()).ToString("MM/dd/yyyy") + "</td>";
            MessageHeader += "<th align=\"left\" style=\"border: 1px solid #ddd;height: 30px;\">" + "Date" + "</th>";
            MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + _WadaGhata.Date.ToString("MM/dd/yyyy") + "</td>";

            if (dt.Rows[0]["Lot_1"].ToString() == "")
                MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + "" + "</td>";
            else
                MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + ((DataTable)Lot_DA.Get_Lot_By_Id(int.Parse(dt.Rows[0]["Lot_1"].ToString()))).Rows[0]["Name"] + "</td>";
            MessageHeader += "<th align=\"left\" style=\"border: 1px solid #ddd;height: 30px;\">" + "Lot" + "</th>";
            if (_WadaGhata.Lot_1 == null)
                MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + "" + "</td>";
            else
                MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + ((DataTable)Lot_DA.Get_Lot_By_Id(int.Parse(_WadaGhata.Lot_1.ToString()))).Rows[0]["Name"] + "</td>";

            MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + dt.Rows[0]["Pcs"] + "</td>";
            MessageHeader += "<th align=\"left\" style=\"border: 1px solid #ddd;height: 30px;\">" + "Pcs" + "</th>";
            MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + _WadaGhata.Pcs + "</td>";

            MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + dt.Rows[0]["Sqft"] + "</td>";
            MessageHeader += "<th align=\"left\" style=\"border: 1px solid #ddd;height: 30px;\">" + "Sqft" + "</th>";
            MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + _WadaGhata.Sqft + "</td>";

            if (dt.Rows[0]["Lot_2"].ToString() == "")
                MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + "" + "</td>";
            else
                MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + ((DataTable)Lot_DA.Get_Lot_By_Id(int.Parse(dt.Rows[0]["Lot_2"].ToString()))).Rows[0]["Name"] + "</td>";
            MessageHeader += "<th align=\"left\" style=\"border: 1px solid #ddd;height: 30px;\">" + "Lot" + "</th>";
            if (_WadaGhata.Lot_2 == null)
                MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + "" + "</td>";
            else
                MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + ((DataTable)Lot_DA.Get_Lot_By_Id(int.Parse(_WadaGhata.Lot_2.ToString()))).Rows[0]["Name"] + "</td>";

            MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + dt.Rows[0]["Pcs2"] + "</td>";
            MessageHeader += "<th align=\"left\" style=\"border: 1px solid #ddd;height: 30px;\">" + "Pcs" + "</th>";
            MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + _WadaGhata.Pcs2 + "</td>";

            MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + dt.Rows[0]["Sqft2"] + "</td>";
            MessageHeader += "<th align=\"left\" style=\"border: 1px solid #ddd;height: 30px;\">" + "Sqft" + "</th>";
            MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + _WadaGhata.Sqft2 + "</td>";

            MessageDataOld += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + dt.Rows[0]["Description"] + "</td>";
            MessageHeader += "<th align=\"left\" style=\"border: 1px solid #ddd;height: 30px;\">" + "Description" + "</th>";
            MessageDataNew += "<td  style=\"border: 1px solid #ddd;font-family:Courier New;height: 50px;\">" + _WadaGhata.Description + "</td>";

            #endregion

            MessageDataOld += "</tr>";
            MessageHeader += "</tr>";
            MessageDataNew += "</tr>";

            MessageBody += tableStart + MessageDataOld + MessageHeader + MessageDataNew + tableEnd + "<h3>After Edit</h3>";
            sendMail.sendEmailMessage(ConfigurationManager.AppSettings["From"], ConfigurationManager.AppSettings["To"], ConfigurationManager.AppSettings["Password"], "Wada Ghata Edit", MessageBody);
        }
        public WadaGhata Get(int Id)
        {
            WadaGhata _WadaGhata = new WadaGhata();

            DataTable dt = WadaGhata_DA.Get_WadaGhata_By_ID(Id);

            foreach (DataRow row in dt.Rows)
            {
                _WadaGhata.Lot_1 = int.Parse(row["Lot_1"].ToString());
                _WadaGhata.Pcs = int.Parse(row["Pcs"].ToString());
                _WadaGhata.Sqft = float.Parse(row["Sqft"].ToString());
                if (row["Lot_2"].ToString() != "")
                    _WadaGhata.Lot_2 = int.Parse(row["Lot_2"].ToString());
                if (row["Pcs2"].ToString() != "")
                    _WadaGhata.Pcs2 = int.Parse(row["Pcs2"].ToString());
                if (row["Sqft2"].ToString() != "")
                    _WadaGhata.Sqft2 = float.Parse(row["Sqft2"].ToString());
                _WadaGhata.Date2 = Convert.ToDateTime(row["Date"].ToString()).ToString("MM/dd/yyyy");
                _WadaGhata.Description = row["Description"].ToString();

            }
            return _WadaGhata;
        }
        private bool Validate_Lot()
        {
            lblMessage.Text = "";
            lblMessage.ForeColor = System.Drawing.Color.Red;
            bool flag = false;

            #region WadaGhata

            WadaGhata _WadaGhata = new WadaGhata();

            _WadaGhata.Lot_1 = int.Parse(ddlLot1.SelectedValue);

            if (divLot2.Visible == true)
            {
                _WadaGhata.Lot_2 = int.Parse(ddlLot2.SelectedValue);
            }
            _WadaGhata.Date = Convert.ToDateTime(txtDate.Text);

            #endregion

            // for Lot
            flag = WadaGhata_DA.IsLotBeforeWadaGhata(_WadaGhata);

            if (!flag)
            {
                lblMessage.Text = "Your Wada Ghata Date is earlier than Lot";
            }
            return flag;
        }
        protected void InsertWadaGhata()
        {
            #region WadaGhata
            WadaGhata _WadaGhata = new WadaGhata();

            _WadaGhata.Lot_1 = int.Parse(ddlLot1.SelectedValue);
            if (ddlLot2.SelectedValue != "")
            {
                _WadaGhata.Lot_2 = int.Parse(ddlLot2.SelectedValue);
                _WadaGhata.Pcs2 = int.Parse(txtPcs2.Text);
                _WadaGhata.Sqft2 = float.Parse(txtSqFt2.Text);
            }
            _WadaGhata.Pcs = int.Parse(txtPcs.Text);
            _WadaGhata.Sqft = float.Parse(txtSqFt.Text);
            _WadaGhata.Date = Convert.ToDateTime(txtDate.Text);
            _WadaGhata.Description = txtDescription.Text;

            _WadaGhata.IsDeleted = false;
            #endregion

            lblMessage.Text += WadaGhata_DA.InsertWadaGhata(_WadaGhata);
            if (!lblMessage.Text.Contains(Constants.ALREADY_EXIST))
            {
                UpdateDeliveryForWadaGhata();
                lblMessage.ForeColor = System.Drawing.Color.Green;

                Response.Write("<script>alert('Inserted Successfully'); document.location='/forms/formWadaGhata';</script>");
            }
            else
            {
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        public static string UpdateWadaGhata(WadaGhata _WadaGhata)
        {
            DbCommand command = Catalog_Access.CreateCommand();
            command.CommandText = "sp_UpdateWadaGhata";

            DbParameter param;

            param = command.CreateParameter();
            param.ParameterName = "@Id";
            param.Value = _WadaGhata.Id;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Date";
            param.Value = _WadaGhata.Date ;
            param.DbType = DbType.Date;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Lot_1";
            param.Value = _WadaGhata.Lot_1;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Pcs";
            param.Value = _WadaGhata.Pcs;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Sqft";
            param.Value = _WadaGhata.Sqft;
            param.DbType = DbType.Decimal;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Lot_2";
            param.Value = _WadaGhata.Lot_2;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Pcs2";
            param.Value = _WadaGhata.Pcs2;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Sqft2";
            param.Value = _WadaGhata.Sqft2;
            param.DbType = DbType.Decimal;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Description";
            param.Value = _WadaGhata.Description;
            param.DbType = DbType.String;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Return";
            param.DbType = DbType.String;
            param.Size = 2;
            param.Direction = ParameterDirection.Output;
            command.Parameters.Add(param);

            Catalog_Access.ExecuteNonQuery(command);
            if (command.Parameters["@Return"].Value.ToString() == Constants.SP_ALREADY_EXIST)
            {
                return Constants.ALREADY_EXIST;
            }
            else
            {
                return Constants.SUCESS_UPDATE;
            }
        }
        public static bool IsLotBeforeWadaGhata(WadaGhata _WadaGhata)
        {
            DbCommand command = Catalog_Access.CreateCommand();
            command.CommandText = "sp_IsLotBeforeCutting";

            DbParameter param;

            param = command.CreateParameter();
            param.ParameterName = "@Lot1";
            param.Value = _WadaGhata.Lot_1;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Lot2";
            param.Value = _WadaGhata.Lot_2;
            param.DbType = DbType.Int32;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Date";
            param.Value = _WadaGhata.Date;
            param.DbType = DbType.Date;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = "@Return";
            param.DbType = DbType.String;
            param.Size = 2;
            param.Direction = ParameterDirection.Output;
            command.Parameters.Add(param);

            Catalog_Access.ExecuteNonQuery(command);

            if (command.Parameters["@Return"].Value.ToString() == "1")
            {
                return true;
            }
            else
            {
                return false;
            }
        }