diff --git a/app/Import/Providers/Wave.php b/app/Import/Providers/Wave.php index 08f3bba9e881..3da28843ebd0 100644 --- a/app/Import/Providers/Wave.php +++ b/app/Import/Providers/Wave.php @@ -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() {} diff --git a/app/Import/Transformer/BaseTransformer.php b/app/Import/Transformer/BaseTransformer.php index 8a3473ea5557..ec4e6b01a27c 100644 --- a/app/Import/Transformer/BaseTransformer.php +++ b/app/Import/Transformer/BaseTransformer.php @@ -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) diff --git a/app/Import/Transformer/Wave/VendorTransformer.php b/app/Import/Transformer/Wave/VendorTransformer.php new file mode 100644 index 000000000000..0f0fe76b322c --- /dev/null +++ b/app/Import/Transformer/Wave/VendorTransformer.php @@ -0,0 +1,56 @@ +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' ), + ], + ], + ]; + } +} \ No newline at end of file diff --git a/tests/Feature/Import/Wave/WaveTest.php b/tests/Feature/Import/Wave/WaveTest.php index 4b0cab13cb92..b474dc472cc2 100644 --- a/tests/Feature/Import/Wave/WaveTest.php +++ b/tests/Feature/Import/Wave/WaveTest.php @@ -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')); } diff --git a/tests/Feature/Import/wave_vendors.csv b/tests/Feature/Import/wave_vendors.csv new file mode 100644 index 000000000000..a7e23df0efd7 --- /dev/null +++ b/tests/Feature/Import/wave_vendors.csv @@ -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