How to get the latest request in model Laravel? - php

My Code:
$filter = Products::select(DB::raw('SUM(price) as `total_price`')
DB::raw('SUM(count) as `total_count`')
DB::raw("DATE_FORMAT(date, '%m/%Y') new_date"),
DB::raw('YEAR(date) year, MONTH(date) month'))
->groupBy('year', 'month')
->get();
$result = [];
$total_price = 0;
$total_count = 0;
$new_date = null;
foreach($filter as $f){
$new_date = $f->new_date;
$total_price = $f->total_price;
$total_count = $f->total_count;
}
*note : please use dd($f) ,
it will display total price, total count, and new date but it always the latest ones that shown
example:
if i input price and count at 02-08-2018 first
i input price and count at 02-08-2020 second,
i input price and count at 02-08-2016 third,
it will show the latest date which is all the data in 02-08-2020
what i want is, it will show according to the latest submit or input, which we can get by the latest request in form,
if we look at the example, the expected result should be all the data at 02-08-2016 .
Can anyone help me please????

Related

Group by query not working fine According me

I want to get a values group_by 'recivedID' but not working fine. find value only one first value is show on form how show all add all values and show.
table 'fuel_transaction'
recivedID hsdltrs
1 500
1 600
1 300
2 100
2 600
result show on form // no need
recivedID ->1
hsdltrs ->500 //worng result
I need result
//hsdltrs ->500+600+300=1400
recivedID ->1
hsdltrs ->1400
<?php
//$this->db->Select SUM(ABS(hsdltrs)) as Data ; //error please check its sir
$this->db->where('issuedate >=',$date1);
$this->db->where('issuedate <=',$date2);
$this->db->where('recivedID',$a);
$this->db->group_by('recivedID');
$queryb = $this->db->get('fuel_transaction');
if ($queryb->num_rows() > 0)
{
foreach ($queryb->result() as $rowc)
{
$tdate = $rowc->issuedate;
$hsdltrs = $rowc->hsdltrs;
$hsd += $rowc->hsdltrs;
?>
You need to add select_sum.
Then, inside your foreach it will be available using $rowc->sumhsd:
$this->db->select_sum('hsdltrs', 'sumhsd'); //Here it comes
$this->db->where('issuedate >=',$date1);
$this->db->where('issuedate <=',$date2);
$this->db->where('recivedID',$a);
$this->db->group_by('recivedID');
$queryb = $this->db->get('fuel_transaction');
if ($queryb->num_rows() > 0) {
foreach ($queryb->result() as $rowc) {
$tdate = $rowc->issuedate;
$hsdltrs = $rowc->sumhsd; //Here you get the sum

order number auto create based on previous number

What I want to do is create an automatic order number. So like this date-number of order
So the first order of today would be
20190719-01
second-order of today would be
20190719-02
etc.
Then tomorrow it needs to be like 20190720-01 20190720-02 etc and so on.
How can I achieve this? preferably in PHP
str_pad() is your friend here.
$range = range(1,10);
$period = new DatePeriod(
new DateTime('2019-07-17'),
new DateInterval('P1D'),
new DateTime('2019-07-20')
);
foreach ($period as $key => $value) {
foreach($range as $inc){
$seq = str_pad($inc,2,"0",STR_PAD_LEFT);
$order_number = $value->format('Ymd').'-'.$seq;
echo $order_number.PHP_EOL;
}
echo PHP_EOL."===========".PHP_EOL;
}
WORKING DEMO: https://3v4l.org/R5POt
Normally order numbers would be sequential, and can therefore easily be derived from the AUTO INCREMENT field. In your case this is not so easy.
You could start by getting the highest AUTO INCREMENT field from the database for all orders before today:
SELECT MAX(id) FROM orders WHERE DATE(orderDate) < CURDATE();
Let us call this id $offsetId. If no orders exist yet it should be zero.
Then first insert the order information into the database, so you can get the id of that order. You can do this either with $con->insert_id in MySQLi or lastInsertId() in PDO. let's call this the $orderId.
The reason, to use a database in this way, is to prevent that two orders ever get the same order number.
Now you can compute the order number for today:
$orderNumber = date("Ymd") . "-" . sprintf('%02d', $orderId - $offsetId);
And you can store this in the database, with the order.
You should probably anticipate more than 99 orders a day...

Calculate new percentage when new record is added

I have this MySQL that I am using to calculate a percentage for a segment in a call. When a new record is added, I am able to get the correct percentage for each row.
$sumGreeting = $dbcon->prepare("SELECT SUM(greeting_msg + provide_agent_name + clear_and_aud + confirm_caller_name + confirm_caller_tel + ask_alt_number) AS SumGrt FROM table GROUP BY coach , id");
$sumGreeting->execute();
I want to calculate the final percentage each time a new record is added. Taking in mind the previous or existing records. At the moment I'm getting this result below:
Current Result
I'm using this code
$initRows = 0;
$result = 0;
while ($row = $sumGreeting->fetch(PDO::FETCH_ASSOC))
{
$initRows = $row['SumGrt'];
$result = ($initRows/12)*100;
echo var_dump($result);
echo $result ." <br />";
}
The Expected results will be the final percentage of all the columns.
This section has a total of 12 points.
Thanks in advance

Sum up all "amount" fields in a Laravel collection

I am trying to get the total amount collected in a month, let's say January.
I am able to retrieve all the created_at fields which were created in the month of January in the $main_data_January variable. The $main_data_January variable contains this type of results.
As you can see, each item has a field called amount.
Problem
I want to retrieve the value of amount field from each item and sum them up. As I'm doing it in the total_earnings_janunary, but this is where I am facing the problem.
By adding both the amounts, the expected result should be 13000, but
the result is 8000. It is only getting only the first value of amount after being in a loop.
$customers = Tag::all();
$total_earnings_janunary = 0;
$query_date = '2017-01-04';
$start_date_janunary = date('Y-01-01', strtotime($query_date));
$end_date_janunary = date('Y-m-t', strtotime($query_date));
foreach ($customers as $customer) {
if (Auth::User()->gym_code == $customer->gym_code ) {
$main_data_janunarys = DB::table('tags')->whereBetween('created_at', [$start_date_janunary,$end_date_janunary])->get();
}
}
for ($i=0; $i <= count($main_data_janunarys); $i++) {
$total_earnings_janunary = $total_earnings_janunary + $main_data_janunarys[$i]->amount;
dd($total_earnings_janunary);
}
Few things to note:
You're querying the Tag model and storing it in $customers. Resulting entities are not customers. Make sure that's intended.
In the first foreach loop, you are overwriting previous value of the $main_data_janunarys over and over. Use break if that's intended.
In the for loop, you are dding early. Move it outside the loop and you'll see your intended results. In the loop exit condition you should be using < instead of <= as you're starting from zero.
To sum up the collection you could just use Collection::sum() method, like: $main_data_janunarys->sum('amount');.
Try this:
$customers = Tag::all();
$total_earnings_janunary = 0;
$query_date = '2017-01-04';
$start_date_janunary = date('Y-01-01', strtotime($query_date));
$end_date_janunary = date('Y-m-t', strtotime($query_date));
foreach ($customers as $customer)
{
if( Auth::User()->gym_code == $customer->gym_code ) {
$main_data_janunarys = DB::table('tags')->whereBetween('created_at', [$start_date_janunary,$end_date_janunary])->get();
}
}
$total_earnings_january = $main_data_januarys->sum('amount');
You can read further about Laravel's Collection here

SQL data within the last year

I have created a query which seems to execute with no errors. I am getting the total of two columns in two different fields related to one customer, I only want to retrieve the data which was within the past year.
I have used this but it seems to throw all the data stored.
$yearago = (date('Y')-1).date('-m-d');
$sumtotal = $adb->pquery("SELECT
SUM(currentamount),
SUM(newcurrentamount),
(SUM(currentamount) + SUM(newcurrentamount)) as 'Total'
FROM vtiger_addisa, vtiger_isa
WHERE vtiger_addisa.addrelatedclient = $relatedclient
AND vtiger_isa.relatedclient = $relatedclient
AND vtiger_isa.startdate >= $yearago
");
$sumtotal->fetchInto($row);
$clientotal = $row['Total'];
echo $clientotal;
//print_r($sumtotal);

Categories