/// <summary> /// This is a global refresher method /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Refresh(object sender, EventArgs e) { decimal netSellingPrice = 0; decimal netDiscountAmount = 0; decimal netAfterDiscountAmount = 0; decimal netTaxAmount = 0; decimal netTotalAmount = 0; if (ViewState["currentTable"] != null) { DataTable currentTable = (DataTable)ViewState["currentTable"]; for (var i = 0; i < currentTable.Rows.Count; i++) { //retrive columns TextBox pName = (TextBox)GridView1.Rows[i].Cells[1].FindControl("ProductName"); TextBox cPrice = (TextBox)GridView1.Rows[i].Cells[1].FindControl("CostPrice"); TextBox sPrice = (TextBox)GridView1.Rows[i].Cells[1].FindControl("SellPrice"); TextBox quantity = (TextBox)GridView1.Rows[i].Cells[1].FindControl("Quantity");; TextBox measurements = (TextBox)GridView1.Rows[i].Cells[1].FindControl("Measurements"); TextBox discountAmount = (TextBox)GridView1.Rows[i].Cells[1].FindControl("DiscountAmount"); TextBox amountAfterDiscount = (TextBox)GridView1.Rows[i].Cells[1].FindControl("AmountAfterDiscount"); TextBox taxAmount = (TextBox)GridView1.Rows[i].Cells[1].FindControl("TaxAmount"); TextBox FinalPrice = (TextBox)GridView1.Rows[i].Cells[1].FindControl("FinalPrice"); // update all columns currentTable.Rows[i]["ProductName"] = pName.Text; currentTable.Rows[i]["Quantity"] = quantity.Text; currentTable.Rows[i]["CostPrice"] = cPrice.Text; currentTable.Rows[i]["SellPrice"] = sPrice.Text; currentTable.Rows[i]["DiscountAmount"] = Math.Round(Convert.ToDecimal(discountAmount.Text)); currentTable.Rows[i]["AmountAfterDiscount"] = Math.Round((Convert.ToDecimal(sPrice.Text) * Convert.ToInt32(quantity.Text)) - Convert.ToDecimal(discountAmount.Text)).ToString(); currentTable.Rows[i]["Measurements"] = measurements.Text; currentTable.Rows[i]["TaxAmount"] = Math.Round(Convert.ToDecimal(taxAmount.Text)); var refreshPriceCalculator = new ManualInvoicPriceCalculate { SellingPrice = Math.Round(Convert.ToDecimal(sPrice.Text)), Quantity = Convert.ToInt32(quantity.Text), DiscountAmount = Math.Round(Convert.ToDecimal(discountAmount.Text)), // give 0 discount initial during first time load TaxAmount = Math.Round(Convert.ToDecimal(taxAmount.Text)), // give 0 tax value amount }; currentTable.Rows[i]["FinalPrice"] = Math.Round(this.FinalPriceByRow(refreshPriceCalculator)); netDiscountAmount = netDiscountAmount + refreshPriceCalculator.DiscountAmount.GetValueOrDefault(); netSellingPrice = netSellingPrice + refreshPriceCalculator.SellingPrice.GetValueOrDefault(); netTotalAmount = netTotalAmount + this.FinalPriceByRow(refreshPriceCalculator); netTaxAmount = netTaxAmount + refreshPriceCalculator.TaxAmount.GetValueOrDefault(); netAfterDiscountAmount = netAfterDiscountAmount + Convert.ToDecimal(currentTable.Rows[i]["AmountAfterDiscount"]); } ViewState["currentTable"] = currentTable; GridView1.DataSource = currentTable; GridView1.DataBind(); this.UpdateFooterRow(netSellingPrice, netDiscountAmount, netAfterDiscountAmount, netTaxAmount, netTotalAmount); } }
private DataTable CreateDataTable(Product product) { DataTable dt = new DataTable(); DataRow dr = null; dt.Columns.Add(new DataColumn("RowNumber", typeof(int))); dt.Columns.Add(new DataColumn("ProductId", typeof(int))); dt.Columns.Add(new DataColumn("ProductName", typeof(string))); dt.Columns.Add(new DataColumn("Quantity", typeof(int))); dt.Columns.Add(new DataColumn("Measurements", typeof(string))); dt.Columns.Add(new DataColumn("CostPrice", typeof(decimal))); dt.Columns.Add(new DataColumn("SellPrice", typeof(decimal))); dt.Columns.Add(new DataColumn("DiscountAmount", typeof(decimal))); dt.Columns.Add(new DataColumn("AmountAfterDiscount", typeof(string))); dt.Columns.Add(new DataColumn("TaxAmount", typeof(decimal))); dt.Columns.Add(new DataColumn("FinalPrice", typeof(decimal))); dr = dt.NewRow(); dr["RowNumber"] = 1; dr["ProductId"] = product.Id; dr["ProductName"] = product.Name; dr["Quantity"] = 1; dr["Measurements"] = product.Measurements; dr["CostPrice"] = product.CostPrice; dr["SellPrice"] = product.SellPrice; dr["DiscountAmount"] = 0; dr["AmountAfterDiscount"] = product.SellPrice; dr["TaxAmount"] = 0; var refreshPriceCalculator = new ManualInvoicPriceCalculate { SellingPrice = product.SellPrice, Quantity = 1, DiscountAmount = 0, // give 0 discount initial during first time load TaxAmount = 0, // give 0 tax value amount AmountAfterDiscount = product.SellPrice // set to sell price }; var finalPrice = Math.Round(this.FinalPriceByRow(refreshPriceCalculator)); dr["FinalPrice"] = finalPrice; dt.Rows.Add(dr); ViewState["currentTable"] = dt; return(dt); }
private decimal FinalPriceByRow(ManualInvoicPriceCalculate manual) { var finalPrice = ((manual.Quantity * manual.SellingPrice) - manual.DiscountAmount.GetValueOrDefault()) + manual.TaxAmount.GetValueOrDefault(); return(finalPrice.GetValueOrDefault()); }
private void CopyPreviousValues(DataTable currentTable, Product product) { DataRow tr = null; if (currentTable.Rows.Count > 0) { tr = currentTable.NewRow(); tr["RowNumber"] = currentTable.Rows.Count + 1; tr["ProductId"] = product.Id; currentTable.Rows.Add(tr); ViewState["currentTable"] = currentTable; for (var i = 0; i < currentTable.Rows.Count; i++) { TextBox pName = (TextBox)GridView1.Rows[i].Cells[1].FindControl("ProductName"); TextBox cPrice = (TextBox)GridView1.Rows[i].Cells[1].FindControl("CostPrice"); TextBox sPrice = (TextBox)GridView1.Rows[i].Cells[1].FindControl("SellPrice"); TextBox quantity = (TextBox)GridView1.Rows[i].Cells[1].FindControl("Quantity");; TextBox measurements = (TextBox)GridView1.Rows[i].Cells[1].FindControl("Measurements"); TextBox discountAmount = (TextBox)GridView1.Rows[i].Cells[1].FindControl("DiscountAmount"); TextBox amountAfterDiscount = (TextBox)GridView1.Rows[i].Cells[1].FindControl("amountAfterDiscount"); TextBox taxAmount = (TextBox)GridView1.Rows[i].Cells[1].FindControl("TaxAmount"); TextBox FinalPrice = (TextBox)GridView1.Rows[i].Cells[1].FindControl("FinalPrice"); if (i < currentTable.Rows.Count - 1) { // copy previous rows pName.Text = currentTable.Rows[i]["ProductName"].ToString(); quantity.Text = currentTable.Rows[i]["Quantity"].ToString(); cPrice.Text = currentTable.Rows[i]["CostPrice"].ToString(); sPrice.Text = currentTable.Rows[i]["SellPrice"].ToString(); discountAmount.Text = currentTable.Rows[i]["DiscountAmount"].ToString(); amountAfterDiscount.Text = currentTable.Rows[i]["AmountAfterDiscount"].ToString(); measurements.Text = currentTable.Rows[i]["Measurements"].ToString(); taxAmount.Text = currentTable.Rows[i]["TaxAmount"].ToString(); FinalPrice.Text = currentTable.Rows[i]["FinalPrice"].ToString(); } else { // no more rows in data Table. currentTable.Rows[i]["ProductName"] = product.Name; currentTable.Rows[i]["Quantity"] = 1; currentTable.Rows[i]["CostPrice"] = product.CostPrice.GetValueOrDefault(); currentTable.Rows[i]["SellPrice"] = product.SellPrice.GetValueOrDefault(); currentTable.Rows[i]["DiscountAmount"] = 0; currentTable.Rows[i]["AmountAfterDiscount"] = product.SellPrice; currentTable.Rows[i]["Measurements"] = product.Measurements; currentTable.Rows[i]["TaxAmount"] = 0; var refreshPriceCalculator = new ManualInvoicPriceCalculate { SellingPrice = product.SellPrice, Quantity = 1, DiscountAmount = 0, // give 0 discount initial during first time load TaxAmount = 0, // give 0 tax value amount AmountAfterDiscount = product.SellPrice // set to sell price }; currentTable.Rows[i]["FinalPrice"] = Math.Round(this.FinalPriceByRow(refreshPriceCalculator)); } ViewState["currentTable"] = currentTable; GridView1.DataSource = currentTable; GridView1.DataBind(); } } else { var dataTable = this.CreateDataTable(product); ViewState["currentTable"] = dataTable; GridView1.DataSource = dataTable; GridView1.DataBind(); } }