InvoiceSettingsController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Http\Controllers\Admin\SettingsControllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\InvoiceSettings;
  5. use Illuminate\Http\Request;
  6. use ZipArchive;
  7. class InvoiceSettingsController extends Controller
  8. {
  9. public $tabTitle = 'Invoice Settings';
  10. public $invoiceSettings;
  11. public function __construct()
  12. {
  13. $this->invoiceSettings = InvoiceSettings::first();
  14. }
  15. public function index()
  16. {
  17. return view('admin.settings.tabs.invoice', [
  18. 'invoiceSettings' => $this->invoiceSettings,
  19. ]);
  20. }
  21. public function updateInvoiceSettings(Request $request)
  22. {
  23. $request->validate([
  24. 'logo' => 'nullable|max:10000|mimes:jpg,png,jpeg',
  25. ]);
  26. InvoiceSettings::updateOrCreate([
  27. 'id' => "1"
  28. ], [
  29. 'company_name' => $request->get('company-name'),
  30. 'company_adress' => $request->get('company-address'),
  31. 'company_phone' => $request->get('company-phone'),
  32. 'company_mail' => $request->get('company-mail'),
  33. 'company_vat' => $request->get('company-vat'),
  34. 'company_web' => $request->get('company-web'),
  35. 'invoice_prefix' => $request->get('invoice-prefix'),
  36. ]);
  37. if ($request->hasFile('logo')) {
  38. $request->file('logo')->storeAs('public', 'logo.png');
  39. }
  40. return redirect()->route('admin.settings.index')->with('success', 'Invoice settings updated!');
  41. }
  42. public function downloadAllInvoices()
  43. {
  44. $zip = new ZipArchive;
  45. $zip_safe_path = storage_path('invoices.zip');
  46. $res = $zip->open($zip_safe_path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  47. $result = $this::rglob(storage_path('app/invoice/*'));
  48. if ($res === TRUE) {
  49. $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!");
  50. foreach ($result as $file) {
  51. if (file_exists($file) && is_file($file)) {
  52. $zip->addFile($file, basename($file));
  53. }
  54. }
  55. $zip->close();
  56. }
  57. return response()->download($zip_safe_path);
  58. }
  59. public function rglob($pattern, $flags = 0)
  60. {
  61. $files = glob($pattern, $flags);
  62. foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
  63. $files = array_merge($files, $this::rglob($dir . '/' . basename($pattern), $flags));
  64. }
  65. return $files;
  66. }
  67. }