如何根据日期获得价格并将价格总和作为总价?

时间:2017-10-09 07:32:05

标签: php mysql laravel

这是我的代码

 $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');
}

我有两个模特:衣服和细节衣服

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

示例:: 当我输入衬衫价格时,它会去细节衣服并将其存放在那里,它也作为衣服存放在衣服中_total_price

它将根据月份显示所有记录,但是当我总结它时,它不会根据我的需要显示,

我想要的例子: 首先,如果我本月输入价格1000两次,总价格应该是2000,如果我输入下个月的价格1000两次,总价格应该是2000而不是4000

第二,如果我输入日期示例:2017-08-25,它将存储到两个模型,但特别是对于CLOTHES模型,它将始终根据月份和年份更新日期到最新提交,

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
注意*在衣服模型中,它只会根据月份和年份显示1行记录,并且在同一个月和一年中永远不会显示2行记录

任何人都可以帮助我??????

2 个答案:

答案 0 :(得分:0)

我认为您忘记将$ clothes_total_price重置为零。我也从内部foreach到外部移动了一条线。

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');
}

编辑:SQLFiddle

中的补充答案

您按一个月分组,从该月开始计算最长日期,然后将该月的价格合计:

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
;

答案 1 :(得分:0)

我已经弄明白了如何回答这个问题

详情衣服模型

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
        ]);
}