ctrlpanel/app/Http/Controllers/Admin/SettingsControllers/InvoiceSettingsController.php

80 lines
2.5 KiB
PHP
Raw Normal View History

2021-06-05 09:26:32 +00:00
<?php
namespace App\Http\Controllers\Admin\SettingsControllers;
2021-06-05 09:26:32 +00:00
use App\Http\Controllers\Controller;
2021-12-07 12:16:44 +00:00
use App\Models\InvoiceSettings;
2021-06-05 09:26:32 +00:00
use Illuminate\Http\Request;
2021-12-02 08:54:30 +00:00
use ZipArchive;
2021-06-05 09:26:32 +00:00
class InvoiceSettingsController extends Controller
2021-06-05 09:26:32 +00:00
{
public $tabTitle = 'Invoice Settings';
public $invoiceSettings;
2021-12-14 18:50:36 +00:00
public function __construct()
{
$this->invoiceSettings = InvoiceSettings::first();
2021-06-05 09:26:32 +00:00
}
public function index()
2021-11-12 17:23:06 +00:00
{
return view('admin.settings.tabs.invoice', [
'invoiceSettings' => $this->invoiceSettings,
2021-06-05 09:26:32 +00:00
]);
}
2021-11-30 17:40:56 +00:00
public function updateInvoiceSettings(Request $request)
{
$request->validate([
2021-12-01 11:19:06 +00:00
'logo' => 'nullable|max:10000|mimes:jpg,png,jpeg',
2021-11-30 17:40:56 +00:00
]);
2021-12-14 18:50:36 +00:00
InvoiceSettings::updateOrCreate([
'id' => "1"
], [
'company_name' => $request->get('company-name'),
2021-12-16 18:38:25 +00:00
'company_adress' => $request->get('company-address'),
2021-12-14 18:50:36 +00:00
'company_phone' => $request->get('company-phone'),
'company_mail' => $request->get('company-mail'),
'company_vat' => $request->get('company-vat'),
'company_web' => $request->get('company-web'),
'invoice_prefix' => $request->get('invoice-prefix'),
]);
2021-11-30 17:40:56 +00:00
2021-12-01 11:19:06 +00:00
if ($request->hasFile('logo')) {
$request->file('logo')->storeAs('public', 'logo.png');
}
2021-11-30 17:40:56 +00:00
return redirect()->route('admin.settings.index')->with('success', 'Invoice settings updated!');
}
2021-12-07 19:27:54 +00:00
public function downloadAllInvoices()
{
2021-12-02 08:54:30 +00:00
$zip = new ZipArchive;
$zip_safe_path = storage_path('invoices.zip');
2021-12-07 19:27:54 +00:00
$res = $zip->open($zip_safe_path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
2021-12-02 08:54:30 +00:00
$result = $this::rglob(storage_path('app/invoice/*'));
if ($res === TRUE) {
2021-12-07 19:27:54 +00:00
$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) {
2021-12-02 08:54:30 +00:00
if (file_exists($file) && is_file($file)) {
2021-12-07 19:27:54 +00:00
$zip->addFile($file, basename($file));
2021-12-02 08:54:30 +00:00
}
}
$zip->close();
}
2021-12-07 19:27:54 +00:00
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));
2021-12-07 19:24:25 +00:00
}
2021-12-07 19:27:54 +00:00
return $files;
2021-12-02 08:54:30 +00:00
}
2021-06-05 09:26:32 +00:00
}