CourseList.php 35 KB

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