protected void RadGrid1_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl userControl = (UserControl)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);
            DateTime TransDateMonth = DateTime.Now;
            try
            {
                TransDateMonth = (DateTime)(userControl.FindControl("dtpTransDate") as RadDatePicker).DbSelectedDate;
            }
            catch (Exception ex)
            {
                string error = ex.Message;
            }
            int transDateMonth = TransDateMonth.Month;
            //perubahan terakhir, autonumber mengikuti bulan transaksi, bukan bulan berjalan
            //string month = DateTime.Now.Month.ToString().Trim();
            string month = transDateMonth.ToString().Trim();
            string autonumber = "";
            if (month.Length == 1)
                month = ('0' + month).Trim();

            Trans Trans = new Trans();
            //Auto Number
            AutoNumberingQuery anQ = new AutoNumberingQuery();
            anQ.SelectAll();
            anQ.Where(anQ.KaMonth == month, anQ.KaYear == DateTime.Now.Year.ToString().Trim());
            AutoNumberingCollection anColl = new AutoNumberingCollection();

            anColl.Load(anQ);
            //KelAnggaranQuery kaQ = new KelAnggaranQuery();
            //kaQ.SelectAll();
            //kaQ.Where(kaQ.KaCode == (userControl.FindControl("cmbTransCODE") as RadComboBox).SelectedValue.Trim());
            //KelAnggaranCollection kaColl = new KelAnggaranCollection();

            //kaColl.Load(kaQ);            

            if (anColl.Count == 0)
            {
                AutoNumberingCollection anCollection = new AutoNumberingCollection();
                AutoNumbering aN = new AutoNumbering();
                aN = anCollection.AddNew();
                aN.KaYear = DateTime.Now.Year.ToString().Trim();
                aN.KaMonth = month;
                aN.LastNumber = 1;
                aN.PrefixNumber = "000";
                //AutoNumbering an = new AutoNumbering { KaYear = DateTime.Now.Year.ToString().Trim(), KaMonth = month, LastNumber = 1, PrefixNumber = "000" };
                autonumber = String.Format("{0}{1}{2}-0001", autonumber, DateTime.Now.Year.ToString().Trim(), month);
                //an.Save();

                try
                {
                    TransCollection transCollection = new TransCollection();
                    Trans = transCollection.AddNew();
                    //Trans.KaCode = (userControl.FindControl("cmbTransCODE") as RadComboBox).SelectedItem.Value;
                    Trans.KaCode = (userControl.FindControl("HiddenField1") as HiddenField).Value;
                    Trans.TransNumber = autonumber.ToUpper();
                    Trans.CekBgNumber = (userControl.FindControl("txtCek") as RadTextBox).Text;
                    //Trans.Amount = Convert.ToDecimal((userControl.FindControl("txtDebitAmount") as RadNumericTextBox).Value - (userControl.FindControl("txtCreditAmount") as RadNumericTextBox).Value);
                    Trans.DebitAmount = Convert.ToDecimal((userControl.FindControl("txtDebitAmount") as RadNumericTextBox).Value);
                    Trans.CreditAmount = Convert.ToDecimal((userControl.FindControl("txtCreditAmount") as RadNumericTextBox).Value);
                    Trans.Descript = (userControl.FindControl("txtDESC") as RadTextBox).Text;
                    Trans.KasId = (userControl.FindControl("cmbKasID") as RadComboBox).SelectedValue.ToUpper();
                    Trans.TransDate = (DateTime)(userControl.FindControl("dtpTransDate") as RadDatePicker).DbSelectedDate;
                    Trans.UserInsert = "toro";
                    Trans.TglInsert = DateTime.Now;
                    Trans.PeriodId = (Int32)HttpContext.Current.Session["_periodeId"];
                    Trans.Isverified = false;
                    Trans.IsClosed = false;

                    using (esTransactionScope scope = new esTransactionScope())
                    {
                        anCollection.Save();
                        transCollection.Save();
                        scope.Complete();
                    }  
                }
                catch (Exception ex)
                {
                    string es = ex.Message;
                }
                finally
                {
                    RadGrid1.Rebind();
                }
            }
            else
                foreach (AutoNumbering aa in anColl)
                {
                    string i = (aa.LastNumber + 1).ToString().Trim();
                    if (i.Length == 1)
                        autonumber = String.Format("{0}{1}{2}-000{3}", autonumber, aa.KaYear, aa.KaMonth, i);
                    if (i.Length == 2)
                        autonumber = String.Format("{0}{1}{2}-00{3}", autonumber, aa.KaYear, aa.KaMonth, i);
                    if (i.Length == 3)
                        autonumber = String.Format("{0}{1}{2}-0{3}", autonumber, aa.KaYear, aa.KaMonth, i);
                    if (i.Length == 4)
                        autonumber = String.Format("{0}{1}{2}-{3}", autonumber, aa.KaYear, aa.KaMonth, i);
                    AutoNumbering anb = new AutoNumbering();
                    AutoNumberingCollection anCollection = new AutoNumberingCollection();
                    anb = anCollection.AddNew();
                    if (anb.LoadByPrimaryKey((long)aa.Id))
                    {
                        anb.LastNumber = aa.LastNumber + 1;                        

                        try
                        {
                            TransCollection transCollection = new TransCollection();
                            Trans = transCollection.AddNew();                            
                            Trans.KaCode = (userControl.FindControl("HiddenField1") as HiddenField).Value;
                            Trans.TransNumber = autonumber.ToUpper();
                            Trans.CekBgNumber = (userControl.FindControl("txtCek") as RadTextBox).Text;                            
                            Trans.DebitAmount = Convert.ToDecimal((userControl.FindControl("txtDebitAmount") as RadNumericTextBox).Value);
                            Trans.CreditAmount = Convert.ToDecimal((userControl.FindControl("txtCreditAmount") as RadNumericTextBox).Value);
                            Trans.Descript = (userControl.FindControl("txtDESC") as RadTextBox).Text;
                            Trans.KasId = (userControl.FindControl("cmbKasID") as RadComboBox).SelectedValue.ToUpper();
                            Trans.TransDate = (DateTime)(userControl.FindControl("dtpTransDate") as RadDatePicker).DbSelectedDate;
                            Trans.UserInsert = "toro";
                            Trans.TglInsert = DateTime.Now;
                            Trans.PeriodId = (Int32)HttpContext.Current.Session["_periodeId"];
                            Trans.Isverified = false;
                            Trans.IsClosed = false;

                            anb.LastNumber = aa.LastNumber + 1;

                            using (esTransactionScope scope = new esTransactionScope())
                            {                                
                                transCollection.Save();
                                scope.Complete();
                            }
                            anb.Save();                            
                        }
                        catch (Exception ex)
                        {
                            string es = ex.Message;
                        }
                        finally
                        {
                            RadGrid1.Rebind();
                        }
                    }
                }

        }
        protected void cmdSave_Click(object sender, EventArgs e)
        {
            //ADD
            Trans trans = new Trans();
            string Descript = txtDescription.Text;
            string CreditAmoun = txtCreditAmount.Text;
            if (Request.QueryString["BKU_ID"] == null)
            {
                int TransDateMonth = ((DateTime)dtpTransDate.SelectedDate).Month;
                string month = TransDateMonth.ToString().Trim();
                string autonumber = "";
                if (month.Length == 1)
                    month = ('0' + month).Trim();

                //Trans Trans = new Trans();
                //Auto Number
                AutoNumberingQuery anQ = new AutoNumberingQuery();
                anQ.SelectAll();
                anQ.Where(anQ.KaMonth == month, anQ.KaYear == DateTime.Now.Year.ToString().Trim());
                AutoNumberingCollection anColl = new AutoNumberingCollection();
                anColl.Load(anQ);
                //New month, new autonumber
                if (anColl.Count == 0)
                {
                    AutoNumberingCollection anCollection = new AutoNumberingCollection();
                    AutoNumbering aN = new AutoNumbering();
                    aN = anCollection.AddNew();
                    aN.KaYear = DateTime.Now.Year.ToString().Trim();
                    aN.KaMonth = month;
                    aN.LastNumber = 1;
                    aN.PrefixNumber = "000";
                    autonumber = String.Format("{0}{1}{2}-0001", autonumber, DateTime.Now.Year.ToString().Trim(), month);
                    try
                    {
                        Decimal DebitAmount = 0;
                        Decimal CreditAmount = 0;
                        TransCollection transColl = new TransCollection();
                        trans = transColl.AddNew();
                        trans.TransNumber = autonumber.ToUpper();
                        trans.KaCode = cmbTransCode.SelectedValue;
                        trans.TransDate = dtpTransDate.SelectedDate;
                        trans.KasId = cmbKasID.SelectedValue;
                        trans.Descript = txtDescription.Text;
                        trans.CekBgNumber = txtCekBG.Text;
                        if (Decimal.TryParse(txtDebitAmount.Text, out DebitAmount))
                            trans.DebitAmount = DebitAmount;
                        else
                            trans.DebitAmount = 0;
                        if (Decimal.TryParse(txtCreditAmount.Text, out CreditAmount))
                            trans.CreditAmount = CreditAmount;
                        else
                            trans.CreditAmount = 0;
                        trans.UserInsert = HttpContext.Current.Session["UserId"].ToString();
                        trans.TglInsert = DateTime.Now;
                        trans.Isverified = false;
                        trans.IsClosed = false;
                        trans.PeriodId = (Int32)HttpContext.Current.Session["_periodeId"];

                        using (esTransactionScope scope = new esTransactionScope())
                        {
                            anCollection.Save();
                            transColl.Save();
                            scope.Complete();
                        }
                    }
                    catch (Exception ex)
                    {
                        string es = ex.Message;
                    }
                }
                // If month already exist, increment autonumber
                else
                {
                    foreach (AutoNumbering aa in anColl)
                    {
                        string i = (aa.LastNumber + 1).ToString().Trim();
                        if (i.Length == 1)
                            autonumber = String.Format("{0}{1}{2}-000{3}", autonumber, aa.KaYear, aa.KaMonth, i);
                        if (i.Length == 2)
                            autonumber = String.Format("{0}{1}{2}-00{3}", autonumber, aa.KaYear, aa.KaMonth, i);
                        if (i.Length == 3)
                            autonumber = String.Format("{0}{1}{2}-0{3}", autonumber, aa.KaYear, aa.KaMonth, i);
                        if (i.Length == 4)
                            autonumber = String.Format("{0}{1}{2}-{3}", autonumber, aa.KaYear, aa.KaMonth, i);
                        AutoNumbering anb = new AutoNumbering();
                        AutoNumberingCollection anCollection = new AutoNumberingCollection();
                        anb = anCollection.AddNew();
                        if (anb.LoadByPrimaryKey((long)aa.Id))
                        {
                            anb.LastNumber = aa.LastNumber + 1;

                            try
                            {
                                Decimal DebitAmount = 0;
                                Decimal CreditAmount = 0;
                                TransCollection transColl = new TransCollection();
                                trans = transColl.AddNew();
                                trans.TransNumber = autonumber.ToUpper();
                                trans.KaCode = cmbTransCode.SelectedValue;
                                trans.TransDate = dtpTransDate.SelectedDate;
                                trans.KasId = cmbKasID.SelectedValue;
                                trans.CekBgNumber = txtCekBG.Text;
                                trans.KasId = cmbKasID.SelectedValue;
                                trans.Descript = txtDescription.Text;
                                if (Decimal.TryParse(txtDebitAmount.Text, out DebitAmount))
                                    trans.DebitAmount = DebitAmount;
                                else
                                    trans.DebitAmount = 0;
                                if (Decimal.TryParse(txtCreditAmount.Text, out CreditAmount))
                                    trans.CreditAmount = CreditAmount;
                                else
                                    trans.CreditAmount = 0;
                                trans.PeriodId = (Int32)HttpContext.Current.Session["_periodeId"];
                                trans.Isverified = false;
                                trans.IsClosed = false;
                                trans.UserInsert = HttpContext.Current.Session["UserId"].ToString();
                                trans.TglInsert = DateTime.Now;

                                anb.LastNumber = aa.LastNumber + 1;

                                using (esTransactionScope scope = new esTransactionScope())
                                {
                                    transColl.Save();
                                    anb.Save();
                                    scope.Complete();
                                }
                                //anb.Save();
                            }
                            catch (Exception ex)
                            {
                                string es = ex.Message;
                            }
                        }
                    }
                }
            }
            //EDIT
            else
            {
                long bkuID = 0;
                Decimal DebitAmount = 0;
                Decimal CreditAmount = 0;
                try
                {
                    if (long.TryParse(txtTransID.Text, out bkuID))
                    {
                        if (trans.LoadByPrimaryKey(bkuID))
                        {
                            trans.TransDate = dtpTransDate.SelectedDate;
                            trans.KaCode = cmbTransCode.SelectedItem.Value;
                            trans.KasId = cmbKasID.SelectedItem.Value;
                            trans.Descript = Descript;
                            trans.CekBgNumber = txtCekBG.Text;
                            if (Decimal.TryParse(txtDebitAmount.Text, out DebitAmount))
                                trans.DebitAmount = DebitAmount;
                            else
                                trans.DebitAmount = 0;
                            if (Decimal.TryParse(txtCreditAmount.Text, out CreditAmount))
                                trans.CreditAmount = CreditAmount;
                            else
                                trans.CreditAmount = 0;
                            trans.UserUpdate = HttpContext.Current.Session["UserId"].ToString();
                            trans.TglUpdate = DateTime.Now;
                            trans.Save();
                        }
                    }
                }
                catch (Exception ex)
                {
                    string es = ex.Message;
                }
            }
            if (Request.QueryString["BKU_ID"] != null)
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('navigateAndRebind');", true);
                HttpContext.Current.Session["CUDMode"] = "1"; // EDIT
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind();", true);
                HttpContext.Current.Session["CUDMode"] = "0"; //ADD
            }
        }