diff --git a/BeWo/ServiceProxy/GeneratedAccountingEnhancedService.cs b/BeWo/ServiceProxy/GeneratedAccountingEnhancedService.cs index cad6ed44e..5ad66fb54 100644 --- a/BeWo/ServiceProxy/GeneratedAccountingEnhancedService.cs +++ b/BeWo/ServiceProxy/GeneratedAccountingEnhancedService.cs @@ -19,7 +19,7 @@ namespace BeWo.ServiceProxy [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IAccountingEnhancedService/GetXRechnung", ReplyAction="http://tempuri.org/IAccountingEnhancedService/GetXRechnungResponse")] [System.ServiceModel.FaultContractAttribute(typeof(BS.Shared.Core.BeWoFault), Action="http://tempuri.org/IAccountingEnhancedService/GetXRechnungBeWoFaultFault", Name="BeWoFault", Namespace="http://schemas.datacontract.org/2004/07/BS.Shared.Core")] - BS.Shared.DataContracts.MikePHPContracts.ApiResponse GetXRechnung(long invoice_base_oid, int report_type, string report_url); + BS.Shared.DataContracts.MikePHPContracts.ApiResponse> GetXRechnung(long invoice_base_oid, int report_type, string report_url); } [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] @@ -56,7 +56,7 @@ namespace BeWo.ServiceProxy { } - public BS.Shared.DataContracts.MikePHPContracts.ApiResponse GetXRechnung(long invoice_base_oid, int report_type, string report_url) + public BS.Shared.DataContracts.MikePHPContracts.ApiResponse> GetXRechnung(long invoice_base_oid, int report_type, string report_url) { return base.Channel.GetXRechnung(invoice_base_oid, report_type, report_url); } diff --git a/BeWo/ViewModel/AbstractDCMapperVM.cs b/BeWo/ViewModel/AbstractDCMapperVM.cs index 028071f01..4f8cdb46a 100644 --- a/BeWo/ViewModel/AbstractDCMapperVM.cs +++ b/BeWo/ViewModel/AbstractDCMapperVM.cs @@ -94,12 +94,28 @@ namespace BeWo.ViewModel if (originalValueProvider != null) { - bool isDirty = !EqualityComparer.Default.Equals(originalValueProvider(), value); + var isDirty = true; + if(DataContract != null) + { + isDirty = !EqualityComparer.Default.Equals(getSafe(originalValueProvider), value); + } StoreDirtyInformation(isDirty, propertyName); } FirePropertyChanged(propertyName); return true; } + + private T getSafe(Func originalValueProvider) + { + try + { + return originalValueProvider(); + } + catch (Exception e) + { + return default(T); + } + } } } \ No newline at end of file diff --git a/BeWo/ViewModel/InvoiceBaseVM.cs b/BeWo/ViewModel/InvoiceBaseVM.cs index 6082dafb4..330772753 100644 --- a/BeWo/ViewModel/InvoiceBaseVM.cs +++ b/BeWo/ViewModel/InvoiceBaseVM.cs @@ -917,11 +917,12 @@ namespace BeWo.ViewModel get; set; } + public StoredFileVM XRechnungStoredFileVM { get; set; } + public long? Oid => DataContract.InvoiceBaseOid; public string SenderStreet => DataContract.SenderStreet; public string SenderTown => DataContract.SenderTown; public string SenderPostCode => DataContract.SenderPostCode; - public StoredFileDC XRechnungStoredFile => DataContract.XRechnungStoredFile; public IList IInvoiceItems => throw new NotImplementedException(); @@ -991,6 +992,8 @@ namespace BeWo.ViewModel IsGkvValid = pDataContract.IsGkvValid; IsGkvValidError = pDataContract.IsGkvValidError; IsAlreadyInActiveGkvAbrechnung = pDataContract.IsAlreadyInActiveGkvAbrechnung; + + XRechnungStoredFileVM = new StoredFileVM(pDataContract.XRechnungStoredFile); } protected override InvoiceBaseDC MapToDataContract(InvoiceBaseDC pDataContract, bool doCommit) diff --git a/BeWo/ViewModel/ListViewModel/InvoiceBaseListVM.cs b/BeWo/ViewModel/ListViewModel/InvoiceBaseListVM.cs index fe696ddaa..55614e398 100644 --- a/BeWo/ViewModel/ListViewModel/InvoiceBaseListVM.cs +++ b/BeWo/ViewModel/ListViewModel/InvoiceBaseListVM.cs @@ -7,23 +7,25 @@ using BeWo.ServiceProxy; using BS.Shared.Core; using BS.Shared.DataContracts; using BS.Shared.Extensions; +using DevExpress.DataAccess.Native.Web; using DevExpress.Mvvm; +using DevExpress.Xpf.Editors; using DevExpress.Xpf.Layout.Core; namespace BeWo.ViewModel.ListViewModel { - public class InvoiceBaseListVM : AbstractDCListMapperVM - { - public static string PropertyName_DateTimeSpanFilter = "DateTimeSpanFilter"; + public class InvoiceBaseListVM : AbstractDCListMapperVM + { + public static string PropertyName_DateTimeSpanFilter = "DateTimeSpanFilter"; - public static string PropertyName_FilterResultAmountSum = "FilterResultAmountSum"; - public static string PropertyName_FilterResultAmountNotPaidSum = "FilterResultAmountNotPaidSum"; - - private DateTimeSpan _DateTimeSpanFilter; - private BindingList _FilteredInvoices; + public static string PropertyName_FilterResultAmountSum = "FilterResultAmountSum"; + public static string PropertyName_FilterResultAmountNotPaidSum = "FilterResultAmountNotPaidSum"; - public InvoiceBaseListVM(List pDCs) : base(pDCs) - { + private DateTimeSpan _DateTimeSpanFilter; + private BindingList _FilteredInvoices; + + public InvoiceBaseListVM(List pDCs) : base(pDCs) + { VMList.ListChanged += (s, e) => FirePropertyChanged(PropertyName_FilterResultAmountSum); VMList.ListChanged += (s, e) => FirePropertyChanged(PropertyName_FilterResultAmountNotPaidSum); @@ -31,64 +33,83 @@ namespace BeWo.ViewModel.ListViewModel ShowXRechnungCommand = new DelegateCommand(ShowXRechnung, canExecute); } - public DelegateCommand ShowXRechnungCommand { get; set; } + public DelegateCommand ShowXRechnungCommand { get; set; } public DateTimeSpan DateTimeSpanFilter - { - get - { - return _DateTimeSpanFilter; - } - - set - { - if (AreDifferent(_DateTimeSpanFilter, value)) - { - _DateTimeSpanFilter = value; - FirePropertyChanged(PropertyName_DateTimeSpanFilter); - - } - } - } - - public decimal? FilterResultAmountSum - { - get - { - return VMList.Sum(e => e.TotalAmount); - } - } - public decimal? FilterResultAmountNotPaidSum - { - get - { - decimal? sum = 0.0m; - foreach (var item in VMList) - sum += item.IsPaid ? item.TotalAmount : 0; - - return FilterResultAmountSum - sum; - } - } - - public void ShowXRechnung() - { - var invoice_base_oid = SelectedVM.DataContract.InvoiceBaseOid ?? -1; - var invoice_type = (int)SelectedVM.DataContract.Type; - var report_link = SelectedVM.ReportLink; - - ServiceFacade.DoAccountingEnhancedServiceAsnyc( - x => x.GetXRechnung(invoice_base_oid, invoice_type, report_link), - response => + { + get { - if (response.Success) + return _DateTimeSpanFilter; + } + + set + { + if (AreDifferent(_DateTimeSpanFilter, value)) { - BeWoApp.MainControl.WindowService.ShowERechnungWindow(new MemoryStream(response.Data)); - } - else - { - MessageBox.Show(response.ToErrorString()); + _DateTimeSpanFilter = value; + FirePropertyChanged(PropertyName_DateTimeSpanFilter); + } } + } + + public decimal? FilterResultAmountSum + { + get + { + return VMList.Sum(e => e.TotalAmount); + } + } + public decimal? FilterResultAmountNotPaidSum + { + get + { + decimal? sum = 0.0m; + foreach (var item in VMList) + sum += item.IsPaid ? item.TotalAmount : 0; + + return FilterResultAmountSum - sum; + } + } + + public void ShowXRechnung() + { + var invoice_base = SelectedVM; + + // Holt bereits geladene + if (invoice_base.XRechnungStoredFileVM is StoredFileVM vm && vm.Data is byte[] bytes) + { + BeWoApp.MainControl.WindowService.ShowERechnungWindow(new MemoryStream(bytes)); + return; + } + + // Noch nicht geladen + var invoice_base_dc = invoice_base.DataContract; + var invoice_base_oid = invoice_base_dc.InvoiceBaseOid ?? -1; + var invoice_type = (int)invoice_base_dc.Type; + var report_link = invoice_base.ReportLink; + ServiceFacade.DoAccountingEnhancedServiceAsnyc( + x => x.GetXRechnung(invoice_base_oid, invoice_type, report_link), + response => + { + if (response.Success) + { + if (response.Data.Item1 is StoredFileDC dc) + { + invoice_base.XRechnungStoredFileVM.UpdateByDataContract(dc); + } + + bytes = response.Data.Item2; + + invoice_base.XRechnungStoredFileVM.Data = bytes; + + BeWoApp.MainControl.WindowService.ShowERechnungWindow(new MemoryStream(bytes)); + } + else + { + MessageBox.Show(response.ToErrorString()); + } + } ); } @@ -101,13 +122,13 @@ namespace BeWo.ViewModel.ListViewModel FirePropertyChanged(PropertyName_FilterResultAmountNotPaidSum); } - internal void UpdateList(IList vmList) - { - VMList.Clear(); - VMList.AddRange(vmList); + internal void UpdateList(IList vmList) + { + VMList.Clear(); + VMList.AddRange(vmList); - FirePropertyChanged(PropertyName_FilterResultAmountSum); - FirePropertyChanged(PropertyName_FilterResultAmountNotPaidSum); - } - } + FirePropertyChanged(PropertyName_FilterResultAmountSum); + FirePropertyChanged(PropertyName_FilterResultAmountNotPaidSum); + } + } } \ No newline at end of file diff --git a/BeWo/ViewModel/StoredFileVM.cs b/BeWo/ViewModel/StoredFileVM.cs index c343d6751..181d5d03c 100644 --- a/BeWo/ViewModel/StoredFileVM.cs +++ b/BeWo/ViewModel/StoredFileVM.cs @@ -14,8 +14,9 @@ namespace BeWo.ViewModel private string _Checksum; private int? _BeWoObjectTid; private long? _BeWoObjectOid; + private byte[] _Data; - public StoredFileVM(StoredFileDC dc) : base(dc, dc.Oid) + public StoredFileVM(StoredFileDC dc) : base(dc, dc?.Oid) { } @@ -62,8 +63,17 @@ namespace BeWo.ViewModel set => SetProperty(ref _BeWoObjectOid, value, nameof(BeWoObjectOid), () => DataContract.BeWoObjectOid); } + public byte[] Data + { + get => _Data; + set => SetProperty(ref _Data, value, nameof(Data)); + } + protected override void InitByDataContract(StoredFileDC pDataContract) { + if (pDataContract is null) + return; + _FileName = pDataContract.FileName; _FilePath = pDataContract.FilePath; _ContentType = pDataContract.ContentType; @@ -85,5 +95,19 @@ namespace BeWo.ViewModel return pDataContract; } + + public override void UpdateByDataContract(StoredFileDC pDataContract) + { + if (pDataContract is null) + return; + + FileName = pDataContract.FileName; + FilePath = pDataContract.FilePath; + ContentType = pDataContract.ContentType; + Size = pDataContract.Size; + Checksum = pDataContract.Checksum; + BeWoObjectTid = pDataContract.BeWoObjectTid; + BeWoObjectOid = pDataContract.BeWoObjectOid; + } } } diff --git a/Service/Plugins/XRechnungService.cs b/Service/Plugins/XRechnungService.cs index 2943fb33e..305a41f11 100644 --- a/Service/Plugins/XRechnungService.cs +++ b/Service/Plugins/XRechnungService.cs @@ -1,12 +1,15 @@ using BeWo.Data.Access; using BeWo.Data.Entities; using BeWo.Service.Core; +using BeWo.Service.DCEntityMapper; using BeWo.Service.ServiceImplementations; using BeWo.Service.ServiceProxy; using BeWo.Service.ServiceUtils; using BS.Shared; using BS.Shared.Core; +using BS.Shared.DataContracts; using BS.Shared.DataContracts.MikePHPContracts; +using BS.Shared.Interface; using DevExpress.Export; using DevExpress.Utils.Filtering.Internal; using System; @@ -21,17 +24,21 @@ namespace BeWo.Service.Plugins { public class XRechnungService : AbstractIDSpecificDefaultClass { - public ApiResponse GetXRechnung(long invoice_base_oid, int report_type, string report_url) + public ApiResponse> GetXRechnung(long invoice_base_oid, int report_type, string report_url) { // Hole Parameter var lStoragePath = MergedConfig.GetSetting(WebConfigSetting.StoredTenantFilesFolderPath); var lStorage = DAOFactory.GetLocalTenantFileStorage(lStoragePath); var invoice_base = DAOFactory.GenericDAO.LoadByID(invoice_base_oid); + var stored_file = invoice_base.XRechnungStoredFile; // Falls Lokal vorhanden, gebe lokale Version zurück - if(invoice_base.XRechnungStoredFile?.FileName is string lFileName && tryGetXRechnungLocal(lStorage, lFileName, out var bytes)) - return ApiResponse.SuccessResponse(bytes); + if (stored_file?.FileName is string lFileName && tryGetXRechnungLocal(lStorage, lFileName, out var bytes)) + { + var stored_file_dc = MapperFactory.StoredFile.MapToNewDC(stored_file); + return ApiResponse>.SuccessResponse(new Tuple(stored_file_dc, bytes)); + } // Lokal nicht vorhanden, berechne XRechnung var str = ServiceConfigReader.GetInvoiceTestJsonString(); @@ -42,6 +49,50 @@ namespace BeWo.Service.Plugins return requestXRechnung(lStorage, invoice_base, str, token_url); } + private ApiResponse> requestXRechnung(LocalTenantFileStorageDAO pStorage, InvoiceBase invoice_base, object obj, string token_url) + { + var response = ServiceFacade.XRechnung.Create(obj, token_url); + + var final_response = new ApiResponse>(); + ApiResponse.CopyTo(response, final_response); + + var lFileName = pStorage.GetXRechnungFileName(invoice_base.Oid.Value); + + if (response.Success) + { + try + { + var bytes = response.Data; + StoredFile stored_file = null; + using (MemoryStream ms = new MemoryStream(bytes)) + { + stored_file = pStorage.SaveHybridFile(BWPFeature.XRechnung, lFileName, "application/pdf", ms, TableID.InvoiceBase, invoice_base.Oid.Value); + + invoice_base.XRechnungStoredFile = stored_file; + + DAOFactory.GenericDAO.Update(invoice_base); + } + + var stored_file_dc = MapperFactory.StoredFile.MapToNewDC(stored_file); + final_response.Data = new Tuple(stored_file_dc, bytes); + } + catch (Exception ex) + { + try + { + ServiceHelper.CoreLogger.LogError(ex, nameof(GetXRechnung)); + } + catch (Exception ex2) + { + response.ErrorStrings.Add("AccountingEnhancedServiceImp: " + ex2.ToString()); + } + response.ErrorStrings.Add("AccountingEnhancedServiceImp: " + ex.ToString()); + } + } + + return final_response; + } + private string GetTokenUrl(string report_url) { string download_url; @@ -56,43 +107,6 @@ namespace BeWo.Service.Plugins return token_url; } - private ApiResponse requestXRechnung(LocalTenantFileStorageDAO pStorage, InvoiceBase invoice_base, object obj, string token_url) - { - // Request - var response = ServiceFacade.XRechnung.Create(obj, token_url); - - var lFileName = pStorage.GetXRechnungFileName(invoice_base.Oid.Value); - - if (response.Success) - { - try - { - var bytes = response.Data; - using (MemoryStream ms = new MemoryStream(bytes)) - { - var file = pStorage.SaveHybridFile(BWPFeature.XRechnung, lFileName, "application/pdf", ms, TableID.InvoiceBase, invoice_base.Oid.Value); - - invoice_base.XRechnungStoredFile = file; - - DAOFactory.GenericDAO.Update(invoice_base); - } - } - catch (Exception ex) - { - try - { - ServiceHelper.CoreLogger.LogError(ex, nameof(GetXRechnung)); - } - catch (Exception ex2) - { - response.ErrorStrings.Add("AccountingEnhancedServiceImp: " + ex2.ToString()); - } - response.ErrorStrings.Add("AccountingEnhancedServiceImp: " + ex.ToString()); - } - } - - return response; - } private bool tryGetXRechnungLocal(LocalTenantFileStorageDAO pStorage, string pFileName, out byte[] bytes) { diff --git a/Service/ServiceContracts/Enhanced/IAccountingEnhancedService.cs b/Service/ServiceContracts/Enhanced/IAccountingEnhancedService.cs index 530d9de33..4fb0d3d83 100644 --- a/Service/ServiceContracts/Enhanced/IAccountingEnhancedService.cs +++ b/Service/ServiceContracts/Enhanced/IAccountingEnhancedService.cs @@ -19,6 +19,6 @@ namespace BeWo.Service.ServiceContracts.Enhanced { [FaultContract(typeof(BeWoFault))] [OperationContract] - ApiResponse GetXRechnung(long invoice_base_oid, int report_type, string report_url); + ApiResponse> GetXRechnung(long invoice_base_oid, int report_type, string report_url); } } \ No newline at end of file diff --git a/Service/ServiceImplementations/Enhanced/AccountingEnhancedServiceImp.cs b/Service/ServiceImplementations/Enhanced/AccountingEnhancedServiceImp.cs index 09caf905f..e19eec014 100644 --- a/Service/ServiceImplementations/Enhanced/AccountingEnhancedServiceImp.cs +++ b/Service/ServiceImplementations/Enhanced/AccountingEnhancedServiceImp.cs @@ -33,7 +33,7 @@ namespace BeWo.Service.ServiceImplementations.Enhanced [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)] public class AccountingEnhancedServiceImp : IAccountingEnhancedService { - public ApiResponse GetXRechnung(long invoice_base_oid, int report_type, string report_url) + public ApiResponse> GetXRechnung(long invoice_base_oid, int report_type, string report_url) { var service = PluginLoader.FindClass(); return service.GetXRechnung(invoice_base_oid, report_type, report_url); diff --git a/Shared/DataContracts/MikePHPContracts/ApiResponse.cs b/Shared/DataContracts/MikePHPContracts/ApiResponse.cs index 78bd6d7ee..8c9f94d3d 100644 --- a/Shared/DataContracts/MikePHPContracts/ApiResponse.cs +++ b/Shared/DataContracts/MikePHPContracts/ApiResponse.cs @@ -28,6 +28,22 @@ namespace BS.Shared.DataContracts.MikePHPContracts public bool Success => (Errors?.Count ?? 0) == 0 && (ErrorStrings?.Count ?? 0) == 0; + public ApiResponse() + { + + } + + public static void CopyTo(ApiResponse from, ApiResponse to) + { + to.Errors = from.Errors; + to.ErrorStrings = from.ErrorStrings; + to.Messages = from.Messages; + to.Meta = from.Meta; + to.TraceId = from.TraceId; + to.Timestamp = from.Timestamp; + to.StatusCode = from.StatusCode; + } + //Factory Methods public static ApiResponse SuccessResponse(T data, List messages = null, Dictionary meta = null) {