Skip to content

Commit

Permalink
Finished command for generating CSV schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ajovanoski committed Mar 16, 2021
1 parent bce495e commit 4edd5e7
Show file tree
Hide file tree
Showing 8,087 changed files with 164,505 additions and 143 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions app/Activity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Activity extends Model
{
//
}
56 changes: 56 additions & 0 deletions app/Console/Commands/CreateSchedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Controllers\ScheduleController;

class CreateSchedule extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'command:generate_schedule';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Command that will generate CSV schedule for next 90 days of entered date for cleaning office.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$startDate = $this->ask('Enter date [format: yyyy-mm-dd] ');
$validator = \Validator::make(['date' => $startDate], ['date' => 'required|date|date_format:Y-m-d']);
if ($validator->fails()) {
$message = $validator->messages();
$this->info($message);
return 0;
} else {
$scheduleController = new ScheduleController();
$scheduleController->generateSchedule($startDate);
$this->info('Generate schedule from date : ' . $startDate);
$this->info('CSV file created successfully!');

return 0;
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
150 changes: 150 additions & 0 deletions app/Http/Controllers/ScheduleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

namespace App\Http\Controllers;
use \Illuminate\Http\Request;
use App\Activity;
USE DB;

class ScheduleController extends Controller
{
public function generateSchedule($startDate)
{
$getActivities = Activity::all();
$activitiesArray = [];
if (!empty($getActivities)) {
foreach ($getActivities as $activity) {
$activitiesArray[] = [
'id' => $activity->id,
'name' => $activity->name,
'duration' => $activity->duration
];
}
}

// next 3 months
$duration = 90;
// define empty array
$lastWorkingDaysDates = [];

// find lastWorkingDays for next 3 months
for ($i = 0; $i < 3; $i++) {
$checkDate = date('Y-m-d', strtotime('+'. $i .' month', strtotime($startDate)));
$lastDateOfMonth = date("Y-m-t", strtotime($checkDate));

$lastWorkingDaysDates[$i] = $this->detectLastWorkingDay($checkDate, $lastDateOfMonth);
}

$schedule = [];
// get month (in number format)
$month = date("m", strtotime($startDate));
// set ref cleaning to 0
$ref = 0;
// generate data for next 3 months
for ($i = 0; $i < $duration; $i++) {
// day by day
$currentDayRow = date('Y-m-d', strtotime('+'. $i .' day', strtotime($startDate)));
// get month of current date
$checkMonth = date("m", strtotime($currentDayRow));
// get date name of current date
$dayName = date("l", strtotime($currentDayRow));

// set empty values ( we can comment this if we don't want empty rows into CSV file.)
$schedule[$i]['date'] = $currentDayRow;
$schedule[$i]['day'] = $dayName;
$schedule[$i]['activities'] = '';
$schedule[$i]['duration'] = '';

// get day date (number format)
$day = date("j", strtotime($currentDayRow));
// check if it's Tuesday or Thursday
if ($dayName == 'Tuesday' || $dayName == 'Thursday') {
// set default values
$act = $activitiesArray[0]['name'];
$timeEst = date('H:i', mktime(0, $activitiesArray[0]['duration']));
// if ref is not cleaned or if it's first(tuesday/thursday) of the month
if ($ref === 0 || $day === '1') {
$act = $activitiesArray[0]['name'] . '|' . $activitiesArray[2]['name'];
$timeEst = date('H:i', mktime(0, $activitiesArray[0]['duration'] + $activitiesArray[2]['duration']));

// increase ref + 1 cos now it's clened
$ref++;
// change month if
$month = $checkMonth;
}

$schedule[$i]['date'] = $currentDayRow;
$schedule[$i]['day'] = $dayName;
$schedule[$i]['activities'] = $act;
$schedule[$i]['duration'] = $timeEst;
}
// if month is passed update initial variable and reset the ref to 0
if ($month != $checkMonth) {
$ref = 0;
$month = $checkMonth;
}

// check if it's last working day
if (in_array($currentDayRow, $lastWorkingDaysDates)) {
$act = $activitiesArray[1]['name'];
$timeEst = date('H:i', mktime(0, $activitiesArray[1]['duration']));
// check if there is any other activity added before
if (!empty($schedule[$i]['activities'])) {
$act = $activitiesArray[0]['name'] . '|' . $activitiesArray[1]['name'];
$timeEst = date('H:i', mktime(0, $activitiesArray[0]['duration'] + $activitiesArray[1]['duration']));
}

$schedule[$i]['date'] = $currentDayRow;
$schedule[$i]['day'] = $dayName;
$schedule[$i]['activities'] = $act;
$schedule[$i]['duration'] = $timeEst;
}
}
// export CSV
$this->exportCsvFile($schedule, 'schedule-' . $startDate);
}

public function detectLastWorkingDay($date, $lastDateOfMonth)
{
// set default value
$lastWorkingDay = date('Y-m-d', strtotime($date));
$lastDayName = date("l",strtotime($lastDateOfMonth));

// If it's Sunday or Saturday we set Friday as last working day
$checkLastWorkingDay = $this->getLastWorkingDayDate($lastDayName, $lastDateOfMonth);
if (!empty($checkLastWorkingDay)) {
$lastWorkingDay = $checkLastWorkingDay;
}

return $lastWorkingDay;
}

public function getLastWorkingDayDate($name, $lastDateOfMonth)
{
switch ($name) {
case 'Sunday':
$lastWorkingDay = date('Y-m-d', strtotime('-2 day', strtotime($lastDateOfMonth)));
break;
case 'Saturday':
$lastWorkingDay = date('Y-m-d', strtotime('-1 day', strtotime($lastDateOfMonth)));
break;
default:
$lastWorkingDay = $lastDateOfMonth;
}

return $lastWorkingDay;
}

public function exportCsvFile($data, $filename)
{
header('Content-Type: text/csv;');
header('Content-Disposition: attachment; filename=' . $filename . '.csv');
header("Pragma: no-cache");
header("Expires: 0");
$fh = fopen('./csv/'. $filename . '.csv' , 'w');
fputcsv($fh, array('Date','Day','Activities','Duration(HH:MM)'));
foreach ($data as $row) {
fputcsv($fh, array($row['date'],$row['day'],$row['activities'],$row['duration']));
}
}
}

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 2 additions & 1 deletion scheduler/composer.json → composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^6.3.1|^7.0.1",
"laravel/framework": "^7.29",
"laravel/tinker": "^2.5"
"laravel/tinker": "^2.5",
"maatwebsite/excel": "^3.1"
},
"require-dev": {
"facade/ignition": "^2.0",
Expand Down
Loading

0 comments on commit 4edd5e7

Please sign in to comment.