<?php

namespace App\Traits;

use App\Models\Project\Order;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;



trait MonthlySaleRecord
{
    public function graphData($vendor_id = null)
    {
        $record = [['Opening Move', 'Earnings']];
        DB::enableQueryLog();
        for ($i=0; $i <= 11 ; $i++) {
        $dt = Carbon::Create(date(date('Y').'-'.$i.'-d'));
            $data = [
                'month' => $dt->format('F'),
            ];
            $to = $dt->format('Y-m-d');
            $from = $dt->subMonth()->format('Y-m-d');
            $data['total'] = Order::select(DB::raw('IF(SUM(paid) > 0 ,SUM(paid),0) as total'))

                ->whereBetween('created_at',[$from,$to])
                ->first()
                ->total;

            array_push($record,[$data['month'],$data['total']]);
        }
        return $record;
        // ddd($record);
    }
}
