Skip to content

Commit

Permalink
#2 [Hook] add: hook addMoreBoxStatsCustomer and addMoreRecentObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-eoxia committed Mar 8, 2023
1 parent 8d2a8b7 commit 26d2f4b
Showing 1 changed file with 122 additions and 27 deletions.
149 changes: 122 additions & 27 deletions class/actions_easycrm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,39 +62,134 @@ public function __construct(DoliDB $db)
}

/**
* Overloading the printMainArea function : replacing the parent's function with the one below
* Overloading the addMoreBoxStatsCustomer function : replacing the parent's function with the one below
*
* @param array $parameters Hook metadatas (context, etc...)
* @return int 0 < on error, 0 on success, 1 to replace standard code
* @param array $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @return int 0 < on error, 0 on success, 1 to replace standard code
* @throws Exception
*/
public function addMoreBoxStatsCustomer(array $parameters, $object, $action): int
public function addMoreBoxStatsCustomer(array $parameters, CommonObject $object, string $action): int
{
global $user;

global $conf, $langs, $user;
// Do something only for the current context
if ($parameters['currentcontext'] == 'thirdpartycomm') {
if (isModEnabled('project') && $user->rights->project->lire) {
$project = new Project($this->db);
$project->fetchAll('', '', 0, 0, []);

// Box factures
$tmp = $object->getOutstandingBills('customer', 0);
$outstandingOpened = $tmp['opened'];
$outstandingTotal = $tmp['total_ht'];
$outstandingTotalIncTax = $tmp['total_ttc'];

$text = $langs->trans("OverAllInvoices");
$link = DOL_URL_ROOT.'/compta/facture/list.php?socid='.$object->id;
$icon = 'bill';
if ($link) {
$boxstat .= '<a href="'.$link.'" class="boxstatsindicator thumbstat nobold nounderline">';
if (isModEnabled('project') && $user->hasRight('projet', 'lire') && isModEnabled('saturne')) {
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
require_once __DIR__ . '/../../saturne/lib/object.lib.php';

$projects = saturne_fetch_all_object_type('Project', '', '', 0, 0, ['customsql' => 't.fk_soc = ' . $object->id]);
$projectData = [];
if (is_array($projects) && !empty($projects)) {
foreach ($projects as $project) {
$projectData['total_opp_amount'] += $project->opp_amount;
$projectData['total_opp_weighted_amount'] += $project->opp_amount * $project->opp_percent / 100;
}
}
$boxstat .= '<div class="boxstats" title="'.dol_escape_htmltag($text).'">';
$boxstat .= '<span class="boxstatstext">'.img_object("", $icon).' <span>'.$text.'</span></span><br>';
$boxstat .= '<span class="boxstatsindicator">'.price($outstandingTotal, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
$boxstat .= '</div>';
if ($link) {
$boxstat .= '</a>';

// Project box opportunity amount
$boxTitle = $langs->transnoentities('OpportunityAmount');
$link = DOL_URL_ROOT . '/projet/list.php?socid=' . $object->id;
$boxStat = '<a href="' . $link . '" class="boxstatsindicator thumbstat nobold nounderline">';
$boxStat .= '<div class="boxstats" title="' . dol_escape_htmltag($boxTitle) . '">';
$boxStat .= '<span class="boxstatstext">' . img_object('', 'project') . ' <span>' . $boxTitle . '</span></span><br>';
$boxStat .= '<span class="boxstatsindicator">' . price($projectData['total_opp_amount'], 1, $langs, 1, 0, -1, $conf->currency) . '</span>';
$boxStat .= '</div>';
$boxStat .= '</a>';

// Project box opportunity weighted amount
$boxTitle = $langs->transnoentities('OpportunityWeightedAmount');
$boxStat .= '<a href="' . $link . '" class="boxstatsindicator thumbstat nobold nounderline">';
$boxStat .= '<div class="boxstats" title="' . dol_escape_htmltag($boxTitle) . '">';
$boxStat .= '<span class="boxstatstext">' . img_object('', 'project') . ' <span>' . $boxTitle . '</span></span><br>';
$boxStat .= '<span class="boxstatsindicator">' . price($projectData['total_opp_weighted_amount'], 1, $langs, 1, 0, -1, $conf->currency) . '</span>';
$boxStat .= '</div>';
$boxStat .= '</a>';

$this->resprints = $boxStat;
}
}

return 0; // or return 1 to replace standard code
}

/**
* Overloading the addMoreRecentObjects function : replacing the parent's function with the one below
*
* @param array $parameters Hook metadatas (context, etc...)
* @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
* @param string $action Current action (if set). Generally create or edit or null
* @return int 0 < on error, 0 on success, 1 to replace standard code
* @throws Exception
*/
public function addMoreRecentObjects(array $parameters, CommonObject $object, string $action): int
{
global $conf, $db, $langs, $user;

// Do something only for the current context
if ($parameters['currentcontext'] == 'thirdpartycomm') {
if (isModEnabled('project') && $user->hasRight('projet', 'lire') && isModEnabled('saturne')) {
require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
require_once __DIR__ . '/../../saturne/lib/object.lib.php';

$projects = saturne_fetch_all_object_type('Project', 'DESC', 'datec', 0, 0, ['customsql' => 't.fk_soc = ' . $object->id]);
if (is_array($projects) && !empty($projects)) {
$nbProjects = count($projects);
$maxList = $conf->global->MAIN_SIZE_SHORTLIST_LIMIT;

$out = '<div class="div-table-responsive-no-min">';
$out .= '<table class="noborder centpercent lastrecordtable">';

$out .= '<tr class="liste_titre">';
$out .= '<td colspan="4"><table class="nobordernopadding centpercent"><tr>';
$out .= '<td>' . $langs->trans('LastProjects', ($nbProjects <= $maxList ? '' : $maxList)) . '</td>';
$out .= '<td class="right"><a class="notasortlink" href="' . DOL_URL_ROOT . '/projet/list.php?socid=' . $object->id . '">' . $langs->trans('AllProjects') . '<span class="badge marginleftonlyshort">' . $nbProjects .'</span></a></td>';
$out .= '<td class="right" style="width: 20px;"><a href="' . DOL_URL_ROOT . '/projet/stats/index.php?socid=' . $object->id . '">' . img_picto($langs->trans('Statistics'), 'stats') . '</a></td>';
$out .= '</tr></table></td>';
$out .= '</tr>';

foreach ($projects as $project) {
$out .= '<tr class="oddeven">';
$out .= '<td class="nowraponall">';
$out .= $project->getNomUrl(1);
// Preview
$filedir = $conf->projet->multidir_output[$project->entity] . '/' . dol_sanitizeFileName($project->ref);
$fileList = null;
if (!empty($filedir)) {
$fileList = dol_dir_list($filedir, 'files', 0, '', '(\.meta|_preview.*.*\.png)$', 'date', SORT_DESC);
}
if (is_array($fileList) && !empty($fileList)) {
// Defined relative dir to DOL_DATA_ROOT
$relativedir = '';
if ($filedir) {
$relativedir = preg_replace('/^' . preg_quote(DOL_DATA_ROOT, '/') . '/', '', $filedir);
$relativedir = preg_replace('/^\//', '', $relativedir);
}
// Get list of files stored into database for same relative directory
if ($relativedir) {
completeFileArrayWithDatabaseInfo($fileList, $relativedir);
if (!empty($sortfield) && !empty($sortorder)) { // If $sortfield is for example 'position_name', we will sort on the property 'position_name' (that is concat of position+name)
$fileList = dol_sort_array($fileList, $sortfield, $sortorder);
}
}
require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';

$formfile = new FormFile($db);

$relativepath = dol_sanitizeFileName($project->ref) . '/' . dol_sanitizeFileName($project->ref) . '.pdf';
$out .= $formfile->showPreview($fileList, $project->element, $relativepath);
}
$out .= '</td><td class="right" style="width: 80px;">' . dol_print_date($project->datec, 'day') . '</td>';
$out .= '<td class="right" style="min-width: 60px;">' . price($project->budget_amount) . '</td>';
$out .= '<td class="right" style="min-width: 60px;" class="nowrap">' . $project->LibStatut($project->fk_statut, 5) . '</td></tr>';
}

$out .= '</table>';
$out .= '</div>';

$this->resprints = $out;
}
}
}
Expand Down

0 comments on commit 26d2f4b

Please sign in to comment.