diff --git a/app/Http/Controllers/ImportQuickbooksController.php b/app/Http/Controllers/ImportQuickbooksController.php index 9030d3c33a01..be08f66333d4 100644 --- a/app/Http/Controllers/ImportQuickbooksController.php +++ b/app/Http/Controllers/ImportQuickbooksController.php @@ -1,4 +1,13 @@ query('realmId'); - $access_token_object = $qb->getAuth()->accessTokenFromCode($request->query('code'), $realm); - $qb->getAuth()->saveOAuthToken($access_token_object); + $access_token_object = $qb->sdk()->accessTokenFromCode($request->query('code'), $realm); + $qb->sdk()->saveOAuthToken($access_token_object); return redirect(config('ninja.react_url')); @@ -43,8 +52,8 @@ class ImportQuickbooksController extends BaseController $company = $request->getCompany(); $qb = new QuickbooksService($company); - $authorizationUrl = $qb->getAuth()->getAuthorizationUrl(); - $state = $qb->getAuth()->getState(); + $authorizationUrl = $qb->sdk()->getAuthorizationUrl(); + $state = $qb->sdk()->getState(); Cache::put($state, $token, 190); diff --git a/app/Import/Transformer/Quickbooks/ClientTransformer.php b/app/Import/Transformer/Quickbooks/ClientTransformer.php index 1a1d487652aa..7ad80ad8c737 100644 --- a/app/Import/Transformer/Quickbooks/ClientTransformer.php +++ b/app/Import/Transformer/Quickbooks/ClientTransformer.php @@ -67,7 +67,7 @@ class ClientTransformer extends BaseTransformer } $transformed_data = $this->preTransform($data); - $transformed_data['contacts'][0] = $this->getContacts($data)->toArray() + ['company_id' => $this->company->id ]; + $transformed_data['contacts'][0] = $this->getContacts($data)->toArray() + ['company_id' => $this->company->id, 'user_id' => $this->company->owner()->id ]; return $transformed_data; } @@ -79,7 +79,9 @@ class ClientTransformer extends BaseTransformer 'last_name' => $this->getString($data, 'FamilyName'), 'phone' => $this->getString($data, 'PrimaryPhone.FreeFormNumber'), 'email' => $this->getString($data, 'PrimaryEmailAddr.Address'), - 'company_id' => $this->company->id + 'company_id' => $this->company->id, + 'user_id' => $this->company->owner()->id, + 'send_email' => true, ]); } diff --git a/app/Services/Import/Quickbooks/QuickbooksService.php b/app/Services/Import/Quickbooks/QuickbooksService.php index 1eb45c0cd9dc..d2a9cdd1bfb5 100644 --- a/app/Services/Import/Quickbooks/QuickbooksService.php +++ b/app/Services/Import/Quickbooks/QuickbooksService.php @@ -11,11 +11,9 @@ namespace App\Services\Import\Quickbooks; -use Carbon\Carbon; use App\Models\Company; use QuickBooksOnline\API\Core\CoreConstants; use QuickBooksOnline\API\DataService\DataService; -use QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken; // quickbooks_realm_id // quickbooks_refresh_token @@ -40,12 +38,12 @@ class QuickbooksService 'auth_mode' => 'oauth2', 'scope' => "com.intuit.quickbooks.accounting", // 'RedirectURI' => 'https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl', - 'RedirectURI' => 'https://above-distinctly-teal.ngrok-free.app/quickbooks/authorized', + 'RedirectURI' => $this->testMode ? 'https://above-distinctly-teal.ngrok-free.app/quickbooks/authorized' : 'https://invoicing.co/quickbooks/authorized', 'baseUrl' => $this->testMode ? CoreConstants::SANDBOX_DEVELOPMENT : CoreConstants::QBO_BASEURL, ]; $merged = array_merge($config, $this->ninjaAccessToken()); - nlog($merged); + $this->sdk = DataService::Configure($merged); $this->sdk->setLogLocation(storage_path("logs/quickbooks.log")); @@ -71,7 +69,7 @@ class QuickbooksService return $this->sdk; } - public function getAuth(): SdkWrapper + public function sdk(): SdkWrapper { return new SdkWrapper($this->sdk, $this->company); } diff --git a/app/Services/Import/Quickbooks/SdkWrapper.php b/app/Services/Import/Quickbooks/SdkWrapper.php index 34fa51509a13..493719144cec 100644 --- a/app/Services/Import/Quickbooks/SdkWrapper.php +++ b/app/Services/Import/Quickbooks/SdkWrapper.php @@ -1,4 +1,13 @@ accessTokenExpiresAt < time()){ $new_token = $this->sdk->getOAuth2LoginHelper()->refreshToken(); -nlog("getting new token"); + $this->setAccessToken($new_token); $this->saveOAuthToken($this->accessToken()); } @@ -118,9 +127,7 @@ nlog("getting new token"); // $this->sdk = $this->sdk->updateOAuth2Token($token); $this->token = $token; -nlog("set access token"); -nlog($token); return $this; } @@ -144,10 +151,12 @@ nlog($token); $this->company->save(); } - public function totalRecords(string $entity) //returns an array not int + + /// Data Access /// + + public function totalRecords(string $entity): int { - nlog($this->sdk->Query("select count(*) from $entity")); - return $this->sdk->Query("select count(*) from $entity"); + return (int)$this->sdk->Query("select count(*) from $entity"); } private function queryData(string $query, int $start = 1, $limit = 100): array @@ -155,39 +164,39 @@ nlog($token); return (array) $this->sdk->Query($query, $start, $limit); } - // public function fetchRecords(string $entity, int $max = 1000): array - // { + public function fetchRecords(string $entity, int $max = 1000): array + { - // if(!in_array($entity, $this->entities)) { - // return []; - // } + if(!in_array($entity, $this->entities)) { + return []; + } - // $records = []; - // $start = 0; - // $limit = 100; - // try { - // $total = $this->totalRecords($entity); - // $total = min($max, $total); + $records = []; + $start = 0; + $limit = 100; + try { + $total = $this->totalRecords($entity); + $total = min($max, $total); - // // Step 3 & 4: Get chunks of records until the total required records are retrieved - // do { - // $limit = min(self::MAXRESULTS, $total - $start); - // $recordsChunk = $this->queryData("select * from $entity", $start, $limit); - // if(empty($recordsChunk)) { - // break; - // } + // Step 3 & 4: Get chunks of records until the total required records are retrieved + do { + $limit = min(self::MAXRESULTS, $total - $start); + $recordsChunk = $this->queryData("select * from $entity", $start, $limit); + if(empty($recordsChunk)) { + break; + } - // $records = array_merge($records, $recordsChunk); - // $start += $limit; - // } while ($start < $total); - // if(empty($records)) { - // throw new \Exception("No records retrieved!"); - // } + $records = array_merge($records, $recordsChunk); + $start += $limit; + } while ($start < $total); + if(empty($records)) { + throw new \Exception("No records retrieved!"); + } - // } catch (\Throwable $th) { - // nlog("Fetch Quickbooks API Error: {$th->getMessage()}"); - // } + } catch (\Throwable $th) { + nlog("Fetch Quickbooks API Error: {$th->getMessage()}"); + } - // return $records; - // } + return $records; + } }