Skip to content

Commit

Permalink
Standard voucher importation web service and use case added
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcota committed Nov 11, 2021
1 parent 773e63a commit 237e9d8
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/* Empiria Financial *****************************************************************************************
* *
* Module : Banobras Integration Services Component : Vouchers Importer *
* Assembly : FinancialAccounting.BanobrasIntegration.dll Pattern : Input Data Holder *
* Type : VoucherImportationCommand License : Please read LICENSE.txt file *
* *
* Summary : Command payload used to import vouchers using rules and coming from external systems. *
* *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System;
using Empiria.FinancialAccounting.Vouchers;

namespace Empiria.FinancialAccounting.BanobrasIntegration.VouchersImporter.Adapters {

/// <summary>Command payload used to import vouchers using rules and coming from external systems.</summary>
public class VoucherImportationCommand {

public string SystemUID {
get; set;
} = string.Empty;


public string ImportationRuleUID {
get; set;
} = string.Empty;


public ToImportVoucherDto[] Vouchers {
get; set;
} = new ToImportVoucherDto[0];

} // class VoucherImportationCommand


public class ToImportVoucherDto {

public string ImportationSet {
get; set;
} = string.Empty;


public string UniqueID {
get; set;
} = string.Empty;


public string LedgerCode {
get; set;
} = string.Empty;


public string Concept {
get; set;
} = string.Empty;


public DateTime AccountingDate {
get; internal set;
}


public DateTime RecordingDate {
get; internal set;
}


public string VoucherTypeUID {
get; set;
} = string.Empty;


public string TransactionTypeUID {
get; set;
} = string.Empty;


public string FunctionalAreaCode {
get; set;
} = string.Empty;


public string ElaboratedByCode {
get; set;
} = string.Empty;


public string OperationNumber {
get; set;
} = string.Empty;


public ToImportVoucherEntryDto[] Entries {
get; set;
} = new ToImportVoucherEntryDto[0];


} // class ToImportVoucherDto


public class ToImportVoucherEntryDto {

public string AccountNumber {
get; set;
} = string.Empty;


public string SectorCode {
get; set;
} = string.Empty;


public string SubledgerAccountNumber {
get; set;
} = string.Empty;


public string ResponsibilityAreaCode {
get; set;
} = string.Empty;


public string BudgetConcept {
get; set;
} = string.Empty;


public string EventTypeCode {
get; set;
} = string.Empty;


public string VerificationNumber {
get; set;
} = string.Empty;


public VoucherEntryType VoucherEntryType {
get; set;
}


public DateTime Date {
get; set;
} = ExecutionServer.DateMinValue;


public string Concept {
get; set;
} = string.Empty;


public string CurrencyCode {
get; set;
} = string.Empty;


public decimal Amount {
get; set;
}


public decimal ExchangeRate {
get; set;
}


public bool Protected {
get; set;
}


public string OperationNumber {
get; set;
} = string.Empty;


} // class ToImportVoucherEntryDto

} // namespace Empiria.FinancialAccounting.BanobrasIntegration.VouchersImporter.Adapters
167 changes: 167 additions & 0 deletions BanobrasIntegration/VouchersImporter/Domain/StandardVoucherImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/* Empiria Financial *****************************************************************************************
* *
* Module : Banobras Integration Services Component : Vouchers Importer *
* Assembly : FinancialAccounting.BanobrasIntegration.dll Pattern : Service provider *
* Type : StandardVoucherImporter License : Please read LICENSE.txt file *
* *
* Summary : Performs batch voucher importation tasks from a standard structure. *
* *
************************* Copyright(c) La Vía Óntica SC, Ontica LLC and contributors. All rights reserved. **/
using System.Collections.Generic;
using System.Linq;

using Empiria.FinancialAccounting.Vouchers.Adapters;
using Empiria.FinancialAccounting.Vouchers.UseCases;

using Empiria.FinancialAccounting.BanobrasIntegration.VouchersImporter.Adapters;

namespace Empiria.FinancialAccounting.BanobrasIntegration.VouchersImporter {

/// <summary>Performs voucher importation tasks from a standard structure
/// adapted from distinct sources.</summary>
internal class StandardVoucherImporter {

private readonly VoucherImportationCommand _command;
private readonly FixedList<ToImportVoucher> _toImportVouchersList;

#region Public methods

internal StandardVoucherImporter(VoucherImportationCommand command) {
Assertion.AssertObject(command, "command");

_command = command;
_toImportVouchersList = new FixedList<ToImportVoucher>();
}


internal ImportVouchersResult DryRunImport() {
var result = new ImportVouchersResult();

result.VoucherTotals = GetImportVoucherTotals();

result.Errors = GetImportErrors();
result.Warnings = GetImportWarnings();

return result;
}


internal ImportVouchersResult Import() {
ImportVouchersResult result = this.DryRunImport();

//if (result.HasErrors) {
// return result;
//}

using (var usecases = VoucherEditionUseCases.UseCaseInteractor()) {
foreach (ToImportVoucher voucher in _toImportVouchersList) {
VoucherImporterDataService.StoreVoucher(voucher);
VoucherImporterDataService.StoreVoucherIssues(voucher);

if (voucher.HasErrors) {
continue;
}

VoucherFields voucherFields = MapToVoucherFields(voucher.Header);
FixedList<VoucherEntryFields> entriesFields = MapToVoucherEntriesFields(voucher.Entries);

usecases.ImportVoucher(voucherFields, entriesFields, true); // _command.TryToCloseVouchers)
}
}

return result;
}


#endregion Public methods

#region Private methods

private FixedList<NamedEntityDto> GetImportErrors() {
var errors = this._toImportVouchersList.SelectMany(
z => z.AllIssues.FindAll(w => w.Type == VoucherIssueType.Error));

return new FixedList<NamedEntityDto>(errors.Select(x => x.ToNamedEntity()));
}


private FixedList<NamedEntityDto> GetImportWarnings() {
var warnings = this._toImportVouchersList.SelectMany(
z => z.AllIssues.FindAll(w => w.Type == VoucherIssueType.Warning));

return new FixedList<NamedEntityDto>(warnings.Select(x => x.ToNamedEntity()));
}


private FixedList<ImportVouchersTotals> GetImportVoucherTotals() {
var importationSets = this._toImportVouchersList.Select(x => x.Header.ImportationSet)
.Distinct();

var list = new List<ImportVouchersTotals>(importationSets.Count());

foreach (string set in importationSets) {
var totals = new ImportVouchersTotals {
UID = set,
Description = set
};
var setVouchers = this._toImportVouchersList.FindAll(x => x.Header.ImportationSet.Equals(set));

totals.VouchersCount = setVouchers.Count;
totals.ErrorsCount = setVouchers.Sum(x => x.AllIssues.Count(y => y.Type == VoucherIssueType.Error));
totals.WarningsCount = setVouchers.Sum(x => x.AllIssues.Count(y => y.Type == VoucherIssueType.Warning));

list.Add(totals);
}

return list.ToFixedList();
}


private FixedList<VoucherEntryFields> MapToVoucherEntriesFields(FixedList<ToImportVoucherEntry> entries) {
return new FixedList<VoucherEntryFields>(entries.Select(x => MapToVoucherEntryFields(x)));
}


private VoucherFields MapToVoucherFields(ToImportVoucherHeader header) {
return new VoucherFields {
Concept = header.Concept,
AccountingDate = header.AccountingDate,
RecordingDate = header.RecordingDate,
ElaboratedByUID = header.ElaboratedBy.UID,
LedgerUID = header.Ledger.UID,
TransactionTypeUID = header.TransactionType.UID,
VoucherTypeUID = header.VoucherType.UID,
FunctionalAreaId = header.FunctionalArea.Id
};
}


private VoucherEntryFields MapToVoucherEntryFields(ToImportVoucherEntry entry) {
return new VoucherEntryFields {
LedgerAccountId = entry.LedgerAccount.Id,
SubledgerAccountId = entry.SubledgerAccount.Id,
StandardAccountIdForCreateLedgerAccount = entry.StandardAccount.Id,
SubledgerAccountNoToCreate = entry.SubledgerAccountNo,
SectorId = entry.Sector.Id,
ResponsibilityAreaId = entry.ResponsibilityArea.Id,
BudgetConcept = entry.BudgetConcept,
EventTypeId = entry.EventType.Id,
VerificationNumber = entry.VerificationNumber,
Date = entry.Date,
Concept = entry.Concept,
VoucherEntryType = entry.VoucherEntryType,
CurrencyUID = entry.Currency.UID,
Amount = entry.Amount,
ExchangeRate = entry.ExchangeRate,
BaseCurrencyAmount = entry.BaseCurrencyAmount,
Protected = entry.Protected,
CreateLedgerAccount = entry.CreateLedgerAccount,
CreateSubledgerAccount = entry.CreateSubledgerAccount
};
}

#endregion Private methods

} // class StandardVoucherImporter

} // namespace Empiria.FinancialAccounting.BanobrasIntegration.VouchersImporter
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ public FixedList<ToImportVoucherIssue> Issues {
get; internal set;
}

} // class StandardVoucherHeader
} // class ToImportVoucherHeader

} // namespace Empiria.FinancialAccounting.BanobrasIntegration.VouchersImporter
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,30 @@ static public ImportVouchersUseCases UseCaseInteractor() {

#endregion Constructors and parsers

#region Database importers

#region Standard voucher importation

public ImportVouchersResult DryRunStandardVoucherImportation(VoucherImportationCommand command) {
Assertion.AssertObject(command, "command");

var importer = new StandardVoucherImporter(command);

return importer.DryRunImport();
}


public ImportVouchersResult StandardVoucherImportation(VoucherImportationCommand command) {
Assertion.AssertObject(command, "command");

var importer = new StandardVoucherImporter(command);

return importer.Import();
}

#endregion Standard voucher importation


#region Database importers

public ImportVouchersResult ImportVouchersFromDatabase(ImportVouchersCommand command) {
Assertion.AssertObject(command, "command");
Expand Down
Loading

0 comments on commit 237e9d8

Please sign in to comment.