Skip to content

Commit

Permalink
Evarisk#268 [Triggers] fix: rework linked objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Sep 24, 2024
1 parent 60cf3f9 commit c4bde31
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 62 deletions.
87 changes: 25 additions & 62 deletions core/tpl/registrationcertificatefr_linked_objects.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,75 +27,38 @@
* Variable : $fromProductLot
*/

// Load Dolibarr libraries
require_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';
require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';

// Load DoliCar libraries
require_once __DIR__ . '/../../class/registrationcertificatefr.class.php';

$registrationCertificate = new RegistrationCertificateFr($db);
$propal = new Propal($db);
$commande = new Commande($db);
$facture = new Facture($db);

$registrationCertificates = $registrationCertificate->fetchAll('', '',0,0, ['customsql' => ($fromProductLot ? 't.fk_lot = ' : 't.rowid = ') . GETPOST('id')]);
if (is_array($registrationCertificates) && !empty($registrationCertificates)) {
foreach ($registrationCertificates as $registrationCertificate) {
$objectsLinkedList[$registrationCertificate->id] = $registrationCertificate->getLinkedObjects();
}

$out = load_fiche_titre($langs->transnoentities('LinkedObjects'), '', 'dolicar_color@dolicar');
$out .= '<table class="noborder centpercent">';
$out .= '<tr class="liste_titre">';
$out .= '<td>' . $langs->trans('ObjectType') . '</td>';
$out .= '<td>' . $langs->trans('Object') . '</td>';
$out .= '<td>' . $langs->trans('Mileage') . '</td>';
$out .= '<td>' . $langs->trans('Date') . '</td>';
$out .= '</tr>';
$out = load_fiche_titre($langs->transnoentities('LinkedObjects'), '', 'dolicar_color@dolicar');
$out .= '<table class="noborder centpercent">';
$out .= '<tr class="liste_titre">';
$out .= '<td>' . $langs->trans('ObjectType') . '</td>';
$out .= '<td>' . $langs->trans('Object') . '</td>';
$out .= '<td>' . $langs->trans('Mileage') . '</td>';
$out .= '<td>' . $langs->trans('Date') . '</td>';
$out .= '</tr>';

function renderTableRows($object, $objectIDS, $langs, &$out, $key) {
foreach ($objectIDS as $objectID) {
$object->fetch($objectID);
$object->fetch_optionals();
$registrationCertificate = new RegistrationCertificateFr($db);
$registrationCertificate->fetch(!isset($fromProductLot) ? GETPOST('id') : '', !isset($fromProductLot) ? GETPOST('ref') : '', isset($fromProductLot) ? ' AND t.fk_lot = ' . GETPOST('id') : '');
$registrationCertificate->fetchObjectLinked(null, '', $registrationCertificate->id, $registrationCertificate->module . '_' . $registrationCertificate->element);
if (!empty($registrationCertificate->linkedObjects)) {
foreach ($registrationCertificate->linkedObjects as $linkedObjectElement => $linkedObjects) {
foreach ($linkedObjects as $linkedObject) {
$out .= '<tr>';
$out .= '<td class="nowrap">'. $langs->transnoentities($key) .'</td>';
$out .= '<td>'. $object->getNomUrl(1) .'</td>';
$out .= '<td>'. $object->array_options['options_mileage'] .'</td>';
$out .= '<td>'. dol_print_date($object->date_creation, 'dayhour') .'</td>';
$out .= '<td class="nowrap">' . $langs->transnoentities(ucfirst($linkedObjectElement)) . '</td>';
$out .= '<td>' . $linkedObject->getNomUrl(1) . '</td>';
$out .= '<td>' . $linkedObject->array_options['options_mileage'] . '</td>';
$out .= '<td>' . dol_print_date($linkedObject->date_creation, 'dayhour') . '</td>';
$out .= '</tr>';
}
}
} else {
$out .= '<tr><td colspan="4">' . $langs->trans('NoLinkedObjectsToPrint') . '</td></tr>';
}

if (!empty($objectsLinkedList)) {
foreach ($objectsLinkedList as $subList) {
if (!empty($subList)) {
foreach ($subList as $key => $objectIDS) {
switch ($key) {
case 'facture':
renderTableRows($facture, $objectIDS, $langs, $out, $key);
break;
case 'propal':
renderTableRows($propal, $objectIDS, $langs, $out, $key);
break;
case 'commande':
renderTableRows($commande, $objectIDS, $langs, $out, $key);
break;
}
}
} else {
$out .= '<tr><td colspan="4">' . $langs->trans('NoLinkedObjectsToPrint') . '</td></tr>';
}
}
} else {
$out .= '<tr><td colspan="4">' . $langs->trans('NoLinkedObjectsToPrint') . '</td></tr>';
}
$out .= '</table>';
?>
$out .= '</table>'; ?>

<script>
jQuery('.fichecenter').append(<?php echo json_encode($out); ?>);
</script>
<?php
}
<script>
jQuery('.fichecenter').first().append(<?php echo json_encode($out); ?>);
</script>
42 changes: 42 additions & 0 deletions core/triggers/interface_99_modDoliCar_DoliCarTriggers.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,42 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$actionComm->percentage = -1;

switch ($action) {
case 'PROPAL_CREATE' :
case 'ORDER_CREATE' :
case 'BILL_CREATE' :
if (GETPOSTISSET('options_registrationcertificatefr') && !empty(GETPOST('options_registrationcertificatefr'))) {
require_once __DIR__ . '/../../class/registrationcertificatefr.class.php';
$registrationCertificateFr = new RegistrationCertificateFr($this->db);
$registrationCertificateFr->fetch(GETPOST('options_registrationcertificatefr'));

$registrationCertificateFr->add_object_linked($object->element, $object->id);
}
break;

case 'PROPAL_MODIFY' :
case 'ORDER_MODIFY' :
case 'BILL_MODIFY' :
if (GETPOSTISSET('options_registrationcertificatefr') && !empty(GETPOST('options_registrationcertificatefr'))) {
require_once __DIR__ . '/../../class/registrationcertificatefr.class.php';
$registrationCertificateFr = new RegistrationCertificateFr($this->db);
$registrationCertificateFr->fetch(GETPOST('options_registrationcertificatefr'));

$object->updateObjectLinked(null, '', $registrationCertificateFr->id, $registrationCertificateFr->module . '_' . $registrationCertificateFr->element);
}
break;

case 'PROPAL_DELETE' :
case 'ORDER_DELETE' :
case 'BILL_DELETE' :
if (GETPOSTISSET('options_registrationcertificatefr') && !empty(GETPOST('options_registrationcertificatefr'))) {
require_once __DIR__ . '/../../class/registrationcertificatefr.class.php';
$registrationCertificateFr = new RegistrationCertificateFr($this->db);
$registrationCertificateFr->fetch(GETPOST('options_registrationcertificatefr'));

$object->deleteObjectLinked(null, '', $registrationCertificateFr->id, $registrationCertificateFr->module . '_' . $registrationCertificateFr->element);
}
break;

case 'REGISTRATIONCERTIFICATEFR_CREATE' :
$actionComm->code = 'AC_' . strtoupper($object->element) . '_CREATE';
$actionComm->label = $langs->trans('ObjectCreateTrigger', $langs->transnoentities(ucfirst($object->element)), $object->ref);
Expand All @@ -122,6 +158,12 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$actionComm->create($user);
break;

case 'REGISTRATIONCERTIFICATEFR_ARCHIVE' :
$actionComm->code = 'AC_' . strtoupper($object->element) . '_ARCHIVE';
$actionComm->label = $langs->transnoentities('ObjectArchivedTrigger', $langs->transnoentities(ucfirst($object->element)), $object->ref);
$actionComm->create($user);
break;

default:
dol_syslog("Trigger '" . $this->name . "' for action '$action' launched by " . __FILE__ . ". id=" . $object->id);
break;
Expand Down

0 comments on commit c4bde31

Please sign in to comment.