Exemplo n.º 1
0
        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            ControlsValidate?Vd  = Commons.ValidateData(FirstControl, ControlsArray);
            MessageDialog    Msg = null;

            if (Vd != null)
            {
                Msg       = new MessageDialog(Messages.SaveMessageTitleSubscriptions, Vd.Value.Message, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                Msg.Owner = Window.GetWindow(this);
                Msg.ShowDialog();
                Vd.Value.Control.Focus();
            }
            else
            {
                DomainClasses.CustomersTb MyCustomer = Commons.Db.Customers.Find(Convert.ToInt32(TextBoxCustomerId.Text), Commons.CurrentYear);
                if (MyCustomer == null)
                {
                    Msg       = new MessageDialog(Messages.SaveMessageTitleSubscriptions, Messages.NotFoundCustomers, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                    Msg.Owner = Window.GetWindow(this);
                    Msg.ShowDialog();
                    return;
                }
                DomainClasses.AccountTypesTb MyAccountType = Commons.Db.AccountTypes.Find(((Int32)((ComboBoxItem)ComboBoxAccountType.SelectedItem).Tag), Commons.CurrentYear);
                if (MyAccountType == null)
                {
                    Msg       = new MessageDialog(Messages.SaveMessageTitleSubscriptions, Messages.NotFoundAccountTypes, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                    Msg.Owner = Window.GetWindow(this);
                    Msg.ShowDialog();
                    return;
                }

                DomainClasses.SubscriptionsTb MySubscription = null;
                MySubscription = Commons.Db.Subscriptions.Find(TextBoxSubScriptionId.Text, Commons.CurrentYear);

                if (MySubscription == null)
                {
                    Msg       = new MessageDialog(Messages.SaveMessageTitleSubscriptions, Messages.SaveMessageSubscriptions, MessageDialogButtons.YesNo, MessageDialogType.Warning, GridHeader.Background);
                    Msg.Owner = Window.GetWindow(this);
                    if (Msg.ShowDialog() == true)
                    {
                        try
                        {
                            int?MaxId = Commons.Db.WaterMeters.Where(x => x.Year == Commons.CurrentYear).Max(x => (int?)x.Id);
                            if (MaxId == null)
                            {
                                MaxId = 0;
                            }
                            MaxId++;
                            WaterMetersTb Wm = Commons.Db.WaterMeters.Add(new DomainClasses.WaterMetersTb
                            {
                                Id               = Convert.ToInt32(MaxId),
                                SubId            = TextBoxSubScriptionId.Text.Trim(),
                                Year             = Commons.CurrentYear,
                                ReadStart        = Convert.ToInt64(TextBoxWaterMeterNumber.Text.Trim()),
                                ReadDateStart    = TextBoxNumberReadDate.Text.Trim(),
                                ReadEnd          = Convert.ToInt64(TextBoxWaterMeterNumber.Text.Trim()),
                                ReadDateEnd      = TextBoxNumberReadDate.Text.Trim(),
                                WaterMeterSerial = TextBoxWaterMeterSerial.Text.Trim(),
                                Description      = "اولین کنتور آب",
                            });
                            Commons.Db.Subscriptions.Add(new DomainClasses.SubscriptionsTb
                            {
                                Id              = TextBoxSubScriptionId.Text.Trim(),
                                Year            = Commons.CurrentYear,
                                WaterMeter      = Wm,
                                PrevNumber      = Convert.ToInt64(TextBoxWaterMeterNumber.Text.Trim()),
                                PrevReadDate    = TextBoxNumberReadDate.Text.Trim(),
                                CurrentNumber   = Convert.ToInt64(TextBoxWaterMeterNumber.Text.Trim()),
                                CurrentReadDate = TextBoxNumberReadDate.Text.Trim(),
                                Debt            = Convert.ToInt64(TextBoxDebt.Text),
                                deficit1000     = Convert.ToInt64(TextBoxDeficit1000.Text),
                                BillingInPeriod = 0,
                                PostalCode      = TextBoxPostalCode.Text.Trim(),
                                Address         = TextBoxAddress.Text.Trim(),
                                CustomerId      = Convert.ToInt32(TextBoxCustomerId.Text.Trim()),
                                CustomerYear    = Commons.CurrentYear,
                                AccountTypeId   = Convert.ToInt32(((ComboBoxItem)ComboBoxAccountType.SelectedItem).Tag),
                                AccountTypeYear = Commons.CurrentYear,
                                PreventTypeId   = Convert.ToInt32(((ComboBoxItem)ComboBoxPreventType.SelectedValue).Tag),
                                PreventTypeYear = Commons.CurrentYear,
                                Description     = TextBoxComments.Text.Trim()
                            });
                            Commons.Db.SaveChanges();
                            Commons.SetFromEdited("SubScriptions");
                            SearchAgain = true;
                            DataGridView.Items.Refresh();
                            Msg       = new MessageDialog(Messages.SaveMessageTitleSubscriptions, Messages.SaveMessageSuccessSubscriptions, MessageDialogButtons.Ok, MessageDialogType.Information, GridHeader.Background);
                            Msg.Owner = Window.GetWindow(this);
                            Msg.ShowDialog();
                            ButtonNew_Click(null, null);
                        }
                        catch (Exception Ex)
                        {
                            if (Ex.InnerException != null)
                            {
                                if (Ex.InnerException.InnerException != null && Ex.InnerException.InnerException is SqlException)
                                {
                                    SqlException SEx = (SqlException)Ex.InnerException.InnerException;
                                    if (SEx.Message.Contains("IX_Unique_SubscriptionsTbs_PostalCode"))
                                    {
                                        Msg       = new MessageDialog(Messages.SaveMessageTitleSubscriptions, Messages.PostalCodeIsRepetitive, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                        Msg.Owner = Window.GetWindow(this);
                                        Msg.ShowDialog();
                                        TextBoxPostalCode.Focus();
                                        TextBoxPostalCode.SelectAll();
                                    }
                                }
                            }
                            else
                            {
                                Msg       = new MessageDialog(Messages.SaveMessageTitleSubscriptions, Messages.ErrorSendingDataToDatabase, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                                Msg.Owner = Window.GetWindow(this);
                                Msg.ShowDialog();
                            }
                        }
                    }
                }
                else
                {
                    Msg       = new MessageDialog(Messages.EditMessageTitleSubscriptions, Messages.EditMessageSubscriptions, MessageDialogButtons.YesNo, MessageDialogType.Warning, GridHeader.Background);
                    Msg.Owner = Window.GetWindow(this);
                    if (Msg.ShowDialog() == true)
                    {
                        try
                        {
                            if (CheckSubscriptionInUse(MySubscription) == false)
                            {
                                //Save When it doesn't have bill
                                WaterMetersTb WmTb = Commons.Db.WaterMeters.Find(MySubscription.WaterMeterId, Commons.CurrentYear);
                                if (WmTb != null)
                                {
                                    WmTb.ReadDateStart    = TextBoxNumberReadDate.Text.Trim();
                                    WmTb.ReadDateEnd      = TextBoxNumberReadDate.Text.Trim();
                                    WmTb.ReadEnd          = Convert.ToInt64(TextBoxWaterMeterNumber.Text.Trim());
                                    WmTb.ReadStart        = Convert.ToInt64(TextBoxWaterMeterNumber.Text.Trim());
                                    WmTb.WaterMeterSerial = TextBoxWaterMeterSerial.Text.Trim();
                                }
                                else
                                {
                                    Msg       = new MessageDialog(Messages.EditMessageTitleSubscriptions, Messages.NotWaterMeterExist, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                                    Msg.Owner = Window.GetWindow(this);
                                    Msg.ShowDialog();
                                }

                                MySubscription.PrevNumber      = Convert.ToInt64(TextBoxWaterMeterNumber.Text.Trim());
                                MySubscription.PrevReadDate    = TextBoxNumberReadDate.Text.Trim();
                                MySubscription.CurrentNumber   = Convert.ToInt64(TextBoxWaterMeterNumber.Text.Trim());
                                MySubscription.CurrentReadDate = TextBoxNumberReadDate.Text.Trim();
                                //MySubscription.Debt = 0;
                                //MySubscription.deficit1000 = 0;
                            }
                            else
                            {
                                Msg       = new MessageDialog(Messages.EditMessageTitleSubscriptions, Messages.SubScriptInUseEdit, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                                Msg.Owner = Window.GetWindow(this);
                                Msg.ShowDialog();
                            }

                            MySubscription.PostalCode    = TextBoxPostalCode.Text.Trim();
                            MySubscription.Address       = TextBoxAddress.Text.Trim();
                            MySubscription.CustomerId    = Convert.ToInt32(TextBoxCustomerId.Text.Trim());
                            MySubscription.AccountTypeId = Convert.ToInt32(((ComboBoxItem)ComboBoxAccountType.SelectedItem).Tag);
                            MySubscription.PreventTypeId = Convert.ToInt32(((ComboBoxItem)ComboBoxPreventType.SelectedValue).Tag);
                            MySubscription.Description   = TextBoxComments.Text.Trim();
                            MySubscription.Debt          = Convert.ToInt64(TextBoxDebt.Text);
                            MySubscription.deficit1000   = Convert.ToInt64(TextBoxDeficit1000.Text);
                            Commons.Db.SaveChanges();
                            Commons.SetFromEdited("SubScriptions");

                            List <SubscriptionsTb> Lc = (List <SubscriptionsTb>)DataGridView.ItemsSource;
                            if (Lc != null)
                            {
                                SubscriptionsTb Rc = Lc.Find(x => x.Id.Trim() == MySubscription.Id.Trim() && x.Year == Commons.CurrentYear);
                                if (Rc != null)
                                {
                                    Rc.AccountType     = MySubscription.AccountType;
                                    Rc.AccountType     = MySubscription.AccountType;
                                    Rc.AccountTypeId   = MySubscription.AccountTypeId;
                                    Rc.AccountTypeYear = MySubscription.AccountTypeYear;
                                    Rc.Address         = MySubscription.Address;
                                    Rc.BillingInPeriod = MySubscription.BillingInPeriod;
                                    Rc.CurrentReadDate = MySubscription.CurrentReadDate;
                                    Rc.CustomerId      = MySubscription.CustomerId;
                                    Rc.CustomerYear    = MySubscription.CustomerYear;
                                    Rc.Debt            = MySubscription.Debt;
                                    Rc.deficit1000     = MySubscription.deficit1000;
                                    Rc.Description     = MySubscription.Description;
                                    Rc.PostalCode      = MySubscription.PostalCode;
                                    Rc.PreventTypeId   = MySubscription.PreventTypeId;
                                    Rc.PreventTypeYear = MySubscription.PreventTypeYear;
                                    Rc.PrevNumber      = MySubscription.PrevNumber;
                                    Rc.PrevReadDate    = MySubscription.PrevReadDate;
                                    Rc.RecordDate      = MySubscription.RecordDate;
                                    Rc.WaterMeterId    = MySubscription.WaterMeterId;
                                    Rc.WaterMeterYear  = MySubscription.WaterMeterYear;
                                    Rc.Year            = MySubscription.Year;
                                }
                            }

                            SearchAgain = true;
                            DataGridView.Items.Refresh();
                            Msg       = new MessageDialog(Messages.EditMessageTitleSubscriptions, Messages.EditMessageSuccessSubscriptions, MessageDialogButtons.Ok, MessageDialogType.Information, GridHeader.Background);
                            Msg.Owner = Window.GetWindow(this);
                            Msg.ShowDialog();
                            ButtonNew_Click(null, null);
                        }
                        catch (Exception Ex)
                        {
                            if (Ex.InnerException != null)
                            {
                                if (Ex.InnerException != null)
                                {
                                    if (Ex.InnerException.InnerException != null && Ex.InnerException.InnerException is SqlException)
                                    {
                                        SqlException SEx = (SqlException)Ex.InnerException.InnerException;
                                        if (SEx.Message.Contains("IX_Unique_SubscriptionsTbs_PostalCode"))
                                        {
                                            Msg       = new MessageDialog(Messages.SaveMessageTitleSubscriptions, Messages.PostalCodeIsRepetitive, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                            Msg.Owner = Window.GetWindow(this);
                                            Msg.ShowDialog();
                                            TextBoxPostalCode.Focus();
                                            TextBoxPostalCode.SelectAll();
                                            return;
                                        }
                                    }
                                }
                            }
                            Msg       = new MessageDialog(Messages.EditMessageTitleSubscriptions, Messages.ErrorSendingDataToDatabase, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                            Msg.Owner = Window.GetWindow(this);
                            Msg.ShowDialog();
                        }
                    }
                }
            }
        }
        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();
            }
        }
Exemplo n.º 3
0
        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.SaveMessageTitleBillsCancelling, Vd.Value.Message, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                    Msg.Owner = Window.GetWindow(this);
                    Msg.ShowDialog();
                    Vd.Value.Control.Focus();
                }
                else
                {
                    DomainClasses.SubscriptionsTb MySubscription = CurrentSubscriptionBilling.SubScription;
                    //در صورتی که قبض مربوطه مربوط به کنتور قبلی باشد امکان ابطال آن وجود ندارد
                    if (CurrentSubscriptionBilling.SubScription.WaterMeterId == CurrentSubscriptionBilling.Bill.WaterMeterId && CurrentSubscriptionBilling.SubScription.WaterMeterYear == CurrentSubscriptionBilling.Bill.WaterMeterYear)
                    {
                        Msg       = new MessageDialog(Messages.SaveMessageTitleBillsCancelling, Messages.SaveMessageBillsCancelling, MessageDialogButtons.YesNo, MessageDialogType.Warning, GridHeader.Background);
                        Msg.Owner = Window.GetWindow(this);
                        if (Msg.ShowDialog() == true)
                        {
                            try
                            {
                                long   PrevDebt         = CurrentSubscriptionBilling.Bill.PrevDebt;
                                long   PrevDeficit1000  = CurrentSubscriptionBilling.Bill.Prevdeficit1000;
                                string PrevPrevReadDate = CurrentSubscriptionBilling.Bill.PrevPrevReadDate
                                , PrevReadDate          = MySubscription.PrevReadDate;
                                long PrevPrevNumber     = CurrentSubscriptionBilling.Bill.PrevPrevNumber
                                , PrevNumber            = MySubscription.PrevNumber;

                                if (CurrentSubscriptionBilling.Bill.Status == BillsStatus.ReceivableFull || CurrentSubscriptionBilling.Bill.Status == BillsStatus.ReceivableNotMatch)
                                {
                                    PrevDebt        = PrevDebt - CurrentSubscriptionBilling.Bill.ReceivablePrice;
                                    PrevDeficit1000 = PrevDeficit1000 - CurrentSubscriptionBilling.Bill.ReceivableDefict1000;
                                }

                                CurrentSubscriptionBilling.Bill.CancelDate = Commons.GetCurrentPersianDate();
                                CurrentSubscriptionBilling.Bill.Status     = BillsStatus.Cancel;

                                MySubscription.Debt            = PrevDebt;
                                MySubscription.deficit1000     = PrevDeficit1000;
                                MySubscription.PrevReadDate    = PrevPrevReadDate;
                                MySubscription.PrevNumber      = PrevPrevNumber;
                                MySubscription.CurrentReadDate = PrevReadDate;
                                MySubscription.CurrentNumber   = PrevNumber;
                                //به روز رسانی آخرین قرائت با کنتور فعلی
                                MySubscription.WaterMeter.ReadEnd     = PrevNumber;
                                MySubscription.WaterMeter.ReadDateEnd = PrevReadDate;

                                Commons.Db.SaveChanges();

                                Msg       = new MessageDialog(Messages.SaveMessageTitleBillsCancelling, Messages.SaveMessageSuccessBillsCancelling, 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.SaveMessageTitleBillsCancelling, Messages.ErrorSendingDataToDatabase, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                                Msg.Owner = Window.GetWindow(this);
                                Msg.ShowDialog();
                                ButtonCancel.Focus();
                            }
                        }
                    }
                    //در صورتی که قبض مربوطه مربوط به کنتور قبلی باشد امکان ابطال آن وجود ندارد
                    else
                    {
                        Msg       = new MessageDialog(Messages.SaveMessageTitleBillsCancelling, Messages.CanNotCancelBillBecauseOfWaterMeter, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                        Msg.Owner = Window.GetWindow(this);
                        Msg.ShowDialog();
                        ButtonCancel.Focus();
                    }
                }
            }
            else
            {
                Msg       = new MessageDialog(Messages.SaveMessageTitleBillsCancelling, Messages.NotExistPeriodBilling, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                Msg.Owner = Window.GetWindow(this);
                Msg.ShowDialog();
                TextBoxSubScriptionId.Text = "";
                TextBoxSubScriptionId.Focus();
                TextBoxSubScriptionId.SelectAll();
            }
        }
Exemplo n.º 4
0
        private void PreSave()
        {
            ControlsValidate?Vd   = Commons.ValidateData(FirstControl, ControlsArray);
            MessageDialog    Msg  = null;
            bool             Next = false;

            if (CurrentBillPeriod != null)
            {
                if (Vd != null)
                {
                    Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Vd.Value.Message, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                    Msg.ShowDialog();
                    Vd.Value.Control.Focus();
                }
                else
                {
                    if (string.Compare(CurrentBillPeriod.DateTo.Trim(), Commons.GetCurrentPersianDate()) <= 0)
                    {
                        DomainClasses.SubscriptionsTb MySubscription = CurrentSubscriptionBilling.SubScription;
                        long NewNumber = Convert.ToInt64(TextBoxCurrentRead.Text);
                        if (NewNumber == 0)
                        {
                            Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.SaveMessageBillingNext, MessageDialogButtons.YesNo, MessageDialogType.Warning, GridHeader.Background);
                            if (Msg.ShowDialog() == true)
                            {
                                MySubscription.BillingInPeriod = CurrentBillPeriod.Id;
                                //Commons.Db.Entry(MySubscription).CurrentValues.SetValues(MySubscription);
                                //Commons.Db.SaveChanges();
                                Next = true;
                                DoNext();
                            }
                            TextBoxCurrentRead.SelectAll();
                            return;
                        }
                        long MyConsumption = NewNumber - MySubscription.CurrentNumber;
                        int  ChkDate       = 0;
                        if (MyConsumption >= 0)
                        {
                            ChkDate = Commons.CheckDateFromTo(MySubscription.CurrentReadDate, CurrentBillPeriod.DateTo);
                            int DistMonth = 0;
                            if (ChkDate == 0)
                            {
                                DistMonth = Commons.DistanceBetweenMonthsForBilling(MySubscription.CurrentReadDate, CurrentBillPeriod.DateTo);
                                if (DistMonth > 0)
                                {
                                    long[] Result = Commons.Consumption(MyConsumption, CurrentSubscriptionBilling.AccountTypeFormula, DistMonth);
                                    if (Result != null)
                                    {
                                        try
                                        {
                                            BillsTb NewBill = Commons.Db.Bills.Add(new DomainClasses.BillsTb
                                            {
                                                SubscriptionId              = MySubscription.Id,
                                                Status                      = BillsStatus.Billing,
                                                PrevNumber                  = MySubscription.CurrentNumber,
                                                PrevReadDate                = MySubscription.CurrentReadDate,
                                                CurrentNumber               = NewNumber,
                                                CurrentReadDate             = CurrentBillPeriod.DateTo,
                                                Consumption                 = MyConsumption,
                                                AllowableConsumption        = Result[0],
                                                ExtraConsumption            = Result[1],
                                                PriceOfConsumption          = Result[4],
                                                PriceOfAllowableConsumption = Result[2],
                                                PriceOfExtraConsumption     = Result[3],
                                                Vat              = Result[5],
                                                Year             = Commons.GetCurrentYear(),
                                                CurrentPeriod    = CurrentBillPeriod.Id,
                                                PrevDebt         = MySubscription.Debt,
                                                Prevdeficit1000  = MySubscription.deficit1000,
                                                PrevPrevNumber   = MySubscription.PrevNumber,
                                                PrevPrevReadDate = MySubscription.PrevReadDate,
                                                WaterMeterSerial = MySubscription.WaterMeterSerial,
                                                AccountTypeId    = MySubscription.AccountTypeId,
                                                PreventTypeId    = MySubscription.PreventTypeId,
                                                ReceivableDate   = ""
                                            });
                                            long AllPrices  = Result[4] + Result[5] + MySubscription.Debt + MySubscription.deficit1000;
                                            long NewDeficit = Commons.GetDeficit1000(AllPrices);
                                            MySubscription.Debt        = AllPrices - NewDeficit;
                                            MySubscription.deficit1000 = NewDeficit;

                                            MySubscription.PrevNumber      = MySubscription.CurrentNumber;
                                            MySubscription.PrevReadDate    = MySubscription.CurrentReadDate;
                                            MySubscription.CurrentReadDate = CurrentBillPeriod.DateTo;
                                            MySubscription.CurrentNumber   = NewNumber;
                                            MySubscription.BillingInPeriod = CurrentBillPeriod.Id;
                                            Bills.Add(new BillingDetails
                                            {
                                                Id                          = NewBill.Id,
                                                SubId                       = CurrentSubscriptionBilling.SubScription.Id,
                                                CustId                      = CurrentSubscriptionBilling.CustId,
                                                Name                        = CurrentSubscriptionBilling.CustName,
                                                Family                      = CurrentSubscriptionBilling.CustFamily,
                                                Father                      = CurrentSubscriptionBilling.CustFather,
                                                Prevent                     = CurrentSubscriptionBilling.PreventName,
                                                AccounType                  = CurrentSubscriptionBilling.AccountTypeName,
                                                PrevDebt                    = CurrentSubscriptionBilling.SubScription.Debt,
                                                deficit1000                 = NewDeficit,
                                                Prevdeficit1000             = CurrentSubscriptionBilling.SubScription.deficit1000,
                                                CurrentReadDate             = CurrentBillPeriod.DateTo,
                                                CurrentNumber               = NewNumber,
                                                PrevReadDate                = CurrentSubscriptionBilling.SubScription.CurrentReadDate,
                                                PrevNumber                  = CurrentSubscriptionBilling.SubScription.CurrentNumber,
                                                AllowableConsumption        = Result[0],
                                                Consumption                 = MyConsumption,
                                                ExtraConsumption            = Result[1],
                                                PriceOfAllowableConsumption = Result[2],
                                                PriceOfConsumption          = Result[4],
                                                PriceOfExtraConsumption     = Result[3],
                                                Vat                         = Result[5],
                                                SumOfPeriod                 = Result[4] + CurrentSubscriptionBilling.SubScription.Debt + Result[5],
                                                AllPrices                   = AllPrices,
                                                PayablePrice                = AllPrices - NewDeficit
                                            });
                                            Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.PreSaveMessageSuccessBilling, MessageDialogButtons.Ok, MessageDialogType.Information, GridHeader.Background);
                                            Msg.ShowDialog();
                                            Next = true;
                                        }
                                        catch
                                        {
                                            Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.ErrorSendingDataToDatabase, MessageDialogButtons.Ok, MessageDialogType.Error, GridHeader.Background);
                                            Msg.ShowDialog();
                                            TextBoxCurrentRead.SelectAll();
                                        }
                                    }
                                    else
                                    {
                                        Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.FormulaIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                        Msg.ShowDialog();
                                        TextBoxCurrentRead.SelectAll();
                                    }
                                }
                                else
                                {
                                    Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.ToDateIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                    Msg.ShowDialog();
                                    TextBoxCurrentRead.SelectAll();
                                }
                            }
                            else
                            {
                                if (ChkDate == -1)
                                {
                                    Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.ToDateIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                    Msg.ShowDialog();
                                    TextBoxCurrentRead.SelectAll();
                                }
                                else
                                {
                                    Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.DateIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                                    Msg.ShowDialog();
                                    TextBoxCurrentRead.SelectAll();
                                }
                            }
                        }
                        else
                        {
                            Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.NumberIsWrong, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                            Msg.ShowDialog();
                            TextBoxCurrentRead.SelectAll();
                        }
                    }
                    else
                    {
                        Msg = new MessageDialog(Messages.SaveMessageTitleSingleBilling, Messages.DateIsNotAllowedBiggarThanCurrentDate, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                        Msg.ShowDialog();
                        TextBoxCurrentRead.SelectAll();
                    }
                }
            }
            else
            {
                Msg = new MessageDialog(Messages.SaveMessageTitleBilling, Messages.NotExistPeriodBilling, MessageDialogButtons.Ok, MessageDialogType.Warning, GridHeader.Background);
                Msg.ShowDialog();
                TextBoxCurrentRead.SelectAll();
            }
            if (Next)
            {
                DoNext();
            }
        }