1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-12 22:22:32 +01:00
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
/**
* 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;
@ -25,8 +34,8 @@ class ImportQuickbooksController extends BaseController
$qb = new QuickbooksService($company);
$realm = $request->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);

View File

@ -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,
]);
}

View File

@ -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);
}

View File

@ -1,4 +1,13 @@
<?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;
@ -99,7 +108,7 @@ class SdkWrapper
if($token_object->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;
}
}