--- /dev/null
+<?php
+
+require_once('protected/required.php');
+
+class SugarAppliance {
+
+ // TO-DO : IMPLEMENT ATTRIBUTES, CONSTRUCTOR ETC.
+ //protected $id, $product, $manager, $reference, $changelog, $maintained, $internalFqdn, $externalFqdn, $os, $discInfos, $channel;
+
+ /// CONSTRUCTOR
+ /*function __construct($id = null, $product = null, $manager = null, $reference = null, $changelog = null,
+ $maintained = null, $internalFqdn = null, $externalFqdn = null, $os = null, $discInfos = null, $channel = null) {
+
+ $this -> id = $id;
+ $this -> product = $product;
+ // MANAGER REFERENCE
+ $this -> manager = $manager;
+ // APPLIANCE REFERENCE
+ $this -> reference = $reference;
+ // CHANGELOG ID
+ $this -> changelog = $changelog;
+ // APPLIANCE MAINTENANCE STATE
+ $this -> maintained = $maintained;
+ // FQDNs
+ $this -> internalFqdn = $internalFqdn;
+ $this -> externalFqdn = $externalFqdn;
+ // OS
+ $this -> os = $os;
+ // UUIDS
+ $this -> discInfos = $discInfos;
+ // CHANNEL
+ $this -> channel = $channel;
+ }*/
+
+ /// GETTERS
+ /*public function getId() { return $this -> id; }
+ public function getProduct() { return $this->product; }
+ public function getManager() { return $this->manager; }
+ public function getReference() { return $this->reference; }
+ public function getChangelog() { return $this->changelog; }
+ public function isMaintained() { return $this->maintained; }
+ public function getInternalFqdn() { return $this->internalFqdn; }
+ public function getExternalFqdn() { return $this->externalFqdn; }
+ public function getOs() { return $this->os; }
+ public function getDiscInfos() { return $this->discInfos; }
+ public function getChannel() { return $this->channel; }*/
+
+ /// SETTERS
+ /*public function setId($id) { $this -> id = $id; }
+ public function setProduct($product) { $this->product = $product; }
+ public function setManager($manager) { $this->manager = $manager; }
+ public function setReference($reference) { $this->reference = $reference; }
+ public function setChangelog($changelog) { $this->changelog = $changelog; }
+ public function setMaintained($maintained) { $this->maintained = $maintained; }
+ public function setInternalFqdn($internalFqdn) { $this->internalFqdn = $internalFqdn; }
+ public function setExternalFqdn($externalFqdn) { $this->externalFqdn = $externalFqdn; }
+ public function setOs($os) { $this->os = $os; }
+ public function setDiscinfos($discInfos) { $this->discInfos = $discInfos; }
+ public function setChannel($channel) { $this->channel = $channel; }*/
+
+ public function createOpportunity($applianceReference, $versionId = null, $versionDeclaration = null) {
+ $applianceManager = new PdoApplianceManager();
+ $changelogManager = new PdoChangelogManager();
+ $productManager = new PdoProductManager();
+ $contractManager = new PdoContractManager();
+ $versionManager = new PdoVersionManager();
+
+ // GETTING REFERENCES AND OBJECTS :
+ // APPLIANCE
+ $appliance = $applianceManager -> getApplianceByReference($applianceReference);
+ $applianceReferenceArray = explode("/",$applianceReference);
+ // PRODUCT
+ $product = $productManager -> getProduct($appliance -> getProduct());
+ $productAcronym = $applianceReferenceArray[0];
+ // VERSION (DEFAULTS TO LAST DECLARED IF NO VERSION INFORMATION IS INDICATED)
+ if(isset($versionId)) {
+ $version = $versionManager -> getVersion($versionId);
+ } else {
+ $version = $changelogManager -> findLastVersionForId($appliance -> getChangelog());
+ }
+ if(!isset($versionDeclaration)) {
+ $versionDeclaration = $changelogManager -> findLastVersionDeclarationForId($appliance -> getChangelog());
+ }
+ $versionReference = $productAcronym.' '.$version -> getValue();
+ // CUSTOMER
+ $customerAPId = substr($applianceReferenceArray[1],0,-11);
+ // CONTRACT
+ $contractReference = $productAcronym."/".$customerAPId."/20".substr($applianceReferenceArray[1],19,-4);
+ $contract = $contractManager->getContractByNumber($contractReference);
+ // OPPORTUNITY
+ $opportunityReference = $productAcronym."/".substr($customerAPId,0,-6);
+
+ // LOGIN
+ $sugarConnector = new SugarConnector();
+ $sugarConnector -> login('admin', 'admin');
+
+ // GETTING APPLIANCE IN SUGAR
+ $moduleName = 'allo_Instances';
+ $requestedInfos = array('name');
+ $sqlWhereClause = "allo_instances.name = '".$applianceReference."'";
+ $appliance_infos = json_decode($sugarConnector -> getInfosModule($moduleName, $requestedInfos, $sqlWhereClause));
+ if(isset($appliance_infos->entry_list[0])) {
+ $applianceSugarId = $appliance_infos->entry_list[0]->id;
+ }
+
+ // QUESTIONING ITS VALIDITY STATE
+ $contractState = $contract->getState();
+
+ // IF NOT VALID OR DOES NOT EXIST, ERROR
+ if(!isset($contract) || ($contractState != "^encours^" && $contractState != "^signe^" && $contractState != "^signe_maj_version^")) {
+ echo "Erreur : Contrat de maintenance inexistant ou invalide pour cette instance";
+ } else {
+
+ // IF APPLIANCE DOES NOT EXIST, CREATING NEW ONE
+ if(!isset($applianceSugarId)) {
+ $moduleName = 'allo_Instances';
+ $parameters = array(
+ 'name' => $applianceReference
+ );
+ $appliance_infos = json_decode($sugarConnector -> updateOrCreateEntryModule($moduleName, $parameters));
+ $applianceSugarId = $appliance_infos->id;
+
+ // GETTING THE MAINTENANCE CONTRACT SUGAR ID
+ $moduleName = 'maint_contrat_maintenance';
+ $requestedInfos = array('id');
+ $sqlWhereClause = "maint_contrat_maintenance.name = '".$contractReference."'";
+ $contract_infos = json_decode($sugarConnector -> getInfosModule($moduleName, $requestedInfos, $sqlWhereClause));
+ if(isset($contract_infos->entry_list[0])) {
+ $contractSugarId = $contract_infos->entry_list[0]->id;
+ }
+
+ // LINKING IT TO MAINTENANCE CONTRACT IN SUGAR
+ if(isset($applianceSugarId) && isset($contractSugarId)) {
+ $moduleName = 'maint_contrat_maintenance';
+ $parameters = array(
+ 'link_name' => 'maint_contrat_maintenance_allo_instances_1',
+ 'primary_module_id' => $contractSugarId,
+ 'related_module_id' => $applianceSugarId
+ );
+ $sugarConnector -> createRelationship($moduleName, $parameters);
+ }
+
+ // LINKING IT TO MAINTENANCE CONTRACT IN DB
+ $contractAppliances = $contract -> getAppliances();
+ $contractAppliances[] = $appliance -> getId();
+ $contractManager -> updateContract($contract);
+ }
+
+ // GETTING OPPORTUNITIES OF APPLIANCE
+ $moduleName = 'allo_Instances';
+ $parameters = array(
+ "link_name" => "allo_instances_opportunities_1",
+ "primary_module_id" => $applianceSugarId,
+ );
+ $requestedInfos = array(
+ 'id',
+ 'name',
+ 'date_closed',
+ 'date_entered',
+ 'liste_hebergement_c',
+ 'type_exploitation_c'
+ );
+ $sqlWhereClause = "opportunities.name = '".$opportunityReference."'";
+ $opportunity_infos = json_decode($sugarConnector -> getRelationships($moduleName, $parameters, $requestedInfos, $sqlWhereClause));
+
+ $opportunitySugarId = $opportunity_infos->entry_list[0]->id;
+ if(isset($opportunitySugarId)) {
+
+ // GETTING MOST RECENT OPPORTUNITY
+ $lastOpportunity = $opportunity_infos->entry_list[0];
+ foreach($opportunity_infos->entry_list as $opportunity_info) {
+
+ // RESEARCH CRITERION : NO CLOSING DATE AND LAST ADDED
+ if(!isset($opportunity_info->name_value_list->date_closed->value) &&
+ $opportunity_info->name_value_list->date_entered->value > $lastOpportunity->name_value_list->date_entered->value) {
+ $lastOpportunity = $opportunity_info;
+ }
+ }
+
+ // UPDATING LAST OPPORTUNITY IN SUGAR : ADDING CLOSING DATE
+ $lastOpportunitySugarId = $lastOpportunity->id;
+ if(isset($versionDeclaration)) {
+ $moduleName = 'Opportunities';
+ $parameters = array(
+ 'id' => $lastOpportunitySugarId,
+ 'date_closed' => $versionDeclaration
+ );
+ $sugarConnector -> updateOrCreateEntryModule($moduleName, $parameters);
+
+ // CREATING NEW OPPORTUNITY
+ $moduleName = 'Opportunities';
+ $parameters = array(
+ 'name' => $lastOpportunity->name_value_list->name->value,
+ 'type_exploitation_c' => $lastOpportunity->name_value_list->type_exploitation_c->value,
+ 'date_opened_c' => $versionDeclaration,
+ 'systeme_d_exploitation_c' => $appliance->getOs(),
+ 'liste_hebergement_c' => $lastOpportunity->name_value_list->liste_hebergement_c->value,
+ 'description' => 'Création par ALLO'
+ );
+ $opportunity_infos = json_decode($sugarConnector -> updateOrCreateEntryModule($moduleName, $parameters));
+ $opportunitySugarId = $opportunity_infos->id;
+ }
+
+ // IF NO OPPORTUNITY IS FOUND ON SUGAR FOR THIS APPLIANCE, CREATING NEW ONE
+ } else {
+ $moduleName = 'Opportunities';
+ $parameters = array(
+ 'name' => $opportunityReference,
+ 'type_exploitation_c' => $appliance->getChannel(),
+ 'date_opened_c' => $changelogManager->findFirstVersionDeclarationForId($appliance->getChangelog()),
+ 'systeme_d_exploitation_c' => $appliance->getOs(),
+ // TO-DO : CHANGE THIS VALUE FOR EACH MANAGEMENT TYPE
+ 'liste_hebergement_c' => 'CLIENT',
+ 'description' => 'Création par ALLO'
+ );
+ $opportunity_infos = json_decode($sugarConnector -> updateOrCreateEntryModule($moduleName, $parameters));
+ $opportunitySugarId = $opportunity_infos->id;
+ }
+
+ // LINKING OPPORTUNITY TO APPLIANCE
+ if(isset($applianceSugarId) && isset($opportunitySugarId)) {
+ $moduleName = 'allo_Instances';
+ $parameters = array(
+ 'link_name' => 'allo_instances_opportunities_1',
+ 'primary_module_id' => $applianceSugarId,
+ 'related_module_id' => $opportunitySugarId
+ );
+ $sugarConnector -> createRelationship($moduleName, $parameters);
+ }
+
+ // GETTING VERSION ID IN SUGAR
+ $moduleName = 'adul__Produits';
+ $requestedInfos = array('name');
+ $sqlWhereClause = "adul__produits.name = '".$versionReference."'";
+ $version_infos = json_decode($sugarConnector -> getInfosModule($moduleName, $requestedInfos, $sqlWhereClause));
+ if(isset($version_infos->entry_list[0])) {
+ $versionSugarId = $version_infos->entry_list[0]->id;
+ }
+
+ // LINKING OPPORTUNITY TO VERSION
+ if(isset($versionSugarId) && isset($opportunitySugarId)) {
+ $moduleName = 'adul__Produits';
+ $parameters = array(
+ 'link_name' => 'adul__prits_opportunities',
+ 'primary_module_id' => $versionSugarId,
+ 'related_module_id' => $opportunitySugarId
+ );
+ $sugarConnector -> createRelationship($moduleName, $parameters);
+ }
+
+ // GETTING PRODUCT CATALOG ID IN SUGAR
+ $moduleName = 'maint_catalogue_produits';
+ $requestedInfos = array('name');
+ $sqlWhereClause = "maint_catalogue_produits.name = '".$product -> getName()."'";
+ $catalog_infos = json_decode($sugarConnector -> getInfosModule($moduleName, $requestedInfos, $sqlWhereClause));
+ if(isset($catalog_infos->entry_list[0])) {
+ $catalogSugarId = $catalog_infos->entry_list[0]->id;
+ }
+
+ // LINKING OPPORTUNITY TO PRODUCT CATALOG
+ if(isset($catalogSugarId) && isset($opportunitySugarId)) {
+ $moduleName = 'maint_catalogue_produits';
+ $parameters = array(
+ 'link_name' => 'maint_caits_opportunities',
+ 'primary_module_id' => $catalogSugarId,
+ 'related_module_id' => $opportunitySugarId
+ );
+ $sugarConnector -> createRelationship($moduleName, $parameters);
+ }
+
+ // GETTING CUSTOMER ID
+ $moduleName = 'Accounts';
+ $requestedInfos = array(
+ 'id',
+ 'officenumber_c'
+ );
+ $sqlWhereClause = "accounts_cstm.officenumber_c = '".$customerAPId."'";
+ $customer_infos = json_decode($sugarConnector -> getInfosModule($moduleName, $requestedInfos, $sqlWhereClause));
+ if(isset($customer_infos->entry_list[0])) {
+ $customerSugarId = $customer_infos->entry_list[0]->id;
+ }
+
+ // LINKING OPPORTUNITY TO CUSTOMER (FOR N-N RELATIONS, IT GOES BOTH WAYS)
+ if(isset($customerSugarId) && isset($opportunitySugarId)) {
+ $moduleName = 'Accounts';
+ $parameters = array(
+ 'link_name' => 'opportunities',
+ 'primary_module_id' => $customerSugarId,
+ 'related_module_id' => $opportunitySugarId
+ );
+ $sugarConnector -> createRelationship($moduleName, $parameters);
+ $moduleName = 'Opportunities';
+ $parameters = array(
+ 'link_name' => 'accounts',
+ 'primary_module_id' => $opportunitySugarId,
+ 'related_module_id' => $opportunitySugarId
+ );
+ $sugarConnector -> createRelationship($moduleName, $parameters);
+ }
+
+ // TO-DO : LINK OPPORTUNITY TO MAINTENANCE CONTRACT
+ }
+ }
+
+ public function checkAll() {
+ $applianceManager = new PdoApplianceManager();
+ $changelogManager = new PdoChangelogManager();
+ $warningManager = new PdoWarningManager();
+ $versionManager = new PdoVersionManager();
+
+ // LOGIN
+ $sugarConnector = new SugarConnector();
+ $sugarConnector -> login('admin', 'admin');
+
+ // GETTING APPLIANCES
+ $moduleName = 'allo_Instances';
+ $requestedInfos = array(
+ 'id',
+ 'name'
+ );
+ $appliances_infos = json_decode($sugarConnector -> getInfosModule($moduleName, $requestedInfos));
+
+ foreach($appliances_infos->entry_list as $appliance_infos) {
+
+ // IF APPLIANCE EXISTS IN SUGAR
+ if(isset($appliance_infos)) {
+ $applianceReference = $appliance_infos->name_value_list->name->value;
+ $applianceReferenceArray = explode("/",$applianceReference);
+ $customerAPId = substr($applianceReferenceArray[1],0,-11);
+ $opportunityReference = $applianceReferenceArray[0]."/".substr($customerAPId,0,-6);
+
+ // GETTING APPLIANCE IN DB
+ $appliance = $applianceManager -> getApplianceByReference($applianceReference);
+
+ // IF APPLIANCE EXISTS ON BOTH SIDES, COMPARING INFORMATIONS
+ if(isset($appliance) && $appliance->getId() > 0) {
+
+ // GETTING CHANGELOG OF APPLIANCE
+ $changelog = $changelogManager -> getChangelog($appliance -> getChangelog());
+ $versions = $changelog -> getVersions();
+
+ // CHECKING OPPORTUNITY EXISTENCE FOR EACH VERSION OF CHANGELOG
+ foreach($versions as $version) {
+
+ // GETTING VALUE OF EACH VERSION OF CHANGELOG, AND ITS DECLARATION DATE
+ $versionArray = explode(';',$version);
+ $versionId = $versionArray[0];
+ $versionDeclaration = $versionArray[1];
+ $versionValue = $versionManager -> getVersion($versionId) -> getValue();
+ $versionReference = $applianceReferenceArray[0].' '.$versionValue;
+
+ // GETTING VERSION ID ON SUGAR
+ $moduleName = 'adul__Produits';
+ $requestedInfos = array(
+ 'id',
+ 'name'
+ );
+ $sqlWhereClause = "adul__produits.name = '".$versionReference."'";
+ $version_infos = json_decode($sugarConnector -> getInfosModule($moduleName, $requestedInfos, $sqlWhereClause));
+ if(isset($version_infos->entry_list[0])) {
+ $versionSugarId = $version_infos->entry_list[0]->id;
+ }
+
+ // GETTING OPPORTUNITIES LINKED TO THE VERSION IN SUGAR
+ if(isset($versionSugarId)) {
+ $moduleName = 'adul__Produits';
+ $parameters = array(
+ "link_name" => "adul__prits_opportunities",
+ "primary_module_id" => $versionSugarId,
+ );
+ $requestedInfos = array(
+ 'id',
+ 'name',
+ 'date_opened_c'
+ );
+ $sqlWhereClause = "opportunities.name = '".$opportunityReference."'";
+ $result = json_decode($sugarConnector -> getRelationships($moduleName, $parameters, $requestedInfos, $sqlWhereClause));
+
+ // COMPARING INFORMATIONS IN ORDER TO FIND A CONCORDANCE : DATES, CHANNEL, et cetera
+ $opportunityFoundInSugar = false;
+ foreach($result->entry_list as $opportunity_info) {
+
+ // IF OPPORTUNITY EXISTS IN SUGAR
+ if(isset($opportunity_info->id)) {
+ if($opportunity_info->name_value_list->date_opened_c->value == $versionDeclaration) {
+ $opportunityFoundInSugar = true;
+ break;
+ }
+ }
+ }
+
+ // IF NO OPPORTUNITY IS FOUND FOR THE VERSION
+ if(!$opportunityFoundInSugar) {
+
+ // CREATING NEW WARNING, VERSION TYPE
+ $warningMessage = "Version declaree ".$versionValue." n'a pas de fiche d'installation sur Sugar pour cette instance applicative.";
+ $warning = new Warning(null, $versionDeclaration, "synchro", $warningMessage, $versionId);
+ $warningManager -> createWarning($warning);
+
+ // GETTING APPLIANCE CHANGELOG
+ $changelogManager = new PdoChangelogManager();
+ $changelog = $changelogManager -> getChangelog($appliance -> getChangelog());
+
+ // ADDING WARNING TO CHANGELOG
+ $warnings = $changelog -> getWarnings();
+ if((array_search("", $warnings)) !== false) {
+ $warnings = array($warningManager -> getLastWarning() -> getId());
+ } else {
+ $warnings[] = $warningManager -> getLastWarning() -> getId();
+ }
+ $changelog -> setWarnings($warnings);
+ $changelogManager -> updateChangelog($changelog);
+ }
+ } else {
+ error_log("Version ".$versionReference." non trouvee sur Sugar.");
+ }
+ }
+ } else {
+ error_log("Instance applicative existante sur Sugar introuvable sur ALLO Serveur.");
+ }
+ }
+ }
+ }
+}