ldc-mesbr-app/app/Http/Controllers/ExportController.php

87 lines
3.1 KiB
PHP
Raw Normal View History

2019-03-18 08:58:41 +01:00
<?php
namespace App\Http\Controllers;
2021-04-27 17:29:47 +02:00
2019-03-18 08:58:41 +01:00
use App;
use CpChart\Data;
use CpChart\Image;
use App\Traits\CouponTrait;
use App\Traits\GraphTrait;
use App\Site;
use App\Coupon;
use App\Http\Requests\Export\PDFExport;
2019-03-18 08:58:41 +01:00
use Datetime;
use Illuminate\Http\Request;
class ExportController extends Controller
{
use CouponTrait;
use GraphTrait;
2021-04-27 17:29:47 +02:00
2019-03-18 08:58:41 +01:00
/**
* Handle the incoming request.
*
2025-04-03 09:30:16 +02:00
* @param \Illuminate\Http\Request $request
2019-03-18 08:58:41 +01:00
* @return \Illuminate\Http\Response
*/
public function show(Request $request)
{
2025-04-03 09:30:16 +02:00
$sites = Site::all();
2019-03-18 08:58:41 +01:00
$startdate = new DateTime("first day of last month");
$startdate = $startdate->format('Y-m-d');
2025-04-03 09:30:16 +02:00
$enddate = date('Y-m-d');
2019-03-18 08:58:41 +01:00
2021-04-27 17:29:47 +02:00
return view('export.index')->with(['sites' => $sites, 'startdate' => $startdate, 'enddate' => $enddate]);
2019-03-18 08:58:41 +01:00
}
2025-04-03 09:30:16 +02:00
// ------ AJAX Export
public function exportCoupons(PDFExport $request)
2021-04-27 17:29:47 +02:00
{
$ids = $request->ids;
$startdate = $request->startdate;
$enddate = $request->enddate;
2025-04-03 09:30:16 +02:00
$coupons = Coupon::find($ids);
2019-03-18 08:58:41 +01:00
$labels = $this->getLabelsForStats($startdate, $enddate);
$paths = array();
2019-03-18 08:58:41 +01:00
foreach ($coupons as $coupon) {
2025-04-03 09:30:16 +02:00
$cpName = str_replace(' ', '', $coupon->coupon_name);
$full_path = 'export/' . $cpName . '_' . date('d_m_Y') . '.pdf';
2025-04-03 09:30:16 +02:00
$dataCoupon = $this->getDataForCouponAllCamp($labels, $coupon);
$coupon->couponQuantity = $this->getFullQuantity($coupon);
2019-03-18 08:58:41 +01:00
$coupon->couponFullPrinted = $this->getUsedQuantity($coupon);
2025-04-03 09:30:16 +02:00
$coupon->couponPrinted = $this->getUsedQuantityOnPeriod($coupon, $startdate, $enddate);
2019-03-18 08:58:41 +01:00
$pdf = App::make('dompdf.wrapper');
$myData = new Data();
2021-04-27 17:29:47 +02:00
$myData->addPoints($dataCoupon, "My Serie 1");
$myData->addPoints($labels, "My Serie 2");
$myData->setAxisName(0, "Coupons Diffusés");
2019-03-18 08:58:41 +01:00
$myData->setAbscissa("My Serie 2");
2021-04-27 17:29:47 +02:00
$myData->setSerieTicks("My Serie 2", 2);
$serieSettings = array("R" => 229,"G" => 11,"B" => 11);
$myData->setPalette("My Serie 1", $serieSettings);
$myPicture = new Image(700, 230, $myData);
$myPicture->setGraphArea(60, 40, 670, 190);
$myPicture->setFontProperties(array("FontSize" => 10));
$myPicture->drawScale(array("DrawSubTicks" => true, 'LabelSkip' => 1, 'LabelRotation' => 25, "Mode" => SCALE_MODE_START0, "CycleBackground" => true,"GridR" => 102,"GridG" => 102,"GridB" => 102));
$myPicture->setShadow(true, array("X" => 1,"Y" => 1,"R" => 0,"G" => 0,"B" => 0,"Alpha" => 10));
$myPicture->drawLineChart(array("DisplayColor" => DISPLAY_AUTO));
2019-03-18 08:58:41 +01:00
$myPicture->Render('export/temp.png');
2021-04-27 17:29:47 +02:00
2019-03-18 08:58:41 +01:00
$view = view('export.template')->with(['src' => 'export/temp.png', 'coupon' => $coupon, 'startdate' => $startdate, 'enddate' => $enddate])->render();
$pdf->loadHTML($view);
$pdf->save($full_path);
2019-03-20 14:14:02 +01:00
array_push($paths, $full_path);
2025-04-03 09:30:16 +02:00
}//end foreach
2019-03-18 08:58:41 +01:00
return json_encode($paths);
2019-03-18 08:58:41 +01:00
}
}