-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate Accounting Platforms (QuickBooks and X
- Loading branch information
1 parent
605f42c
commit 3d98b45
Showing
7 changed files
with
332 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\AccountingIntegration; | ||
use App\Services\AccountingService; | ||
use Illuminate\Http\Request; | ||
|
||
class AccountingIntegrationController extends Controller | ||
{ | ||
protected $accountingService; | ||
|
||
public function __construct(AccountingService $accountingService) | ||
{ | ||
$this->accountingService = $accountingService; | ||
} | ||
|
||
public function connect(Request $request) | ||
{ | ||
$validated = $request->validate([ | ||
'platform' => 'required|in:quickbooks,xero', | ||
'credentials' => 'required|array', | ||
]); | ||
|
||
try { | ||
$connection = $this->accountingService->connectPlatform($validated['platform'], $validated['credentials']); | ||
|
||
AccountingIntegration::create([ | ||
'user_id' => auth()->id(), | ||
'platform' => $validated['platform'], | ||
'connection_details' => $connection, | ||
]); | ||
|
||
return response()->json(['message' => 'Successfully connected to ' . $validated['platform']]); | ||
} catch (\Exception $e) { | ||
return response()->json(['error' => $e->getMessage()], 400); | ||
} | ||
} | ||
|
||
public function disconnect(AccountingIntegration $integration) | ||
{ | ||
$integration->delete(); | ||
return response()->json(['message' => 'Successfully disconnected from ' . $integration->platform]); | ||
} | ||
|
||
public function status(AccountingIntegration $integration) | ||
{ | ||
return response()->json([ | ||
'platform' => $integration->platform, | ||
'connected' => true, | ||
'last_synced' => $integration->last_synced, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\AccountingIntegration; | ||
use App\Exceptions\AccountingIntegrationException; | ||
|
||
class AccountingService | ||
{ | ||
protected $quickbooksService; | ||
protected $xeroService; | ||
|
||
public function __construct(QuickBooksService $quickbooksService, XeroService $xeroService) | ||
{ | ||
$this->quickbooksService = $quickbooksService; | ||
$this->xeroService = $xeroService; | ||
} | ||
|
||
public function syncInvoice(AccountingIntegration $integration, $invoice) | ||
{ | ||
switch ($integration->platform) { | ||
case 'quickbooks': | ||
return $this->quickbooksService->syncInvoice($integration, $invoice); | ||
case 'xero': | ||
return $this->xeroService->syncInvoice($integration, $invoice); | ||
default: | ||
throw new AccountingIntegrationException("Unsupported accounting platform"); | ||
} | ||
} | ||
|
||
public function syncPayment(AccountingIntegration $integration, $payment) | ||
{ | ||
switch ($integration->platform) { | ||
case 'quickbooks': | ||
return $this->quickbooksService->syncPayment($integration, $payment); | ||
case 'xero': | ||
return $this->xeroService->syncPayment($integration, $payment); | ||
default: | ||
throw new AccountingIntegrationException("Unsupported accounting platform"); | ||
} | ||
} | ||
|
||
public function connectPlatform(string $platform, array $credentials) | ||
{ | ||
switch ($platform) { | ||
case 'quickbooks': | ||
return $this->quickbooksService->connect($credentials); | ||
case 'xero': | ||
return $this->xeroService->connect($credentials); | ||
default: | ||
throw new AccountingIntegrationException("Unsupported accounting platform"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
database/migrations/[timestamp]_create_accounting_integrations_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateAccountingIntegrationsTable extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::create('accounting_integrations', function (Blueprint $table) { | ||
$table->id(); | ||
$table->foreignId('user_id')->constrained()->onDelete('cascade'); | ||
$table->string('platform'); | ||
$table->json('connection_details'); | ||
$table->timestamp('last_synced')->nullable(); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists('accounting_integrations'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\User; | ||
use App\Models\AccountingIntegration; | ||
use App\Services\AccountingService; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use Tests\TestCase; | ||
use Mockery; | ||
|
||
class AccountingIntegrationTest extends TestCase | ||
{ | ||
use RefreshDatabase, WithFaker; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->mockAccountingService = Mockery::mock(AccountingService::class); | ||
$this->app->instance(AccountingService::class, $this->mockAccountingService); | ||
} | ||
|
||
public function testSuccessfulConnectionToQuickBooks() | ||
{ | ||
$user = User::factory()->create(); | ||
$this->actingAs($user); | ||
|
||
$this->mockAccountingService->shouldReceive('connectPlatform') | ||
->once() | ||
->with('quickbooks', Mockery::any()) | ||
->andReturn(['access_token' => 'fake_token']); | ||
|
||
$response = $this->postJson('/api/accounting/connect', [ | ||
'platform' => 'quickbooks', | ||
'credentials' => ['code' => 'fake_code'], | ||
]); | ||
|
||
$response->assertStatus(200); | ||
$this->assertDatabaseHas('accounting_integrations', [ | ||
'user_id' => $user->id, | ||
'platform' => 'quickbooks', | ||
]); | ||
} | ||
|
||
public function testInvoiceSyncingFromCRMToAccountingPlatform() | ||
{ | ||
$user = User::factory()->create(); | ||
$integration = AccountingIntegration::factory()->create(['user_id' => $user->id]); | ||
$transaction = Transaction::factory()->create(['user_id' => $user->id]); | ||
|
||
$this->mockAccountingService->shouldReceive('syncInvoice') | ||
->once() | ||
->with($integration, $transaction) | ||
->andReturn(true); | ||
|
||
|
||
$response = $this->actingAs($user)->postJson("/api/transactions/{$transaction->id}/sync-invoice"); | ||
|
||
$response->assertStatus(200); | ||
$response->assertJson(['message' => 'Invoice synced successfully']); | ||
} | ||
|
||
public function testPaymentDataSyncingFromAccountingPlatformToCRM() | ||
{ | ||
$user = User::factory()->create(); | ||
$integration = AccountingIntegration::factory()->create(['user_id' => $user->id]); | ||
$transaction = Transaction::factory()->create(['user_id' => $user->id]); | ||
|
||
$this->mockAccountingService->shouldReceive('syncPayment') | ||
->once() | ||
->with($integration, $transaction) | ||
->andReturn(true); | ||
|
||
$response = $this->actingAs($user)->postJson("/api/transactions/{$transaction->id}/sync-payment"); | ||
|
||
$response->assertStatus(200); | ||
$response->assertJson(['message' => 'Payment synced successfully']); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Mockery::close(); | ||
parent::tearDown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\User; | ||
use App\Models\Transaction; | ||
use App\Models\AccountingIntegration; | ||
use App\Services\AccountingService; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Tests\TestCase; | ||
use Mockery; | ||
|
||
class TransactionTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->mockAccountingService = Mockery::mock(AccountingService::class); | ||
$this->app->instance(AccountingService::class, $this->mockAccountingService); | ||
} | ||
|
||
public function testTransactionCreationWithAccountingPlatformSync() | ||
{ | ||
$user = User::factory()->create(); | ||
$integration = AccountingIntegration::factory()->create(['user_id' => $user->id]); | ||
|
||
$this->mockAccountingService->shouldReceive('syncInvoice') | ||
->once() | ||
->andReturn(true); | ||
|
||
$response = $this->actingAs($user)->postJson('/api/transactions', [ | ||
'amount' => 1000, | ||
'description' => 'Test transaction', | ||
'accounting_integration_id' => $integration->id, | ||
]); | ||
|
||
$response->assertStatus(201); | ||
$this->assertDatabaseHas('transactions', [ | ||
'amount' => 1000, | ||
'description' => 'Test transaction', | ||
]); | ||
} | ||
|
||
public function testUpdatingTransactionStatusWithAccountingPlatformSync() | ||
{ | ||
$user = User::factory()->create(); | ||
$integration = AccountingIntegration::factory()->create(['user_id' => $user->id]); | ||
$transaction = Transaction::factory()->create([ | ||
'user_id' => $user->id, | ||
'accounting_integration_id' => $integration->id, | ||
]); | ||
|
||
$this->mockAccountingService->shouldReceive('syncPayment') | ||
->once() | ||
->andReturn(true); | ||
|
||
$response = $this->actingAs($user)->patchJson("/api/transactions/{$transaction->id}", [ | ||
'status' => 'paid', | ||
]); | ||
|
||
$response->assertStatus(200); | ||
$this->assertDatabaseHas('transactions', [ | ||
'id' => $transaction->id, | ||
'status' => 'paid', | ||
]); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
Mockery::close(); | ||
parent::tearDown(); | ||
} | ||
} |