<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class LogClaimJournal
{
    
    public $afterCommit = true;

    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        $policy = $event->claim->member->currentPolicy;
        $policy->limitJournals()->create([
            'previous_balance' => $policy->limit_balance,
            'total_credit' => $event->claim->total_claim * -1,
            'type' => 'credit',
            'balance' => bcsub($policy->limit_balance, $event->claim->total_claim),
            'description' => 'Log for Claim #'. $event->claim->code,
        ]);
    }
}
