1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| public string CreateBLVente(string DO_Tiers, List<DocLigneDataModel> docLignesDocAcahat)
{
StringBuilder log = new StringBuilder();
Console.WriteLine("Open connection w/ Sage");
if (conn.OpenBaseCompta() && conn.OpenBaseGesCom())
{
try
{
Console.WriteLine("Creating doc");
// Création d'un objet processus "Création de document
IPMDocument processDoc = conn.BaseGesCom.CreateProcess_Document(DocumentType.DocumentTypeVenteLivraison);
// Conversion du document du processus (IBODocument3) dans le type du document de destination : Facture de vente (IBODocumentVente3)
IBODocumentVente3 doc = (IBODocumentVente3)processDoc.Document;
// Affectation du cliente au document
//doc.SetDefaultClient(conn.BaseCompta.FactoryClient.ReadNumero(docEntete.DO_Tiers));
Console.WriteLine($"Adding Client: {DO_Tiers}");
doc.SetDefaultClient(conn.BaseGesCom.CptaApplication.FactoryClient.ReadNumero(DO_Tiers));
Console.WriteLine("DocEntete Create");
// Ajoute les lignes d'articles
foreach (var docLigne in docLignesDocAcahat)
{
// Ligne du nouveau document vente
IBODocumentLigne3 omDocLigneVente = processDoc.AddArticle(conn.BaseGesCom.FactoryArticle.ReadReference(docLigne.AR_Ref), double.Parse(docLigne.DL_Qte.ToString()));
omDocLigneVente.DO_Ref = docLigne.DO_Ref;
if (docLigne.AR_SuiviStock == 1)
{
omDocLigneVente.LS_NoSerie = new FactureData().GetLsNoSerie(docLigne.DL_No);
}
}
Console.WriteLine("DocLignesAdded");
// Si le document est cohérent et peut être écrit en base
if (processDoc.CanProcess)
{
log.Append($"Doc Processed {doc.DO_Piece}");
// Génération de document dans la base
processDoc.Process();
}
else
{
Console.WriteLine("\nDocument cannot be processed");
Console.WriteLine("================================================================");
// Récuperation des erreurs
for (int i = 1; i == processDoc.Errors.Count; i++)
{
IFailInfo iFail = processDoc.Errors[i];
Console.WriteLine($"Error code: {iFail.ErrorCode} \nIndice: {iFail.Indice} \nDescription: {iFail.Text}\n");
}
}
}
catch (Exception ex)
{
log.Append($"Error: {ex.Message}\nStackTrace: {ex.StackTrace}");
Console.WriteLine($"Error: {ex.Message}\nStackTrace:\n{ex.StackTrace}");
}
finally
{
// Fermeture de connexion
conn.CloseBaseGesCom();
conn.CloseBaseCompta();
}
}
else
{
Console.WriteLine("\nClosing connection");
log.Append("Failed to open connection");
Console.WriteLine("Failed to open connection");
}
return log.ToString();
} |
Partager