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(); } } } }
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(); } }