Record.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use DateInterval;
  5. use DatePeriod;
  6. use DateTime;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Facades\Log;
  11. class Record extends Component
  12. {
  13. public $records, $dataId, $totals;
  14. public $in;
  15. public $out;
  16. public $payments = [];
  17. public $fromDate;
  18. public $toDate;
  19. public $appliedFromDate;
  20. public $appliedToDate;
  21. public $selectedPeriod = 'OGGI';
  22. public $filterCausals = null;
  23. public $filterMember = null;
  24. public $isFiltering = false;
  25. public array $recordDatas = [];
  26. public array $labels = [];
  27. public array $causals = [];
  28. public $members = array();
  29. public function hydrate()
  30. {
  31. $this->emit('load-select');
  32. }
  33. public function mount()
  34. {
  35. $this->fromDate = date("Y-m-d");
  36. $this->toDate = date("Y-m-d");
  37. $this->appliedFromDate = date("Y-m-d");
  38. $this->appliedToDate = date("Y-m-d");
  39. $this->getCausals(\App\Models\Causal::select('id', 'name')->where('parent_id', null)->get(), 0);
  40. $this->members = \App\Models\Member::select(['id', 'first_name', 'last_name', 'fiscal_code'])->orderBy('last_name')->orderBy('first_name')->get();
  41. $this->payments = \App\Models\PaymentMethod::select('id', 'name', 'type')->where('enabled', true)->where('money', false)->get();
  42. }
  43. public function resetFilters()
  44. {
  45. $this->selectedPeriod = 'OGGI';
  46. $this->filterCausals = [];
  47. $this->filterMember = null;
  48. $today = date("Y-m-d");
  49. $this->fromDate = $today;
  50. $this->toDate = $today;
  51. $this->appliedFromDate = $today;
  52. $this->appliedToDate = $today;
  53. $this->emit('filters-reset');
  54. }
  55. public function applyFilters()
  56. {
  57. $this->isFiltering = true;
  58. $this->setPeriodDates();
  59. $this->appliedFromDate = $this->fromDate;
  60. $this->appliedToDate = $this->toDate;
  61. $this->render();
  62. $this->isFiltering = false;
  63. $this->emit('filters-applied');
  64. }
  65. private function setPeriodDates()
  66. {
  67. $today = now();
  68. switch ($this->selectedPeriod) {
  69. case 'OGGI':
  70. $this->fromDate = $today->format('Y-m-d');
  71. $this->toDate = $today->format('Y-m-d');
  72. break;
  73. case 'IERI':
  74. $yesterday = $today->copy()->subDay();
  75. $this->fromDate = $yesterday->format('Y-m-d');
  76. $this->toDate = $yesterday->format('Y-m-d');
  77. break;
  78. case 'MESE CORRENTE':
  79. $this->fromDate = $today->copy()->startOfMonth()->format('Y-m-d');
  80. $this->toDate = $today->copy()->endOfMonth()->format('Y-m-d');
  81. break;
  82. case 'MESE PRECEDENTE':
  83. $lastMonth = $today->copy()->subMonth();
  84. $this->fromDate = $lastMonth->startOfMonth()->format('Y-m-d');
  85. $this->toDate = $lastMonth->endOfMonth()->format('Y-m-d');
  86. break;
  87. case 'ULTIMO TRIMESTRE':
  88. $this->fromDate = $today->copy()->subMonths(3)->startOfMonth()->format('Y-m-d');
  89. $this->toDate = $today->copy()->subMonth()->endOfMonth()->format('Y-m-d');
  90. break;
  91. case 'ULTIMO QUADRIMESTRE':
  92. $this->fromDate = $today->copy()->subMonths(4)->startOfMonth()->format('Y-m-d');
  93. $this->toDate = $today->copy()->subMonth()->endOfMonth()->format('Y-m-d');
  94. break;
  95. }
  96. }
  97. public function getCausals($records, $indentation)
  98. {
  99. foreach ($records as $record) {
  100. $this->causals[] = array('id' => $record->id, 'name' => $record->getTree(), 'text' => $record->getTree(), 'level' => $indentation);
  101. if (count($record->childs))
  102. $this->getCausals($record->childs, $indentation + 1);
  103. }
  104. }
  105. public function getMonth($m)
  106. {
  107. $ret = '';
  108. switch ($m) {
  109. case 1:
  110. $ret = 'Gennaio';
  111. break;
  112. case 2:
  113. $ret = 'Febbraio';
  114. break;
  115. case 3:
  116. $ret = 'Marzo';
  117. break;
  118. case 4:
  119. $ret = 'Aprile';
  120. break;
  121. case 5:
  122. $ret = 'Maggio';
  123. break;
  124. case 6:
  125. $ret = 'Giugno';
  126. break;
  127. case 7:
  128. $ret = 'Luglio';
  129. break;
  130. case 8:
  131. $ret = 'Agosto';
  132. break;
  133. case 9:
  134. $ret = 'Settembre';
  135. break;
  136. case 10:
  137. $ret = 'Ottobre';
  138. break;
  139. case 11:
  140. $ret = 'Novembre';
  141. break;
  142. case 12:
  143. $ret = 'Dicembre';
  144. break;
  145. default:
  146. $ret = '';
  147. break;
  148. }
  149. return $ret;
  150. }
  151. public function render()
  152. {
  153. $month = 0;
  154. $year = 0;
  155. $this->records = array();
  156. $this->totals = array();
  157. $exclude_from_records = \App\Models\Member::where('exclude_from_records', true)->pluck('id')->toArray();
  158. $datas = \App\Models\Record::with('member', 'supplier', 'payment_method')
  159. ->select('records.*', 'records_rows.*')
  160. ->join('records_rows', 'records.id', '=', 'records_rows.record_id')
  161. ->whereBetween('date', [$this->appliedFromDate, $this->appliedToDate])
  162. ->where(function ($query) {
  163. $query->where('type', 'OUT')
  164. ->orWhere(function ($query) {
  165. $query->where('records.corrispettivo_fiscale', true)
  166. ->orWhere('records.commercial', false);
  167. });
  168. })
  169. ->where(function ($query) use ($exclude_from_records) {
  170. $query->where('type', 'OUT')
  171. ->orWhere(function ($subquery) use ($exclude_from_records) {
  172. $subquery->whereNotIn('member_id', $exclude_from_records);
  173. });
  174. });
  175. if ($this->filterCausals != null && sizeof($this->filterCausals) > 0) {
  176. $causals = array();
  177. foreach ($this->filterCausals as $z) {
  178. $causals[] = $z;
  179. $childs = \App\Models\Causal::where('parent_id', $z)->get();
  180. foreach ($childs as $c) {
  181. $causals[] = $c->id;
  182. $childsX = \App\Models\Causal::where('parent_id', $c->id)->get();
  183. foreach ($childsX as $cX) {
  184. $causals[] = $cX->id;
  185. }
  186. }
  187. }
  188. $datas->whereIn('causal_id', $causals);
  189. }
  190. if ($this->filterMember != null && $this->filterMember > 0) {
  191. $datas->where('member_id', $this->filterMember);
  192. }
  193. $datas = $datas->orderBy('date', 'ASC')
  194. ->orderBy('records.created_at', 'ASC')
  195. ->orderBy('records_rows.id', 'ASC')
  196. ->get();
  197. $groupedData = [];
  198. $causalsCount = [];
  199. $nominativi = [];
  200. foreach ($datas as $idx => $data) {
  201. $causalCheck = \App\Models\Causal::findOrFail($data->causal_id);
  202. $paymentCheck = $data->payment_method->money;
  203. if (!$paymentCheck && ($causalCheck->no_first == null || !$causalCheck->no_first)) {
  204. if (!$data->deleted) {
  205. $amount = $data->amount;
  206. $amount += getVatValue($amount, $data->vat_id);
  207. } else {
  208. $amount = $data->amount;
  209. }
  210. // Determine the type (commercial or non-commercial)
  211. $typeLabel = $data->commercial ? 'Commerciale' : 'Non Commerciale';
  212. // Get nominativo
  213. $nominativo = '';
  214. if ($data->type == "IN") {
  215. if ($data->member) {
  216. $nominativo = $data->member->last_name . " " . $data->member->first_name;
  217. }
  218. } else {
  219. if ($data->supplier) {
  220. $nominativo = $data->supplier->name;
  221. }
  222. }
  223. // Create grouping key: date + type + payment_method + IN/OUT + nominativo
  224. $groupKey = $data->date . '|' . $typeLabel . '|' . $data->payment_method->name . '|' . $data->type . '|' . $nominativo;
  225. // Initialize group if not exists
  226. if (!isset($groupedData[$groupKey])) {
  227. $groupedData[$groupKey] = [
  228. 'date' => $data->date,
  229. 'type_label' => $typeLabel,
  230. 'payment_method' => $data->payment_method->name,
  231. 'transaction_type' => $data->type,
  232. 'nominativo' => $nominativo,
  233. 'amount' => 0,
  234. 'deleted' => false,
  235. 'causals' => [],
  236. 'notes' => []
  237. ];
  238. $causalsCount[$groupKey] = [];
  239. $nominativi[$groupKey] = $nominativo;
  240. }
  241. $groupedData[$groupKey]['amount'] += $amount;
  242. $causalsCount[$groupKey][$causalCheck->getTree()] = true;
  243. if (!empty($data->note)) {
  244. $groupedData[$groupKey]['notes'][] = $data->note;
  245. }
  246. if ($data->deleted) {
  247. $groupedData[$groupKey]['deleted'] = true;
  248. }
  249. }
  250. }
  251. foreach ($groupedData as $groupKey => $group) {
  252. $causalsInGroup = array_keys($causalsCount[$groupKey]);
  253. $causalDisplay = $group['type_label'];
  254. if (count($causalsInGroup) > 1) {
  255. $detailDisplay = 'Varie|' . implode('|', $causalsInGroup);
  256. } else {
  257. $detailDisplay = $causalsInGroup[0];
  258. }
  259. $recordKey = $group['date'] . "§" . $causalDisplay . "§" . $group['nominativo'] . "§" . $detailDisplay . "§" . ($group['deleted'] ? 'DELETED' : '') . "§";
  260. if (!isset($this->records[$recordKey][$group['payment_method']][$group['transaction_type']])) {
  261. $this->records[$recordKey][$group['payment_method']][$group['transaction_type']] = 0;
  262. }
  263. $this->records[$recordKey][$group['payment_method']][$group['transaction_type']] += $group['amount'];
  264. if (!isset($this->totals[$group['payment_method']])) {
  265. $this->totals[$group['payment_method']]["IN"] = 0;
  266. $this->totals[$group['payment_method']]["OUT"] = 0;
  267. }
  268. if (!$group['deleted'])
  269. $this->totals[$group['payment_method']][$group['transaction_type']] += $group['amount'];
  270. }
  271. return view('livewire.records');
  272. }
  273. private function getLabels($fromDate, $toDate)
  274. {
  275. $begin = new DateTime($fromDate);
  276. $end = new DateTime($toDate);
  277. $interval = DateInterval::createFromDateString('1 day');
  278. $date_range = new DatePeriod($begin, $interval, $end);
  279. foreach ($date_range as $date) {
  280. $labels[] = $date->format('d/M');
  281. }
  282. return $labels;
  283. }
  284. private function getRecordData($type, $fromDate, $toDate)
  285. {
  286. $data = [];
  287. $begin = new DateTime($fromDate);
  288. $end = new DateTime($toDate);
  289. $interval = DateInterval::createFromDateString('1 day');
  290. $date_range = new DatePeriod($begin, $interval, $end);
  291. foreach ($date_range as $date) {
  292. if ($type == 'IN') {
  293. $found = false;
  294. foreach ($this->in as $in) {
  295. if (date("Y-m-d", strtotime($in->date)) == $date->format('Y-m-d')) {
  296. $data[] = number_format($in->total, 0, "", "");
  297. $found = true;
  298. }
  299. }
  300. if (!$found)
  301. $data[] = 0;
  302. }
  303. if ($type == 'OUT') {
  304. $found = false;
  305. foreach ($this->out as $out) {
  306. if (date("Y-m-d", strtotime($out->date)) == $date->format('Y-m-d')) {
  307. $data[] = number_format($out->total, 0, "", "");
  308. $found = true;
  309. }
  310. }
  311. if (!$found)
  312. $data[] = 0;
  313. }
  314. }
  315. return $data;
  316. }
  317. public function export()
  318. {
  319. ini_set('memory_limit', '512M');
  320. gc_enable();
  321. $letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
  322. $spreadsheet = new Spreadsheet();
  323. $activeWorksheet = $spreadsheet->getActiveSheet();
  324. $activeWorksheet->setCellValue('A1', "Data");
  325. $activeWorksheet->setCellValue('B1', "Tipo");
  326. $activeWorksheet->setCellValue('C1', "Causale");
  327. $activeWorksheet->setCellValue('D1', "Nominativo");
  328. $activeWorksheet->setCellValue('E1', "Stato");
  329. $idx = 0;
  330. foreach ($this->payments as $p) {
  331. if ($idx >= count($letters)) {
  332. break;
  333. }
  334. $activeWorksheet->setCellValue($letters[$idx] . '1', $p->name);
  335. $idx++;
  336. if ($idx >= count($letters)) {
  337. break;
  338. }
  339. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx] . '1');
  340. $idx++;
  341. }
  342. $idx = 0;
  343. $activeWorksheet->setCellValue('A2', "");
  344. $activeWorksheet->setCellValue('B2', "");
  345. $activeWorksheet->setCellValue('C2', "");
  346. $activeWorksheet->setCellValue('D2', "");
  347. $activeWorksheet->setCellValue('E2', "");
  348. foreach ($this->payments as $p) {
  349. if ($p->type == 'ALL') {
  350. if ($idx >= count($letters)) {
  351. break;
  352. }
  353. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  354. $idx++;
  355. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  356. $idx++;
  357. } elseif ($p->type == 'IN') {
  358. if ($idx >= count($letters)) {
  359. break;
  360. }
  361. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  362. $idx++;
  363. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  364. $idx++;
  365. } elseif ($p->type == 'OUT') {
  366. if ($idx >= count($letters)) {
  367. break;
  368. }
  369. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  370. $idx++;
  371. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  372. $idx++;
  373. }
  374. }
  375. $activeWorksheet->getStyle('A1:Q1')->getFont()->setBold(true);
  376. $activeWorksheet->getStyle('A2:Q2')->getFont()->setBold(true);
  377. $count = 3;
  378. $batchSize = 1000;
  379. $recordsProcessed = 0;
  380. $totalRecords = count($this->records);
  381. $recordsArray = array_chunk($this->records, $batchSize, true);
  382. foreach ($recordsArray as $recordsBatch) {
  383. foreach ($recordsBatch as $causal => $record) {
  384. $check = $causal;
  385. $parts = explode("§", $check);
  386. $d = $parts[0] ?? '';
  387. $c = $parts[1] ?? '';
  388. $j = $parts[2] ?? '';
  389. $det = $parts[3] ?? '';
  390. $deleted = $parts[4] ?? '';
  391. $detailParts = explode('|', $det);
  392. $exportDetail = count($detailParts) > 1 ? implode(', ', array_slice($detailParts, 1)) : $det;
  393. $activeWorksheet->setCellValue('A' . $count, !empty($d) ? date("d/m/Y", strtotime($d)) : '');
  394. $activeWorksheet->setCellValue('B' . $count, $c);
  395. $activeWorksheet->setCellValue('C' . $count, $exportDetail);
  396. $activeWorksheet->setCellValue('D' . $count, $j);
  397. $stato = ($deleted === 'DELETED') ? 'ANNULLATA' : '';
  398. $activeWorksheet->setCellValue('E' . $count, $stato);
  399. if ($stato === 'ANNULLATA') {
  400. $activeWorksheet->getStyle('E' . $count)->getFont()->getColor()->setARGB('FFFF0000');
  401. }
  402. $idx = 0;
  403. foreach ($this->payments as $p) {
  404. if ($idx >= count($letters) - 1) {
  405. break;
  406. }
  407. if (isset($record[$p->name])) {
  408. $inValue = isset($record[$p->name]["IN"]) ? formatPrice($record[$p->name]["IN"]) : "";
  409. $outValue = isset($record[$p->name]["OUT"]) ? formatPrice($record[$p->name]["OUT"]) : "";
  410. $activeWorksheet->setCellValue($letters[$idx] . $count, $inValue);
  411. $idx++;
  412. $activeWorksheet->setCellValue($letters[$idx] . $count, $outValue);
  413. $idx++;
  414. } else {
  415. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  416. $idx++;
  417. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  418. $idx++;
  419. }
  420. }
  421. $count++;
  422. $recordsProcessed++;
  423. if ($recordsProcessed % 500 === 0) {
  424. gc_collect_cycles();
  425. }
  426. }
  427. unset($recordsBatch);
  428. gc_collect_cycles();
  429. }
  430. $count++;
  431. $idx = 0;
  432. $activeWorksheet->setCellValue('A' . $count, 'Totale');
  433. $activeWorksheet->setCellValue('B' . $count, '');
  434. $activeWorksheet->setCellValue('C' . $count, '');
  435. $activeWorksheet->setCellValue('D' . $count, '');
  436. $activeWorksheet->setCellValue('E' . $count, '');
  437. foreach ($this->payments as $p) {
  438. if ($idx >= count($letters) - 1) {
  439. break;
  440. }
  441. if (isset($this->totals[$p->name])) {
  442. if ($p->type == 'ALL') {
  443. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"] ?? 0));
  444. $idx++;
  445. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"] ?? 0));
  446. $idx++;
  447. } elseif ($p->type == 'IN') {
  448. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["IN"] ?? 0));
  449. $idx++;
  450. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  451. $idx++;
  452. } elseif ($p->type == 'OUT') {
  453. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  454. $idx++;
  455. $activeWorksheet->setCellValue($letters[$idx] . $count, formatPrice($this->totals[$p->name]["OUT"] ?? 0));
  456. $idx++;
  457. }
  458. } else {
  459. if ($p->type == 'ALL') {
  460. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  461. $idx++;
  462. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  463. $idx++;
  464. } elseif ($p->type == 'IN') {
  465. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  466. $idx++;
  467. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  468. $idx++;
  469. } elseif ($p->type == 'OUT') {
  470. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  471. $idx++;
  472. $activeWorksheet->setCellValue($letters[$idx] . $count, "0");
  473. $idx++;
  474. }
  475. }
  476. }
  477. $activeWorksheet->getStyle('A' . $count . ':Q' . $count)->getFont()->setBold(true);
  478. $activeWorksheet->getColumnDimension('A')->setWidth(20);
  479. $activeWorksheet->getColumnDimension('B')->setWidth(40);
  480. $activeWorksheet->getColumnDimension('C')->setWidth(40);
  481. $activeWorksheet->getColumnDimension('D')->setWidth(40);
  482. $activeWorksheet->getColumnDimension('E')->setWidth(20);
  483. foreach ($letters as $l) {
  484. $activeWorksheet->getColumnDimension($l)->setWidth(20);
  485. }
  486. $filename = 'prima_nota_' . date("YmdHis") . '.xlsx';
  487. try {
  488. $currentClient = session('currentClient', 'default');
  489. $tempPath = sys_get_temp_dir() . '/' . $filename;
  490. $writer = new Xlsx($spreadsheet);
  491. $writer->save($tempPath);
  492. unset($spreadsheet, $activeWorksheet, $writer);
  493. gc_collect_cycles();
  494. $disk = Storage::disk('s3');
  495. $s3Path = $currentClient . '/prima_nota/' . $filename;
  496. $primaNotaFolderPath = $currentClient . '/prima_nota/.gitkeep';
  497. if (!$disk->exists($primaNotaFolderPath)) {
  498. $disk->put($primaNotaFolderPath, '');
  499. Log::info("Created prima_nota folder for client: {$currentClient}");
  500. }
  501. $fileContent = file_get_contents($tempPath);
  502. $uploaded = $disk->put($s3Path, $fileContent, 'private');
  503. if (!$uploaded) {
  504. throw new \Exception('Failed to upload file to Wasabi S3');
  505. }
  506. Log::info("Prima Nota exported to Wasabi", [
  507. 'client' => $currentClient,
  508. 'path' => $s3Path,
  509. 'size' => filesize($tempPath),
  510. 'records_processed' => $recordsProcessed
  511. ]);
  512. if (file_exists($tempPath)) {
  513. unlink($tempPath);
  514. }
  515. $downloadUrl = $disk->temporaryUrl($s3Path, now()->addHour());
  516. return redirect($downloadUrl);
  517. } catch (\Exception $e) {
  518. Log::error('Error exporting Prima Nota to Wasabi S3', [
  519. 'error' => $e->getMessage(),
  520. 'client' => session('currentClient', 'unknown'),
  521. 'filename' => $filename,
  522. 'records_processed' => $recordsProcessed ?? 0
  523. ]);
  524. $currentClient = session('currentClient', 'default');
  525. $clientFolder = storage_path('app/prima_nota/' . $currentClient);
  526. if (!is_dir($clientFolder)) {
  527. mkdir($clientFolder, 0755, true);
  528. Log::info("Created local client prima_nota folder: {$clientFolder}");
  529. }
  530. $localPath = $clientFolder . '/' . $filename;
  531. if (isset($tempPath) && file_exists($tempPath)) {
  532. rename($tempPath, $localPath);
  533. } else {
  534. $writer = new Xlsx($spreadsheet);
  535. $writer->save($localPath);
  536. unset($spreadsheet, $activeWorksheet, $writer);
  537. }
  538. gc_collect_cycles();
  539. Log::warning("Prima Nota saved locally due to S3 error", [
  540. 'client' => $currentClient,
  541. 'local_path' => $localPath,
  542. 'error' => $e->getMessage()
  543. ]);
  544. session()->flash('warning', 'File salvato localmente a causa di un errore del cloud storage.');
  545. return response()->download($localPath)->deleteFileAfterSend();
  546. }
  547. }
  548. }