<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CashBook extends Model
{
    use HasFactory;

    public static function invoice($request, $invoiceId)
    {
        $latestCashBook = CashBook::latest('id')->first();
        $currentBalance = $latestCashBook ? $latestCashBook->balance : 0;

        $cashBook = new CashBook();
        $cashBook->date         = date('Y-m-d');
        $cashBook->invoice_id   = $invoiceId;
        $cashBook->detail       = "N/A";
        $cashBook->dr           = $request->received;
        $cashBook->balance      = $currentBalance + $cashBook->dr;
        $cashBook->save();
    }
    public static function cost($request)
    {
        $latestCashBook = CashBook::latest('id')->first();
        $currentBalance = $latestCashBook ? $latestCashBook->balance : 0;

        $cashBook = new CashBook();
        $cashBook->date         = date('Y-m-d');
        $cashBook->detail       = $request->detail;
        $cashBook->cr           = $request->cost;
        $cashBook->balance      = $currentBalance - $cashBook->cr;
        $cashBook->save();
    }

}
