* Copyright (C) 2004-2011 Laurent Destailleur * Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2015 Jean-François Ferry * Copyright (C) 2019 Nicolas ZABOURI * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/contrat/index.php * \ingroup contrat * \brief Home page of contract area */ require "../main.inc.php"; require_once DOL_DOCUMENT_ROOT."/contrat/class/contrat.class.php"; require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php"; $hookmanager = new HookManager($db); // Initialize technical object to manage hooks. Note that conf->hooks_modules contains array $hookmanager->initHooks(array('contractindex')); // Load translation files required by the page $langs->loadLangs(array('products', 'companies', 'contracts')); $sortfield = GETPOST('sortfield', 'alpha'); $sortorder = GETPOST('sortorder', 'alpha'); $page = GETPOST('page', 'int'); $statut = GETPOST('statut') ?GETPOST('statut') : 1; // Security check $socid = 0; $id = GETPOST('id', 'int'); if (!empty($user->socid)) $socid = $user->socid; $result = restrictedArea($user, 'contrat', $id); $staticcompany = new Societe($db); $staticcontrat = new Contrat($db); $staticcontratligne = new ContratLigne($db); $productstatic = new Product($db); /* * Action */ // None /* * View */ $now = dol_now(); llxHeader(); print load_fiche_titre($langs->trans("ContractsArea"), '', 'commercial'); //print ''; //print '
'; print '
'; if (!empty($conf->global->MAIN_SEARCH_FORM_ON_HOME_AREAS)) // This is useless due to the global search combo { // Search contract if (!empty($conf->contrat->enabled)) { print '
'; print ''; print '
'; print ''; print ''; print ''; print ''; print ''; print "
'.$langs->trans("Search").'
'.$langs->trans("Contract").':
\n"; print "
"; } } /* * Statistics */ $nb = array(); $total = 0; $totalinprocess = 0; $dataseries = array(); $vals = array(); // Search by status (except expired) $sql = "SELECT count(cd.rowid), cd.statut"; $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c"; if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql .= " WHERE cd.fk_contrat = c.rowid AND c.fk_soc = s.rowid"; $sql .= " AND (cd.statut != 4 OR (cd.statut = 4 AND (cd.date_fin_validite is null or cd.date_fin_validite >= '".$db->idate($now)."')))"; $sql .= " AND c.entity IN (".getEntity('contract', 0).")"; if ($user->socid) $sql .= ' AND c.fk_soc = '.$user->socid; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; $sql .= " GROUP BY cd.statut"; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $i = 0; while ($i < $num) { $row = $db->fetch_row($resql); if ($row) { $nb[$row[1]] = $row[0]; if ($row[1] != 5) { $vals[$row[1]] = $row[0]; $totalinprocess += $row[0]; } $total += $row[0]; } $i++; } $db->free($resql); } else { dol_print_error($db); } // Search by status (only expired) $sql = "SELECT count(cd.rowid), cd.statut"; $sql .= " FROM ".MAIN_DB_PREFIX."societe as s"; $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd, ".MAIN_DB_PREFIX."contrat as c"; if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql .= " WHERE cd.fk_contrat = c.rowid AND c.fk_soc = s.rowid"; $sql .= " AND (cd.statut = 4 AND cd.date_fin_validite < '".$db->idate($now)."')"; $sql .= " AND c.entity IN (".getEntity('contract', 0).")"; if ($user->socid) $sql .= ' AND c.fk_soc = '.$user->socid; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; $sql .= " GROUP BY cd.statut"; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); // 0 inactive, 4 active, 5 closed $i = 0; while ($i < $num) { $row = $db->fetch_row($resql); if ($row) { $nb[$row[1].true] = $row[0]; if ($row[1] != 5) { $vals[$row[1]] = $row[0]; $totalinprocess += $row[0]; } $total += $row[0]; } $i++; } $db->free($resql); } else { dol_print_error($db); } print '
'; print ''; print ''."\n"; $listofstatus = array(0, 4, 4, 5); $bool = false; foreach ($listofstatus as $status) { $dataseries[] = array($staticcontratligne->LibStatut($status, 1, ($bool ? 1 : 0)), (isset($nb[$status.$bool]) ? (int) $nb[$status.$bool] : 0)); if (empty($conf->use_javascript_ajax)) { print ''; print ''; print ''; print "\n"; } if ($status == 4 && !$bool) $bool = true; else $bool = false; } if (!empty($conf->use_javascript_ajax)) { print ''; } $listofstatus = array(0, 4, 4, 5); $bool = false; foreach ($listofstatus as $status) { if (empty($conf->use_javascript_ajax)) { print ''; print ''; print ''; if ($status == 4 && !$bool) $bool = true; else $bool = false; print "\n"; } } //if ($totalinprocess != $total) //print ''; print ''; print "
'.$langs->trans("Statistics").' - '.$langs->trans("Services").'
'.$staticcontratligne->LibStatut($status, 0, ($bool ? 1 : 0)).''.($nb[$status.$bool] ? $nb[$status.$bool] : 0).' '.$staticcontratligne->LibStatut($status, 3, ($bool ? 1 : 0)).'
'; include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php'; $dolgraph = new DolGraph(); $dolgraph->SetData($dataseries); $dolgraph->setShowLegend(1); $dolgraph->setShowPercent(1); $dolgraph->SetType(array('pie')); $dolgraph->setHeight('200'); $dolgraph->draw('idgraphstatus'); print $dolgraph->show($total ? 0 : 1); print '
'.$staticcontratligne->LibStatut($status, 0, ($bool ? 1 : 0)).''.($nb[$status.$bool] ? $nb[$status.$bool] : 0).' '.$staticcontratligne->LibStatut($status, 3, ($bool ? 1 : 0)).'
'.$langs->trans("Total").' ('.$langs->trans("ServicesRunning").')'.$totalinprocess.'
'.$langs->trans("Total").''.$total.'

"; // Draft contracts if (!empty($conf->contrat->enabled) && $user->rights->contrat->lire) { $sql = "SELECT c.rowid, c.ref,"; $sql .= " s.nom as name, s.rowid as socid"; $sql .= " FROM ".MAIN_DB_PREFIX."contrat as c, ".MAIN_DB_PREFIX."societe as s"; if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql .= " WHERE s.rowid = c.fk_soc"; $sql .= " AND c.entity IN (".getEntity('contract', 0).")"; $sql .= " AND c.statut = 0"; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; if ($socid) $sql .= " AND c.fk_soc = ".$socid; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); print '
'; print ''; print ''; print ''; if ($num) { $companystatic = new Societe($db); $i = 0; //$tot_ttc = 0; while ($i < $num) { $obj = $db->fetch_object($resql); $staticcontrat->ref = $obj->ref; $staticcontrat->id = $obj->rowid; $companystatic->id = $obj->socid; $companystatic->name = $obj->name; $companystatic->client = 1; print ''; print ''; print ''; //$tot_ttc+=$obj->total_ttc; $i++; } } else { print ''; } print "
'.$langs->trans("DraftContracts").($num ? ''.$num.'' : '').'
'; print $staticcontrat->getNomUrl(1, ''); print ''; print $companystatic->getNomUrl(1, '', 16); print '
'.$langs->trans("NoContracts").'

"; $db->free($resql); } else { dol_print_error($db); } } //print '
'; print '
'; // Last modified contracts $max = 5; $sql = 'SELECT '; $sql .= ' sum('.$db->ifsql("cd.statut=0", 1, 0).') as nb_initial,'; $sql .= ' sum('.$db->ifsql("cd.statut=4 AND (cd.date_fin_validite IS NULL OR cd.date_fin_validite >= '".$db->idate($now)."')", 1, 0).') as nb_running,'; $sql .= ' sum('.$db->ifsql("cd.statut=4 AND (cd.date_fin_validite IS NOT NULL AND cd.date_fin_validite < '".$db->idate($now)."')", 1, 0).') as nb_expired,'; $sql .= ' sum('.$db->ifsql("cd.statut=4 AND (cd.date_fin_validite IS NOT NULL AND cd.date_fin_validite < '".$db->idate($now - $conf->contrat->services->expires->warning_delay)."')", 1, 0).') as nb_late,'; $sql .= ' sum('.$db->ifsql("cd.statut=5", 1, 0).') as nb_closed,'; $sql .= " c.rowid as cid, c.ref, c.datec, c.tms, c.statut, s.nom as name, s.rowid as socid"; $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,"; if (!$user->rights->societe->client->voir && !$socid) $sql .= " ".MAIN_DB_PREFIX."societe_commerciaux as sc,"; $sql .= " ".MAIN_DB_PREFIX."contrat as c"; $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."contratdet as cd ON c.rowid = cd.fk_contrat"; $sql .= " WHERE c.fk_soc = s.rowid"; $sql .= " AND c.entity IN (".getEntity('contract', 0).")"; $sql .= " AND c.statut > 0"; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; if ($socid) $sql .= " AND s.rowid = ".$socid; $sql .= " GROUP BY c.rowid, c.ref, c.datec, c.tms, c.statut, s.nom, s.rowid"; $sql .= " ORDER BY c.tms DESC"; $sql .= " LIMIT ".$max; dol_syslog("contrat/index.php", LOG_DEBUG); $result = $db->query($sql); if ($result) { $num = $db->num_rows($result); $i = 0; print '
'; print ''; print ''; print ''; //print ''; print ''; print "\n"; while ($i < $num) { $obj = $db->fetch_object($result); print ''; print ''; print ''; print ''; //print ''; print ''; print ''; print ''; print ''; print "\n"; $i++; } $db->free($result); print "
'.$langs->trans("LastContracts", 5).''.$langs->trans("DateModification").''.$langs->trans("Status").''.$langs->trans("Services").'
'; $staticcontrat->ref = ($obj->ref ? $obj->ref : $obj->cid); $staticcontrat->id = $obj->cid; print $staticcontrat->getNomUrl(1, 16); if ($obj->nb_late) print img_warning($langs->trans("Late")); print ''; $staticcompany->id = $obj->socid; $staticcompany->name = $obj->name; print $staticcompany->getNomUrl(1, '', 20); print ''.dol_print_date($db->jdate($obj->tms), 'dayhour').''.$staticcontrat->LibStatut($obj->statut,2).''.($obj->nb_initial > 0 ? ''.$obj->nb_initial.''.$staticcontratligne->LibStatut(0, 3, -1, 'class="paddingleft"') : '').''.($obj->nb_running > 0 ? ''.$obj->nb_running.''.$staticcontratligne->LibStatut(4, 3, 0, 'class="marginleft"') : '').''.($obj->nb_expired > 0 ? ''.$obj->nb_expired.''.$staticcontratligne->LibStatut(4, 3, 1, 'class="paddingleft"') : '').''.($obj->nb_closed > 0 ? ''.$obj->nb_closed.''.$staticcontratligne->LibStatut(5, 3, -1, 'class="paddingleft"') : '').'
"; } else { dol_print_error($db); } print '
'; // Last modified services $sql = "SELECT c.ref, c.fk_soc, "; $sql .= " cd.rowid as cid, cd.statut, cd.label, cd.fk_product, cd.description as note, cd.fk_contrat, cd.date_fin_validite,"; $sql .= " s.nom as name,"; $sql .= " p.rowid as pid, p.ref as pref, p.label as plabel, p.fk_product_type as ptype, p.entity as pentity"; $sql .= " FROM (".MAIN_DB_PREFIX."contrat as c"; $sql .= ", ".MAIN_DB_PREFIX."societe as s"; if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd"; $sql .= ") LEFT JOIN ".MAIN_DB_PREFIX."product as p ON cd.fk_product = p.rowid"; $sql .= " WHERE c.entity IN (".getEntity('contract', 0).")"; $sql .= " AND cd.fk_contrat = c.rowid"; $sql .= " AND c.fk_soc = s.rowid"; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; if ($socid) $sql .= " AND s.rowid = ".$socid; $sql .= " ORDER BY cd.tms DESC"; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $i = 0; print '
'; print ''; print ''; print "\n"; while ($i < min($num, $max)) { $obj = $db->fetch_object($resql); print ''; print ''; print ''; print ''; print ''; print "\n"; $i++; } $db->free(); print "
'.$langs->trans("LastModifiedServices", $max).'
'; $staticcontrat->ref = ($obj->ref ? $obj->ref : $obj->fk_contrat); $staticcontrat->id = $obj->fk_contrat; print $staticcontrat->getNomUrl(1, 16); //if (1 == 1) print img_warning($langs->trans("Late")); print ''; if ($obj->fk_product > 0) { $productstatic->id = $obj->fk_product; $productstatic->type = $obj->ptype; $productstatic->ref = $obj->pref; $productstatic->entity = $obj->pentity; print $productstatic->getNomUrl(1, '', 20); } else { print ''.img_object($langs->trans("ShowService"), "service"); if ($obj->label) print ' '.dol_trunc($obj->label, 20).''; else print ' '.dol_trunc($obj->note, 20); } print ''; $staticcompany->id = $obj->fk_soc; $staticcompany->name = $obj->name; print $staticcompany->getNomUrl(1, '', 20); print ''; $dateend = $db->jdate($obj->date_fin_validite); print $staticcontratligne->LibStatut($obj->statut, 3, ($dateend && $dateend < $now) ? 1 : 0); print '
"; } else { dol_print_error($db); } print '
'; // Not activated services $sql = "SELECT c.ref, c.fk_soc, cd.rowid as cid, cd.statut, cd.label, cd.fk_product, cd.description as note, cd.fk_contrat,"; $sql .= " s.nom as name,"; $sql .= " p.rowid as pid, p.ref as pref, p.label as plabel, p.fk_product_type as ptype, p.entity as pentity"; $sql .= " FROM (".MAIN_DB_PREFIX."contrat as c"; $sql .= ", ".MAIN_DB_PREFIX."societe as s"; if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd"; $sql .= " ) LEFT JOIN ".MAIN_DB_PREFIX."product as p ON cd.fk_product = p.rowid"; $sql .= " WHERE c.entity IN (".getEntity('contract', 0).")"; $sql .= " AND c.statut = 1"; $sql .= " AND cd.statut = 0"; $sql .= " AND cd.fk_contrat = c.rowid"; $sql .= " AND c.fk_soc = s.rowid"; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; if ($socid) $sql .= " AND s.rowid = ".$socid; $sql .= " ORDER BY cd.tms DESC"; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $i = 0; print '
'; print ''; print ''; print "\n"; while ($i < $num) { $obj = $db->fetch_object($resql); print ''; print ''; print ''; print ''; print ''; print "\n"; $i++; } $db->free(); print "
'.$langs->trans("NotActivatedServices").' '.$num.'
'; $staticcontrat->ref = ($obj->ref ? $obj->ref : $obj->fk_contrat); $staticcontrat->id = $obj->fk_contrat; print $staticcontrat->getNomUrl(1, 16); print ''; if ($obj->fk_product > 0) { $productstatic->id = $obj->fk_product; $productstatic->type = $obj->ptype; $productstatic->ref = $obj->pref; $productstatic->entity = $obj->pentity; print $productstatic->getNomUrl(1, '', 20); } else { print ''.img_object($langs->trans("ShowService"), "service"); if ($obj->label) print ' '.dol_trunc($obj->label, 20).''; else print ' '.dol_trunc($obj->note, 20); } print ''; $staticcompany->id = $obj->fk_soc; $staticcompany->name = $obj->name; print $staticcompany->getNomUrl(1, '', 20); print ''; print $staticcontratligne->LibStatut($obj->statut, 3); print '
"; } else { dol_print_error($db); } print '
'; // Expired services $sql = "SELECT c.ref, c.fk_soc, cd.rowid as cid, cd.statut, cd.label, cd.fk_product, cd.description as note, cd.fk_contrat,"; $sql .= " s.nom as name,"; $sql .= " p.rowid as pid, p.ref as pref, p.label as plabel, p.fk_product_type as ptype, p.entity as pentity"; $sql .= " FROM (".MAIN_DB_PREFIX."contrat as c"; $sql .= ", ".MAIN_DB_PREFIX."societe as s"; if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql .= ", ".MAIN_DB_PREFIX."contratdet as cd"; $sql .= " ) LEFT JOIN ".MAIN_DB_PREFIX."product as p ON cd.fk_product = p.rowid"; $sql .= " WHERE c.entity IN (".getEntity('contract', 0).")"; $sql .= " AND c.statut = 1"; $sql .= " AND cd.statut = 4"; $sql .= " AND cd.date_fin_validite < '".$db->idate($now)."'"; $sql .= " AND cd.fk_contrat = c.rowid"; $sql .= " AND c.fk_soc = s.rowid"; if (!$user->rights->societe->client->voir && !$socid) $sql .= " AND s.rowid = sc.fk_soc AND sc.fk_user = ".$user->id; if ($socid) $sql .= " AND s.rowid = ".$socid; $sql .= " ORDER BY cd.tms DESC"; $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); $i = 0; print '
'; print ''; print ''; print "\n"; while ($i < $num) { $obj = $db->fetch_object($resql); print ''; print ''; print ''; print ''; print ''; print "\n"; $i++; } $db->free(); print "
'.$langs->trans("ListOfExpiredServices").' '.$num.'
'; $staticcontrat->ref = ($obj->ref ? $obj->ref : $obj->fk_contrat); $staticcontrat->id = $obj->fk_contrat; print $staticcontrat->getNomUrl(1, 16); print ''; if ($obj->fk_product > 0) { $productstatic->id = $obj->fk_product; $productstatic->type = $obj->ptype; $productstatic->ref = $obj->pref; $productstatic->entity = $obj->pentity; print $productstatic->getNomUrl(1, '', 20); } else { print ''.img_object($langs->trans("ShowService"), "service"); if ($obj->label) print ' '.dol_trunc($obj->label, 20).''; else print ' '.dol_trunc($obj->note, 20); } print ''; $staticcompany->id = $obj->fk_soc; $staticcompany->name = $obj->name; print $staticcompany->getNomUrl(1, '', 20); print ''; print $staticcontratligne->LibStatut($obj->statut, 3, 1); print '
"; } else { dol_print_error($db); } //print '
'; print ''; $parameters = array('user' => $user); $reshook = $hookmanager->executeHooks('dashboardContracts', $parameters, $object); // Note that $action and $object may have been modified by hook llxFooter(); $db->close();