diff --git a/app/Http/Controllers/ReportController.php b/app/Http/Controllers/ReportController.php index 6b2ee1e90f..5549c8df19 100644 --- a/app/Http/Controllers/ReportController.php +++ b/app/Http/Controllers/ReportController.php @@ -12,6 +12,10 @@ use Utils; use View; use Carbon; use Validator; +use stdClass; +use DateInterval; +use DatePeriod; + /** * Class ReportController. @@ -173,4 +177,79 @@ class ReportController extends BaseController session()->flash('message', trans('texts.deleted_scheduled_report')); } + + public function showEmailReport() + { + $data = [ + 'account' => auth()->user()->account, + ]; + + return view('reports.emails', $data); + } + + public function loadEmailReport($startDate, $endDate) + { + $account = auth()->user()->account; + $startDate = date_create($startDate); + $endDate = date_create($endDate); + $postmark = new \Postmark\PostmarkClient(config('services.postmark')); + $obj = new stdClass; + + $eventTypes = ['sent', 'opened']; + + foreach ($eventTypes as $eventType) { + $data = []; + $endDate->modify('+1 day'); + $interval = new DateInterval('P1D'); + $period = new DatePeriod($startDate, $interval, $endDate); + $endDate->modify('-1 day'); + $records = []; + + if ($eventType == 'sent') { + $response = $postmark->getOutboundSendStatistics(null, request()->start_date, request()->end_date); + } else { + $response = $postmark->getOutboundOpenStatistics(null, request()->start_date, request()->end_date); + } + + foreach ($response->days as $key => $val) { + $field = $eventType == 'opened' ? 'unique' : $eventType; + $data[$val['date']] = $val[$field]; + } + + foreach ($period as $day) { + $date = $day->format('Y-m-d'); + $records[] = isset($data[$date]) ? $data[$date] : 0; + + if ($eventType == 'sent') { + $labels[] = $day->format('m/d/Y'); + } + } + + if ($eventType == 'sent') { + $color = '51,122,183'; + } elseif ($eventType == 'opened') { + $color = '54,193,87'; + } elseif ($eventType == 'bounced') { + $color = '128,128,128'; + } + + $group = new stdClass(); + $group->data = $records; + $group->label = trans("texts.{$eventType}"); + $group->lineTension = 0; + $group->borderWidth = 4; + $group->borderColor = "rgba({$color}, 1)"; + $group->backgroundColor = "rgba({$color}, 0.1)"; + $datasets[] = $group; + } + + $data = new stdClass(); + $data->labels = $labels; + $data->datasets = $datasets; + + $response = new stdClass(); + $response->data = $data; + + return response()->json($response); + } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 1d55584551..75a20573ac 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2752,6 +2752,9 @@ $LANG = array( 'processing' => 'Processing', 'reactivate' => 'Reactivate', 'reactivated_email' => 'The email address has been reactivated', + 'emails' => 'Emails', + 'opened' => 'Opened', + 'bounced' => 'Bounced', ); diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 0c35e9d40b..ad378395de 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -17,6 +17,7 @@ @if (Auth::user()->hasPermission('view_all')) function loadChart(data) { + console.log(data); var ctx = document.getElementById('chart-canvas').getContext('2d'); if (window.myChart) { window.myChart.config.data = data; diff --git a/resources/views/reports/emails.blade.php b/resources/views/reports/emails.blade.php new file mode 100644 index 0000000000..055f2998cc --- /dev/null +++ b/resources/views/reports/emails.blade.php @@ -0,0 +1,187 @@ +@extends('header') + +@section('head') + @parent + + + + +@stop + +@section('top-right') +