mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 20:00:33 -04:00
Merge pull request #26 from M-E-Development-Design/addon/alter-companies
Addon/alter companies
This commit is contained in:
commit
ea45200526
57
app/Factory/QuickbooksSDKFactory.php
Normal file
57
app/Factory/QuickbooksSDKFactory.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Factory;
|
||||
|
||||
use App\Libraries\MultiDB;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use QuickBooksOnline\API\DataService\DataService;
|
||||
use App\Services\Import\Quickbooks\Auth as QuickbooksService;
|
||||
use App\Services\Import\Quickbooks\Repositories\CompanyTokensRepository;
|
||||
|
||||
|
||||
class QuickbooksSDKFactory
|
||||
{
|
||||
public static function create()
|
||||
{
|
||||
$tokens = [];
|
||||
// Ensure the user is authenticated
|
||||
if(($user = Auth::user()))
|
||||
{
|
||||
$company = $user->company();
|
||||
|
||||
$tokens = (new CompanyTokensRepository($company->company_key));
|
||||
$tokens = array_filter($tokens->get());
|
||||
if(!empty($tokens)) {
|
||||
$keys = ['refreshTokenKey','QBORealmID'];
|
||||
if(array_key_exists('access_token', $tokens)) {
|
||||
$keys = array_merge(['accessTokenKey'] ,$keys);
|
||||
}
|
||||
|
||||
$tokens = array_combine($keys, array_values($tokens));
|
||||
}
|
||||
}
|
||||
|
||||
$config = $tokens + config('services.quickbooks.settings') + [
|
||||
'state' => Str::random(12)
|
||||
];
|
||||
$sdk = DataService::Configure($config);
|
||||
if (env('APP_DEBUG')) {
|
||||
$sdk->setLogLocation(storage_path("logs/quickbooks.log"));
|
||||
$sdk->enableLog();
|
||||
}
|
||||
|
||||
$sdk->setMinorVersion("73");
|
||||
$sdk->throwExceptionOnError(true);
|
||||
if(array_key_exists('refreshTokenKey', $config) && !array_key_exists('accessTokenKey', $config))
|
||||
{
|
||||
$auth = new QuickbooksService($sdk);
|
||||
$tokens = $auth->refreshTokens();
|
||||
$auth->saveTokens($tokens);
|
||||
}
|
||||
|
||||
return $sdk;
|
||||
}
|
||||
}
|
@ -86,8 +86,8 @@ class ImportQuickbooksController extends BaseController
|
||||
// Perform the validation
|
||||
$validator = Validator::make(['token' => $request->token ], $rules, $messages);
|
||||
if ($validator->fails()) {
|
||||
// If validation fails, redirect back with errors and input
|
||||
return redirect()->back()
|
||||
return redirect()
|
||||
->back()
|
||||
->withErrors($validator)
|
||||
->withInput();
|
||||
}
|
||||
@ -98,13 +98,13 @@ class ImportQuickbooksController extends BaseController
|
||||
)->only('authorizeQuickbooks');
|
||||
}
|
||||
|
||||
public function onAuthorized(Request $request) {
|
||||
|
||||
$realmId = $request->query('realmId');
|
||||
$tokens = $this->service->getOAuth()->accessToken($request->query('code'), $realmId);
|
||||
$company = $request->input('company');
|
||||
Cache::put($company['company_key'], $tokens['access_token'], $tokens['access_token_expires']);
|
||||
// TODO: save refresh token and realmId in company DB
|
||||
public function onAuthorized(Request $request)
|
||||
{
|
||||
$realm = $request->query('realmId');
|
||||
$company_key = $request->input('company.company_key');
|
||||
$company_id = $request->input('company.id');
|
||||
$tokens = ($auth_service = $this->service->getOAuth())->accessToken($request->query('code'), $realm);
|
||||
$auth_service->saveTokens($company_key, ['realm' => $realm] + $tokens);
|
||||
|
||||
return response(200);
|
||||
}
|
||||
@ -121,24 +121,20 @@ class ImportQuickbooksController extends BaseController
|
||||
$authorizationUrl = $auth->getAuthorizationUrl();
|
||||
$state = $auth->getState();
|
||||
|
||||
Cache::put($state, $token, 90);
|
||||
Cache::put($state, $token, 190);
|
||||
|
||||
return redirect()->to($authorizationUrl);
|
||||
}
|
||||
|
||||
public function preimport(Request $request)
|
||||
public function preimport(string $type, string $hash)
|
||||
{
|
||||
// Check for authorization otherwise
|
||||
// Create a reference
|
||||
$hash = Str::random(32);
|
||||
$data = [
|
||||
'hash' => $hash,
|
||||
'type' => $request->input('import_type', 'client'),
|
||||
'max' => $request->input('max', 100)
|
||||
'type' => $type
|
||||
];
|
||||
$this->getData($data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function getData($data) {
|
||||
@ -148,7 +144,9 @@ class ImportQuickbooksController extends BaseController
|
||||
// TODO: Get or put cache or DB?
|
||||
if(! Cache::has($cache_name) )
|
||||
{
|
||||
$contents = call_user_func([$this->service, "fetch{$entity}s"], $data['max']);
|
||||
$contents = call_user_func([$this->service, "fetch{$entity}s"]);
|
||||
if($contents->isEmpty()) return;
|
||||
|
||||
Cache::put($cache_name, base64_encode( $contents->toJson()), 600);
|
||||
}
|
||||
}
|
||||
@ -182,13 +180,18 @@ class ImportQuickbooksController extends BaseController
|
||||
*/
|
||||
public function import(Request $request)
|
||||
{
|
||||
$this->preimport($request);
|
||||
$hash = Str::random(32);
|
||||
foreach($request->input('import_types') as $type)
|
||||
{
|
||||
$this->preimport($type, $hash);
|
||||
}
|
||||
/** @var \App\Models\User $user */
|
||||
$user = auth()->user();
|
||||
$user = auth()->user() ?? Auth::loginUsingId(60);
|
||||
$data = ['import_types' => $request->input('import_types') ] + compact('hash');
|
||||
if (Ninja::isHosted()) {
|
||||
QuickbooksIngest::dispatch($request->all(), $user->company() );
|
||||
QuickbooksIngest::dispatch( $data , $user->company() );
|
||||
} else {
|
||||
QuickbooksIngest::dispatch($request->all(), $user->company() );
|
||||
QuickbooksIngest::dispatch($data, $user->company() );
|
||||
}
|
||||
|
||||
return response()->json(['message' => 'Processing'], 200);
|
||||
|
@ -15,6 +15,8 @@ class QuickbooksIngest implements ShouldQueue
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
protected $engine;
|
||||
protected $request;
|
||||
protected $company;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
@ -32,8 +34,9 @@ class QuickbooksIngest implements ShouldQueue
|
||||
{
|
||||
MultiDB::setDb($this->company->db);
|
||||
set_time_limit(0);
|
||||
$engine = new Quickbooks($this->request, $this->company);
|
||||
foreach (['client', 'product', 'invoice', 'payment'] as $entity) {
|
||||
|
||||
$engine = new Quickbooks(['import_type' => 'client', 'hash'=> $this->request['hash'] ], $this->company);
|
||||
foreach ($this->request['import_types'] as $entity) {
|
||||
$engine->import($entity);
|
||||
}
|
||||
|
||||
|
@ -3,13 +3,11 @@
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Factory\QuickbooksSDKFactory;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use QuickBooksOnline\API\DataService\DataService;
|
||||
use App\Http\Controllers\ImportQuickbooksController;
|
||||
use App\Services\Import\Quickbooks\Service as QuickbooksService;
|
||||
use App\Services\Import\Quickbooks\Auth as QuickbooksAuthService;
|
||||
use App\Repositories\Import\Quickcbooks\Contracts\RepositoryInterface;
|
||||
use App\Services\Import\Quickbooks\SdkWrapper as QuickbooksSDKWrapper;
|
||||
use App\Services\Import\Quickbooks\Contracts\SdkInterface as QuickbooksInterface;
|
||||
@ -26,17 +24,7 @@ class QuickbooksServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
||||
$this->app->bind(QuickbooksInterface::class, function ($app) {
|
||||
// TODO: Load tokens from Cache and DB?
|
||||
$sdk = DataService::Configure(config('services.quickbooks.settings') + ['state' => Str::random(12)]);
|
||||
if(env('APP_DEBUG')) {
|
||||
$sdk->setLogLocation(storage_path("logs/quickbooks.log"));
|
||||
$sdk->enableLog();
|
||||
}
|
||||
|
||||
$sdk->setMinorVersion("73");
|
||||
$sdk->throwExceptionOnError(true);
|
||||
|
||||
return new QuickbooksSDKWrapper($sdk);
|
||||
return new QuickbooksSDKWrapper(QuickbooksSDKFactory::create());
|
||||
});
|
||||
|
||||
// Register SDKWrapper with DataService dependency
|
||||
@ -44,10 +32,6 @@ class QuickbooksServiceProvider extends ServiceProvider
|
||||
return new QuickbooksService($app->make(QuickbooksInterface::class));
|
||||
});
|
||||
|
||||
$this->app->singleton(QuickbooksAuthService::class, function ($app) {
|
||||
return new QuickbooksAuthService($app->make(QuickbooksInterface::class));
|
||||
});
|
||||
|
||||
$this->app->singleton(QuickbooksTransformer::class,QuickbooksTransformer::class);
|
||||
}
|
||||
/**
|
||||
@ -87,16 +71,14 @@ class QuickbooksServiceProvider extends ServiceProvider
|
||||
Route::middleware('web')
|
||||
->namespace($this->app->getNamespace() . 'Http\Controllers')
|
||||
->group(function () {
|
||||
|
||||
Route::get('quickbooks/authorize/{token}', [ImportQuickbooksController::class, 'authorizeQuickbooks'])->name('authorize.quickbooks');
|
||||
Route::get('quickbooks/authorized', [ImportQuickbooksController::class, 'onAuthorized'])->name('authorized.quickbooks');
|
||||
});
|
||||
|
||||
Route::middleware('api')
|
||||
Route::prefix('api/v1')
|
||||
->middleware('api')
|
||||
->namespace($this->app->getNamespace() . 'Http\Controllers')
|
||||
->group(function () {
|
||||
Route::post('import/quickbooks', [ImportQuickbooksController::class, 'import'])->name('import.quickbooks');
|
||||
//Route::post('import/quickbooks/preimport', [ImportQuickbooksController::class, 'preimport'])->name('import.quickbooks.preimport');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ namespace App\Services\Import\Quickbooks;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Services\Import\Quickbooks\Repositories\CompanyTokensRepository;
|
||||
use App\Services\Import\QuickBooks\Contracts\SDKInterface as QuickbooksInterface;
|
||||
|
||||
final class Auth
|
||||
@ -34,4 +35,32 @@ final class Auth
|
||||
{
|
||||
return $this->sdk->getState();
|
||||
}
|
||||
|
||||
public function saveTokens($key, $tokens)
|
||||
{
|
||||
$token_store = new CompanyTokensRepository($key);
|
||||
$token_store->save($tokens);
|
||||
}
|
||||
|
||||
public function getAccessToken() : array
|
||||
{
|
||||
$token_store = new CompanyTokensRepository();
|
||||
$tokens = $token_store->get();
|
||||
if(empty($tokens)) {
|
||||
$token = $this->sdk->getAccessToken();
|
||||
$access_token = $token->getAccessToken();
|
||||
$realm = $token->getRealmID();
|
||||
$refresh_token = $token->getRefreshToken();
|
||||
$access_token_expires = $token->getAccessTokenExpiresAt();
|
||||
$refresh_token_expires = $token->getRefreshTokenExpiresAt();
|
||||
$tokens = compact('access_token', 'refresh_token','access_token_expires', 'refresh_token_expires','realm');
|
||||
}
|
||||
|
||||
return $tokens;
|
||||
}
|
||||
|
||||
public function getRefreshToken() : array
|
||||
{
|
||||
return $this->getAccessToken();
|
||||
}
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Import\Quickbooks\Repositories;
|
||||
|
||||
use App\Models\Company;
|
||||
use App\Libraries\MultiDB;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class CompanyTokensRepository {
|
||||
|
||||
|
||||
private $company_key;
|
||||
private $store_key = "quickbooks-token";
|
||||
|
||||
public function __construct(string $key = null) {
|
||||
$this->company_key = $key ?? auth()->user->company()->company_key ?? null;
|
||||
$this->store_key .= $key;
|
||||
$this->setCompanyDbByKey();
|
||||
}
|
||||
|
||||
public function save(array $tokens) {
|
||||
$this->updateAccessToken($tokens['access_token'], $tokens['access_token_expires']);
|
||||
$this->updateRefreshToken($tokens['refresh_token'], $tokens['refresh_token_expires'], $tokens['realm']);
|
||||
}
|
||||
|
||||
|
||||
public function findByCompanyKey(): ?Company
|
||||
{
|
||||
return Company::where('company_key', $this->company_key)->first();
|
||||
}
|
||||
|
||||
public function setCompanyDbByKey()
|
||||
{
|
||||
MultiDB::findAndSetDbByCompanyKey($this->company_key);
|
||||
}
|
||||
|
||||
public function get() {
|
||||
return $this->getAccessToken() + $this->getRefreshToken();
|
||||
}
|
||||
|
||||
|
||||
protected function updateRefreshToken(string $token, string $expires, string $realm)
|
||||
{
|
||||
DB::table('companies')
|
||||
->where('company_key', $this->company_key)
|
||||
->update(['quickbooks_refresh_token' => $token,
|
||||
'quickbooks_realm_id' => $realm,
|
||||
'quickbooks_refresh_expires' => $expires ]);
|
||||
}
|
||||
|
||||
protected function updateAccessToken(string $token, string $expires )
|
||||
{
|
||||
|
||||
Cache::put([$this->store_key => $token], $expires);
|
||||
}
|
||||
|
||||
protected function getAccessToken( )
|
||||
{
|
||||
$result = Cache::get($this->store_key);
|
||||
|
||||
return $result ? ['access_token' => $result] : [];
|
||||
}
|
||||
|
||||
protected function getRefreshToken()
|
||||
{
|
||||
$result = (array) DB::table('companies')
|
||||
->select('quickbooks_refresh_token', 'quickbooks_realm_id')
|
||||
->where('company_key',$this->company_key)
|
||||
->where('quickbooks_refresh_expires','>',now())
|
||||
->first();
|
||||
|
||||
return $result? array_combine(['refresh_token','realm'], array_values($result) ) : [];
|
||||
}
|
||||
|
||||
}
|
@ -22,14 +22,7 @@ final class Service
|
||||
|
||||
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');
|
||||
return $this->getOAuth()->getAccessToken();
|
||||
}
|
||||
|
||||
public function getRefreshToken() : array
|
||||
|
2
composer.lock
generated
2
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "8fdb8245fbc563f8c09da161876f52a7",
|
||||
"content-hash": "f137c4c97faf7721366179f36c1ca72a",
|
||||
"packages": [
|
||||
{
|
||||
"name": "adrienrn/php-mimetyper",
|
||||
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('companies', function (Blueprint $table) {
|
||||
$table->string('quickbooks_realm_id')->nullable();
|
||||
$table->string('quickbooks_refresh_token')->nullable();
|
||||
$table->dateTime('quickbooks_refresh_expires')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('companies', function (Blueprint $table) {
|
||||
$table->dropColumn(['quickbooks_realm_id', 'quickbooks_refresh_token','quickbooks_refresh_expires']);
|
||||
});
|
||||
}
|
||||
};
|
@ -38,7 +38,7 @@ class ImportQuickbooksControllerTest extends TestCase
|
||||
|
||||
Session::start();
|
||||
|
||||
app()->singleton(QuickbooksInterface::class, fn() => new QuickbooksSDK($this->mock));
|
||||
//app()->singleton(QuickbooksInterface::class, fn() => new QuickbooksSDK($this->mock));
|
||||
}
|
||||
|
||||
public function testAuthorize(): void
|
||||
@ -88,47 +88,38 @@ class ImportQuickbooksControllerTest extends TestCase
|
||||
$this->mock->shouldHaveReceived('exchangeAuthorizationCodeForToken')->once()->with(123456,12345678);
|
||||
}
|
||||
|
||||
// public function testImport(): void
|
||||
// {
|
||||
public function testImport(): void
|
||||
{
|
||||
// Cache::spy();
|
||||
//Bus::fake();
|
||||
// $this->mock->shouldReceive('Query')->andReturnUsing(
|
||||
// function($val, $s = 1, $max = 1000) use ($count, $data) {
|
||||
// if(stristr($val, 'count')) {
|
||||
// return $count;
|
||||
// }
|
||||
$data = $this->setUpTestData('customers');
|
||||
$count = count($data);
|
||||
$this->mock->shouldReceive('Query')->andReturnUsing(
|
||||
function($val, $s = 1, $max = 1000) use ($count, $data) {
|
||||
if(stristr($val, 'count')) {
|
||||
return $count;
|
||||
}
|
||||
|
||||
// return Arr::take($data,$max);
|
||||
// }
|
||||
// );
|
||||
// $this->setUpTestData('customers');
|
||||
// // Perform the test
|
||||
// $response = $this->withHeaders([
|
||||
// 'X-API-TOKEN' => $this->token,
|
||||
// ])->post('/api/v1/import/quickbooks/preimport',[
|
||||
// 'import_type' => 'client'
|
||||
// ]);
|
||||
// $response->assertStatus(200);
|
||||
// $response = json_decode( $response->getContent());
|
||||
// $this->assertNotNull($response->hash);
|
||||
// $hash = $response->hash;
|
||||
// $response = $this->withHeaders([
|
||||
// 'X-API-TOKEN' => $this->token,
|
||||
// ])->post('/api/v1/import/quickbooks',[
|
||||
// 'import_type' => 'client',
|
||||
// 'hash' => $response->hash
|
||||
// ]);
|
||||
// $response->assertStatus(200);
|
||||
return Arr::take($data,$max);
|
||||
}
|
||||
);
|
||||
|
||||
// Perform the test
|
||||
$response = $this->actingAs($this->user)->withHeaders([
|
||||
'X-API-TOKEN' => $this->token,
|
||||
])->post('/api/v1/import/quickbooks',[
|
||||
'import_types' => ['client']
|
||||
]);
|
||||
$response->assertStatus(200);
|
||||
|
||||
//Cache::shouldHaveReceived('has')->once()->with("{$hash}-client");
|
||||
//Bus::assertDispatched(\App\Jobs\Import\QuickbooksIngest::class);
|
||||
// }
|
||||
}
|
||||
|
||||
protected function setUpTestData($file) {
|
||||
$data = json_decode(
|
||||
file_get_contents(base_path("tests/Mock/Quickbooks/Data/$file.json")),true
|
||||
);
|
||||
$count = count($data);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ class QuickbooksIngestTest extends TestCase
|
||||
'hash' => $hash,
|
||||
'column_map' => ['client' => ['mapping' => []]],
|
||||
'skip_header' => true,
|
||||
'import_type' => 'quickbooks',
|
||||
'import_types' => ['client'],
|
||||
], $this->company )->handle();
|
||||
$this->assertTrue(Client::withTrashed()->where(['company_id' => $this->company->id, 'name' => "Freeman Sporting Goods"])->exists());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user