Duffer Derek
<?php
namespace App\Http\Service\Economic;
use App\Exceptions\PodioAttributeNotFoundException;
use App\Exceptions\PodioItemCreationFailedException;
use App\Http\Interfaces\Invoice\EconomicInvoiceLineSyncInterface;
use App\Http\Modules\ApiWrapper\PodioApi;
use App\Http\Modules\DataFormatter\ToPodio\InvoiceLine as ToPodioInvoiceLine;
use Illuminate\Support\Facades\Log;
class EconomicInvoiceLineSync
{
/**
*
* @var PodioApi
*/
protected $podioApi;
public function __construct()
{
$this->podioApi = new PodioApi();
}
public function podioCreateAttribute($invoiceLine, $invoiceNo, $creditNoteNo, $currency, $exchangeRate): array
{
$toPodio = new ToPodioInvoiceLine($invoiceLine);
return $toPodio->getData($invoiceNo, $creditNoteNo, $currency, $exchangeRate);
}
// public function podioUpdateAttribute($invoiceLine, $draftNo, $currency): array
// {
// $toPodio = new ToPodioInvoiceLine($invoiceLine);
// return $toPodio->getData($draftNo, $currency);
// }
/**
* @throws PodioAttributeNotFoundException
* @throws PodioItemCreationFailedException
*/
public function processInvoiceLineItem($invoiceLineArray, $invoiceNo, $creditNoteNo, $currency, $exchangeRate): array
{
$lineItems = [];
foreach ($invoiceLineArray as $invoiceLine) {
$attributes = $this->podioCreateAttribute($invoiceLine, $invoiceNo, $creditNoteNo, $currency, $exchangeRate);
$options = ['silent' => true, 'hook' => false];
if (!$attributes) {
throw new PodioAttributeNotFoundException('No attributes Provided');
}
$response = $this->podioApi->podioItemCreate(config('podio.app_auth.invoiceLine.app_id'), $attributes, $options);
if ($response == null) {
throw new PodioItemCreationFailedException("Failed to create InvoiceLine Item in podio");
}
$lineItems[] = $response->item_id;
}
return array_unique($lineItems);
}
public function checkInvoiceLineDeleted($draftNo, $lineNos)
{
$limit = 50;
$offset = 0;
$attributes = array(
'filters' => array(
config('podio_apps.invoiceLine.external_ids.draft_no') => (string)$draftNo,
),
'limit' => $limit,
'offset' => $offset
);
$appId = config('podio.app_auth.invoiceLine.app_id');
$itemCollection = $this->podioApi->filterPodioItems($attributes, $appId);
foreach ($itemCollection as $item) {
foreach ($item->fields as $field) {
if ($field->external_id == 'e-invoice-draft-id') {
$id = (int)$field->values;
if (!in_array($id, $lineNos)) {
\PodioItem::delete($item->item_id, $attributes = array(), $options = array());
}
}
}
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists