Commit 4bc366990e1a0981dab8db03cdb76c44e53e3a1a
1 parent
945561a5
Exists in
master
and in
2 other branches
Correções no relatório de impressões
Showing
4 changed files
with
163 additions
and
13 deletions
Show diff stats
Controller/PrinterController.php
| ... | ... | @@ -13,9 +13,19 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; |
| 13 | 13 | |
| 14 | 14 | use Swpb\Bundle\CocarBundle\Entity\Printer; |
| 15 | 15 | use Swpb\Bundle\CocarBundle\Entity\PrinterCounter; |
| 16 | +use Swpb\Bundle\CocarBundle\Entity\PrinterCounterRepository; | |
| 16 | 17 | use Swpb\Bundle\CocarBundle\Form\PrinterType; |
| 17 | 18 | |
| 19 | +use Ddeboer\DataImport\Workflow; | |
| 20 | +use Ddeboer\DataImport\Reader\ArrayReader; | |
| 21 | +use Ddeboer\DataImport\Writer\CsvWriter; | |
| 22 | +use Ddeboer\DataImport\ValueConverter\CallbackValueConverter; | |
| 23 | + | |
| 18 | 24 | use Doctrine\ORM\EntityManager; |
| 25 | +use Symfony\Component\HttpFoundation\StreamedResponse; | |
| 26 | + | |
| 27 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; | |
| 28 | + | |
| 19 | 29 | |
| 20 | 30 | /** |
| 21 | 31 | * Printer controller. |
| ... | ... | @@ -57,6 +67,8 @@ class PrinterController extends Controller |
| 57 | 67 | $start = isset($start) ? $start : (time() - ((60*60*24)*30)); |
| 58 | 68 | $end = isset($end) ? $end : time(); |
| 59 | 69 | |
| 70 | + /* | |
| 71 | + | |
| 60 | 72 | $printers = $em->getRepository('CocarBundle:Printer')->findAll(); |
| 61 | 73 | |
| 62 | 74 | $printerCounter = array(); |
| ... | ... | @@ -90,6 +102,10 @@ class PrinterController extends Controller |
| 90 | 102 | } |
| 91 | 103 | |
| 92 | 104 | $displayAll = ($request->query->get('all')) ? $request->query->get('all') : 0; |
| 105 | + */ | |
| 106 | + $displayAll = true; | |
| 107 | + | |
| 108 | + $printers = $em->getRepository('CocarBundle:PrinterCounter')->relatorioGeral($start, $end); | |
| 93 | 109 | |
| 94 | 110 | if(!$displayAll) |
| 95 | 111 | { |
| ... | ... | @@ -98,13 +114,73 @@ class PrinterController extends Controller |
| 98 | 114 | } |
| 99 | 115 | return array( |
| 100 | 116 | "printer" => $printers, |
| 101 | - "printerCounter" => $pCounter, | |
| 117 | + //"printerCounter" => $pCounter, | |
| 102 | 118 | "form" => $this->createCalendarForm(0, new \DateTime(date("Y-m-d", $start)), new \DateTime(date("Y-m-d", $end)))->createView(), |
| 103 | 119 | "start" => $start, |
| 104 | 120 | "end" => $end, |
| 105 | 121 | "displayAll" => $displayAll |
| 106 | 122 | ); |
| 107 | 123 | } |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Generate a CSV file | |
| 127 | + * | |
| 128 | + * @Route("/csv", name="printer_csv") | |
| 129 | + * | |
| 130 | + */ | |
| 131 | + public function csvAction(Request $request) | |
| 132 | + { | |
| 133 | + $em = $this->getDoctrine()->getManager(); | |
| 134 | + | |
| 135 | + $form = $request->query->get('form'); | |
| 136 | + | |
| 137 | + if($form) | |
| 138 | + { | |
| 139 | + $start = new \DateTime($form['startDate']); | |
| 140 | + $start = $start->format('U'); | |
| 141 | + | |
| 142 | + $end = new \DateTime($form['endDate']); | |
| 143 | + $end = $end->format('U'); | |
| 144 | + } | |
| 145 | + | |
| 146 | + $start = isset($start) ? $start : (time() - ((60*60*24)*30)); | |
| 147 | + $end = isset($end) ? $end : time(); | |
| 148 | + | |
| 149 | + $printers = $em->getRepository('CocarBundle:PrinterCounter')->relatorioCsvGeral($start, $end); | |
| 150 | + | |
| 151 | + // Gera CSV | |
| 152 | + $reader = new ArrayReader($printers); | |
| 153 | + | |
| 154 | + // Create the workflow from the reader | |
| 155 | + $workflow = new Workflow($reader); | |
| 156 | + | |
| 157 | + | |
| 158 | + // As you can see, the first names are not capitalized correctly. Let's fix | |
| 159 | + // that with a value converter: | |
| 160 | + $converter = new CallbackValueConverter(function ($input) { | |
| 161 | + return date('d/m/Y', $input); | |
| 162 | + }); | |
| 163 | + $workflow->addValueConverter('endDate', $converter); | |
| 164 | + $workflow->addValueConverter('startDate', $converter); | |
| 165 | + | |
| 166 | + // Add the writer to the workflow | |
| 167 | + $tmpfile = tempnam(sys_get_temp_dir(), 'impressoras'); | |
| 168 | + $file = new \SplFileObject($tmpfile, 'w'); | |
| 169 | + $writer = new CsvWriter($file); | |
| 170 | + $workflow->addWriter($writer); | |
| 171 | + | |
| 172 | + // Process the workflow | |
| 173 | + $workflow->process(); | |
| 174 | + | |
| 175 | + // Retorna o arquivo | |
| 176 | + $response = new BinaryFileResponse($tmpfile); | |
| 177 | + $response->headers->set('Content-Type', 'text/csv'); | |
| 178 | + $response->headers->set('Content-Disposition', 'attachment; filename="impressoras.csv"'); | |
| 179 | + $response->headers->set('Content-Transfer-Encoding', 'binary'); | |
| 180 | + | |
| 181 | + return $response; | |
| 182 | + } | |
| 183 | + | |
| 108 | 184 | /** |
| 109 | 185 | * Creates a new Printer entity. |
| 110 | 186 | * | ... | ... |
Entity/PrinterCounterRepository.php
| ... | ... | @@ -12,4 +12,77 @@ use Doctrine\ORM\EntityRepository; |
| 12 | 12 | */ |
| 13 | 13 | class PrinterCounterRepository extends EntityRepository |
| 14 | 14 | { |
| 15 | + /** | |
| 16 | + * Classe do relatório geral de impressão | |
| 17 | + * | |
| 18 | + * @param $start | |
| 19 | + * @param $end | |
| 20 | + * @return array | |
| 21 | + */ | |
| 22 | + public function relatorioGeral($start, $end) { | |
| 23 | + | |
| 24 | + | |
| 25 | + $_dql = "SELECT printer.id, | |
| 26 | + max(pc1.prints) as printsEnd, | |
| 27 | + pc1.blackInk, | |
| 28 | + pc1.coloredInk, | |
| 29 | + max(pc1.date) as endDate, | |
| 30 | + min(pc2.prints) as printsStart, | |
| 31 | + min(pc2.date) as startDate, | |
| 32 | + printer.name, | |
| 33 | + printer.description, | |
| 34 | + printer.host | |
| 35 | + FROM CocarBundle:PrinterCounter pc1 | |
| 36 | + INNER JOIN CocarBundle:Printer printer WITH pc1.printer = printer.id | |
| 37 | + INNER JOIN CocarBundle:PrinterCounter pc2 WITH (pc1.printer = pc2.printer AND pc2.date >= :start) | |
| 38 | + WHERE pc1.date <= :end | |
| 39 | + GROUP BY printer.id, | |
| 40 | + pc1.blackInk, | |
| 41 | + pc1.coloredInk, | |
| 42 | + printer.name, | |
| 43 | + printer.description, | |
| 44 | + printer.host | |
| 45 | + ORDER BY printer.id ASC"; | |
| 46 | + | |
| 47 | + return $this->getEntityManager()->createQuery( $_dql ) | |
| 48 | + ->setParameter('start', $start) | |
| 49 | + ->setParameter('end', $end) | |
| 50 | + ->getArrayResult(); | |
| 51 | + | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Classe do relatório geral de impressão no formato CSV | |
| 56 | + * | |
| 57 | + * @param $start | |
| 58 | + * @param $end | |
| 59 | + * @return array | |
| 60 | + */ | |
| 61 | + public function relatorioCsvGeral($start, $end) { | |
| 62 | + | |
| 63 | + | |
| 64 | + $_dql = "SELECT printer.id, | |
| 65 | + max(pc1.prints) as printsEnd, | |
| 66 | + max(pc1.date) as endDate, | |
| 67 | + min(pc2.prints) as printsStart, | |
| 68 | + min(pc2.date) as startDate, | |
| 69 | + printer.name, | |
| 70 | + printer.host, | |
| 71 | + (max(pc1.prints) - min(pc2.prints)) as totalPrints | |
| 72 | + FROM CocarBundle:PrinterCounter pc1 | |
| 73 | + INNER JOIN CocarBundle:Printer printer WITH pc1.printer = printer.id | |
| 74 | + INNER JOIN CocarBundle:PrinterCounter pc2 WITH (pc1.printer = pc2.printer AND pc2.date >= :start) | |
| 75 | + WHERE pc1.date <= :end | |
| 76 | + GROUP BY printer.id, | |
| 77 | + printer.name, | |
| 78 | + printer.description, | |
| 79 | + printer.host | |
| 80 | + ORDER BY printer.id ASC"; | |
| 81 | + | |
| 82 | + return $this->getEntityManager()->createQuery( $_dql ) | |
| 83 | + ->setParameter('start', $start) | |
| 84 | + ->setParameter('end', $end) | |
| 85 | + ->getArrayResult(); | |
| 86 | + | |
| 87 | + } | |
| 15 | 88 | } | ... | ... |
Resources/views/Printer/index.html.twig
| ... | ... | @@ -10,6 +10,7 @@ |
| 10 | 10 | {{ form_widget(form.startDate) }} |
| 11 | 11 | {{ form_widget(form.endDate) }} |
| 12 | 12 | <button type="submit">Enviar</button> |
| 13 | + <button type="submit" formaction="{{ path('printer_csv') }}">Gerar CSV</button> | |
| 13 | 14 | </form> |
| 14 | 15 | </li> |
| 15 | 16 | </ul> |
| ... | ... | @@ -18,9 +19,13 @@ |
| 18 | 19 | <thead> |
| 19 | 20 | <tr> |
| 20 | 21 | <th>Id</th> |
| 21 | - <th>Impressões</th> | |
| 22 | 22 | <th>Nome</th> |
| 23 | 23 | <th>Host</th> |
| 24 | + <th>Contador Inicial</th> | |
| 25 | + <th>Data Inicial</th> | |
| 26 | + <th>Contador Final</th> | |
| 27 | + <th>Data Final</th> | |
| 28 | + <th>Impressões</th> | |
| 24 | 29 | <th>Ações</th> |
| 25 | 30 | </tr> |
| 26 | 31 | </thead> |
| ... | ... | @@ -28,21 +33,16 @@ |
| 28 | 33 | {% for entity in printer %} |
| 29 | 34 | <tr> |
| 30 | 35 | <td><a href="{{ path('printer_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td> |
| 31 | - <td> | |
| 32 | - {% if printerCounter[entity.id] is defined %} | |
| 33 | - {{ printerCounter[entity.id]['prints'] }} | |
| 34 | - {% else %} | |
| 35 | - 0 | |
| 36 | - {% endif %} | |
| 37 | - </td> | |
| 38 | 36 | <td>{{ entity.name }}</td> |
| 39 | 37 | <td>{{ entity.host }}</td> |
| 38 | + <td>{{ entity.printsStart }}</td> | |
| 39 | + <td>{{ entity.startDate|date("d/m/Y") }}</td> | |
| 40 | + <td>{{ entity.printsEnd }}</td> | |
| 41 | + <td>{{ entity.endDate|date("d/m/Y") }}</td> | |
| 42 | + <td>{{ (entity.printsEnd - entity.printsStart) }}</td> | |
| 40 | 43 | <td> |
| 41 | 44 | <ul> |
| 42 | 45 | <li> |
| 43 | - <a href="{{ path('cocar_printer_reports', { 'id': entity.id }) }}">Relatórios</a> | |
| 44 | - </li> | |
| 45 | - <li> | |
| 46 | 46 | <a href="{{ path('printer_show', { 'id': entity.id }) }}">Visualizar</a> |
| 47 | 47 | </li> |
| 48 | 48 | <li> | ... | ... |
composer.json