/// <summary>
        /// Envoie la commande via le service intermag
        /// </summary>
        /// <param name="dsCde"></param>
        /// <param name="targetDb"></param>
        public bool sendSqlCde(DataSet dsCde, string targetDb)
        {
            try
            {
                int          typeCde = getTypeCde(dsCde);
                StringWriter sw      = new StringWriter();
                dsCde.WriteXml(sw, XmlWriteMode.IgnoreSchema);

                Log($"Base sélectionnée : {targetDb}");

                // Si ok call le service distant
                IntermagServiceClient client = createClient(targetDb);
                if (client.IsAlive())
                {
                    Log("Serveur en ligne ...");
                    client.Close();

                    client = createClient(targetDb);
                    string result = client.CreateCommand(sw.ToString(), targetDb, typeCde);

                    if (result.StartsWith("[OK]"))
                    {
                        string[] datas    = result.Split(';');
                        string   magPiece = datas[1];
                        Log(datas[2]);

                        // Modifie le doc
                        editCde((string)dsCde.Tables[0].Rows[0]["DO_Piece"], magPiece, dsCde, targetDb, typeCde);
                        client.Close();
                        return(true);
                    }
                    else
                    {
                        Log($"{targetDb}::{result}");
                        client.Close();
                        return(false);
                    }
                }
                else
                {
                    Log($"{dbName}::ERREUR : Serveur '{targetDb}' indisponible");

                    return(false);
                }
            }
            catch (Exception e)
            {
                Log($"{dbName}::ERREUR : {e.Message}");
                EventLog.WriteEntry("Application", e.ToString(), EventLogEntryType.Error);
                return(false);
            }
        }
Пример #2
0
        public void Disconnect(IList users)
        {
            IntermagServiceClient client = createClient();

            foreach (User user in users)
            {
                if (user.hostProcessId != "")
                {
                    // Vrai connexion Sage
                    client.killUser(dbName, user.hostname, user.ntUsername, user.hostProcessId, user.cbSession);
                }
                else
                {
                    // Connexion fantôme
                    client.deleteUserSession(dbName, user.cbSession);
                }
            }

            client.Close();
        }
        /// <summary>
        /// Cde Retro: Duplique le bon de commande fournisseur en cde magasin
        /// </summary>
        /// <param name="docOrigin"></param>
        /// <param name="magPieces"></param>
        /// <param name="targetDb"></param>
        private void editCommandeRetro(IBODocumentAchat3 docOrigin, string magPieces, string targetDb)
        {
            try
            {
                // Commande RETRO
                IBOFournisseur3 fourn;

                using (SqlConnection cnx = new SqlConnection(cnxString))
                {
                    cnx.Open();
                    using (SqlCommand cmd = cnx.CreateCommand())
                    {
                        cmd.CommandText =
                            "SELECT CT_Num FROM F_COMPTET WHERE CT_Classement = @ctClassement AND CT_Type = 1";
                        cmd.Parameters.AddWithValue("@ctClassement", targetDb);
                        using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow))
                        {
                            if (reader.HasRows)
                            {
                                reader.Read();
                                fourn = GetInstance().CptaApplication.FactoryFournisseur.ReadNumero(reader["CT_Num"].ToString());
                            }
                            else
                            {
                                throw new Exception($"Fournisseur '{targetDb}' non trouvé");
                            }
                        }
                    }
                }

                // Duplication du doc d'origine en document d'achat magasin
                IPMDocument       procDoc = GetInstance().CreateProcess_Document(DocumentType.DocumentTypeAchatCommandeConf);
                IBODocumentAchat3 docNew  = (IBODocumentAchat3)procDoc.Document;

                docNew.SetDefaultDO_Piece();
                docNew.DO_Statut = DocumentStatutType.DocumentStatutTypeConfirme;
                docNew.DO_Ref    = magPieces;
                docNew.SetDefaultFournisseur(fourn);
                docNew.Collaborateur = docOrigin.Collaborateur;

                IBODocumentAchatLigne3 docNewLigne;

                foreach (IBODocumentAchatLigne3 docOrigineLigne in docOrigin.FactoryDocumentLigne.List)
                {
                    if (docOrigineLigne.Article == null)
                    {
                        // Ligne de commentaire
                        docNewLigne           = (IBODocumentAchatLigne3)docNew.FactoryDocumentLigne.Create();
                        docNewLigne.DL_Design = docOrigineLigne.DL_Design;
                        docNewLigne.Write();
                    }
                    else
                    {
                        docNewLigne = (IBODocumentAchatLigne3)addArticleFromLigne(procDoc, docOrigineLigne, docOrigineLigne.DL_Qte);
                        docNewLigne.AF_RefFourniss  = docOrigineLigne.AF_RefFourniss;
                        docNewLigne.Taxe[0]         = GetInstance().CptaApplication.FactoryTaxe.ReadCode("8");
                        docNewLigne.DL_PrixUnitaire = docOrigineLigne.DL_MontantHT / docOrigineLigne.DL_Qte;
                        docNewLigne.SetDefaultRemise();
                        docNewLigne.DO_Ref            = docOrigineLigne.InfoLibre["DLNo"];
                        docNewLigne.TxtComplementaire = docOrigineLigne.TxtComplementaire;
                        docNewLigne.DL_Design         = docOrigineLigne.DL_Design;
                        docNewLigne.Write();
                    }
                }

                if (procDoc.CanProcess)
                {
                    procDoc.Process();

                    foreach (IBODocumentAchatLigne3 ligne in GetDocument(docNew.DO_Piece).FactoryDocumentLigne.List)
                    {
                        if (ligne.Article != null && (ligne.Article.AR_Ref == "DIVERS" || ligne.Article.Famille.FA_CodeFamille == "UNIQUE"))
                        {
                            new DiversRepository().saveLigneAchat(ligne);
                        }
                    }

                    using (SqlConnection cnx = new SqlConnection(cnxString))
                    {
                        cnx.Open();
                        // Met à jour les infos libres
                        using (SqlCommand cmd = cnx.CreateCommand())
                        {
                            cmd.CommandText = @"UPDATE F_DOCENTETE 
                                                SET [Date Statut] = GETDATE(), [RETRO_FOURN] = @retroFourn 
                                                WHERE DO_Piece = @doPiece";
                            cmd.Parameters.AddWithValue("@doPiece", docNew.DO_Piece);
                            cmd.Parameters.AddWithValue("@retroFourn", docOrigin.Fournisseur.CT_Num);
                            cmd.ExecuteNonQuery();
                        }
                    }

                    docNew.Refresh();
                }
                else
                {
                    throw new Exception(GetProcessError(procDoc));
                }

                // Met à jour la référence du doc origine
                docOrigin.DO_Ref    = $"Voir {docNew.DO_Piece}";
                docOrigin.DO_Statut = DocumentStatutType.DocumentStatutTypeConfirme;
                docOrigin.Write();

                // Contremarque
                using (SqlConnection cnx = new SqlConnection(cnxString))
                {
                    cnx.Open();
                    SqlTransaction transaction = cnx.BeginTransaction();

                    try
                    {
                        using (SqlCommand cmd = cnx.CreateCommand())
                        {
                            cmd.Transaction = transaction;
                            // Recup les lignes du doc d'origine
                            cmd.CommandText = @"SELECT CM.DL_NoIn, CM.DL_NoOut, CM_Qte
                                                FROM F_CMLIEN CM
                                                JOIN F_DOCLIGNE DL ON DL.DL_No = CM.DL_NoIn
                                                WHERE DL.DO_PIECE = @doPiece  ";
                            cmd.Parameters.AddWithValue("@doPiece", docOrigin.DO_Piece);

                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                if (reader.HasRows)
                                {
                                    // Y a de la contremarque sur le doc
                                    while (reader.Read())
                                    {
                                        int dlNoIn = (int)reader["DL_NoIn"];
                                        using (SqlCommand cmd3 = cnx.CreateCommand())
                                        {
                                            cmd3.Transaction = transaction;
                                            cmd3.CommandText =
                                                "UPDATE F_CMLIEN SET DL_NoIn = (SELECT DL_No FROM F_DOCLIGNE WHERE DO_Ref = @dlNoIn) WHERE DL_NoIn = @dlNoIn";
                                            cmd3.Parameters.AddWithValue("@dlNoIn", dlNoIn.ToString());
                                            cmd3.ExecuteNonQuery();
                                        }

                                        // On remplace DO_Ref par sa valeur réelle
                                        using (SqlCommand cmdUpd = cnx.CreateCommand())
                                        {
                                            cmdUpd.Transaction = transaction;
                                            cmdUpd.CommandText = "UPDATE F_DOCLIGNE SET DO_Ref = @doRef WHERE DO_Ref = @uid";
                                            cmdUpd.Parameters.AddWithValue("@doRef", magPieces);
                                            cmdUpd.Parameters.AddWithValue("@uid", docOrigin.DO_Piece + dlNoIn);
                                            cmdUpd.ExecuteNonQuery();
                                        }
                                    }
                                }
                            }

                            transaction.Commit();
                        }
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        throw new Exception(e.ToString());
                    }
                }

                Log($"{dbName} :: Doc Achat {docOrigin.DO_Piece} dupliqué vers le doc achat {docNew.DO_Piece}");

                // Met à jour la commande distante
                IntermagServiceClient client = createClient(targetDb);
                client.SetDoRef(docNew.DO_Piece, targetDb, magPieces.Split('/')[0]);
                client.Close();

                // Suppression du document fournisseur d'origine
                if (MessageBox.Show(
                        "Souhaitez-vous supprimer le document fournisseur d'origine?",
                        "Suppression doc origine",
                        MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    docOrigin.Remove();
                }
                else
                {
                    Log($"Vous devrez supprimer le document d'achat n° {docOrigin.DO_Piece} manuellement.");
                    // Dévérouille le doc
                    docOrigin.Read();
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }