i will have a think
multiply product qty with rate with maximum rate in the list in relationship
guys i have a issue in multiplying the product quantity picked from sales table and multiply it with rate from purchase table.
i have purchases tables with column
id
product_id
date
qty
rate_per_kg
and this is my sales table with columns
sold_date
product_id
qty
but i dont have any relationship between sales and purchases table.
but both purchases and sales table have relationship with products table.
this is my model for Purchase
public function product()
{
return $this->belongsTo('App\Product', 'product_id');
}
this is my Sales model
public function product()
{
return $this->belongsTo('App\Product', 'product_id');
}
this is my Product model
public function purchase()
{
return $this->hasMany('App\Purchase', 'product_id');
}
public function sale()
{
return $this->hasMany('App\Sales', 'product_id');
}
my product table entry
id product_name
01 Onion
my purchase entry
id product_id date qty rate_per_kg
01 01 31-05-2019 50 32
whats my doubt is, when i enter a sales entry, like this sales entry
sold_date product_id qty
1-06-2019 01 30
1-06-2019 01 10
2-06-2019 01 5
i should get a report like when i search from date:1-06-2019 to date:2-06-2019
when i give the from and to date it must fetch records from sales table by grouping the product name total the quantity and then fetch the max(rate_per_kg) that is fetch the maximum rate_per_kg of that specific quantity for the given from and to date from purchase table and multiply it with the quantity in sales table and display the records as report as give below format.
report
productName sold rate_per_kg totalAmt
Onion 45 32 1440
How to perform this??
Kindly some one help please
Assuming that more recent prices have a higher ID
The following eloquent query gets the most recent price
You could use this result set as a collection from which you can lookup the price when you want to multiply with quantity.
$latestPrices = ProductRate::whereIn('id',
DB::table('product_rate')
->selectRaw('max(id) as id')
->groupBy('product_id')
->pluck('id')
)->get();
Please or to participate in this conversation.