diff --git a/app/Services/Import/Quickbooks/Service.php b/app/Services/Import/Quickbooks/Service.php new file mode 100644 index 000000000000..58461f4a4d25 --- /dev/null +++ b/app/Services/Import/Quickbooks/Service.php @@ -0,0 +1,67 @@ +sdk = $quickbooks; + } + + public function getAccessToken() : array + { + // TODO: Cache token and + $token = $this->sdk->getAccessToken(); + $access_token = $token->getAccessToken(); + $refresh_token = $token->getRefreshToken(); + $access_token_expires = $token->getAccessTokenExpiresAt(); + $refresh_token_expires = $token->getRefreshTokenExpiresAt(); + //TODO: Cache token object. Update $sdk instance? + return compact('access_token', 'refresh_token','access_token_expires', 'refresh_token_expires'); + } + + public function getRefreshToken() : array + { + // TODO: Check if token is Cached otherwise fetch a new one and Cache token and expire + return $this->getAccessToken(); + } + /** + * fetch QuickBooks invoice records + * @param int $max The maximum records to fetch. Default 100 + * @return Illuminate\Support\Collection; + */ + public function fetchInvoices(int $max = 100): Collection + { + return $this->transformer->transform($this->fetchRecords( 'Invoice', $max), 'Invoice'); + } + + protected function fetchRecords(string $entity, $max = 100) : Collection { + return (self::RepositoryFactory($entity))->get($max); + } + + private static function RepositoryFactory(string $entity) : RepositoryInterface + { + return app("\\App\\Repositories\\Import\Quickbooks\\{$entity}Repository"); + } + + /** + * fetch QuickBooks customer records + * @param int $max The maximum records to fetch. Default 100 + * @return Illuminate\Support\Collection; + */ + public function fetchCustomers(int $max = 100): Collection + { + return $this->fetchRecords('Customer', $max) ; + } + + public function totalRecords(string $entity) : int + { + return (self::RepositoryFactory($entity))->count(); + } +} \ No newline at end of file