diff --git a/src/app/Console/Commands/Data/Reports/VatCommand.php b/src/app/Console/Commands/Data/Reports/VatCommand.php new file mode 100644 index 00000000..6468100a --- /dev/null +++ b/src/app/Console/Commands/Data/Reports/VatCommand.php @@ -0,0 +1,88 @@ +argument('email'); + + $result = DB::select( +"SELECT + DATE_FORMAT(p.created_at, '%Y-%m-%d %H:%I') AS timestamp, + v.country AS country, + p.id AS payment_id, + ROUND((amount / 100), 2) AS income_gross, + ROUND(((amount - (amount / (100 + v.rate) * v.rate)) / 100), 2) AS income_net, + ROUND(((amount / (100 + v.rate) * v.rate) / 100), 2) AS income_vat +FROM + payments p +INNER JOIN vat_rates v + ON p.vat_rate_id = v.id +INNER JOIN wallets w + ON p.wallet_id = w.id +INNER JOIN user_settings us + ON w.user_id = us.user_id +WHERE + p.status = 'paid' + AND us.`key` = 'country' +ORDER BY timestamp, country" + ); + + $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); + } +}