2024-07-23 02:02:13 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2024-08-21 07:07:52 +02:00
|
|
|
use App\Http\Requests\Quickbooks\AuthorizedQuickbooksRequest;
|
2024-08-02 04:32:20 +02:00
|
|
|
use \Closure;
|
2024-07-23 02:02:13 +02:00
|
|
|
use App\Utils\Ninja;
|
2024-08-02 04:32:20 +02:00
|
|
|
use App\Models\Company;
|
2024-08-21 05:04:09 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2024-07-23 02:02:13 +02:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\Jobs\Import\QuickbooksIngest;
|
2024-08-21 05:04:09 +02:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-08-02 04:32:20 +02:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2024-08-21 05:04:09 +02:00
|
|
|
use App\Http\Requests\Quickbooks\AuthQuickbooksRequest;
|
|
|
|
use App\Services\Import\Quickbooks\QuickbooksService;
|
2024-07-23 02:02:13 +02:00
|
|
|
|
|
|
|
class ImportQuickbooksController extends BaseController
|
|
|
|
{
|
2024-08-21 05:04:09 +02:00
|
|
|
|
2024-08-21 07:07:52 +02:00
|
|
|
private array $import_entities = [
|
2024-07-30 20:19:15 +02:00
|
|
|
'client' => 'Customer',
|
|
|
|
'invoice' => 'Invoice',
|
|
|
|
'product' => 'Item',
|
|
|
|
'payment' => 'Payment'
|
|
|
|
];
|
|
|
|
|
2024-08-21 07:07:52 +02:00
|
|
|
public function onAuthorized(AuthorizedQuickbooksRequest $request)
|
2024-08-08 18:53:45 +02:00
|
|
|
{
|
2024-08-21 07:07:52 +02:00
|
|
|
|
|
|
|
MultiDB::findAndSetDbByCompanyKey($request->getTokenContent()['company_key']);
|
|
|
|
$company = $request->getCompany();
|
|
|
|
$qb = new QuickbooksService($company);
|
|
|
|
|
2024-08-08 18:53:45 +02:00
|
|
|
$realm = $request->query('realmId');
|
2024-08-21 07:07:52 +02:00
|
|
|
$access_token_object = $qb->getAuth()->accessToken($request->query('code'), $realm);
|
2024-08-21 08:49:48 +02:00
|
|
|
nlog($access_token_object); //OAuth2AccessToken
|
2024-08-21 07:07:52 +02:00
|
|
|
$company->quickbooks = $access_token_object;
|
|
|
|
$company->save();
|
2024-08-08 18:53:45 +02:00
|
|
|
|
2024-08-21 08:49:48 +02:00
|
|
|
return response()->json(['message' => 'Success'], 200); //todo swapout for redirect to UI
|
2024-08-08 18:53:45 +02:00
|
|
|
}
|
2024-07-30 20:19:15 +02:00
|
|
|
|
2024-07-23 02:02:13 +02:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-08-21 05:04:09 +02:00
|
|
|
public function authorizeQuickbooks(AuthQuickbooksRequest $request, string $token)
|
2024-07-23 02:02:13 +02:00
|
|
|
{
|
2024-08-21 05:04:09 +02:00
|
|
|
|
|
|
|
MultiDB::findAndSetDbByCompanyKey($request->getTokenContent()['company_key']);
|
|
|
|
$company = $request->getCompany();
|
|
|
|
$qb = new QuickbooksService($company);
|
|
|
|
|
|
|
|
$authorizationUrl = $qb->getAuth()->getAuthorizationUrl();
|
|
|
|
$state = $qb->getAuth()->getState();
|
2024-08-02 04:32:20 +02:00
|
|
|
|
2024-08-08 18:53:45 +02:00
|
|
|
Cache::put($state, $token, 190);
|
2024-07-23 02:02:13 +02:00
|
|
|
|
2024-08-02 04:32:20 +02:00
|
|
|
return redirect()->to($authorizationUrl);
|
2024-07-23 02:02:13 +02:00
|
|
|
}
|
|
|
|
|
2024-08-08 18:53:45 +02:00
|
|
|
public function preimport(string $type, string $hash)
|
2024-07-23 02:02:13 +02:00
|
|
|
{
|
|
|
|
// Check for authorization otherwise
|
|
|
|
// Create a reference
|
|
|
|
$data = [
|
2024-07-30 20:19:15 +02:00
|
|
|
'hash' => $hash,
|
2024-08-08 18:53:45 +02:00
|
|
|
'type' => $type
|
2024-07-23 02:02:13 +02:00
|
|
|
];
|
2024-07-30 20:19:15 +02:00
|
|
|
$this->getData($data);
|
2024-07-23 02:02:13 +02:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:19:15 +02:00
|
|
|
protected function getData($data) {
|
2024-07-23 02:02:13 +02:00
|
|
|
|
2024-07-30 20:19:15 +02:00
|
|
|
$entity = $this->import_entities[$data['type']];
|
|
|
|
$cache_name = "{$data['hash']}-{$data['type']}";
|
|
|
|
// TODO: Get or put cache or DB?
|
2024-08-08 18:53:45 +02:00
|
|
|
if(! Cache::has($cache_name) )
|
2024-07-30 20:19:15 +02:00
|
|
|
{
|
2024-08-08 18:53:45 +02:00
|
|
|
$contents = call_user_func([$this->service, "fetch{$entity}s"]);
|
|
|
|
if($contents->isEmpty()) return;
|
|
|
|
|
2024-07-30 20:19:15 +02:00
|
|
|
Cache::put($cache_name, base64_encode( $contents->toJson()), 600);
|
|
|
|
}
|
2024-07-23 02:02:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/import_json",
|
|
|
|
* operationId="getImportJson",
|
|
|
|
* tags={"import"},
|
|
|
|
* summary="Import data from the system",
|
|
|
|
* description="Import data from the system",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="success",
|
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function import(Request $request)
|
|
|
|
{
|
2024-08-08 18:53:45 +02:00
|
|
|
$hash = Str::random(32);
|
|
|
|
foreach($request->input('import_types') as $type)
|
|
|
|
{
|
|
|
|
$this->preimport($type, $hash);
|
|
|
|
}
|
2024-07-23 02:02:13 +02:00
|
|
|
/** @var \App\Models\User $user */
|
2024-08-21 05:04:09 +02:00
|
|
|
// $user = auth()->user() ?? Auth::loginUsingId(60);
|
2024-08-08 18:53:45 +02:00
|
|
|
$data = ['import_types' => $request->input('import_types') ] + compact('hash');
|
2024-07-23 02:02:13 +02:00
|
|
|
if (Ninja::isHosted()) {
|
2024-08-08 18:53:45 +02:00
|
|
|
QuickbooksIngest::dispatch( $data , $user->company() );
|
2024-07-23 02:02:13 +02:00
|
|
|
} else {
|
2024-08-08 18:53:45 +02:00
|
|
|
QuickbooksIngest::dispatch($data, $user->company() );
|
2024-07-23 02:02:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(['message' => 'Processing'], 200);
|
|
|
|
}
|
|
|
|
}
|