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/Products/ProductService.php
<?php


namespace App\KommaApp\Products;

use App\KommaApp\Base\Service;
use App\KommaApp\Products\Models\Product;
use App\KommaApp\ProductSignUps\ProductSignUpService;
use App\KommaApp\Orders\OrderService;
use App\KommaApp\Regions\Models\Region;
use App\KommaApp\WeFact\WeFactAPI;
use App\KommaApp\WeFact\WeFactService;
use App\Mail\ProductSignUpMail;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use Spatie\CalendarLinks\Link;

class ProductService extends Service
{

    /**
     * Splice the form request into workable array/objects
     *
     * @param  array  $formValues
     * @return array
     */
    public function convertRequest(array $formValues)
    {
        $product = Product::find(array_pull($formValues, 'product_id'))
            ->load('translation');

        $orderProduct = (object)[
            'productable_type' => Product::class,
            'productable_id' => $product->id,
            'name'  => $product->translation->name,

            // Calculated the price without vat by the Product type Vat and ceil to whole integer
            'price' =>  round($product->price_amount / (($product->vat_amount / 100) + 1)),

            'vat' => $product->vat_amount,
            'price_including_vat' => $product->price_amount,
        ];

        $mailableValues = (object)[
            'first_name' => $formValues['invoice_first_name'],
            'last_name' => $formValues['invoice_last_name'],
            'email' => $formValues['email'],
            'site_url' => array_pull($formValues, 'site_url'),
            'we_transfer' => array_pull($formValues, 'we_transfer'),
        ];

        // Manual add country
        $formValues['invoice_country'] = 'Nederland';

        return [$product, $orderProduct, $mailableValues, $formValues];
    }

    /**
     * Sync a product with WeFact
     *
     * @param  Product  $product
     */
    public function syncProduct(Product $product)
    {
        if(empty($product->wefact_code)) {
            throw new \InvalidArgumentException('Product (id:' . $product->id .') did not have a wefact_code, and therefor could not sync.');
        }

        /** @var WeFactService $weFactService */
        $weFactService = app()->make(WeFactService::class);

        $weFactProduct = $weFactService->getProduct($product->wefact_code);
        $weFactProductModifiedDate = Carbon::createFromFormat(Carbon::DEFAULT_TO_STRING_FORMAT, $weFactProduct->Modified);

        // WeFact product and Product are up-to-date
        if($weFactProductModifiedDate == $product->wefact_modified_at) {
            Log::info(self::class.': Sync - WeFact product ' . $product->wefact_code . ' and product ' . $product->id . ' are up-to-date.');
            return;
        }

        // WeFact is newer, therefor sync WeFact to Product
        elseif($weFactProductModifiedDate > $product->wefact_modified_at){
            $this->syncWeFactProductToProduct($product, $weFactProduct, $weFactProductModifiedDate);
            return;
        }
    }

    /**
     * Save WeFact product into our belonging product
     *
     * @param  Product  $product
     * @param $weFactProduct
     * @param  Carbon  $weFactProductModifiedDate
     */
    private function syncWeFactProductToProduct(Product $product, $weFactProduct, Carbon $weFactProductModifiedDate)
    {
        $product->price_amount = WeFactAPI::convertPriceExclToIncl($weFactProduct->PriceExcl, $weFactProduct->TaxPercentage);;
        $product->price = '€ ' . number_format(($product->price_amount / 100), 2, ',', '.');

        $product->wefact_name = $weFactProduct->ProductName;
        $product->wefact_description = $weFactProduct->ProductKeyPhrase;
        $product->wefact_modified_at = $weFactProductModifiedDate;
        $product->save();

        Log::info(self::class.': Sync - Product ' . $product->id . ' has been synced with WeFact product ' . $product->wefact_code . '.');
    }

}