<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

    function about(){
        return App\Models\About::find(1);
    }
    function setting(){
        return App\Models\Setting::find(1);
    }
    function payable(){
        $todayspay = DB::select("SELECT c.id AS customer_id, c.name AS customer_name, c.phone, c.address, SUM(s.grand_total_price) AS total_amount, SUM(s.payment) AS total_payment, SUM(s.due_amount) AS total_due,pd.created_at FROM possible_dates pd INNER JOIN sales s ON pd.customer_id = s.customer_id INNER JOIN customers c ON pd.customer_id = c.id WHERE pd.possible_date = CURRENT_DATE AND s.due_amount > 0 GROUP BY c.name, c.phone, c.address;");
        $data = [];
        foreach ($todayspay as $payd) {
            $createdAt = Carbon::parse($payd->created_at);
            //echo $createdAt->diffForHumans();
            $data[] = [
                    'name' => $payd->customer_name,
                    'phone' => $payd->phone,
                    'address' => $payd->address,
                    'total_amount' => $payd->total_amount,
                    'payment' => $payd->total_payment,
                    'due' => $payd->total_due,
                    'url'=> url('csrs').'?customer_id='.$payd->customer_id.'&report_type=Invoice',
                    'id'=> $payd->customer_id,
                    'date_time'=> $payd->created_at,
                    'date_diff'=> $createdAt->diffForHumans(),
                ];
        }
        return $data;
    }
