245 lines
7.8 KiB
C#
245 lines
7.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Windows;
|
|
using BeWo.Annotations;
|
|
using BeWo.Core;
|
|
using BeWo.Core.Service;
|
|
using BeWo.ServiceProxy;
|
|
using BS.Shared;
|
|
using BS.Shared.Core;
|
|
using BS.Shared.DataContracts;
|
|
using BS.Shared.DataContracts.Compact;
|
|
using ChatController.Utilities.Extensions;
|
|
using DevExpress.Xpf.Core;
|
|
using DevExpress.XtraReports.UI;
|
|
|
|
namespace BeWo.View.Report.Kilometerauswertung
|
|
{
|
|
public partial class KilometerauslastungsView : INotifyPropertyChanged
|
|
{
|
|
public bool IsCreateButtonEnabled => _ShouldDoListReport || _SelectedCustomer != null;
|
|
|
|
private List<CompactCustomerDC> AllCustomers { get; set; } = new List<CompactCustomerDC>();
|
|
|
|
private bool _ShouldDoListReport;
|
|
|
|
private CompactCustomerDC _SelectedCustomer;
|
|
public CompactCustomerDC SelectedCustomer
|
|
{
|
|
get => _SelectedCustomer;
|
|
|
|
set
|
|
{
|
|
if(!Equals(_SelectedCustomer, value))
|
|
{
|
|
_SelectedCustomer = value;
|
|
|
|
if(value != null)
|
|
{
|
|
OnPropertyChanged(nameof(IsCreateButtonEnabled));
|
|
}
|
|
|
|
OnPropertyChanged(nameof(SelectedCustomer));
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<string> MonthNames => DateTimeFormatInfo.CurrentInfo?.MonthNames.Where(s => !string.IsNullOrEmpty(s)).ToList();
|
|
|
|
private int _SelectedMonthIndex;
|
|
|
|
private List<int> _Years;
|
|
|
|
public List<int> Years
|
|
{
|
|
get => _Years;
|
|
|
|
set
|
|
{
|
|
if(_Years != value)
|
|
{
|
|
_Years = value;
|
|
|
|
OnPropertyChanged(nameof(Years));
|
|
}
|
|
}
|
|
}
|
|
|
|
public int SelectedMonthIndex
|
|
{
|
|
get => _SelectedMonthIndex;
|
|
|
|
set
|
|
{
|
|
if(_SelectedMonthIndex != value)
|
|
{
|
|
_SelectedMonthIndex = value;
|
|
|
|
OnPropertyChanged(nameof(SelectedMonthIndex));
|
|
}
|
|
}
|
|
}
|
|
|
|
private int _SelectedYearIndex;
|
|
|
|
public int SelectedYearIndex
|
|
{
|
|
get => _SelectedYearIndex;
|
|
|
|
set
|
|
{
|
|
if(_SelectedYearIndex != value)
|
|
{
|
|
_SelectedYearIndex = value;
|
|
|
|
OnPropertyChanged(nameof(SelectedYearIndex));
|
|
}
|
|
}
|
|
}
|
|
|
|
public KilometerauslastungsView()
|
|
{
|
|
InitializeComponent();
|
|
|
|
DataContext = this;
|
|
|
|
var monthIndex = DateTime.Now.Month - 2;
|
|
|
|
SelectedMonthIndex = monthIndex < 0 ? 11 : monthIndex;
|
|
|
|
InitYearComboBox();
|
|
|
|
ServiceFacade.DoCustomerServiceAsync(s => s.GetAllActiveCustomersCompact(false, BeWoApp.LoggedOnEmployeeOid), allCustomers =>
|
|
{
|
|
this.Dispatch(() =>
|
|
{
|
|
if(BeWoApp.LoggedOnUser.HasRight(UserRightType.ViewAll) || BeWoApp.LoggedOnUser.HasRight(UserRightType.CustomerView_View))
|
|
{
|
|
AllCustomers = allCustomers;
|
|
}
|
|
else
|
|
{
|
|
if(BeWoApp.LoggedOnUser.HasRight(UserRightType.Customer_ViewMyTeams))
|
|
{
|
|
AllCustomers = allCustomers.Where(customer => customer.IsRelatedToTeam || customer.IsRelatedToEmployee).ToList();
|
|
}
|
|
else if(BeWoApp.LoggedOnUser.HasRight(UserRightType.Customer_ViewMyCustomers))
|
|
{
|
|
AllCustomers = allCustomers.Where(customer => customer.IsRelatedToEmployee).ToList();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
ThemeManager.SetTheme(BeWoApp.CurrentBeWo.MainWindow, Theme.Office2010Black);
|
|
|
|
Unloaded += delegate
|
|
{
|
|
ThemeManager.SetTheme(BeWoApp.CurrentBeWo.MainWindow, null);
|
|
};
|
|
}
|
|
|
|
private void InitYearComboBox()
|
|
{
|
|
var years = new List<int> {DateTime.Now.Year};
|
|
|
|
for(var i = 0; i < 10; i++)
|
|
{
|
|
years.Add(DateTime.Now.Year - i);
|
|
}
|
|
|
|
Years = years;
|
|
}
|
|
|
|
private void PopUpEdit_OnPopUpClick(object sender, RoutedEventArgs e)
|
|
{
|
|
CustomerSearchViewPopup.IsOpen = true;
|
|
}
|
|
|
|
private void CustomerSearchViewPopup_CustomerSelected(object sender, EventArgs<CompactCustomerDC> e)
|
|
{
|
|
SelectedCustomer = e.Data;
|
|
CustomerSearchViewPopup.IsOpen = false;
|
|
}
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
[NotifyPropertyChangedInvocator]
|
|
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
|
|
private void ButtonGenerate_OnClick(object sender, RoutedEventArgs e)
|
|
{
|
|
var selectedYear = Years[SelectedYearIndex];
|
|
|
|
var rangeDate = new DateTime(selectedYear, SelectedMonthIndex + 1, 1);
|
|
|
|
var end = rangeDate.AddMonths(1).AddDays(-1);
|
|
|
|
var timeSpan = new DateTimeSpan(rangeDate, end);
|
|
|
|
var customerOids = _ShouldDoListReport ? AllCustomers.Select(customer => customer.CustomerOid).ToList() : new List<long> {SelectedCustomer.CustomerOid};
|
|
|
|
ServiceFacade.DoReportServiceAsync(rs => rs.CheckForAnyServiceRecordsWithDistanceValues(customerOids, timeSpan), hasAny =>
|
|
{
|
|
if(hasAny)
|
|
{
|
|
if(_ShouldDoListReport)
|
|
{
|
|
ServiceFacade.DoReportServiceAsync(s => s.CreateKilometerauswertungForAllCustomers(AllCustomers.Select(customer => customer.CustomerOid).ToList(), rangeDate), ShowReport, true);
|
|
}
|
|
else
|
|
{
|
|
ServiceFacade.DoReportServiceAsync(s => s.CreateKilometerauswertung(SelectedCustomer.CustomerOid, rangeDate), ShowReport, true);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.Dispatch(() => { MessageBox.Show("Der ausgewählte Zeitraum enthält keinerlei Buchungen mit Angaben zu gefahreren Kilometern", "Kilometerauswertung", MessageBoxButton.OK, MessageBoxImage.Warning); });
|
|
}
|
|
});
|
|
}
|
|
|
|
private void ShowReport(MemoryStream reportStream)
|
|
{
|
|
if(reportStream == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
this.Dispatch(() =>
|
|
{
|
|
var report = new XtraReport();
|
|
|
|
try
|
|
{
|
|
report.PrintingSystem.LoadDocument(reportStream);
|
|
}
|
|
catch(Exception exception)
|
|
{
|
|
MessageBox.Show("Beim Öffnen der Druckvorschau ist leider ein Fehler aufgetreten!", "Fehler", MessageBoxButton.OK, MessageBoxImage.Exclamation);
|
|
}
|
|
|
|
reportStream.Close();
|
|
reportStream.Dispose();
|
|
|
|
DocumentPreviewControl.DocumentSource = report;
|
|
DocumentPreviewControl.Visibility = Visibility.Visible;
|
|
});
|
|
}
|
|
|
|
private void AllCustomersCheckBox_OnCheckedChanged(object sender, RoutedEventArgs e)
|
|
{
|
|
_ShouldDoListReport = AllCustomersCheckBox.IsChecked.HasValue && AllCustomersCheckBox.IsChecked.Value;
|
|
OnPropertyChanged(nameof(IsCreateButtonEnabled));
|
|
}
|
|
}
|
|
}
|