This commit is contained in:
David Bomba 2024-08-23 22:55:31 +10:00
parent b24d843164
commit b1f73f97de
4 changed files with 64 additions and 46 deletions

View File

@ -1,4 +1,13 @@
<?php <?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\Http\Controllers; namespace App\Http\Controllers;
@ -25,8 +34,8 @@ class ImportQuickbooksController extends BaseController
$qb = new QuickbooksService($company); $qb = new QuickbooksService($company);
$realm = $request->query('realmId'); $realm = $request->query('realmId');
$access_token_object = $qb->getAuth()->accessTokenFromCode($request->query('code'), $realm); $access_token_object = $qb->sdk()->accessTokenFromCode($request->query('code'), $realm);
$qb->getAuth()->saveOAuthToken($access_token_object); $qb->sdk()->saveOAuthToken($access_token_object);
return redirect(config('ninja.react_url')); return redirect(config('ninja.react_url'));
@ -43,8 +52,8 @@ class ImportQuickbooksController extends BaseController
$company = $request->getCompany(); $company = $request->getCompany();
$qb = new QuickbooksService($company); $qb = new QuickbooksService($company);
$authorizationUrl = $qb->getAuth()->getAuthorizationUrl(); $authorizationUrl = $qb->sdk()->getAuthorizationUrl();
$state = $qb->getAuth()->getState(); $state = $qb->sdk()->getState();
Cache::put($state, $token, 190); Cache::put($state, $token, 190);

View File

@ -67,7 +67,7 @@ class ClientTransformer extends BaseTransformer
} }
$transformed_data = $this->preTransform($data); $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; return $transformed_data;
} }
@ -79,7 +79,9 @@ class ClientTransformer extends BaseTransformer
'last_name' => $this->getString($data, 'FamilyName'), 'last_name' => $this->getString($data, 'FamilyName'),
'phone' => $this->getString($data, 'PrimaryPhone.FreeFormNumber'), 'phone' => $this->getString($data, 'PrimaryPhone.FreeFormNumber'),
'email' => $this->getString($data, 'PrimaryEmailAddr.Address'), '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,
]); ]);
} }

View File

@ -11,11 +11,9 @@
namespace App\Services\Import\Quickbooks; namespace App\Services\Import\Quickbooks;
use Carbon\Carbon;
use App\Models\Company; use App\Models\Company;
use QuickBooksOnline\API\Core\CoreConstants; use QuickBooksOnline\API\Core\CoreConstants;
use QuickBooksOnline\API\DataService\DataService; use QuickBooksOnline\API\DataService\DataService;
use QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken;
// quickbooks_realm_id // quickbooks_realm_id
// quickbooks_refresh_token // quickbooks_refresh_token
@ -40,12 +38,12 @@ class QuickbooksService
'auth_mode' => 'oauth2', 'auth_mode' => 'oauth2',
'scope' => "com.intuit.quickbooks.accounting", 'scope' => "com.intuit.quickbooks.accounting",
// 'RedirectURI' => 'https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl', // '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, 'baseUrl' => $this->testMode ? CoreConstants::SANDBOX_DEVELOPMENT : CoreConstants::QBO_BASEURL,
]; ];
$merged = array_merge($config, $this->ninjaAccessToken()); $merged = array_merge($config, $this->ninjaAccessToken());
nlog($merged);
$this->sdk = DataService::Configure($merged); $this->sdk = DataService::Configure($merged);
$this->sdk->setLogLocation(storage_path("logs/quickbooks.log")); $this->sdk->setLogLocation(storage_path("logs/quickbooks.log"));
@ -71,7 +69,7 @@ class QuickbooksService
return $this->sdk; return $this->sdk;
} }
public function getAuth(): SdkWrapper public function sdk(): SdkWrapper
{ {
return new SdkWrapper($this->sdk, $this->company); return new SdkWrapper($this->sdk, $this->company);
} }

View File

@ -1,4 +1,13 @@
<?php <?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\Services\Import\Quickbooks; namespace App\Services\Import\Quickbooks;
@ -99,7 +108,7 @@ class SdkWrapper
if($token_object->accessTokenExpiresAt < time()){ if($token_object->accessTokenExpiresAt < time()){
$new_token = $this->sdk->getOAuth2LoginHelper()->refreshToken(); $new_token = $this->sdk->getOAuth2LoginHelper()->refreshToken();
nlog("getting new token");
$this->setAccessToken($new_token); $this->setAccessToken($new_token);
$this->saveOAuthToken($this->accessToken()); $this->saveOAuthToken($this->accessToken());
} }
@ -118,9 +127,7 @@ nlog("getting new token");
// $this->sdk = $this->sdk->updateOAuth2Token($token); // $this->sdk = $this->sdk->updateOAuth2Token($token);
$this->token = $token; $this->token = $token;
nlog("set access token");
nlog($token);
return $this; return $this;
} }
@ -144,10 +151,12 @@ nlog($token);
$this->company->save(); $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 (int)$this->sdk->Query("select count(*) from $entity");
return $this->sdk->Query("select count(*) from $entity");
} }
private function queryData(string $query, int $start = 1, $limit = 100): array 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); 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)) { if(!in_array($entity, $this->entities)) {
// return []; return [];
// } }
// $records = []; $records = [];
// $start = 0; $start = 0;
// $limit = 100; $limit = 100;
// try { try {
// $total = $this->totalRecords($entity); $total = $this->totalRecords($entity);
// $total = min($max, $total); $total = min($max, $total);
// // Step 3 & 4: Get chunks of records until the total required records are retrieved // Step 3 & 4: Get chunks of records until the total required records are retrieved
// do { do {
// $limit = min(self::MAXRESULTS, $total - $start); $limit = min(self::MAXRESULTS, $total - $start);
// $recordsChunk = $this->queryData("select * from $entity", $start, $limit); $recordsChunk = $this->queryData("select * from $entity", $start, $limit);
// if(empty($recordsChunk)) { if(empty($recordsChunk)) {
// break; break;
// } }
// $records = array_merge($records, $recordsChunk); $records = array_merge($records, $recordsChunk);
// $start += $limit; $start += $limit;
// } while ($start < $total); } while ($start < $total);
// if(empty($records)) { if(empty($records)) {
// throw new \Exception("No records retrieved!"); throw new \Exception("No records retrieved!");
// } }
// } catch (\Throwable $th) { } catch (\Throwable $th) {
// nlog("Fetch Quickbooks API Error: {$th->getMessage()}"); nlog("Fetch Quickbooks API Error: {$th->getMessage()}");
// } }
// return $records; return $records;
// } }
} }