CourseList.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6. use App\Models\MemberCourse;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Storage;
  9. use Illuminate\Support\Facades\Auth;
  10. use App\Http\Middleware\TenantMiddleware;
  11. class CourseList extends Component
  12. {
  13. public $records = array();
  14. public $recordsNoPaginate = array();
  15. public $courses = array();
  16. public $start = 0;
  17. public $totalRecords = 0;
  18. public $pages = 0;
  19. public $currentPage = 1;
  20. public $pageLength = 10;
  21. public $totS = [];
  22. public $totSExcel = [];
  23. public $sort = '';
  24. public $dir = '';
  25. public $hasFilter = false;
  26. public $courseId = 0;
  27. public $filterCourse = [];
  28. public $filterLevel = [];
  29. public $filterFrequency = [];
  30. public $filterType = [];
  31. public $filterDuration = [];
  32. public $course_durations = [];
  33. public $course_types = [];
  34. public $course_frequencies = [];
  35. public $course_levels = [];
  36. public $course_years = [];
  37. public $totals = [];
  38. public $totalIsc = [];
  39. public $selectedCourseId;
  40. public $selectedMemberId;
  41. public $aaa;
  42. public $months = array('Set', 'Ott', 'Nov', 'Dic', 'Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago');
  43. public function boot()
  44. {
  45. app(TenantMiddleware::class)->setupTenantConnection();
  46. }
  47. public function mount()
  48. {
  49. $this->selectedCourseId = 0;
  50. $this->selectedMemberId = 0;
  51. $this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
  52. $this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
  53. $this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
  54. $this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
  55. $this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
  56. $this->courses = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
  57. //if (sizeof($this->courses) > 0)
  58. // $this->courseId = $this->courses[0]->id;
  59. }
  60. public function updatedfilterCourse($value)
  61. {
  62. $this->emit('load-data-table');
  63. }
  64. public function updatedfilterLevel($value)
  65. {
  66. $this->emit('load-data-table');
  67. }
  68. public function updatedfilterFrequency($value)
  69. {
  70. $this->emit('load-data-table');
  71. }
  72. public function updatedfilterType($value)
  73. {
  74. $this->emit('load-data-table');
  75. }
  76. public function updatedfilterDuration($value)
  77. {
  78. $this->emit('load-data-table');
  79. }
  80. public function updatedpageLength($value)
  81. {
  82. $this->emit('load-data-table');
  83. }
  84. public function render()
  85. {
  86. $member_course = \App\Models\MemberCourse::with('member')->with('course');
  87. if ($this->hasFilter) {
  88. if (isset($_GET["search"]["value"])) {
  89. if ($_GET["search"]["value"] != '') {
  90. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  91. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  92. $query->whereRaw("CONCAT(first_name, ' ', last_name) like '%" . $v . "%'")
  93. ->orWhereRaw("CONCAT(last_name, ' ', first_name) like '%" . $v . "%'");
  94. })->pluck('id');
  95. /*
  96. $v = str_replace("'", "\'", stripcslashes($_GET["search"]["value"]));
  97. $member_ids = \App\Models\Member::where(function ($query) use ($v) {
  98. $query->where('first_name', 'like', '%' . $v . '%')
  99. ->orWhere('last_name', 'like', '%' . $v . '%');
  100. })->pluck('id');*/
  101. $member_course = $member_course->whereIn('member_id', $member_ids);
  102. }
  103. }
  104. if (sizeof($this->filterCourse) > 0) {
  105. $course_ids = [];
  106. $courses = $this->filterCourse;
  107. foreach ($courses as $c) {
  108. $all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
  109. foreach ($all as $a) {
  110. $course_ids[] = $a->id;
  111. }
  112. }
  113. $member_course = $member_course->whereIn('course_id', $course_ids);
  114. }
  115. if (sizeof($this->filterLevel) > 0) {
  116. $course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
  117. $member_course = $member_course->whereIn('course_id', $course_ids);
  118. }
  119. if (sizeof($this->filterFrequency) > 0) {
  120. $course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
  121. $member_course = $member_course->whereIn('course_id', $course_ids);
  122. }
  123. if (sizeof($this->filterType) > 0) {
  124. $course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
  125. $member_course = $member_course->whereIn('course_id', $course_ids);
  126. }
  127. if (sizeof($this->filterDuration) > 0) {
  128. $course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
  129. $member_course = $member_course->whereIn('course_id', $course_ids);
  130. }
  131. }
  132. $totals = [];
  133. $totalIsc = [];
  134. $datas = [];
  135. $xxx = 1;
  136. /*
  137. $sortColumn = '';
  138. if (isset($_GET["order"]))
  139. {
  140. }
  141. */
  142. $column = '';
  143. $sort_value = 0;
  144. if ($this->sort != '') {
  145. $f = $this->sort;
  146. $d = $this->dir;
  147. if ($f >= 5 && $f <= 16) {
  148. $column = 'column_' . ($f - 2);
  149. if (session()->get('sort_column')) {
  150. if (session()->get('sort_column') != $f) {
  151. session()->put('sort_column', $f);
  152. session()->put('sort_order', $d);
  153. session()->put('sort_value', 0);
  154. $sort_value = 0;
  155. } else {
  156. if (session()->get('sort_order') == $d) {
  157. //session()->put('sort_value', 0);
  158. $sort_value = session()->get('sort_value', 0);
  159. } else {
  160. if (session()->get('sort_value', 0) == 0) {
  161. $sort_value = 1;
  162. }
  163. if (session()->get('sort_value', 0) == 1) {
  164. $sort_value = 2;
  165. }
  166. if (session()->get('sort_value', 0) == 2) {
  167. $sort_value = 3;
  168. }
  169. if (session()->get('sort_value', 0) == 3) {
  170. $sort_value = 0;
  171. }
  172. session()->put('sort_value', $sort_value);
  173. }
  174. session()->put('sort_order', $d);
  175. }
  176. } else {
  177. session()->put('sort_column', $f);
  178. session()->put('sort_order', $d);
  179. session()->put('sort_value', 0);
  180. $sort_value = 0;
  181. }
  182. }
  183. }
  184. //print $sort_value;
  185. $totals = [];
  186. $prices = [];
  187. $member_course_totals = $member_course->get();
  188. foreach ($member_course_totals as $x) {
  189. $price = 0;
  190. $price = $x->price; // $x->course->price;
  191. $subPrice = $x->subscription_price; // $x->course->subscription_price;
  192. $records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
  193. $prices = [];
  194. foreach ($records as $record) {
  195. foreach ($record->rows as $row) {
  196. if ($row->causal_id == $x->course->sub_causal_id) // || str_contains(strtolower($row->note), 'iscrizione'))
  197. //if (str_contains(strtolower($row->note), 'iscrizione'))
  198. {
  199. $subPrice = $row->amount;
  200. }
  201. if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  202. $tot = sizeof(json_decode($row->when));
  203. foreach (json_decode($row->when) as $m) {
  204. if (isset($prices[$m->month]))
  205. $prices[$m->month] += $row->amount / $tot;
  206. else
  207. $prices[$m->month] = $row->amount / $tot;
  208. }
  209. }
  210. }
  211. }
  212. for ($i = 1; $i <= 12; $i++) {
  213. $cls = $this->getColor($x->months, $i, isset($prices[$i]) && $prices[$i] == $price);
  214. if ($cls != 'wgrey') {
  215. if (!isset($totals[$i])) {
  216. $totals[$i]['green'] = 0;
  217. $totals[$i]['orange'] = 0;
  218. $totals[$i]['yellow'] = 0;
  219. }
  220. if ($cls == 'yellow') {
  221. $totals[$i][$cls] += 1;
  222. } else {
  223. $p = isset($prices[$i]) ? $prices[$i] : $price;
  224. //if (isset($totals[$i][$cls]))
  225. $totals[$i][$cls] += $p;
  226. //else
  227. //$totals[$i][$cls] = $p;
  228. }
  229. }
  230. }
  231. $sub = $x->subscribed ? "Y" : "N";
  232. if (isset($totalIsc[$sub]))
  233. $totalIsc[$sub] += $subPrice;
  234. else
  235. $totalIsc[$sub] = $subPrice;
  236. $s = 0;
  237. if ($column != '') {
  238. $z = 0;
  239. switch ($column) {
  240. case 'column_3':
  241. $z = 9;
  242. break;
  243. case 'column_4':
  244. $z = 10;
  245. break;
  246. case 'column_5':
  247. $z = 11;
  248. break;
  249. case 'column_6':
  250. $z = 12;
  251. break;
  252. case 'column_7':
  253. $z = 1;
  254. break;
  255. case 'column_8':
  256. $z = 2;
  257. break;
  258. case 'column_9':
  259. $z = 3;
  260. break;
  261. case 'column_10':
  262. $z = 4;
  263. break;
  264. case 'column_11':
  265. $z = 5;
  266. break;
  267. case 'column_12':
  268. $z = 6;
  269. break;
  270. case 'column_13':
  271. $z = 7;
  272. break;
  273. case 'column_14':
  274. $z = 8;
  275. break;
  276. default:
  277. $z = 0;
  278. break;
  279. }
  280. $c = getColor($x->months, $z);
  281. if ($sort_value == 0) {
  282. switch ($c) {
  283. case 'wgrey':
  284. $s = 0;
  285. break;
  286. case 'orange':
  287. $s = 1;
  288. break;
  289. case 'green':
  290. $s = 2;
  291. break;
  292. case 'yellow':
  293. $s = 3;
  294. break;
  295. default:
  296. $s = 0;
  297. break;
  298. }
  299. }
  300. if ($sort_value == 1) {
  301. switch ($c) {
  302. case 'wgrey':
  303. $s = 3;
  304. break;
  305. case 'orange':
  306. $s = 0;
  307. break;
  308. case 'green':
  309. $s = 1;
  310. break;
  311. case 'yellow':
  312. $s = 2;
  313. break;
  314. default:
  315. $s = 0;
  316. break;
  317. }
  318. }
  319. if ($sort_value == 2) {
  320. switch ($c) {
  321. case 'wgrey':
  322. $s = 2;
  323. break;
  324. case 'orange':
  325. $s = 3;
  326. break;
  327. case 'green':
  328. $s = 0;
  329. break;
  330. case 'yellow':
  331. $s = 1;
  332. break;
  333. default:
  334. $s = 0;
  335. break;
  336. }
  337. }
  338. if ($sort_value == 3) {
  339. switch ($c) {
  340. case 'wgrey':
  341. $s = 1;
  342. break;
  343. case 'orange':
  344. $s = 2;
  345. break;
  346. case 'green':
  347. $s = 3;
  348. break;
  349. case 'yellow':
  350. $s = 0;
  351. break;
  352. default:
  353. $s = 0;
  354. break;
  355. }
  356. }
  357. }
  358. $datas[] = array(
  359. "column_19" => $x->course->name,
  360. "column_0" => $x->member->last_name,
  361. "column_1" => $x->member->first_name,
  362. "column_2" => $x->subscribed . "§" . formatPrice($subPrice),
  363. "column_3" => $this->getColor($x->months, 9, isset($prices[9]) && $prices[9] >= $price) . "§" . formatPrice(isset($prices[9]) ? $prices[9] : $price) . "§" . (isset($prices[9]) && $prices[9] <= $price ? 'X' : ''),
  364. "column_4" => $this->getColor($x->months, 10, isset($prices[10]) && $prices[10] >= $price) . "§" . formatPrice(isset($prices[10]) ? $prices[10] : $price) . "§" . (isset($prices[10]) && $prices[10] <= $price ? 'X' : ''),
  365. "column_5" => $this->getColor($x->months, 11, isset($prices[11]) && $prices[11] >= $price) . "§" . formatPrice(isset($prices[11]) ? $prices[11] : $price) . "§" . (isset($prices[11]) && $prices[11] <= $price ? 'X' : ''),
  366. "column_6" => $this->getColor($x->months, 12, isset($prices[12]) && $prices[12] >= $price) . "§" . formatPrice(isset($prices[12]) ? $prices[12] : $price) . "§" . (isset($prices[12]) && $prices[12] <= $price ? 'X' : ''),
  367. "column_7" => $this->getColor($x->months, 1, isset($prices[1]) && $prices[1] >= $price) . "§" . formatPrice(isset($prices[1]) ? $prices[1] : $price) . "§" . (isset($prices[1]) && $prices[1] <= $price ? 'X' : ''),
  368. "column_8" => $this->getColor($x->months, 2, isset($prices[2]) && $prices[2] >= $price) . "§" . formatPrice(isset($prices[2]) ? $prices[2] : $price) . "§" . (isset($prices[2]) && $prices[2] <= $price ? 'X' : ''),
  369. "column_9" => $this->getColor($x->months, 3, isset($prices[3]) && $prices[3] >= $price) . "§" . formatPrice(isset($prices[3]) ? $prices[3] : $price) . "§" . (isset($prices[3]) && $prices[3] <= $price ? 'X' : ''),
  370. "column_10" => $this->getColor($x->months, 4, isset($prices[4]) && $prices[4] >= $price) . "§" . formatPrice(isset($prices[4]) ? $prices[4] : $price) . "§" . (isset($prices[4]) && $prices[4] <= $price ? 'X' : ''),
  371. "column_11" => $this->getColor($x->months, 5, isset($prices[5]) && $prices[5] >= $price) . "§" . formatPrice(isset($prices[5]) ? $prices[5] : $price) . "§" . (isset($prices[5]) && $prices[5] <= $price ? 'X' : ''),
  372. "column_12" => $this->getColor($x->months, 6, isset($prices[6]) && $prices[6] >= $price) . "§" . formatPrice(isset($prices[6]) ? $prices[6] : $price) . "§" . (isset($prices[6]) && $prices[6] <= $price ? 'X' : ''),
  373. "column_13" => $this->getColor($x->months, 7, isset($prices[7]) && $prices[7] >= $price) . "§" . formatPrice(isset($prices[7]) ? $prices[7] : $price) . "§" . (isset($prices[7]) && $prices[7] <= $price ? 'X' : ''),
  374. "column_14" => $this->getColor($x->months, 8, isset($prices[8]) && $prices[8] >= $price) . "§" . formatPrice(isset($prices[8]) ? $prices[8] : $price) . "§" . (isset($prices[8]) && $prices[8] <= $price ? 'X' : ''),
  375. "column_15" => $x->course_id,
  376. "column_16" => $x->id,
  377. "column_17" => $x->member_id,
  378. "column_18" => $xxx++,
  379. "column_20" => $s
  380. );
  381. }
  382. $count = $member_course->count();
  383. $this->totSExcel = [];
  384. $this->totS = [];
  385. //$js = '';
  386. $xx = 4;
  387. $str = '';
  388. if ($count > 0) {
  389. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=green><small>" . (isset($totalIsc["Y"]) ? formatPrice($totalIsc["Y"]) : 0) . "</small></a><br>";
  390. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=orange><small>" . (isset($totalIsc["N"]) ? formatPrice($totalIsc["N"]) : 0) . "</small></a><br>";
  391. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=yellow><small>0</small></a><br>";
  392. $this->totSExcel[] = array('green' => (isset($totalIsc["Y"]) ? formatPrice($totalIsc["Y"]) : 0), 'orange' => (isset($totalIsc["N"]) ? formatPrice($totalIsc["N"]) : 0), 'yellow' => 0);
  393. }
  394. $this->totS[] = $str;
  395. $str = "";
  396. foreach ($totals as $z => $t) {
  397. if ($z == 1) $xx = 5;
  398. if ($z == 2) $xx = 6;
  399. if ($z == 3) $xx = 7;
  400. if ($z == 4) $xx = 8;
  401. if ($z == 5) $xx = 9;
  402. if ($z == 6) $xx = 10;
  403. if ($z == 7) $xx = 11;
  404. if ($z == 8) $xx = 12;
  405. if ($z == 9) $xx = 1;
  406. if ($z == 10) $xx = 2;
  407. if ($z == 11) $xx = 3;
  408. if ($z == 12) $xx = 4;
  409. $str = '';
  410. $aaa = [];
  411. foreach ($t as $x => $c) {
  412. $y = $x == 'yellow' ? $c : formatPrice($c);
  413. $str .= "<a style='width:100%;float:right; text-align:right; display:block;' class=" . $x . "><small>" . $y . "</small></a><br>";
  414. $aaa[$x] = $y;
  415. }
  416. $this->totSExcel[$xx] = $aaa;
  417. $this->totS[$xx] = $str;
  418. //$js .= $xx . "§" . $str . "_";
  419. $xx += 1;
  420. }
  421. for ($e = sizeof($this->totS); $e <= 12; $e++) {
  422. $this->totS[] = '';
  423. }
  424. if ($this->sort != '') {
  425. $s = $this->sort;
  426. if ($s == 1) $s = 21;
  427. if ($column != '')
  428. array_multisort(array_column($datas, 'column_20'), SORT_ASC, SORT_NATURAL | SORT_FLAG_CASE, $datas);
  429. else
  430. array_multisort(array_column($datas, 'column_' . ($s - 2)), $this->dir == "ASC" ? SORT_ASC : SORT_DESC, SORT_NATURAL | SORT_FLAG_CASE, $datas);
  431. }
  432. $xxx = 1;
  433. foreach ($datas as $yyy => $d) {
  434. $datas[$yyy]["column_18"] = $xxx++;
  435. }
  436. $this->totalRecords = sizeof($datas);
  437. //$start = 0;
  438. $this->recordsNoPaginate = [];
  439. $this->recordsNoPaginate = $datas;
  440. //if (isset($_GET["start"]))
  441. $datas = array_slice($datas, $this->start, $this->pageLength);
  442. $this->pages = ceil($this->totalRecords / $this->pageLength);
  443. $this->records = [];
  444. $this->records = $datas;
  445. //$this->totS = $js;
  446. return view('livewire.course_list');
  447. }
  448. public function setPage($page)
  449. {
  450. $this->currentPage = $page;
  451. $this->start = $this->pageLength * ($page - 1);
  452. $this->emit('load-data-table');
  453. }
  454. public function setSort($sort)
  455. {
  456. $this->sort = $sort;
  457. if ($this->dir == '')
  458. $this->dir = 'ASC';
  459. else
  460. $this->dir = $this->dir == 'ASC' ? 'DESC' : 'ASC';
  461. $this->emit('load-data-table');
  462. }
  463. public function search()
  464. {
  465. $this->currentPage = 1;
  466. $this->start = 0;
  467. $this->hasFilter = true;
  468. $this->emit('load-data-table');
  469. }
  470. public function getColor($months, $m, $all)
  471. {
  472. $class = "wgrey";
  473. foreach (json_decode($months) as $mm) {
  474. if ($mm->m == $m) {
  475. if ($mm->status == "") {
  476. $class = "orange";
  477. }
  478. if ($mm->status == "1") {
  479. $class = "green";
  480. }
  481. if ($mm->status == "2") {
  482. $class = "yellow";
  483. }
  484. if (!$all && $class == "green") {
  485. $class = "orange";
  486. }
  487. }
  488. }
  489. return $class;
  490. }
  491. public function newPayment($course_id, $months, $member_id, $id, $subscription)
  492. {
  493. $newMonths = array();
  494. if ($months != '') {
  495. $mm = explode(",", $months);
  496. foreach ($mm as $month) {
  497. if ($month < 5) $month += 12;
  498. if ($month >= 5) $month -= 4;
  499. $newMonths[] = $month;
  500. }
  501. }
  502. $c = \App\Models\Course::findOrFail($course_id);
  503. $m = \App\Models\MemberCourse::findOrFail($id);
  504. $price = $m->price;
  505. $subscription_price = $m->subscription_price;
  506. $records = \App\Models\Record::where('member_course_id', $m->id)->where('deleted', 0)->get();
  507. foreach ($records as $record) {
  508. if (in_array($month, json_decode($record->months))) {
  509. foreach ($record->rows as $row) {
  510. if ($row->causal_id == $c->causal_id && !str_contains(strtolower($row->note), 'iscrizione')) {
  511. $tot = sizeof(json_decode($row->when));
  512. foreach (json_decode($row->when) as $m) {
  513. $price -= $row->amount / $tot;
  514. }
  515. }
  516. }
  517. }
  518. }
  519. return redirect()->to('/in?new=1&memberId=' . $member_id . (sizeof($newMonths) > 0 ? '&causalId=' . $c->causal_id : '') . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=' . ($subscription ? 1 : 0) . (sizeof($newMonths) > 0 ? '&months=' . implode("|", $newMonths) : '') . (sizeof($newMonths) > 0 ? ('&price=' . $price) : '') . '&subscription_price=' . $subscription_price . "&courseId=" . $id);
  520. }
  521. public function suspendPayment($course_id, $month, $member_id, $id, $subscription)
  522. {
  523. $monthMap = [
  524. 1 => 9, // September
  525. 2 => 10, // October
  526. 3 => 11, // November
  527. 4 => 12, // December
  528. 5 => 1, // January
  529. 6 => 2, // February
  530. 7 => 3, // March
  531. 8 => 4, // April
  532. 9 => 5, // May
  533. 10 => 6, // June
  534. 11 => 7, // July
  535. 12 => 8 // August
  536. ];
  537. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  538. $memberCourse = MemberCourse::where('id', $id)
  539. ->where('member_id', $member_id)
  540. ->where('course_id', $course_id)
  541. ->first();
  542. if (!$memberCourse) {
  543. return response()->json(['error' => 'Non Trovato'], 404);
  544. }
  545. $monthsData = json_decode($memberCourse->months, true);
  546. if (!is_array($monthsData)) {
  547. return response()->json(['error' => 'Invalid months data format'], 400);
  548. }
  549. $monthUpdated = false;
  550. foreach ($monthsData as &$monthData) {
  551. if ($monthData['m'] == $dbMonth) {
  552. $monthData['status'] = 2;
  553. $monthUpdated = true;
  554. }
  555. }
  556. if (!$monthUpdated) {
  557. return response()->json(['error' => 'Month not found in data'], 404);
  558. }
  559. $memberCourse->months = json_encode($monthsData);
  560. $memberCourse->save();
  561. session()->flash('success', 'Payment suspended successfully');
  562. return redirect()->to('/course_list');
  563. }
  564. public function resumePayment($course_id, $month, $member_id, $id, $subscription)
  565. {
  566. Log::info('resumePayment');
  567. Log::info($course_id);
  568. Log::info($month);
  569. Log::info($member_id);
  570. Log::info($id);
  571. $monthMap = [
  572. 1 => 9, // September
  573. 2 => 10, // October
  574. 3 => 11, // November
  575. 4 => 12, // December
  576. 5 => 1, // January
  577. 6 => 2, // February
  578. 7 => 3, // March
  579. 8 => 4, // April
  580. 9 => 5, // May
  581. 10 => 6, // June
  582. 11 => 7, // July
  583. 12 => 8 // August
  584. ];
  585. $dbMonth = isset($monthMap[$month]) ? $monthMap[$month] : $month;
  586. $memberCourse = MemberCourse::where('id', $id)
  587. ->where('member_id', $member_id)
  588. ->where('course_id', $course_id)
  589. ->first();
  590. if (!$memberCourse) {
  591. return response()->json(['error' => 'Non Trovato'], 404);
  592. }
  593. $monthsData = json_decode($memberCourse->months, true);
  594. Log::info('data mese', $monthsData);
  595. if (!is_array($monthsData)) {
  596. return response()->json(['error' => 'Invalid months data format'], 400);
  597. }
  598. $monthUpdated = false;
  599. foreach ($monthsData as &$monthData) {
  600. if ($monthData['m'] == $dbMonth) {
  601. $monthData['status'] = "";
  602. $monthUpdated = true;
  603. }
  604. }
  605. Log::info($monthUpdated);
  606. if (!$monthUpdated) {
  607. return response()->json(['error' => 'Month not found in data'], 404);
  608. }
  609. $memberCourse->months = json_encode($monthsData);
  610. $memberCourse->save();
  611. session()->flash('success', 'Payment resumed successfully');
  612. return redirect()->to('/course_list');
  613. }
  614. /*
  615. public function newPayment()
  616. {
  617. $c = \App\Models\Course::findOrFail($this->selectedCourseId);
  618. return redirect()->to('/in?new=1&memberId=' . $this->selectedMemberId . '&causalId=' . $c->causal_id . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=0' . (sizeof($this->payMonths) > 0 ? '&months=' . implode("|", $this->payMonths) : "") . '&price=' . $c->price . '&subscription_price=' . $c->subscription_price . "&courseId=" . $this->selectedCourseId);
  619. }
  620. */
  621. public function newSubscription($course_id, $member_id, $id)
  622. {
  623. $c = \App\Models\Course::findOrFail($course_id);
  624. return redirect()->to('/in?new=1&memberId=' . $member_id . '&causalId=' . $c->causal_id . '&subCausalId=' . $c->sub_causal_id . '&createSubscription=1&price=0.00&subscription_price=' . $c->subscription_price . "&courseId=" . $id);
  625. }
  626. public function disableSearch()
  627. {
  628. $this->filterCourse = [];
  629. $this->filterLevel = [];
  630. $this->filterType = [];
  631. $this->filterDuration = [];
  632. $this->filterFrequency = [];
  633. $this->hasFilter = false;
  634. }
  635. public function export()
  636. {
  637. $letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
  638. $spreadsheet = new Spreadsheet();
  639. $activeWorksheet = $spreadsheet->getActiveSheet();
  640. //$activeWorksheet->setCellValue('A1', 'PrimaNota');
  641. $activeWorksheet->setCellValue('A1', "Corso");
  642. $activeWorksheet->setCellValue('B1', "Cognome");
  643. $activeWorksheet->setCellValue('C1', "Nome");
  644. $activeWorksheet->setCellValue('D1', "Iscrizione");
  645. $activeWorksheet->setCellValue('E1', "Settembre");
  646. $activeWorksheet->setCellValue('F1', "Ottobre");
  647. $activeWorksheet->setCellValue('G1', "Novembre");
  648. $activeWorksheet->setCellValue('H1', "Dicembre");
  649. $activeWorksheet->setCellValue('I1', "Gennaio");
  650. $activeWorksheet->setCellValue('J1', "Febbraio");
  651. $activeWorksheet->setCellValue('K1', "Marzo");
  652. $activeWorksheet->setCellValue('L1', "Aprile");
  653. $activeWorksheet->setCellValue('M1', "Maggio");
  654. $activeWorksheet->setCellValue('N1', "Giugno");
  655. $activeWorksheet->setCellValue('O1', "Luglio");
  656. $activeWorksheet->setCellValue('P1', "Agosto");
  657. $count = 2;
  658. foreach ($this->recordsNoPaginate as $idx => $record) {
  659. $activeWorksheet->setCellValue('A' . $count, $record["column_19"]);
  660. $activeWorksheet->setCellValue('B' . $count, $record["column_0"]);
  661. $activeWorksheet->setCellValue('C' . $count, $record["column_1"]);
  662. list($color, $value) = explode("§", $record["column_2"]);
  663. $activeWorksheet->setCellValue('D' . $count, $value);
  664. $c = '#FFFFFF';
  665. if ($color == 0)
  666. $c = 'ffa500';
  667. if ($color == 1)
  668. $c = '00ff00';
  669. $activeWorksheet->getStyle('D' . $count . ':D' . $count)
  670. ->getFill()
  671. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  672. ->getStartColor()
  673. ->setARGB($c);
  674. for ($ii = 3; $ii <= 14; $ii++) {
  675. list($color, $value) = explode("§", $record["column_" . $ii]);
  676. $c = 'FFFFFF';
  677. if ($color == 'orange')
  678. $c = 'ffa500';
  679. if ($color == 'green')
  680. $c = '00ff00';
  681. if ($color == 'yellow') {
  682. $c = '5088bf';
  683. $activeWorksheet->getStyle($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count)
  684. ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);;
  685. }
  686. if ($color == 'wgrey')
  687. $value = '';
  688. $activeWorksheet->setCellValue($letters[$ii + 1] . $count, $value);
  689. $activeWorksheet->getStyle($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count)
  690. ->getFill()
  691. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  692. ->getStartColor()
  693. ->setARGB($c);
  694. /*$activeWorksheet->cells($letters[$ii + 1] . $count . ':' . $letters[$ii + 1] . $count, function ($cells) use ($c)
  695. {
  696. $cells->setBackground($c);
  697. //$cells->setAlignment('center');
  698. });*/
  699. }
  700. $activeWorksheet->getStyle("A1:P1")->getFont()->setBold(true);
  701. $count++;
  702. }
  703. // Totali
  704. $activeWorksheet->setCellValue('A' . (1 + $count), '');
  705. $activeWorksheet->setCellValue('B' . (1 + $count), '');
  706. $activeWorksheet->setCellValue('C' . (1 + $count), '');
  707. for ($x = 0; $x <= sizeof($this->totSExcel); $x++) {
  708. if (isset($this->totSExcel[$x])) {
  709. $activeWorksheet->setCellValue($letters[$x + 3] . (1 + $count), isset($this->totSExcel[$x]['green']) ? $this->totSExcel[$x]['green'] : 0);
  710. $activeWorksheet->getStyle($letters[$x + 3] . (1 + $count) . ':' . $letters[$x + 3] . (1 + $count))
  711. ->getFill()
  712. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  713. ->getStartColor()
  714. ->setARGB('00ff00');
  715. $activeWorksheet->setCellValue($letters[$x + 3] . (2 + $count), isset($this->totSExcel[$x]['orange']) ? $this->totSExcel[$x]['orange'] : 0);
  716. $activeWorksheet->getStyle($letters[$x + 3] . (2 + $count) . ':' . $letters[$x + 3] . (2 + $count))
  717. ->getFill()
  718. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  719. ->getStartColor()
  720. ->setARGB('ffa500');
  721. $activeWorksheet->setCellValue($letters[$x + 3] . (3 + $count), isset($this->totSExcel[$x]['yellow']) ? $this->totSExcel[$x]['yellow'] : 0);
  722. $activeWorksheet->getStyle($letters[$x + 3] . (3 + $count) . ':' . $letters[$x + 3] . (3 + $count))
  723. ->getFill()
  724. ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
  725. ->getStartColor()
  726. ->setARGB('5088bf');
  727. $activeWorksheet->getStyle($letters[$x + 3] . (3 + $count) . ':' . $letters[$x + 3] . (3 + $count))
  728. ->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_WHITE);;
  729. }
  730. }
  731. try {
  732. $currentClient = session('currentClient', 'default');
  733. $filename = 'pagamento_corsi_' . date("YmdHis") . '.xlsx';
  734. $tempPath = sys_get_temp_dir() . '/' . $filename;
  735. $writer = new Xlsx($spreadsheet);
  736. $writer->save($tempPath);
  737. $disk = Storage::disk('s3');
  738. $s3Path = $currentClient . '/exports/' . $filename;
  739. $exportFolderPath = $currentClient . '/exports/.gitkeep';
  740. if (!$disk->exists($exportFolderPath)) {
  741. $disk->put($exportFolderPath, '');
  742. Log::info("Created exports folder for client: {$currentClient}");
  743. }
  744. $fileContent = file_get_contents($tempPath);
  745. $uploaded = $disk->put($s3Path, $fileContent, 'private');
  746. if (!$uploaded) {
  747. throw new \Exception('Failed to upload file to Wasabi S3');
  748. }
  749. Log::info("Excel file uploaded to Wasabi", [
  750. 'client' => $currentClient,
  751. 'path' => $s3Path,
  752. 'size' => filesize($tempPath)
  753. ]);
  754. $downloadUrl = $disk->temporaryUrl($s3Path, now()->addHour());
  755. if (file_exists($tempPath)) {
  756. unlink($tempPath);
  757. }
  758. $this->emit('load-data-table');
  759. return redirect($downloadUrl);
  760. } catch (\Exception $e) {
  761. Log::error('Error exporting to Wasabi S3', [
  762. 'error' => $e->getMessage(),
  763. 'client' => session('currentClient', 'unknown')
  764. ]);
  765. $currentClient = session('currentClient', 'default');
  766. $clientFolder = storage_path('app/exports/' . $currentClient);
  767. if (!is_dir($clientFolder)) {
  768. mkdir($clientFolder, 0755, true);
  769. Log::info("Created local client folder: {$clientFolder}");
  770. }
  771. $localPath = $clientFolder . '/pagamento_corsi_' . date("YmdHis") . '.xlsx';
  772. $writer = new Xlsx($spreadsheet);
  773. $writer->save($localPath);
  774. $this->emit('load-data-table');
  775. // Show error message to user
  776. session()->flash('warning', 'File saved locally due to cloud storage error.');
  777. return response()->download($localPath)->deleteFileAfterSend();
  778. }
  779. }
  780. }