Fixes for quickbooks creation

This commit is contained in:
David Bomba 2024-09-25 13:20:05 +10:00
parent 4dde0cfbb6
commit 2c188b026d
2 changed files with 119 additions and 71 deletions

View File

@ -2,24 +2,25 @@
namespace Tests\Feature\Import\Quickbooks; namespace Tests\Feature\Import\Quickbooks;
use Tests\TestCase;
use App\Import\Providers\Quickbooks;
use App\Import\Transformer\BaseTransformer;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Tests\MockAccountData;
use Illuminate\Support\Facades\Cache;
use Mockery; use Mockery;
use Tests\TestCase;
use ReflectionClass;
use App\Models\Client; use App\Models\Client;
use App\Models\Company; use App\Models\Company;
use App\Models\Product;
use App\Models\Invoice; use App\Models\Invoice;
use App\Services\Quickbooks\QuickbooksService; use App\Models\Product;
use Tests\MockAccountData;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use ReflectionClass; use App\DataMapper\ClientSync;
use App\Utils\Traits\MakesHash;
use App\Import\Providers\Quickbooks;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use App\Import\Transformer\BaseTransformer;
use App\Services\Quickbooks\QuickbooksService;
use Illuminate\Routing\Middleware\ThrottleRequests;
use QuickBooksOnline\API\Facades\Invoice as QbInvoice; use QuickBooksOnline\API\Facades\Invoice as QbInvoice;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class QuickbooksTest extends TestCase class QuickbooksTest extends TestCase
{ {
@ -29,6 +30,7 @@ class QuickbooksTest extends TestCase
protected $quickbooks; protected $quickbooks;
protected $data; protected $data;
protected QuickbooksService $qb;
protected function setUp(): void protected function setUp(): void
{ {
@ -45,12 +47,9 @@ class QuickbooksTest extends TestCase
$this->makeTestData(); $this->makeTestData();
} }
public function testCreateCustomerInQb() public function createQbCustomer()
{ {
$c = Company::whereNotNull('quickbooks')->first();
$qb = new QuickbooksService($c);
$customerData = [ $customerData = [
"DisplayName" => $this->client->present()->name(), // Required and must be unique "DisplayName" => $this->client->present()->name(), // Required and must be unique
"PrimaryEmailAddr" => [ "PrimaryEmailAddr" => [
@ -85,79 +84,127 @@ class QuickbooksTest extends TestCase
"WebAddr" => $this->client->website ?? '', "WebAddr" => $this->client->website ?? '',
]; ];
$customer = \QuickBooksOnline\API\Facades\Customer::create($customerData); $customer = \QuickBooksOnline\API\Facades\Customer::create($customerData);
// $customer = $qb->sdk->createCustomer($customerData);
$this->assertNotNull($customer);
nlog($customer);
// Send the create request to QuickBooks // Send the create request to QuickBooks
$resultingCustomerObj = $qb->sdk->Add($customer); $resultingCustomerObj = $this->qb->sdk->Add($customer);
return $resultingCustomerObj;
}
public function testCreateInvoiceInQb()
{
$this->company = Company::whereNotNull('quickbooks')->first();
$this->qb = new QuickbooksService($this->company);
$customer = $this->createQbCustomer();
//create ninja invoice
$qb_invoice = $this->createQbInvoice($customer);
$this->assertNotNull($qb_invoice);
nlog($qb_invoice);
}
public function testCreateCustomerInQb()
{
$this->company = Company::whereNotNull('quickbooks')->first();
$this->qb = new QuickbooksService($this->company);
$resultingCustomerObj = $this->createQbCustomer();
// Check for errors // Check for errors
$error = $qb->sdk->getLastError(); $error = $this->qb->sdk->getLastError();
if ($error) { if ($error) {
$this->fail("The Customer could not be created: " . $error->getResponseBody()); $this->fail("The Customer could not be created: " . $error->getResponseBody());
} }
nlog($resultingCustomerObj); $qb_id = data_get($resultingCustomerObj, 'Id.value');
$this->assertNotNull($qb_id);
$sync = new ClientSync();
$sync->qb_id = $qb_id;
$this->client->sync = $sync;
$this->client->save();
$c = $this->client->fresh();
$this->assertEquals($qb_id, $c->sync->qb_id);
} }
public function createQbInvoice($customer)
// public function testCreateInvoiceInQb()
// {
// $c = Company::whereNotNull('quickbooks')->first();
// $qb = new QuickbooksService($c);
// //create QB customer
// //create ninja invoice
// //create QB invoice
// }
public function stubData()
{ {
// Inside the create method $line_items = [];
$lineItems = [ $line_num = 1;
[
"Amount" => 100.00, foreach($this->invoice->line_items as $line_item)
"DetailType" => "SalesItemLineDetail", {
"SalesItemLineDetail" => [
"ItemRef" => [ $line_items[] = [
"value" => "1", // Replace with actual Item ID from QuickBooks 'LineNum' => $line_num, // Add the line number
"name" => "Services" 'DetailType' => 'SalesItemLineDetail',
] 'SalesItemLineDetail' => [
] 'ItemRef' => [
] 'value' => $line_item->product_key,
],
'Qty' => $line_item->quantity,
'UnitPrice' => $line_item->cost,
],
'Description' => $line_item->notes,
// 'Type' => $this->getQuickBooksItemType($line_item),
// 'PurchaseCost' => $line_item->product_cost,
// 'Taxable' => $line_item->isTaxable(),
'Amount' => $line_item->line_total,
]; ];
$invoiceData = [ $line_num++;
"Line" => $lineItems,
"CustomerRef" => [
"value" => "1", // Replace with actual Customer ID from QuickBooks
],
"BillEmail" => [
"Address" => "customer@example.com"
],
"DueDate" => "2023-12-31",
"TotalAmt" => 100.00,
"DocNumber" => "INV-001"
];
$invoice = QbInvoice::create($invoiceData); }
$invoiceData = [
"Line" => $line_items,
"CustomerRef" => [
"value" => data_get($customer, 'Id.value')
],
"BillEmail" => [
"Address" => $this->client->present()->email()
],
"DueDate" => $this->invoice->due_date,
"TotalAmt" => $this->invoice->amount,
"DocNumber" => $this->invoice->number,
// "Note" => $this->invoice->public_notes,
];
$invoice = \QuickBooksOnline\API\Facades\Invoice::create($invoiceData);
nlog($invoice);
return $this->qb->sdk->Add($invoice);
} }
private function getQuickBooksItemType($line_item): string
{
$typeMap = [
'1' => 'Inventory', // product
'2' => 'Service', // service
'3' => 'NonInventory', // unpaid gateway fee
'4' => 'NonInventory', // paid gateway fee
'5' => 'Service', // late fee
'6' => 'NonInventory', // expense
];
return $typeMap[$line_item->type_id] ?? 'Service';
}
} }

View File

@ -840,6 +840,7 @@ trait MockAccountData
$item = InvoiceItemFactory::create(); $item = InvoiceItemFactory::create();
$item->quantity = 1; $item->quantity = 1;
$item->notes = $this->faker->sentence;
$item->cost = 10; $item->cost = 10;
$item->task_id = $this->encodePrimaryKey($this->task->id); $item->task_id = $this->encodePrimaryKey($this->task->id);
$item->expense_id = $this->encodePrimaryKey($this->expense->id); $item->expense_id = $this->encodePrimaryKey($this->expense->id);