From acc3371ba8d2384ee652d43f4417c22ead0c9b7e Mon Sep 17 00:00:00 2001 From: 1Day Date: Tue, 4 Jan 2022 09:05:53 +0100 Subject: [PATCH] Download Invoices in Payment-View --- .../Controllers/Admin/InvoiceController.php | 70 +++++++++++++++++++ .../Controllers/Admin/PaymentController.php | 7 +- .../SettingsController.php | 27 ------- .../views/admin/payments/index.blade.php | 6 ++ .../admin/settings/tabs/invoice.blade.php | 4 -- routes/web.php | 6 +- 6 files changed, 87 insertions(+), 33 deletions(-) create mode 100644 app/Http/Controllers/Admin/InvoiceController.php diff --git a/app/Http/Controllers/Admin/InvoiceController.php b/app/Http/Controllers/Admin/InvoiceController.php new file mode 100644 index 00000000..bc20ef4c --- /dev/null +++ b/app/Http/Controllers/Admin/InvoiceController.php @@ -0,0 +1,70 @@ +open($zip_safe_path, ZipArchive::CREATE | ZipArchive::OVERWRITE); + $result = $this::rglob(storage_path('app/invoice/*')); + if ($res === TRUE) { + $zip->addFromString("1. Info.txt", __("Created at") . " " . now()->format("d.m.Y")); + foreach ($result as $file) { + if (file_exists($file) && is_file($file)) { + $zip->addFile($file, basename($file)); + } + } + $zip->close(); + } + return response()->download($zip_safe_path); + } + + /** + * @param $pattern + * @param $flags + * @return array|false + */ + public function rglob($pattern, $flags = 0) + { + $files = glob($pattern, $flags); + foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { + $files = array_merge($files, $this::rglob($dir . '/' . basename($pattern), $flags)); + } + return $files; + } + + /** + * @param $paymentID + * @param $date + */ + public function downloadSingleInvoice(Request $request) + { + $id = $request->id; + try { + $query = Invoice::where('payment_id', '=', $id)->firstOrFail(); + } catch (Throwable $e) { + return redirect()->back()->with("error", __("Error!")); + } + + $invoice_path = storage_path('app/invoice/' . $query->invoice_user . '/' . $query->created_at->format("Y") . '/' . $query->invoice_name . '.pdf'); + + if (!file_exists($invoice_path)) { + return redirect()->back()->with("error", __("Error!")); + } + + + return response()->download($invoice_path); + + } + +} diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index ea7d3dd0..ff882da6 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -591,6 +591,11 @@ class PaymentController extends Controller ->editColumn('created_at', function (Payment $payment) { return $payment->created_at ? $payment->created_at->diffForHumans() : ''; }) - ->make(); + ->addColumn('actions', function (Payment $payment) { + return ' +'; + }) + ->rawColumns(['actions']) + ->make(true); } } diff --git a/app/Http/Controllers/Admin/SettingsControllers/SettingsController.php b/app/Http/Controllers/Admin/SettingsControllers/SettingsController.php index 0e0da944..cfc7a531 100644 --- a/app/Http/Controllers/Admin/SettingsControllers/SettingsController.php +++ b/app/Http/Controllers/Admin/SettingsControllers/SettingsController.php @@ -9,7 +9,6 @@ use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Http\Request; use Illuminate\Http\Response; -use ZipArchive; class SettingsController extends Controller { @@ -86,30 +85,4 @@ class SettingsController extends Controller return redirect()->route('admin.settings.index')->with('success', 'Invoice settings updated!'); } - public function downloadAllInvoices() - { - $zip = new ZipArchive; - $zip_safe_path = storage_path('invoices.zip'); - $res = $zip->open($zip_safe_path, ZipArchive::CREATE | ZipArchive::OVERWRITE); - $result = $this::rglob(storage_path('app/invoice/*')); - if ($res === TRUE) { - $zip->addFromString("1. Info.txt", "This Archive contains all Invoices from all Users!\nIf there are no Invoices here, no Invoices have ever been created!"); - foreach ($result as $file) { - if (file_exists($file) && is_file($file)) { - $zip->addFile($file, basename($file)); - } - } - $zip->close(); - } - return response()->download($zip_safe_path); - } - - public function rglob($pattern, $flags = 0) - { - $files = glob($pattern, $flags); - foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { - $files = array_merge($files, $this::rglob($dir . '/' . basename($pattern), $flags)); - } - return $files; - } } diff --git a/resources/views/admin/payments/index.blade.php b/resources/views/admin/payments/index.blade.php index f37485ea..de1fc9c1 100644 --- a/resources/views/admin/payments/index.blade.php +++ b/resources/views/admin/payments/index.blade.php @@ -27,6 +27,10 @@
{{ __('Payments') }}
+
@@ -43,6 +47,7 @@ {{ __('Payment ID') }} {{ __('Payment Method') }} {{ __('Created at') }} + @@ -78,6 +83,7 @@ {data: 'payment_id'}, {data: 'payment_method'}, {data: 'created_at'}, + {data: 'actions' , sortable : false}, ], fnDrawCallback: function(oSettings) { $('[data-toggle="popover"]').popover(); diff --git a/resources/views/admin/settings/tabs/invoice.blade.php b/resources/views/admin/settings/tabs/invoice.blade.php index df6469e0..e4e31880 100644 --- a/resources/views/admin/settings/tabs/invoice.blade.php +++ b/resources/views/admin/settings/tabs/invoice.blade.php @@ -1,10 +1,6 @@ @inject('Invoices', 'App\Classes\Settings\InvoiceSettingsC')
-
@csrf diff --git a/routes/web.php b/routes/web.php index e648508f..c39ec7f8 100644 --- a/routes/web.php +++ b/routes/web.php @@ -3,6 +3,7 @@ use App\Http\Controllers\Admin\ActivityLogController; use App\Http\Controllers\Admin\ApplicationApiController; use App\Http\Controllers\Admin\ConfigurationController; +use App\Http\Controllers\Admin\InvoiceController; use App\Http\Controllers\Admin\OverViewController; use App\Http\Controllers\Admin\PaymentController; use App\Http\Controllers\Admin\CreditProductController; @@ -132,9 +133,12 @@ Route::middleware(['auth', 'checkSuspended'])->group(function () { #settings Route::patch('settings/update/icons', [SettingsController::class, 'updateIcons'])->name('settings.update.icons'); Route::patch('settings/update/invoice-settings', [SettingsController::class, 'updateInvoiceSettings'])->name('settings.update.invoicesettings'); - Route::get('settings/download-invoices', [SettingsController::class, 'downloadAllInvoices'])->name('settings.downloadAllInvoices');; Route::resource('settings', SettingsController::class)->only('index'); + #invoices + Route::get('invoices/download-invoices', [InvoiceController::class, 'downloadAllInvoices'])->name('invoices.downloadAllInvoices');; + Route::get('invoices/download-single-invoice', [InvoiceController::class, 'downloadSingleInvoice'])->name('invoices.downloadSingleInvoice');; + #usefullinks Route::get('usefullinks/datatable', [UsefulLinkController::class, 'datatable'])->name('usefullinks.datatable'); Route::resource('usefullinks', UsefulLinkController::class);