ExportPrimaNota.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. <?php
  2. namespace App\Jobs;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Support\Facades\Mail;
  9. use Illuminate\Support\Facades\Log;
  10. use App\Mail\ExportNotification;
  11. use App\Events\ExportCompleted;
  12. use App\Events\ExportFailed;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  15. class ExportPrimaNota implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. public $timeout = 1200;
  19. public $tries = 3;
  20. public $maxExceptions = 3;
  21. //protected $exportData;
  22. //protected $exportTotals;
  23. protected $records;
  24. protected $emailAddress;
  25. protected $emailSubject;
  26. protected $dateRange;
  27. protected $userId;
  28. protected $payments;
  29. protected $filters;
  30. /**
  31. * Create a new job instance.
  32. */
  33. //public function __construct($exportData, $exportTotals, $emailAddress, $emailSubject, $dateRange, $userId, $payments, $filters = [])
  34. public function __construct($records, $emailAddress, $emailSubject, $dateRange, $userId, $payments, $filters = [])
  35. {
  36. //$this->exportData = $exportData;
  37. //$this->exportTotals = $exportTotals;
  38. $this->records = $records;
  39. $this->emailAddress = $emailAddress;
  40. $this->emailSubject = $emailSubject;
  41. $this->dateRange = $dateRange;
  42. $this->userId = $userId;
  43. $this->payments = $payments;
  44. $this->filters = $filters;
  45. $this->onQueue('exports');
  46. }
  47. /**
  48. * Execute the job.
  49. */
  50. public function handle()
  51. {
  52. try {
  53. Log::info('Starting background export', [
  54. 'user_id' => $this->userId,
  55. 'email' => $this->emailAddress,
  56. 'date_range' => $this->dateRange,
  57. //'total_records' => count($this->exportData)
  58. ]);
  59. ini_set('memory_limit', '1024M');
  60. $filename = 'prima_nota_' . date("Ymd_His") . '_' . $this->userId . '.xlsx';
  61. $tempPath = sys_get_temp_dir() . '/' . $filename;
  62. $this->createExcelFile($tempPath);
  63. if (!file_exists($tempPath) || filesize($tempPath) === 0) {
  64. throw new \Exception('Excel file creation failed');
  65. }
  66. $fileSize = filesize($tempPath);
  67. $maxSize = 25 * 1024 * 1024;
  68. if ($fileSize > $maxSize) {
  69. throw new \Exception('File too large for email attachment (' . round($fileSize / 1024 / 1024, 2) . 'MB > 25MB)');
  70. }
  71. $user = \App\Models\User::find($this->userId);
  72. $emailData = [
  73. 'subject' => $this->emailSubject,
  74. 'from_date' => $this->dateRange['from'],
  75. 'to_date' => $this->dateRange['to'],
  76. // 'total_records' => count($this->exportData),
  77. 'total_records' => count($this->records) - 1,
  78. 'user_name' => $user ? $user->name : 'Utente',
  79. 'generated_at' => now()->format('d/m/Y H:i:s'),
  80. 'filters_applied' => $this->getFiltersDescription(),
  81. 'file_size' => round($fileSize / 1024 / 1024, 2) . ' MB'
  82. ];
  83. Mail::to($this->emailAddress)->send(new ExportNotification($emailData, $tempPath, $filename));
  84. if (class_exists(ExportCompleted::class)) {
  85. broadcast(new ExportCompleted($this->userId, $filename, $this->emailAddress));
  86. }
  87. Log::info('Background export completed successfully', [
  88. 'user_id' => $this->userId,
  89. 'email' => $this->emailAddress,
  90. 'filename' => $filename,
  91. 'file_size' => $fileSize,
  92. 'processing_time' => microtime(true) - LARAVEL_START ?? 0
  93. ]);
  94. } catch (\Exception $e) {
  95. Log::error('Background export failed', [
  96. 'user_id' => $this->userId,
  97. 'email' => $this->emailAddress,
  98. 'error' => $e->getMessage(),
  99. 'trace' => $e->getTraceAsString()
  100. ]);
  101. if (class_exists(ExportFailed::class)) {
  102. broadcast(new ExportFailed($this->userId, $e->getMessage()));
  103. }
  104. throw $e;
  105. } finally {
  106. if (isset($tempPath) && file_exists($tempPath)) {
  107. unlink($tempPath);
  108. }
  109. gc_collect_cycles();
  110. }
  111. }
  112. /**
  113. * Handle a job failure.
  114. */
  115. public function failed(\Throwable $exception)
  116. {
  117. Log::error('Export job failed permanently', [
  118. 'user_id' => $this->userId,
  119. 'email' => $this->emailAddress,
  120. 'attempts' => $this->attempts(),
  121. 'error' => $exception->getMessage()
  122. ]);
  123. try {
  124. Mail::raw(
  125. "Il tuo export della Prima Nota non è riuscito dopo {$this->tries} tentativi.\n\n" .
  126. "Errore: {$exception->getMessage()}\n\n" .
  127. "Contatta il supporto tecnico se il problema persiste.",
  128. function ($message) {
  129. $message->to($this->emailAddress)
  130. ->subject('Export Prima Nota - Errore');
  131. }
  132. );
  133. } catch (\Exception $e) {
  134. Log::error('Failed to send failure notification email', [
  135. 'user_id' => $this->userId,
  136. 'error' => $e->getMessage()
  137. ]);
  138. }
  139. }
  140. /**
  141. * Create Excel file with export data
  142. */
  143. private function createExcelFile($filePath)
  144. {
  145. $startTime = microtime(true);
  146. $spreadsheet = new Spreadsheet();
  147. $activeWorksheet = $spreadsheet->getActiveSheet();
  148. $activeWorksheet->setCellValue('A1', "Data");
  149. $activeWorksheet->setCellValue('B1', "Tipologia");
  150. $activeWorksheet->setCellValue('C1', "Causale");
  151. $activeWorksheet->setCellValue('D1', "Nominativo");
  152. $activeWorksheet->setCellValue('E1', "Stato");
  153. $activeWorksheet->setCellValue('F1', "Entrata");
  154. $activeWorksheet->setCellValue('G1', "Uscita");
  155. $activeWorksheet->setCellValue('H1', "Origine");
  156. $activeWorksheet->setCellValue('I1', "Destinazione");
  157. $activeWorksheet->setCellValue('J1', "Metodo di pagamento");
  158. $activeWorksheet->getStyle('A1:J1')->getFill()
  159. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  160. ->getStartColor()->setARGB('FF0C6197');
  161. $activeWorksheet->getStyle('A1:J1')->getFont()->getColor()->setARGB('FFFFFFFF');
  162. $idx = 2;
  163. foreach($this->records as $record)
  164. {
  165. if ($record->date != '')
  166. {
  167. $activeWorksheet->setCellValue('A' . $idx, date("d/m/Y", strtotime($record->date)));
  168. $activeWorksheet->setCellValue('B' . $idx, $record->commercial ? 'Commerciale' : 'Non commerciale');
  169. $activeWorksheet->setCellValue('C' . $idx, $record->causal_name);
  170. $activeWorksheet->setCellValue('D' . $idx, $record->type == 'IN' ? ($record->member->first_name . " " . $record->member->last_name) : @$record->supplier->name);
  171. $activeWorksheet->setCellValue('E' . $idx, $record->deleted ? 'Annullata' : '');
  172. $activeWorksheet->setCellValue('F' . $idx, $record->type == 'IN' ? formatPrice($record->amount) : '');
  173. $activeWorksheet->setCellValue('G' . $idx, $record->type == 'OUT' ? formatPrice($record->amount) : '');
  174. $activeWorksheet->setCellValue('H' . $idx, $record->type == 'OUT' ? $record->origin : '');
  175. $activeWorksheet->setCellValue('I' . $idx, $record->type == 'IN' ? $record->destination : '');
  176. $activeWorksheet->setCellValue('J' . $idx, $record->payment_method->name);
  177. }
  178. else
  179. {
  180. $activeWorksheet->setCellValue('A' . $idx, "Totali");
  181. $activeWorksheet->setCellValue('B' . $idx, "");
  182. $activeWorksheet->setCellValue('C' . $idx, "");
  183. $activeWorksheet->setCellValue('D' . $idx, "");
  184. $activeWorksheet->setCellValue('E' . $idx, "");
  185. $activeWorksheet->setCellValue('F' . $idx, formatPrice($record->total_in));
  186. $activeWorksheet->setCellValue('G' . $idx, formatPrice($record->total_out));
  187. $activeWorksheet->setCellValue('H' . $idx, "");
  188. $activeWorksheet->setCellValue('I' . $idx, "");
  189. $activeWorksheet->setCellValue('J' . $idx, "");
  190. }
  191. $idx++;
  192. }
  193. $activeWorksheet->getColumnDimension('A')->setWidth(10);
  194. $activeWorksheet->getColumnDimension('B')->setWidth(30);
  195. $activeWorksheet->getColumnDimension('C')->setWidth(30);
  196. $activeWorksheet->getColumnDimension('D')->setWidth(30);
  197. $activeWorksheet->getColumnDimension('E')->setWidth(10);
  198. $activeWorksheet->getColumnDimension('F')->setWidth(10);
  199. $activeWorksheet->getColumnDimension('G')->setWidth(10);
  200. $activeWorksheet->getColumnDimension('H')->setWidth(20);
  201. $activeWorksheet->getColumnDimension('I')->setWidth(20);
  202. $activeWorksheet->getColumnDimension('J')->setWidth(30);
  203. $activeWorksheet->getStyle('A' . ($idx - 1) . ':J' . ($idx - 1))->getFont()->setBold(true);
  204. try {
  205. $writerStart = microtime(true);
  206. $writer = new Xlsx($spreadsheet);
  207. $writer->save($filePath);
  208. $writerTime = microtime(true) - $writerStart;
  209. Log::info('Job createExcelFile: File saved successfully', [
  210. 'file_path' => $filePath,
  211. 'file_exists' => file_exists($filePath),
  212. 'file_size' => file_exists($filePath) ? filesize($filePath) : 0,
  213. 'writer_time' => $writerTime,
  214. 'total_time' => microtime(true) - $startTime,
  215. 'memory_peak' => memory_get_peak_usage(true)
  216. ]);
  217. } catch (\Exception $e) {
  218. Log::error('Job createExcelFile: Error during file save', [
  219. 'file_path' => $filePath,
  220. 'error' => $e->getMessage(),
  221. 'trace' => $e->getTraceAsString(),
  222. 'memory_usage' => memory_get_usage(true),
  223. 'time_elapsed' => microtime(true) - $startTime
  224. ]);
  225. throw $e;
  226. }
  227. Log::info('Job createExcelFile: Cleaning up memory');
  228. unset($spreadsheet, $activeWorksheet, $writer);
  229. gc_collect_cycles();
  230. Log::info('Job createExcelFile: Completed successfully', [
  231. 'total_time' => microtime(true) - $startTime,
  232. 'memory_after_cleanup' => memory_get_usage(true)
  233. ]);
  234. }
  235. private function createExcelFileOLD($filePath)
  236. {
  237. Log::info('Job createExcelFile: Starting Excel file creation', [
  238. 'file_path' => $filePath,
  239. //'export_data_count' => count($this->exportData),
  240. 'payments_count' => count($this->payments),
  241. 'memory_before' => memory_get_usage(true),
  242. 'time_limit' => ini_get('max_execution_time')
  243. ]);
  244. $startTime = microtime(true);
  245. Log::info('Job createExcelFile: Preparing column letters');
  246. $letters = array('F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA');
  247. Log::info('Job createExcelFile: Creating Spreadsheet object');
  248. $spreadsheet = new Spreadsheet();
  249. $activeWorksheet = $spreadsheet->getActiveSheet();
  250. $activeWorksheet->setTitle('Prima Nota');
  251. Log::info('Job createExcelFile: Setting document properties');
  252. $spreadsheet->getProperties()
  253. ->setCreator('Prima Nota System')
  254. ->setLastModifiedBy('Sistema')
  255. ->setTitle('Prima Nota Export')
  256. ->setSubject('Export Prima Nota')
  257. ->setDescription('Export dei dati Prima Nota dal ' . $this->dateRange['from'] . ' al ' . $this->dateRange['to']);
  258. Log::info('Job createExcelFile: Setting basic headers');
  259. $activeWorksheet->setCellValue('A1', "Data");
  260. $activeWorksheet->setCellValue('B1', "Causale");
  261. $activeWorksheet->setCellValue('C1', "Dettaglio Causale");
  262. $activeWorksheet->setCellValue('D1', "Nominativo");
  263. $activeWorksheet->setCellValue('E1', "Stato");
  264. Log::info('Job createExcelFile: Setting payment method headers and merging cells');
  265. $idx = 0;
  266. foreach ($this->payments as $p) {
  267. if ($idx >= count($letters)) {
  268. Log::warning('Job createExcelFile: Reached letter limit during header setup', [
  269. 'payment_index' => $idx,
  270. 'payment_name' => $p['name']
  271. ]);
  272. break;
  273. }
  274. Log::debug('Job createExcelFile: Setting payment header', [
  275. 'payment_name' => $p['name'],
  276. 'column_index' => $idx,
  277. 'column_letter' => $letters[$idx]
  278. ]);
  279. $activeWorksheet->setCellValue($letters[$idx] . '1', $p['name']);
  280. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx + 1] . '1');
  281. $idx += 2;
  282. }
  283. Log::info('Job createExcelFile: Setting sub-headers (row 2)');
  284. $activeWorksheet->setCellValue('A2', "");
  285. $activeWorksheet->setCellValue('B2', "");
  286. $activeWorksheet->setCellValue('C2', "");
  287. $activeWorksheet->setCellValue('D2', "");
  288. $activeWorksheet->setCellValue('E2', "");
  289. $idx = 0;
  290. foreach ($this->payments as $p) {
  291. if ($idx >= count($letters) - 1) {
  292. Log::warning('Job createExcelFile: Reached letter limit during sub-header setup', [
  293. 'payment_index' => $idx,
  294. 'payment_name' => $p['name']
  295. ]);
  296. break;
  297. }
  298. if ($p['type'] == 'ALL') {
  299. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  300. $idx++;
  301. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  302. $idx++;
  303. } elseif ($p['type'] == 'IN') {
  304. $activeWorksheet->setCellValue($letters[$idx] . '2', "Entrate");
  305. $idx++;
  306. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  307. $idx++;
  308. } elseif ($p['type'] == 'OUT') {
  309. $activeWorksheet->setCellValue($letters[$idx] . '2', "");
  310. $idx++;
  311. $activeWorksheet->setCellValue($letters[$idx] . '2', "Uscite");
  312. $idx++;
  313. }
  314. }
  315. Log::info('Job createExcelFile: Applying header styles');
  316. $maxCol = min(count($letters) - 1, count($this->payments) * 2 + 4);
  317. $lastColumnLetter = $letters[$maxCol];
  318. $activeWorksheet->getStyle('A1:' . $lastColumnLetter . '2')
  319. ->getFont()->setBold(true);
  320. $activeWorksheet->getStyle('A1:' . $lastColumnLetter . '1')
  321. ->getFill()
  322. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  323. ->getStartColor()->setARGB('FF0C6197');
  324. $activeWorksheet->getStyle('A1:' . $lastColumnLetter . '1')
  325. ->getFont()->getColor()->setARGB('FFFFFFFF');
  326. Log::info('Job createExcelFile: Starting data row processing', [
  327. //'total_export_records' => count($this->exportData),
  328. 'time_elapsed_so_far' => microtime(true) - $startTime
  329. ]);
  330. $count = 3;
  331. $batchSize = 500;
  332. $processed = 0;
  333. $batchNumber = 0;
  334. foreach ($this->exportData as $causal => $record) {
  335. if ($processed % 100 == 0) {
  336. Log::info('Job createExcelFile: Processing progress', [
  337. 'processed' => $processed,
  338. 'total' => count($this->exportData),
  339. 'current_row' => $count,
  340. 'memory_usage' => memory_get_usage(true),
  341. 'memory_peak' => memory_get_peak_usage(true),
  342. 'time_elapsed' => microtime(true) - $startTime
  343. ]);
  344. }
  345. try {
  346. $parts = explode("§", $causal);
  347. $d = $parts[0] ?? '';
  348. $c = $parts[1] ?? '';
  349. $j = $parts[2] ?? '';
  350. $det = $parts[3] ?? '';
  351. $deleted = $parts[4] ?? '';
  352. $detailParts = explode('|', $det);
  353. $exportDetail = count($detailParts) > 1 ? implode(', ', array_slice($detailParts, 1)) : $det;
  354. Log::debug('Job createExcelFile: Setting row data', [
  355. 'row' => $count,
  356. 'causal_parts' => count($parts)
  357. ]);
  358. $activeWorksheet->setCellValue('A' . $count, !empty($d) ? date("d/m/Y", strtotime($d)) : '');
  359. $activeWorksheet->setCellValue('B' . $count, $c);
  360. $activeWorksheet->setCellValue('C' . $count, $exportDetail);
  361. $activeWorksheet->setCellValue('D' . $count, $j);
  362. $stato = ($deleted === 'DELETED') ? 'ANNULLATA' : '';
  363. $activeWorksheet->setCellValue('E' . $count, $stato);
  364. if ($stato === 'ANNULLATA') {
  365. $activeWorksheet->getStyle('E' . $count)->getFont()->getColor()->setARGB('FFFF0000');
  366. }
  367. Log::debug('Job createExcelFile: Setting payment data for row', ['row' => $count]);
  368. $idx = 0;
  369. foreach ($this->payments as $p) {
  370. if ($idx >= count($letters) - 1) {
  371. Log::warning('Job createExcelFile: Reached letter limit during payment data', [
  372. 'row' => $count,
  373. 'payment_index' => $idx
  374. ]);
  375. break;
  376. }
  377. if (isset($record[$p['name']])) {
  378. $inValue = isset($record[$p['name']]["IN"]) ? $this->formatPrice($record[$p['name']]["IN"]) : "";
  379. $outValue = isset($record[$p['name']]["OUT"]) ? $this->formatPrice($record[$p['name']]["OUT"]) : "";
  380. $activeWorksheet->setCellValue($letters[$idx] . $count, $inValue);
  381. $idx++;
  382. $activeWorksheet->setCellValue($letters[$idx] . $count, $outValue);
  383. $idx++;
  384. } else {
  385. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  386. $idx++;
  387. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  388. $idx++;
  389. }
  390. }
  391. $count++;
  392. $processed++;
  393. if ($processed % $batchSize === 0) {
  394. $batchNumber++;
  395. Log::info('Job createExcelFile: Batch completed, running garbage collection', [
  396. 'batch_number' => $batchNumber,
  397. 'processed' => $processed,
  398. 'memory_before_gc' => memory_get_usage(true)
  399. ]);
  400. gc_collect_cycles();
  401. Log::info('Job createExcelFile: Garbage collection completed', [
  402. 'memory_after_gc' => memory_get_usage(true)
  403. ]);
  404. }
  405. } catch (\Exception $e) {
  406. Log::error('Job createExcelFile: Error processing data row', [
  407. 'row' => $count,
  408. 'processed_so_far' => $processed,
  409. 'causal' => $causal,
  410. 'error' => $e->getMessage(),
  411. 'trace' => $e->getTraceAsString()
  412. ]);
  413. throw $e;
  414. }
  415. }
  416. Log::info('Job createExcelFile: Data processing completed, adding totals row', [
  417. 'total_processed' => $processed,
  418. 'final_row' => $count,
  419. 'processing_time' => microtime(true) - $startTime
  420. ]);
  421. $count++;
  422. $activeWorksheet->setCellValue('A' . $count, 'TOTALE');
  423. $activeWorksheet->setCellValue('B' . $count, '');
  424. $activeWorksheet->setCellValue('C' . $count, '');
  425. $activeWorksheet->setCellValue('D' . $count, '');
  426. $activeWorksheet->setCellValue('E' . $count, '');
  427. Log::info('Job createExcelFile: Setting totals data');
  428. $idx = 0;
  429. foreach ($this->payments as $p) {
  430. if ($idx >= count($letters) - 1) {
  431. break;
  432. }
  433. if (isset($this->exportTotals[$p['name']])) {
  434. if ($p['type'] == 'ALL') {
  435. $activeWorksheet->setCellValue($letters[$idx] . $count, $this->formatPrice($this->exportTotals[$p['name']]["IN"] ?? 0));
  436. $idx++;
  437. $activeWorksheet->setCellValue($letters[$idx] . $count, $this->formatPrice($this->exportTotals[$p['name']]["OUT"] ?? 0));
  438. $idx++;
  439. } elseif ($p['type'] == 'IN') {
  440. $activeWorksheet->setCellValue($letters[$idx] . $count, $this->formatPrice($this->exportTotals[$p['name']]["IN"] ?? 0));
  441. $idx++;
  442. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  443. $idx++;
  444. } elseif ($p['type'] == 'OUT') {
  445. $activeWorksheet->setCellValue($letters[$idx] . $count, "");
  446. $idx++;
  447. $activeWorksheet->setCellValue($letters[$idx] . $count, $this->formatPrice($this->exportTotals[$p['name']]["OUT"] ?? 0));
  448. $idx++;
  449. }
  450. } else {
  451. $activeWorksheet->setCellValue($letters[$idx] . $count, "0,00");
  452. $idx++;
  453. $activeWorksheet->setCellValue($letters[$idx] . $count, "0,00");
  454. $idx++;
  455. }
  456. }
  457. Log::info('Job createExcelFile: Applying totals row styling');
  458. $activeWorksheet->getStyle('A' . $count . ':' . $lastColumnLetter . $count)
  459. ->getFont()->setBold(true);
  460. $activeWorksheet->getStyle('A' . $count . ':' . $lastColumnLetter . $count)
  461. ->getFill()
  462. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  463. ->getStartColor()->setARGB('FFF0F0F0');
  464. Log::info('Job createExcelFile: Setting column dimensions');
  465. $activeWorksheet->getColumnDimension('A')->setWidth(15);
  466. $activeWorksheet->getColumnDimension('B')->setWidth(25);
  467. $activeWorksheet->getColumnDimension('C')->setWidth(30);
  468. $activeWorksheet->getColumnDimension('D')->setWidth(25);
  469. $activeWorksheet->getColumnDimension('E')->setWidth(15);
  470. foreach ($letters as $l) {
  471. $activeWorksheet->getColumnDimension($l)->setWidth(15);
  472. }
  473. Log::info('Job createExcelFile: Setting freeze panes');
  474. $activeWorksheet->freezePane('A3');
  475. Log::info('Job createExcelFile: Creating Excel writer and saving file', [
  476. 'file_path' => $filePath,
  477. 'memory_before_save' => memory_get_usage(true)
  478. ]);
  479. try {
  480. $writerStart = microtime(true);
  481. $writer = new Xlsx($spreadsheet);
  482. $writer->save($filePath);
  483. $writerTime = microtime(true) - $writerStart;
  484. Log::info('Job createExcelFile: File saved successfully', [
  485. 'file_path' => $filePath,
  486. 'file_exists' => file_exists($filePath),
  487. 'file_size' => file_exists($filePath) ? filesize($filePath) : 0,
  488. 'writer_time' => $writerTime,
  489. 'total_time' => microtime(true) - $startTime,
  490. 'memory_peak' => memory_get_peak_usage(true)
  491. ]);
  492. } catch (\Exception $e) {
  493. Log::error('Job createExcelFile: Error during file save', [
  494. 'file_path' => $filePath,
  495. 'error' => $e->getMessage(),
  496. 'trace' => $e->getTraceAsString(),
  497. 'memory_usage' => memory_get_usage(true),
  498. 'time_elapsed' => microtime(true) - $startTime
  499. ]);
  500. throw $e;
  501. }
  502. Log::info('Job createExcelFile: Cleaning up memory');
  503. unset($spreadsheet, $activeWorksheet, $writer);
  504. gc_collect_cycles();
  505. Log::info('Job createExcelFile: Completed successfully', [
  506. 'total_time' => microtime(true) - $startTime,
  507. 'memory_after_cleanup' => memory_get_usage(true)
  508. ]);
  509. }
  510. // Generate column letters more efficiently
  511. private function generateColumnLetters($count)
  512. {
  513. $letters = [];
  514. for ($i = 0; $i < $count && $i < 100; $i++) { // Limit to prevent infinite loops
  515. if ($i < 26) {
  516. $letters[] = chr(65 + $i); // A-Z
  517. } else {
  518. $letters[] = 'A' . chr(65 + ($i - 26)); // AA, AB, AC...
  519. }
  520. }
  521. return $letters;
  522. }
  523. // Build headers more efficiently
  524. private function buildHeaders($activeWorksheet, $letters)
  525. {
  526. // Set basic headers
  527. $basicHeaders = [
  528. 'A1' => 'Data',
  529. 'B1' => 'Causale',
  530. 'C1' => 'Dettaglio Causale',
  531. 'D1' => 'Nominativo',
  532. 'E1' => 'Stato'
  533. ];
  534. // Use fromArray for faster header setting
  535. $activeWorksheet->fromArray(array_values($basicHeaders), null, 'A1', true);
  536. // Set payment method headers
  537. $paymentHeaders = [];
  538. $subHeaders = [];
  539. $idx = 5; // Start after basic headers (F column = index 5)
  540. foreach ($this->payments as $p) {
  541. if ($idx >= count($letters)) break;
  542. $paymentHeaders[] = $p['name'];
  543. $activeWorksheet->mergeCells($letters[$idx] . '1:' . $letters[$idx + 1] . '1');
  544. // Sub headers for row 2
  545. if ($p['type'] == 'ALL') {
  546. $subHeaders[$letters[$idx] . '2'] = 'Entrate';
  547. $subHeaders[$letters[$idx + 1] . '2'] = 'Uscite';
  548. } elseif ($p['type'] == 'IN') {
  549. $subHeaders[$letters[$idx] . '2'] = 'Entrate';
  550. $subHeaders[$letters[$idx + 1] . '2'] = '';
  551. } elseif ($p['type'] == 'OUT') {
  552. $subHeaders[$letters[$idx] . '2'] = '';
  553. $subHeaders[$letters[$idx + 1] . '2'] = 'Uscite';
  554. }
  555. $idx += 2;
  556. }
  557. // Set payment headers
  558. $col = 5;
  559. foreach ($paymentHeaders as $header) {
  560. if ($col < count($letters)) {
  561. $activeWorksheet->setCellValue($letters[$col] . '1', $header);
  562. }
  563. $col += 2;
  564. }
  565. // Set sub headers
  566. foreach ($subHeaders as $cell => $value) {
  567. $activeWorksheet->setCellValue($cell, $value);
  568. }
  569. }
  570. // Build data rows with batch processing
  571. private function buildDataRows($activeWorksheet, $letters)
  572. {
  573. $rowNum = 3; // Start after headers
  574. $batchSize = 100; // Process in smaller batches
  575. $batch = [];
  576. $batchCount = 0;
  577. foreach ($this->exportData as $causal => $record) {
  578. $parts = explode("§", $causal);
  579. $d = $parts[0] ?? '';
  580. $c = $parts[1] ?? '';
  581. $j = $parts[2] ?? '';
  582. $det = $parts[3] ?? '';
  583. $deleted = $parts[4] ?? '';
  584. $detailParts = explode('|', $det);
  585. $exportDetail = count($detailParts) > 1 ? implode(', ', array_slice($detailParts, 1)) : $det;
  586. // Prepare row data
  587. $rowData = [
  588. !empty($d) ? date("d/m/Y", strtotime($d)) : '',
  589. $c,
  590. $exportDetail,
  591. $j,
  592. ($deleted === 'DELETED') ? 'ANNULLATA' : ''
  593. ];
  594. // Add payment method values
  595. $idx = 0;
  596. foreach ($this->payments as $p) {
  597. if ($idx >= count($letters) - 6) break; // Leave room for basic columns
  598. if (isset($record[$p['name']])) {
  599. $inValue = isset($record[$p['name']]["IN"]) ? $this->formatPrice($record[$p['name']]["IN"]) : "";
  600. $outValue = isset($record[$p['name']]["OUT"]) ? $this->formatPrice($record[$p['name']]["OUT"]) : "";
  601. $rowData[] = $inValue;
  602. $rowData[] = $outValue;
  603. } else {
  604. $rowData[] = "";
  605. $rowData[] = "";
  606. }
  607. $idx += 2;
  608. }
  609. $batch[] = $rowData;
  610. $batchCount++;
  611. // Process batch when it reaches batch size
  612. if ($batchCount >= $batchSize) {
  613. $this->writeBatchToWorksheet($activeWorksheet, $batch, $rowNum);
  614. $rowNum += $batchCount;
  615. $batch = [];
  616. $batchCount = 0;
  617. gc_collect_cycles(); // Force garbage collection
  618. }
  619. }
  620. // Process remaining batch
  621. if (!empty($batch)) {
  622. $this->writeBatchToWorksheet($activeWorksheet, $batch, $rowNum);
  623. }
  624. }
  625. // Write batch data efficiently
  626. private function writeBatchToWorksheet($activeWorksheet, $batch, $startRow)
  627. {
  628. if (empty($batch)) return;
  629. try {
  630. // Use fromArray for much faster bulk insertion
  631. $activeWorksheet->fromArray($batch, null, 'A' . $startRow, true);
  632. // Apply conditional formatting for deleted records
  633. foreach ($batch as $index => $row) {
  634. $currentRow = $startRow + $index;
  635. if (isset($row[4]) && $row[4] === 'ANNULLATA') {
  636. $activeWorksheet->getStyle('E' . $currentRow)
  637. ->getFont()->getColor()->setARGB('FFFF0000');
  638. }
  639. }
  640. } catch (\Exception $e) {
  641. Log::error('Error writing batch to worksheet', [
  642. 'error' => $e->getMessage(),
  643. 'batch_size' => count($batch),
  644. 'start_row' => $startRow
  645. ]);
  646. // Fallback to individual cell setting
  647. foreach ($batch as $index => $row) {
  648. $currentRow = $startRow + $index;
  649. foreach ($row as $colIndex => $value) {
  650. $col = $this->getColumnLetter($colIndex);
  651. $activeWorksheet->setCellValue($col . $currentRow, $value);
  652. }
  653. }
  654. }
  655. }
  656. // Helper to get column letter by index
  657. private function getColumnLetter($index)
  658. {
  659. if ($index < 26) {
  660. return chr(65 + $index);
  661. } else {
  662. return 'A' . chr(65 + ($index - 26));
  663. }
  664. }
  665. // Build totals row efficiently
  666. private function buildTotalsRow($activeWorksheet, $letters)
  667. {
  668. $totalRows = count($this->exportData) + 3; // +3 for headers and spacing
  669. $totalRow = $totalRows + 1;
  670. $totalsData = ['TOTALE', '', '', '', ''];
  671. $idx = 0;
  672. foreach ($this->payments as $p) {
  673. if ($idx >= count($letters) - 6) break;
  674. if (isset($this->exportTotals[$p['name']])) {
  675. if ($p['type'] == 'ALL') {
  676. $totalsData[] = $this->formatPrice($this->exportTotals[$p['name']]["IN"] ?? 0);
  677. $totalsData[] = $this->formatPrice($this->exportTotals[$p['name']]["OUT"] ?? 0);
  678. } elseif ($p['type'] == 'IN') {
  679. $totalsData[] = $this->formatPrice($this->exportTotals[$p['name']]["IN"] ?? 0);
  680. $totalsData[] = "";
  681. } elseif ($p['type'] == 'OUT') {
  682. $totalsData[] = "";
  683. $totalsData[] = $this->formatPrice($this->exportTotals[$p['name']]["OUT"] ?? 0);
  684. }
  685. } else {
  686. $totalsData[] = "0,00";
  687. $totalsData[] = "0,00";
  688. }
  689. $idx += 2;
  690. }
  691. // Write totals row
  692. $activeWorksheet->fromArray([$totalsData], null, 'A' . $totalRow, true);
  693. }
  694. // Apply styles more efficiently
  695. private function applyStylesEfficiently($activeWorksheet, $letters)
  696. {
  697. $maxCol = min(count($letters) - 1, count($this->payments) * 2 + 4);
  698. $lastCol = $letters[$maxCol];
  699. $totalRows = count($this->exportData) + 4; // +4 for headers, spacing, and totals
  700. // Apply header styles
  701. $headerRange = 'A1:' . $lastCol . '2';
  702. $activeWorksheet->getStyle($headerRange)->getFont()->setBold(true);
  703. // Apply header background
  704. $activeWorksheet->getStyle('A1:' . $lastCol . '1')
  705. ->getFill()
  706. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  707. ->getStartColor()->setARGB('FF0C6197');
  708. $activeWorksheet->getStyle('A1:' . $lastCol . '1')
  709. ->getFont()->getColor()->setARGB('FFFFFFFF');
  710. // Apply totals row styles
  711. $totalsRange = 'A' . $totalRows . ':' . $lastCol . $totalRows;
  712. $activeWorksheet->getStyle($totalsRange)->getFont()->setBold(true);
  713. $activeWorksheet->getStyle($totalsRange)
  714. ->getFill()
  715. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  716. ->getStartColor()->setARGB('FFF0F0F0');
  717. }
  718. // Set column dimensions efficiently
  719. private function setColumnDimensions($activeWorksheet, $letters)
  720. {
  721. $dimensions = [
  722. 'A' => 15,
  723. 'B' => 25,
  724. 'C' => 30,
  725. 'D' => 25,
  726. 'E' => 15
  727. ];
  728. foreach ($dimensions as $col => $width) {
  729. $activeWorksheet->getColumnDimension($col)->setWidth($width);
  730. }
  731. // Set payment method column widths
  732. for ($i = 5; $i < count($letters) && $i < 50; $i++) { // Limit to prevent excessive loops
  733. $activeWorksheet->getColumnDimension($letters[$i])->setWidth(15);
  734. }
  735. }
  736. /**
  737. * Format price for display
  738. */
  739. private function formatPrice($amount)
  740. {
  741. return number_format($amount, 2, ',', '.');
  742. }
  743. /**
  744. * Get description of applied filters
  745. */
  746. private function getFiltersDescription()
  747. {
  748. $descriptions = [];
  749. if (!empty($this->filters['member'])) {
  750. $descriptions[] = "Utente: {$this->filters['member']}";
  751. }
  752. if (!empty($this->filters['causals'])) {
  753. $descriptions[] = "Causali: " . (is_array($this->filters['causals']) ? implode(', ', $this->filters['causals']) : $this->filters['causals']);
  754. }
  755. if (!empty($this->filters['payment_methods'])) {
  756. $descriptions[] = "Metodi di pagamento: " . (is_array($this->filters['payment_methods']) ? implode(', ', $this->filters['payment_methods']) : $this->filters['payment_methods']);
  757. }
  758. if (!empty($this->filters['origins'])) {
  759. $descriptions[] = "Origini: " . (is_array($this->filters['origins']) ? implode(', ', $this->filters['origins']) : $this->filters['origins']);
  760. }
  761. if (!empty($this->filters['destinations'])) {
  762. $descriptions[] = "Destinazioni: " . (is_array($this->filters['destinations']) ? implode(', ', $this->filters['destinations']) : $this->filters['destinations']);
  763. }
  764. return empty($descriptions) ? 'Nessun filtro applicato' : implode(' | ', $descriptions);
  765. }
  766. public function executeWithTimeoutMonitoring($callback, $description = 'Operation')
  767. {
  768. $startTime = microtime(true);
  769. $maxExecutionTime = ini_get('max_execution_time');
  770. Log::info("Starting monitored operation: {$description}", [
  771. 'start_time' => $startTime,
  772. 'max_execution_time' => $maxExecutionTime,
  773. 'memory_start' => memory_get_usage(true)
  774. ]);
  775. try {
  776. // Execute every 5 seconds to monitor progress
  777. $lastCheck = $startTime;
  778. $result = null;
  779. // For non-blocking operations, we can't easily interrupt,
  780. // but we can log progress
  781. register_tick_function(function () use ($startTime, $maxExecutionTime, $description, &$lastCheck) {
  782. $currentTime = microtime(true);
  783. if ($currentTime - $lastCheck >= 5) { // Log every 5 seconds
  784. $elapsed = $currentTime - $startTime;
  785. $remaining = $maxExecutionTime > 0 ? $maxExecutionTime - $elapsed : 'unlimited';
  786. Log::info("Operation progress: {$description}", [
  787. 'elapsed_time' => $elapsed,
  788. 'remaining_time' => $remaining,
  789. 'memory_current' => memory_get_usage(true),
  790. 'memory_peak' => memory_get_peak_usage(true)
  791. ]);
  792. if ($maxExecutionTime > 0 && $elapsed > ($maxExecutionTime * 0.8)) {
  793. Log::warning("Operation approaching timeout: {$description}", [
  794. 'elapsed_time' => $elapsed,
  795. 'max_time' => $maxExecutionTime,
  796. 'percentage_used' => ($elapsed / $maxExecutionTime) * 100
  797. ]);
  798. }
  799. $lastCheck = $currentTime;
  800. }
  801. });
  802. declare(ticks=1000);
  803. $result = $callback();
  804. $totalTime = microtime(true) - $startTime;
  805. Log::info("Operation completed successfully: {$description}", [
  806. 'total_time' => $totalTime,
  807. 'memory_peak' => memory_get_peak_usage(true)
  808. ]);
  809. return $result;
  810. } catch (\Exception $e) {
  811. $totalTime = microtime(true) - $startTime;
  812. Log::error("Operation failed: {$description}", [
  813. 'error' => $e->getMessage(),
  814. 'total_time' => $totalTime,
  815. 'memory_peak' => memory_get_peak_usage(true),
  816. 'trace' => $e->getTraceAsString()
  817. ]);
  818. throw $e;
  819. }
  820. }
  821. private function checkTimeoutRisk($operationName, $startTime = null)
  822. {
  823. if ($startTime === null) {
  824. $startTime = $_SERVER['REQUEST_TIME_FLOAT'] ?? microtime(true);
  825. }
  826. $maxExecutionTime = ini_get('max_execution_time');
  827. if ($maxExecutionTime <= 0) {
  828. return false; // No limit set
  829. }
  830. $elapsed = microtime(true) - $startTime;
  831. $remaining = $maxExecutionTime - $elapsed;
  832. $percentageUsed = ($elapsed / $maxExecutionTime) * 100;
  833. Log::info("Timeout check: {$operationName}", [
  834. 'elapsed_time' => $elapsed,
  835. 'remaining_time' => $remaining,
  836. 'percentage_used' => $percentageUsed,
  837. 'memory_usage' => memory_get_usage(true)
  838. ]);
  839. if ($percentageUsed > 80) {
  840. Log::warning("High timeout risk detected: {$operationName}", [
  841. 'elapsed_time' => $elapsed,
  842. 'remaining_time' => $remaining,
  843. 'percentage_used' => $percentageUsed
  844. ]);
  845. return true;
  846. }
  847. return false;
  848. }
  849. // SOLUTION 8: Log configuration and environment info at start
  850. public function logEnvironmentInfo()
  851. {
  852. Log::info('=== EXPORT ENVIRONMENT INFO ===', [
  853. 'php_version' => PHP_VERSION,
  854. 'memory_limit' => ini_get('memory_limit'),
  855. 'max_execution_time' => ini_get('max_execution_time'),
  856. 'max_input_time' => ini_get('max_input_time'),
  857. 'post_max_size' => ini_get('post_max_size'),
  858. 'upload_max_filesize' => ini_get('upload_max_filesize'),
  859. 'default_socket_timeout' => ini_get('default_socket_timeout'),
  860. 'current_memory_usage' => memory_get_usage(true),
  861. 'current_memory_peak' => memory_get_peak_usage(true),
  862. 'server_time' => date('Y-m-d H:i:s'),
  863. 'timezone' => date_default_timezone_get(),
  864. 'sapi_name' => php_sapi_name(),
  865. 'loaded_extensions' => get_loaded_extensions()
  866. ]);
  867. }
  868. }