Cleanup for QB Imports
This commit is contained in:
parent
014485820c
commit
49ca95117f
|
|
@ -2,9 +2,11 @@
|
||||||
|
|
||||||
namespace App\Import\Providers;
|
namespace App\Import\Providers;
|
||||||
|
|
||||||
|
use App\Models\Invoice;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use App\Services\Quickbooks\QuickbooksService;
|
use App\Services\Quickbooks\QuickbooksService;
|
||||||
use App\Services\Quickbooks\Transformers\ClientTransformer;
|
use App\Services\Quickbooks\Transformers\ClientTransformer;
|
||||||
|
use App\Services\Quickbooks\Transformers\PaymentTransformer;
|
||||||
|
|
||||||
class QBBackup extends BaseImport implements ImportInterface
|
class QBBackup extends BaseImport implements ImportInterface
|
||||||
{
|
{
|
||||||
|
|
@ -54,6 +56,40 @@ class QBBackup extends BaseImport implements ImportInterface
|
||||||
public function payment()
|
public function payment()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
$payments = isset($this->qb_data['payments']) && is_array($this->qb_data['payments']) ? $this->qb_data['payments'] : [];
|
||||||
|
|
||||||
|
foreach ($payments as $payment) {
|
||||||
|
|
||||||
|
$payment_transformer = new PaymentTransformer($this->company);
|
||||||
|
|
||||||
|
$transformed = $payment_transformer->qbToNinja($payment);
|
||||||
|
|
||||||
|
$ninja_payment = $payment_transformer->buildPayment($payment);
|
||||||
|
$ninja_payment->service()->applyNumber()->save();
|
||||||
|
|
||||||
|
|
||||||
|
$invoice = Invoice::query()
|
||||||
|
->withTrashed()
|
||||||
|
->where('company_id', $this->company->id)
|
||||||
|
->where('sync->qb_id', $payment['invoice_id'])
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($invoice) {
|
||||||
|
|
||||||
|
$paymentable = new \App\Models\Paymentable();
|
||||||
|
$paymentable->payment_id = $ninja_payment->id;
|
||||||
|
$paymentable->paymentable_id = $invoice->id;
|
||||||
|
$paymentable->paymentable_type = 'invoices';
|
||||||
|
$paymentable->amount = $transformed['applied'] + $ninja_payment->credits->sum('amount');
|
||||||
|
$paymentable->created_at = $ninja_payment->date; //@phpstan-ignore-line
|
||||||
|
$paymentable->save();
|
||||||
|
|
||||||
|
$invoice->service()->applyPayment($ninja_payment, $paymentable->amount);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function vendor()
|
public function vendor()
|
||||||
|
|
|
||||||
|
|
@ -56,8 +56,6 @@ class QbInvoice implements SyncInterface
|
||||||
|
|
||||||
$ninja_invoice_data = $this->invoice_transformer->qbToNinja($record);
|
$ninja_invoice_data = $this->invoice_transformer->qbToNinja($record);
|
||||||
|
|
||||||
nlog($ninja_invoice_data);
|
|
||||||
|
|
||||||
$payment_ids = $ninja_invoice_data['payment_ids'] ?? [];
|
$payment_ids = $ninja_invoice_data['payment_ids'] ?? [];
|
||||||
|
|
||||||
$client_id = $ninja_invoice_data['client_id'] ?? null;
|
$client_id = $ninja_invoice_data['client_id'] ?? null;
|
||||||
|
|
@ -201,13 +199,35 @@ class QbInvoice implements SyncInterface
|
||||||
if ($invoice->id) {
|
if ($invoice->id) {
|
||||||
$this->qbInvoiceUpdate($ninja_invoice_data, $invoice);
|
$this->qbInvoiceUpdate($ninja_invoice_data, $invoice);
|
||||||
}
|
}
|
||||||
nlog($ninja_invoice_data);
|
|
||||||
//new invoice scaffold
|
//new invoice scaffold
|
||||||
$invoice->fill($ninja_invoice_data);
|
$invoice->fill($ninja_invoice_data);
|
||||||
$invoice->saveQuietly();
|
$invoice->saveQuietly();
|
||||||
|
|
||||||
$invoice = $invoice->calc()->getInvoice()->service()->markSent()->applyNumber()->createInvitations()->save();
|
$invoice = $invoice->calc()->getInvoice()->service()->markSent()->applyNumber()->createInvitations()->save();
|
||||||
|
|
||||||
|
foreach ($payment_ids as $payment_id) {
|
||||||
|
|
||||||
|
$payment = $this->service->sdk->FindById('Payment', $payment_id);
|
||||||
|
|
||||||
|
$payment_transformer = new PaymentTransformer($this->service->company);
|
||||||
|
|
||||||
|
$transformed = $payment_transformer->qbToNinja($payment);
|
||||||
|
|
||||||
|
$ninja_payment = $payment_transformer->buildPayment($payment);
|
||||||
|
$ninja_payment->service()->applyNumber()->save();
|
||||||
|
|
||||||
|
$paymentable = new \App\Models\Paymentable();
|
||||||
|
$paymentable->payment_id = $ninja_payment->id;
|
||||||
|
$paymentable->paymentable_id = $invoice->id;
|
||||||
|
$paymentable->paymentable_type = 'invoices';
|
||||||
|
$paymentable->amount = $transformed['applied'] + $ninja_payment->credits->sum('amount');
|
||||||
|
$paymentable->created_at = $ninja_payment->date; //@phpstan-ignore-line
|
||||||
|
$paymentable->save();
|
||||||
|
|
||||||
|
$invoice->service()->applyPayment($ninja_payment, $paymentable->amount);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if ($record instanceof \QuickBooksOnline\API\Data\IPPSalesReceipt) {
|
if ($record instanceof \QuickBooksOnline\API\Data\IPPSalesReceipt) {
|
||||||
$invoice->service()->markPaid()->save();
|
$invoice->service()->markPaid()->save();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,84 @@
|
||||||
|
<?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\Quickbooks\Models;
|
||||||
|
|
||||||
|
use App\Models\Client;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use App\DataMapper\ClientSync;
|
||||||
|
use App\Factory\ClientFactory;
|
||||||
|
use App\Interfaces\SyncInterface;
|
||||||
|
use App\Factory\ClientContactFactory;
|
||||||
|
use App\Services\Quickbooks\QuickbooksService;
|
||||||
|
use App\Services\Quickbooks\Transformers\ClientTransformer;
|
||||||
|
use App\Services\Quickbooks\Transformers\PaymentTransformer;
|
||||||
|
|
||||||
|
class QbPayment implements SyncInterface
|
||||||
|
{
|
||||||
|
public function __construct(public QuickbooksService $service)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function find(string $id): mixed
|
||||||
|
{
|
||||||
|
return $this->service->sdk->FindById('Payment', $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function importToNinja(array $records): void
|
||||||
|
{
|
||||||
|
|
||||||
|
foreach ($records as $payment) {
|
||||||
|
|
||||||
|
$payment_transformer = new PaymentTransformer($this->service->company);
|
||||||
|
|
||||||
|
$transformed = $payment_transformer->qbToNinja($payment);
|
||||||
|
|
||||||
|
$ninja_payment = $payment_transformer->buildPayment($payment);
|
||||||
|
$ninja_payment->service()->applyNumber()->save();
|
||||||
|
|
||||||
|
$invoice = Invoice::query()
|
||||||
|
->withTrashed()
|
||||||
|
->where('company_id', $this->service->company->id)
|
||||||
|
->where('sync->qb_id', $payment['invoice_id'])
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($invoice) {
|
||||||
|
|
||||||
|
$paymentable = new \App\Models\Paymentable();
|
||||||
|
$paymentable->payment_id = $ninja_payment->id;
|
||||||
|
$paymentable->paymentable_id = $invoice->id;
|
||||||
|
$paymentable->paymentable_type = 'invoices';
|
||||||
|
$paymentable->amount = $transformed['applied'] + $ninja_payment->credits->sum('amount');
|
||||||
|
$paymentable->created_at = $ninja_payment->date; //@phpstan-ignore-line
|
||||||
|
$paymentable->save();
|
||||||
|
|
||||||
|
$invoice->service()->applyPayment($ninja_payment, $paymentable->amount);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function syncToNinja(array $records): void
|
||||||
|
{
|
||||||
|
$transformer = new PaymentTransformer($this->service->company);
|
||||||
|
|
||||||
|
foreach ($records as $record) {
|
||||||
|
$ninja_data = $transformer->qbToNinja($record);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function syncToForeign(array $records): void
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,12 +20,13 @@ use App\Factory\InvoiceFactory;
|
||||||
use App\Factory\ProductFactory;
|
use App\Factory\ProductFactory;
|
||||||
use App\DataMapper\QuickbooksSync;
|
use App\DataMapper\QuickbooksSync;
|
||||||
use App\Factory\ClientContactFactory;
|
use App\Factory\ClientContactFactory;
|
||||||
|
use App\Services\Quickbooks\Models\QbClient;
|
||||||
use QuickBooksOnline\API\Core\CoreConstants;
|
use QuickBooksOnline\API\Core\CoreConstants;
|
||||||
use App\Services\Quickbooks\Models\QbInvoice;
|
use App\Services\Quickbooks\Models\QbInvoice;
|
||||||
|
use App\Services\Quickbooks\Models\QbPayment;
|
||||||
use App\Services\Quickbooks\Models\QbProduct;
|
use App\Services\Quickbooks\Models\QbProduct;
|
||||||
use QuickBooksOnline\API\DataService\DataService;
|
use QuickBooksOnline\API\DataService\DataService;
|
||||||
use App\Services\Quickbooks\Jobs\QuickbooksImport;
|
use App\Services\Quickbooks\Jobs\QuickbooksImport;
|
||||||
use App\Services\Quickbooks\Models\QbClient;
|
|
||||||
use App\Services\Quickbooks\Transformers\ClientTransformer;
|
use App\Services\Quickbooks\Transformers\ClientTransformer;
|
||||||
use App\Services\Quickbooks\Transformers\InvoiceTransformer;
|
use App\Services\Quickbooks\Transformers\InvoiceTransformer;
|
||||||
use App\Services\Quickbooks\Transformers\PaymentTransformer;
|
use App\Services\Quickbooks\Transformers\PaymentTransformer;
|
||||||
|
|
@ -41,6 +42,8 @@ class QuickbooksService
|
||||||
|
|
||||||
public QbClient $client;
|
public QbClient $client;
|
||||||
|
|
||||||
|
public QbPayment $payment;
|
||||||
|
|
||||||
public QuickbooksSync $settings;
|
public QuickbooksSync $settings;
|
||||||
|
|
||||||
private bool $testMode = true;
|
private bool $testMode = true;
|
||||||
|
|
@ -82,6 +85,8 @@ class QuickbooksService
|
||||||
|
|
||||||
$this->client = new QbClient($this);
|
$this->client = new QbClient($this);
|
||||||
|
|
||||||
|
$this->payment = new QbPayment($this);
|
||||||
|
|
||||||
$this->settings = $this->company->quickbooks->settings;
|
$this->settings = $this->company->quickbooks->settings;
|
||||||
|
|
||||||
// $this->checkDefaultAccounts(); // disabled, because if OAuth not present, we don't have access to the accounts.
|
// $this->checkDefaultAccounts(); // disabled, because if OAuth not present, we don't have access to the accounts.
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ use ReflectionClass;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
|
use App\Models\Payment;
|
||||||
use App\Models\Product;
|
use App\Models\Product;
|
||||||
use Tests\MockAccountData;
|
use Tests\MockAccountData;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
@ -27,6 +28,7 @@ use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||||
use QuickBooksOnline\API\Facades\Invoice as QbInvoice;
|
use QuickBooksOnline\API\Facades\Invoice as QbInvoice;
|
||||||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
use App\Services\Quickbooks\Transformers\ClientTransformer;
|
use App\Services\Quickbooks\Transformers\ClientTransformer;
|
||||||
|
|
||||||
class QuickbooksMappingTest extends TestCase
|
class QuickbooksMappingTest extends TestCase
|
||||||
{
|
{
|
||||||
use MockAccountData;
|
use MockAccountData;
|
||||||
|
|
@ -77,6 +79,14 @@ class QuickbooksMappingTest extends TestCase
|
||||||
|
|
||||||
$this->assertGreaterThan($pre_count, $post_count);
|
$this->assertGreaterThan($pre_count, $post_count);
|
||||||
|
|
||||||
|
$pre_count = Payment::where('company_id', $this->company->id)->count();
|
||||||
|
|
||||||
|
$this->assertGreaterThan(0, count($this->qb_data['payments']));
|
||||||
|
|
||||||
|
$qb->payment->importToNinja($this->qb_data['payments']);
|
||||||
|
$post_count = Payment::where('company_id', $this->company->id)->count();
|
||||||
|
|
||||||
|
$this->assertGreaterThan($pre_count, $post_count);
|
||||||
|
|
||||||
Client::where('company_id', $this->company->id)->cursor()->each(function ($client) {
|
Client::where('company_id', $this->company->id)->cursor()->each(function ($client) {
|
||||||
$client->forceDelete();
|
$client->forceDelete();
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue