reports.blade.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  1. {{-- resources/views/livewire/reports.blade.php --}}
  2. <div>
  3. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  4. <link rel="stylesheet" href="{{ asset('css/chart-reports.css') }}">
  5. <div class="dashboard-container">
  6. <div class="controls-section">
  7. <div class="control-group">
  8. <label for="season-filter">Stagione di Riferimento:</label>
  9. <select class="form-select" wire:model="seasonFilter" wire:change="updateCharts">
  10. @foreach($this->getAvailableSeasons() as $season)
  11. <option value="{{ $season }}">{{ $season }}</option>
  12. @endforeach
  13. </select>
  14. </div>
  15. </div>
  16. @php
  17. $summary = $this->getYearlySummary();
  18. @endphp
  19. <div class="summary-cards">
  20. <div class="summary-card income">
  21. <h3>Entrate Totali</h3>
  22. <div class="value">€{{ number_format($summary['totalIncome'], 2, ',', '.') }}</div>
  23. </div>
  24. <div class="summary-card expense">
  25. <h3>Uscite Totali</h3>
  26. <div class="value">€{{ number_format($summary['totalExpenses'], 2, ',', '.') }}</div>
  27. </div>
  28. <div class="summary-card delta {{ $summary['delta'] < 0 ? 'negative' : '' }}">
  29. <h3>Bilancio Netto</h3>
  30. <div class="value">€{{ number_format($summary['delta'], 2, ',', '.') }}</div>
  31. </div>
  32. </div>
  33. <div wire:ignore>
  34. <div class="chart-row">
  35. <div class="chart-card">
  36. <div class="chart-header">
  37. <h3 class="chart-title">Entrate e Uscite Mensili - <span
  38. id="monthly-season-title">{{ $seasonFilter }}</span></h3>
  39. </div>
  40. <div class="chart-body">
  41. <div style="display: grid; grid-template-columns: 1fr 300px; align-items: start;">
  42. <div class="chart-container">
  43. <canvas id="monthly-chart-{{ str_replace('-', '', $seasonFilter) }}"></canvas>
  44. </div>
  45. <div class="monthly-table-container" id="monthly-table">
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. <div class="chart-row">
  52. <div class="chart-card">
  53. <div class="chart-header">
  54. <h3 class="chart-title">Causali Performanti - <span
  55. id="causals-season-title">{{ $seasonFilter }}</span></h3>
  56. </div>
  57. <div class="chart-body">
  58. <div style="display: grid; grid-template-columns: 1fr 700px; gap: 1rem; align-items: start;">
  59. <div class="causals-table-container" id="causals-table">
  60. </div>
  61. <div class="chart-container">
  62. <canvas id="causals-chart-{{ str_replace('-', '', $seasonFilter) }}"></canvas>
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. <div class="chart-row">
  69. <div class="chart-card">
  70. <div class="chart-header">
  71. <h3 class="chart-title">Tesserati per Stagione</h3>
  72. </div>
  73. <div class="chart-body">
  74. <div style="display: grid; grid-template-columns: 1fr 300px; gap: 1rem; align-items: start;">
  75. <div class="chart-container">
  76. <canvas id="members-chart-{{ str_replace('-', '', $seasonFilter) }}"></canvas>
  77. </div>
  78. <div class="members-table-container" id="members-table">
  79. </div>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. <div class="chart-row">
  86. <div class="chart-card modern-course-card">
  87. <div class="chart-header">
  88. <h3 class="chart-title">Analisi Corsi</h3>
  89. </div>
  90. <div class="chart-body">
  91. <div class="course-controls">
  92. <div class="control-group">
  93. <label>Seleziona Corso ({{ $seasonFilter }}):</label>
  94. <select class="form-select modern-select" wire:model.live="selectedCourse">
  95. <option value="">Seleziona un Corso</option>
  96. @foreach($this->getCoursesForSelect() as $course)
  97. <option value="{{ $course['id'] }}">{{ $course['full_name'] }}</option>
  98. @endforeach
  99. </select>
  100. </div>
  101. </div>
  102. @if($selectedCourse)
  103. <div wire:ignore wire:key="course-chart-{{ $seasonFilter }}-{{ $selectedCourse }}">
  104. <div class="modern-chart-layout">
  105. <div class="course-delta-table"
  106. id="course-delta-table-{{ str_replace('-', '', $seasonFilter) }}-{{ $selectedCourse }}">
  107. </div>
  108. <div class="modern-chart-container">
  109. <canvas
  110. id="courses-chart-{{ str_replace('-', '', $seasonFilter) }}-{{ $selectedCourse }}"></canvas>
  111. </div>
  112. </div>
  113. </div>
  114. @else
  115. <div class="chart-placeholder">
  116. <div style="text-align: center;">
  117. <div style="font-size: 3rem; margin-bottom: 1rem; opacity: 0.3;">📊</div>
  118. <p style="font-size: 1.25rem; font-weight: 600; margin: 0;">Seleziona un corso per
  119. visualizzare il grafico</p>
  120. <p style="font-size: 1rem; opacity: 0.7; margin-top: 0.5rem;">Usa il menu a tendina sopra
  121. per scegliere un corso</p>
  122. </div>
  123. </div>
  124. @endif
  125. </div>
  126. </div>
  127. </div>
  128. </div>
  129. <script>
  130. window.ReportsChartManager = window.ReportsChartManager || {
  131. charts: {},
  132. currentSeason: null,
  133. destroyChart: function (chartId) {
  134. if (this.charts[chartId]) {
  135. this.charts[chartId].destroy();
  136. delete this.charts[chartId];
  137. }
  138. },
  139. destroyAllCharts: function () {
  140. Object.keys(this.charts).forEach(chartId => {
  141. this.destroyChart(chartId);
  142. });
  143. },
  144. destroySeasonCharts: function (oldSeasonKey) {
  145. const chartsToDestroy = Object.keys(this.charts).filter(chartId =>
  146. chartId.includes(oldSeasonKey)
  147. );
  148. chartsToDestroy.forEach(chartId => this.destroyChart(chartId));
  149. },
  150. updateMainCharts: function () {
  151. console.log('=== updateMainCharts called ===');
  152. const seasonFilter = @this.get('seasonFilter');
  153. const monthlyTitle = document.getElementById('monthly-season-title');
  154. const causalsTitle = document.getElementById('causals-season-title');
  155. if (monthlyTitle) {
  156. monthlyTitle.textContent = seasonFilter;
  157. }
  158. if (causalsTitle) {
  159. causalsTitle.textContent = seasonFilter;
  160. }
  161. const originalSeasonKey = '{{ str_replace('-', '', $seasonFilter) }}';
  162. console.log('Using original season key for canvas IDs:', originalSeasonKey);
  163. @this.call('getMonthlyTotals').then(monthlyData => {
  164. console.log('Got monthly data:', monthlyData);
  165. this.createMonthlyChart(originalSeasonKey, monthlyData);
  166. this.updateMonthlyTable(monthlyData);
  167. });
  168. @this.call('getTopCausalsByAmount').then(causalsData => {
  169. console.log('Got causals data:', causalsData);
  170. this.createCausalsChart(originalSeasonKey, causalsData);
  171. });
  172. @this.call('getTesseratiData').then(membersData => {
  173. console.log('Got members data:', membersData);
  174. this.createMembersChart(originalSeasonKey, membersData);
  175. this.updateMembersTable(membersData);
  176. });
  177. },
  178. forceUpdateCharts: function () {
  179. console.log('Force updating charts...');
  180. this.currentSeason = null;
  181. this.updateMainCharts();
  182. },
  183. createMonthlyChart: function (seasonKey, monthlyData) {
  184. const chartId = `monthly-chart-${seasonKey}`;
  185. const canvas = document.getElementById(chartId);
  186. if (!canvas) {
  187. console.error('Canvas not found for ID:', chartId);
  188. return;
  189. }
  190. this.destroyChart(chartId);
  191. const ctx = canvas.getContext('2d');
  192. const incomeGradient = ctx.createLinearGradient(0, 0, 0, 400);
  193. incomeGradient.addColorStop(0, 'rgba(0, 184, 148, 1)');
  194. incomeGradient.addColorStop(1, 'rgba(0, 184, 148, 1)');
  195. const expenseGradient = ctx.createLinearGradient(0, 0, 0, 400);
  196. expenseGradient.addColorStop(0, 'rgba(255, 107, 107, 1)');
  197. expenseGradient.addColorStop(1, 'rgba(255, 107, 107, 1)');
  198. this.charts[chartId] = new Chart(ctx, {
  199. type: 'bar',
  200. data: {
  201. labels: monthlyData.labels,
  202. datasets: [
  203. {
  204. label: 'Entrate',
  205. data: monthlyData.datasets[0].data,
  206. backgroundColor: incomeGradient,
  207. borderColor: '#00b894',
  208. borderWidth: 2,
  209. borderRadius: 12,
  210. borderSkipped: false,
  211. },
  212. {
  213. label: 'Uscite',
  214. data: monthlyData.datasets[1].data,
  215. backgroundColor: expenseGradient,
  216. borderColor: '#ff6b6b',
  217. borderWidth: 2,
  218. borderRadius: 12,
  219. borderSkipped: false,
  220. }
  221. ]
  222. },
  223. options: {
  224. responsive: true,
  225. maintainAspectRatio: false,
  226. plugins: {
  227. legend: {
  228. position: 'top',
  229. labels: {
  230. usePointStyle: true,
  231. padding: 20,
  232. font: { weight: '500' }
  233. }
  234. },
  235. tooltip: {
  236. backgroundColor: 'rgba(255, 255, 255,1)',
  237. titleColor: '#212529',
  238. bodyColor: '#495057',
  239. borderColor: '#e9ecef',
  240. borderWidth: 12,
  241. cornerRadius: 8,
  242. callbacks: {
  243. label: function (context) {
  244. return context.dataset.label + ': €' +
  245. new Intl.NumberFormat('it-IT').format(context.parsed.y);
  246. }
  247. }
  248. }
  249. },
  250. scales: {
  251. x: {
  252. grid: { display: false },
  253. ticks: { font: { weight: '500' } }
  254. },
  255. y: {
  256. beginAtZero: true,
  257. grid: { color: 'rgba(0, 0, 0, 0.05)' },
  258. ticks: {
  259. callback: function (value) {
  260. return '€' + new Intl.NumberFormat('it-IT').format(value);
  261. }
  262. }
  263. }
  264. },
  265. elements: {
  266. bar: {
  267. borderRadius: {
  268. topLeft: 12,
  269. topRight: 12,
  270. bottomLeft: 0,
  271. bottomRight: 0
  272. }
  273. }
  274. },
  275. animation: {
  276. duration: 1000,
  277. easing: 'easeOutQuart'
  278. }
  279. }
  280. });
  281. },
  282. createCausalsChart: function (seasonKey, causalsData) {
  283. const chartId = `causals-chart-${seasonKey}`;
  284. const canvas = document.getElementById(chartId);
  285. if (!canvas) return;
  286. this.destroyChart(chartId);
  287. const ctx = canvas.getContext('2d');
  288. const colors = [
  289. 'rgba(59, 91, 219, 0.8)',
  290. 'rgba(0, 184, 148, 0.8)',
  291. 'rgba(34, 184, 207, 0.8)',
  292. 'rgba(255, 212, 59, 0.8)',
  293. 'rgba(255, 107, 107, 0.8)',
  294. 'rgba(142, 68, 173, 0.8)',
  295. 'rgba(230, 126, 34, 0.8)',
  296. 'rgba(149, 165, 166, 0.8)',
  297. 'rgba(241, 196, 15, 0.8)',
  298. 'rgba(231, 76, 60, 0.8)'
  299. ];
  300. const dataValues = causalsData.inData.map(item => parseFloat(item.value));
  301. const total = dataValues.reduce((sum, value) => sum + value, 0);
  302. this.charts[chartId] = new Chart(ctx, {
  303. type: 'doughnut',
  304. data: {
  305. labels: causalsData.inLabels,
  306. datasets: [{
  307. label: 'Importo',
  308. data: dataValues,
  309. backgroundColor: colors,
  310. borderColor: colors.map(color => color.replace('0.8', '1')),
  311. borderWidth: 2,
  312. hoverOffset: 8
  313. }]
  314. },
  315. options: {
  316. responsive: true,
  317. maintainAspectRatio: false,
  318. cutout: '30%',
  319. layout: {
  320. padding: {
  321. top: 10,
  322. right: 10,
  323. bottom: 10,
  324. left: 10
  325. }
  326. },
  327. plugins: {
  328. legend: {
  329. display: false
  330. },
  331. tooltip: {
  332. backgroundColor: 'rgba(255, 255, 255, 0.95)',
  333. titleColor: '#212529',
  334. bodyColor: '#495057',
  335. borderColor: '#e9ecef',
  336. borderWidth: 1,
  337. cornerRadius: 8,
  338. titleFont: {
  339. size: 13,
  340. weight: 'bold'
  341. },
  342. bodyFont: {
  343. size: 12,
  344. weight: '500'
  345. },
  346. padding: 12,
  347. callbacks: {
  348. title: function (context) {
  349. return context[0].label;
  350. },
  351. label: function (context) {
  352. const value = context.raw;
  353. const percentage = total > 0 ? ((value / total) * 100).toFixed(1) : 0;
  354. return [
  355. `Importo: €${new Intl.NumberFormat('it-IT', {
  356. minimumFractionDigits: 2,
  357. maximumFractionDigits: 2
  358. }).format(value)}`,
  359. `Percentuale: ${percentage}%`
  360. ];
  361. }
  362. }
  363. },
  364. },
  365. animation: {
  366. animateRotate: true,
  367. duration: 1000
  368. }
  369. }
  370. });
  371. this.updateCausalsTable(causalsData, dataValues, total);
  372. },
  373. updateCausalsTable: function (causalsData, dataValues, total) {
  374. const container = document.getElementById('causals-table');
  375. if (!container) return;
  376. const colors = [
  377. '#3b5bdb', '#00b894', '#22b8cf', '#ffd43b', '#ff6b6b',
  378. '#8e44ad', '#e67e22', '#95a5a6', '#f1c40f', '#e74c3c'
  379. ];
  380. let tableHtml = `
  381. <div class="causals-table compact">
  382. <div class="table-header">
  383. <div class="table-cell causale">Causale</div>
  384. <div class="table-cell euro">Importo</div>
  385. <div class="table-cell percent">%</div>
  386. </div>
  387. `;
  388. causalsData.inLabels.forEach((label, index) => {
  389. const value = dataValues[index] || 0;
  390. const percentage = total > 0 ? ((value / total) * 100).toFixed(1) : 0;
  391. const color = colors[index % colors.length];
  392. tableHtml += `
  393. <div class="table-row">
  394. <div class="table-cell causale">
  395. <span class="causale-indicator" style="background-color: ${color}"></span>
  396. ${label}
  397. </div>
  398. <div class="table-cell euro">€${new Intl.NumberFormat('it-IT', {
  399. minimumFractionDigits: 2,
  400. maximumFractionDigits: 2
  401. }).format(value)}</div>
  402. <div class="table-cell percent">${percentage}%</div>
  403. </div>
  404. `;
  405. });
  406. tableHtml += '</div>';
  407. container.innerHTML = tableHtml;
  408. },
  409. createMembersChart: function (seasonKey, membersData) {
  410. const chartId = `members-chart-${seasonKey}`;
  411. const canvas = document.getElementById(chartId);
  412. if (!canvas) return;
  413. this.destroyChart(chartId);
  414. const ctx = canvas.getContext('2d');
  415. const gradient = ctx.createLinearGradient(0, 0, 0, 400);
  416. gradient.addColorStop(0, 'rgba(59, 91, 219, 0.3)');
  417. gradient.addColorStop(1, 'rgba(59, 91, 219, 0.05)');
  418. const processedDatasets = membersData.datasets.map((dataset, index) => {
  419. if (dataset.label === 'Totale Membri Tesserati') {
  420. return {
  421. ...dataset,
  422. backgroundColor: gradient,
  423. borderColor: '#3b5bdb',
  424. borderWidth: 3,
  425. pointBackgroundColor: '#3b5bdb',
  426. pointBorderColor: '#ffffff',
  427. pointBorderWidth: 2,
  428. pointRadius: 6,
  429. pointHoverRadius: 8,
  430. type: 'line',
  431. order: 1,
  432. fill: true
  433. };
  434. } else {
  435. return {
  436. ...dataset,
  437. borderWidth: 2,
  438. pointRadius: 4,
  439. pointHoverRadius: 6,
  440. pointBorderColor: '#ffffff',
  441. pointBorderWidth: 1,
  442. type: 'line',
  443. order: 2,
  444. fill: false,
  445. backgroundColor: 'transparent'
  446. };
  447. }
  448. });
  449. this.charts[chartId] = new Chart(ctx, {
  450. type: 'line',
  451. data: {
  452. labels: membersData.labels,
  453. datasets: processedDatasets
  454. },
  455. options: {
  456. responsive: true,
  457. maintainAspectRatio: false,
  458. interaction: {
  459. mode: 'index',
  460. intersect: false,
  461. },
  462. plugins: {
  463. legend: {
  464. display: true,
  465. position: 'top',
  466. labels: {
  467. usePointStyle: true,
  468. padding: 15,
  469. font: { weight: '500', size: 12 }
  470. }
  471. },
  472. tooltip: {
  473. backgroundColor: 'rgba(255, 255, 255, 0.95)',
  474. titleColor: '#212529',
  475. bodyColor: '#495057',
  476. borderColor: '#e9ecef',
  477. borderWidth: 1,
  478. cornerRadius: 8,
  479. callbacks: {
  480. title: function (context) {
  481. return 'Stagione: ' + context[0].label;
  482. },
  483. label: function (context) {
  484. return context.dataset.label + ': ' + context.parsed.y;
  485. }
  486. }
  487. }
  488. },
  489. scales: {
  490. x: {
  491. grid: { display: false },
  492. ticks: {
  493. font: { weight: '500' }
  494. }
  495. },
  496. y: {
  497. beginAtZero: true,
  498. grid: { color: 'rgba(0, 0, 0, 0.05)' },
  499. ticks: {
  500. precision: 0,
  501. callback: function (value) {
  502. return Math.floor(value); // Ensure integer values
  503. }
  504. }
  505. }
  506. },
  507. animation: {
  508. duration: 1000,
  509. easing: 'easeOutQuart'
  510. }
  511. }
  512. });
  513. },
  514. updateMonthlyTable: function (monthlyData) {
  515. const container = document.getElementById('monthly-table');
  516. if (!container) return;
  517. const incomeData = monthlyData.datasets[0].data;
  518. const expenseData = monthlyData.datasets[1].data;
  519. const monthNames = monthlyData.labels;
  520. let tableHtml = `
  521. <div class="monthly-table">
  522. <div class="table-header">
  523. <div class="table-cell month">Mese</div>
  524. <div class="table-cell">Entrate</div>
  525. <div class="table-cell">Uscite</div>
  526. <div class="table-cell">Delta</div>
  527. </div>
  528. `;
  529. monthNames.forEach((month, index) => {
  530. const income = parseFloat(incomeData[index] || 0);
  531. const expense = parseFloat(expenseData[index] || 0);
  532. const net = income - expense;
  533. const rowClass = net < 0 ? 'negative' : (net > 0 ? 'positive' : 'neutral');
  534. tableHtml += `
  535. <div class="table-row ${rowClass}">
  536. <div class="table-cell month-name">${month}</div>
  537. <div class="table-cell income">€${new Intl.NumberFormat('it-IT').format(income)}</div>
  538. <div class="table-cell expense">€${new Intl.NumberFormat('it-IT').format(expense)}</div>
  539. <div class="table-cell net">€${new Intl.NumberFormat('it-IT').format(net)}</div>
  540. </div>
  541. `;
  542. });
  543. tableHtml += '</div>';
  544. container.innerHTML = tableHtml;
  545. },
  546. updateMembersTable: function (membersData) {
  547. const container = document.getElementById('members-table');
  548. if (!container) return;
  549. const seasonLabels = membersData.labels;
  550. const memberCounts = membersData.datasets[0].data;
  551. let tableHtml = `
  552. <div class="members-table">
  553. <div class="table-header">
  554. <div class="table-cell">Stagione</div>
  555. <div class="table-cell">Tesserati</div>
  556. <div class="table-cell">Variazione</div>
  557. </div>
  558. `;
  559. seasonLabels.forEach((season, index) => {
  560. const current = parseInt(memberCounts[index] || 0);
  561. const previous = index > 0 ? parseInt(memberCounts[index - 1] || 0) : 0;
  562. const variation = index > 0 ? current - previous : 0;
  563. const variationPercent = previous > 0 ? Math.round((variation / previous) * 100 * 10) / 10 : 0;
  564. const rowClass = variation > 0 ? 'positive' : (variation < 0 ? 'negative' : 'neutral');
  565. let variationText = '—';
  566. if (index > 0) {
  567. if (variation > 0) {
  568. variationText = `<span class="variation-positive">+${variation} (+${variationPercent}%)</span>`;
  569. } else if (variation < 0) {
  570. variationText = `<span class="variation-negative">${variation} (${variationPercent}%)</span>`;
  571. } else {
  572. variationText = `<span class="variation-neutral">${variation}</span>`;
  573. }
  574. }
  575. tableHtml += `
  576. <div class="table-row ${rowClass}">
  577. <div class="table-cell season-name">${season}</div>
  578. <div class="table-cell members-count">${new Intl.NumberFormat('it-IT').format(current)}</div>
  579. <div class="table-cell variation">${variationText}</div>
  580. </div>
  581. `;
  582. });
  583. tableHtml += '</div>';
  584. container.innerHTML = tableHtml;
  585. },
  586. createCourseChart: function () {
  587. console.log('Creating course chart...');
  588. const seasonFilter = '{{ $seasonFilter }}';
  589. const selectedCourse = '{{ $selectedCourse ?? '' }}';
  590. const seasonKey = '{{ str_replace('-', '', $seasonFilter) }}';
  591. console.log('Selected course:', selectedCourse, 'for season:', seasonFilter);
  592. if (!selectedCourse || selectedCourse.trim() === '') {
  593. console.log('No course selected, skipping chart creation');
  594. return;
  595. }
  596. const chartId = `courses-chart-${seasonKey}-${selectedCourse}`;
  597. const canvas = document.getElementById(chartId);
  598. if (!canvas) return;
  599. this.destroyChart(chartId);
  600. const courseData = @json($this->getCourseMonthlyEarnings());
  601. const ctx = canvas.getContext('2d');
  602. this.charts[chartId] = new Chart(ctx, {
  603. type: 'bar',
  604. data: {
  605. labels: courseData.labels,
  606. datasets: courseData.datasets.map(dataset => {
  607. if (dataset.type === 'line') {
  608. return {
  609. ...dataset,
  610. type: 'line',
  611. fill: false,
  612. backgroundColor: 'transparent'
  613. };
  614. }
  615. return dataset;
  616. })
  617. },
  618. options: {
  619. responsive: true,
  620. maintainAspectRatio: false,
  621. interaction: {
  622. mode: 'index',
  623. intersect: false,
  624. },
  625. scales: {
  626. x: {
  627. grid: { display: false },
  628. ticks: { font: { weight: '500' } }
  629. },
  630. y: {
  631. beginAtZero: true,
  632. grid: {
  633. color: 'rgba(0, 0, 0, 1)',
  634. borderDash: [5, 5]
  635. },
  636. ticks: {
  637. callback: function (value) {
  638. return '€' + new Intl.NumberFormat('it-IT').format(value);
  639. }
  640. }
  641. }
  642. },
  643. plugins: {
  644. legend: {
  645. display: true,
  646. position: 'top',
  647. labels: {
  648. usePointStyle: true,
  649. padding: 20,
  650. font: { weight: '500' }
  651. }
  652. },
  653. tooltip: {
  654. backgroundColor: 'rgba(255, 255, 255, 1)',
  655. titleColor: '#212529',
  656. bodyColor: '#495057',
  657. borderColor: '#e9ecef',
  658. borderWidth: 1,
  659. cornerRadius: 8,
  660. callbacks: {
  661. label: function (context) {
  662. return context.dataset.label + ': €' +
  663. new Intl.NumberFormat('it-IT').format(context.parsed.y);
  664. }
  665. }
  666. }
  667. },
  668. animation: {
  669. duration: 1000,
  670. easing: 'easeOutQuart'
  671. }
  672. }
  673. });
  674. },
  675. createCourseChartWithValue: function (selectedCourseValue) {
  676. console.log('Creating modern course chart with value:', selectedCourseValue);
  677. const seasonFilter = '{{ $seasonFilter }}';
  678. const seasonKey = '{{ str_replace('-', '', $seasonFilter) }}';
  679. const chartId = `courses-chart-${seasonKey}-${selectedCourseValue}`;
  680. const tableId = `course-delta-table-${seasonKey}-${selectedCourseValue}`;
  681. let canvas = document.getElementById(chartId);
  682. const tableContainer = document.getElementById(tableId);
  683. if (!canvas) {
  684. console.log('Canvas not found for chart ID:', chartId);
  685. const chartContainer = document.querySelector('.modern-chart-container');
  686. if (chartContainer) {
  687. chartContainer.innerHTML = `
  688. <div class="chart-empty-state">
  689. <div style="text-align: center; padding: 4rem 2rem;">
  690. <div style="font-size: 4rem; margin-bottom: 1.5rem; opacity: 0.3;">📊</div>
  691. <h3 style="font-size: 1.5rem; font-weight: 600; margin-bottom: 1rem; color: #374151;">
  692. Grafico non disponibile
  693. </h3>
  694. <p style="font-size: 1rem; opacity: 0.7; margin: 0; max-width: 400px; margin-left: auto; margin-right: auto; line-height: 1.5;">
  695. Il grafico per questo corso non può essere visualizzato nella stagione selezionata.
  696. </p>
  697. </div>
  698. </div>
  699. `;
  700. }
  701. if (tableContainer) {
  702. tableContainer.innerHTML = '';
  703. }
  704. return;
  705. }
  706. this.destroyChart(chartId);
  707. @this.call('getCourseData', selectedCourseValue).then(courseData => {
  708. console.log('Received course data:', courseData);
  709. if (courseData.isEmpty) {
  710. console.log('No data available for course, showing message');
  711. if (tableContainer) {
  712. tableContainer.innerHTML = '';
  713. }
  714. const chartContainer = canvas.parentElement;
  715. chartContainer.innerHTML = `
  716. <div class="chart-empty-state">
  717. <div style="text-align: center; padding: 4rem 2rem;">
  718. <div style="font-size: 4rem; margin-bottom: 1.5rem; opacity: 0.3;">📊</div>
  719. <h3 style="font-size: 1.5rem; font-weight: 600; margin-bottom: 1rem; color: #374151;">
  720. ${courseData.message}
  721. </h3>
  722. <p style="font-size: 1rem; opacity: 0.7; margin: 0; max-width: 400px; margin-left: auto; margin-right: auto; line-height: 1.5;">
  723. Questo corso non ha pagamenti registrati per la stagione selezionata.
  724. </p>
  725. </div>
  726. </div>
  727. `;
  728. return;
  729. }
  730. if (!courseData || !courseData.labels || courseData.labels.length === 0) {
  731. console.log('No data available for chart');
  732. return;
  733. }
  734. let canvasElement = document.getElementById(chartId);
  735. if (!canvasElement) {
  736. const chartContainer = canvas.parentElement;
  737. chartContainer.innerHTML = `<canvas id="${chartId}"></canvas>`;
  738. canvasElement = document.getElementById(chartId);
  739. }
  740. this.updateCourseTable(tableContainer, courseData.tableData);
  741. const participantData = courseData.datasets.find(d => d.participantData)?.participantData || [];
  742. const ctx = canvasElement.getContext('2d');
  743. const earnedGradient = ctx.createLinearGradient(0, 0, 0, 400);
  744. earnedGradient.addColorStop(0, 'rgba(16, 185, 129, 1)');
  745. earnedGradient.addColorStop(1, 'rgba(16, 185, 129, 1)');
  746. const totalData = courseData.datasets.find(d => d.label === 'Pagamenti Attesi')?.data || [];
  747. const earnedData = courseData.datasets.find(d => d.label === 'Pagamenti Effettuati')?.data || [];
  748. this.charts[chartId] = new Chart(ctx, {
  749. type: 'bar',
  750. data: {
  751. labels: courseData.labels,
  752. datasets: [
  753. {
  754. label: 'Pagamenti Effettuati',
  755. backgroundColor: earnedGradient,
  756. borderColor: 'rgba(16, 185, 129, 1)',
  757. borderWidth: 0,
  758. borderRadius: 8,
  759. borderSkipped: false,
  760. data: earnedData,
  761. type: 'bar',
  762. order: 2
  763. },
  764. {
  765. label: 'Pagamenti Attesi',
  766. backgroundColor: 'transparent',
  767. borderColor: 'rgba(59, 130, 246, 1)',
  768. borderWidth: 3,
  769. pointBackgroundColor: 'rgba(59, 130, 246, 1)',
  770. pointBorderColor: '#ffffff',
  771. pointBorderWidth: 3,
  772. pointRadius: 7,
  773. pointHoverRadius: 9,
  774. data: totalData,
  775. type: 'line',
  776. tension: 0.3,
  777. order: 1,
  778. participantData: participantData
  779. }
  780. ]
  781. },
  782. options: {
  783. responsive: true,
  784. maintainAspectRatio: false,
  785. interaction: {
  786. mode: 'index',
  787. intersect: false,
  788. },
  789. layout: {
  790. padding: {
  791. top: 20,
  792. right: 20,
  793. bottom: 20,
  794. left: 10
  795. }
  796. },
  797. scales: {
  798. x: {
  799. grid: {
  800. display: false
  801. },
  802. ticks: {
  803. font: {
  804. weight: '600',
  805. size: 13
  806. },
  807. color: '#6b7280'
  808. },
  809. border: {
  810. display: false
  811. }
  812. },
  813. y: {
  814. beginAtZero: true,
  815. grid: {
  816. color: 'rgba(156, 163, 175, 0.15)',
  817. borderDash: [3, 3]
  818. },
  819. border: {
  820. display: false
  821. },
  822. ticks: {
  823. font: {
  824. size: 12,
  825. weight: '500'
  826. },
  827. color: '#6b7280',
  828. callback: function (value) {
  829. return '€' + new Intl.NumberFormat('it-IT', {
  830. minimumFractionDigits: 0,
  831. maximumFractionDigits: 0
  832. }).format(value);
  833. }
  834. }
  835. }
  836. },
  837. plugins: {
  838. legend: {
  839. display: true,
  840. position: 'top',
  841. labels: {
  842. usePointStyle: true,
  843. padding: 15,
  844. font: { weight: '500', size: 12 },
  845. }
  846. },
  847. tooltip: {
  848. backgroundColor: 'rgba(255, 255, 255, 0.98)',
  849. titleColor: '#111827',
  850. bodyColor: '#374151',
  851. borderColor: 'rgba(229, 231, 235, 0.8)',
  852. borderWidth: 1,
  853. cornerRadius: 12,
  854. titleFont: {
  855. weight: 'bold',
  856. size: 15
  857. },
  858. bodyFont: {
  859. size: 14,
  860. weight: '500'
  861. },
  862. padding: 16,
  863. boxPadding: 8,
  864. usePointStyle: true,
  865. displayColors: true,
  866. callbacks: {
  867. title: function (context) {
  868. return context[0].label;
  869. },
  870. label: function (context) {
  871. let label = context.dataset.label + ': €' +
  872. new Intl.NumberFormat('it-IT').format(context.parsed.y);
  873. if (context.dataset.label === 'Pagamenti Effettuati') {
  874. const earnedValue = parseFloat(context.parsed.y) || 0;
  875. const totalValue = parseFloat(totalData[context.dataIndex]) || 0;
  876. const missingValue = Math.max(0, totalValue - earnedValue);
  877. if (participantData[context.dataIndex]) {
  878. label += '\n👥 Partecipanti: ' + participantData[context.dataIndex];
  879. }
  880. if (missingValue > 0) {
  881. label += '\n🔴 Mancanti: €' + new Intl.NumberFormat('it-IT').format(missingValue);
  882. }
  883. }
  884. if (context.dataset.label === 'Pagamenti Attesi' && participantData[context.dataIndex]) {
  885. label += '\n👥 Partecipanti: ' + participantData[context.dataIndex];
  886. }
  887. return label;
  888. }
  889. }
  890. }
  891. },
  892. animation: {
  893. duration: 1500,
  894. easing: 'easeOutCubic'
  895. },
  896. elements: {
  897. bar: {
  898. borderRadius: {
  899. topLeft: 8,
  900. topRight: 8,
  901. bottomLeft: 0,
  902. bottomRight: 0
  903. }
  904. },
  905. line: {
  906. borderCapStyle: 'round',
  907. borderJoinStyle: 'round'
  908. },
  909. point: {
  910. hoverBorderWidth: 4,
  911. borderWidth: 3
  912. }
  913. }
  914. }
  915. });
  916. }).catch(error => {
  917. console.error('Error calling getCourseData:', error);
  918. });
  919. },
  920. updateCourseTable: function (container, tableData) {
  921. if (!container || !tableData) return;
  922. let tableHtml = `
  923. <div class="course-table">
  924. <div class="table-header">
  925. <div class="table-cell month">Mese</div>
  926. <div class="table-cell participants">👥</div>
  927. <div class="table-cell delta">Mancanti</div>
  928. <div class="table-cell percentage">%</div>
  929. </div>
  930. `;
  931. tableData.forEach(row => {
  932. const earned = parseFloat(row.earned) || 0;
  933. const total = parseFloat(row.total) || 0;
  934. const delta = Math.max(0, total - earned);
  935. let percentage = 0;
  936. let percentageDisplay = '—';
  937. let percentageClass = 'neutral';
  938. if (total > 0) {
  939. percentage = Math.round((earned / total) * 100);
  940. percentageDisplay = percentage + '%';
  941. // Color based on completion
  942. if (percentage >= 100) {
  943. percentageClass = 'good';
  944. } else if (percentage >= 80) {
  945. percentageClass = 'warning';
  946. } else {
  947. percentageClass = 'bad';
  948. }
  949. }
  950. // Delta styling: positive when delta is 0 (fully paid), negative when there's missing amount
  951. const deltaClass = (total > 0 && delta === 0) ? 'positive' :
  952. (delta > 0) ? 'negative' : 'neutral';
  953. tableHtml += `
  954. <div class="table-row">
  955. <div class="table-cell month">${row.month}</div>
  956. <div class="table-cell participants">${row.participants}</div>
  957. <div class="table-cell delta ${deltaClass}">€${new Intl.NumberFormat('it-IT').format(delta)}</div>
  958. <div class="table-cell percentage ${percentageClass}">${percentageDisplay}</div>
  959. </div>
  960. `;
  961. });
  962. tableHtml += '</div>';
  963. container.innerHTML = tableHtml;
  964. },
  965. updateCourseChart: function () {
  966. if (this.selectedCourse) {
  967. const seasonFilter = @json($seasonFilter);
  968. const seasonKey = seasonFilter.replace('-', '');
  969. this.createCourseChartWithValue(this.selectedCourse);
  970. }
  971. }
  972. };
  973. document.addEventListener('DOMContentLoaded', function () {
  974. setTimeout(() => {
  975. window.ReportsChartManager.updateMainCharts();
  976. }, 100);
  977. });
  978. document.addEventListener('livewire:navigated', function () {
  979. setTimeout(() => {
  980. window.ReportsChartManager.updateMainCharts();
  981. }, 100);
  982. });
  983. document.addEventListener('livewire:updated', function (event) {
  984. console.log('Livewire updated, waiting for component to fully update...');
  985. setTimeout(() => {
  986. console.log('Now updating charts after delay');
  987. window.ReportsChartManager.forceUpdateCharts();
  988. }, 800);
  989. });
  990. document.addEventListener('livewire:load', function () {
  991. Livewire.on('courseSelected', (courseId) => {
  992. console.log('Course selected event received:', courseId);
  993. setTimeout(() => {
  994. window.ReportsChartManager.createCourseChartWithValue(courseId);
  995. }, 200);
  996. });
  997. Livewire.on('chartsUpdated', () => {
  998. console.log('Charts updated event received from Livewire');
  999. setTimeout(() => {
  1000. window.ReportsChartManager.forceUpdateCharts();
  1001. }, 200);
  1002. });
  1003. });
  1004. </script>
  1005. </div>