Example #1
0
        private void FrmTerm_Load(object sender, EventArgs e)
        {
            Grid.AutoGenerateColumns     = false;
            TxtBasicAmount.BackColor     = SystemColors.Control;
            TxtTotalTermAmount.BackColor = SystemColors.Control;
            DataTable dt = _objSalesBillingTerm.GetTermListForTermCalculation(_Module, ClsGlobal.BranchId, _ProductId);

            dt.Columns.Add("Amount");
            decimal _totalBasicAmt = 0;;

            decimal.TryParse(TxtBasicAmount.Text, out decimal _basicAmt);
            _totalBasicAmt      = _basicAmt;
            dt.DefaultView.Sort = "TermPosition ASC";
            dt = dt.DefaultView.ToTable();
            #region ----------- if exist data update rate percent and manual value in datatable-------
            if (!string.IsNullOrEmpty(_ExistTermData))
            {
                string[] val         = _ExistTermData.Split('|');
                string   sign        = val[0];
                string   sn          = val[1];
                string   ratepercent = val[2];
                string   amount      = val[3];

                string[] arrsign        = sign.Split(',');
                string[] arrsn          = sn.Split(',');
                string[] arrRatepercent = ratepercent.Split(',');
                string[] arrAmount      = amount.Split(',');

                for (int j = 0; j < arrsn.Length; j++)
                {
                    DataRow[] dr1 = dt.Select("TermId='" + arrsn[j] + "'");
                    dr1[0]["TermRate"] = arrRatepercent[j];
                    if (Convert.ToDecimal(arrRatepercent[j]) == 0)
                    {
                        dr1[0]["Amount"] = ClsGlobal.DecimalFormate(Convert.ToDecimal(arrAmount[j]), 1, ClsGlobal._AmountDecimalFormat).ToString();
                    }
                    else
                    {
                        dr1[0]["Amount"] = ClsGlobal.DecimalFormate(0, 1, ClsGlobal._AmountDecimalFormat).ToString();
                    }

                    _ExistTermData = "";
                    dt.AcceptChanges();
                }
            }
            #endregion
            int i = 1;
            #region ----------- calculate term value from datatable DT and update in Grid -------
            foreach (DataRow r in dt.Rows)
            {
                if (dt.Rows.Count > 0)
                {
                    string formula = "";
                    var    index   = Grid.Rows.Add();
                    Grid.Rows[index].Cells["SNo"].Value      = i;
                    Grid.Rows[index].Cells["TermId"].Value   = r["TermId"].ToString();
                    Grid.Rows[index].Cells["TermDesc"].Value = r["TermDesc"].ToString();
                    Grid.Rows[index].Cells["Basis"].Value    = r["Basis"].ToString();
                    Grid.Rows[index].Cells["Sign"].Value     = r["Sign"].ToString();
                    Grid.Rows[index].Cells["TermRate"].Value = ClsGlobal.DecimalFormate(Convert.ToDecimal(r["TermRate"].ToString()), 1, ClsGlobal._AmountDecimalFormat).ToString();
                    decimal.TryParse(r["TermRate"].ToString(), out decimal output);
                    decimal.TryParse(r["Amount"].ToString(), out decimal amt);
                    Grid.Rows[index].Cells["Formula"].Value      = r["Formula"].ToString();
                    Grid.Rows[index].Cells["TermPosition"].Value = r["TermPosition"].ToString();
                    formula = r["Formula"].ToString();
                    if (r["Basis"].ToString().Trim().ToUpper() == "V")
                    {
                        if (amt > 0)
                        {
                            Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(amt, 1, ClsGlobal._AmountDecimalFormat).ToString();
                        }
                        else
                        {
                            if (output > 0)
                            {
                                if (i == 1)
                                {
                                    Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(((_basicAmt / 100) * output), 1, ClsGlobal._AmountDecimalFormat).ToString();
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(formula))
                                    {
                                        decimal _CalCurrentBasicAmt = CalCurrentBasicAmt(i - 1);
                                        Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(((_CalCurrentBasicAmt / 100) * output), 1, ClsGlobal._AmountDecimalFormat).ToString();
                                    }
                                    else
                                    {
                                        DataTable GridTable = ClsGlobal.GridToDataTable(Grid);
                                        DataTable myTable   = new DataTable();
                                        myTable = GridTable.Clone();
                                        decimal _CalCurrentBasicAmt = 0;
                                        _CalCurrentBasicAmt = CalBasicAmt4Formula(formula, GridTable, myTable);
                                        Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(((_CalCurrentBasicAmt / 100) * output), 1, ClsGlobal._AmountDecimalFormat).ToString();
                                    }
                                }
                            }
                            else
                            {
                                Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(0, 1, ClsGlobal._AmountDecimalFormat).ToString();
                            }
                        }
                    }
                    else if (r["Basis"].ToString().Trim().ToUpper() == "Q")
                    {
                        if (amt > 0)
                        {
                            Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(Convert.ToDecimal(r["Amount"].ToString()), 1, ClsGlobal._AmountDecimalFormat).ToString();
                        }
                        else
                        {
                            if (output > 0)
                            {
                                Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate((_Qty * output), 1, ClsGlobal._AmountDecimalFormat).ToString();
                            }
                            else
                            {
                                Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(0, 1, ClsGlobal._AmountDecimalFormat).ToString();
                            }
                        }
                    }
                    else if (r["Basis"].ToString().Trim().ToUpper() == "R")
                    {
                        decimal roundoffVal = Convert.ToDecimal(CalTotalForRoundOff());
                        if (Convert.ToDecimal(roundoffVal.ToString().Split('.').Last()) >= 50)
                        {
                            if (r["Sign"].ToString().Trim() == "+")
                            {
                                decimal a = (1 - Convert.ToDecimal(roundoffVal.ToString().Split('.').Last()) / 100);
                                Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(a, 1, ClsGlobal._AmountDecimalFormat).ToString();
                            }
                            else
                            {
                                Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(0, 1, ClsGlobal._AmountDecimalFormat).ToString();
                            }
                        }
                        else
                        {
                            if (r["Sign"].ToString().Trim() == "-")
                            {
                                decimal b = (Convert.ToDecimal(roundoffVal.ToString().Split('.').Last()) / 100);
                                Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(b, 1, ClsGlobal._AmountDecimalFormat).ToString();
                            }
                            else
                            {
                                Grid.Rows[index].Cells["Amount"].Value = ClsGlobal.DecimalFormate(0, 1, ClsGlobal._AmountDecimalFormat).ToString();
                            }
                        }
                    }
                    i++;
                }
            }
            #endregion
            CalTotal();
            ClsGlobal.DisableGridColumnsSorting(Grid);
        }