public void btnAddProduct_Click(object sender, EventArgs e)
        {
            PurchaseProduct newprod = new PurchaseProduct();

            newprod.id = Convert.ToInt64(Purchase_ProductID.Text);
            newprod.ShortDescription = Purchase_Product.Text;
            newprod.LongDescription  = Purchase_Description2.Text;
            newprod.UM         = Purchase_Um.Text;
            newprod.Qta        = Convert.ToDouble(Purchase_Qta.Text);
            newprod.UnitPrice  = StaticFunctions.FixDecimal(Purchase_Up.Text);
            newprod.Vat        = (Purchase_Vat.Text.Length > 0) ? Convert.ToDecimal(Purchase_Vat.Text) : 0;
            newprod.ListPrice  = StaticFunctions.FixDecimal(Purchase_Pl.Text);
            newprod.FinalPrice = StaticFunctions.FixDecimal(Purchase_Pf.Text);

            ArrayList np = new ArrayList();

            if (Session["newBillProd"] != null)
            {
                np = (ArrayList)Session["newBillProd"];
            }

            newprod.ObId = np.Count;

            np.Add(newprod);
            Session["newBillProd"] = np;

            RepeaterPurchaseProduct.DataSource = np;
            RepeaterPurchaseProduct.DataBind();
            RepeaterPurchaseProduct.Visible = true;
        }
        public void PurchaseProductCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
            case "DelPurPro":
                Literal   ObjectID = (Literal)e.Item.FindControl("ObjectID");
                ArrayList np       = new ArrayList();
                np = (ArrayList)Session["newBillProd"];
                RemoveAndRestoreProduct(ref np, Convert.ToInt32(ObjectID.Text));
                Session["newBillProd"]             = np;
                RepeaterPurchaseProduct.DataSource = np;
                RepeaterPurchaseProduct.DataBind();
                RepeaterPurchaseProduct.Visible = true;
                break;

            case "ModPurPro":
                ObjectID = (Literal)e.Item.FindControl("ObjectID");
                np       = new ArrayList();
                np       = (ArrayList)Session["newBillProd"];
                PurchaseProduct pProd = (PurchaseProduct)np[Convert.ToInt32(ObjectID.Text)];
                if (pProd.id == 0)
                {
                    EstFreeProduct.Text = pProd.ShortDescription;
                    EstFreeProduct.Text = pProd.LongDescription;
                    EstFreeUm.Text      = pProd.UM;
                    EstFreeQta.Text     = Convert.ToInt32(pProd.Qta).ToString();
                    EstFreeUp.Text      = Convert.ToDecimal(pProd.UnitPrice).ToString();
                    EstFreeVat.Text     = Convert.ToInt32(pProd.Vat).ToString();
                    EstFreePf.Text      = Convert.ToDecimal(pProd.ListPrice).ToString();
                    EstFreePf.Text      = Convert.ToDecimal(pProd.FinalPrice).ToString();
                }
                else
                {
                    Purchase_ProductID.Text    = pProd.id.ToString();
                    Purchase_Product.Text      = pProd.ShortDescription;
                    Purchase_Description2.Text = pProd.LongDescription;
                    Purchase_Um.Text           = pProd.UM;
                    Purchase_Qta.Text          = pProd.Qta.ToString();
                    Purchase_Up.Text           = pProd.UnitPrice.ToString();
                    Purchase_Vat.Text          = pProd.Vat.ToString();
                    Purchase_Pl.Text           = pProd.ListPrice.ToString();
                    Purchase_Pf.Text           = pProd.FinalPrice.ToString();
                }

                RemoveAndRestoreProduct(ref np, Convert.ToInt32(ObjectID.Text));
                Session["newBillProd"]             = np;
                RepeaterPurchaseProduct.DataSource = np;
                RepeaterPurchaseProduct.DataBind();
                RepeaterPurchaseProduct.Visible = true;
                break;
            }
        }
        private void btnAddFreeProductOpp_Click(object sender, EventArgs e)
        {
            ArrayList np = new ArrayList();

            if (Session["newBillProd"] != null)
            {
                np = (ArrayList)Session["newBillProd"];
            }

            PurchaseProduct newprod = new PurchaseProduct();

            newprod.id = 0;
            newprod.ShortDescription = EstFreeProduct.Text;
            newprod.LongDescription  = EstFreeProduct.Text;
            newprod.UM         = EstFreeUm.Text;
            newprod.Qta        = Convert.ToInt32(EstFreeQta.Text);
            newprod.UnitPrice  = StaticFunctions.FixDecimal(EstFreeUp.Text);
            newprod.Vat        = Convert.ToInt32(EstFreeVat.Text);
            newprod.ListPrice  = StaticFunctions.FixDecimal(EstFreePf.Text);
            newprod.FinalPrice = StaticFunctions.FixDecimal(EstFreePf.Text);
            newprod.Reduction  = 0;
            newprod.ObId       = np.Count;
            np.Add(newprod);


            Session["newBillProd"]             = np;
            RepeaterPurchaseProduct.DataSource = np;
            RepeaterPurchaseProduct.DataBind();
            RepeaterPurchaseProduct.Visible = true;

            EstFreeProduct.Text = string.Empty;
            EstFreeUm.Text      = string.Empty;
            EstFreeQta.Text     = string.Empty;
            EstFreeUp.Text      = string.Empty;
            EstFreeVat.Text     = string.Empty;
            EstFreePf.Text      = string.Empty;
        }
        public void RepeaterPurchaseCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
            case "OpenPurchase":
                Literal PurchaseID = (Literal)e.Item.FindControl("PurchaseID");
                DataSet ds         = DatabaseConnection.CreateDataset("SELECT * FROM CRM_BILL WHERE ID=" + int.Parse(PurchaseID.Text));

                TextBox Purchase_BillingDate    = (TextBox)CardPurchase.FindControl("Purchase_BillingDate");
                TextBox Purchase_PaymentDate    = (TextBox)CardPurchase.FindControl("Purchase_PaymentDate");
                TextBox Purchase_ExpirationDate = (TextBox)CardPurchase.FindControl("Purchase_ExpirationDate");
                TextBox Purchase_BillNumber     = (TextBox)CardPurchase.FindControl("Purchase_BillNumber");

                TextBox  Purchase_Note    = (TextBox)CardPurchase.FindControl("Purchase_Note");
                CheckBox Purchase_Payment = (CheckBox)CardPurchase.FindControl("Purchase_Payment");
                TextBox  Purchase_ID      = (TextBox)CardPurchase.FindControl("Purchase_ID");

                Purchase_ID.Text             = PurchaseID.Text;
                Purchase_BillingDate.Text    = Convert.ToDateTime(ds.Tables[0].Rows[0]["BillingDate"]).ToShortDateString();
                Purchase_PaymentDate.Text    = (ds.Tables[0].Rows[0]["PaymentDate"] != DBNull.Value) ? Convert.ToDateTime(ds.Tables[0].Rows[0]["PaymentDate"]).ToShortDateString() : "";
                Purchase_ExpirationDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["ExpirationDate"]).ToShortDateString();
                Purchase_BillNumber.Text     = ds.Tables[0].Rows[0]["BillNumber"].ToString();

                Purchase_Note.Text       = ds.Tables[0].Rows[0]["Note"].ToString();
                Purchase_Payment.Checked = (bool)ds.Tables[0].Rows[0]["Payment"];
                if ((long)ds.Tables[0].Rows[0]["OwnerID"] > 0)
                {
                    this.Purchase_TextboxOwnerID.Text = ds.Tables[0].Rows[0]["OwnerID"].ToString();
                    this.Purchase_TextboxOwner.Text   = DatabaseConnection.SqlScalar("SELECT ISNULL(NAME,'')+' '+ISNULL(SURNAME,'') AS OWNER FROM ACCOUNT WHERE UID=" + ds.Tables[0].Rows[0]["OwnerID"].ToString());
                }
                else
                {
                    this.Purchase_TextboxOwnerID.Text = string.Empty;
                    this.Purchase_TextboxOwner.Text   = string.Empty;
                }

                ds.Clear();

                ds = DatabaseConnection.CreateDataset("SELECT * FROM CRM_BILLROWS WHERE BILLID=" + int.Parse(PurchaseID.Text));
                ArrayList pp = new ArrayList();
                foreach (DataRow d in ds.Tables[0].Rows)
                {
                    PurchaseProduct Pprod = new PurchaseProduct();
                    Pprod.id = Convert.ToInt64(d["ProductID"]);
                    Pprod.ShortDescription = DatabaseConnection.SqlScalar("SELECT SHORTDESCRIPTION FROM CATALOGPRODUCTS WHERE ID=" + d["ProductID"].ToString());
                    Pprod.LongDescription  = d["Description"].ToString();
                    Pprod.UM         = (string)d["UM"];
                    Pprod.Qta        = (int)d["Qta"];
                    Pprod.UnitPrice  = (decimal)d["UnitPrice"];
                    Pprod.Vat        = Convert.ToInt32(d["Tax"]);
                    Pprod.ListPrice  = (decimal)d["ListPrice"];
                    Pprod.FinalPrice = (decimal)d["FinalPrice"];
                    Pprod.ObId       = pp.Count;
                    pp.Add(Pprod);
                }

                Session["newBillProd"] = pp;

                RepeaterPurchaseProduct.DataSource = pp;
                RepeaterPurchaseProduct.DataBind();
                RepeaterPurchaseProduct.Visible = true;

                CardPurchase.Visible     = true;
                RepeaterPurchase.Visible = false;


                break;
            }
        }
        public void FillProduct()
        {
            ArrayList pp = new ArrayList();

            if (!FromQuote)
            {
                DataSet ds = DatabaseConnection.CreateDataset("SELECT * FROM CRM_BILL WHERE ID=" + PurchaseID);
                Purchase_ID.Text             = PurchaseID.ToString();
                Purchase_BillingDate.Text    = Convert.ToDateTime(ds.Tables[0].Rows[0]["BillingDate"]).ToShortDateString();
                Purchase_PaymentDate.Text    = (ds.Tables[0].Rows[0]["PaymentDate"] != DBNull.Value) ? Convert.ToDateTime(ds.Tables[0].Rows[0]["PaymentDate"]).ToShortDateString() : "";
                Purchase_ExpirationDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["ExpirationDate"]).ToShortDateString();
                Purchase_BillNumber.Text     = ds.Tables[0].Rows[0]["BillNumber"].ToString();

                Purchase_Note.Text       = ds.Tables[0].Rows[0]["Note"].ToString();
                Purchase_Payment.Checked = (bool)ds.Tables[0].Rows[0]["Payment"];
                if ((long)ds.Tables[0].Rows[0]["OwnerID"] > 0)
                {
                    this.Purchase_TextboxOwnerID.Text = ds.Tables[0].Rows[0]["OwnerID"].ToString();
                    this.Purchase_TextboxOwner.Text   = DatabaseConnection.SqlScalar("SELECT ISNULL(NAME,'')+' '+ISNULL(SURNAME,'') AS OWNER FROM ACCOUNT WHERE UID=" + ds.Tables[0].Rows[0]["OwnerID"].ToString());
                }
                else
                {
                    this.Purchase_TextboxOwnerID.Text = string.Empty;
                    this.Purchase_TextboxOwner.Text   = string.Empty;
                }

                ds.Clear();
                ds = DatabaseConnection.CreateDataset("SELECT * FROM CRM_BILLROWS WHERE BILLID=" + PurchaseID);

                foreach (DataRow d in ds.Tables[0].Rows)
                {
                    PurchaseProduct Pprod = new PurchaseProduct();
                    Pprod.id = Convert.ToInt64(d["ProductID"]);
                    Pprod.ShortDescription = DatabaseConnection.SqlScalar("SELECT SHORTDESCRIPTION FROM CATALOGPRODUCTS WHERE ID=" + d["ProductID"].ToString());
                    Pprod.LongDescription  = d["Description"].ToString();
                    Pprod.UM         = (string)d["UM"];
                    Pprod.Qta        = (int)d["Qta"];
                    Pprod.UnitPrice  = (decimal)d["UnitPrice"];
                    Pprod.Vat        = Convert.ToInt32(d["Tax"]);
                    Pprod.ListPrice  = (decimal)d["ListPrice"];
                    Pprod.FinalPrice = (decimal)d["FinalPrice"];
                    Pprod.ObId       = pp.Count;
                    pp.Add(Pprod);
                }
            }
            else
            {
                DataSet ds = DatabaseConnection.CreateDataset("SELECT * FROM QUOTE_VIEW WHERE QUOTEID=" + this.QuoteID);
                Purchase_ID.Text             = QuoteID.ToString();
                Purchase_BillingDate.Text    = DateTime.Now.ToShortDateString();
                Purchase_PaymentDate.Text    = String.Empty;
                Purchase_ExpirationDate.Text = String.Empty;
                Purchase_BillNumber.Text     = String.Empty;

                Purchase_Note.Text       = ds.Tables[0].Rows[0]["Description"].ToString();
                Purchase_Payment.Checked = false;
                if ((int)ds.Tables[0].Rows[0]["OwnerID"] > 0)
                {
                    this.Purchase_TextboxOwnerID.Text = ds.Tables[0].Rows[0]["OwnerID"].ToString();
                    this.Purchase_TextboxOwner.Text   = DatabaseConnection.SqlScalar("SELECT ISNULL(NAME,'')+' '+ISNULL(SURNAME,'') AS OWNER FROM ACCOUNT WHERE UID=" + ds.Tables[0].Rows[0]["OwnerID"].ToString());
                }
                else
                {
                    this.Purchase_TextboxOwnerID.Text = string.Empty;
                    this.Purchase_TextboxOwner.Text   = string.Empty;
                }

                ds.Clear();

                DataTable dt = DatabaseConnection.CreateDataset("SELECT * FROM ESTIMATEDROWS WHERE ESTIMATEID=" + QuoteID).Tables[0];

                foreach (DataRow ddr in dt.Rows)
                {
                    if (ddr["CatalogID"].ToString() != "0")
                    {
                        DataSet tempNP = DatabaseConnection.CreateDataset("SELECT * FROM CATALOGPRODUCTS WHERE ID=" + ddr["CatalogID"].ToString());
                        if (tempNP.Tables[0].Rows.Count > 0)
                        {
                            DataRow         pInfo   = tempNP.Tables[0].Rows[0];
                            PurchaseProduct newprod = new PurchaseProduct();
                            newprod.id = Convert.ToInt64(ddr["CatalogID"]);
                            newprod.ShortDescription = pInfo["ShortDescription"].ToString();
                            newprod.LongDescription  = pInfo["LongDescription"].ToString();
                            newprod.UM         = pInfo["Unit"].ToString();
                            newprod.Qta        = (double)ddr["Qta"];
                            newprod.UnitPrice  = (decimal)ddr["Uprice"];
                            newprod.Vat        = (pInfo["Vat"] != DBNull.Value) ? (decimal)pInfo["Vat"] : 0;
                            newprod.ListPrice  = Math.Round(Convert.ToDecimal(newprod.Qta) * newprod.UnitPrice, 2);
                            newprod.FinalPrice = (decimal)ddr["NewUPrice"];
                            try
                            {
                                newprod.Reduction = (int)ddr["reduction"];
                            }
                            catch
                            {
                                newprod.Reduction = 0;
                            }

                            pp.Add(newprod);
                        }
                    }
                    else
                    {
                        PurchaseProduct newprod = new PurchaseProduct();
                        newprod.id = 0;
                        newprod.ShortDescription = ddr["Description"].ToString();
                        newprod.LongDescription  = ddr["Description"].ToString();
                        newprod.UM         = "";
                        newprod.Qta        = (double)ddr["qta"];
                        newprod.UnitPrice  = (decimal)ddr["Uprice"];
                        newprod.Vat        = 0;
                        newprod.ListPrice  = Math.Round(Convert.ToDecimal(newprod.Qta) * newprod.UnitPrice, 2);
                        newprod.FinalPrice = (decimal)ddr["NewUPrice"];
                        try
                        {
                            newprod.Reduction = (int)ddr["reduction"];
                        }
                        catch
                        {
                            newprod.Reduction = 0;
                        }
                        pp.Add(newprod);
                    }
                }
            }

            Session["newBillProd"] = pp;

            RepeaterPurchaseProduct.DataSource = pp;
            RepeaterPurchaseProduct.DataBind();
            RepeaterPurchaseProduct.Visible = true;
        }