<?php

namespace App\Traits;

use App\Models\Ticket;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;

trait hasCommission
{
  use Currencies;

  protected static function BootHasCommission()
  {
    if (!app()->runningInConsole()) {
      $currentUser = Auth::user();
      
      if ($currentUser->is_admin==1) 
      {
         return;
      }
      


      static::creating(function ($model) use ($currentUser) {
        $model->commission =   $currentUser->office->booking_commission;
      });

      static::saving(function ($model) use ($currentUser) {
        $fees = isEligibleFees($model);
        if($fees){
            $model->tax = $fees->calculator($model->total_net);
        }
        else{
          if( $model->ticket_status == Ticket::STATE_‫‪REFUNDED‬‬){
            $model->tax = $model->convertCurrency(setting('booking.tax_refund')*100  , $model->currency_code);
           }
           if($model->ticket_status== Ticket::STATE_‫‪EXCHANGED‬‬){
            $model->tax = $model->convertCurrency(setting('booking.tax_reissue')*100  , $model->currency_code);
           } 
        }
       if( $model->ticket_status == Ticket::STATE_‫‪REFUNDED‬‬){
         $model->tax_office = $model->convertCurrency($currentUser->office->canellation_commission *100 , $model->currency_code);
       }
       if($model->ticket_status== Ticket::STATE_‫‪EXCHANGED‬‬){
        $model->tax_office = $model->convertCurrency($currentUser->office->exchange_commission *100 , $model->currency_code);
       }
      });
    }
  }


}
