Created
March 6, 2018 07:52
-
-
Save aliemre/5bae41b26eec9ccb87f3bbea42a973c2 to your computer and use it in GitHub Desktop.
Bulk File Download Within Zip In Symfony
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function exAction(Request $request) | |
{ | |
clearstatcache(); | |
$em = $this->getDoctrine()->getManager(); | |
$responseData = []; | |
$files = []; | |
if ($request->query->get('download', false)) { | |
$schedules = $em | |
->getRepository('SystemOfficeBundle:Schedule') | |
->findByContainerFillDate(); | |
if (count($schedules)) { | |
/** @var Schedule $schedule */ | |
foreach ($schedules as $schedule) { | |
$bookings = $schedule->getBookings(); | |
if ($bookings->count()) { | |
$today = new \DateTime(); | |
$today->setTimezone(new \DateTimeZone('Europe/Istanbul')); | |
$spreadsheet = new Spreadsheet(); | |
$spreadsheet->getProperties() | |
->setCreator('JMS Rezervasyon') | |
->setTitle($schedule->getTitle() . ' - ' . $schedule->getVessel()->getName() . ' : ' . $today->format('d-m-Y H:i')) | |
->setDescription('Plana ait rezervasyon listesi.') | |
; | |
$sheet = $spreadsheet->getActiveSheet(); | |
$sheet->setCellValue('A1', 'REF. NO'); | |
$sheet->setCellValue('B1', 'MÜŞTERİ'); | |
$sheet->setCellValue('C1', 'G. YÜKLEYİCİ'); | |
$sheet->setCellValue('D1', 'T. LİMANI'); | |
$sheet->setCellValue('E1', 'ADET / P. TİPİ'); | |
$sheet->setCellValue('F1', 'KİLO'); | |
$sheet->setCellValue('G1', 'CBM/M3'); | |
$sheet->setCellValue('H1', 'İSTİFLEME'); | |
$sheet->setCellValue('I1', 'DEPODA'); | |
// Style | |
$sheet->getColumnDimension('A')->setWidth(25); | |
$sheet->getColumnDimension('B')->setWidth(30); | |
$sheet->getColumnDimension('C')->setWidth(25); | |
$sheet->getColumnDimension('D')->setWidth(25); | |
$sheet->getColumnDimension('E')->setWidth(20); | |
$sheet->getColumnDimension('F')->setWidth(15); | |
$sheet->getColumnDimension('G')->setWidth(15); | |
$sheet->getColumnDimension('H')->setWidth(17); | |
$sheet->getColumnDimension('I')->setWidth(17); | |
$sheet->getStyle('A1:I1')->applyFromArray(['font' => ['bold' => true]]); | |
// Data | |
$index = 5; | |
$totalProductAmount = 0.0; | |
$totalProductType = '-'; | |
$totalProductWeight = 0.0; | |
$totalProductfCBM = 0.0; | |
$totalWeight = 0.0; | |
$totalVolume = 0.0; | |
// First Header | |
$sheet->setCellValue('A3', 'DEPODA OLMAYANLAR'); | |
$sheet->getStyle('A3')->applyFromArray(['font' => ['bold' => true]]); | |
/** @var Booking $booking */ | |
foreach ($bookings as $booking) { | |
// $booking->getShipment() && ! $booking->getShipment()->getIsContainerLoaded() | |
if (! $booking->getIsPickedUp()) { | |
$sheet->setCellValue('A' . $index, $booking->getNumber()); | |
$sheet->setCellValue('B' . $index, $booking->getCustomer()->getCompanyName()); | |
$sheet->setCellValue('C' . $index, $booking->getShipper()); | |
$sheet->setCellValue('D' . $index, $booking->getEvacuationPort()); | |
$products = $booking->getProducts(); | |
if ($products->count()) { | |
/** @var Product $product */ | |
foreach ($products as $product) { | |
if ($booking->getIsStackable()) { | |
if ($booking->getPossibleHeight() != "") { | |
$height = $booking->getPossibleHeight(); | |
} else { | |
$height = $booking->getPossibleHeightOther(); | |
} | |
} else { | |
$height = $product->getHeight(); | |
} | |
$totalProductAmount += $product->getCupAmount(); | |
$totalProductType = $product->getCupType(); | |
$totalProductWeight += $product->getWeight(); | |
$cbm = ($product->getCupAmount() * $product->getLength() * $product->getWidth() * $height) / 1000000; | |
$totalProductfCBM += $cbm; | |
} | |
$sheet->setCellValue('E' . $index, $totalProductAmount . " / " . $totalProductType); | |
$sheet->setCellValue('F' . $index, $totalProductWeight); | |
$sheet->setCellValue('G' . $index, number_format($totalProductfCBM)); | |
$totalWeight += $booking->getPossibleHeight(); | |
$totalVolume += number_format($booking->getPossibleVolume()); | |
} else { | |
$sheet->setCellValue('E' . $index, $booking->getCupAmount() . " /" . $booking->getCupType()); | |
$sheet->setCellValue('F' . $index, $booking->getPossibleHeight()); | |
$sheet->setCellValue('G' . $index, number_format($booking->getPossibleVolume())); | |
} | |
$isStackable = $booking->getIsStackable() ? "EVET" : "HAYIR"; | |
$sheet->setCellValue('H' . $index, $isStackable); | |
if ($booking->getIsFreeTransportation()) { | |
$isInHouse = "S. BÖLGE"; | |
} else { | |
$isInHouse = $booking->getIsPickedUp() ? "EVET" : "HAYIR"; | |
//$color = $booking->getIsPickedUp() ? Color::COLOR_GREEN : Color::COLOR_RED; | |
//$sheet->getStyle('I' . $index)->getFont()->getColor()->setRGB($color); | |
$sheet->getStyle('I' . $index)->applyFromArray(['font' => ['bold' => true]]); | |
} | |
$sheet->setCellValue('I' . $index, $isInHouse); | |
$index++; | |
} | |
} | |
// Second Header | |
$sheet->setCellValue('A' . ++$index, 'DEPODA OLANLAR'); | |
$sheet->getStyle('A' . $index++)->applyFromArray(['font' => ['bold' => true]]); | |
$index++; | |
/** @var Booking $booking */ | |
foreach ($bookings as $booking) { | |
// $booking->getShipment() && ! $booking->getShipment()->getIsContainerLoaded() | |
if ($booking->getIsPickedUp()) { | |
$sheet->setCellValue('A' . $index, $booking->getNumber()); | |
$sheet->setCellValue('B' . $index, $booking->getCustomer()->getCompanyName()); | |
$sheet->setCellValue('C' . $index, $booking->getShipper()); | |
$sheet->setCellValue('D' . $index, $booking->getEvacuationPort()); | |
$products = $booking->getProducts(); | |
if ($products->count()) { | |
/** @var Product $product */ | |
foreach ($products as $product) { | |
if ($booking->getIsStackable()) { | |
if ($booking->getPossibleHeight() != "") { | |
$height = $booking->getPossibleHeight(); | |
} else { | |
$height = $booking->getPossibleHeightOther(); | |
} | |
} else { | |
$height = $product->getHeight(); | |
} | |
$totalProductAmount += $product->getCupAmount(); | |
$totalProductType = $product->getCupType(); | |
$totalProductWeight += $product->getWeight(); | |
$cbm = ($product->getCupAmount() * $product->getLength() * $product->getWidth() * $height) / 1000000; | |
$totalProductfCBM += $cbm; | |
} | |
$sheet->setCellValue('E' . $index, $totalProductAmount . " / " . $totalProductType); | |
$sheet->setCellValue('F' . $index, $totalProductWeight); | |
$sheet->setCellValue('G' . $index, number_format($totalProductfCBM)); | |
$totalWeight += $booking->getPossibleHeight(); | |
$totalVolume += number_format($booking->getPossibleVolume()); | |
} else { | |
$sheet->setCellValue('E' . $index, $booking->getCupAmount() . " /" . $booking->getCupType()); | |
$sheet->setCellValue('F' . $index, $booking->getPossibleHeight()); | |
$sheet->setCellValue('G' . $index, number_format($booking->getPossibleVolume())); | |
} | |
$isStackable = $booking->getIsStackable() ? "EVET" : "HAYIR"; | |
$sheet->setCellValue('H' . $index, $isStackable); | |
if ($booking->getIsFreeTransportation()) { | |
$isInHouse = "S. BÖLGE"; | |
} else { | |
$isInHouse = $booking->getIsPickedUp() ? "EVET" : "HAYIR"; | |
//$color = $booking->getIsPickedUp() ? Color::COLOR_GREEN : Color::COLOR_RED; | |
//$sheet->getStyle('I' . $index)->getFont()->getColor()->setRGB($color); | |
$sheet->getStyle('I' . $index)->applyFromArray(['font' => ['bold' => true]]); | |
} | |
$sheet->setCellValue('I' . $index, $isInHouse); | |
$index++; | |
} | |
} | |
$sheet->setCellValue('E' . ($index+2), 'TOPLAM'); | |
$sheet->setCellValue('F' . ($index+2), $totalWeight); | |
$sheet->setCellValue('G' . ($index+2), $totalVolume); | |
$sheet->getStyle('E' . ($index+2) . ':G' . ($index+2))->applyFromArray(['font' => ['bold' => true]]); | |
// Save Xlsx | |
$webFolder = $this->getParameter('web_folder'); | |
$uploadDir = 'uploads/dbr/' . $today->format('dmYHi') . '/'; | |
if (! is_dir($webFolder . $uploadDir)) { | |
mkdir($webFolder . $uploadDir, '0777', true); | |
} | |
$fileName = 'DBR_' . $today->format('d-m-Y-H-i') . '_' . $schedule->getId() . '.xlsx'; | |
$fileDir = $webFolder . $uploadDir; | |
$filePath = $uploadDir . $fileName; | |
$writer = new Xlsx($spreadsheet); | |
$writer->save($fileDir . $fileName); | |
$files[$fileName] = $fileDir . $fileName; | |
$responseData['downloaded'] = true; | |
} | |
} | |
// Save Zip | |
$zip = new \ZipArchive(); | |
$datetime = new \DateTime(); | |
$filename = $datetime->format('Y-m-d-H-i-s') . ".zip"; | |
$filepath = $this->container->getParameter('web_folder') . 'uploads/dbr/' . $filename; | |
if ($zip->open($filepath, \ZipArchive::CREATE) !== true) { | |
throw new NotImplementedException("Bu şekilde bir ZIP arşivi oluşturulamıyor."); | |
} | |
foreach ($files as $filename => $path) { | |
if (file_exists($path)) { | |
$zip->addFile($path, $filename); | |
} | |
} | |
$zip->close(); | |
// Download Zip | |
$response = new Response(); | |
$response->headers->set('Cache-Control', 'private'); | |
$response->headers->set('Content-type', mime_content_type($filepath)); | |
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filepath) . '";'); | |
$response->headers->set('Content-length', filesize($filepath)); | |
$response->sendHeaders(); | |
$response->setContent(file_get_contents($filepath)); | |
return $response; | |
} | |
} | |
return $this->render('SystemOfficeBundle:Admin/Report:dbr.html.twig', $responseData); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment