Advertisement
Guest User

WS

a guest
Feb 18th, 2020
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 71.11 KB | None | 0 0
  1. using System;
  2. using System.Web.Services;
  3. using Microsoft.Xrm.Sdk;
  4. using Microsoft.Xrm.Tooling.Connector;
  5. using WebServiceCrm.Properties;
  6. using Microsoft.Xrm.Sdk.Query;
  7. using System.Net;
  8. //using CrmEarlyBound;
  9. using Microsoft.Crm.Sdk.Messages;
  10.  
  11. namespace WebServiceCrm
  12. {
  13.     //System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  14.  
  15.     [WebService(Namespace = "WebServiceCrm")]
  16.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  17.     [System.ComponentModel.ToolboxItem(false)]
  18.  
  19.  
  20.     public class WebService : System.Web.Services.WebService
  21.     {
  22.  
  23.         /*static public OrganizationServiceProxy _serviceProxy;
  24.         static public string connectionString = @"AuthType = Office365; Url=" + Settings.Default.url + "; Username=" + Settings.Default.utilisateur + "; Password=" + Settings.Default.mdp + "";
  25.         static public CrmServiceClient conn = new CrmServiceClient(connectionString);
  26.         static public IOrganizationService _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
  27.         */
  28.         [WebMethod]
  29.         public String Devis(string initiateur, string customerRefERP, int revision, int status, float amount, string nomTarif, string description, string pdf, string division, string contractNumber, DateTime dateCommande, DateTime dateSoumission)
  30.         {
  31.             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  32.             string connectionString = @"AuthType = Office365; Url=" + Settings.Default.url + "; Username=" + Settings.Default.utilisateur + "; Password=" + Settings.Default.mdp + "";
  33.             CrmServiceClient conn = new CrmServiceClient(connectionString);
  34.  
  35.             IOrganizationService _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
  36.  
  37.             // CONTROLE ARGUMENTS REQUETE
  38.             if (revision < 0)
  39.             {
  40.                 return "NOK - REVISION MUST BE GREATER THAN 0";
  41.             }
  42.             if (string.IsNullOrEmpty(initiateur) || (initiateur.Length > 55))
  43.             {
  44.                 return "NOK - INITIATEUR MUST BE NOT NULL AND LOWER THAN 55 CHARACTERS";
  45.             }
  46.             if ((status != 1) && (status != 2))
  47.             {
  48.                 return "NOK - STATUS MUST BE EQUAL TO 1 FOR SEND TO THE CUSTOMER OR 2 FOR WON";
  49.             }
  50.             if (float.IsNaN(amount))
  51.             {
  52.                 return "NOK - AMOUNT MUST BE NOT NULL";
  53.             }
  54.             if (string.IsNullOrEmpty(nomTarif))
  55.             {
  56.                 return "NOK - TARIF MUST BE NOT NULL";
  57.             }
  58.             if (string.IsNullOrEmpty(customerRefERP))
  59.             {
  60.                 return "NOK - REF ERP MUST BE NOT NULL";
  61.             }
  62.             if (description.Length > 1999)
  63.             {
  64.                 return "NOK - DESCRIPTION MUST BE LOWER THAN 2000 CHARACTERS";
  65.             }
  66.             if ((status == 2) && string.IsNullOrEmpty(dateCommande.ToString()))
  67.             {
  68.                 return "NOK - STATUS IS WON AND ORDER DATE IS NON-EXISTENT";
  69.             }
  70.             if (string.IsNullOrEmpty(contractNumber))
  71.             {
  72.                 return "NOK – CONTRACT NUMBER IS NON-EXISTENT";
  73.             }
  74.             if (string.IsNullOrEmpty(division))
  75.             {
  76.                 return "NOK – BUSINESS UNIT IS NON-EXISTENT";
  77.             }
  78.  
  79.  
  80.             // CREATION D'UNE ENTITE DEVIS
  81.             Quote devis = new Quote();
  82.  
  83.             string[] quoteFields = { "quoteid", "new_numerodevisifs", "new_numerodevisdefit", "statecode" };
  84.             Entity devisUp = GetInfo(_service, "quote", quoteFields, "name", (customerRefERP + " - " + contractNumber + " - " + revision));
  85.  
  86.             if (!string.IsNullOrEmpty(devisUp.LogicalName))
  87.             {
  88.                 devis = devisUp.ToEntity<Quote>();
  89.             }
  90.  
  91.             // OPPORTUNITE
  92.             string[] quoteFieldsOpportunity = { "opportunityid", "ownerid", "new_link_ifs" };
  93.             Entity opportunity = GetInfoOpp(_service, "opportunity", quoteFieldsOpportunity, "new_ifsid", contractNumber);
  94.  
  95.             if (string.IsNullOrEmpty(opportunity.LogicalName))
  96.             {
  97.                 return "NOK _ OPPORTUNITY WITH ID_IFS DON'T FIND";
  98.             }
  99.  
  100.             Opportunity opp = new Opportunity();
  101.             opp = opportunity.ToEntity<Opportunity>();
  102.  
  103.             if (!string.IsNullOrEmpty(opportunity.LogicalName))
  104.             {
  105.                 devis.OpportunityId = new EntityReference("opportunity", opportunity.Id);
  106.                 devis.OwnerId = new EntityReference("systemuser", opp.OwnerId.Id);
  107.             }
  108.  
  109.             // Name
  110.             devis.Name = customerRefERP + " - " + contractNumber + " - " + revision;
  111.             devis.new_numerodevisifs = customerRefERP + " - " + contractNumber + " - " + revision;
  112.             devis.new_numerodevisdefit = customerRefERP + " - " + contractNumber + " - " + revision;
  113.  
  114.  
  115.             //Numero Chrono , compteur de devis
  116.             string[] chronoFields = { "new_valeur" };
  117.  
  118.             Entity InfoChrono = GetInfo(_service, "new_compteur", chronoFields, "new_name", "ChronosDevisIFS");
  119.             new_compteur Chrono = InfoChrono.ToEntity<new_compteur>();
  120.  
  121.             System.DateTime moment = System.DateTime.Now;
  122.             String year = moment.Year.ToString();
  123.  
  124.             // PDF
  125.             if (!string.IsNullOrEmpty(pdf))
  126.             {
  127.                 byte[] decodePDF = Convert.FromBase64String(pdf);
  128.                 string filePath = @"\SiteIIS\data\" + devis.Name + ".pdf";
  129.  
  130.                 using (var pdfFile = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
  131.                 {
  132.                     pdfFile.Write(decodePDF, 0, decodePDF.Length);
  133.                     pdfFile.Flush();
  134.                 }
  135.             }
  136.  
  137.             // Description
  138.             if (!string.IsNullOrEmpty(description))
  139.             {
  140.                 devis.Description = description;
  141.             }
  142.             else
  143.             {
  144.                 devis.Description = "";
  145.             }
  146.  
  147.             // DATE
  148.             DateTime minDate = new DateTime();
  149.             minDate = DateTime.MinValue;
  150.  
  151.             // throw new Exception("DATE MINI : ______" + minDate.ToString());
  152.             int compareDateSoumission = DateTime.Compare(dateSoumission, minDate);
  153.             int compareDateCommande = DateTime.Compare(dateCommande, minDate);
  154.  
  155.             if (compareDateSoumission > 0)
  156.             {
  157.                 devis.new_Datedenvoidudevis = dateSoumission;
  158.             }
  159.             if (compareDateCommande > 0)
  160.             {
  161.                 devis.new_datedecommande = dateCommande;
  162.             }
  163.  
  164.             // Tarif 2019
  165.             if (!string.IsNullOrEmpty(nomTarif))
  166.             {
  167.                 string[] nomTarifField = { "pricelevelid" };
  168.                 Entity idTarif = GetInfo(_service, "pricelevel", nomTarifField, "name", nomTarif);
  169.                 devis.PriceLevelId = new EntityReference("pricelevel", idTarif.Id);
  170.             }
  171.  
  172.             // Price EURO
  173.             string[] euroFields = { "transactioncurrencyid" };
  174.  
  175.             Entity price = GetInfo(_service, "transactioncurrency", euroFields, "isocurrencycode", "EUR");
  176.             TransactionCurrency currencyId = price.ToEntity<TransactionCurrency>();
  177.  
  178.             devis.TransactionCurrencyId = new EntityReference("transactioncurrency", currencyId.Id);
  179.  
  180.             // Montant
  181.             devis.new_prixdevente = new Money((decimal)amount);
  182.  
  183.             // Initiateur
  184.             /*if (!string.IsNullOrEmpty(initiateur)) {
  185.                string[] ownerFields = { "systemuserid" };
  186.                 Entity initiateurEntity = GetInfo(_service, "systemuser", ownerFields, "fullname", initiateur);
  187.                 if (!string.IsNullOrEmpty(initiateurEntity.Id.ToString()))
  188.                 {
  189.                     devis.OwnerId = new EntityReference("systemuser", initiateurEntity.Id);
  190.                 }
  191.             }*/
  192.  
  193.             // check division
  194.             String[] RecupChamps = { "new_referenceerpcompteid", "new_compte", "owningbusinessunit", "new_name" };
  195.             String[] businessChamps = { "businessunitid" };
  196.             Entity divisionId = GetInfo(_service, "businessunit", businessChamps, "divisionname", division);
  197.             Guid businessId = new Guid();
  198.             businessId = divisionId.Id;
  199.  
  200.             EntityCollection compteId = GetInfoBis(_service, "new_referenceerpcompte", RecupChamps, "new_name", customerRefERP, "new_division", businessId.ToString());
  201.             int nbCompte = 0;
  202.  
  203.  
  204.             foreach (new_referenceerpcompte refCompteId in compteId.Entities)
  205.             {
  206.                 nbCompte++;
  207.             }
  208.  
  209.             if (nbCompte == 1)
  210.             {
  211.                 foreach (new_referenceerpcompte refCompteId in compteId.Entities)
  212.                 {
  213.                     // Account Name
  214.                     devis.CustomerId = new EntityReference("account", refCompteId.new_Compte.Id);
  215.                 }
  216.             }
  217.             else if (nbCompte < 1)
  218.             {
  219.                 return "NOK – THERE IS NO ACCOUNT FOR THIS CUSTOMER ERP REFERENCES";
  220.  
  221.             }
  222.             else
  223.             {
  224.                 return "NOK – THERE IS TWO ACCOUNT OR MORE WITH THE SAME CUSTOMER ERP REFERENCES";
  225.             }
  226.  
  227.             //LIGNE PROJET INSTALLATEUR
  228.             string[] quoteFieldsProjet = { "new_installateurdunprojetid" };
  229.             Entity ligneProjet = GetInfoTer(_service, "new_installateurdunprojet", quoteFieldsProjet, "new_opportunit", opportunity.Id.ToString(), "new_installateur", devis.CustomerId.Id.ToString());
  230.             if (!string.IsNullOrEmpty(ligneProjet.LogicalName))
  231.             {
  232.                 devis.new_projetinstallateur = new EntityReference("new_installateurdunprojet", ligneProjet.Id);
  233.             }
  234.  
  235.             //foreach (new_referenceerpcompte refCompteId in compteId.Entities)
  236.             //{
  237.             //  if (!string.IsNullOrEmpty(refCompteId.Id.ToString()))
  238.             // {
  239.             //   Entity divisionInfo = GetInfo(_service, "businessunit", "name", "businessunitid", refCompteId.new_division.Id.ToString());
  240.             //  BusinessUnit divisionName = divisionInfo.ToEntity<BusinessUnit>();
  241.  
  242.             // DIVISION : 13630d5a-755e-e811-8144-70106fa7c0e1 === "DEF FRANCE DIROP COMPTE SPÉCIFIQUES"
  243.             //if (refCompteId.new_division.Id.ToString() == "13630d5a-755e-e811-8144-70106fa7c0e1")
  244.             // if ( divisionName.Name.ToString().Contains(division))
  245.             //if (divisionName.Name.ToString().Contains("DEF FR"))
  246.             //{
  247.             //   devis.CustomerId = new EntityReference("account", refCompteId.new_Compte.Id);
  248.             //}
  249.             //}
  250.             //}
  251.  
  252.  
  253.             if (devis.Id == new Guid())
  254.             {
  255.  
  256.                 // Numéro du devis
  257.                 //devis.new_numerodevisdefit = "IFS-" + year.Substring(2, 2) + "-" + Chrono.new_valeur.ToString() + "-" + revision;
  258.                 //devis.new_numerodevisifs = (contractNumber + "-" + revision);
  259.  
  260.                 // Incrémentation du compteur de devis
  261.                 Chrono.new_valeur = (int.Parse(Chrono.new_valeur) + 1).ToString();
  262.                 _service.Update(Chrono);
  263.                 // Status
  264.                 devis.StateCode = QuoteState.Draft;
  265.                 devis.StatusCode = new OptionSetValue(1);
  266.  
  267.                 // ID DU DEVIS CREEE
  268.                 Guid idDevis = new Guid();
  269.                 idDevis = _service.Create(devis);
  270.  
  271.                 /*String[] newQuoteField = { "quoteid" };
  272.                 Entity RecupNewDevis = GetInfo(_service, "quote", newQuoteField, "quoteid", idDevis.ToString());
  273.                 Quote devisRecup = RecupNewDevis.ToEntity<Quote>();*/
  274.  
  275.                 devis.Id = idDevis;
  276.  
  277.                 //PIECE JOINT ADD BASE64 PDF BORDEREAU
  278.                 if (!string.IsNullOrEmpty(pdf))
  279.                 {
  280.                     Annotation note = new Annotation();
  281.                     // ASSOCIATION AU DEVIS
  282.                     note.ObjectTypeCode = "quote";
  283.                     note.ObjectId = new EntityReference("quote", devis.Id);
  284.  
  285.                     // ASSOCIATION DU PDF
  286.                     note.NoteText = "Fichier PDF";
  287.                     if (status == 1)
  288.                     {
  289.                         note.Subject = "Bordereau généré à la creation du devis";
  290.                     }
  291.                     else
  292.                     {
  293.                         note.Subject = "Bordereau généré à la cloture du devis";
  294.                     }
  295.                     note.FileName = devis.Name + ".pdf";
  296.                     note.MimeType = "application/pdf";
  297.                     note.DocumentBody = pdf;
  298.                     _service.Create(note);
  299.                 }
  300.  
  301.  
  302.                 // Passage du devis créé en Actif
  303.                 devis.StateCode = QuoteState.Active;
  304.                 devis.StatusCode = new OptionSetValue(3);
  305.                 _service.Update(devis);
  306.  
  307.                 if (status == 2)
  308.                 {
  309.  
  310.                     // Passage du devis créé en Conclue
  311.                     WinQuoteRequest winQuoteRequest = new WinQuoteRequest();
  312.                     Entity quoteClose = new Entity("quoteclose");
  313.                     quoteClose.Attributes["quoteid"] = new EntityReference("quote", devis.Id);
  314.                     quoteClose.Attributes["subject"] = "Quote Close" + DateTime.Now.ToString();
  315.                     winQuoteRequest.QuoteClose = quoteClose;
  316.                     winQuoteRequest.Status = new OptionSetValue(-1);
  317.                     _service.Execute(winQuoteRequest);
  318.  
  319.                     return "OK - QUOTE NAME WON AND CLOSED: " + devis.Name;
  320.                 }
  321.  
  322.                 return "OK _ QUOTE NAME : " + devis.Name;
  323.             }
  324.             else
  325.             {
  326.                 if ((status == 1) || (devis.StateCode.ToString() != "Active"))
  327.                 {
  328.                     return "NOK _ QUOTE ALREADY EXIST AND IT IS CLOSED, PLEASE CREATE NEW REVISION";
  329.                 }
  330.  
  331.  
  332.                 _service.Update(devis);
  333.                 // CHECK LES
  334.                 if (!string.IsNullOrEmpty(pdf))
  335.                 {
  336.                     Annotation note = new Annotation();
  337.                     // ASSOCIATION AU DEVIS
  338.                     note.ObjectTypeCode = "quote";
  339.                     note.ObjectId = new EntityReference("quote", devis.Id);
  340.  
  341.                     // ASSOCIATION DU PDF
  342.                     note.NoteText = "Fichier PDF";
  343.                     note.Subject = "Bordereau généré à la cloture du devis";
  344.                     note.FileName = devis.Name + ".pdf";
  345.                     note.MimeType = "application/pdf";
  346.                     note.DocumentBody = pdf;
  347.                     _service.Create(note);
  348.                 }
  349.  
  350.  
  351.                 // Passage du devis Actif en Conclus
  352.                 WinQuoteRequest winQuoteRequest = new WinQuoteRequest();
  353.                 Entity quoteClose = new Entity("quoteclose");
  354.                 quoteClose.Attributes["quoteid"] = new EntityReference("quote", devis.Id);
  355.                 quoteClose.Attributes["subject"] = "Quote Close" + DateTime.Now.ToString();
  356.                 winQuoteRequest.QuoteClose = quoteClose;
  357.                 winQuoteRequest.Status = new OptionSetValue(-1);
  358.                 _service.Execute(winQuoteRequest);
  359.  
  360.                 return "OK - QUOTE NAME WON AND CLOSED: " + devis.Name;
  361.             }
  362.  
  363.             // Creation Annotation avec pièce jointe
  364.             /*if (!string.IsNullOrEmpty(pdf) && (status == 1)) {
  365.                 Annotation note = new Annotation();
  366.  
  367.                 // ASSOCIATION AU DEVIS
  368.                 note.ObjectTypeCode = "quote";
  369.                 note.ObjectId = new EntityReference("quote", devis.Id);
  370.  
  371.                 // ASSOCIATION DU PDF
  372.                 note.NoteText = "Fichier pdf bis";
  373.                 note.Subject = "sujet de la pièce jointe";
  374.                 note.FileName = devis.Name + ".pdf";
  375.                 note.MimeType = "application/pdf";
  376.                 note.DocumentBody = pdf;
  377.  
  378.                 _service.Create(note);
  379.                 return "​​​​​​​OK - QUOTE NAME: " + devis.Name;
  380.             } */
  381.         }
  382.  
  383.         [WebMethod]
  384.         public String Compte(string nomCompte, string numeroCompte, int typeRelation, string remise, string initiateur, string adresse1, string adresse2, string ville, string codePostal, string region, string pays, string tel, string siteWeb, string fax, string email, string nomTarif, string paymentTermsCode, string shippingMethodCode, string referenceUnique, string division, bool statut)
  385.         {
  386.             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  387.             string connectionString = @"AuthType = Office365; Url=" + Settings.Default.url + "; Username=" + Settings.Default.utilisateur + "; Password=" + Settings.Default.mdp + "";
  388.             CrmServiceClient conn = new CrmServiceClient(connectionString);
  389.             IOrganizationService _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
  390.  
  391.             int condition = 0;
  392.  
  393.             // CONTROLE INPUT DATA
  394.             if (nomCompte.Length > 100)
  395.             {
  396.                 return "NOK _ Account name would be less than 100 char";
  397.             }
  398.             if (numeroCompte.Length > 25)
  399.             {
  400.                 return "NOK _ ACCOUNT NUMBER would be less than 25 char";
  401.             }
  402.             if ((typeRelation < 1) || (typeRelation > 6))
  403.             {
  404.                 return "NOK _ RELATION TYPE would be less than 6 char";
  405.             }
  406.             if (ville.Length < 1)
  407.             {
  408.                 return "NOK _ CITY cannot be null";
  409.             }
  410.             if (ville.Length > 110)
  411.             {
  412.                 return "NOK _ CITY would be less than 110 char";
  413.             }
  414.             if (pays.Length != 2)
  415.             {
  416.                 return "NOK _ COUNTRY FORMAT would be ISO format , only 2 char ";
  417.             }
  418.             if ((initiateur.Length < 5) || (initiateur.Length > 75))
  419.             {
  420.                 return "NOK _ OWNER would be less than 75 char";
  421.             }
  422.             if ((nomTarif.Length < 3) || (nomTarif.Length > 10))
  423.             {
  424.                 return "NOK _ WRONG TARIF (exemple : '2019')";
  425.             }
  426.  
  427.             // SIRET
  428.             //if (referenceUnique.Length != 14 )
  429.             //{
  430.             //    return " NOK - WRONG SIRET FORMAT (less than 14 char)";
  431.             //}
  432.  
  433.             // DIVISION
  434.             //
  435.  
  436.             Account compte = new Account();
  437.             Entity compteTempo = new Entity();
  438.             compteTempo = getReferenceCompteByCle(_service, referenceUnique);
  439.  
  440.             if (!string.IsNullOrEmpty(compteTempo.Id.ToString()))
  441.             {
  442.                 compte = compteTempo.ToEntity<Account>();
  443.             }
  444.  
  445.             BusinessUnit ReferenceActuelle = new BusinessUnit();
  446.             Entity referenceActuelleTempo = new Entity();
  447.             String[] businessUnitField = { "businessunitid" };
  448.             referenceActuelleTempo = GetInfo(_service, "businessunit", businessUnitField, "name", division);
  449.             if (!string.IsNullOrEmpty(referenceActuelleTempo.Id.ToString()))
  450.             {
  451.                 ReferenceActuelle = referenceActuelleTempo.ToEntity<BusinessUnit>();
  452.             }
  453.             else
  454.             {
  455.                 return "NOK _ DIVISION doesn't not exist";
  456.             }
  457.  
  458.  
  459.  
  460.             if (string.IsNullOrEmpty(compte.Name))
  461.             {
  462.                 // Creation
  463.                 condition = 1;
  464.                 compte.StateCode = AccountState.Active;
  465.                 compte.StatusCode = new OptionSetValue(1);
  466.             }
  467.             else
  468.             {
  469.  
  470.                 if (compte.CustomerTypeCode.Value == 3 || compte.CustomerTypeCode.Value == 4)
  471.                 {
  472.  
  473.                     if (!string.IsNullOrEmpty(compte.new_societereferente.ToString()) && compte.new_societereferente.Id == ReferenceActuelle.Id)
  474.                     {
  475.                         // update
  476.                         condition = 2;
  477.                     }
  478.                     else
  479.                     {
  480.                     // creation ref ERP
  481.                         condition = 3;
  482.                     }
  483.                 }
  484.                 else
  485.                 {
  486.                     // transformation client
  487.                     condition = 4;
  488.                 }
  489.             }
  490.  
  491.             /*********** PREPARATION CREATION / MISE A JOUR COMPTE ****************/
  492.  
  493.             if (!string.IsNullOrEmpty(referenceUnique))
  494.             {
  495.  
  496.                 //Nom du compte
  497.                 if (!string.IsNullOrEmpty(nomCompte))
  498.                 {
  499.                     compte.Name = nomCompte;
  500.                 }
  501.  
  502.                 //Remise
  503.                 if (!string.IsNullOrEmpty(remise))
  504.                 {
  505.                     String[] discountField = { "ac_remiseclientid" };
  506.                     Entity remiseEntity = GetInfo(_service, "ac_remiseclient", discountField, "ac_name", remise);
  507.                     if (!string.IsNullOrEmpty(remiseEntity.Id.ToString()))
  508.                     {
  509.                         compte.ac_RemiseId = new EntityReference("ac_remiseclient", remiseEntity.Id);
  510.                     }
  511.  
  512.                 }
  513.  
  514.                 //Initiateur
  515.                 if (!string.IsNullOrEmpty(initiateur))
  516.                 {
  517.                     String[] userIdField = { "systemuserid" };
  518.                     Entity initiateurEntity = GetInfo(_service, "systemuser", userIdField, "fullname", initiateur);
  519.                     //Entity initiateurEntity = GetInfo(_service, "team", "teamid", "name", initiateur);
  520.                     if (!string.IsNullOrEmpty(initiateurEntity.Id.ToString()))
  521.                     {
  522.                         //compte.OwnerId = new EntityReference("team", initiateurEntity.Id);
  523.                         compte.OwnerId = new EntityReference("systemuser", initiateurEntity.Id);
  524.                     }
  525.                 }
  526.  
  527.                 //Telephone
  528.                 compte.Telephone1 = tel;
  529.  
  530.                 //Site web
  531.                 compte.WebSiteURL = siteWeb;
  532.  
  533.                 //Fax
  534.                 compte.Fax = fax;
  535.  
  536.                 //Email
  537.                 compte.asd_eMailenvoiAR = email;
  538.  
  539.                 //Adresse1
  540.                 compte.Address1_Line1 = adresse1;
  541.  
  542.                 //Adresse 2
  543.                 compte.Address1_Line2 = adresse2;
  544.  
  545.  
  546.                 //Pays
  547.                 new_country_list Paystempo = new new_country_list();
  548.                 if (!string.IsNullOrEmpty(pays))
  549.                 {
  550.                     if (pays.Length == 2)
  551.                     {
  552.                         String[] countryField = { "new_country_listid", "new_typedecleunique" };
  553.                         Entity PaysEntity = GetInfo(_service, "new_country_list", countryField, "new_codepays", pays);
  554.  
  555.                         //Entity initiateurEntity = GetInfo(_service, "team", "teamid", "name", initiateur);
  556.                         if (!string.IsNullOrEmpty(PaysEntity.Id.ToString()))
  557.                         {
  558.                             Paystempo = PaysEntity.ToEntity<new_country_list>();
  559.                             compte.new_PaysIndicatif = new EntityReference("new_country_list", PaysEntity.Id);
  560.                             compte.Address1_Country = pays;
  561.                         }
  562.                         else
  563.                         {
  564.                             return "NOK – COUNTRY NOT FOUND";
  565.  
  566.                         }
  567.                     }
  568.                     else
  569.                     {
  570.                         return "NOK – COUNTRY FIELD IS NOT IN ISO FORMAT";
  571.                     }
  572.                 }
  573.  
  574.  
  575.                 //REFERENCE UNIQUE
  576.                 if (condition == 1)
  577.                 {
  578.                     if (Paystempo.Contains("new_typedecleunique"))
  579.                     {
  580.                         if (Paystempo.new_typedecleunique.Value == 100000001)
  581.                         {
  582.                             compte.asd_TVAintracom = referenceUnique;
  583.                             compte.new_cleunique = referenceUnique;
  584.                         }
  585.                         else if (Paystempo.new_typedecleunique.Value == 100000000)
  586.                         {
  587.                             compte.exp_siret = referenceUnique;
  588.                             compte.new_cleunique = referenceUnique;
  589.                         }
  590.                         else if (Paystempo.new_typedecleunique.Value == 100000002)
  591.                         {
  592.                             compte.new_taxnumber = referenceUnique;
  593.                             compte.new_cleunique = referenceUnique;
  594.                         }
  595.                         else if (Paystempo.new_typedecleunique.Value == 100000003)
  596.                         {
  597.                             compte.new_uniformsocialcreditcode = referenceUnique;
  598.                             compte.new_cleunique = referenceUnique;
  599.                         }
  600.                         else if (Paystempo.new_typedecleunique.Value == 100000004)
  601.                         {
  602.                             compte.new_rci = referenceUnique;
  603.                             compte.new_cleunique = referenceUnique;
  604.                         }
  605.                     }
  606.                     else
  607.                     {
  608.                         compte.new_cleuniquenoncatgoriser = referenceUnique;
  609.                     }
  610.                 }
  611.  
  612.                 if (condition == 1 || condition == 4)
  613.                 {
  614.                     compte.new_societereferente = new EntityReference("businessunit", ReferenceActuelle.Id);
  615.                 }
  616.  
  617.                 //Ville
  618.                 if (!string.IsNullOrEmpty(ville))
  619.                 {
  620.                     compte.Address1_City = ville;
  621.                 }
  622.  
  623.  
  624.  
  625.                 //Region
  626.                 compte.Address1_StateOrProvince = region;
  627.  
  628.                 //Code Postal
  629.                 compte.Address1_PostalCode = codePostal;
  630.  
  631.                 //Type de relation
  632.                 if (!string.IsNullOrEmpty(typeRelation.ToString()))
  633.                 {
  634.                     compte.CustomerTypeCode = new OptionSetValue(typeRelation);
  635.                 }
  636.  
  637.                 //Condition de paiement
  638.                 if (!string.IsNullOrEmpty(paymentTermsCode.ToString()))
  639.                 {
  640.                     compte.PaymentTermsCode = new OptionSetValue(Convert.ToInt32(paymentTermsCode));
  641.                 }
  642.  
  643.                 //mode de livraison
  644.                 if (!string.IsNullOrEmpty(shippingMethodCode.ToString()))
  645.                 {
  646.                     compte.Address1_FreightTermsCode = new OptionSetValue(Convert.ToInt32(shippingMethodCode));
  647.                 }
  648.  
  649.                 if (!string.IsNullOrEmpty(nomTarif))
  650.                 {
  651.                     //LIAISON TARIF
  652.                     String[] tarifField = { "pricelevelid" };
  653.                     Entity idTarif = GetInfo(_service, "pricelevel", tarifField, "name", nomTarif);
  654.  
  655.                     compte.DefaultPriceLevelId = new EntityReference("pricelevel", idTarif.Id);
  656.                 }
  657.                 else
  658.                 {
  659.                     compte.DefaultPriceLevelId = null;
  660.                 }
  661.  
  662.  
  663.                 /*********** FIN PREPARATION MISE A JOUR COMPTE ****************/
  664.  
  665.  
  666.                 if (condition == 1)
  667.                 {
  668.                     if (!statut)
  669.                     {
  670.                         // CREATION COMPTE
  671.                         Guid guidCompte = _service.Create(compte);
  672.  
  673.                         // CREATION REF ERP
  674.                         new_referenceerpcompte referenceERP = new new_referenceerpcompte();
  675.                         String[] userField = { "systemuserid" };
  676.                         Entity initiateurEntity2 = GetInfo(_service, "systemuser", userField, "fullname", initiateur);
  677.                         referenceERP.OwnerId = new EntityReference("systemuser", initiateurEntity2.Id);
  678.                         //Entity divisionEntity = GetInfo(_service, "businessunit", "businessunitid", "name", division);
  679.                         String[] businessField = { "businessunitid" };
  680.                         Entity divisionEntity = GetInfo(_service, "businessunit", businessField, "divisionname", division);
  681.                         referenceERP.new_division = new EntityReference("team", divisionEntity.Id);
  682.                         referenceERP.new_name = numeroCompte;
  683.                         referenceERP.new_Ref_defaut = true;
  684.                         referenceERP.new_Compte = new EntityReference("account", guidCompte);
  685.                         referenceERP.new_bloque = false;
  686.  
  687.                         _service.Create(referenceERP);
  688.                         return "OK - CREATION ACCOUNT";
  689.                     }
  690.                     else
  691.                     {
  692.                         return "OK – BLOQUED ACCOUNT, NOT CREATED";
  693.                     }
  694.  
  695.                 }
  696.                 else if (condition == 2)
  697.                 {
  698.                     if (statut)
  699.                     {
  700.                         EntityCollection tableauRefErpTampon = getAllRefErpByIdCompte(_service, compte.Id);
  701.                         int nb = 0;
  702.  
  703.                         foreach (new_referenceerpcompte referp in tableauRefErpTampon.Entities)
  704.                         {
  705.                             if (referp.new_division.Id == compte.new_societereferente.Id)
  706.                             {
  707.                                 referp.new_bloque = true;
  708.                                 _service.Update(referp);
  709.  
  710.                             }
  711.                             else if ((nb < 2) && (referp.new_division.Id != compte.new_societereferente.Id))
  712.                             {
  713.                                 // throw new Exception(compte.new_societereferente.Id.ToString() + " referp.newDiv" + referp.new_division.Id.ToString() );
  714.                                 compte.new_societereferente.Id = referp.new_division.Id;
  715.                             }
  716.                             nb++;
  717.                         } // Cannot insert duplicate key
  718.  
  719.  
  720.                         if (nb <= 1)
  721.                         {
  722.                             compte.CustomerTypeCode = new OptionSetValue(1);
  723.                             compte.StateCode = AccountState.Inactive;
  724.                             compte.StatusCode = new OptionSetValue(2);
  725.                             compte.new_societereferente = null;
  726.                         }
  727.                     }
  728.  
  729.                     // MODIF COMPTE
  730.                     _service.Update(compte);
  731.                     return "OK – MODIFICATION ACCOUNT";
  732.                 }
  733.                 else if (condition == 3)
  734.                 {
  735.                     //VERIF REF ERP
  736.                     new_referenceerpcompte referenceERP = new new_referenceerpcompte();
  737.                     referenceERP = getReferenceERPCompte(_service, compte.Id, ReferenceActuelle.Id).ToEntity<new_referenceerpcompte>();
  738.  
  739.                     // compte bloque ?
  740.                     // if (statut) { referenceERP.new_bloque = statut; }
  741.                     // else { referenceERP.new_bloque = false; }
  742.  
  743.                     if (string.IsNullOrEmpty(referenceERP.new_name))
  744.                     {
  745.                         if (!statut)
  746.                         {
  747.                             // CREATE REF ERP
  748.                             String[] refERPField = { "systemuserid" };
  749.                             Entity initiateurEntity2 = GetInfo(_service, "systemuser", refERPField, "fullname", initiateur);
  750.                             referenceERP.OwnerId = new EntityReference("systemuser", initiateurEntity2.Id);
  751.                             String[] businessIdPField = { "businessunitid" };
  752.                             Entity divisionEntity = GetInfo(_service, "businessunit", businessIdPField, "divisionname", division);
  753.                             referenceERP.new_division = new EntityReference("team", divisionEntity.Id);
  754.                             referenceERP.new_name = numeroCompte;
  755.                             referenceERP.new_Ref_defaut = false;
  756.                             referenceERP.new_Compte = new EntityReference("account", compte.Id);
  757.                             _service.Create(referenceERP);
  758.                             return "OK - CREATION REF ERP";
  759.                         }
  760.                         else
  761.                         {
  762.                             return "OK - BLOCKED ACCOUNT REF ERP, NOT CREATED";
  763.                         }
  764.                     }
  765.                     else if (referenceERP.new_bloque != statut)
  766.                     {
  767.  
  768.                         referenceERP.new_bloque = statut;
  769.                         _service.Update(referenceERP);
  770.                         return "OK - MODIF REF ERP";
  771.                     }
  772.                     else
  773.                     {
  774.                         return "OK - REF ERP UNCHANGED";
  775.                     }
  776.                 }
  777.                 else if (condition == 4)
  778.                 {
  779.                     if (!statut)
  780.                     {
  781.                         // UNLOCK CLIENT
  782.                         if (compte.StateCode.ToString() == "Inactive")
  783.                         {
  784.                             compte.StateCode = AccountState.Active;
  785.                             compte.StatusCode = new OptionSetValue(1);
  786.                         }
  787.  
  788.                         //TRANFORMATION EN CLIENT + MAJ
  789.                         _service.Update(compte);
  790.  
  791.                         //VERIF REF ERP
  792.                         new_referenceerpcompte referenceERP = new new_referenceerpcompte();
  793.                         referenceERP = getReferenceERPCompte(_service, compte.Id, ReferenceActuelle.Id).ToEntity<new_referenceerpcompte>();
  794.  
  795.                         if (string.IsNullOrEmpty(referenceERP.new_name))
  796.                         {
  797.                             // CREATE REF ERP
  798.                             String[] sysUserField = { "systemuserid" };
  799.                             Entity initiateurEntity2 = GetInfo(_service, "systemuser", sysUserField, "fullname", initiateur);
  800.                             referenceERP.OwnerId = new EntityReference("systemuser", initiateurEntity2.Id);
  801.                             String[] businessField = { "businessunitid" };
  802.                             Entity divisionEntity = GetInfo(_service, "businessunit", businessField, "divisionname", division);
  803.                             referenceERP.new_division = new EntityReference("team", divisionEntity.Id);
  804.                             referenceERP.new_name = numeroCompte;
  805.                             referenceERP.new_Ref_defaut = true;
  806.                             referenceERP.new_Compte = new EntityReference("account", compte.Id);
  807.  
  808.                             // COMPTE BLOQUE ?
  809.                             // if (statut) { referenceERP.new_bloque = statut; }
  810.                             // else { referenceERP.new_bloque = false; }
  811.  
  812.                             _service.Create(referenceERP);
  813.                         }
  814.                         // // COMPTE BLOQUE ?
  815.                         // if (statut) { referenceERP.new_bloque = statut; }
  816.                         // else { referenceERP.new_bloque = false; }
  817.  
  818.                         // _service.Update(referenceERP);
  819.  
  820.                         if (typeRelation == 1)
  821.                         {
  822.                             return " OK - PROSPECT UPDATED";
  823.                         }
  824.                         return "OK -  TRANFORMATION PROSPECT TO ACCOUNT";
  825.                     }
  826.                     else
  827.                     {
  828.                         return "OK -BLOQUED ACCOUNT, NO TRANFORMED TO ACCOUNT";
  829.                     }
  830.                 }
  831.                 else
  832.                 {
  833.                     return "NOK - MISSING MENDATORY FIELD";
  834.                 }
  835.             }
  836.             return "NOK - MISSING CLE UNIQUE (SIRET, TAX NUMBER, TVA INTRACOM, …)";
  837.         }
  838.  
  839.  
  840.         [WebMethod]
  841.         public string Produit(string nomProduit, string idProduit, string groupUnit, string defaultUnit, string defaultTarif, string division, decimal prixCession, int categorieMateriel, string classification, string itemCategoryCode, string productGroupCode, string descriptionLongue, string metier, int bloquer)
  842.         {
  843.  
  844.             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  845.  
  846.             string connectionString = @"AuthType = Office365; Url=" + Settings.Default.url + "; Username=" + Settings.Default.utilisateur + "; Password=" + Settings.Default.mdp + "";
  847.             CrmServiceClient conn = new CrmServiceClient(connectionString);
  848.             IOrganizationService _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
  849.  
  850.             Product produit = new Product();
  851.             produit = GetProduit(_service, idProduit).ToEntity<Product>();
  852.  
  853.             if (!string.IsNullOrEmpty(idProduit))
  854.             {
  855.                 //Nom du produit
  856.                 if (!string.IsNullOrEmpty(nomProduit))
  857.                 {
  858.                     produit.Name = nomProduit;
  859.                 }
  860.  
  861.                 // 1 => PRODUIT
  862.                 produit.ProductStructure = new OptionSetValue(1);
  863.  
  864.                 // Nombre de chiffre après la virgule => dans notre cas toujours 2
  865.                 produit.QuantityDecimal = 2;
  866.  
  867.                 //PRIX DE CESSION
  868.                 if (!string.IsNullOrEmpty(prixCession.ToString()))
  869.                 {
  870.                     produit.ac_Prixdecession = new Money(prixCession);
  871.                 }
  872.                 else
  873.                 {
  874.                     produit.ac_Prixdecession = new Money(0);
  875.                 }
  876.  
  877.                 //Groupe d'unité
  878.                 if (!string.IsNullOrEmpty(groupUnit))
  879.                 {
  880.                     String[] teamField = { "uomscheduleid" };
  881.                     Entity GroupeUnite = GetInfo(_service, "uomschedule", teamField, "name", groupUnit);
  882.                     if (!string.IsNullOrEmpty(GroupeUnite.Id.ToString()))
  883.                     {
  884.                         produit.DefaultUoMScheduleId = new EntityReference("uomschedule", GroupeUnite.Id);
  885.                     }
  886.                 }
  887.  
  888.                 //Unité par défaut
  889.                 if (!string.IsNullOrEmpty(defaultUnit))
  890.                 {
  891.                     String[] teamField = { "uomid" };
  892.                     Entity DefautUnit = GetInfo(_service, "uom", teamField, "name", defaultUnit);
  893.                     if (!string.IsNullOrEmpty(DefautUnit.Id.ToString()))
  894.                     {
  895.                         produit.DefaultUoMId = new EntityReference("uom", DefautUnit.Id);
  896.                     }
  897.                 }
  898.  
  899.                 //Division
  900.                 if (!string.IsNullOrEmpty(division))
  901.                 {
  902.                     String[] businessField = { "businessunitid" };
  903.                     Entity Division = GetInfo(_service, "businessunit", businessField, "name", division);
  904.                     if (!string.IsNullOrEmpty(Division.Id.ToString()))
  905.                     {
  906.                         produit.ac_Division = new EntityReference("businessunit", Division.Id);
  907.                     }
  908.                 }
  909.  
  910.                 //Catégorie
  911.                 if (!string.IsNullOrEmpty(categorieMateriel.ToString()))
  912.                 {
  913.                     produit.ac_Categorie = new OptionSetValue(categorieMateriel);
  914.                 }
  915.  
  916.                 //itemCategoryCode
  917.                 if (!string.IsNullOrEmpty(itemCategoryCode.ToString()))
  918.                 {
  919.                     produit.new_ItemCategoryCode = new OptionSetValue(Convert.ToInt32(itemCategoryCode));
  920.                 }
  921.                 //productGroupCode
  922.                 if (!string.IsNullOrEmpty(productGroupCode.ToString()))
  923.                 {
  924.                     produit.new_ProductGroupCode = new OptionSetValue(Convert.ToInt32(productGroupCode));
  925.                 }
  926.  
  927.                 //Classification
  928.                 if (!string.IsNullOrEmpty(classification.ToString()))
  929.                 {
  930.                     produit.new_classification = classification;
  931.                 }
  932.  
  933.                 //Description Longue
  934.                 if (!string.IsNullOrEmpty(descriptionLongue.ToString()))
  935.                 {
  936.                     produit.Description = descriptionLongue;
  937.                 }
  938.  
  939.                 //metier
  940.                 /*if (!string.IsNullOrEmpty(metier.ToString()))
  941.                 {
  942.                     produit.new_metier = new OptionSetValue(Convert.ToInt32(metier));
  943.                 }*/
  944.  
  945.                 //bloquer 0 = non et 1 = oui
  946.                 /*if (bloquer == 0)
  947.                 {
  948.                     produit.StateCode = ProductState.Active;
  949.                     produit.StatusCode = new OptionSetValue(1);
  950.                 }
  951.                 else
  952.                 {
  953.                     produit.StateCode = ProductState.Inactive;
  954.                     produit.StatusCode = new OptionSetValue(2);
  955.                 }*/
  956.                 //produit.new_blocked = Convert.ToBoolean(bloquer);
  957.  
  958.  
  959.                 //CREATION OU MODIFICATION
  960.                 if (string.IsNullOrEmpty(produit.ProductNumber))
  961.                 {
  962.                     produit.ProductNumber = idProduit;
  963.                     _service.Create(produit);
  964.  
  965.  
  966.                     Product produit2 = new Product();
  967.                     produit2 = GetProduit(_service, idProduit).ToEntity<Product>();
  968.  
  969.                     //Tarifs par défaut
  970.                     if (!string.IsNullOrEmpty(defaultTarif))
  971.                     {
  972.                         String[] priceField = { "pricelevelid" };
  973.                         Entity DefautTarif = GetInfo(_service, "pricelevel", priceField, "name", defaultTarif);
  974.                         if (!string.IsNullOrEmpty(DefautTarif.Id.ToString()))
  975.                         {
  976.                             produit2.PriceLevelId = new EntityReference("pricelevel", DefautTarif.Id);
  977.                         }
  978.                     }
  979.  
  980.                     //produit2.StateCode = ProductState.Active;
  981.                     _service.Update(produit2);
  982.  
  983.                     /*if (!string.IsNullOrEmpty(defaultTarif))
  984.                     {
  985.                         Tarif(defaultTarif, idProduit, "unité principale", 1, 1, 0 );
  986.                         Tarif("2017", idProduit, "unité principale", 1, 1, 0);
  987.                         return "CREATION - OK + LIGNE TARIFAIRE 0 - OK";
  988.                     }*/
  989.  
  990.                     return "CREATION - OK";
  991.                 }
  992.                 else
  993.                 {
  994.  
  995.  
  996.  
  997.                     _service.Update(produit);
  998.  
  999.                     //throw new Exception("CRM TARIF === "+ CRMtarif.Id.ToString());
  1000.  
  1001.                     /* if (!string.IsNullOrEmpty(defaultTarif))
  1002.                      {
  1003.  
  1004.                          //LIAISON TARIF
  1005.                          Entity idTarif = GetInfo(_service, "pricelevel", "pricelevelid", "name", defaultTarif);
  1006.                          Entity LigneTarifaire = GetElementTarifaire(_service, produit.Id, idTarif.Id);
  1007.                          if (!string.IsNullOrEmpty(LigneTarifaire.Id.ToString()))
  1008.                          {
  1009.                              Tarif(defaultTarif, idProduit, "unité principale", 1, 1, 0);
  1010.  
  1011.                          }
  1012.  
  1013.  
  1014.                          //LIAISON TARIF
  1015.                          Entity idTarif3 = GetInfo(_service, "pricelevel", "pricelevelid", "name", "2017");
  1016.                          Entity LigneTarifaire3 = GetElementTarifaire(_service, produit.Id, idTarif3.Id);
  1017.                          if (!string.IsNullOrEmpty(LigneTarifaire3.Id.ToString()))
  1018.                          {
  1019.                              Tarif("2017", idProduit, "unité principale", 1, 1, 0);
  1020.  
  1021.                          }
  1022.  
  1023.                          return "MODIFICATION - OK + LIGNE TARIFAIRE 0 - OK";
  1024.                      }*/
  1025.  
  1026.                     return "MODIFICATION - OK";
  1027.                 }
  1028.  
  1029.             }
  1030.             else
  1031.             {
  1032.                 return "Numéro de produit - NOK";
  1033.             }
  1034.  
  1035.         }
  1036.  
  1037.         [WebMethod]
  1038.         public string Tarif(string nomTarif, string numeroProduit, string unite, int optionQuantiteVente, int modeTarification, decimal montant)
  1039.         {
  1040.  
  1041.             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  1042.  
  1043.             string connectionString = @"AuthType = Office365; Url=" + Settings.Default.url + "; Username=" + Settings.Default.utilisateur + "; Password=" + Settings.Default.mdp + "";
  1044.             CrmServiceClient conn = new CrmServiceClient(connectionString);
  1045.             IOrganizationService _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
  1046.  
  1047.             //LIAISON TARIF
  1048.             String[] tarifField = { "pricelevelid" };
  1049.             Entity idTarif = GetInfo(_service, "pricelevel", tarifField, "name", nomTarif);
  1050.  
  1051.             //LIAISON PRODUIT
  1052.             String[] productField = { "productid" };
  1053.             Entity idProduit = GetInfo(_service, "product", productField, "productnumber", numeroProduit);
  1054.  
  1055.             ProductPriceLevel tarifProduit = new ProductPriceLevel();
  1056.             Entity tarifProduitTemp = new Entity();
  1057.             tarifProduitTemp = GetElementTarifaire(_service, idProduit.Id, idTarif.Id);
  1058.             if (tarifProduitTemp is ProductPriceLevel)
  1059.             {
  1060.                 tarifProduit = tarifProduitTemp.ToEntity<ProductPriceLevel>();
  1061.             }
  1062.  
  1063.             if (!string.IsNullOrEmpty(nomTarif) && !string.IsNullOrEmpty(numeroProduit) && !string.IsNullOrEmpty(montant.ToString()))
  1064.             {
  1065.  
  1066.                 //LIAISON UNITE
  1067.                 String[] unitField = { "uomid" };
  1068.                 Entity idUnite = GetInfo(_service, "uom", unitField, "name", unite);
  1069.                 tarifProduit.UoMId = new EntityReference("uom", idUnite.Id);
  1070.  
  1071.                 //Option de quantité de vente
  1072.                 if (!string.IsNullOrEmpty(optionQuantiteVente.ToString()))
  1073.                 {
  1074.                     tarifProduit.QuantitySellingCode = new OptionSetValue(optionQuantiteVente);
  1075.                 }
  1076.  
  1077.                 //Mode de tarification
  1078.                 if (!string.IsNullOrEmpty(modeTarification.ToString()))
  1079.                 {
  1080.                     tarifProduit.PricingMethodCode = new OptionSetValue(modeTarification);
  1081.                 }
  1082.  
  1083.                 //Montant
  1084.                 tarifProduit.Amount = new Money(montant);
  1085.  
  1086.                 //CREATION OU MODIFICATION
  1087.                 if (tarifProduit.PriceLevelId is EntityReference && tarifProduit.ProductId is EntityReference)
  1088.                 {
  1089.                     _service.Update(tarifProduit);
  1090.                     return "MODIFICATION - OK";
  1091.                 }
  1092.                 else
  1093.                 {
  1094.                     tarifProduit.PriceLevelId = new EntityReference("pricelevel", idTarif.Id);
  1095.                     tarifProduit.ProductId = new EntityReference("product", idProduit.Id);
  1096.                     _service.Create(tarifProduit);
  1097.                     return "CREATION - OK";
  1098.                 }
  1099.             }
  1100.             else
  1101.             {
  1102.                 return "Numéro de produit - NOK";
  1103.             }
  1104.         }
  1105.  
  1106.         /*[WebMethod]
  1107.         public String Batiment(string nomBatiment, string numeroBatiment, string initiateur, string adresse1, string adresse2, string ville, string codePostal, string region, string pays, string numeroCompte, string division)
  1108.         {
  1109.  
  1110.             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  1111.  
  1112.             string connectionString = @"AuthType = Office365; Url=" + Settings.Default.url + "; Username=" + Settings.Default.utilisateur + "; Password=" + Settings.Default.mdp + "";
  1113.             CrmServiceClient conn = new CrmServiceClient(connectionString);
  1114.             IOrganizationService _service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
  1115.  
  1116.             int condition = 0;
  1117.  
  1118.             new_referenceerpbatiment referenceERP = new new_referenceerpbatiment();
  1119.             referenceERP = GetReferenceERPBatiment(_service, numeroBatiment).ToEntity<new_referenceerpbatiment>();
  1120.  
  1121.             //throw new Exception(referenceERP.Id.ToString());
  1122.  
  1123.             new_site batiment = new new_site();
  1124.             //if (!string.IsNullOrEmpty(referenceERP.new_Compte.Id.ToString()))
  1125.             if (referenceERP.GetAttributeValue<EntityReference>("new_batiment") != null)
  1126.             {
  1127.                 ColumnSet columns = new ColumnSet(new String[] { "new_name", "new_siteid" });
  1128.                 batiment = (new_site)_service.Retrieve("new_site", referenceERP.new_Batiment.Id, columns);
  1129.             }
  1130.  
  1131.  
  1132.             if (!string.IsNullOrEmpty(batiment.new_name))
  1133.             {
  1134.                 condition = 3;
  1135.             }
  1136.             else
  1137.             {
  1138.                 batiment = GetBatiment(_service, nomBatiment).ToEntity<new_site>();
  1139.                 if (!string.IsNullOrEmpty(batiment.new_name))
  1140.                 {
  1141.                     condition = 2;
  1142.                 }
  1143.                 else
  1144.                 {
  1145.                     condition = 1;
  1146.                 }
  1147.             }
  1148.  
  1149.             if (!string.IsNullOrEmpty(numeroBatiment))
  1150.             {
  1151.                 ///////////////// PREPARATION CREATION / MISE A JOUR BATIMENT //////////////////
  1152.  
  1153.                 //Nom du compte
  1154.                 if (!string.IsNullOrEmpty(nomBatiment))
  1155.                 {
  1156.                     batiment.new_name = nomBatiment;
  1157.                 }
  1158.  
  1159.  
  1160.                 //Initiateur
  1161.                 if (!string.IsNullOrEmpty(initiateur))
  1162.                 {
  1163.                     //Entity initiateurEntity = GetInfo(_service, "team", "teamid", "name", initiateur);
  1164.                     Entity initiateurEntity = GetInfo(_service, "systemuser", "systemuserid", "fullname", initiateur);
  1165.                     if (!string.IsNullOrEmpty(initiateurEntity.Id.ToString()))
  1166.                     {
  1167.                         //batiment.OwnerId = new EntityReference("team", initiateurEntity.Id);
  1168.                         batiment.OwnerId = new EntityReference("systemuser", initiateurEntity.Id);
  1169.                     }
  1170.                 }
  1171.  
  1172.  
  1173.                 //Adresse1
  1174.                 batiment.new_AdresseRue1 = adresse1;
  1175.  
  1176.                 //Adresse 2
  1177.                 batiment.new_AdresseRue2 = adresse2;
  1178.  
  1179.                 //Ville
  1180.                 batiment.new_AdresseVille = ville;
  1181.  
  1182.                 //Pays
  1183.                 batiment.new_AdressePays = pays;
  1184.  
  1185.                 //Code Postal
  1186.                 batiment.new_AdresseCodepostal = codePostal;
  1187.  
  1188.                 //Compte
  1189.                 new_referenceerpcompte referenceERPCompte = new new_referenceerpcompte();
  1190.                 referenceERPCompte = GetReferenceERPCompte(_service, numeroCompte).ToEntity<new_referenceerpcompte>();
  1191.                 batiment.new_Compteexploitant = referenceERPCompte.new_Compte;
  1192.  
  1193.                 ///////////////// FIN PREPARATION MISE A JOUR BATIMENT /////////////////
  1194.  
  1195.                 // 3 condition :
  1196.                 //Entity initiateurEntity2 = GetInfo(_service, "team", "teamid", "name", initiateur);
  1197.                 Entity initiateurEntity2 = GetInfo(_service, "systemuser", "systemuserid", "fullname", initiateur);
  1198.                 //referenceERP.OwnerId = new EntityReference("team", initiateurEntity2.Id);
  1199.                 referenceERP.OwnerId = new EntityReference("systemuser", initiateurEntity2.Id);
  1200.  
  1201.                 Entity divisionEntity = GetInfo(_service, "businessunit", "businessunitid", "divisionname", division);
  1202.                 referenceERP.new_Division = new EntityReference("team", divisionEntity.Id);
  1203.  
  1204.                 referenceERP.new_Ref_defaut = true;
  1205.  
  1206.                 // 1. Compte existe et ligne ref erp aussi
  1207.                 if (condition == 3)
  1208.                 {
  1209.                     _service.Update(batiment);
  1210.                     return "MODIFICATION BATIMENT - OK";
  1211.  
  1212.                 }
  1213.                 // 2. Compte existe mais pas la ligne REF ERP
  1214.                 else if (condition == 2)
  1215.                 {
  1216.                     referenceERP.new_Batiment = batiment.ToEntityReference();
  1217.                     referenceERP.new_name = numeroBatiment;
  1218.                     _service.Create(referenceERP);
  1219.                     _service.Update(batiment);
  1220.                     return "MODIFICATION BATIMENT - OK + CREATION REF ERP - OK";
  1221.  
  1222.                     //3. Le compte n'existe pas et donc la ligne REF ERP non plus !
  1223.                 }
  1224.                 else if (condition == 1)
  1225.                 {
  1226.                     Guid guidCompte = _service.Create(batiment);
  1227.                     ColumnSet cols = new ColumnSet(new String[] { "new_name", "new_siteid" });
  1228.                     new_site RetrievedBatiment = (new_site)_service.Retrieve("new_site", guidCompte, cols);
  1229.                     referenceERP.new_Batiment = new EntityReference("new_site", RetrievedBatiment.Id);
  1230.                     referenceERP.new_name = numeroBatiment;
  1231.                     _service.Create(referenceERP);
  1232.                     return "CREATION - OK";
  1233.                 }
  1234.                 else
  1235.                 {
  1236.                     return "ERROR";
  1237.                 }
  1238.  
  1239.             }
  1240.             else
  1241.             {
  1242.                 return "Numéro de batiment vide";
  1243.             }
  1244.  
  1245.         }*/
  1246.  
  1247.  
  1248.         static Entity GetInfo(IOrganizationService Service, string nomTable, string[] ChampRecup, string champCondition, string variableCondition)
  1249.         {
  1250.             QueryExpression query = new QueryExpression();
  1251.  
  1252.             query.ColumnSet = new ColumnSet(ChampRecup);
  1253.             query.EntityName = nomTable;
  1254.  
  1255.             FilterExpression Fe = new FilterExpression();
  1256.  
  1257.             ConditionExpression Ce = new ConditionExpression();
  1258.             Ce.AttributeName = champCondition;
  1259.             Ce.Operator = ConditionOperator.Equal;
  1260.             Ce.Values.Add(variableCondition);
  1261.             Fe.AddCondition(Ce);
  1262.  
  1263.             query.Criteria.AddFilter(Fe);
  1264.  
  1265.             try
  1266.             {
  1267.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1268.  
  1269.                 return retrieve[0];
  1270.             }
  1271.             catch (Exception ex)
  1272.             {
  1273.                 //throw new Exception(ex.Message);
  1274.                 Entity entity = new Entity();
  1275.                 return entity;
  1276.             }
  1277.         }
  1278.  
  1279.         static Entity GetInfoOpp(IOrganizationService Service, string nomTable, string[] ChampRecup, string champCondition, string variableCondition)
  1280.         {
  1281.             QueryExpression query = new QueryExpression();
  1282.  
  1283.             query.ColumnSet = new ColumnSet(ChampRecup);
  1284.             query.EntityName = nomTable;
  1285.  
  1286.             FilterExpression Fe = new FilterExpression();
  1287.  
  1288.             ConditionExpression Ce = new ConditionExpression();
  1289.             Ce.AttributeName = champCondition;
  1290.             Ce.Operator = ConditionOperator.Equal;
  1291.             Ce.Values.Add(variableCondition);
  1292.             Fe.AddCondition(Ce);
  1293.  
  1294.             query.Criteria.AddFilter(Fe);
  1295.  
  1296.             try
  1297.             {
  1298.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1299.  
  1300.                 return retrieve[0];
  1301.             }
  1302.             catch (Exception)
  1303.             {
  1304.                 // throw new Exception("NOK _ OPPORTUNITY WITH ID_IFS DON'T FIND");
  1305.                 Entity entity = new Entity();
  1306.                 return entity;
  1307.             }
  1308.         }
  1309.  
  1310.         static EntityCollection GetInfoBis(IOrganizationService Service, string nomTable, string[] ChampRecup, string champCondition1, string variableCondition1, string champCondition2, string variableCondition2)
  1311.         {
  1312.             QueryExpression query = new QueryExpression();
  1313.  
  1314.             query.ColumnSet = new ColumnSet(ChampRecup);
  1315.             query.EntityName = nomTable;
  1316.  
  1317.             FilterExpression Fe = new FilterExpression(LogicalOperator.And);
  1318.  
  1319.             ConditionExpression Ce = new ConditionExpression();
  1320.             Ce.AttributeName = champCondition1;
  1321.             Ce.Operator = ConditionOperator.Equal;
  1322.             Ce.Values.Add(variableCondition1);
  1323.             Fe.AddCondition(Ce);
  1324.  
  1325.             ConditionExpression Ce2 = new ConditionExpression();
  1326.             Ce2.AttributeName = champCondition2;
  1327.             Ce2.Operator = ConditionOperator.Equal;
  1328.             Ce2.Values.Add(variableCondition2);
  1329.             Fe.AddCondition(Ce2);
  1330.  
  1331.  
  1332.             query.Criteria.AddFilter(Fe);
  1333.  
  1334.             try
  1335.             {
  1336.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1337.  
  1338.                 return retrieve;
  1339.             }
  1340.             catch (Exception ex)
  1341.             {
  1342.                 ///throw new Exception(ex.Message);
  1343.                 EntityCollection entity = new EntityCollection();
  1344.                 return entity;
  1345.             }
  1346.         }
  1347.  
  1348.         static Entity GetInfoTer(IOrganizationService Service, string nomTable, string[] ChampRecup, string champCondition1, string variableCondition1, string champCondition2, string variableCondition2)
  1349.         {
  1350.             QueryExpression query = new QueryExpression();
  1351.  
  1352.             query.ColumnSet = new ColumnSet(ChampRecup);
  1353.             query.EntityName = nomTable;
  1354.  
  1355.             FilterExpression Fe = new FilterExpression(LogicalOperator.And);
  1356.  
  1357.             ConditionExpression Ce = new ConditionExpression();
  1358.             Ce.AttributeName = champCondition1;
  1359.             Ce.Operator = ConditionOperator.Equal;
  1360.             Ce.Values.Add(variableCondition1);
  1361.             Fe.AddCondition(Ce);
  1362.  
  1363.             ConditionExpression Ce2 = new ConditionExpression();
  1364.             Ce2.AttributeName = champCondition2;
  1365.             Ce2.Operator = ConditionOperator.Equal;
  1366.             Ce2.Values.Add(variableCondition2);
  1367.             Fe.AddCondition(Ce2);
  1368.  
  1369.  
  1370.             query.Criteria.AddFilter(Fe);
  1371.  
  1372.             try
  1373.             {
  1374.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1375.  
  1376.                 return retrieve[0];
  1377.             }
  1378.             catch (Exception ex)
  1379.             {
  1380.                 //throw new Exception(ex.Message);
  1381.                 Entity entity = new Entity();
  1382.                 return entity;
  1383.             }
  1384.         }
  1385.  
  1386.         static EntityCollection GetDivisions(IOrganizationService Service, string nomTable, string ChampRecup, string champCondition, string variableCondition)
  1387.         {
  1388.             QueryExpression query = new QueryExpression();
  1389.  
  1390.             query.ColumnSet = new ColumnSet(ChampRecup);
  1391.             query.EntityName = nomTable;
  1392.  
  1393.             FilterExpression Fe = new FilterExpression();
  1394.  
  1395.             ConditionExpression Ce = new ConditionExpression();
  1396.             Ce.AttributeName = champCondition;
  1397.             Ce.Operator = ConditionOperator.Equal;
  1398.             Ce.Values.Add(variableCondition);
  1399.             Fe.AddCondition(Ce);
  1400.  
  1401.             query.Criteria.AddFilter(Fe);
  1402.  
  1403.             try
  1404.             {
  1405.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1406.  
  1407.                 return retrieve;
  1408.             }
  1409.             catch (Exception)
  1410.             {
  1411.                 //throw new Exception(ex.Message);
  1412.                 EntityCollection entity = new EntityCollection();
  1413.                 return entity;
  1414.             }
  1415.         }
  1416.  
  1417.         static Entity GetElementTarifaire(IOrganizationService Service, Guid idProduit, Guid idTarif)
  1418.         {
  1419.             QueryExpression query = new QueryExpression();
  1420.  
  1421.             query.ColumnSet = new ColumnSet("productpricelevelid", "productid", "pricelevelid");
  1422.             query.EntityName = "productpricelevel";
  1423.  
  1424.             FilterExpression Fe = new FilterExpression(LogicalOperator.And);
  1425.  
  1426.             ConditionExpression Ce = new ConditionExpression();
  1427.             Ce.AttributeName = "productid";
  1428.             Ce.Operator = ConditionOperator.Equal;
  1429.             Ce.Values.Add(idProduit);
  1430.             Fe.AddCondition(Ce);
  1431.  
  1432.             ConditionExpression Ce2 = new ConditionExpression();
  1433.             Ce2.AttributeName = "pricelevelid";
  1434.             Ce2.Operator = ConditionOperator.Equal;
  1435.             Ce2.Values.Add(idTarif);
  1436.             Fe.AddCondition(Ce2);
  1437.  
  1438.             query.Criteria.AddFilter(Fe);
  1439.  
  1440.             try
  1441.             {
  1442.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1443.                 return retrieve[0];
  1444.             }
  1445.             catch (Exception)
  1446.             {
  1447.                 //throw new Exception(ex.Message);
  1448.                 Entity entity = new Entity();
  1449.                 return entity;
  1450.             }
  1451.         }
  1452.  
  1453.         static Entity GetCompte(IOrganizationService Service, string nomCompte)
  1454.         {
  1455.             QueryExpression query = new QueryExpression();
  1456.  
  1457.             query.ColumnSet = new ColumnSet("name");
  1458.             query.EntityName = "account";
  1459.             FilterExpression Fe = new FilterExpression();
  1460.  
  1461.             ConditionExpression Ce = new ConditionExpression();
  1462.             Ce.AttributeName = "name";
  1463.             Ce.Operator = ConditionOperator.Equal;
  1464.             Ce.Values.Add(nomCompte);
  1465.             Fe.AddCondition(Ce);
  1466.  
  1467.             query.Criteria.AddFilter(Fe);
  1468.  
  1469.             try
  1470.             {
  1471.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1472.                 return retrieve[0];
  1473.             }
  1474.             catch (Exception)
  1475.             {
  1476.                 //throw new Exception(ex.Message);
  1477.                 Account entity = new Account();
  1478.                 entity.asd_Ncompte = "";
  1479.                 throw new Exception("retour catch");
  1480.                 //return entity;
  1481.             }
  1482.         }
  1483.  
  1484.         static Entity GetBatiment(IOrganizationService Service, string nomBatiment)
  1485.         {
  1486.             QueryExpression query = new QueryExpression();
  1487.  
  1488.             query.ColumnSet = new ColumnSet("new_name");
  1489.             query.EntityName = "new_site";
  1490.  
  1491.             FilterExpression Fe = new FilterExpression();
  1492.  
  1493.             ConditionExpression Ce = new ConditionExpression();
  1494.             Ce.AttributeName = "new_name";
  1495.             Ce.Operator = ConditionOperator.Equal;
  1496.             Ce.Values.Add(nomBatiment);
  1497.             Fe.AddCondition(Ce);
  1498.  
  1499.             query.Criteria.AddFilter(Fe);
  1500.  
  1501.             try
  1502.             {
  1503.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1504.                 return retrieve[0];
  1505.             }
  1506.             catch (Exception)
  1507.             {
  1508.                 //throw new Exception(ex.Message);
  1509.                 new_site entity = new new_site();
  1510.                 //entity.asd_Ncompte = "";
  1511.                 return entity;
  1512.             }
  1513.         }
  1514.  
  1515.         static Entity GetProduit(IOrganizationService Service, string idProduit)
  1516.         {
  1517.             QueryExpression query = new QueryExpression();
  1518.  
  1519.             query.ColumnSet = new ColumnSet("productnumber");
  1520.             query.EntityName = "product";
  1521.  
  1522.             FilterExpression Fe = new FilterExpression();
  1523.  
  1524.             ConditionExpression Ce = new ConditionExpression();
  1525.             Ce.AttributeName = "productnumber";
  1526.             Ce.Operator = ConditionOperator.Equal;
  1527.             Ce.Values.Add(idProduit);
  1528.             Fe.AddCondition(Ce);
  1529.  
  1530.             query.Criteria.AddFilter(Fe);
  1531.  
  1532.             try
  1533.             {
  1534.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1535.                 return retrieve[0];
  1536.             }
  1537.             catch (Exception)
  1538.             {
  1539.                 //throw new Exception(ex.Message);
  1540.                 Product entity = new Product();
  1541.                 entity.ProductNumber = "";
  1542.                 return entity;
  1543.             }
  1544.         }
  1545.  
  1546.         /////////////////////////////////////////////////////////////////////////////////////////////////////
  1547.         private static EntityCollection getAllRefErpByIdCompte(IOrganizationService _service, Guid compteId)
  1548.         {
  1549.             // throw new Exception(compteId.ToString());
  1550.             QueryExpression query = new QueryExpression();
  1551.  
  1552.             query.ColumnSet = new ColumnSet("new_referenceerpcompteid", "new_compte", "createdon", "new_bloque", "new_division");
  1553.             query.EntityName = "new_referenceerpcompte";
  1554.             FilterExpression Fe = new FilterExpression();
  1555.  
  1556.             ConditionExpression Ce = new ConditionExpression();
  1557.             Ce.AttributeName = "new_compte";
  1558.             Ce.Operator = ConditionOperator.Equal;
  1559.             Ce.Values.Add(compteId);
  1560.             Fe.AddCondition(Ce);
  1561.  
  1562.             ConditionExpression CeBis = new ConditionExpression();
  1563.             CeBis.AttributeName = "new_bloque";
  1564.             CeBis.Operator = ConditionOperator.Equal;
  1565.             CeBis.Values.Add(false);
  1566.             Fe.AddCondition(CeBis);
  1567.  
  1568.             query.Orders.Add(new OrderExpression("createdon", OrderType.Ascending));
  1569.             query.Criteria.AddFilter(Fe);
  1570.  
  1571.             try
  1572.             {
  1573.                 EntityCollection retrieve = _service.RetrieveMultiple(query);
  1574.                 return retrieve;
  1575.             }
  1576.             catch
  1577.             {
  1578.                 EntityCollection entity = new EntityCollection();
  1579.                 return entity;
  1580.             }
  1581.  
  1582.             // retrieve = retrieve.OrderBy(x => x.CreatedOn).ToList();
  1583.             //
  1584.             // throw new Exception(retrieve[0]);
  1585.             // Last one ???
  1586.  
  1587.  
  1588.  
  1589.             // if (retrieve.GetLength() > 1 && statut)
  1590.             // {
  1591.             //     return GetTest(Service, retrieve)
  1592.             // } else
  1593.             // {
  1594.             //     return retrieve[0];
  1595.             // }
  1596.  
  1597.             // DOC MICROSOFT
  1598.             //
  1599.             // using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
  1600.             // {
  1601.             //   var query_orderbylookup = from a in svcContext.AccountSet
  1602.             //                         where a.Address1_Name == "Contoso Pharmaceuticals"
  1603.             //                         orderby a.PrimaryContactId
  1604.             //                         select new
  1605.             //                         {
  1606.             //                             a.Name,
  1607.             //                             a.Address1_City
  1608.             //                         };
  1609.             //   foreach (var a in query_orderbylookup)
  1610.             //    {
  1611.             //    System.Console.WriteLine(a.Name + " " + a.Address1_City);
  1612.             //    }
  1613.             // }
  1614.  
  1615.         }
  1616.         /////////////////////////////////////////////////////////////////////////////////////////////////////
  1617.  
  1618.         static Entity getReferenceERPCompte(IOrganizationService Service, Guid compteId, Guid divisionId)
  1619.         {
  1620.             QueryExpression query = new QueryExpression();
  1621.  
  1622.             query.ColumnSet = new ColumnSet("new_compte", "new_referenceerpcompteid", "new_name", "new_bloque");
  1623.             query.EntityName = "new_referenceerpcompte";
  1624.  
  1625.             FilterExpression Fe = new FilterExpression(LogicalOperator.And);
  1626.  
  1627.             ConditionExpression Ce1 = new ConditionExpression();
  1628.             Ce1.AttributeName = "new_compte";
  1629.             Ce1.Operator = ConditionOperator.Equal;
  1630.             Ce1.Values.Add(compteId);
  1631.             Fe.AddCondition(Ce1);
  1632.  
  1633.             ConditionExpression Ce2 = new ConditionExpression();
  1634.             Ce2.AttributeName = "new_division";
  1635.             Ce2.Operator = ConditionOperator.Equal;
  1636.             Ce2.Values.Add(divisionId);
  1637.             Fe.AddCondition(Ce2);
  1638.             query.Criteria.AddFilter(Fe);
  1639.  
  1640.             try
  1641.             {
  1642.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1643.                 return retrieve[0];
  1644.  
  1645.             }
  1646.             catch (Exception)
  1647.             {
  1648.                 //throw new Exception(ex.Message);
  1649.                 new_referenceerpcompte entity = new new_referenceerpcompte();
  1650.                 //entity.new_Compte = new EntityReference("Account", null); ;
  1651.                 return entity;
  1652.             }
  1653.         }
  1654.  
  1655.  
  1656.         static Entity getReferenceCompteByCle(IOrganizationService _service, string cleUnique)
  1657.         {
  1658.             QueryExpression query = new QueryExpression();
  1659.  
  1660.             query.ColumnSet = new ColumnSet("accountid", "name", "new_societereferente", "customertypecode", "statuscode", "statecode");
  1661.             query.EntityName = "account";
  1662.  
  1663.             FilterExpression Fe = new FilterExpression();
  1664.  
  1665.             ConditionExpression Ce = new ConditionExpression();
  1666.             Ce.AttributeName = "new_cleunique";
  1667.             Ce.Operator = ConditionOperator.Equal;
  1668.             Ce.Values.Add(cleUnique);
  1669.             Fe.AddCondition(Ce);
  1670.  
  1671.             query.Criteria.AddFilter(Fe);
  1672.             try
  1673.             {
  1674.                 EntityCollection retrieve = _service.RetrieveMultiple(query);
  1675.                 return retrieve[0];
  1676.             }
  1677.             catch (Exception)
  1678.             {
  1679.                 //throw new Exception(ex.Message);
  1680.                 Account entity = new Account();
  1681.                 //entity.new_Compte = new EntityReference("Account", null); ;
  1682.                 return entity;
  1683.             }
  1684.         }
  1685.  
  1686.         /*static Entity GetReferenceERPBatiment(IOrganizationService Service, string NoBatiment)
  1687.         {
  1688.             QueryExpression query = new QueryExpression();
  1689.  
  1690.             query.ColumnSet = new ColumnSet("new_batiment", "new_referenceerpbatimentid", "new_name");
  1691.             query.EntityName = "new_referenceerpbatiment";
  1692.  
  1693.             FilterExpression Fe = new FilterExpression();
  1694.  
  1695.             ConditionExpression Ce = new ConditionExpression();
  1696.             Ce.AttributeName = "new_name";
  1697.             Ce.Operator = ConditionOperator.Equal;
  1698.             Ce.Values.Add(NoBatiment);
  1699.             Fe.AddCondition(Ce);
  1700.  
  1701.             query.Criteria.AddFilter(Fe);
  1702.  
  1703.             try
  1704.             {
  1705.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1706.                 return retrieve[0];
  1707.             }
  1708.             catch (Exception)
  1709.             {
  1710.                 //throw new Exception(ex.Message);
  1711.                 new_referenceerpbatiment entity = new new_referenceerpbatiment();
  1712.                ///entity.new_Compte = new EntityReference("Account", null); ;
  1713.                 return entity;
  1714.             }
  1715.         }*/
  1716.         static EntityCollection GetActivities(IOrganizationService Service, String Condition1, Guid Valeur1)
  1717.         {
  1718.             QueryExpression query = new QueryExpression();
  1719.  
  1720.             query.ColumnSet = new ColumnSet("regardingobjectid", "activitytypecode", "activityid");
  1721.             query.EntityName = "activitypointer";
  1722.  
  1723.             FilterExpression Fe = new FilterExpression(LogicalOperator.And);
  1724.  
  1725.             ConditionExpression Ce = new ConditionExpression();
  1726.             Ce.AttributeName = "regardingobjectid";
  1727.             Ce.Operator = ConditionOperator.Equal;
  1728.             Ce.Values.Add(Valeur1);
  1729.             Fe.AddCondition(Ce);
  1730.  
  1731.             query.Criteria.AddFilter(Fe);
  1732.  
  1733.             try
  1734.             {
  1735.                 EntityCollection retrieve = Service.RetrieveMultiple(query);
  1736.                 return retrieve;
  1737.             }
  1738.             catch (Exception ex)
  1739.             {
  1740.                 throw new Exception(ex.Message);
  1741.             }
  1742.         }
  1743.  
  1744.     }
  1745. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement