Advertisement
Guest User

Untitled

a guest
May 27th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.88 KB | None | 0 0
  1. package Marica::Base::Agenda::agenda_general ;
  2. use lib qw(/home/lib);
  3. use strict ;
  4.  
  5. use warnings ;
  6.  
  7. use Apache2::Const -compile => qw( OK REDIRECT ) ;
  8.  
  9. use Marica::dictionary ;
  10.  
  11. sub handler {
  12.  
  13. my $r = shift ;
  14.  
  15. my $req = Apache2::Request->new($r) ;
  16.  
  17. my $id_client = $r->pnotes('session')->{id_client} ;
  18.  
  19. my $content = '<h1>' . _( 'Agenda général', $r ) . '</h1>' ;
  20.  
  21. #récupérer les arguments
  22. my %args ;
  23.  
  24. my @args ;
  25.  
  26. if ($req->param) { #si présence de paramètres
  27.  
  28. @args = $req->param ;
  29.  
  30. for (@args) {
  31.  
  32. $args{$_} = $req->param($_) ;
  33.  
  34. }
  35.  
  36. }
  37.  
  38. my $args_hash = \%args;
  39.  
  40. my $dbh = $r->pnotes('dbh') ;
  41.  
  42. #affichage des deux semaines à partir de $start_date et des éléments déjà enregistrés
  43. $content .= display_agenda( $r, $dbh, $id_client ) ;
  44.  
  45. $r->no_cache(1) ;
  46.  
  47. $r->content_type('text/html; charset=utf-8') ;
  48.  
  49. print $content ;
  50.  
  51. return Apache2::Const::OK ;
  52.  
  53. }
  54.  
  55. 1 ;
  56.  
  57.  
  58. sub display_agenda {
  59.  
  60. my ($r, $dbh, $id_client) = @_ ;
  61.  
  62. my $calendar = '<table>';
  63.  
  64. my ($days_of_week_row, $dates_row1, $dates_row2) ;
  65.  
  66. my ($background_color, $border_color) ;
  67.  
  68. my ( $date_displayed, $mnemo ) ;
  69.  
  70. my $content ;
  71.  
  72. #recherche des éléments déjà enregistrés;
  73. #pour les non-admistrateurs, limiter l'affichage aux dossiers autorisés
  74. #pour les administrateurs : ajouter les alertes contrats
  75. my $sql = ( $r->pnotes('session')->{administrateur} == 0 ) ?
  76. 'SELECT t1.id_agenda, t1.id_item, t1.date_expected, t1.date_expected - CURRENT_DATE as diff_date, t2.libelle, coalesce(t1.description, \'\') as description, t1.id_contentieux, t3.ref_dossier, coalesce(t3.notes_contentieux, \'\') as notes_contentieux FROM ( tblcontentieux as t3 INNER JOIN ( marica_dossier_group t5 INNER JOIN marica_user_group t4 using (id_group) ) ON t3.id_contentieux = t5.id_contentieux ) INNER JOIN ( tblcontentieux_agenda as t1 INNER JOIN tblcontentieux_expected as t2 using (id_item) ) on t1.id_contentieux = t3.id_contentieux WHERE t3.id_client = ? AND t4.id_user = \'' . $r->pnotes('session')->{username} . '\'
  77. UNION SELECT 0 as id_agenda, id_contrat_type as id_item, ( t1.date_fin - ( t1.duree_preavis * interval \'1 month\' ) )::date as date_expected, t1.date_fin - CURRENT_DATE as diff_date, \'' . _( 'Alerte contrat', $r ) . '\' as "libelle", t2.libelle || \' - \' || t1.libelle || \' - \' || t1.police as description, t1.id_contrat_autre as id_contentieux, coalesce(t1.police,\'' . _( 'Contrats', $r ) . '\') as ref_dossier, \'\' as notes_contentieux
  78. FROM tblcontrat_autre t1 INNER JOIN tblcontrat_type t2 using(id_contrat_type)
  79. WHERE t1.tacite_reconduction = 0 and t2.id_client = ' . $r->pnotes('session')->{id_client} . '
  80. AND ( ( CURRENT_DATE + interval \'1 month\' ) - ( t1.date_fin - ( duree_preavis * interval \'1 month\' ) ) ) between interval \'1 day\' and interval \'1 month\'
  81. ORDER BY 3, 4' :
  82. 'SELECT t1.id_agenda, t1.id_item, t1.date_expected, t1.date_expected - CURRENT_DATE as diff_date, t2.libelle, coalesce(t1.description, \'\') as description, t1.id_contentieux, t3.ref_dossier, coalesce(t3.notes_contentieux, \'\') as notes_contentieux FROM tblcontentieux as t3 INNER JOIN ( tblcontentieux_agenda as t1 INNER JOIN tblcontentieux_expected as t2 using (id_item) ) using (id_contentieux) WHERE t3.id_client = ?
  83. UNION SELECT 0 as id_agenda, id_contrat_type as id_item, ( t1.date_fin - ( t1.duree_preavis * interval \'1 month\' ) )::date as date_expected, t1.date_fin - CURRENT_DATE as diff_date, \'' . _( 'Alerte contrat', $r ) . '\' as "libelle", t2.libelle || \' - \' || t1.libelle || \' - \' || t1.police as description, t1.id_contrat_autre as id_contentieux, coalesce(t1.police,\'' . _( 'Contrats', $r ) . '\') as ref_dossier, \'\' as notes_contentieux
  84. FROM tblcontrat_autre t1 INNER JOIN tblcontrat_type t2 using(id_contrat_type)
  85. WHERE t1.tacite_reconduction = 0 and t2.id_client = ' . $r->pnotes('session')->{id_client} . '
  86. AND ( ( CURRENT_DATE + interval \'1 month\' ) - ( t1.date_fin - ( duree_preavis * interval \'1 month\' ) ) ) between interval \'1 day\' and interval \'1 month\'
  87. ORDER BY 3, 4';
  88.  
  89. my $sth = $dbh->prepare($sql) ;
  90.  
  91. $sth->execute($id_client) ;
  92.  
  93. my @agenda = @{ $sth->fetchall_arrayref( { } ) } ;
  94.  
  95. #hash par date
  96. my %agenda ;
  97.  
  98. #liste des éléments enregistrés
  99. my $items_in_agenda = '
  100. <li style="list-style: none; width: 90em;"><div class=container><div class=spacer></div>
  101. <span class=headerspan style="margin: .2em;">
  102. <span class=headerspan style="width: 10em">' . $r->pnotes('session')->{field_labels}->{tblcontentieux}->{ref_dossier} . '</span>
  103. <span class=headerspan style="width: 6em">' . _( 'Date prévue', $r ) . '</span>
  104. <span class=headerspan style="width: 20em">' . _('Élément attendu', $r ) . '</span>
  105. <span class=headerspan style="width: 50em">' . _ ( 'Description', $r ) . '</span>
  106. </span>
  107. <div class=spacer></div></div></li>
  108. ' ;
  109.  
  110. for ( @agenda ) { #si présence de données
  111.  
  112. #
  113. #signaler les éléments en retard en rouge, ceux dans les 14 jours en bleu, les autres normal
  114. #
  115.  
  116. #créer une clé dans %agenda
  117. $agenda{ $_->{date_expected} }++ ;
  118.  
  119. #toujours signaler en rouge les éléments en retard (diff_date < 0)
  120. #les éléments situés dans le tableau des 14 prochains jours sont affichés sur fond bleu pâle
  121. #ceux situés au-delà sur fond jaune
  122. if ( $_->{diff_date} < 0 ) {
  123.  
  124. $background_color = '#f63' ;
  125.  
  126. $border_color = 'black' ;
  127.  
  128. } elsif ( $_->{diff_date} <= 13 ) {
  129.  
  130. $background_color = '#eee' ;
  131.  
  132. $border_color = 'blue' ;
  133.  
  134. } else {
  135.  
  136. $background_color = '#ffa' ;
  137.  
  138. $border_color = '#000' ;
  139.  
  140. } # if ( $_->{diff_date} < 0 )
  141.  
  142. #couleur spéciale pour les alertes contrats
  143. if ( $_->{id_agenda} == 0 ) {
  144.  
  145. $background_color = '#fbb' ;
  146.  
  147. $border_color = '#000' ;
  148.  
  149. }
  150.  
  151. #style de la case dossier
  152. my $style = ' style=" white-space: nowrap; background-color: ' . $background_color . '; border: thin solid ' . $border_color . ';"' ;
  153.  
  154. my $dossier = $_->{ref_dossier} || '' ;
  155.  
  156. #pour les utilisateurs du compte demo, l'agenda renvoie sur l'onglet etat
  157. #pour les autres, l'agenda renvoie directement sur l'onglet agenda
  158. #on crée cette exception pour le compte démo parce que les utilisateurs
  159. #ne voient pas le passage au dossier sinon
  160. my $id_onglet = ( $r->pnotes('session')->{username} =~ /^demo_/ ) ? '' : '&amp;id_onglet=5' ;
  161.  
  162. #lien de modification de l'item; si id_agenda == 0, c'est une alerte contrat, sinon c'est un dossier contentieux
  163. my $modify_item_in_agenda_href = ( $_->{id_agenda} == 0 ) ?
  164. '/base/contrats_autres?id_contrat_autre=' . $_->{id_contentieux} . '&amp;id_contrat_type=' . $_->{id_item} :
  165. '/base/contentieux_detail?id_contentieux=' . $_->{id_contentieux} . $id_onglet . '&amp;id_agenda=' . $_->{id_agenda} ;
  166.  
  167. my $post_it = $_->{notes_contentieux} ;
  168.  
  169. #ajouter l'élément à la liste
  170. $items_in_agenda .= '
  171. <li style="list-style: none; width: 90em;"><div class=container ' . $style . '><div class=spacer></div>
  172. <a class=listitem href="' . $modify_item_in_agenda_href . '" title="' . $post_it . '">
  173. <span class=blockspan style="margin: .2em;">
  174. <span class=blockspan style="width: 10em">' . $_->{ref_dossier} . '</span>
  175. <span class=blockspan style="width: 6em">' . $_->{date_expected} . '</span>
  176. <span class=blockspan style="width: 20em">' . $_->{libelle} . '</span>
  177. <span class=blockspan style="width: 50em">' . $_->{description} . '</span>
  178. </span></a>
  179. <div class=spacer></div></div></li>
  180. ' ;
  181.  
  182. } # for ( @agenda )
  183.  
  184. $items_in_agenda = '<ul style="margin: .1em; padding: .1em;">' . $items_in_agenda . '</ul>' ;
  185.  
  186. #recherche des noms des jours de la semaine localisés, avec formatage de la date pour affichage dans les cases de l'agenda
  187. $sql = q { select CURRENT_DATE + i as date_displayed, extract(day from CURRENT_DATE + i) || ' ' || to_char(CURRENT_DATE + i, 'TMMon') as day_of_month, to_char(current_date + i , 'TMDay') as day_of_week from generate_series(0,13) i ORDER by i } ;
  188.  
  189. my $days_of_week = $dbh->selectall_arrayref( $sql ) ;
  190.  
  191. my @days_of_week = @{$days_of_week} ;
  192.  
  193. #construction de la ligne d'en-tête et de la première ligne de l'agenda
  194. for ( 0 .. 6 ) {
  195.  
  196. $date_displayed = $days_of_week->[$_]->[0] ;
  197.  
  198. $mnemo = $days_of_week->[$_]->[1] ;
  199.  
  200. #en-tête de la table : afficher les noms de 7 jours à partir d'aujourd'hui
  201. $days_of_week_row .= '<th>' . $days_of_week->[$_]->[2] . '</th>' ;
  202.  
  203. $background_color = ( defined $agenda{$date_displayed} ) ? ' background-color: #eee;' : '' ;
  204.  
  205. $dates_row1 .= '<td style="width: 100px; border: thin groove blue;' . $background_color .'"><sup>' . $mnemo . '</sup><div style="text-align: center; font-size: xx-large;">' . ( '*' x ( $agenda{$date_displayed} || 0 ) || '&nbsp' ) . '</div></td>' ;
  206.  
  207. } # for ( 0 .. 6 )
  208.  
  209. #construction de la deuxième ligne
  210. for ( 7 .. 13 ) {
  211.  
  212. $date_displayed = $days_of_week->[$_]->[0] ;
  213.  
  214. $mnemo = $days_of_week->[$_]->[1] ;
  215.  
  216. $background_color = ( $agenda{ $date_displayed } ) ? ' background-color: #eee;' : '' ;
  217.  
  218. $dates_row2 .= '<td style="width: 100px; border: thin groove blue;' . $background_color .'"><sup>' . $mnemo . '</sup><div style="text-align: center; font-size: xx-large;">' . ( '*' x ( $agenda{$date_displayed} || 0 ) || '&nbsp' ) . '</div></td>' ;
  219.  
  220. } # for ( 7 .. 13 )
  221.  
  222. $calendar .= '<tr>' . $days_of_week_row . '</tr>' ;
  223.  
  224. $calendar .= '<tr>' . $dates_row1 . '</tr>' ;
  225.  
  226. $calendar .= '<tr>' . $dates_row2 . '</tr>' ;
  227.  
  228. $calendar .= '</table>' ;
  229.  
  230. #afficher le calendrier au-dessus de la liste des éléments déjà enregistréss
  231. $content .= '<h2>' . _( 'Éléments enregistrés pour les deux semaines à venir', $r ) . '</h2><hr>' ;
  232.  
  233. $content .= $calendar ;
  234.  
  235. $content .= '<hr>' ;
  236.  
  237. $content .= '<table><tr><td><h3>' . _( 'Tous les éléments enregistrés', $r ) . '</h3></td></tr></table>' ;
  238.  
  239. if ( @agenda ) { #il existe des éléments à l'agenda
  240.  
  241. $content .= $items_in_agenda ;
  242.  
  243. } else { # !(@agenda)
  244.  
  245. $content .= '<h3 class=warning style="margin: 2.5em; padding: 2.5em;">' . _( 'Aucun élément enregistré', $r ) . '</h3>' ;
  246.  
  247. } # if ( @agenda )
  248.  
  249. my $legend = '
  250. <h3>' . _( 'Légende', $r ) . '</h3>
  251. <table>
  252. <tr><td style=""><span class=blockspan style="width: 100%; background-color: #f63; color: blue;">' . _( 'En retard', $r ) . '</span></td></tr>
  253. <tr><td style=""><span class=blockspan style="width: 100%; background-color: #eee; color: blue;">' . _( 'Attendu dans les deux prochaines semaines', $r ) . '</span></td></tr>
  254. <tr><td style=""><span class=blockspan style="width: 100%; background-color: #ffa; color: blue;">' . _( 'Attendu plus tard', $r ) . '</span></td></tr>
  255. <tr><td style=""><span class=blockspan style="width: 100%; background-color: #faa; color: blue;">' . _( 'Alerte contrat', $r ) . '</span></td></tr>
  256. </table>
  257. ';
  258.  
  259. $content .= $legend ;
  260.  
  261. return $content ;
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement