diff --git a/src/app/Console/Commands/Data/Reports/VatCommand.php b/src/app/Console/Commands/Data/Reports/VatCommand.php index 79c927ee..195f8dcb 100644 --- a/src/app/Console/Commands/Data/Reports/VatCommand.php +++ b/src/app/Console/Commands/Data/Reports/VatCommand.php @@ -1,90 +1,98 @@ argument('email'); + // Note: Constructor takes care of date, but startOfDay()/endOfDay() takes care of time + $start = (new Carbon('first day of last month'))->startOfDay(); + $end = (new Carbon('last day of last month'))->endOfDay(); + $result = DB::select( <<= ? + AND p.created_at <= ? ORDER BY timestamp, country - SQL + SQL, + [$start->toDateTimeString(), $end->toDateTimeString()] ); $fp = fopen('php://memory', 'w'); foreach ($result as $record) { fputcsv($fp, (array) $record); } rewind($fp); $csv = stream_get_contents($fp); fclose($fp); $this->sendMail($recipient, $csv); } /** * Sends an email message with csv file attached */ protected function sendMail($recipient, $csv) { $plainBody = 'See the attached report!'; $attachment = Attachment::fromData(fn () => $csv, 'Report.csv')->withMime('text/csv'); $mail = new \App\Mail\Mailable(); $mail->subject('VAT Report') // This hack allows as to use plain text body instead of a Laravel view ->text(new \Illuminate\Support\HtmlString($plainBody)) ->to($recipient) ->attach($attachment); \App\Mail\Helper::sendMail($mail); } }