<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/3/22
 * Time: 下午6:00
 */

namespace App\Modules\Channel\Services;

use DB;

class ChannelOrdersService
{
    public static function getCurrentMonthNewChannelsOrdersSum($channel_id)
    {
        return DB::table('orders')
            ->join('distribution_channels', 'distribution_channels.id', '=', 'orders.distribution_channel_id')
            ->where([
                ['distribution_channels.id', '=', $channel_id],
                ['orders.status', '=', 'PAID'],
                ['distribution_channels.created_at', '>=', date('Y-m-01 00:00:00')]
            ])
            ->sum('price');
    }

    public static function getChannelsOrdersSum($channel_id, $start_date='')
    {
        if ($start_date) {
            $param = [
                ['distribution_channel_id', '=', $channel_id],
                ['status', '=', 'PAID'],
                ['created_at', '>=', $start_date],
            ];
        } else {
            $param = [
                ['distribution_channel_id', '=', $channel_id],
                ['status', '=', 'PAID'],
            ];
        }
        return DB::table('orders')->where($param)->sum('price');
    }
}