예제 #1
0
 static function generateIdex(CMbObject $object, $tag, $group_id)
 {
     $group = CGroups::loadFromGuid("CGroups-{$group_id}");
     // On préfère générer un identifiant d'un établissement virtuel pour les séjours non-facturables
     $group_id_pour_sejour_facturable = CAppUI::conf('dPsante400 CDomain group_id_pour_sejour_facturable', $group);
     if ($object instanceof CSejour && !$object->facturable && $group_id_pour_sejour_facturable) {
         $group_id = $group_id_pour_sejour_facturable;
     }
     $group_domain = new CGroupDomain();
     $group_domain->object_class = $object->_class;
     $group_domain->group_id = $group_id;
     $group_domain->master = 1;
     $group_domain->loadMatchingObject();
     if (!$group_domain->_id) {
         return;
     }
     $domain = $group_domain->loadRefDomain();
     $conf = CAppUI::conf("dPsante400 CIncrementer");
     $cluster_count = abs(intval($conf["cluster_count"]));
     $cluster_position = abs(intval($conf["cluster_position"]));
     if ($cluster_count == 0) {
         $cluster_count = 1;
     }
     if ($cluster_count == 1) {
         $cluster_position = 0;
     }
     $mutex = new CMbSemaphore("incrementer-{$object->_class}");
     $mutex->acquire();
     $incrementer = $domain->loadRefIncrementer();
     // Chargement du dernier 'increment' s'il existe sinon on déclenche une erreur
     if (!$incrementer->_id) {
         $mutex->release();
         return;
     }
     // Incrementation de l'idex
     $value = $incrementer->value;
     // Valeur compatible avec la position dans le cluster
     do {
         $value++;
     } while ($value % $cluster_count != $cluster_position);
     do {
         // Idex vraiment disponible ?
         $idex = new CIdSante400();
         $idex->object_class = $object->_class;
         $idex->tag = $tag;
         $idex->id400 = self::formatValue($object, $incrementer->pattern, $value);
         $idex->loadMatchingObject();
     } while ($idex->_id && ($value += $cluster_count));
     $incrementer->value = $value;
     $incrementer->last_update = CMbDT::dateTime();
     $incrementer->store();
     // Création de l'identifiant externe
     $idex->object_id = $object->_id;
     $idex->last_update = CMbDT::dateTime();
     $idex->store();
     $mutex->release();
     return $idex;
 }