reports.blade.php 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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 500px; 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 totalDataset = membersData.datasets.find(d => d.label === 'Totale Membri Tesserati');
  551. const cardTypeDatasets = membersData.datasets.filter(d => d.label !== 'Totale Membri Tesserati');
  552. const memberCounts = totalDataset ? totalDataset.data : [];
  553. let tableHtml = `
  554. <div class="members-table">
  555. <div class="table-header">
  556. <div class="table-cell">Stagione</div>
  557. <div class="table-cell">Totale</div>
  558. <div class="table-cell">Variazione</div>
  559. <div class="table-cell">Tipologie</div>
  560. </div>
  561. `;
  562. seasonLabels.forEach((season, index) => {
  563. const current = parseInt(memberCounts[index] || 0);
  564. const previous = index > 0 ? parseInt(memberCounts[index - 1] || 0) : 0;
  565. const variation = index > 0 ? current - previous : 0;
  566. const variationPercent = previous > 0 ? Math.round((variation / previous) * 100 * 10) / 10 : 0;
  567. const rowClass = variation > 0 ? 'positive' : (variation < 0 ? 'negative' : 'neutral');
  568. let variationText = '—';
  569. if (index > 0) {
  570. if (variation > 0) {
  571. variationText = `<span class="variation-positive">+${variation} (+${variationPercent}%)</span>`;
  572. } else if (variation < 0) {
  573. variationText = `<span class="variation-negative">${variation} (${variationPercent}%)</span>`;
  574. } else {
  575. variationText = `<span class="variation-neutral">${variation}</span>`;
  576. }
  577. }
  578. // Build card type breakdown
  579. let cardTypeBreakdown = '';
  580. cardTypeDatasets.forEach((dataset, datasetIndex) => {
  581. const count = dataset.data[index] || 0;
  582. if (count > 0) {
  583. const color = dataset.borderColor || '#6b7280';
  584. cardTypeBreakdown += `
  585. <div class="card-type-item">
  586. <span class="card-type-indicator" style="background-color: ${color}"></span>
  587. <span class="card-type-name">${dataset.label}</span>
  588. <span class="card-type-count">${count}</span>
  589. </div>
  590. `;
  591. }
  592. });
  593. if (!cardTypeBreakdown) {
  594. cardTypeBreakdown = '<div class="no-card-types">Nessun dettaglio</div>';
  595. }
  596. tableHtml += `
  597. <div class="table-row ${rowClass}">
  598. <div class="table-cell season-name">${season}</div>
  599. <div class="table-cell members-count">${new Intl.NumberFormat('it-IT').format(current)}</div>
  600. <div class="table-cell variation">${variationText}</div>
  601. <div class="table-cell card-types">
  602. <div class="card-types-container">
  603. ${cardTypeBreakdown}
  604. </div>
  605. </div>
  606. </div>
  607. `;
  608. });
  609. tableHtml += '</div>';
  610. container.innerHTML = tableHtml;
  611. },
  612. createCourseChart: function () {
  613. console.log('Creating course chart...');
  614. const seasonFilter = '{{ $seasonFilter }}';
  615. const selectedCourse = '{{ $selectedCourse ?? '' }}';
  616. const seasonKey = '{{ str_replace('-', '', $seasonFilter) }}';
  617. console.log('Selected course:', selectedCourse, 'for season:', seasonFilter);
  618. if (!selectedCourse || selectedCourse.trim() === '') {
  619. console.log('No course selected, skipping chart creation');
  620. return;
  621. }
  622. const chartId = `courses-chart-${seasonKey}-${selectedCourse}`;
  623. const canvas = document.getElementById(chartId);
  624. if (!canvas) return;
  625. this.destroyChart(chartId);
  626. const courseData = @json($this->getCourseMonthlyEarnings());
  627. const ctx = canvas.getContext('2d');
  628. this.charts[chartId] = new Chart(ctx, {
  629. type: 'bar',
  630. data: {
  631. labels: courseData.labels,
  632. datasets: courseData.datasets.map(dataset => {
  633. if (dataset.type === 'line') {
  634. return {
  635. ...dataset,
  636. type: 'line',
  637. fill: false,
  638. backgroundColor: 'transparent'
  639. };
  640. }
  641. return dataset;
  642. })
  643. },
  644. options: {
  645. responsive: true,
  646. maintainAspectRatio: false,
  647. interaction: {
  648. mode: 'index',
  649. intersect: false,
  650. },
  651. scales: {
  652. x: {
  653. grid: { display: false },
  654. ticks: { font: { weight: '500' } }
  655. },
  656. y: {
  657. beginAtZero: true,
  658. grid: {
  659. color: 'rgba(0, 0, 0, 1)',
  660. borderDash: [5, 5]
  661. },
  662. ticks: {
  663. callback: function (value) {
  664. return '€' + new Intl.NumberFormat('it-IT').format(value);
  665. }
  666. }
  667. }
  668. },
  669. plugins: {
  670. legend: {
  671. display: true,
  672. position: 'top',
  673. labels: {
  674. usePointStyle: true,
  675. padding: 20,
  676. font: { weight: '500' }
  677. }
  678. },
  679. tooltip: {
  680. backgroundColor: 'rgba(255, 255, 255, 1)',
  681. titleColor: '#212529',
  682. bodyColor: '#495057',
  683. borderColor: '#e9ecef',
  684. borderWidth: 1,
  685. cornerRadius: 8,
  686. callbacks: {
  687. label: function (context) {
  688. return context.dataset.label + ': €' +
  689. new Intl.NumberFormat('it-IT').format(context.parsed.y);
  690. }
  691. }
  692. }
  693. },
  694. animation: {
  695. duration: 1000,
  696. easing: 'easeOutQuart'
  697. }
  698. }
  699. });
  700. },
  701. createCourseChartWithValue: function (selectedCourseValue) {
  702. console.log('Creating modern course chart with value:', selectedCourseValue);
  703. const seasonFilter = '{{ $seasonFilter }}';
  704. const seasonKey = '{{ str_replace('-', '', $seasonFilter) }}';
  705. const chartId = `courses-chart-${seasonKey}-${selectedCourseValue}`;
  706. const tableId = `course-delta-table-${seasonKey}-${selectedCourseValue}`;
  707. let canvas = document.getElementById(chartId);
  708. const tableContainer = document.getElementById(tableId);
  709. if (!canvas) {
  710. console.log('Canvas not found for chart ID:', chartId);
  711. const chartContainer = document.querySelector('.modern-chart-container');
  712. if (chartContainer) {
  713. chartContainer.innerHTML = `
  714. <div class="chart-empty-state">
  715. <div style="text-align: center; padding: 4rem 2rem;">
  716. <div style="font-size: 4rem; margin-bottom: 1.5rem; opacity: 0.3;">📊</div>
  717. <h3 style="font-size: 1.5rem; font-weight: 600; margin-bottom: 1rem; color: #374151;">
  718. Grafico non disponibile
  719. </h3>
  720. <p style="font-size: 1rem; opacity: 0.7; margin: 0; max-width: 400px; margin-left: auto; margin-right: auto; line-height: 1.5;">
  721. Il grafico per questo corso non può essere visualizzato nella stagione selezionata.
  722. </p>
  723. </div>
  724. </div>
  725. `;
  726. }
  727. if (tableContainer) {
  728. tableContainer.innerHTML = '';
  729. }
  730. return;
  731. }
  732. this.destroyChart(chartId);
  733. @this.call('getCourseData', selectedCourseValue).then(courseData => {
  734. console.log('Received course data:', courseData);
  735. if (courseData.isEmpty) {
  736. console.log('No data available for course, showing message');
  737. if (tableContainer) {
  738. tableContainer.innerHTML = '';
  739. }
  740. const chartContainer = canvas.parentElement;
  741. chartContainer.innerHTML = `
  742. <div class="chart-empty-state">
  743. <div style="text-align: center; padding: 4rem 2rem;">
  744. <div style="font-size: 4rem; margin-bottom: 1.5rem; opacity: 0.3;">📊</div>
  745. <h3 style="font-size: 1.5rem; font-weight: 600; margin-bottom: 1rem; color: #374151;">
  746. ${courseData.message}
  747. </h3>
  748. <p style="font-size: 1rem; opacity: 0.7; margin: 0; max-width: 400px; margin-left: auto; margin-right: auto; line-height: 1.5;">
  749. Questo corso non ha pagamenti registrati per la stagione selezionata.
  750. </p>
  751. </div>
  752. </div>
  753. `;
  754. return;
  755. }
  756. if (!courseData || !courseData.labels || courseData.labels.length === 0) {
  757. console.log('No data available for chart');
  758. return;
  759. }
  760. let canvasElement = document.getElementById(chartId);
  761. if (!canvasElement) {
  762. const chartContainer = canvas.parentElement;
  763. chartContainer.innerHTML = `<canvas id="${chartId}"></canvas>`;
  764. canvasElement = document.getElementById(chartId);
  765. }
  766. this.updateCourseTable(tableContainer, courseData.tableData);
  767. const participantData = courseData.datasets.find(d => d.participantData)?.participantData || [];
  768. const ctx = canvasElement.getContext('2d');
  769. const earnedGradient = ctx.createLinearGradient(0, 0, 0, 400);
  770. earnedGradient.addColorStop(0, 'rgba(16, 185, 129, 1)');
  771. earnedGradient.addColorStop(1, 'rgba(16, 185, 129, 1)');
  772. const totalData = courseData.datasets.find(d => d.label === 'Pagamenti Attesi')?.data || [];
  773. const earnedData = courseData.datasets.find(d => d.label === 'Pagamenti Effettuati')?.data || [];
  774. this.charts[chartId] = new Chart(ctx, {
  775. type: 'bar',
  776. data: {
  777. labels: courseData.labels,
  778. datasets: [
  779. {
  780. label: 'Pagamenti Effettuati',
  781. backgroundColor: earnedGradient,
  782. borderColor: 'rgba(16, 185, 129, 1)',
  783. borderWidth: 0,
  784. borderRadius: 8,
  785. borderSkipped: false,
  786. data: earnedData,
  787. type: 'bar',
  788. order: 2
  789. },
  790. {
  791. label: 'Pagamenti Attesi',
  792. backgroundColor: 'transparent',
  793. borderColor: 'rgba(59, 130, 246, 1)',
  794. borderWidth: 3,
  795. pointBackgroundColor: 'rgba(59, 130, 246, 1)',
  796. pointBorderColor: '#ffffff',
  797. pointBorderWidth: 3,
  798. pointRadius: 7,
  799. pointHoverRadius: 9,
  800. data: totalData,
  801. type: 'line',
  802. tension: 0.3,
  803. order: 1,
  804. participantData: participantData
  805. }
  806. ]
  807. },
  808. options: {
  809. responsive: true,
  810. maintainAspectRatio: false,
  811. interaction: {
  812. mode: 'index',
  813. intersect: false,
  814. },
  815. layout: {
  816. padding: {
  817. top: 20,
  818. right: 20,
  819. bottom: 20,
  820. left: 10
  821. }
  822. },
  823. scales: {
  824. x: {
  825. grid: {
  826. display: false
  827. },
  828. ticks: {
  829. font: {
  830. weight: '600',
  831. size: 13
  832. },
  833. color: '#6b7280'
  834. },
  835. border: {
  836. display: false
  837. }
  838. },
  839. y: {
  840. beginAtZero: true,
  841. grid: {
  842. color: 'rgba(156, 163, 175, 0.15)',
  843. borderDash: [3, 3]
  844. },
  845. border: {
  846. display: false
  847. },
  848. ticks: {
  849. font: {
  850. size: 12,
  851. weight: '500'
  852. },
  853. color: '#6b7280',
  854. callback: function (value) {
  855. return '€' + new Intl.NumberFormat('it-IT', {
  856. minimumFractionDigits: 0,
  857. maximumFractionDigits: 0
  858. }).format(value);
  859. }
  860. }
  861. }
  862. },
  863. plugins: {
  864. legend: {
  865. display: true,
  866. position: 'top',
  867. labels: {
  868. usePointStyle: true,
  869. padding: 15,
  870. font: { weight: '500', size: 12 },
  871. }
  872. },
  873. tooltip: {
  874. backgroundColor: 'rgba(255, 255, 255, 0.98)',
  875. titleColor: '#111827',
  876. bodyColor: '#374151',
  877. borderColor: 'rgba(229, 231, 235, 0.8)',
  878. borderWidth: 1,
  879. cornerRadius: 12,
  880. titleFont: {
  881. weight: 'bold',
  882. size: 15
  883. },
  884. bodyFont: {
  885. size: 14,
  886. weight: '500'
  887. },
  888. padding: 16,
  889. boxPadding: 8,
  890. usePointStyle: true,
  891. displayColors: true,
  892. callbacks: {
  893. title: function (context) {
  894. return context[0].label;
  895. },
  896. label: function (context) {
  897. let label = context.dataset.label + ': €' +
  898. new Intl.NumberFormat('it-IT').format(context.parsed.y);
  899. if (context.dataset.label === 'Pagamenti Effettuati') {
  900. const earnedValue = parseFloat(context.parsed.y) || 0;
  901. const totalValue = parseFloat(totalData[context.dataIndex]) || 0;
  902. const missingValue = Math.max(0, totalValue - earnedValue);
  903. if (participantData[context.dataIndex]) {
  904. label += '\n👥 Partecipanti: ' + participantData[context.dataIndex];
  905. }
  906. if (missingValue > 0) {
  907. label += '\n🔴 Mancanti: €' + new Intl.NumberFormat('it-IT').format(missingValue);
  908. }
  909. }
  910. if (context.dataset.label === 'Pagamenti Attesi' && participantData[context.dataIndex]) {
  911. label += '\n👥 Partecipanti: ' + participantData[context.dataIndex];
  912. }
  913. return label;
  914. }
  915. }
  916. }
  917. },
  918. animation: {
  919. duration: 1500,
  920. easing: 'easeOutCubic'
  921. },
  922. elements: {
  923. bar: {
  924. borderRadius: {
  925. topLeft: 8,
  926. topRight: 8,
  927. bottomLeft: 0,
  928. bottomRight: 0
  929. }
  930. },
  931. line: {
  932. borderCapStyle: 'round',
  933. borderJoinStyle: 'round'
  934. },
  935. point: {
  936. hoverBorderWidth: 4,
  937. borderWidth: 3
  938. }
  939. }
  940. }
  941. });
  942. }).catch(error => {
  943. console.error('Error calling getCourseData:', error);
  944. });
  945. },
  946. updateCourseTable: function (container, tableData) {
  947. if (!container || !tableData) return;
  948. let tableHtml = `
  949. <div class="course-table">
  950. <div class="table-header">
  951. <div class="table-cell month">Mese</div>
  952. <div class="table-cell participants">👥</div>
  953. <div class="table-cell delta">Mancanti</div>
  954. <div class="table-cell percentage">%</div>
  955. </div>
  956. `;
  957. tableData.forEach(row => {
  958. const earned = parseFloat(row.earned) || 0;
  959. const total = parseFloat(row.total) || 0;
  960. const delta = Math.max(0, total - earned);
  961. let percentage = 0;
  962. let percentageDisplay = '—';
  963. let percentageClass = 'neutral';
  964. if (total > 0) {
  965. percentage = Math.round((earned / total) * 100);
  966. percentageDisplay = percentage + '%';
  967. // Color based on completion
  968. if (percentage >= 100) {
  969. percentageClass = 'good';
  970. } else if (percentage >= 80) {
  971. percentageClass = 'warning';
  972. } else {
  973. percentageClass = 'bad';
  974. }
  975. }
  976. // Delta styling: positive when delta is 0 (fully paid), negative when there's missing amount
  977. const deltaClass = (total > 0 && delta === 0) ? 'positive' :
  978. (delta > 0) ? 'negative' : 'neutral';
  979. tableHtml += `
  980. <div class="table-row">
  981. <div class="table-cell month">${row.month}</div>
  982. <div class="table-cell participants">${row.participants}</div>
  983. <div class="table-cell delta ${deltaClass}">€${new Intl.NumberFormat('it-IT').format(delta)}</div>
  984. <div class="table-cell percentage ${percentageClass}">${percentageDisplay}</div>
  985. </div>
  986. `;
  987. });
  988. tableHtml += '</div>';
  989. container.innerHTML = tableHtml;
  990. },
  991. updateCourseChart: function () {
  992. if (this.selectedCourse) {
  993. const seasonFilter = @json($seasonFilter);
  994. const seasonKey = seasonFilter.replace('-', '');
  995. this.createCourseChartWithValue(this.selectedCourse);
  996. }
  997. }
  998. };
  999. document.addEventListener('DOMContentLoaded', function () {
  1000. setTimeout(() => {
  1001. window.ReportsChartManager.updateMainCharts();
  1002. }, 100);
  1003. });
  1004. document.addEventListener('livewire:navigated', function () {
  1005. setTimeout(() => {
  1006. window.ReportsChartManager.updateMainCharts();
  1007. }, 100);
  1008. });
  1009. document.addEventListener('livewire:updated', function (event) {
  1010. console.log('Livewire updated, waiting for component to fully update...');
  1011. setTimeout(() => {
  1012. console.log('Now updating charts after delay');
  1013. window.ReportsChartManager.forceUpdateCharts();
  1014. }, 800);
  1015. });
  1016. document.addEventListener('livewire:load', function () {
  1017. Livewire.on('courseSelected', (courseId) => {
  1018. console.log('Course selected event received:', courseId);
  1019. setTimeout(() => {
  1020. window.ReportsChartManager.createCourseChartWithValue(courseId);
  1021. }, 200);
  1022. });
  1023. Livewire.on('chartsUpdated', () => {
  1024. console.log('Charts updated event received from Livewire');
  1025. setTimeout(() => {
  1026. window.ReportsChartManager.forceUpdateCharts();
  1027. }, 200);
  1028. });
  1029. });
  1030. </script>
  1031. </div>