reports.blade.php 53 KB

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