diff --git a/app/Casts/QuickbooksSettingsCast.php b/app/Casts/QuickbooksSettingsCast.php index a5027733f180..815725213590 100644 --- a/app/Casts/QuickbooksSettingsCast.php +++ b/app/Casts/QuickbooksSettingsCast.php @@ -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); } } diff --git a/app/DataMapper/ClientSync.php b/app/DataMapper/ClientSync.php index 56b2b8e185f9..b45b2aca45a7 100644 --- a/app/DataMapper/ClientSync.php +++ b/app/DataMapper/ClientSync.php @@ -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); + } } diff --git a/app/DataMapper/InvoiceSync.php b/app/DataMapper/InvoiceSync.php index 90ecde71989a..b56b1a2239c2 100644 --- a/app/DataMapper/InvoiceSync.php +++ b/app/DataMapper/InvoiceSync.php @@ -20,7 +20,11 @@ use Illuminate\Contracts\Database\Eloquent\Castable; 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); + } } diff --git a/app/DataMapper/ProductSync.php b/app/DataMapper/ProductSync.php index 1417b0b0129b..dfc6aada909f 100644 --- a/app/DataMapper/ProductSync.php +++ b/app/DataMapper/ProductSync.php @@ -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); + } } diff --git a/app/DataMapper/QuickbooksSettings.php b/app/DataMapper/QuickbooksSettings.php index 1442914e4b5f..2881c6d384a3 100644 --- a/app/DataMapper/QuickbooksSettings.php +++ b/app/DataMapper/QuickbooksSettings.php @@ -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 $arguments - */ public static function castUsing(array $arguments): string { return QuickbooksSettingsCast::class; } + public static function fromArray(array $data): self + { + return new self($data); + } + } diff --git a/app/DataMapper/QuickbooksSync.php b/app/DataMapper/QuickbooksSync.php new file mode 100644 index 000000000000..d5154862b399 --- /dev/null +++ b/app/DataMapper/QuickbooksSync.php @@ -0,0 +1,49 @@ +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'] ?? []); + } +} diff --git a/app/DataMapper/QuickbooksSyncMap.php b/app/DataMapper/QuickbooksSyncMap.php new file mode 100644 index 000000000000..b4d1e203b18c --- /dev/null +++ b/app/DataMapper/QuickbooksSyncMap.php @@ -0,0 +1,32 @@ +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; diff --git a/app/Services/Quickbooks/Models/QbProduct.php b/app/Services/Quickbooks/Models/QbProduct.php index cc86b7c6ea77..33a0c61b68ac 100644 --- a/app/Services/Quickbooks/Models/QbProduct.php +++ b/app/Services/Quickbooks/Models/QbProduct.php @@ -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;