public void ImportEinvoiceIntoBilling1()
{
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;
}
var 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;
}
MercatorUi.Forms.Gescom.GescomClasses.ImportFromEinvoiceDescriptor importFromEinvoiceDescriptor = new MercatorUi.Forms.Gescom.GescomClasses.ImportFromEinvoiceDescriptor
{
Silent = true,
Journal = "BrouA", // importer vers un brouillon d'achats
ReplacementItemId = "ARTIMC6XS9" // si l'article n'est pas identifier, cet article sera utilisé
};
using (MercatorUi.Engine.Gescom.BillingEngine billingEngine = message.ImportIntoBilling(importFromEinvoiceDescriptor, out string error))
{
if (error != null)
{
MercatorUi.Globals.MercatorTasksToMain.Log(error, isError: true);
return;
}
billingEngine.PeppolBoxId = message.Id;
// ici on peut ajouter dans l'écriture comptable des informations venant du contenu XML . Par exemple le nom et l'adresse mail du contact
MercatorPeppol.ReceivedDoc.ParseContentRet parsedContent = message.ReceivedDoc.ParseContent(MercatorUi.Globals.Langue);
billingEngine.PiedsVRecord.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;
// on récupère les warnings éventuels
if (!string.IsNullOrEmpty(importFromEinvoiceDescriptor.Warning))
billingEngine.PiedsVRecord.NOTE2 = importFromEinvoiceDescriptor.Warning;
if (!billingEngine.Save())
{
MercatorUi.Globals.MercatorTasksToMain.Log("Erreur lors de l'enregistrement du document : " + billingEngine.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", billingEngine.Journal, billingEngine.Piece));
}
}
}