Skip to content

Commit

Permalink
#508 [CompletionCertificate] fix: loop in hook instead of write_file
Browse files Browse the repository at this point in the history
  • Loading branch information
evarisk-micka committed Jan 18, 2024
1 parent 3a3ae08 commit 44b0f45
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 156 deletions.
60 changes: 60 additions & 0 deletions class/actions_dolimeet.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,66 @@ public function saturneBuildDoc(array $parameters, CommonObject $object, string
}
}
}
} else if (strpos($parameters['context'], 'contractcard') !== false) {
if (strpos($parameters['model'], 'completioncertificate') !== false) {
require_once __DIR__ . '/session.class.php';

$session = new Session($this->db);
$document = new CompletioncertificateDocument($this->db);
$signatory = new SaturneSignature($this->db, 'dolimeet', $object->element);

$duration = 0;
$sessions = $session->fetchAll('', '', 0, 0, ['customsql' => 't.fk_contrat = ' . $object->id . ') AND (t.status != -1']);
if (is_array($sessions) && !empty($sessions)) {
foreach ($sessions as $session) {
$duration += $session->duration;
}
$dateEnd = $sessions[count($sessions)]->date_end;
if ($dateEnd != $object->array_options['options_trainingsession_end']) {
setEventMessages($langs->trans('WarningDateEndNotEqual'), [], 'warnings');
}
}
$parameters['moreparams']['object'] = $object;
$parameters['moreparams']['object']->date_start = $object->array_options['options_trainingsession_start'];
$parameters['moreparams']['object']->date_end = $object->array_options['options_trainingsession_end'];
$parameters['moreparams']['object']->duration = $duration;
$parameters['moreparams']['object']->fk_contrat = $object->id;
$contactList = [];
$responsible = 0;
foreach (['external', 'internal'] as $source) {
$contactList = array_merge($contactList, $object->liste_contact(-1, $source, 0, 'TRAINEE'));
$responsible = ($object->liste_contact(-1, $source, 0, 'SESSIONTRAINER') ?: $responsible);
}
$parameters['moreparams']['object']->id = $responsible[0]['id'];
$parameters['moreparams']['object']->element = 'trainingsession';
if (is_array($contactList) && !empty($contactList)) {
foreach ($contactList as $contact) {
$parameters['moreparams']['attendant'] = $signatory;
$parameters['moreparams']['attendant']->firstname = $contact['firstname'];
$parameters['moreparams']['attendant']->lastname = $contact['lastname'];
$parameters['moreparams']['attendant']->element_type = ($contact['source'] == 'external' ? 'socpeople' : 'user');
$parameters['moreparams']['attendant']->element_id = $contact['id'];
$document->element = 'trainingsessiondocument';
$result = $document->generateDocument((!empty($parameters['models']) ? $parameters['models'][1] : $parameters['model']), $parameters['outputlangs'], $parameters['hidedetails'], $parameters['hidedesc'], $parameters['hideref'], $parameters['moreparams']);
if ($result <= 0) {
setEventMessages($document->error, $document->errors, 'errors');
$action = '';
}
}
$documentType = explode('_odt', (!empty($parameters['models']) ? $parameters['models'][1] : $parameters['model']));
if ($document->element != $documentType[0]) {
$document->element = $documentType[0];
}
setEventMessages($langs->trans('FileGenerated') . ' - ' . '<a href=' . DOL_URL_ROOT . '/document.php?modulepart=dolimeet&file=' . urlencode($document->element . '/' . $object->ref . '/' . $document->last_main_doc) . '&entity=' . $conf->entity . '"' . '>' . $document->last_main_doc, []);
$urlToRedirect = $_SERVER['REQUEST_URI'];
$urlToRedirect = preg_replace('/#builddoc$/', '', $urlToRedirect);
$urlToRedirect = preg_replace('/action=builddoc&?/', '', $urlToRedirect); // To avoid infinite loop
if (!GETPOST('forcebuilddoc')){
header('Location: ' . $urlToRedirect . '#builddoc');
exit;
}
}
}
}

return 0; // or return 1 to replace standard code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,181 +100,88 @@ public function write_file(SaturneDocuments $objectDocument, Translate $outputLa
{
global $conf, $langs;

require_once DOL_DOCUMENT_ROOT . '/contrat/class/contrat.class.php';
require_once __DIR__ . '/../../../../../class/session.class.php';
$object = $moreParam['object'];

$object = $moreParam['object'];
$userTmp = new User($this->db);
$thirdparty = new Societe($this->db);
$signatory = new SaturneSignature($this->db, 'dolimeet', $object->element);
$userTmp = new User($this->db);
$signatory = new SaturneSignature($this->db, 'dolimeet', $object->element);

if (get_class($object) == 'Contrat') {
$object->fetch_optionals();
if (!empty($object->array_options['options_label'])) {
$tmpArray['contract_label'] = $object->array_options['options_label'];
if (!empty($moreParam['attendant'])) {
$moreParam['documentName'] = strtoupper($moreParam['attendant']->lastname) . '_' . ucfirst($moreParam['attendant']->firstname) . '_';
} else {
$moreParam['documentName'] = '';
}

if (!empty($object->fk_contrat)) {
require_once DOL_DOCUMENT_ROOT . '/contrat/class/contrat.class.php';
$contract = new Contrat($this->db);
$contract->fetch($object->fk_contrat);
$contract->fetch_optionals();
if (!empty($contract->array_options['options_label'])) {
$tmpArray['contract_label'] = $contract->array_options['options_label'];
} else {
$tmpArray['contract_label'] = $object->ref;
$tmpArray['contract_label'] = $contract->ref;
}
$trainingSessionDict = saturne_fetch_dictionary('c_trainingsession_type');
if (is_array($trainingSessionDict) && !empty($trainingSessionDict) && !empty($object->array_options['options_trainingsession_type'])) {
$tmpArray['action_nature'] = $langs->trans($trainingSessionDict[$object->array_options['options_trainingsession_type']]->label);
if (is_array($trainingSessionDict) && !empty($trainingSessionDict) && !empty($contract->array_options['options_trainingsession_type'])) {
$tmpArray['action_nature'] = $langs->trans($trainingSessionDict[$contract->array_options['options_trainingsession_type']]->label);
} else {
$tmpArray['action_nature'] = '';
}

$tmpArray['date_start'] = dol_print_date($object->array_options['options_trainingsession_start'], 'dayhour', 'tzuser');
$tmpArray['date_end'] = dol_print_date($object->array_options['options_trainingsession_end'], 'dayhour', 'tzuser');

$duration = 0;
$session = new Session($this->db);
$sessions = $session->fetchAll('', '', 0, 0, ['customsql' => 't.fk_contrat = ' . $object->id . ') AND (t.status = 2']);
if (is_array($sessions) && !empty($sessions)) {
foreach ($sessions as $session) {
$duration += $session->duration;
}
$dateEnd = $sessions[count($sessions)]->date_end;
if ($dateEnd != $object->array_options['options_trainingsession_end']) {
setEventMessages($langs->trans('WarningDateEndNotEqual'), [], 'warnings');
}
}
$tmpArray['duration'] = convertSecondToTime($duration);

$contactList = [];
foreach (['external', 'internal'] as $source) {
$contactList = array_merge($contactList, $object->liste_contact(-1, $source, 0, 'TRAINEE'));
$responsible = $object->liste_contact(-1, $source, 0, 'SESSIONTRAINER');
}
if (is_array($contactList) && !empty($contactList)) {
foreach ($contactList as $contact) {
$moreParam['documentName'] = strtoupper($contact['lastname']) . '_' . ucfirst($contact['firstname']) . '_';

$tmpArray['attendant_fullname'] = strtoupper($contact['lastname']) . ' ' . ucfirst($contact['firstname']);
if ($contact['source'] == 'internal') {
$tmpArray['attendant_company_name'] = $conf->global->MAIN_INFO_SOCIETE_NOM;
} elseif ($contact['source'] == 'external') {
$thirdparty->fetch($contact['socid']);
$tmpArray['attendant_company_name'] = $thirdparty->name;
} else {
$tmpArray['attendant_company_name'] = '';
}

if (getDolGlobalInt('DOLIMEET_SESSION_TRAINER_RESPONSIBLE') > 0) {
$signatory = $signatory->fetchSignatory('UserSignature', $conf->global->DOLIMEET_SESSION_TRAINER_RESPONSIBLE, 'user');
} else {
$signatory = $signatory->fetchSignatory('SessionTrainer', $responsible[0]['id'], 'trainingsession');
}
if (is_array($signatory) && !empty($signatory)) {
$signatory = array_shift($signatory);
$userTmp->fetch($signatory->element_id);
$tmpArray['mycompany_owner_fullname'] = strtoupper($userTmp->lastname) . ' ' . ucfirst($userTmp->firstname);
$tmpArray['mycompany_owner_job'] = $userTmp->job;
if (dol_strlen($signatory->signature) > 0 && $signatory->signature != $langs->transnoentities('FileGenerated')) {
if ($moreParam['specimen'] == 0 || ($moreParam['specimen'] == 1 && $conf->global->DOLIMEET_SHOW_SIGNATURE_SPECIMEN == 1)) {
$tempDir = $conf->dolimeet->multidir_output[$object->entity ?? 1] . '/temp/';
$encodedImage = explode(',', $signatory->signature)[1];
$decodedImage = base64_decode($encodedImage);
file_put_contents($tempDir . 'signature.png', $decodedImage);
$tmpArray['mycompany_owner_signature'] = $tempDir . 'signature.png';
} else {
$tmpArray['mycompany_owner_signature'] = '';
}
} else {
$tmpArray['mycompany_owner_signature'] = '';
}
} else {
$tmpArray['mycompany_owner_fullname'] = '';
$tmpArray['mycompany_owner_job'] = '';
$tmpArray['mycompany_owner_signature'] = '';
}

$tmpArray['date_creation'] = dol_print_date(dol_now(), 'dayhour', 'tzuser');

$moreParam['tmparray'] = $tmpArray;

$result = parent::write_file($objectDocument, $outputLangs, $srcTemplatePath, $hideDetails, $hideDesc, $hideRef, $moreParam);
}
} else {
$object->errors = [$langs->trans('NoContactDefined')];
$result = -1;
}
return $result;
} else {
if (!empty($moreParam['attendant'])) {
$moreParam['documentName'] = strtoupper($moreParam['attendant']->lastname) . '_' . ucfirst($moreParam['attendant']->firstname) . '_';
} else {
$moreParam['documentName'] = '';
}

if (!empty($object->fk_contrat)) {
$contract = new Contrat($this->db);
$contract->fetch($object->fk_contrat);
$contract->fetch_optionals();
if (!empty($contract->array_options['options_label'])) {
$tmpArray['contract_label'] = $contract->array_options['options_label'];
} else {
$tmpArray['contract_label'] = $contract->ref;
}
$trainingSessionDict = saturne_fetch_dictionary('c_trainingsession_type');
if (is_array($trainingSessionDict) && !empty($trainingSessionDict) && !empty($contract->array_options['options_trainingsession_type'])) {
$tmpArray['action_nature'] = $langs->trans($trainingSessionDict[$contract->array_options['options_trainingsession_type']]->label);
} else {
$tmpArray['action_nature'] = '';
}
} else {
$tmpArray['contract_label'] = '';
$tmpArray['action_nature'] = '';
}

$tmpArray['date_start'] = dol_print_date($object->date_start, 'dayhour', 'tzuser');
$tmpArray['date_end'] = dol_print_date($object->date_end, 'dayhour', 'tzuser');
$tmpArray['duration'] = convertSecondToTime($object->duration);
$tmpArray['contract_label'] = '';
$tmpArray['action_nature'] = '';
}

$tmpArray['attendant_fullname'] = strtoupper($moreParam['attendant']->lastname) . ' ' . ucfirst($moreParam['attendant']->firstname);
if ($moreParam['attendant']->element_type == 'user') {
$tmpArray['attendant_company_name'] = $conf->global->MAIN_INFO_SOCIETE_NOM;
} elseif ($moreParam['attendant']->element_type == 'socpeople') {
$contact = new Contact($this->db);
$contact->fetch($moreParam['attendant']->element_id);
$thirdparty->fetch($contact->fk_soc);
$tmpArray['attendant_company_name'] = $thirdparty->name;
} else {
$tmpArray['attendant_company_name'] = '';
}
$tmpArray['date_start'] = dol_print_date($object->date_start, 'dayhour', 'tzuser');
$tmpArray['date_end'] = dol_print_date($object->date_end, 'dayhour', 'tzuser');
$tmpArray['duration'] = convertSecondToTime($object->duration);

$tmpArray['attendant_fullname'] = strtoupper($moreParam['attendant']->lastname) . ' ' . ucfirst($moreParam['attendant']->firstname);
if ($moreParam['attendant']->element_type == 'user') {
$tmpArray['attendant_company_name'] = $conf->global->MAIN_INFO_SOCIETE_NOM;
} elseif ($moreParam['attendant']->element_type == 'socpeople') {
$contact = new Contact($this->db);
$thirdparty = new Societe($this->db);
$contact->fetch($moreParam['attendant']->element_id);
$thirdparty->fetch($contact->fk_soc);
$tmpArray['attendant_company_name'] = $thirdparty->name;
} else {
$tmpArray['attendant_company_name'] = '';
}

if (getDolGlobalInt('DOLIMEET_SESSION_TRAINER_RESPONSIBLE') > 0) {
$signatory = $signatory->fetchSignatory('UserSignature', $conf->global->DOLIMEET_SESSION_TRAINER_RESPONSIBLE, 'user');
} else {
$signatory = $signatory->fetchSignatory('SessionTrainer', $object->id, $object->element);
}
if (is_array($signatory) && !empty($signatory)) {
$signatory = array_shift($signatory);
$userTmp->fetch($signatory->element_id);
$tmpArray['mycompany_owner_fullname'] = strtoupper($userTmp->lastname) . ' ' . ucfirst($userTmp->firstname);
$tmpArray['mycompany_owner_job'] = $userTmp->job;
if (dol_strlen($signatory->signature) > 0 && $signatory->signature != $langs->transnoentities('FileGenerated')) {
if ($moreParam['specimen'] == 0 || ($moreParam['specimen'] == 1 && $conf->global->DOLIMEET_SHOW_SIGNATURE_SPECIMEN == 1)) {
$tempDir = $conf->dolimeet->multidir_output[$object->entity ?? 1] . '/temp/';
$encodedImage = explode(',', $signatory->signature)[1];
$decodedImage = base64_decode($encodedImage);
file_put_contents($tempDir . 'signature.png', $decodedImage);
$tmpArray['mycompany_owner_signature'] = $tempDir . 'signature.png';
} else {
$tmpArray['mycompany_owner_signature'] = '';
}
if (getDolGlobalInt('DOLIMEET_SESSION_TRAINER_RESPONSIBLE') > 0) {
$signatory = $signatory->fetchSignatory('UserSignature', $conf->global->DOLIMEET_SESSION_TRAINER_RESPONSIBLE, 'user');
} else {
$signatory = $signatory->fetchSignatory('SessionTrainer', $object->id, $object->element);
}
if(is_array($signatory) && !empty($signatory)) {
$signatory = array_shift($signatory);
$userTmp->fetch($signatory->element_id);
$tmpArray['mycompany_owner_fullname'] = strtoupper($userTmp->lastname) . ' ' . ucfirst($userTmp->firstname);
$tmpArray['mycompany_owner_job'] = $userTmp->job;
if (dol_strlen($signatory->signature) > 0 && $signatory->signature != $langs->transnoentities('FileGenerated')) {
if ($moreParam['specimen'] == 0 || ($moreParam['specimen'] == 1 && $conf->global->DOLIMEET_SHOW_SIGNATURE_SPECIMEN == 1)) {
$tempDir = $conf->dolimeet->multidir_output[$object->entity ?? 1] . '/temp/';
$encodedImage = explode(',', $signatory->signature)[1];
$decodedImage = base64_decode($encodedImage);
file_put_contents($tempDir . 'signature.png', $decodedImage);
$tmpArray['mycompany_owner_signature'] = $tempDir . 'signature.png';
} else {
$tmpArray['mycompany_owner_signature'] = '';
}
} else {
$tmpArray['mycompany_owner_fullname'] = '';
$tmpArray['mycompany_owner_job'] = '';
$tmpArray['mycompany_owner_signature'] = '';
}
} else {
$tmpArray['mycompany_owner_fullname'] = '';
$tmpArray['mycompany_owner_job'] = '';
$tmpArray['mycompany_owner_signature'] = '';
}

$tmpArray['date_creation'] = dol_print_date(dol_now(), 'dayhour', 'tzuser');
$tmpArray['date_creation'] = dol_print_date(dol_now(), 'dayhour', 'tzuser');

$moreParam['tmparray'] = $tmpArray;
$moreParam['tmparray'] = $tmpArray;

return parent::write_file($objectDocument, $outputLangs, $srcTemplatePath, $hideDetails, $hideDesc, $hideRef, $moreParam);
}
return parent::write_file($objectDocument, $outputLangs, $srcTemplatePath, $hideDetails, $hideDesc, $hideRef, $moreParam);
}
}

0 comments on commit 44b0f45

Please sign in to comment.