selectedCourseId = 0;
$this->selectedMemberId = 0;
$this->course_types = \App\Models\CourseType::select('*')->where('enabled', true)->get();
$this->course_durations = \App\Models\CourseDuration::select('*')->where('enabled', true)->get();
$this->course_levels = \App\Models\CourseLevel::select('*')->where('enabled', true)->get();
$this->course_frequencies = \App\Models\CourseFrequency::select('*')->where('enabled', true)->get();
$this->course_years = \App\Models\Course::select('year')->where('year', '<>', '')->groupBy('year')->pluck('year');
$this->courses = \App\Models\Course::orderBy('name')->groupBy('name')->pluck('name');
//if (sizeof($this->courses) > 0)
// $this->courseId = $this->courses[0]->id;
}
public function render()
{
// Carico tutti i corsi associati
/*
if ($this->courseId > 0)
$member_course = \App\Models\MemberCourse::where('course_id', $this->courseId)->with('member'); //->get();
else
$member_course = \App\Models\MemberCourse::with('member'); //->get();
*/
if ($this->hasFilter)
{
$member_course = \App\Models\MemberCourse::with('member');
if (sizeof($this->filterCourse) > 0)
{
$course_ids = [];
foreach($this->filterCourse as $c)
{
$all = \App\Models\Course::where('name', 'like', '%' . $c . "%")->get();
foreach($all as $a)
{
$course_ids[] = $a->id;
}
//$course_ids = array_merge($course_ids, $a);
}
//$course_ids = \App\Models\Course::where('name', 'like', '%' . $this->filterCourse . "%")->pluck('id');
$member_course = $member_course->whereIn('course_id', $course_ids);
}
if (sizeof($this->filterLevel) > 0)
{
$course_ids = \App\Models\Course::whereIn('course_level_id', $this->filterLevel)->pluck('id');
$member_course = $member_course->whereIn('course_id', $course_ids);
}
if (sizeof($this->filterFrequency) > 0)
{
$course_ids = \App\Models\Course::whereIn('course_frequency_id', $this->filterFrequency)->pluck('id');
$member_course = $member_course->whereIn('course_id', $course_ids);
}
if (sizeof($this->filterType) > 0)
{
$course_ids = \App\Models\Course::whereIn('course_type_id', $this->filterType)->pluck('id');
$member_course = $member_course->whereIn('course_id', $course_ids);
}
if (sizeof($this->filterDuration) > 0)
{
$course_ids = \App\Models\Course::whereIn('course_duration_id', $this->filterDuration)->pluck('id');
$member_course = $member_course->whereIn('course_id', $course_ids);
}
//$member_course = $member_course->where('course_id', 999999);
$member_course = $member_course->get();
}
else
{
$member_course = \App\Models\MemberCourse::with('member')->get();
}
$this->records = [];
$this->totals = [];
$this->totalIsc = [];
foreach($member_course as $x)
{
$price = 0;
$price = $x->course->price;
$subPrice = $x->course->subscription_price;
$records = \App\Models\Record::where('member_course_id', $x->id)->where('deleted', 0)->get();
$prices = [];
foreach ($records as $record)
{
foreach ($record->rows as $row)
{
//if ($row->causal_id == $x->course->sub_causal_id || str_contains(strtolower($row->note), 'iscrizione'))
if (str_contains(strtolower($row->note), 'iscrizione'))
{
$subPrice = $row->amount;
}
if ($row->causal_id == $x->course->causal_id && !str_contains(strtolower($row->note), 'iscrizione'))
{
$tot = sizeof(json_decode($row->when));
foreach(json_decode($row->when) as $m)
{
$prices[$m->month] = $row->amount / $tot;
}
}
}
}
for($i=1; $i<=12; $i++)
{
$cls = $this->getColor($x->months, $i);
if ($cls != 'grey')
{
if (!isset($this->totals[$i]))
{
$this->totals[$i]['green'] = 0;
$this->totals[$i]['orange'] = 0;
$this->totals[$i]['yellow'] = 0;
}
if ($cls == 'yellow')
{
$this->totals[$i][$cls] += 1;
}
else
{
$p = isset($prices[$i]) ? $prices[$i] : $price;
$this->totals[$i][$cls] += $p;
}
}
}
$sub = $x->subscribed ? "Y" : "N";
if (isset($this->totalIsc[$sub]))
$this->totalIsc[$sub] += $subPrice;
else
$this->totalIsc[$sub] = $subPrice;
$this->records[] = array(
$x->member_id . "§" . $x->member->first_name . "§" . $x->member->last_name,
$this->getColor($x->months, 9) . "§" . (isset($prices[9]) ? $prices[9] : $price),
$this->getColor($x->months, 10) . "§" . (isset($prices[10]) ? $prices[10] : $price),
$this->getColor($x->months, 11) . "§" . (isset($prices[11]) ? $prices[11] : $price),
$this->getColor($x->months, 12) . "§" . (isset($prices[12]) ? $prices[12] : $price),
$this->getColor($x->months, 1) . "§" . (isset($prices[1]) ? $prices[1] : $price),
$this->getColor($x->months, 2) . "§" . (isset($prices[2]) ? $prices[2] : $price),
$this->getColor($x->months, 3) . "§" . (isset($prices[3]) ? $prices[3] : $price),
$this->getColor($x->months, 4) . "§" . (isset($prices[4]) ? $prices[4] : $price),
$this->getColor($x->months, 5) . "§" . (isset($prices[5]) ? $prices[5] : $price),
$this->getColor($x->months, 6) . "§" . (isset($prices[6]) ? $prices[6] : $price),
$this->getColor($x->months, 7) . "§" . (isset($prices[7]) ? $prices[7] : $price),
$this->getColor($x->months, 8) . "§" . (isset($prices[8]) ? $prices[8] : $price),
$x->course_id,
$x->id,
$x->subscribed . "§" . $subPrice
);
}
$js = '';
$xx = 2;
$str = '';
$str .= "" . (isset($this->totalIsc["Y"]) ? formatPrice($this->totalIsc["Y"]) : 0) . "
";
$str .= "" . (isset($this->totalIsc["N"]) ? formatPrice($this->totalIsc["N"]) : 0) . "
";
$str .= "0
";
$js .= "2§" . $str . "_";
$str = "";
foreach($this->totals as $z => $t)
{
if ($z == 1) $xx = 7;
if ($z == 2) $xx = 8;
if ($z == 3) $xx = 9;
if ($z == 4) $xx = 10;
if ($z == 5) $xx = 11;
if ($z == 6) $xx = 12;
if ($z == 7) $xx = 13;
if ($z == 8) $xx = 14;
if ($z == 9) $xx = 3;
if ($z == 10) $xx = 4;
if ($z == 11) $xx = 5;
if ($z == 12) $xx = 6;
$str = '';
foreach($t as $x => $c)
{
$y = $x == 'yellow' ? $c : formatPrice($c);
$str .= "" . $y . "
";
}
$js .= $xx . "§" . $str . "_";
$xx += 1;
}
//$this->aaa = $js;
//$this->emit('setTotals', $js);
$this->emit('load-data-table');
return view('livewire.course_list');
}
public function search()
{
$this->hasFilter = true;
}
public function getColor($months, $m)
{
$class = "grey";
foreach(json_decode($months) as $mm)
{
if ($mm->m == $m)
{
if ($mm->status == "")
{
$class = "orange";
}
if ($mm->status == "1")
{
$class = "green";
}
if ($mm->status == "2")
{
$class = "yellow";
}
}
}
return $class;
}
public function newPayment($course_id, $months, $member_id, $id, $subscription)
{
$newMonths = array();
if ($months != '')
{
$mm = explode(",", $months);
foreach($mm as $month)
{
if ($month < 5) $month += 12;
if ($month >= 5) $month -= 4;
$newMonths[] = $month;
}
}
$c = \App\Models\Course::findOrFail($course_id);
$m = \App\Models\MemberCourse::findOrFail($id);
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=' . $m->price : '') . '&subscription_price=' . $m->subscription_price . "&courseId=" . $id);
}
/*
public function newPayment()
{
$c = \App\Models\Course::findOrFail($this->selectedCourseId);
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);
}
*/
public function newSubscription($course_id, $member_id, $id)
{
$c = \App\Models\Course::findOrFail($course_id);
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);
}
public function disableSearch()
{
$this->filterCourse = [];
$this->filterLevel = [];
$this->filterType = [];
$this->filterDuration = [];
$this->filterFrequency = [];
$this->hasFilter = false;
}
}