public void ImportEinvoiceIntoAccounting()
{
using (MercatorUi.Forms.Other.OtherClasses.PeppolBoxHelper peppolBoxHelper = new MercatorUi.Forms.Other.OtherClasses.PeppolBoxHelper())
{
if (peppolBoxHelper.DataTable == null)
{
MercatorUi.Globals.MercatorTasksToMain.Log("Erreur lors de l'initialisation du PeppolBoxHelper : " + Api.LastError, isError: true);
return;
}
MercatorUi.Forms.Other.OtherClasses.PeppolBoxHelper.PeppolMessage message = peppolBoxHelper.Messages.FirstOrDefault(m => m.IsInvoiceOrCreditNote); // mettre ici une logique de sélection du message à importer
if (message == null)
{
MercatorUi.Globals.MercatorTasksToMain.Log("Message non trouvé", isError: true);
return;
}
using (MercatorUi.Engine.Cpta.BookingEngine bookingEngine = message.ImportIntoAccounting(new MercatorUi.Forms.Accounting.AccountingClasses.ImportFromEinvoiceDescriptor { Silent = true, Journal = "ACH" }, out string error))
{
if (error != null)
{
MercatorUi.Globals.MercatorTasksToMain.Log(error, isError: true);
return;
}
bookingEngine.PeppolBoxId = message.Id;
// ici on peut ajouter dans l'écriture comptable des informations provenant du contenu XML. Par exemple le nom et l'adresse mail du contact
MercatorPeppol.ReceivedDoc.ParseContentRet parsedContent = message.ReceivedDoc.ParseContent(MercatorUi.Globals.Langue);
bookingEngine.PiedsCRecord.NOTE1 = parsedContent.XmlSelectNodes("cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:Name").FirstOrDefault()?.InnerText + " "
+ parsedContent.XmlSelectNodes("cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail").FirstOrDefault()?.InnerText;
if (!bookingEngine.Save())
{
MercatorUi.Globals.MercatorTasksToMain.Log("Erreur lors de l'enregistrement du document comptable : " + bookingEngine.LastError, isError: true);
return;
}
if (!message.RemoveFromListAndDb()) // supprimer le message de la PeppolBox
{
MercatorUi.Globals.MercatorTasksToMain.Log("Erreur lors de la suppression du message : " + Api.LastError, isError: true);
return;
}
MercatorUi.Globals.MercatorTasksToMain.Log(string.Format("Import terminé {0} {1} correctement", bookingEngine.Journal, bookingEngine.Piece));
}
}
}