<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class Rentsum extends Model
{
    use HasFactory;

    protected $table = 'transactions'; // Ensure this matches your actual database table name
    protected $primaryKey = 'id'; // Adjust this if your primary key is different
    public $timestamps = false; // Set to true if your table has created_at and updated_at

    protected $fillable = [
        'entrydate',
        'total',
        'accsubcode',
        'ttype',
    ];

    // Define the relationship with the Tenant model
    public function tenant()
    {
        return $this->belongsTo(modeltTenants::class, 'accsubcode', 'tenantcode');
    }

    /**
     * Fetch Summary Data
     */
    public static function fetchSummaryData($fromDate, $toDate)
    {
        return self::select(
                'transactions.entrydate',
                'transactions.total',
                'transactions.accsubcode',
                'transactions.ttype',
                'transactions.batch',
                'transactions.contracode',
                'transactions.refference',
                'transactions.tax',
                'transactions.discount',
                'transactions.price',
                'tenants.tenantcode',
                'tenants.name as tenant_name',
                'tenants.contactperson',
                'tenants.floor',
                'tenants.space',
                'tenants.rent_amount',
                'tenants.rooms'
            )
            ->join('tenants', 'transactions.accsubcode', '=', 'tenants.tenantcode')
            ->where('transactions.ttype', 'invoice')
            ->where('transactions.contracode', '1000')
            ->whereDate('transactions.entrydate', '>=', $fromDate)
            ->whereDate('transactions.entrydate', '<=', $toDate)
            ->orderBy('transactions.entrydate', 'asc')  // Order by entrydate in descending order
            ->get();
    }


    /**
     * Fetch Detailed Data (Placeholder for Future)
     */
    public static function fetchDetailedData($fromDate, $toDate)
    {
        $data = self::select(
            'transactions.entrydate',
                'transactions.total',
                'transactions.accsubcode',
                'transactions.ttype',
                'transactions.batch',
                'transactions.contracode',
                'transactions.refference',
                'transactions.tax',
                'transactions.discount',
                'transactions.price',
                'tenants.tenantcode',
                'tenants.name as tenant_name',
                'tenants.contactperson',
                'tenants.floor',
                'tenants.space',
                'tenants.rent_amount',
                'tenants.rooms'
        )
        ->join('tenants', 'transactions.accsubcode', '=', 'tenants.tenantcode')
        ->where('transactions.ttype', 'invoice')
        ->where('transactions.contracode', '1000')
        ->whereDate('transactions.entrydate', '>=', $fromDate)
        ->whereDate('transactions.entrydate', '<=', $toDate)
        ->orderBy('transactions.entrydate', 'asc')
        ->get();

        \Log::info('Detailed data:', $data->toArray());  // Log the fetched data

        return $data->groupBy('tenant_name');  // Group by tenant_name
    }
    public static function fetchSummaryOutstandingData($fromDate, $toDate)

    {
        return self::select(
                'transactions.entrydate',
                'transactions.total',
                'transactions.accsubcode',
                'transactions.ttype',
                'transactions.batch',
                'transactions.tax',
                'transactions.contracode',
                'transactions.refference',
                'transactions.discount',
                'transactions.price',
                'transactions.dr',
                'tenants.tenantcode',
                'tenants.name as tenant_name',
                'tenants.contactperson',
                'tenants.floor',
                'tenants.space',
                'tenants.rent_amount',
                'tenants.rooms'

            )
            ->join('tenants', 'transactions.accsubcode', '=', 'tenants.tenantcode')
            ->where('transactions.ttype', 'invoice')
            ->where('transactions.contracode', '1000')
            ->whereDate('transactions.entrydate', '>=', $fromDate)
            ->whereDate('transactions.entrydate', '<=', $toDate)
            ->orderBy('transactions.entrydate', 'asc')  // Order by entrydate in descending order
            ->get();
    }


    /**
     * Fetch Detailed Data (Placeholder for Future)
     */
    public static function fetchDetailedoutstandingData($fromDate, $toDate)
    {
        $data = self::select(
            'transactions.entrydate',
                'transactions.total',
                'transactions.accsubcode',
                'transactions.ttype',
                'transactions.batch',
                'transactions.contracode',
                'transactions.tax',
                'transactions.discount',
                'transactions.refference',
                'transactions.price',
                'transactions.dr',
                'tenants.tenantcode',
                'tenants.name as tenant_name',
                'tenants.contactperson',
                'tenants.floor',
                'tenants.space',
                'tenants.rent_amount',
                'tenants.rooms'

        )
        ->join('tenants', 'transactions.accsubcode', '=', 'tenants.tenantcode')
        ->where('transactions.ttype', 'invoice')
        ->where('transactions.contracode', '1000')
        ->whereDate('transactions.entrydate', '>=', $fromDate)
        ->whereDate('transactions.entrydate', '<=', $toDate)
        ->orderBy('transactions.entrydate', 'asc')
        ->get();

        \Log::info('Detailed data:', $data->toArray());  // Log the fetched data

        return $data->groupBy('tenant_name');  // Group by tenant_name
    }
    public static function fetchSummaryCollectionData($fromDate, $toDate, $curr)
    {
        return self::select(
                'transactions.entrydate',
                'transactions.total',
                'transactions.accsubcode',
                'transactions.ttype',
                'transactions.batch',
                'transactions.contracode',
                'transactions.tax',
                'transactions.discount',
                'transactions.refference',
                'transactions.price',
                'transactions.dr',
                'transactions.currency',
                'tenants.tenantcode',
                'tenants.name as tenant_name',
                'tenants.contactperson',
                'tenants.floor',
                'tenants.space',
                'tenants.rent_amount',
                'tenants.rooms'
            )
            ->join('tenants', 'transactions.accsubcode', '=', 'tenants.tenantcode')
            ->where('transactions.ttype', 'receipt')
            ->where('transactions.contracode', '1000')
            ->where('transactions.currency', $curr)
            ->whereDate('transactions.entrydate', '>=', $fromDate)
            ->whereDate('transactions.entrydate', '<=', $toDate)
            ->orderBy('transactions.entrydate', 'asc')
            ->get();
    }

    public static function fetchAgeSummaryOutstandingData($fromDate, $toDate)

    {
        return self::select(
                'transactions.entrydate',
                'transactions.total',
                'transactions.accsubcode',
                'transactions.ttype',
                'transactions.batch',
                'transactions.tax',
                'transactions.contracode',
                'transactions.refference',
                'transactions.discount',
                'transactions.price',
                'transactions.dr',
                'tenants.tenantcode',
                'tenants.name as tenant_name',
                'tenants.contactperson',
                'tenants.floor',
                'tenants.space',
                'tenants.rent_amount',
                'tenants.rooms'

            )
            ->join('tenants', 'transactions.accsubcode', '=', 'tenants.tenantcode')
            ->where('transactions.ttype', 'invoice')
            ->whereDate('transactions.entrydate', '>=', $fromDate)
            ->whereDate('transactions.entrydate', '<=', $toDate)
            ->orderBy('transactions.entrydate', 'asc')  // Order by entrydate in descending order
            ->get();
    }

}
