Laravel in create common function
<?php
namespace App\Common;
class BpPulse_progress {
private $user_id;
private $start_date_systolic;
private $goal_date_systolic;
private $goal_systolic;
private $last_progress_systolic=0;
private $last_week_systolic_count=0;
private $last_month_systolic_count=0;
private $last_systolic_log_date;
// diastolic
private $start_date_diastolic;
private $goal_date_diastolic;
private $goal_diastolic;
private $last_progress_diastolic=0;
private $last_week_diastolic_count=0;
private $last_month_diastolic_count=0;
private $last_diastolic_log_date;
// pulse
private $start_date_pulse;
private $goal_date_pulse;
private $goal_pulse;
private $last_progress_pulse=0;
private $last_week_pulse_count=0;
private $last_month_pulse_count=0;
private $last_pulse_log_date;
public function __construct($bppulse){
$this->user_id = $bppulse['user_id'];
$this->start_date_systolic = $bppulse['start_date_systolic'];
$this->goal_date_systolic = $bppulse['goal_date_systolic'];
$this->goal_systolic = $bppulse['systolic'];
$this->start_date_diastolic = $bppulse['start_date_diastolic'];
$this->goal_date_diastolic = $bppulse['goal_date_diastolic'];
$this->goal_diastolic = $bppulse['diastolic'];
$this->start_date_pulse = $bppulse['start_date_pulse'];
$this->goal_date_pulse = $bppulse['goal_date_pulse'];
$this->goal_pulse = $bppulse['heart_rate'];
}
public function getBpPulseProgress(){
$data=[];
$this->calculate_systolic_Progress();
$this->calculate_diastolic_Progress();
$this->calculate_pulse_Progress();
$data['goal_date_systolic'] = date('m-d-Y',strtotime($this->start_date_systolic));
$data['last_progress_systolic'] =$this->last_progress_systolic;
$data['last_week_count_systolic'] =$this->last_week_systolic_count;
$data['last_month_count_systolic'] =$this->last_month_systolic_count;
$data['goal_date_diastolic'] = date('m-d-Y',strtotime($this->start_date_diastolic));
$data['last_progress_diastolic'] =$this->last_progress_diastolic;
$data['last_week_count_diastolic'] =$this->last_week_diastolic_count;
$data['last_month_count_diastolic'] =$this->last_month_diastolic_count;
$data['goal_date_pulse'] = date('m-d-Y',strtotime($this->start_date_pulse));
$data['last_progress_pulse'] =$this->last_progress_pulse;
$data['last_week_count_pulse'] =$this->last_week_pulse_count;
$data['last_month_count_pulse'] =$this->last_month_pulse_count;
return $data;
}
public function calculate_systolic_Progress(){
$current_date = \Carbon\Carbon::now()->subDays(30);
$last_weeks = \Carbon\Carbon::now()->subDays(7);
$last_month = $current_date->format('Y-m-d');
$last_week_date = $last_weeks->format('Y-m-d');
$last_week_timstamp = $last_weeks->timestamp;
$user_systolic = \App\Models\Blood_pressure_pulse::where('user_id',$this->user_id)
->where('log_date','>=',$this->start_date_systolic)
->where('log_date','>=',$last_month)
->orderBy('log_date','desc')
->get();
if($user_systolic->count() > 0){
$systolic_log_date_arr = $user_systolic->groupBy("log_date");
$i =1;
foreach ($systolic_log_date_arr as $key1 => $log_date) {
$systolic_last = $log_date->last();
$reading = $systolic_last->systolic;
$calculatePercent = calculateBpPercent($reading, $this->goal_systolic);
$log_timestamp =\Carbon\Carbon::parse($key1)->timestamp;
if($last_week_timstamp <= $log_timestamp){
if($calculatePercent >= 95){
$this->last_week_systolic_count++;
}
}
// monthly progress
if($calculatePercent >= 95){
$this->last_month_systolic_count++;
}
if($i == 1){
$this->last_progress_systolic =$calculatePercent;
}
$i++;
}
}
}
public function calculate_diastolic_Progress(){
$current_date = \Carbon\Carbon::now()->subDays(30);
$last_weeks = \Carbon\Carbon::now()->subDays(7);
$last_month = $current_date->format('Y-m-d');
$last_week_date = $last_weeks->format('Y-m-d');
$last_week_timstamp = $last_weeks->timestamp;
$user_diastolic = \App\Models\Blood_pressure_pulse::where('user_id',$this->user_id)
->where('log_date','>=',$this->start_date_diastolic)
->where('log_date','>=',$last_month)
->orderBy('log_date','desc')
->get();
if($user_diastolic->count() > 0){
$diastolic_log_date_arr = $user_diastolic->groupBy("log_date");
$i =1;
foreach ($diastolic_log_date_arr as $key1 => $log_date) {
$diastolic_last = $log_date->last();
$reading = $diastolic_last->diastolic;
$calculatePercent = calculateBpPercent($reading, $this->goal_diastolic);
$log_timestamp =\Carbon\Carbon::parse($key1)->timestamp;
if($last_week_timstamp <= $log_timestamp){
if($calculatePercent >= 95){
$this->last_week_diastolic_count++;
}
}
// monthly progress
if($calculatePercent >= 95){
$this->last_month_diastolic_count++;
}
if($i == 1){
$this->last_progress_diastolic =$calculatePercent;
}
$i++;
}
}
}
public function calculate_pulse_Progress(){
$current_date = \Carbon\Carbon::now()->subDays(30);
$last_weeks = \Carbon\Carbon::now()->subDays(7);
$last_month = $current_date->format('Y-m-d');
$last_week_date = $last_weeks->format('Y-m-d');
$last_week_timstamp = $last_weeks->timestamp;
$user_pulse = \App\Models\User_pulse::where('user_id',$this->user_id)
->where('log_date','>=',$this->start_date_pulse)
->where('log_date','>=',$last_month)
->orderBy('log_date','desc')
->get();
if($user_pulse->count() > 0){
$pulse_log_date_arr = $user_pulse->groupBy("log_date");
$i =1;
foreach ($pulse_log_date_arr as $key1 => $log_date) {
$pulse_last = $log_date->last();
$reading = $pulse_last->pulse;
$calculatePercent = calculateBpPercent($reading, $this->goal_pulse);
$log_timestamp =\Carbon\Carbon::parse($key1)->timestamp;
if($last_week_timstamp <= $log_timestamp){
if($calculatePercent >= 95){
$this->last_week_pulse_count++;
}
}
// monthly progress
if($calculatePercent >= 95){
$this->last_month_pulse_count++;
}
if($i == 1){
$this->last_progress_pulse =$calculatePercent;
}
$i++;
}
}
}
}