private void CheckSubScriptionId(bool IsForward)
        {
            CurrentSubscriptionBilling = null;
            BindingOperations.GetBindingExpression(FirstControl, TextBox.TextProperty).UpdateSource();
            MessageDialog Msg = null;

            try
            {
                bool             IsValid = true;
                string           SId     = "";
                ControlsValidate?Vd      = Commons.ValidateData((Control)FirstControl, ControlsArraySubScriptionId);
                if (Vd != null)
                {
                    IsValid   = false;
                    Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.ForceToSubscriptionId, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                    Msg.Owner = Window.GetWindow(this);
                    Msg.ShowDialog();
                    EmptyControl();
                    FirstControl.Focus();
                    FirstControl.SelectAll();
                }
                if (IsValid == true)
                {
                    SId = TextBoxSubScriptionId.Text.Trim();
                    CurrentSubscriptionBilling = LoadSubscriptionForBilling(SId);

                    if (CurrentSubscriptionBilling != null)
                    {
                        FillSubScription(CurrentSubscriptionBilling);
                        if (IsForward)
                        {
                            TextBoxCurrentReadDate.Focus();
                            TextBoxCurrentReadDate.SelectAll();
                        }
                        else
                        {
                            TextBoxCurrentRead.Focus();
                            TextBoxCurrentRead.SelectAll();
                        }
                    }
                    else
                    {
                        Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.NotFoundSubscriptionOrBill, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                        Msg.Owner = Window.GetWindow(this);
                        Msg.ShowDialog();
                        EmptyControl();
                        FirstControl.Focus();
                        FirstControl.SelectAll();
                    }
                }
            }
            catch
            {
                Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.ErrorSendingDataToDatabase, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                Msg.Owner = Window.GetWindow(this);
                Msg.ShowDialog();
                FirstControl.Focus();
                FirstControl.SelectAll();
            }
        }
        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            ControlsValidate?Vd  = Commons.ValidateData(FirstControl, ControlsArray);
            MessageDialog    Msg = null;

            if (Commons.CPeriod.Id > 0 && Commons.CPeriod.IsClosed == false)
            {
                if (Vd != null)
                {
                    Msg       = new MessageDialog(Messages.SaveMessageTitleBillsReceivable, Vd.Value.Message, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                    Msg.Owner = Window.GetWindow(this);
                    Msg.ShowDialog();
                    Vd.Value.Control.Focus();
                }
                else
                {
                    if (string.Compare(TextBoxCurrentReadDate.Text.Trim(), Commons.GetCurrentPersianDate()) <= 0)
                    {
                        DomainClasses.SubscriptionsTb MySubscription = CurrentSubscriptionBilling.SubScription;
                        long Price = Convert.ToInt64(TextBoxCurrentRead.Text.Trim());
                        if (Price > 0)
                        {
                            if (string.Compare(TextBoxCurrentReadDate.Text.Trim(), MySubscription.CurrentReadDate) >= 0)
                            {
                                if (Price != MySubscription.Debt)
                                {
                                    Msg       = new MessageDialog(Messages.SaveMessageTitleBillsReceivable, Messages.PriceIsNotEqualWithDebt, MessageDialogButtons.YesNo, MessageDialogType.Warning, GridHeader.Background);
                                    Msg.Owner = Window.GetWindow(this);
                                    if (Msg.ShowDialog() == false)
                                    {
                                        TextBoxCurrentRead.SelectAll();
                                        TextBoxCurrentRead.Focus();
                                        return;
                                    }
                                }

                                Msg       = new MessageDialog(Messages.SaveMessageTitleBillsReceivable, Messages.SaveMessageBillsReceivable, MessageDialogButtons.YesNo, MessageDialogType.Warning, GridHeader.Background);
                                Msg.Owner = Window.GetWindow(this);
                                if (Msg.ShowDialog() == true)
                                {
                                    try
                                    {
                                        long Deficit1000 = Commons.GetDeficit1000(Price);
                                        CurrentSubscriptionBilling.Bill.ReceivableDate            = TextBoxCurrentReadDate.Text.Trim();
                                        CurrentSubscriptionBilling.Bill.ReceivablePrice           = Price - Deficit1000;
                                        CurrentSubscriptionBilling.Bill.ReceivableDefict1000      = Deficit1000;
                                        CurrentSubscriptionBilling.Bill.ReceivablePrevDebt        = MySubscription.Debt;
                                        CurrentSubscriptionBilling.Bill.ReceivablePrevdeficit1000 = MySubscription.deficit1000;
                                        if (Price == MySubscription.Debt)
                                        {
                                            CurrentSubscriptionBilling.Bill.Status = BillsStatus.ReceivableFull;
                                        }
                                        else
                                        {
                                            CurrentSubscriptionBilling.Bill.Status = BillsStatus.ReceivableNotMatch;
                                        }

                                        long OldDebt = MySubscription.Debt, OldDeficit1000 = MySubscription.deficit1000;
                                        MySubscription.Debt        = MySubscription.Debt - (Price - Deficit1000);
                                        MySubscription.deficit1000 = MySubscription.deficit1000 - Deficit1000;
                                        Commons.Db.SaveChanges();
                                        SearchAgain = true;
                                        Msg         = new MessageDialog(Messages.SaveMessageTitleBillsReceivable, Messages.SaveMessageSuccessBillsReceivable, MessageDialogButtons.Ok, MessageDialogType.Information, GridHeader.Background);
                                        Msg.Owner   = Window.GetWindow(this);
                                        Msg.ShowDialog();
                                        EmptyControl();
                                        TextBoxSubScriptionId.Text = "";
                                        TextBoxSubScriptionId.Focus();
                                        TextBoxSubScriptionId.SelectAll();
                                    }
                                    catch
                                    {
                                        Msg       = new MessageDialog(Messages.SaveMessageTitleBillsReceivable, Messages.ErrorSendingDataToDatabase, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                                        Msg.Owner = Window.GetWindow(this);
                                        Msg.ShowDialog();
                                        TextBoxCurrentRead.SelectAll();
                                    }
                                }
                            }
                            else
                            {
                                Msg       = new MessageDialog(Messages.SaveMessageTitleBillsReceivable, Messages.CurrentDateIsNotAllowedLowerThanCurrentReadDate, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                Msg.Owner = Window.GetWindow(this);
                                Msg.ShowDialog();
                                TextBoxCurrentReadDate.Focus();
                                TextBoxCurrentReadDate.SelectAll();
                            }
                        }
                        else
                        {
                            Msg       = new MessageDialog(Messages.SaveMessageTitleBillsReceivable, Messages.PriceIsNotAllowedZeroOrLess, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                            Msg.Owner = Window.GetWindow(this);
                            Msg.ShowDialog();
                            TextBoxCurrentRead.SelectAll();
                        }
                    }
                    else
                    {
                        Msg       = new MessageDialog(Messages.SaveMessageTitleBillsReceivable, Messages.DateIsNotAllowedBiggarThanReceivableDate, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                        Msg.Owner = Window.GetWindow(this);
                        Msg.ShowDialog();
                        TextBoxCurrentReadDate.Focus();
                        TextBoxCurrentReadDate.SelectAll();
                    }
                }
            }
            else
            {
                Msg       = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.NotExistPeriodBilling, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                Msg.Owner = Window.GetWindow(this);
                Msg.ShowDialog();
                TextBoxCurrentRead.SelectAll();
            }
        }
        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            ControlsValidate?Vd  = Commons.ValidateData(FirstControl, ControlsArray);
            MessageDialog    Msg = null;

            if (Commons.CPeriod.Id > 0 && Commons.CPeriod.IsClosed == false)
            {
                if (Vd != null)
                {
                    Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Vd.Value.Message, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                    Msg.Owner = Window.GetWindow(this);
                    Msg.ShowDialog();
                    Vd.Value.Control.Focus();
                }
                else
                {
                    SubscriptionsTb MySubscription = CurrentSubscriptionBilling.SubScription;
                    if (string.Compare(TextBoxCurrentReadDate.Text.Trim(), Commons.GetCurrentPersianDate()) <= 0)
                    {
                        long NewNumber     = Convert.ToInt64(TextBoxCurrentRead.Text);
                        long MyConsumption = NewNumber - MySubscription.CurrentNumber;
                        int  ChkDate       = 0;
                        if (MyConsumption >= 0)
                        {
                            ChkDate = Commons.CheckDateFromTo(MySubscription.CurrentReadDate, TextBoxCurrentReadDate.Text.Trim());
                            int DistMonth = 0;
                            if (ChkDate == 0)
                            {
                                DistMonth = Commons.DistanceBetweenMonthsForBilling(MySubscription.CurrentReadDate, TextBoxCurrentReadDate.Text.Trim());
                                if (DistMonth > 0)
                                {
                                    long[] Result = Commons.Consumption(MyConsumption, CurrentSubscriptionBilling.AccountTypeFormula, DistMonth);
                                    if (Result != null)
                                    {
                                        Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.SaveMessageSingleBilling, MessageDialogButtons.YesNo, MessageDialogType.Warning, GridHeader.Background);
                                        Msg.Owner = Window.GetWindow(this);
                                        if (Msg.ShowDialog() == true)
                                        {
                                            try
                                            {
                                                BillsTb NewBill = Commons.Db.Bills.Add(new DomainClasses.BillsTb
                                                {
                                                    SubscriptionId              = MySubscription.Id,
                                                    SubscriptionYear            = Commons.CurrentYear,
                                                    Status                      = BillsStatus.Billing,
                                                    PrevNumber                  = MySubscription.CurrentNumber,
                                                    PrevReadDate                = MySubscription.CurrentReadDate,
                                                    CurrentNumber               = NewNumber,
                                                    CurrentReadDate             = TextBoxCurrentReadDate.Text.Trim(),
                                                    Consumption                 = MyConsumption,
                                                    AllowableConsumption        = Result[0],
                                                    ExtraConsumption            = Result[1],
                                                    PriceOfConsumption          = Result[4],
                                                    PriceOfAllowableConsumption = Result[2],
                                                    PriceOfExtraConsumption     = Result[3],
                                                    Vat = Result[5],
                                                    SubscriptionCost = Commons.Subscription * DistMonth,
                                                    Year             = Commons.CurrentYear,
                                                    CurrentPeriod    = Commons.CPeriod.Id,
                                                    PrevDebt         = MySubscription.Debt,
                                                    Prevdeficit1000  = MySubscription.deficit1000,
                                                    PrevPrevNumber   = MySubscription.PrevNumber,
                                                    PrevPrevReadDate = MySubscription.PrevReadDate,
                                                    WaterMeterId     = MySubscription.WaterMeter.Id,
                                                    WaterMeterYear   = Commons.CurrentYear,
                                                    AccountTypeId    = MySubscription.AccountTypeId,
                                                    AccountTypeYear  = Commons.CurrentYear,
                                                    PreventTypeId    = MySubscription.PreventTypeId,
                                                    PreventTypeYear  = Commons.CurrentYear,
                                                    ReceivableDate   = "",
                                                    CancelDate       = ""
                                                });
                                                long AllPrices  = Result[4] + Result[5] + MySubscription.Debt + MySubscription.deficit1000 + (Commons.Subscription * DistMonth);
                                                long NewDeficit = Commons.GetDeficit1000(AllPrices);
                                                MySubscription.Debt        = AllPrices - NewDeficit;
                                                MySubscription.deficit1000 = NewDeficit;

                                                MySubscription.PrevNumber      = MySubscription.CurrentNumber;
                                                MySubscription.PrevReadDate    = MySubscription.CurrentReadDate;
                                                MySubscription.CurrentReadDate = TextBoxCurrentReadDate.Text.Trim();
                                                MySubscription.CurrentNumber   = NewNumber;
                                                MySubscription.BillingInPeriod = Commons.CPeriod.Id;
                                                //به روز رسانی آخرین قرائت با کنتور فعلی
                                                MySubscription.WaterMeter.ReadEnd     = NewNumber;
                                                MySubscription.WaterMeter.ReadDateEnd = TextBoxCurrentReadDate.Text.Trim();

                                                Commons.Db.SaveChanges();
                                                SearchAgain = true;
                                                Msg         = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.SaveMessageSuccessSingleBilling, MessageDialogButtons.Ok, MessageDialogType.Information, GridHeader.Background);
                                                Msg.Owner   = Window.GetWindow(this);
                                                Msg.ShowDialog();
                                                EmptyControl();
                                                TextBoxSubScriptionId.Text  = "";
                                                TextBoxCurrentRead.Text     = "";
                                                TextBoxCurrentReadDate.Text = Commons.GetCurrentPersianDate().Trim();
                                                TextBoxSubScriptionId.Focus();
                                                TextBoxSubScriptionId.SelectAll();
                                            }
                                            catch
                                            {
                                                Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.ErrorSendingDataToDatabase, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                                                Msg.Owner = Window.GetWindow(this);
                                                Msg.ShowDialog();
                                                TextBoxCurrentRead.SelectAll();
                                            }
                                        }
                                    }
                                    else
                                    {
                                        Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.FormulaIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                        Msg.Owner = Window.GetWindow(this);
                                        Msg.ShowDialog();
                                        TextBoxCurrentRead.SelectAll();
                                    }
                                }
                                else
                                {
                                    Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.ToDateIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                    Msg.Owner = Window.GetWindow(this);
                                    Msg.ShowDialog();
                                    TextBoxCurrentReadDate.Focus();
                                    TextBoxCurrentReadDate.SelectAll();
                                }
                            }
                            else
                            {
                                if (ChkDate == -1)
                                {
                                    Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.ToDateIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                    Msg.Owner = Window.GetWindow(this);
                                    Msg.ShowDialog();
                                    TextBoxCurrentReadDate.Focus();
                                    TextBoxCurrentReadDate.SelectAll();
                                }
                                else
                                {
                                    Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.DateIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                    Msg.Owner = Window.GetWindow(this);
                                    Msg.ShowDialog();
                                    TextBoxCurrentReadDate.Focus();
                                    TextBoxCurrentReadDate.SelectAll();
                                }
                            }
                        }
                        else
                        {
                            Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.NumberIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                            Msg.Owner = Window.GetWindow(this);
                            Msg.ShowDialog();
                            TextBoxCurrentRead.SelectAll();
                        }
                    }
                    else
                    {
                        Msg       = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.DateIsNotAllowedBiggarThanCurrentDate, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                        Msg.Owner = Window.GetWindow(this);
                        Msg.ShowDialog();
                        TextBoxCurrentReadDate.Focus();
                        TextBoxCurrentReadDate.SelectAll();
                    }
                }
            }
        }