ExportPrimaNota.php 40 KB

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