File: D:/HostingSpaces/Lacom/lacom.nl/app/KommaApp/References/ReferenceService.php
<?php
namespace App\KommaApp\References;
use App\KommaApp\References\Models\Reference;
class ReferenceService
{
public function determineReferenceAndShareWithViews($model, $loadCustomerType = true)
{
$reference = null;
// First try to get the reference through the model
if(!$reference = $model->references->where('active', 1)->shuffle()->first()){
// No reference is bind to this model
// So we load the active references of this site
$references = \App::getSite()->references->where('active', 1)->where('code_name', '!=', 'anvil');
// If the site has only one reference use that one
if($references->count() == 1){
$reference = $references->first();
}
// Else determine if the preferred type has a reference
elseif($references->count() != 0){
// Get the reference from the preferred type
if($loadCustomerType) $type = Reference::REFERENCE_TYPE_CUSTOMER;
else $type = Reference::REFERENCE_TYPE_EMPLOYEE;
$reference = $references->where('type', $type)->shuffle()->first();
// if the reference is still isn't set then there are no references of that type so we get one from the other kind
if(!isset($reference)){
if($loadCustomerType) $type = Reference::REFERENCE_TYPE_EMPLOYEE;
else $type = Reference::REFERENCE_TYPE_CUSTOMER;
$reference = $references->where('type', $type)->shuffle()->first();
}
}
}
\View::share('reference', $reference);
}
}