Updates for castables

This commit is contained in:
David Bomba 2024-09-23 07:16:34 +10:00
parent 49601dfa5e
commit 09b803d9dc
9 changed files with 139 additions and 55 deletions

View File

@ -18,33 +18,19 @@ class QuickbooksSettingsCast implements CastsAttributes
{
public function get($model, string $key, $value, array $attributes)
{
if (is_null($value)) {
return new QuickbooksSettings();
$data = json_decode($value, true);
if(!is_array($data))
return null;
$qb = new QuickbooksSettings();
$qb->accessTokenKey = $data['accessTokenKey'];
$qb->refresh_token = $data['refresh_token'];
$qb->realmID = $data['realmID'];
$qb->accessTokenExpiresAt = $data['accessTokenExpiresAt'];
$qb->refreshTokenExpiresAt = $data['refreshTokenExpiresAt'];
$qb->settings = $data['settings'] ?? [];
return $qb;
return QuickbooksSettings::fromArray($data);
}
public function set($model, string $key, $value, array $attributes)
{
return [
$key => json_encode([
'accessTokenKey' => $value->accessTokenKey,
'refresh_token' => $value->refresh_token,
'realmID' => $value->realmID,
'accessTokenExpiresAt' => $value->accessTokenExpiresAt,
'refreshTokenExpiresAt' => $value->refreshTokenExpiresAt,
'settings' => $value->settings,
])
];
if ($value instanceof QuickbooksSettings) {
return json_encode(get_object_vars($value));
}
return json_encode($value);
}
}

View File

@ -21,7 +21,10 @@ class ClientSync implements Castable
{
public string $qb_id;
public function __construct(array $attributes = [])
{
$this->qb_id = $attributes['qb_id'] ?? '';
}
/**
* Get the name of the caster class to use when casting from / to this cast target.
*
@ -32,4 +35,8 @@ class ClientSync implements Castable
return ClientSyncCast::class;
}
public static function fromArray(array $data): self
{
return new self($data);
}
}

View File

@ -21,6 +21,10 @@ class InvoiceSync implements Castable
{
public string $qb_id;
public function __construct(array $attributes = [])
{
$this->qb_id = $attributes['qb_id'] ?? '';
}
/**
* Get the name of the caster class to use when casting from / to this cast target.
@ -32,4 +36,8 @@ class InvoiceSync implements Castable
return InvoiceSyncCast::class;
}
public static function fromArray(array $data): self
{
return new self($data);
}
}

View File

@ -21,6 +21,10 @@ class ProductSync implements Castable
{
public string $qb_id;
public function __construct(array $attributes = [])
{
$this->qb_id = $attributes['qb_id'] ?? '';
}
/**
* Get the name of the caster class to use when casting from / to this cast target.
@ -32,4 +36,8 @@ class ProductSync implements Castable
return ProductSyncCast::class;
}
public static function fromArray(array $data): self
{
return new self($data);
}
}

View File

@ -30,34 +30,28 @@ class QuickbooksSettings implements Castable
public int $refreshTokenExpiresAt;
public string $baseURL;
/**
* entity client,invoice,quote,purchase_order,vendor,payment
* sync true/false
* update_record true/false
* direction push/pull/birectional
* */
public array $settings = [
'client' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'vendor' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'invoice' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'sales' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'quote' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'purchase_order' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'product' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'payment' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'vendor' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
'expense' => ['sync' => true, 'update_record' => true, 'direction' => 'bidirectional'],
];
public QuickbooksSync $settings;
public function __construct(array $attributes = [])
{
$this->accessTokenKey = $attributes['accessTokenKey'] ?? '';
$this->refresh_token = $attributes['refresh_token'] ?? '';
$this->realmID = $attributes['realmID'] ?? '';
$this->accessTokenExpiresAt = $attributes['accessTokenExpiresAt'] ?? 0;
$this->refreshTokenExpiresAt = $attributes['refreshTokenExpiresAt'] ?? 0;
$this->baseURL = $attributes['baseURL'] ?? '';
$this->settings = new QuickbooksSync($attributes['settings']);
}
/**
* Get the name of the caster class to use when casting from / to this cast target.
*
* @param array<string, mixed> $arguments
*/
public static function castUsing(array $arguments): string
{
return QuickbooksSettingsCast::class;
}
public static function fromArray(array $data): self
{
return new self($data);
}
}

View File

@ -0,0 +1,49 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\DataMapper;
/**
* QuickbooksSync.
*/
class QuickbooksSync
{
public QuickbooksSyncMap $client;
public QuickbooksSyncMap $vendor;
public QuickbooksSyncMap $invoice;
public QuickbooksSyncMap $sales;
public QuickbooksSyncMap $quote;
public QuickbooksSyncMap $purchase_order;
public QuickbooksSyncMap $product;
public QuickbooksSyncMap $payment;
public QuickbooksSyncMap $expense;
public function __construct(array $attributes = [])
{
$this->client = new QuickbooksSyncMap($attributes['client'] ?? []);
$this->vendor = new QuickbooksSyncMap($attributes['vendor'] ?? []);
$this->invoice = new QuickbooksSyncMap($attributes['invoice'] ?? []);
$this->sales = new QuickbooksSyncMap($attributes['sales'] ?? []);
$this->quote = new QuickbooksSyncMap($attributes['quote'] ?? []);
$this->purchase_order = new QuickbooksSyncMap($attributes['purchase_order'] ?? []);
$this->product = new QuickbooksSyncMap($attributes['product'] ?? []);
$this->payment = new QuickbooksSyncMap($attributes['payment'] ?? []);
$this->expense = new QuickbooksSyncMap($attributes['expense'] ?? []);
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\DataMapper;
enum SyncDirection: string
{
case PUSH = 'push';
case PULL = 'pull';
case BIDIRECTIONAL = 'bidirectional';
}
/**
* QuickbooksSyncMap.
*/
class QuickbooksSyncMap
{
public bool $sync = true;
public bool $update_record = true;
public SyncDirection $direction = SyncDirection::BIDIRECTIONAL;
}

View File

@ -104,7 +104,7 @@ class QuickbooksSync implements ShouldQueue
*/
private function syncGate(string $entity, string $direction): bool
{
return (bool) $this->settings[$entity]['sync'] && in_array($this->settings[$entity]['direction'], [$direction,'bidirectional']);
return (bool) $this->settings->{$entity}->sync && in_array($this->settings->{$entity}->direction, [$direction,'bidirectional']);
}
/**
@ -115,7 +115,7 @@ class QuickbooksSync implements ShouldQueue
*/
private function updateGate(string $entity): bool
{
return (bool) $this->settings[$entity]['sync'] && $this->settings[$entity]['update_record'];
return (bool) $this->settings->{$entity}->sync && $this->settings->{$entity}->update_record;
}
/**
@ -222,7 +222,7 @@ class QuickbooksSync implements ShouldQueue
return $invoice;
} elseif($search->count() == 1) {
return $this->settings['invoice']['update_record'] ? $search->first() : null;
return $this->settings->invoice->update_record ? $search->first() : null;
}
return null;
@ -347,7 +347,7 @@ class QuickbooksSync implements ShouldQueue
return ExpenseFactory::create($this->company->id, $this->company->owner()->id);
}
elseif($search->count() == 1) {
return $this->settings['expense']['update_record'] ? $search->first() : null;
return $this->settings->expense->update_record ? $search->first() : null;
}
return null;
@ -377,7 +377,7 @@ class QuickbooksSync implements ShouldQueue
return VendorFactory::create($this->company->id, $this->company->owner()->id);
}
elseif($search->count() == 1) {
return $this->settings['vendor']['update_record'] ? $search->first() : null;
return $this->settings->vendor->update_record ? $search->first() : null;
}
return null;
@ -411,7 +411,7 @@ class QuickbooksSync implements ShouldQueue
return $client;
}
elseif($search->count() == 1) {
return $this->settings['client']['update_record'] ? $search->first() : null;
return $this->settings->client->update_record ? $search->first() : null;
}
return null;

View File

@ -66,7 +66,7 @@ class QbProduct
return $product;
} elseif($search->count() == 1) {
return $this->service->settings['product']['update_record'] ? $search->first() : null;
return $this->service->settings->product->update_record ? $search->first() : null;
}
return null;