Find Date and Time clashes - algorithm

I'm having a hard time identifying booking clashes in my system. I've developed a booking system that integrates with our core system. The column data that is stored in my booking system is:
ResourceID, StartDate, EndDate, StartTime, EndTime
I need to ensure that when another user tries to book a resource, the resource is available. This question has been asked by many, but its usually with one date and start and end time or intersecting between a start date and enddate .
If I wanted to solely check on intersecting dates or time, the formula to use would be
a=existing_booking
b=new_booking
overlap = a.start < b.end && b.start < a.end;
I then found a really good and interesting post regarding overlapping resources containing both start time and end time Code Logic to prevent clash between two reservations but they deal with a reoccurring event over multiple days.
I need to ensure if a resource is booked between 8:50 to 22:00 from the 07-23-2016 to 07-29-2016 ; that someone trying to book on the 07-25-2016 to 07-25-2016 at 08:00 to 08:30 can't book it as it is booked out. After numerous searches I can't seem to find the formula for this, can anyone help post the algorithm or send me a link to an existing answer as its driving me nuts.

could you try the below logic?
01 if (new.StartDate > existing.EndDate OR new.EndDate < existing.StartDate) {
02 resource is available for booking
03 } else if (new.EndDate = existing.StartDate AND new.StartDate <= existing.StartDate) {
04 if (new.EndTime =< existing.StartTime OR new.StartTime >= existing.EndTime) {
05 resource is available for booking
06 } else {
07 resource is NOT available for booking
08 }
09 } else if (new.StartDate = existing.EndDate) {
10 if (new.StartTime >= existing.EndTime) {
11 resource is available for booking
12 } else {
13 resource is NOT available for booking
14 }
15 } else {
16 resource is NOT available for booking
17 }
lets check on a case to case basis;
pass - resource is available
fail - resource is NOT available
An existing booking is in place
ResourceID | StartDate | EndDate | StartTime | EndTime
ID1234 | 07-23-2016 | 07-29-2016 | 08:50 | 22:00
Case 1
ResourceID | StartDate | EndDate | StartTime | EndTime
ID1234 | 07-25-2016 | 07-25-2016 | 08:00 | 08:30
result - this case would fail; #line 16 in the code above
Case 2
ResourceID | StartDate | EndDate | StartTime | EndTime
ID1234 | 07-23-2016 | 07-23-2016 | 08:00 | 08:30
result - this case would pass; #line 05 in the code above
Case 3
ResourceID | StartDate | EndDate | StartTime | EndTime
ID1234 | 07-29-2016 | 07-29-2016 | 22:01 | 22:30
result - this case would pass; #line 11 in the code above
Case 4
ResourceID | StartDate | EndDate | StartTime | EndTime
ID1234 | 07-23-2016 | 07-23-2016 | 09:00 | 11:30
result - this case would fail; #line 07 in the code above
Please do check and let me know if for any case the above logic would be giving an in correct answer.

Related

Laravel GroupBy multiple columns with date

I have these fields in my table, I want get the data by month of the selected year & type_id, then SUM the amount on each month.
Ex: selectYear = 2019
Here is my account_table
id | type_id | amount | kind | created_at
1 | 2 | 200 | expense | 2019-01-15 10:14:31
2 | 2 | 400 | income | 2019-01-15 10:14:31
3 | 2 | 300 | expense | 2018-01-15 10:14:31
4 | 5 | 100 | expense | 2019-02-15 10:14:31
5 | 5 | 200 | income | 2019-03-15 10:14:31
this what I expected output.
if kind is expense the amount is -,
if kind is income the amount is +
Ex. on the month of Jan.
id 1 has 200 expense, and id 2 has 400 income
formula -200 + 400
Jan | Feb | Mar | ... so on
200 | 100 | 200 | ... so on
I hope I delivered and explain well, I am stuck on this functions, please I need help,
UPDATE.
Expected OUTPUT
Thanks in advance.
first, you must define or parse your year and month to numeric value. ex : January become 01, February become 02 etc.
in example :
$year = 2019;
$month = 01;
And you can use this :
$total = 0;
$data = DB::table('account_table')->whereMonth('created_at',$month)->whereYear('created_at',$year)->where('type_id',$type_id)->get;
foreach($data as $value){
if($value->kind == 'income'){
$total += $value->amount;
else{
$total -= $value->amount;
}
}
return $total;
Try following query on your account table:
Account::select(
DB::raw('SUM(IF(kind = "expense", amount * -1, amount)) as sum'),
DB::raw('YEAR(date) as year'),
DB::raw('MONTH(date) as month'),
)
->where('type', $type)
->where(DB::raw('YEAR(date)'), $selectYear)
->groupBy(DB::raw('MONTH(date)'));
You will get data like this:
[
[ sum: 400, year: 2020, month: 7, ]
[ sum: 500, year: 2020, month: 8, ]
...
]

Add extra column as a marker in laravel datatable

What I want to do is to select the record from database based on two conditions, the first condition is to select all of the records that has reached 30 days today, and the second one is the records that has reached 25 days today based on the date field of the records. And for each condition I want to add a column (status) for marking the result. I want the result to be like this:
+--+--------------------+-------------------+-------------------+
|No| Date | Name | Status |
+--+--------------------+-------------------+-------------------+
|1 | 2018-10-17 | John | 30 days |
|2 | 2018-10-22 | Daniel | 25 days |
+--+--------------------+-------------------+-------------------+
This is my query:
$data = DB::table('data_pemohon')->select('*')->whereRaw('tanggal_permohonan + INTERVAL 30 DAY <= NOW()')->orWhereRaw('tanggal_permohonan + INTERVAL 25 DAY <= NOW()')->where('status_paspor','Serahkan paspor')->get();
return Datatables::of($data)->addIndexColumn()->make(true);
How can I do that, help me guys, thanks.

Laravel 5.4 pluck, where

I'm working on an assignment system for radio newscasts. Trying to return a view of all assignments for a particular newscast, and I'm stymied.
Tables
users table
id | name
---|-------
1 | Admin
2 | Susan
3 | Ed
4 | Jen
newscasts table
id | forStation_id | name
---|---------------|-----
1 | 1 | AM
2 | 1 | PM
3 | 2 | Sports
stations table
id | calls
---| -----
1 | JNDV
2 | YXWQ
assignments table
id | anchorId | newscastId | startDate | endDate | isTemp
---|----------|------------|-------------|-------------|--------
1 | 2 | 1 | 01 May 2017 | 31 Dec 2999 |
2 | 3 | 1 | 02 May 2017 | 06 May 2017 | True
3 | 4 | 2 | 01 Apr 2017 | 31 Dec 2999 |
4 | 3 | 3 | 01 Apr 2017 | 28 Apr 2017 |
(part of) Assignment model
public function anchor()
{
return $this->belongsTo(User::class, 'anchor_id')->withTrashed();
}
public function cast()
{
return $this->belongsTo(Newscast::class, 'cast_id')->withTrashed();
}
(part of) Newscast model
public function for_station()
{
return $this->belongsTo(Station::class, 'for_station_id')->withTrashed();
}
function getNameInputStationAttribute() {
return $this->for_station->calls . "-" . $this->name_input;
}
(part of) Assignment controller
/**
* Display all Assignments for one input name.
*
* #param int $name
* #return \Illuminate\Http\Response
*/
public function showAssignmentsByCastName($castName)
{
if (! Gate::allows('assignment_view')) {
return abort(403);
}
$relations = [
'anchors' => \App\User::get()->pluck('name', 'id'),
'casts' => \App\Newscast::with('for_station')->get()->pluck('name_input_station', 'id'),
'assignments' => \App\Assignment::with('cast')->get(),
];
dump($relations);
return view('assign.list', compact('castName') +$relations);
}
As I'd expect, this code returns the full collection of assignments.
Output
Anchor | Cast Name | Start Date | End Date
---------|-------------|-------------|-------------
Susan | JNDV-AM | 01 May 2017 | 31 Dec 2999
Ed | JNDV-AM | 02 May 2017 | 06 May 2017
Jen | JNDV-PM | 01 Apr 2017 | 31 Dec 2999
Ed | YXWQ-Sports | 01 Apr 2017 | 28 Apr 2017
I've tried several ways to limit the assignments to only one newscastId, thus far without success.
Desired Output for /assignment/list/JNDV-AM on 01 May 2017
Anchor | Cast Name | Start Date | End Date
---------|-------------|-------------|-------------
Susan | JNDV-AM | 01 May 2017 | 31 Dec 2999
Ed | JNDV-AM | 02 May 2017 | 06 May 2017
The shorter-term assignment is a temporary one (isTemp=True). During the days it is valid, it should be listed on top.
Desired Output for /assignment/list/JNDV-AM on 02 May 2017 through 06 May 2017
Anchor | Cast Name | Start Date | End Date
---------|-------------|-------------|-------------
Ed | JNDV-AM | 02 May 2017 | 06 May 2017
Susan | JNDV-AM | 01 May 2017 | 31 Dec 2999
I'm modifying code produced by an admin panel generator tool. It seems to me that querying for all users and all casts is not the most efficient way to go about it. I'd think, since I'm looking for only the current & future assignments for one newscast, that the anchors and casts relations should be filtered.
Basic question
What changes should I make to
'assignments' => \App\Assignment::with('cast')->get(),
to get only the assignments for JNDV-AM (newscastId = 1)?
Advanced Question
How do you recommend changing relations to return only the current and future assignments for JNDV-AM, today's assignment first, with the fewest queries possible?
You can pass a function into your with statement:
'assignments' => \App\Assignment::with(['cast' => function ($query) {
$query->where('newscastId', '=', 1);
}])->get()
Here is the working code I came up with:
(part of) Assignments controller
public function showAssignmentsByCastName($calls, $name_input)
{
if (!Gate::allows('assignment_view')) {
return abort(403);
}
$castName = strtoupper($calls). "-" . $name_input;
$station_id = \App\Station::where('calls', $calls)->get();
$station = \App\Station::findOrFail($station_id);
$cast_id = \App\Newscast::where([
['for_station_id', $station->id],
['name_input', $name_input]
])->get();
$cast = \App\Newscast::findOrFail($cast_id);
$relations = [
'anchors' => \App\User::get()->pluck('name', 'id'),
'casts' => \App\Newscast::where('cast_id', $cast->id),
'assignments' => \App\Assignment::where('cast_id', $cast->id)->get(),
];
return view('assign.list', compact('castName') + $relations);
}
web route
Route::get('assign/{calls}-{name_input}', 'AssignmentsController#showAssignmentsByCastName')->name('assign.list');

datamapper, how do you filter a date field by month and year?

using datamapper, how do you filter a date field by month and year?
I can filter all records with today's date.
:date => Date.today
but i am curious if there is an elegant solution
for returning all records from december 2012.
posts.all
+----+------------+
| id | date |
+----+------------+
| 1 | 2009-10-20 |
| 2 | 2012-11-18 |
| 3 | 2012-12-10 |
| 4 | 2012-12-14 |
+----+------------+
posts.all(:date => Date.today)
+----+------------+
| id | date |
+----+------------+
| 4 | 2012-12-14 |
+----+------------+
I think your best bet is to use the date range form (which will result in an SQL "BETWEEN" query):
posts.all(:date => (Date.parse('2012-12-01') .. Date.parse('2012-12-31')))
For other years/months you'll have to lookup or compute the last date of the month instead of hardcoding them as in the example above.
def month_range(year, month)
d0 = Date.parse([year, month, 1].join('-'))
(d0 .. (d0>>1)-1)
end
posts.all(:date => month_range(2012, 12))

How to get the file modified date of a file with respect to a particular time zone in Jscript?

I have a file which gets created/modified on a PST machine. But when I am accessing this file from an Indian standard time, the Modified date will be returned according to the current system time zone. (As explained here) Is there any way I can get this date with respect to the time zone provided
var WshShell = Sys.OleObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
var objFile = fso.GetFile("c:\\abc.txt");
var date = objFile.DateLastModified + "";
Yes, you do this with the DateDiff function in VBS:
http://www.devguru.com/technologies/vbscript/quickref/datediff.html
DevGuru Info
DateDiff(Interval, Date1, Date2, FirstDayofWeek, FirstWeekofYear)
The DateDiff function calculates the amount of time between two different dates.
There are three mandatory arguments.
Interval
The Interval argument defines the the type of time interval you wish to use to
calculate the time difference.
Only the following settings can be used. You must place the setting inside a
pair of double quotes.
| SETTING | DESCRIPTION |
|:--------|:-------------|
| YYYY | Year |
| Q | Quarter |
| M | Month |
| Y | Day Of Year |
| D | Day |
| W | WeekDay |
| WW | Week Of Year |
| H | Hour |
| N | Minute |
| S | Second |
In javascript you can do it like this:
How to calculate date difference in javascript

Resources