public void GetPurchaseByID(ref PurchaseRecord vobjPurchaseRecord)
 {
     new ProductPurchaseDB().GetPurchaseByID(ref vobjPurchaseRecord);
 }
 public void AddEditProductPurchase(ref PurchaseRecord vobjPurchaseRecord)
 {
     new ProductPurchaseDB().AddEditProductPurchase(ref vobjPurchaseRecord);
 }
    protected void Page_Load(object sender, EventArgs e)
    {
         
        if (!Page.IsPostBack)
        {
            serversideEvent = Page.ClientScript.GetPostBackEventReference(lnkAddMore, string.Empty);
            PopulateAutoCompleteDataTable();
            PopulateManufacturer();
            txtDateOfPurchase.Value = string.Format("{0:dd/MM/yyyy}", DateTime.Today);
            gobjProduct = new List<PurchasedProduct>();
            Session["PurchasedProduct"] = gobjProduct;

            string strQuery = Request.QueryString["q"];
            if (!string.IsNullOrEmpty(strQuery))
            {
                Dictionary<String, String> objQuery = Common.PopulateDictionaryFromQueryString(strQuery);
                SelectedMode = objQuery["MODE"].ToString();
                //PurchaseID = Convert.ToInt32(objQuery["ID"].ToString());

                if (SelectedMode == Constants.MODE_EDIT)
                {
                    Session["dtProductDetail"] = null;
                    lblHeader.Text = "EDIT | Product Purchase";
                    PurchaseID = Convert.ToInt32(objQuery["ID"].ToString());
                    PurchaseRecord objPurchaseRecord = new PurchaseRecord();
                    objPurchaseRecord.PurchaseID = PurchaseID;
                    new ProductPurchaseBLL().GetPurchaseByID(ref objPurchaseRecord);
                    gobjProduct = objPurchaseRecord.ProductsPurchased;
                    Session["PurchasedProduct"] = gobjProduct;
                    cmbManufacturer.SelectedValue = objPurchaseRecord.ManufacturerID.ToString();
                    txtDateOfPurchase.Value = objPurchaseRecord.PurchaseDate;
                    lblTotalAmount.Text = Math.Round(objPurchaseRecord.TotalBuyingPrice, 2).ToString();
                    lblTotalQuantity.Text = objPurchaseRecord.TotalQty.ToString();
                    txtPurchaseID.Value = PurchaseID.ToString();
                    PopulateGrid();
                }
                else
                {
                    lblHeader.Text = "ADD | Product Purchase";
                    txtPurchaseID.Value = "0";
                }
            }
            else
            {
                Response.Redirect("BuyingInterface.aspx", false);
            }

            //if (null == Session["PurchasedProduct"])
            //{
            //    gobjProduct = new List<PurchasedProduct>();
            //}
            //else
            //{
            //    gobjProduct = (List<PurchasedProduct>)Session["PurchasedProduct"];
            //}
        }
    }
    protected void lnkSave_Click(object sender, EventArgs e)
    {

        if (Session["PurchasedProduct"] != null)
        {
            if (cmbManufacturer.SelectedIndex > 0)
            {
                gobjProduct = (List<PurchasedProduct>)Session["PurchasedProduct"];
                PurchaseRecord objPurchaseRecord = new PurchaseRecord();
                objPurchaseRecord.ManufacturerID = Convert.ToInt32(cmbManufacturer.SelectedValue);
                objPurchaseRecord.PurchaseDate = txtDateOfPurchase.Value;
                objPurchaseRecord.PurchaseID = Convert.ToInt32(txtPurchaseID.Value);
                objPurchaseRecord.ProductsPurchased = gobjProduct;

                new ProductPurchaseBLL().AddEditProductPurchase(ref objPurchaseRecord);

                if (objPurchaseRecord.ReturnValue > 0)
                {
                    Response.Redirect("BuyingInterface.aspx", true);
                }
                else
                {
                    txtProductBarCode.Text = "";
                    Productid.Value = "0";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alertMsg", "alert('Error in saving record!')", true);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "PopulateType", "Populate('2')", true);

                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alertMsg", "alert('Please select manufacturer!')", true);
            }

        }
    }
        public void GetPurchaseByID(ref PurchaseRecord vobjPurchaseRecord)
        {
            try
            {
                object[] mParams = {
                                    new SqlParameter("@PurchaseID", SqlDbType.Int)
                                };

                mParams[0] = vobjPurchaseRecord.PurchaseID;

                PurchasedProduct objPurchasedProduct;
                ProductSize objProductSize = new ProductSize();
                List<PurchasedProduct> lstPurchasedProduct = new List<PurchasedProduct>();
                List<ProductSize> lstProductSize = new List<ProductSize>();
                int intTotalQty = 0;
                decimal dcmTotalBuyingPrice = 0;

                using (IDataReader reader = dBase.ExecuteReader("sprocCS_GetProductPurchaseByID", mParams))
                {
                    while (reader.Read())
                    {
                        objPurchasedProduct = new PurchasedProduct();
                        if (reader["ManufacturerID"] != DBNull.Value)
                            vobjPurchaseRecord.ManufacturerID = Convert.ToInt32(reader["ManufacturerID"]);
                        if (reader["PurchaseDate"] != DBNull.Value)
                            vobjPurchaseRecord.PurchaseDate = Convert.ToDateTime(reader["PurchaseDate"]).ToString("dd/MM/yyyy");
                        if (reader["ProductID"] != DBNull.Value)
                            objPurchasedProduct.ProductID = Convert.ToInt32(reader["ProductID"]);
                        if (reader["ProductName"] != DBNull.Value)
                            objPurchasedProduct.ProductName = Convert.ToString(reader["ProductName"]);
                        if (reader["BuyingPrice"] != DBNull.Value)
                            objPurchasedProduct.BuyingPrice = Convert.ToDecimal(reader["BuyingPrice"]);
                        if (reader["Tax"] != DBNull.Value)
                            objPurchasedProduct.Tax = Convert.ToDecimal(reader["Tax"]);
                        if (reader["Margin"] != DBNull.Value)
                            objPurchasedProduct.Margin = Convert.ToDecimal(reader["Margin"]);
                        if (reader["SellingPrice"] != DBNull.Value)
                            objPurchasedProduct.SellingPrice = Convert.ToDecimal(reader["SellingPrice"]);
                        if (reader["BarCode"] != DBNull.Value)
                            objPurchasedProduct.BarCode = Convert.ToString(reader["BarCode"]);
                        string[] sizes = Convert.ToString(reader["Sizes"]).Split(',');

                        lstProductSize = new List<ProductSize>();
                        intTotalQty  = 0;
                        for (int i = 0; i < sizes.Length; i++)
                        {
                            objProductSize = new ProductSize();
                            string[] sizeqty = sizes[i].Split('~');
                            objProductSize.SizeID = Convert.ToInt32(sizeqty[0]);
                            objProductSize.Quantity = Convert.ToInt32(sizeqty[2]);
                            objProductSize.ProductID = objPurchasedProduct.ProductID;
                            lstProductSize.Add(objProductSize);
                            if (i > 0)
                                objPurchasedProduct.SizeQty += "<br/>";
                            objPurchasedProduct.SizeQty += sizeqty[1] + ": " + objProductSize.Quantity;


                            intTotalQty += objProductSize.Quantity;
                        }
                        objPurchasedProduct.PurchasedQty = lstProductSize;

                        vobjPurchaseRecord.TotalQty += intTotalQty;
                        vobjPurchaseRecord.TotalBuyingPrice += objPurchasedProduct.BuyingPrice * intTotalQty;
                        lstPurchasedProduct.Add(objPurchasedProduct);
         
                    }
                    vobjPurchaseRecord.ProductsPurchased = lstPurchasedProduct;
                }
            }
            catch (Exception  ex)
            {
                
                Common.LogError("CSWeb > Error > " + (new StackTrace()).GetFrame(0).GetMethod().Name, ex.ToString());
            }
        }
        public void AddEditProductPurchase(ref PurchaseRecord vobjProductPurchase)
        {
            try
            {
                StringBuilder strProducts = new StringBuilder();
                StringBuilder strSizes = new StringBuilder();
                strProducts.Append("<data>");
                strSizes.Append("<data>");
                for (int i = 0; i < vobjProductPurchase.ProductsPurchased.Count; i++)
                {
                    strProducts.Append("<products>");
                    strProducts.Append("<productId>" + vobjProductPurchase.ProductsPurchased[i].ProductID + "</productId>");
                    strProducts.Append("<buyingPrice>" + vobjProductPurchase.ProductsPurchased[i].BuyingPrice + "</buyingPrice>");
                    strProducts.Append("<tax>" + vobjProductPurchase.ProductsPurchased[i].Tax + "</tax>");
                    strProducts.Append("<margin>" + vobjProductPurchase.ProductsPurchased[i].Margin + "</margin>");
                    strProducts.Append("<sellingPrice>" + vobjProductPurchase.ProductsPurchased[i].SellingPrice + "</sellingPrice>");
                    strProducts.Append("</products>");

                    for (int j = 0; j < vobjProductPurchase.ProductsPurchased[i].PurchasedQty.Count; j++)
                    {
                        if (vobjProductPurchase.ProductsPurchased[i].PurchasedQty[j].Quantity > 0)
                        {
                            strSizes.Append("<sizes>");
                            strSizes.Append("<productId>" + vobjProductPurchase.ProductsPurchased[i].PurchasedQty[j].ProductID + "</productId>");
                            strSizes.Append("<sizeId>" + vobjProductPurchase.ProductsPurchased[i].PurchasedQty[j].SizeID + "</sizeId>");
                            strSizes.Append("<quantity>" + vobjProductPurchase.ProductsPurchased[i].PurchasedQty[j].Quantity + "</quantity>");
                            strSizes.Append("</sizes>");
                        }
                    }
                }
                strProducts.Append("</data>");
                strSizes.Append("</data>");

                DbCommand objCmd = dBase.GetStoredProcCommand("sprocCS_InsertUpdatePurchase");
                dBase.AddInParameter(objCmd, "@PurchaseID", DbType.Int32, vobjProductPurchase.PurchaseID);
                dBase.AddInParameter(objCmd, "@ManufacturerID", DbType.Int32, vobjProductPurchase.ManufacturerID);
                dBase.AddInParameter(objCmd, "@PurchaseDate", DbType.String, vobjProductPurchase.PurchaseDate.Trim());
                dBase.AddInParameter(objCmd, "@ProductsPurchased", DbType.Xml, strProducts.ToString());
                dBase.AddInParameter(objCmd, "@PurchasedQty", DbType.Xml, strSizes.ToString());
                dBase.AddInParameter(objCmd, "@UpdatedBy", DbType.Int32, 1);
                dBase.AddOutParameter(objCmd, "@MessageID", DbType.Int32, 4);
                dBase.AddOutParameter(objCmd, "@Message", DbType.String, 255);
                dBase.ExecuteNonQuery(objCmd);

                vobjProductPurchase.ReturnValue = (int)dBase.GetParameterValue(objCmd, "@MessageID");
                vobjProductPurchase.ReturnMessage = (string)dBase.GetParameterValue(objCmd, "@Message");

            }
            catch (Exception ex)
            {

                Common.LogError("CSWeb > Error > " + (new StackTrace()).GetFrame(0).GetMethod().Name, ex.ToString());
            }

        }