Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use IO::Scalar;
  4. use DBI;
  5. use CGI;
  6. use Spreadsheet::WriteExcel;
  7.  
  8. use constant C_HEADING => 0;
  9. use constant C_WIDTH => 1;
  10. my @columns = (
  11. ['USUARIO', 32],
  12. ['EN_PROGRESO', 10],
  13. ['DEVOLUCION', 10],
  14. ['RESUELTO', 10],
  15. ['CLIENTE_NOTIFICADO', 10],
  16. ['COMPLETADO',10],
  17. ['TOTAL', 10],
  18. );
  19.  
  20.  
  21. my $dbh = DBI->connect("dbi:mysql:dbname=otrs;host=localhost", "root", "12345") ||
  22. die "Can't connect to database.\n";
  23.  
  24. #$cgi = new CGI;
  25. #print $cgi->header("application/excel");
  26.  
  27.  
  28.  
  29. my $name = "ticketmonitor_rpt.xls";
  30. print "Content-disposition: attachment; filename=$name\n";
  31. print "Content-type: application/vnd.ms-excel\n\n";
  32.  
  33.  
  34.  
  35.  
  36. #====================================================
  37. #====================================================
  38. #my $xls_str;
  39. #tie *XLS, 'IO::Scalar', \$xls_str;
  40.  
  41. #my $workbook = Spreadsheet::WriteExcel->new(\*XLS);
  42. my $workbook = Spreadsheet::WriteExcel->new("-");
  43. my $worksheet = $workbook->add_worksheet("Datos");
  44.  
  45.  
  46.  
  47. my $default_format = $workbook->add_format(num_format => '@'); $default_format->set_font('Verdana'); $default_format->set_border(1);
  48. my $bold_format = $workbook->add_format(); $bold_format->set_font('Verdana'); $bold_format->set_bold(); $bold_format->set_border(1);
  49.  
  50. $worksheet->write(0,$_,$columns[$_]->[C_HEADING], $bold_format) for (0..$#columns);
  51. $worksheet->set_column($_, $_, $columns[$_]->[C_WIDTH]) for (0..$#columns);
  52. $worksheet->autofilter('A1:M1');
  53.  
  54.  
  55. #$worksheet->write(13, 3, "Hi Excel!");
  56.  
  57.  
  58. my $sth = $dbh->prepare("SELECT CONCAT(u.FIRST_NAME, ' ',u.LAST_NAME) AS Usuario , SUM(En_Progreso) AS 'En_Progreso' ,SUM(Devolucion) AS 'Devolucion', SUM(Resuelto) AS 'Resuelto'
  59. ,SUM(Cliente_notificado) AS 'Cliente_notificado' , SUM(Completado) AS 'Completado', SUM(TOTAL) AS 'TOTAL'
  60. FROM (
  61. SELECT t.responsible_user_id AS Usuario , COUNT(TICKET_STATE_ID ) AS 'En_Progreso' ,0 AS 'Devolucion', 0 AS 'Resuelto'
  62. ,0 AS 'Cliente_notificado',0 AS 'Completado', 0 AS 'TOTAL'
  63. FROM ticket t
  64. WHERE t.TICKET_STATE_ID = 17 AND customer_id = 'crediq' GROUP BY t.responsible_user_id
  65. UNION
  66. SELECT t.responsible_user_id AS Usuario , 0 AS 'En_Progreso' ,COUNT(TICKET_STATE_ID ) AS 'Devolucion', 0 AS 'Resuelto'
  67. ,0 AS 'Cliente_notificado',0 AS 'Completado', 0 AS 'TOTAL'
  68. FROM ticket t
  69. WHERE t.TICKET_STATE_ID = 18 AND customer_id = 'crediq' GROUP BY t.responsible_user_id
  70. UNION
  71. SELECT t.responsible_user_id AS Usuario , 0 AS 'En_Progreso' ,0 AS 'Devolucion', COUNT(TICKET_STATE_ID ) AS 'Resuelto'
  72. ,0 AS 'Cliente_notificado',0 AS 'Completado', 0 AS 'TOTAL'
  73. FROM ticket t
  74. WHERE t.TICKET_STATE_ID = 16 AND customer_id = 'crediq' GROUP BY t.responsible_user_id
  75. UNION
  76. SELECT t.responsible_user_id AS Usuario , 0 AS 'En_Progreso' , 0 AS 'Devolucion', 0 AS 'Resuelto'
  77. ,COUNT(TICKET_STATE_ID ) AS 'Cliente_notificado',0 AS 'Completado', 0 AS 'TOTAL'
  78. FROM ticket t
  79. WHERE t.TICKET_STATE_ID = 20 AND customer_id = 'crediq' GROUP BY t.responsible_user_id
  80. UNION
  81. SELECT t.responsible_user_id AS Usuario , 0 AS 'En Progreso' , 0 AS 'Devolucion', 0 AS 'Resuelto'
  82. ,0 AS 'Cliente_notificado',COUNT(TICKET_STATE_ID ) AS 'Completado', 0 AS 'TOTAL'
  83. FROM ticket t
  84. WHERE t.TICKET_STATE_ID = 19 AND customer_id = 'crediq' GROUP BY t.responsible_user_id
  85. UNION
  86. SELECT t.responsible_user_id AS Usuario , 0 AS 'En_Progreso' , 0 AS 'Devolucion', 0 AS 'Resuelto'
  87. ,0 AS 'Cliente_notificado',0 AS 'Completado', COUNT(TICKET_STATE_ID ) AS 'TOTAL'
  88. FROM ticket t
  89. WHERE customer_id = 'crediq' AND t.TICKET_STATE_ID IN (17,18,16,19,20) GROUP BY t.responsible_user_id
  90. )z JOIN users u on u.ID = z.Usuario GROUP BY z.Usuario order by z.Usuario ");
  91. $sth->execute();
  92. my $i = 1;
  93. my $row;
  94. while ( $row = $sth->fetchrow_arrayref ) {
  95. $worksheet->write_string($i,$_,$row->[$_], $default_format) for (0..$#$row);
  96. $i++;
  97. }
  98. $worksheet->activate();
  99. $sth->finish();
  100. $dbh->disconnect();
  101.  
  102. $workbook->close();
  103. #====================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement