HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/EventSignUps/EventSignUpService.php
<?php


namespace App\KommaApp\EventSignUps;

use App\KommaApp\Base\Service;
use App\KommaApp\Companies\CompanyService;
use App\KommaApp\Events\Models\Event;
use App\KommaApp\EventSignUps\Models\EventSignUp;
use App\Mail\EventSignUpMail;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;

class EventSignUpService extends Service
{
    public function __construct()
    {
        parent::__construct();
    }

    const SigneeFields = ['company', 'first_name', 'last_name', 'email', 'address', 'phone', 'postal', 'city', 'country','site', 'particularities', 'paymentMethod'];

    /**
     * Create signee object from form values
     *
     * @param array $formValues
     * @param string $preFix
     * @return object
     */
    public function createSignee(array &$formValues, string $preFix = '')
    {
        // if signee chooses to use an alternative company name
        //if (isset($formValues['other_invoice_company_name']) && $formValues['other_invoice_company_name'] === 'on' && $formValues['other_invoice_company'] !== '') {
        //    $formValues['company'] = $formValues['other_invoice_company'];
        //}

        $signee = [];
        foreach (self::SigneeFields as $signeeField) $signee[$signeeField] = array_pull($formValues, $preFix . $signeeField);
        return (object)$signee;
    }

    /**
     * Save signee as event sign ups through the event model
     *
     * @param array $signee
     * @param Event $event
     * @param integer $invoiceId
     */
    public function addSigneeToEventSignUps($signee, $mainSignee, Event $event, int $invoiceId, $invoiceIdPrefixed)
    {
        // Create Sign Up
        $signUp = new EventSignUp();
        $signUp->fill((array)$signee);
        $signUp->invoice_id = $invoiceId;

        // Save the Sign Up through the event relation
        $event->signUps()->save($signUp);

        $signedUpBy = null;
        if($signee !== $mainSignee){
            $signedUpBy = $mainSignee->first_name.' '.$mainSignee->last_name;
        }

        // Send mail to erik of the signee
        \Mail::send(new EventSignUpMail( $signUp, $signedUpBy, $event, $invoiceIdPrefixed));

    }

    public function bindCompaniesToSignees(Collection $signees)
    {

        // Get the email addresses of the signees
        $signeeEmails = $signees->where('email', '!=', '');

        // Splice the email addresses into the company part with the id so we can grab them later
        $companyEmailParts = [];
        foreach ($signeeEmails as $signeeEmail)
        {
            // Get everything after the @ sign
            $companyEmailPart = Str::after($signeeEmail->email, '@');
            $companyEmailParts[$signeeEmail->id] = $companyEmailPart;
        }

        // Remove all the duplicate emails and reset the keys
        $uniqueCompanyEmailParts = array_values(array_unique($companyEmailParts));

        /** @var CompanyService $companyService */
        $companyService = app(CompanyService::class);

        // Get the companies by the found parts
        $companies = $companyService->getCompaniesByEmailPart($uniqueCompanyEmailParts);
        $companies->keyBy('email');

        /*
         *
         * Bound companies to the signees
         * Note: we must use the collection each function because else we can't write on the items
         * Yet we now can use the where function of the collection to already filter the not filled email ones
         *
         */
        $signees->where('email', '!=', '')
            ->each(function ($signee) use ($companies, $companyEmailParts) {

            $companyEmailPart = $companyEmailParts[$signee->id];

            $foundCompanies = $companies->filter(function ($company) use ($companyEmailPart) {
                if(Str::endsWith($company->email, $companyEmailPart) || Str::endsWith($company->site_url, $companyEmailPart))
                    return $company;
            });

            // Only append the foundCompany if the collection is not empty and there is only one found (because with multiple we aren't certain which we should pick)
            if($foundCompanies->isNotEmpty() || $foundCompanies->count() == 1 ) {
                $signee->setRelation('foundCompany', $foundCompanies->first());
            };

        });


        /*
         * Note: we can't use the default foreach because we don't have writing access in there
         *
         */
//        foreach ($signees as $signee)
//        {
//            if(empty($signee->email)) continue;
//
//            $companyEmailPart = $companyEmailParts[$signee->id];
//
//            $foundCompanies = $companies->filter(function ($company) use ($companyEmailPart) {
//                if(Str::endsWith($company->email, $companyEmailPart) || Str::endsWith($company->site_url, $companyEmailPart))
//                    return $company;
//            });
//
//            // If collection is empty or the count is bigger then 1 (because then we aren't certain which we should pick) continue
//            if($foundCompanies->isEmpty() || $foundCompanies->count() != 1 ) continue;
//
////            $signee->company = $foundCompanies->first();
//            $signee->setRelation('foundCompany', $foundCompanies->first());
//            $signee->company = 'eler';
//            $boundedSignees->push($signee);
//        }
    }

}