File: /mnt/data/ghayatcom/ghayatcom-api/app/Library/DoctorAvailabilityLibrary.php
<?php
namespace App\Library;
use App\ScheduledAppointment;
use App\ScheduledAppointmentDetail;
use App\User;
use App\UserDetails;
use DB;
use Illuminate\Support\Carbon;
class DoctorAvailabilityLibrary {
function consultationScheduleConflict($user_id, $arr, $system, $update_id="") {
if(!empty($update_id)) {
$query = ScheduledAppointment::with('scheduledAppointmentDetail')->where('id', '!=' , $update_id)->where('doctor_id', $user_id)->get();
} else {
$query = ScheduledAppointment::with('scheduledAppointmentDetail')->where('doctor_id', $user_id)->get();
}
$data = $arr;
if(count($query)) {
if($system == "from_homevisit") {
$from_time_tbl = $data['home_visit_from_time'];
$to_time_tbl = $data['home_visit_to_time'];
$days_explode = explode(",", $data['home_visit_weekdays']);
$days_match = array_filter($days_explode);
$conflictArr = [];
$innerDateCondition = date('Y-m-d');
for ($dd=0; $dd < count($query); $dd++) {
$from_date = date('Y-m-d', strtotime($query[$dd]->from_date));
$date_until = date('Y-m-d', strtotime($query[$dd]->date_until));
if($from_date <= $innerDateCondition && $innerDateCondition <= $date_until) {
$week_days_explode = explode(",", $query[$dd]->week_days);
$week_days_match = array_filter($week_days_explode);
$arr = array_intersect($days_match,$week_days_match);
$lvl = array_values($arr);
if(count($lvl) > 0) {
$time_arr = $query[$dd]->scheduledAppointmentDetail;
$lvl_time = [];
for ($i=0; $i < count($time_arr); $i++) {
$fromCatch = $toCatch = "";
$from_time = date('H:i:s', strtotime($time_arr[$i]->from_time));
$to_time = date('H:i:s', strtotime($time_arr[$i]->to_time));
$status = (($from_time_tbl > $from_time) && ($from_time_tbl < $to_time)) || (($to_time_tbl > $from_time) && ($to_time_tbl < $to_time)) || (($from_time > $from_time_tbl) && ($from_time < $to_time_tbl)) || (($to_time > $from_time_tbl) && ($to_time < $to_time_tbl));
if($status){
$fromCatch = 1;
}
/*if(strtotime($from) > strtotime($from_time_tbl) && strtotime($from) < strtotime($to_time_tbl) ) {
$fromCatch = 1;
}
if(strtotime($from_time_tbl) > strtotime($from) && strtotime($to_time_tbl) < strtotime($from) ) {
$fromCatch = 1;
}
if(strtotime($to) > strtotime($from_time_tbl) && strtotime($to) < strtotime($to_time_tbl) ) {
$toCatch = 2;
}
if(strtotime($from_time_tbl) > strtotime($to) && strtotime($to_time_tbl) < strtotime($to) ) {
$toCatch = 2;
}*/
if($fromCatch != "") {
$lvl_time[] = 1;
}
}
if(count($lvl_time) > 0) {
$conflictArr[] = 1;
}
}
}
}
if(count($conflictArr) > 0) {
return true;
} else {
return false;
}
} else if($system == "from_ondemand") {
/* For test purpose
$day = date('D', strtotime('2022-02-02'));
$demand_time = date('H:i:s', strtotime($data['online_until']));
$demand_date = date('Y-m-d', strtotime('2022-02-02'));*/
$day = date('D');
$demand_start = date('H:i:s');
$demand_time = date('H:i:s', strtotime($data['online_until']));
$demand_date = date('Y-m-d');
$conflictArr = [];
for ($dd=0; $dd < count($query); $dd++) {
$fromDate = $query[$dd]->from_date;
$toDate = $query[$dd]->date_until;
if($fromDate <= $demand_date && $demand_date <= $toDate) {
$days_explode = explode(",", $query[$dd]->week_days);
$days_match = array_filter($days_explode);
if (in_array($day, $days_match)) {
$time_arr = $query[$dd]->scheduledAppointmentDetail;
$lvl_time = [];
for ($i=0; $i < count($time_arr); $i++) {
$fromCatch = $toCatch = "";
$from_time = date('H:i:s', strtotime($time_arr[$i]->from_time));
$to_time = date('H:i:s', strtotime($time_arr[$i]->to_time));
// if($from <= $demand_time && $demand_time <= $to) {
// if(($from_time <= $demand_time && $demand_time < $to_time) || ) {
// $lvl_time[] = 1;
// }
$status = (($demand_start > $from_time) && ($demand_start < $to_time)) || (($demand_time > $from_time) && ($demand_time < $to_time)) || (($from_time > $demand_start) && ($from_time < $demand_time)) || (($to_time > $demand_start) && ($to_time < $demand_time));
if($status){
$lvl_time[] = 1;
}
}
if(count($lvl_time) > 0) {
$conflictArr[] = 1;
}
}
}
}
if(count($conflictArr) > 0) {
return true;
} else {
return false;
}
} else if($system == "from_schedule") {
$fromInput = $data['from_date'];
$toInput = $data['date_until'];
$weekDaysInput = $data['week_days'];
$innerDateCondition = date('Y-m-d');
$days_explode = explode(",", $weekDaysInput);
$days_match = array_filter($days_explode);
$from_to_time = $data['from_to_time'];
if(!empty($update_id)) {
$dateConflictArr = [];
for($dd=0; $dd < count($query); $dd++) {
$from_date_query = $query[$dd]->from_date;
$to_date_query = $query[$dd]->date_until;
$week_days_query = explode(",", $query[$dd]->week_days);
$week_days_query = array_filter($week_days_query);
if($from_date_query <= $innerDateCondition && $innerDateCondition <= $to_date_query) {
$arrWeekConflict = array_intersect($days_match,$week_days_query);
$lvl = array_values($arrWeekConflict);
if(count($lvl) > 0) {
$scheduleDetailData = $query[$dd]->scheduledAppointmentDetail;
$startCatch = $endCatch = "";
if ((strtotime($fromInput) >= strtotime($from_date_query)) && (strtotime($fromInput) <= strtotime($to_date_query)))
{
for($tt=0; $tt < count($scheduleDetailData); $tt++)
{
$sch_from_time = $scheduleDetailData[$tt]->from_time;
$sch_to_time = $scheduleDetailData[$tt]->to_time;
for($tc = 0; $tc < count($from_to_time); $tc++)
{
$from_time_check = $from_to_time[$tc]['from'];
$to_time_check = $from_to_time[$tc]['to'];
$status = (($from_time_check > $sch_from_time) && ($from_time_check < $sch_to_time)) || (($to_time_check > $sch_from_time) && ($to_time_check < $sch_to_time)) || (($sch_from_time > $from_time_check) && ($sch_from_time < $to_time_check)) || (($sch_to_time > $from_time_check) && ($sch_to_time < $to_time_check));
if($status){
$startCatch = 1;
}
}
}
// if (strtotime($from_time_check) > strtotime($sch_from_time) && strtotime($from_time_check) < strtotime($sch_to_time))
// {
// $startCatch = 1;
// }
// }
// }
// $startCatch = 1;
}
if ((strtotime($toInput) >= strtotime($from_date_query)) && (strtotime($toInput) <= strtotime($to_date_query)))
{
for($tt=0; $tt < count($scheduleDetailData); $tt++)
{
$sch_from_time = $scheduleDetailData[$tt]->from_time;
$sch_to_time = $scheduleDetailData[$tt]->to_time;
for($tc = 0; $tc < count($from_to_time); $tc++)
{
$from_time_check = $from_to_time[$tc]['from'];
$to_time_check = $from_to_time[$tc]['to'];
$status = (($from_time_check > $sch_from_time) && ($from_time_check < $sch_to_time)) || (($to_time_check > $sch_from_time) && ($to_time_check < $sch_to_time)) || (($sch_from_time > $from_time_check) && ($sch_from_time < $to_time_check)) || (($sch_to_time > $from_time_check) && ($sch_to_time < $to_time_check));
if($status){
$endCatch = 2;
}
}
}
/*for($time_check = 0; $time_check < count($from_to_time); $time_check++)
{
$to_time_check = $from_to_time[$time_check]['to'];
for($tt=0; $tt < count($scheduleDetailData); $tt++)
{
$sch_from_time = $scheduleDetailData[$tt]->from_time;
$sch_to_time = $scheduleDetailData[$tt]->to_time;
if (strtotime($to_time_check) > strtotime($sch_from_time) && strtotime($to_time_check) < strtotime($sch_to_time))
{
$endCatch = 2;
}
}
}*/
// $endCatch = 2;
}
if($startCatch != "" || $endCatch != "") {
$dateConflictArr[] = 1;
}
}
}
}
if(count($dateConflictArr) > 0) {
return true;
} else {
return false;
}
} else {
$dateConflictArr = [];
for($dd=0; $dd < count($query); $dd++) {
$from_date_query = $query[$dd]->from_date;
$to_date_query = $query[$dd]->date_until;
$week_days_query = explode(",", $query[$dd]->week_days);
$week_days_query = array_filter($week_days_query);
$arrWeekConflict = array_intersect($days_match,$week_days_query);
$lvl = array_values($arrWeekConflict);
if(count($lvl) > 0) {
$scheduleDetailData = $query[$dd]->scheduledAppointmentDetail;
$startCatch = $endCatch = "";
if ((strtotime($fromInput) >= strtotime($from_date_query)) && (strtotime($fromInput) <= strtotime($to_date_query))) {
for($tt=0; $tt < count($scheduleDetailData); $tt++)
{
$sch_from_time = $scheduleDetailData[$tt]->from_time;
$sch_to_time = $scheduleDetailData[$tt]->to_time;
for($tc = 0; $tc < count($from_to_time); $tc++)
{
$from_time_check = $from_to_time[$tc]['from'];
$to_time_check = $from_to_time[$tc]['to'];
$status = (($from_time_check > $sch_from_time) && ($from_time_check < $sch_to_time)) || (($to_time_check > $sch_from_time) && ($to_time_check < $sch_to_time)) || (($sch_from_time > $from_time_check) && ($sch_from_time < $to_time_check)) || (($sch_to_time > $from_time_check) && ($sch_to_time < $to_time_check));
if($status){
$startCatch = 1;
}
}
}
/*for($time_check = 0; $time_check < count($from_to_time); $time_check++)
{
$from_time_check = $from_to_time[$time_check]['from'];
// $to_time_check = $from_to_time[$time_check]['to'];
for($tt=0; $tt < count($scheduleDetailData); $tt++)
{
$sch_from_time = $scheduleDetailData[$tt]->from_time;
$sch_to_time = $scheduleDetailData[$tt]->to_time;
if (strtotime($from_time_check) > strtotime($sch_from_time) && strtotime($from_time_check) < strtotime($sch_to_time))
{
$startCatch = 1;
}
}
}*/
// $startCatch = 1;
}
if ((strtotime($toInput) >= strtotime($from_date_query)) && (strtotime($toInput) <= strtotime($to_date_query))) {
for($tt=0; $tt < count($scheduleDetailData); $tt++)
{
$sch_from_time = $scheduleDetailData[$tt]->from_time;
$sch_to_time = $scheduleDetailData[$tt]->to_time;
for($tc = 0; $tc < count($from_to_time); $tc++)
{
$from_time_check = $from_to_time[$tc]['from'];
$to_time_check = $from_to_time[$tc]['to'];
$status = (($from_time_check > $sch_from_time) && ($from_time_check < $sch_to_time)) || (($to_time_check > $sch_from_time) && ($to_time_check < $sch_to_time)) || (($sch_from_time > $from_time_check) && ($sch_from_time < $to_time_check)) || (($sch_to_time > $from_time_check) && ($sch_to_time < $to_time_check));
if($status){
$endCatch = 2;
}
}
}
/*for($time_check = 0; $time_check < count($from_to_time); $time_check++)
{
// $from_time_check = $from_to_time[$time_check]['from'];
$to_time_check = $from_to_time[$time_check]['to'];
for($tt=0; $tt < count($scheduleDetailData); $tt++)
{
$sch_from_time = $scheduleDetailData[$tt]->from_time;
$sch_to_time = $scheduleDetailData[$tt]->to_time;
if (strtotime($to_time_check) > strtotime($sch_from_time) && strtotime($to_time_check) < strtotime($sch_to_time))
{
$endCatch = 2;
}
}
}*/
// $endCatch = 2;
}
if($startCatch != "" || $endCatch != "") {
$dateConflictArr[] = 1;
}
}
}
if(count($dateConflictArr) > 0) {
return true;
} else {
return false;
}
}
}
} else { return false; }
}
public function consultationOndemandConflict($user_id, $arr, $system) {
$query = UserDetails::where('user_id', $user_id)->first();
$data = $arr;
if($system == "from_homevisit") {
$status = $query->on_demand_available_status;
if($status == 1) {
$days_explode = explode(",", $data['home_visit_weekdays']);
$days_match = array_filter($days_explode);
$demandDay = date('D', strtotime($query->on_demand_time_out));
$demand_start = date("H:i:s");
$demand_time = date('H:i:s', strtotime($query->on_demand_time_out));
if (in_array($demandDay, $days_match)) {
$from_time = date('H:i:s', strtotime($data['home_visit_from_time']));
$to_time = date('H:i:s', strtotime($data['home_visit_to_time']));
$status = (($demand_start > $from_time) && ($demand_start < $to_time)) || (($demand_time > $from_time) && ($demand_time < $to_time)) || (($from_time > $demand_start) && ($from_time < $demand_time)) || (($to_time > $demand_start) && ($to_time < $demand_time));
if($status){
return true;
}
else {
return false;
}
/*if($from <= $demand_time && $demand_time <= $to) {
return true;
} else {
return false;
}*/
} else {
return false;
}
}
} else if($system == "from_schedule") {
$from = $data['from_date'];
$to = $data['date_until'];
$time_arr = $data['from_to_time'];
$status = $query->on_demand_available_status;
$demand_date = date('Y-m-d',strtotime($query->on_demand_time_out));
$demand_start = date("H:i:s");
$demand_end = date('H:i:s',strtotime($query->on_demand_time_out));
if($status == 1) {
if($from <= $demand_date && $demand_date <= $to) {// && $demand_date <= $to
$lvl_time = [];
for ($i=0; $i < count($time_arr); $i++) {
$fromCatch = $toCatch = "";
$from_time = date('H:i:s', strtotime($time_arr[$i]['from']));
$to_time = date('H:i:s', strtotime($time_arr[$i]['to']));
$status = (($demand_start > $from_time) && ($demand_start < $to_time)) || (($demand_end > $from_time) && ($demand_end < $to_time)) || (($from_time > $demand_start) && ($from_time < $demand_end)) || (($to_time > $demand_start) && ($to_time < $demand_end));
if($status){
$lvl_time[] = $i+1;
}
// if($from_time <= $demand_time && $demand_time <= $to_time) {
// $lvl_time[] = $i+1;
// }
}
if(count($lvl_time) > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
}
public function consultationHomeVisitConflict($user_id, $arr, $system) {
$query = UserDetails::where('user_id', $user_id)->first();
$data = $arr;
if($system == "from_schedule") {
$from = $data['from_date'];
$to = $data['date_until'];
$days_explode = explode(",", $data['week_days']);
$days_match = array_filter($days_explode);
$time_arr = $data['from_to_time'];
$week_days_explode = explode(",", $query->home_visit_weekdays);
$week_days_match = array_filter($week_days_explode);
$from_time_tbl = $query->home_visit_from_time;
$to_time_tbl = $query->home_visit_to_time;
$arr = array_intersect($days_match,$week_days_match);
$lvl = array_values($arr);
if(count($lvl) > 0) {
$lvl_time = [];
for ($i=0; $i < count($time_arr); $i++) {
$fromCatch = $toCatch = "";
$from_time = date('H:i:s', strtotime($time_arr[$i]['from']));
$to_time = date('H:i:s', strtotime($time_arr[$i]['to']));
$status = (($from_time_tbl > $from_time) && ($from_time_tbl < $to_time)) || (($to_time_tbl > $from_time) && ($to_time_tbl < $to_time)) || (($from_time > $from_time_tbl) && ($from_time < $to_time_tbl)) || (($to_time > $from_time_tbl) && ($to_time < $to_time_tbl));
if($status){
$fromCatch = 1;
}
/*if(strtotime($from) > strtotime($from_time_tbl) && strtotime($from) < strtotime($to_time_tbl) ) {
$fromCatch = 1;
}
if(strtotime($to) > strtotime($from_time_tbl) && strtotime($to) < strtotime($to_time_tbl) ) {
$toCatch = 2;
}*/
if($fromCatch != "") {
$lvl_time[] = 1;
}
}
if(count($lvl_time) > 0) {
return true;
} else {
return false;
}
} else {
return false;
}
} else if($system == "from_ondemand") {
$days_explode = explode(",", $query->home_visit_weekdays);
$days_match = array_filter($days_explode);
/* For test purpose
$day = date('D', strtotime('2022-02-01'));
$demand_time = date('H:i:s', strtotime($data['online_until']));*/
$day = date('D');
$demand_start = date('H:i:s');
$demand_end = date('H:i:s', strtotime($data['online_until']));
if (in_array($day, $days_match)) {
$from_time = $query->home_visit_from_time;
$to_time = $query->home_visit_to_time;
$status = (($demand_start > $from_time) && ($demand_start < $to_time)) || (($demand_end > $from_time) && ($demand_end < $to_time)) || (($from_time > $demand_start) && ($from_time < $demand_end)) || (($to_time > $demand_start) && ($to_time < $demand_end));
if($status){
return true;
}
else {
return false;
}
// if($from <= $demand_time && $demand_time <= $to) {
/*if($from <= $demand_time && $demand_time < $to) {
return true;
} else {
return false;
}*/
} else {
return false;
}
}
}
}