mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Invoice 2 Go tests
This commit is contained in:
parent
4cc56c3995
commit
20758256c9
@ -123,6 +123,9 @@ class BaseImport
|
|||||||
|
|
||||||
private function groupInvoices($csvData, $key)
|
private function groupInvoices($csvData, $key)
|
||||||
{
|
{
|
||||||
|
if(!$key)
|
||||||
|
return $csvData;
|
||||||
|
|
||||||
// Group by invoice.
|
// Group by invoice.
|
||||||
$grouped = [];
|
$grouped = [];
|
||||||
|
|
||||||
@ -215,6 +218,7 @@ class BaseImport
|
|||||||
foreach ($invoices as $raw_invoice) {
|
foreach ($invoices as $raw_invoice) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
$invoice_data = $invoice_transformer->transform($raw_invoice);
|
$invoice_data = $invoice_transformer->transform($raw_invoice);
|
||||||
|
|
||||||
$invoice_data['line_items'] = $this->cleanItems(
|
$invoice_data['line_items'] = $this->cleanItems(
|
||||||
@ -242,10 +246,6 @@ class BaseImport
|
|||||||
unset($invoice_data['client']);
|
unset($invoice_data['client']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// $validator = Validator::make(
|
|
||||||
// $invoice_data,
|
|
||||||
// (new StoreInvoiceRequest())->rules()
|
|
||||||
// );
|
|
||||||
$validator = $this->request_name::runFormRequest($invoice_data);
|
$validator = $this->request_name::runFormRequest($invoice_data);
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
@ -516,8 +516,9 @@ class BaseImport
|
|||||||
|
|
||||||
public function preTransform(array $data, $entity_type)
|
public function preTransform(array $data, $entity_type)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (empty($this->column_map[$entity_type])) {
|
if (empty($this->column_map[$entity_type])) {
|
||||||
return false;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->skip_header) {
|
if ($this->skip_header) {
|
||||||
|
@ -49,7 +49,6 @@ class Freshbooks extends BaseImport
|
|||||||
$entity_type = 'client';
|
$entity_type = 'client';
|
||||||
|
|
||||||
$data = $this->getCsvData($entity_type);
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
$data = $this->preTransform($data, $entity_type);
|
$data = $this->preTransform($data, $entity_type);
|
||||||
|
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
|
@ -10,7 +10,75 @@
|
|||||||
*/
|
*/
|
||||||
namespace App\Import\Providers;
|
namespace App\Import\Providers;
|
||||||
|
|
||||||
|
use App\Factory\ClientFactory;
|
||||||
|
use App\Factory\InvoiceFactory;
|
||||||
|
use App\Http\Requests\Client\StoreClientRequest;
|
||||||
|
use App\Http\Requests\Invoice\StoreInvoiceRequest;
|
||||||
|
use App\Import\Transformer\Invoice2Go\InvoiceTransformer;
|
||||||
|
use App\Repositories\ClientRepository;
|
||||||
|
use App\Repositories\InvoiceRepository;
|
||||||
|
|
||||||
class Invoice2Go extends BaseImport
|
class Invoice2Go extends BaseImport
|
||||||
{
|
{
|
||||||
|
public array $entity_count = [];
|
||||||
|
|
||||||
|
public function import(string $entity)
|
||||||
|
{
|
||||||
|
if (
|
||||||
|
in_array($entity, [
|
||||||
|
//'client',
|
||||||
|
'invoice',
|
||||||
|
// 'product',
|
||||||
|
// 'payment',
|
||||||
|
// 'vendor',
|
||||||
|
// 'expense',
|
||||||
|
])
|
||||||
|
) {
|
||||||
|
$this->{$entity}();
|
||||||
|
}
|
||||||
|
|
||||||
|
//collate any errors
|
||||||
|
|
||||||
|
$this->finalizeImport();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function invoice() {
|
||||||
|
|
||||||
|
//make sure we update and create products with wave
|
||||||
|
$initial_update_products_value = $this->company->update_products;
|
||||||
|
$this->company->update_products = true;
|
||||||
|
|
||||||
|
$this->company->save();
|
||||||
|
|
||||||
|
$entity_type = 'invoice';
|
||||||
|
|
||||||
|
$data = $this->getCsvData($entity_type);
|
||||||
|
|
||||||
|
$data = $this->preTransform($data, 'invoice');
|
||||||
|
|
||||||
|
if (empty($data)) {
|
||||||
|
$this->entity_count['invoices'] = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->request_name = StoreInvoiceRequest::class;
|
||||||
|
$this->repository_name = InvoiceRepository::class;
|
||||||
|
$this->factory_name = InvoiceFactory::class;
|
||||||
|
|
||||||
|
$this->repository = app()->make($this->repository_name);
|
||||||
|
$this->repository->import_mode = true;
|
||||||
|
|
||||||
|
$this->transformer = new InvoiceTransformer($this->company);
|
||||||
|
|
||||||
|
$invoice_count = $this->ingestInvoices($data, false);
|
||||||
|
|
||||||
|
$this->entity_count['invoices'] = $invoice_count;
|
||||||
|
|
||||||
|
$this->company->update_products = $initial_update_products_value;
|
||||||
|
$this->company->save();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
95
app/Import/Transformer/Invoice2Go/InvoiceTransformer.php
Normal file
95
app/Import/Transformer/Invoice2Go/InvoiceTransformer.php
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* client Ninja (https://clientninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/clientninja/clientninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. client Ninja LLC (https://clientninja.com)
|
||||||
|
*
|
||||||
|
* @license https://www.elastic.co/licensing/elastic-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Import\Transformer\Invoice2Go;
|
||||||
|
|
||||||
|
use App\Import\ImportException;
|
||||||
|
use App\Import\Transformer\BaseTransformer;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class InvoiceTransformer.
|
||||||
|
*/
|
||||||
|
class InvoiceTransformer extends BaseTransformer {
|
||||||
|
/**
|
||||||
|
* @param $line_items_data
|
||||||
|
*
|
||||||
|
* @return bool|array
|
||||||
|
*/
|
||||||
|
public function transform( $invoice_data ) {
|
||||||
|
|
||||||
|
if ( $this->hasInvoice( $invoice_data['DocumentNumber'] ) ) {
|
||||||
|
throw new ImportException( 'Invoice number already exists' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$invoiceStatusMap = [
|
||||||
|
'unsent' => Invoice::STATUS_DRAFT,
|
||||||
|
'sent' => Invoice::STATUS_SENT,
|
||||||
|
];
|
||||||
|
|
||||||
|
$transformed = [
|
||||||
|
'company_id' => $this->company->id,
|
||||||
|
'number' => $this->getString( $invoice_data, 'DocumentNumber' ),
|
||||||
|
'notes' => $this->getString( $invoice_data, 'Comment' ),
|
||||||
|
'date' => isset( $invoice_data['DocumentDate'] ) ? date( 'Y-m-d', strtotime( $invoice_data['DocumentDate'] ) ) : null,
|
||||||
|
// 'currency_id' => $this->getCurrencyByCode( $invoice_data, 'Currency' ),
|
||||||
|
'amount' => $this->getFloat( $invoice_data, 'TotalAmount' ),
|
||||||
|
'status_id' => $invoiceStatusMap[ $status =
|
||||||
|
strtolower( $this->getString( $invoice_data, 'DocumentStatus' ) ) ] ?? Invoice::STATUS_SENT,
|
||||||
|
// 'viewed' => $status === 'viewed',
|
||||||
|
'line_items' => [
|
||||||
|
[
|
||||||
|
'cost' => $this->getFloat( $invoice_data, 'TotalAmount' ),
|
||||||
|
'quantity' => 1,
|
||||||
|
'discount' => $this->getFloat( $invoice_data, 'DiscountValue' ),
|
||||||
|
'is_amount_discount' => false,
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$client_id =
|
||||||
|
$this->getClient( $this->getString( $invoice_data, 'Name' ), $this->getString( $invoice_data, 'EmailRecipient' ) );
|
||||||
|
|
||||||
|
if ( $client_id ) {
|
||||||
|
$transformed['client_id'] = $client_id;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
$settings = new \stdClass;
|
||||||
|
$settings->currency_id = $this->getCurrencyByCode( $invoice_data, 'Currency' );
|
||||||
|
|
||||||
|
$transformed['client'] = [
|
||||||
|
'name' => $this->getString( $invoice_data, 'Name' ),
|
||||||
|
'address1' => $this->getString( $invoice_data, 'DocumentRecipientAddress' ),
|
||||||
|
'shipping_address1' => $this->getString( $invoice_data, 'ShipAddress' ),
|
||||||
|
'credit_balance' => 0,
|
||||||
|
'settings' => $settings,
|
||||||
|
'client_hash' => Str::random( 40 ),
|
||||||
|
'contacts' => [
|
||||||
|
[
|
||||||
|
'email' => $this->getString( $invoice_data, 'EmailRecipient' ),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
if ( ! empty( $invoice_data['Date Paid'] ) ) {
|
||||||
|
$transformed['payments'] = [
|
||||||
|
[
|
||||||
|
'date' => date( 'Y-m-d', strtotime( $invoice_data['DatePaid'] ) ),
|
||||||
|
'amount' => $this->getFloat( $invoice_data, 'Payments' ),
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
nlog($transformed);
|
||||||
|
|
||||||
|
return $transformed;
|
||||||
|
}
|
||||||
|
}
|
147
tests/Feature/Import/Invoice2Go/Invoice2GoTest.php
Normal file
147
tests/Feature/Import/Invoice2Go/Invoice2GoTest.php
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Invoice Ninja (https://invoiceninja.com).
|
||||||
|
*
|
||||||
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
||||||
|
*
|
||||||
|
* @license https://opensource.org/licenses/AAL
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tests\Feature\Import\Invoice2Go;
|
||||||
|
|
||||||
|
use App\Import\Providers\BaseImport;
|
||||||
|
use App\Import\Providers\Freshbooks;
|
||||||
|
use App\Import\Providers\Invoice2Go;
|
||||||
|
use App\Import\Providers\Zoho;
|
||||||
|
use App\Import\Transformer\BaseTransformer;
|
||||||
|
use App\Models\Client;
|
||||||
|
use App\Models\Invoice;
|
||||||
|
use App\Models\Product;
|
||||||
|
use App\Models\Vendor;
|
||||||
|
use App\Utils\Traits\MakesHash;
|
||||||
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||||||
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Tests\MockAccountData;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @covers App\Import\Providers\Invoice2Go
|
||||||
|
*/
|
||||||
|
class Invoice2GoTest extends TestCase
|
||||||
|
{
|
||||||
|
use MakesHash;
|
||||||
|
use MockAccountData;
|
||||||
|
use DatabaseTransactions;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->withoutMiddleware(ThrottleRequests::class);
|
||||||
|
|
||||||
|
config(['database.default' => config('ninja.db.default')]);
|
||||||
|
|
||||||
|
$this->makeTestData();
|
||||||
|
|
||||||
|
$this->withoutExceptionHandling();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInvoice2GoImport()
|
||||||
|
{
|
||||||
|
$csv = file_get_contents(
|
||||||
|
base_path() . '/tests/Feature/Import/i2g_invoices.csv'
|
||||||
|
);
|
||||||
|
$hash = Str::random(32);
|
||||||
|
|
||||||
|
$column_map = [
|
||||||
|
0 => 'Id',
|
||||||
|
1 => 'AccountId',
|
||||||
|
2 => 'State',
|
||||||
|
3 => 'LastUpdated',
|
||||||
|
4 => 'CreatedDate',
|
||||||
|
5 => 'DocumentDate',
|
||||||
|
6 => 'DocumentNumber',
|
||||||
|
7 => 'Comment',
|
||||||
|
8 => 'Name',
|
||||||
|
9 => 'EmailRecipient',
|
||||||
|
10 => 'DocumentStatus',
|
||||||
|
11 => 'OutputStatus',
|
||||||
|
12 => 'PartPayment',
|
||||||
|
13 => 'DocumentRecipientAddress',
|
||||||
|
14 => 'ShipDate',
|
||||||
|
15 => 'ShipAddress',
|
||||||
|
16 => 'ShipAmount',
|
||||||
|
17 => 'ShipTrackingNumber',
|
||||||
|
18 => 'ShipVia',
|
||||||
|
19 => 'ShipFob',
|
||||||
|
20 => 'StyleName',
|
||||||
|
21 => 'DatePaid',
|
||||||
|
22 => 'CustomField',
|
||||||
|
23 => 'DiscountType',
|
||||||
|
24 => 'Discount',
|
||||||
|
25 => 'DiscountValue',
|
||||||
|
26 => 'Taxes',
|
||||||
|
27 => 'WithholdingTaxName',
|
||||||
|
28 => 'WithholdingTaxRate',
|
||||||
|
29 => 'TermsOld',
|
||||||
|
30 => 'Payments',
|
||||||
|
31 => 'Items',
|
||||||
|
32 => 'CurrencyCode',
|
||||||
|
33 => 'TotalAmount',
|
||||||
|
34 => 'BalanceDueAmount',
|
||||||
|
35 => 'AmountPaidAmount',
|
||||||
|
36 => 'DiscountAmount',
|
||||||
|
37 => 'SubtotalAmount',
|
||||||
|
38 => 'TotalTaxAmount',
|
||||||
|
39 => 'WithholdingTaxAmount',
|
||||||
|
];
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'hash' => $hash,
|
||||||
|
'column_map' => ['invoice' => ['mapping' => $column_map]],
|
||||||
|
'skip_header' => true,
|
||||||
|
'import_type' => 'invoice2go',
|
||||||
|
];
|
||||||
|
|
||||||
|
Cache::put($hash . '-invoice', base64_encode($csv), 360);
|
||||||
|
|
||||||
|
$csv_importer = new Invoice2Go($data, $this->company);
|
||||||
|
|
||||||
|
$count = $csv_importer->import('invoice');
|
||||||
|
|
||||||
|
$base_transformer = new BaseTransformer($this->company);
|
||||||
|
|
||||||
|
$this->assertTrue($base_transformer->hasClient('Barry Gordon'));
|
||||||
|
$this->assertTrue($base_transformer->hasClient('James Gordon'));
|
||||||
|
$this->assertTrue($base_transformer->hasClient('Deadpool Inc'));
|
||||||
|
$this->assertTrue($base_transformer->hasClient('Daily Planet'));
|
||||||
|
|
||||||
|
$client_id = $base_transformer->getClient('', 'wade@projectx.net');
|
||||||
|
|
||||||
|
$client = Client::find($client_id);
|
||||||
|
|
||||||
|
$this->assertInstanceOf(Client::class, $client);
|
||||||
|
$this->assertEquals('840', $client->country_id);
|
||||||
|
$this->assertEquals('2584 Sesame Street', $client->address1);
|
||||||
|
|
||||||
|
$this->assertTrue($base_transformer->hasInvoice("1"));
|
||||||
|
$this->assertTrue($base_transformer->hasInvoice("2"));
|
||||||
|
$this->assertTrue($base_transformer->hasInvoice("3"));
|
||||||
|
$this->assertTrue($base_transformer->hasInvoice("4"));
|
||||||
|
$invoice_id = $base_transformer->getInvoiceId("1");
|
||||||
|
$invoice = Invoice::find($invoice_id);
|
||||||
|
|
||||||
|
$this->assertEquals(953.55, $invoice->amount);
|
||||||
|
$this->assertEquals(1 , $invoice->status_id);
|
||||||
|
$this->assertEquals(0, $invoice->balance);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user