whereBetween dates trouble for getting results in Eloquent Laravel - laravel

I have some trouble with getting the correct output of a Eloquent Query.
$checkIn = date(request()->checkin);
$checkOut = date(request()->checkout);
$bookedRooms = App\Booking::whereBetween('checkin', [$checkin, $checkout])->whereBetween('checkout', [$checkin, $checkout])->pluck('room_id');
$availabileRooms = App\Room::whereNotIn('id', $bookedRooms->toArray())->sum('number_of_rooms');
What I have is a checkin and checkout date.
It is a database for a Hotel.
The Hotel has Rooms. There a different types of Rooms, and each Room can exist multiple times. That is set in the Room-table with an integer value of the rooms available in column number_of_rooms
What I want, it the output of the percentage that is still available of a specific Room and the total availability of Rooms in the date range.
I thought that it can be achieved with the code given before, but that can be not.
Can someone help me out? Probably it's simpler then I can think of now..

i looked your code, but could you please tell me one thing. what is the date format of checkin and checkout you getting.
make sure that you getting date format is like 2019-07-04 (YYYY-mm-dd).
i hope this will help you.

Related

Creating time categories and adding them to dataset

I've been trying tirelessly to figure out this issue and I cannot seem to make it work...
I am working on a project comparing activity times of some mammalian species and need to run a multinomial model in the nnet package. To do this, I need to convert the timestamp data I have into categories of time. So far, I have tried to use the which() function, strptime, strftime, and some others that I've had little luck with. I have the data both in date and time format, just time format, and just hour format in my csv and cannot figure out how to categorize time with any of these. I'd like to add these categories to my dataset as well so I can see which species were active during which times.
The times I'd like to classify are as follows:
Nocturnal: 10pm-4am
Crepuscular: 4am-8am; 6pm-10pm
Diurnal: 8am-6pm
Any help would be so greatly appreciated!!!!!

finding a maximum result after comparing two different dates in business objects webi

I would like to seek someone's help with Business objects Webi reports and how to create a variable that looks at two fields, compares them and returns a specific value.
For example.
I would like the report to look at DateA then look at DateB.(DateB can have many dates) Then return the latest date that comes right before DateA
ID..............DateA.............DateB..............IntendedResults
aaa............1/3/19.............1/6/19..............1/2/19
aaa............1/6/19.............1/4/19..............1/4/19
aaa............1/7/19.............1/2/19..............1/4/19
bbb............1/1/19............1/6/19..............(blank)
bbb............1/9/19.............1/9/19..............(blank)
I created a max for each variable
=Max([DateB]) ForEach([Id]) Where( [DateB]<[DateA])
But I keep returing the wrong results showing 0's and 1's. Please help.
Thank you

Not getting the correct totals using Cognos Report Studio. Need to get totals that show up in column

newparts_calc
if (([MonthToDateQuery].[G/L Account] = 4200 and [Query1].[G_L_Group] = 'NEW')) THEN ([Credit Amount]-[Debit Amount]) ELSE (0)
Data Item1
total([newparts_calc])
I need Data Item1 to return newparts_calc values only.
So for example in 1st row Data Item1 should be 8,540.8, but is 34,163.2
Whats wrong? how do i fix?
REVISED QUESTION
I apologize for not making sense on the original question.
I have many of the calc's that im trying to gather and put on a crosstab. I want to see sales by month (row) and part category (column)
[Query2] is the one shown in picture above.
It joins [MonthToDateQuery] AND [Query1]
The join is on 'Invoice' and carnality is 1..1 = 1..1
[MonthToDateQuery] is based on the package im working in. General ledger. It supplies the g/l entries for each sales g/l account
[Query1] is a SQL query i brought in to be able to break out categories even further from g/l group.
For example g/l account 4300 is rebuilt. However i needed to break out even further to see Rebuilt-Production and Rebuilt-New. I can do that with the g/l group.
I saw in my g/l account ledger entries that it referenced the invoice number. So thats how i tied in my SQL.
So as you can see from the table below (which is the view tabular data from query) i need a total. I have tried plugging newparts_calc into my crosstab and setting aggregation to total but the numbers still dont seem right. I dont think i have something set as it should be.
All the calc's im doing are based on single or multiple G/L Accounts and single or multiple G/L Groups.
Any Advice?
As you can see the problem seems to be duplicate invoice numbers.
How can i fix?
Couple things come to mind:
-Set the processing order to 2
-Since your calc is always a multiple and you are joining two queries, you may need to check your cardinality. Sometimes it helps to add derived queries to ensure you are working with the correct grain.
I'm obviously missing something, but if you want
I need Data Item1 to return newparts_calc values only.
just use newparts_calc, without total? That would give you proper value for row 1 -)
If you need a running-total for days (sum of values for previous days) — you should use a running_total function.
At a guess, one of your two queries is returning multiple rows for each invoice, which will cause this double counting. Look at the output of the two queries and see if that's happening. If so, then you just need to work out how to collapse that down to one row per invoice.
Per your new question - The underlying data has got to be causing the issue. Its clearly not 1:1 (note that even though this is what your stated cardinality is, Cognos does not enforce 1:1). Invoice number is not unique, GL Group is at a lower level.

how to chop a DataMapper Collection into one Collection per day?

I have a DataMapper::Collection Object. Each of it's entries has a created_at property. I want to render the entries into html tables, one table per day (I use Sinatra for that).
It was no problem to render everything into one table, but I didn't get it to do so for every day. I thought of an array of DataMapper::Collection objects over which I would just iterate and do the job. But I don't know how to build such an array :/
Does anyone know how to solve my problem, or does anyone have a different/better approach?
Thanks in advance!
You have (at least) two options. The first is to let the database do the work for you. I don't know about datamapper but most database mappers (!) have functionality to group using SQL's GROUP BY. In this case you would have to use a database function to get the date out of the timestamp and then group on that. This is the fastest option and if you and future maintainers are familiar with relational databases probably also the best.
The second option is to to the mapping in your code. I can't come up with an elegant Ruby thing right now but you could at least do:
mapped_result = Hash.new [] # initiates each new entry with empty array
mapper_collection.each do |one_record|
mapped_result[one_record.created_at.strftime '%Y-%m-%d'] << one_record
end
and then you can get to record for a day with
mapped_result['2012-11-19']

PHP Function that Randomly Selects One mySQL Row With A Unix Date of Less than Two Weeks

I need a php function that can randomly select one row that has a unix time date field (like 2011-11-12 or 2011-12-24) that indicates the row was created within the last two weeks from a mySQL table and return the id of that said row.
I don't know how do this. Honestly, I would like someone to hand me the code, but I don't like that because most people don't like that.. and their reasons make sense, but if someone would show me the code, then please do, thus can someone at the least point me in the right direction? Thank you.
You can get the date by using date and strtotime:
$date = date('Y-m-d', strtotime("-2 weeks"));
Then the query would look something like this:
SELECT * FROM table WHERE date_field >= '$date' ORDER BY RAND() LIMIT 1;
ORDER BY RAND() gives you a random row.
strtotime: http://us3.php.net/manual/en/function.strtotime.php
Hope this helps. :)

Resources