public void ImportEinvoiceIntoBilling2()
{
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);
if (message == null)
{
MercatorUi.Globals.MercatorTasksToMain.Log("Message non trouvé", isError: true);
return;
}
MercatorPeppol.ReceivedDoc.ParseContentRet parsedContent = message.ReceivedDoc.ParseContent(MercatorUi.Globals.Langue);
if (string.IsNullOrEmpty(parsedContent.BuyerReference) || string.IsNullOrEmpty(Api.NumOnly(parsedContent.BuyerReference)) || !Int64.TryParse(Api.NumOnly(parsedContent.BuyerReference), out Int64 pieceCommande))
{
MercatorUi.Globals.MercatorTasksToMain.Log("La référence est vide ou invalide. Impossible de trouver la commande. " + parsedContent.BuyerReference, isError: true);
return;
}
List<(string id, string journal, Int64 piece, Int16 type)> pieds_a = Api.Zselect<(string id, string journal, Int64 piece, Int16 type)>(MercatorUi.Globals.RepData,
"select id,journal,piece,type from PIEDS_A (NOLOCK) where (journal='COMMF') and (piece=@piece) and (pieds_a.n_lignes2 > 0)",
new MercatorSqlParam("@piece", pieceCommande));
if (pieds_a == null)
{
MercatorUi.Globals.MercatorTasksToMain.Log("Recherche commande : " + Api.LastError, isError: true);
return;
}
if (!pieds_a.Any())
{
MercatorUi.Globals.MercatorTasksToMain.Log("Commande non trouvée : " + pieceCommande, isError: true);
return;
}
MercatorUi.Forms.Gescom.GescomClasses.ImportFromEinvoiceDescriptor importFromEinvoiceDescriptor = new MercatorUi.Forms.Gescom.GescomClasses.ImportFromEinvoiceDescriptor
{
Silent = true,
Journal = "FactF",
ReplacementItemId = "ARTIMC6XS9",
PreviousDocs = new MercatorUi.Engine.Gescom.Tools.BillingDocDescriptor[] { new MercatorUi.Engine.Gescom.Tools.BillingDocDescriptor(MercatorUi.Engine.Gescom.Billing.TypeVAEnum.A, pieds_a[0].type, pieds_a[0].id, pieds_a[0].journal, pieds_a[0].piece) }
};
using (MercatorUi.Engine.Gescom.BillingEngine billingEngine = message.ImportIntoBilling(importFromEinvoiceDescriptor, out string error))
{
if (error != null)
{
MercatorUi.Globals.MercatorTasksToMain.Log(error, isError: true);
return;
}
if (!string.IsNullOrEmpty(importFromEinvoiceDescriptor.Warning))
billingEngine.PiedsVRecord.NOTE1 = importFromEinvoiceDescriptor.Warning;
if (!billingEngine.Save())
{
MercatorUi.Globals.MercatorTasksToMain.Log("Erreur lors de l'enregistrement du document : " + billingEngine.LastError, isError: true);
return;
}
if (!message.RemoveFromListAndDb())
{
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));
}
}
}