Duffer Derek

Current Path : /var/www/invoice-sync.bitkit.dk/httpdocs/app/Http/Service/Economic/
Upload File :
Current File : /var/www/invoice-sync.bitkit.dk/httpdocs/app/Http/Service/Economic/EconomicInvoiceUpdate.php

<?php

namespace App\Http\Service\Economic;

use App\Exceptions\DownloadFailedException;
use App\Exceptions\InvoiceNotFoundException;
use App\Exceptions\PodioAttributeNotFoundException;
use App\Exceptions\PodioItemCreationFailedException;
use App\Exceptions\PodioItemUpdateFailedException;
use App\Http\Interfaces\Invoice\EconomicInvoiceUpdateInterface;
use App\Http\Modules\Auth\PodioAuthentication;
use App\Models\UnprocessedHooks;
use App\Http\Modules\DataFormatter\ToPodio\Invoice as ToPodioInvoice;
use App\Http\Modules\ApiWrapper\EconomicApi;
use App\Http\Modules\ApiWrapper\PodioApi;

class EconomicInvoiceUpdate implements EconomicInvoiceUpdateInterface
{
    /**
     *
     * @var int
     */
    private $draftNo;

    /**
     *
     * @var EconomicApi
     */
    protected $economicApi;

    /**
     *
     * @var PodioApi
     */
    protected $podioApi;

    /**
     *
     * @var PodioAuthentication
     */
    protected $podioAuth;

    /**
     * @var UnprocessedHooks
     */
    private $hooks;
    /**
     * @var EconomicInvoiceLineSync
     */
    private $syncInvoiceLine;
    /**
     * @var EconomicCreditNoteSync
     */
    private $syncCreditNote;

    public function __construct(UnprocessedHooks $hooks)
    {
        $this->hooks = $hooks;
        $this->economicApi = new EconomicApi();
        $this->podioApi = new PodioApi();
        $this->podioAuth = new PodioAuthentication();
        $this->syncInvoiceLine = new EconomicInvoiceLineSync();
        $this->syncCreditNote = new EconomicCreditNoteSync();
    }

    public function getDraftNo(): int
    {
        $this->draftNo = (int)$this->hooks->object_no;
        return $this->draftNo;
    }

    public function getHook(): UnprocessedHooks
    {
        return $this->hooks;
    }

    /**
     * @throws DownloadFailedException
     * @throws PodioAttributeNotFoundException
     * @throws PodioItemCreationFailedException
     */
    public function podioUpdateAttribute($invoice): array
    {
        $toPodio = new ToPodioInvoice($invoice);
        return $toPodio->getData();
    }

    /**
     * @return mixed|void|null
     * @throws DownloadFailedException
     * @throws PodioAttributeNotFoundException
     * @throws InvoiceNotFoundException
     * @throws PodioItemUpdateFailedException|PodioItemCreationFailedException
     */
    public function processInvoiceUpdate()
    {
        $this->podioAuth->authenticatePodio();
        $draftNo = $this->getDraftNo();
        $invoiceDraft = $this->economicApi->getInvoiceDraftDetails($draftNo);
        $lineNos = [];
        if (array_key_exists('lines', $invoiceDraft)) {
            foreach ($invoiceDraft['lines'] as $lines) {
                $lineNos[] = $lines['lineNumber'];
            }
            $this->syncInvoiceLine->checkInvoiceLineDeleted($this->draftNo, $lineNos);
        }

        $invoiceItemId = $invoiceDraft['references']['other'];

        if (strpos($invoiceDraft['grossAmount'], '-') !== false) { //credited invoice
            //update the specific invoice with credited details
            $this->syncCreditNote->processCreditNoteSync($invoiceDraft, $invoiceItemId);
        } else {
            $attributes = $this->podioUpdateAttribute($invoiceDraft);
            $options = ['silent' => true, 'hook' => false];
            if (!$attributes) {
                throw new PodioAttributeNotFoundException('No attributes Provided');
            }
            return $this->podioApi->podioItemUpdate($invoiceItemId, $attributes, $options);
        }
    }
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists