Tests for vendor import with Wave

This commit is contained in:
David Bomba 2022-02-10 15:10:56 +11:00
parent ac09ffff32
commit 52c0d98d82
5 changed files with 162 additions and 10 deletions

View File

@ -14,13 +14,17 @@ namespace App\Import\Providers;
use App\Factory\ClientFactory;
use App\Factory\InvoiceFactory;
use App\Factory\VendorFactory;
use App\Http\Requests\Client\StoreClientRequest;
use App\Http\Requests\Invoice\StoreInvoiceRequest;
use App\Http\Requests\Vendor\StoreVendorRequest;
use App\Import\Transformer\Wave\ClientTransformer;
use App\Import\Transformer\Wave\InvoiceTransformer;
use App\Import\Transformer\Wave\VendorTransformer;
use App\Models\Client;
use App\Repositories\ClientRepository;
use App\Repositories\InvoiceRepository;
use App\Repositories\VendorRepository;
class Wave extends BaseImport implements ImportInterface
{
@ -35,7 +39,7 @@ class Wave extends BaseImport implements ImportInterface
'invoice',
// 'product',
// 'payment',
// 'vendor',
'vendor',
// 'expense',
])
) {
@ -93,10 +97,9 @@ class Wave extends BaseImport implements ImportInterface
$entity_type = 'invoice';
$data = $this->getCsvData($entity_type);
nlog($data);
$data = $this->preTransform($data, $entity_type);
nlog($data);
if (empty($data)) {
$this->entity_count['invoices'] = 0;
return;
@ -120,9 +123,39 @@ nlog($data);
}
public function payment() {}
public function payment()
{
//these are pulled in when processing invoices
}
public function vendor() {}
public function vendor()
{
$entity_type = 'vendor';
$data = $this->getCsvData($entity_type);
nlog($data);
$data = $this->preTransform($data, $entity_type);
nlog($data);
if (empty($data)) {
$this->entity_count['vendors'] = 0;
return;
}
$this->request_name = StoreVendorRequest::class;
$this->repository_name = VendorRepository::class;
$this->factory_name = VendorFactory::class;
$this->repository = app()->make($this->repository_name);
$this->repository->import_mode = true;
$this->transformer = new VendorTransformer($this->company);
$vendor_count = $this->ingest($data, $entity_type);
$this->entity_count['vendors'] = $vendor_count;
}
public function expense() {}

View File

@ -36,7 +36,7 @@ class BaseTransformer
public function getString($data, $field)
{
return isset($data[$field]) && $data[$field] ? $data[$field] : '';
return isset($data[$field]) && $data[$field] ? trim($data[$field]) : '';
}
public function getValueOrNull($data, $field)

View File

@ -0,0 +1,56 @@
<?php
/**
* Invoice 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\Wave;
use App\Import\ImportException;
use App\Import\Transformer\BaseTransformer;
use Illuminate\Support\Str;
/**
* Class VendorTransformer.
*/
class VendorTransformer extends BaseTransformer {
/**
* @param $data
*
* @return array|bool
*/
public function transform( $data ) {
if ( isset( $data['vendor_name'] ) && $this->hasVendor( $data['vendor_name'] ) ) {
throw new ImportException('Vendor already exists');
}
return [
'company_id' => $this->company->id,
'name' => $this->getString( $data, 'vendor_name' ),
'number' => $this->getValueOrNull( $data, 'account_number' ),
'phone' => $this->getString( $data, 'phone' ),
'website' => $this->getString( $data, 'website' ),
'country_id' => !empty( $data['country'] ) ? $this->getCountryId( $data['country'] ) : null,
'state' => $this->getString( $data, 'province/state' ),
'address1' => $this->getString( $data, 'address_line_1' ),
'address2' => $this->getString( $data, 'address_line_2' ),
'city' => $this->getString( $data, 'city' ),
'postal_code' => $this->getString( $data, 'postal_code/zip_code' ),
'currency_id' => $this->getCurrencyByCode( $data, 'vendor_currency' ),
'client_hash' => Str::random( 40 ),
'contacts' => [
[
'first_name' => $this->getString( $data, 'contact_first_name' ),
'last_name' => $this->getString( $data, 'contact_last_name' ),
'email' => $this->getString( $data, 'email' ),
'phone' => $this->getString( $data, 'phone' ),
],
],
];
}
}

View File

@ -16,6 +16,7 @@ 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;
@ -47,6 +48,70 @@ class WaveTest extends TestCase
$this->withoutExceptionHandling();
}
public function testVendorWaveImport()
{
$csv = file_get_contents(
base_path() . '/tests/Feature/Import/wave_vendors.csv'
);
$hash = Str::random(32);
$column_map = [
0 => 'vendor_name',
1 => 'email',
2 => 'contact_first_name',
3 => 'contact_last_name',
4 => 'vendor_currency',
5 => 'account_number',
6 => 'phone',
7 => 'fax',
8 => 'mobile',
9 => 'toll_free',
10 => 'website',
11 => 'country',
12 => 'province/state',
13 => 'address_line_1',
14 => 'address_line_2',
15 => 'city',
16 => 'postal_code/zip_code',
];
$data = [
'hash' => $hash,
'column_map' => ['vendor' => ['mapping' => $column_map]],
'skip_header' => true,
'import_type' => 'waveaccounting',
];
Cache::put($hash . '-vendor', base64_encode($csv), 360);
$csv_importer = new Wave($data, $this->company);
$count = $csv_importer->import('vendor');
$base_transformer = new BaseTransformer($this->company);
$this->assertTrue($base_transformer->hasVendor('Vendor Name'));
$vendor_id = $base_transformer->getVendorId('Vendor Name');
$vendor = Vendor::find($vendor_id);
$this->assertInstanceOf(Vendor::class, $vendor);
$this->assertEquals(12, $vendor->currency_id);
$this->assertEquals('Australian Capital Territory', $vendor->state);
$this->assertEquals('city', $vendor->city);
$this->assertEquals('postal_cod', $vendor->postal_code);
$this->assertEquals('firstname', $vendor->contacts->first()->first_name);
$this->assertEquals('lastname', $vendor->contacts->first()->last_name);
$this->assertEquals('vendor@gmail.com', $vendor->contacts->first()->email);
$this->assertEquals('phone', $vendor->contacts->first()->phone);
}
public function testClientWaveImport()
{
$csv = file_get_contents(
@ -236,10 +301,6 @@ class WaveTest extends TestCase
$this->assertTrue($invoice->payments()->exists());
$this->assertEquals(3500.41, $invoice->payments->first()->amount);
$product = Product::where('notes', 'Lawn Service')->where('company_id', $this->company->id)->first();
nlog(Product::all()->pluck('notes'));
}

View File

@ -0,0 +1,2 @@
vendor_name,email,contact_first_name,contact_last_name,vendor_currency,account_number,phone,fax,mobile,toll_free,website,country,province/state,address_line_1,address_line_2,city,postal_code/zip_code
Vendor Name,vendor@gmail.com,firstname,lastname,AUD,,phone,fax,mob,,,Australia,Australian Capital Territory,address1,address2,city ,postal_cod
1 vendor_name email contact_first_name contact_last_name vendor_currency account_number phone fax mobile toll_free website country province/state address_line_1 address_line_2 city postal_code/zip_code
2 Vendor Name vendor@gmail.com firstname lastname AUD phone fax mob Australia Australian Capital Territory address1 address2 city postal_cod