Antony Lim
Antony Lim

Reputation: 31

How to get price and sum the price as a total price according to date?

THIS IS MY CODE

 $filter = DB::table('detail_clothes')
            ->get(['id', 'clothes_detail_date', 'clothes_price'])
            ->groupBy(function($date){
                return Carbon::parse($date->clothes_date)->format('m/Y');
            });

$result = [];
$clothes_total_price = 0;
$clothes_date = null;

foreach ($filter as $dn => $dn_value) {
    $clothes = Clothes::where('clothes_date', $dn)
               ->first();

    foreach ($dn_value as $k => $dnval) {
        $clothes_total_price += $dnval->clothes_price;
        $result[$dn]['clothes_total_price'] = $clothes_total_price;
    }

    $date_temp = date_format(date_create_from_format('Y-m-d', $request->clothes_detail_date),'m/Y');
}

I have Two Model : Clothes and Detail Clothes

Clothes : id, clothes_date, clothes_total_price
DetailClothes : id, clothes_detail_date, clothes_price

example:: when i input shirt price it will go to detail clothes and store it there , and it also store in clothes as clothes_total_price

it will display all records according to the month, but when i sum it , it doesnt show according what i want,

what i want for example: first, if i input the price this month 1000 twice, the total price should be 2000, and if i input the price for next month 1000 twice , the total price should be 2000 not 4000

second, if i input the date example: 2017-08-25 , it will store to both model, but spesifically for CLOTHES model it will always update the date according to month and year to the latest submit,

example:
at Detail Clothes model it should be like this::
1st submit : clothes_detail_date : 2017-08-25, clothes_price : 1000
2nd submit : clothes_detail_date : 2017-08-01, clothes_price : 2000, 

expected result:
at Clothes model it should be like this::       
clothes_date : 2017-08-25, clothes_total_price: 3000

note* at Clothes Model it will only show 1 row of record according to month and year , and it will never show 2 record at the same month and year

Can Anyone Help Me??????

Upvotes: 3

Views: 219

Answers (2)

Antony Lim
Antony Lim

Reputation: 31

I already figure it out how to answer this question

DetailClothes Model

public function scopeStoreDetailClothes($query, $request){
    $data = $request->all();
    DetailClothes::create($data)->save();

    $date = Carbon::parse($request->clothes_detail_date);

    $filter = DetailClothes::whereMonth('clothes_detail_date', '=', $date->month)
            ->whereYear('clothes_detail_date', '=', $date->year);

    $total = (object) [
            'clothes_price' => $filter->sum('clothes_price'),
    ]; 

    $detail_clothes = DetailClothes::whereMonth('clothes_detail_date', '=', $date->month)
            ->whereYear('clothes_detail_date', '=', $date->year)
            ->orderBy('clothes_detail_date', 'desc')
            ->first();

    $clothes = Clothes::whereMonth('clothes_date', '=', $date->month)
            ->whereYear('clothes_date', '=', $date->month)
            ->first();

    Clothes::updateOrCreate(
        [
            'clothes_date' => isset($clothes->clothes_date) ? $clothes->clothes_date : null
        ], [
            'clothes_date' => isset($detail_clothes) ? $detail_clothes->clothes_detail_date : $request->clothes_detail_date,
            'clothes_total_price' => $total->clothes_price
        ]);
}

Upvotes: 0

wast
wast

Reputation: 996

I think you forgot to reset $clothes_total_price to zero. Also I moved 1 line from inner foreach to outer.

foreach ($filter as $dn => $dn_value) {
   $clothes_total_price = 0;
   $clothes = Clothes::where('clothes_date', $dn)
           ->first();

   foreach ($dn_value as $k => $dnval) {
     $clothes_total_price += $dnval->clothes_price;
   }
   $result[$dn]['clothes_total_price'] = $clothes_total_price;
   $date_temp = date_format(date_create_from_format('Y-m-d', 
     $request->clothes_detail_date),'m/Y');
}

EDIT: Additional answer in SQLFiddle

You group by a month, take the max date from that month, and you sum the price in that month:

INSERT INTO 
  Clothes (clothes_date, clothes_total_price)
  SELECT * FROM (
    SELECT 
       MAX(clothes_detail_date) new_clothes_date
      ,SUM(clothes_price) new_clothes_total_price
    FROM DetailClothes
    GROUP BY DATE_FORMAT(clothes_detail_date,'%Y%m') 
  ) newTable
ON DUPLICATE KEY UPDATE    
clothes_date=newTable.new_clothes_date
,clothes_total_price=newTable.new_clothes_total_price
;

Upvotes: 0

Related Questions