1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02:00

Vendor Import

This commit is contained in:
David Bomba 2022-02-03 17:38:23 +11:00
parent 816cc283a0
commit 4115a6837b
5 changed files with 151 additions and 7 deletions

View File

@ -14,10 +14,12 @@ use App\Factory\ClientFactory;
use App\Factory\InvoiceFactory;
use App\Factory\PaymentFactory;
use App\Factory\ProductFactory;
use App\Factory\VendorFactory;
use App\Http\Requests\Client\StoreClientRequest;
use App\Http\Requests\Invoice\StoreInvoiceRequest;
use App\Http\Requests\Payment\StorePaymentRequest;
use App\Http\Requests\Product\StoreProductRequest;
use App\Http\Requests\Vendor\StoreVendorRequest;
use App\Import\ImportException;
use App\Import\Providers\BaseImport;
use App\Import\Providers\ImportInterface;
@ -25,10 +27,12 @@ use App\Import\Transformer\Csv\ClientTransformer;
use App\Import\Transformer\Csv\InvoiceTransformer;
use App\Import\Transformer\Csv\PaymentTransformer;
use App\Import\Transformer\Csv\ProductTransformer;
use App\Import\Transformer\Csv\VendorTransformer;
use App\Repositories\ClientRepository;
use App\Repositories\InvoiceRepository;
use App\Repositories\PaymentRepository;
use App\Repositories\ProductRepository;
use App\Repositories\VendorRepository;
use Illuminate\Support\Facades\Validator;
use Symfony\Component\HttpFoundation\ParameterBag;
@ -135,7 +139,6 @@ class Csv extends BaseImport implements ImportInterface
$this->entity_count['invoices'] = $invoice_count;
}
private function payment()
{
$entity_type = 'payment';
@ -163,6 +166,44 @@ class Csv extends BaseImport implements ImportInterface
$this->entity_count['payments'] = $payment_count;
}
private function vendor()
{
$entity_type = 'vendor';
$data = $this->getCsvData($entity_type);
$data = $this->preTransform($data, $entity_type);
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;
}
private function expense()
{
}
private function quote()
{
}
private function task()
{
}
public function preTransform(array $data, $entity_type)
{

View File

@ -0,0 +1,70 @@
<?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\Csv;
use App\Import\ImportException;
use App\Import\Transformer\BaseTransformer;
/**
* Class VendorTransformer.
*/
class VendorTransformer extends BaseTransformer
{
/**
* @param $data
*
* @return array|bool
*/
public function transform($data)
{
if (isset($data->name) && $this->hasVendor($data->name)) {
throw new ImportException('Vendor already exists');
}
return [
'company_id' => $this->company->id,
'name' => $this->getString($data, 'vendor.name'),
'phone' => $this->getString($data, 'vendor.phone'),
'id_number' => $this->getString($data, 'vendor.id_number'),
'vat_number' => $this->getString($data, 'vendor.vat_number'),
'website' => $this->getString($data, 'vendor.website'),
'currency_id' => $this->getCurrencyByCode(
$data,
'vendor.currency_id'
),
'public_notes' => $this->getString($data, 'vendor.public_notes'),
'private_notes' => $this->getString($data, 'vendor.private_notes'),
'address1' => $this->getString($data, 'vendor.address1'),
'address2' => $this->getString($data, 'vendor.address2'),
'city' => $this->getString($data, 'vendor.city'),
'state' => $this->getString($data, 'vendor.state'),
'postal_code' => $this->getString($data, 'vendor.postal_code'),
'custom_value1' => $this->getString($data, 'vendor.custom_value1'),
'custom_value2' => $this->getString($data, 'vendor.custom_value2'),
'custom_value3' => $this->getString($data, 'vendor.custom_value3'),
'custom_value4' => $this->getString($data, 'vendor.custom_value4'),
'vendor_contacts' => [
[
'first_name' => $this->getString(
$data,
'vendor.first_name'
),
'last_name' => $this->getString($data, 'vendor.last_name'),
'email' => $this->getString($data, 'vendor.email'),
'phone' => $this->getString($data, 'vendor.phone'),
],
],
'country_id' => isset($data['vendor.country_id'])
? $this->getCountryId($data['vendor.country_id'])
: null,
];
}
}

View File

@ -85,7 +85,7 @@ class BaseTransformer
nlog("found via contact");
}
}
nlog("did not find client");
// nlog("did not find client");
return null;
}

View File

@ -104,7 +104,7 @@ class ApplyPayment extends AbstractService
->ledger()
->updatePaymentBalance($amount_paid);
nlog("updating client balance by amount {$amount_paid}");
// nlog("updating client balance by amount {$amount_paid}");
$this->invoice
->client

View File

@ -54,6 +54,43 @@ class CsvImportTest extends TestCase
$this->withoutExceptionHandling();
}
public function testVendorCsvImport() {
$csv = file_get_contents( base_path() . '/tests/Feature/Import/vendors.csv' );
$hash = Str::random( 32 );
$column_map = [
0 => 'vendor.name',
19 => 'vendor.currency_id',
20 => 'vendor.public_notes',
21 => 'vendor.private_notes',
22 => 'vendor.first_name',
23 => 'vendor.last_name',
];
$data = [
'hash' => $hash,
'column_map' => [ 'vendor' => [ 'mapping' => $column_map ] ],
'skip_header' => true,
'import_type' => 'csv',
];
$pre_import = Vendor::count();
Cache::put( $hash . '-vendor', base64_encode( $csv ), 360 );
$csv_importer = new Csv($data, $this->company);
$csv_importer->import('vendor');
$base_transformer = new BaseTransformer($this->company);
$this->assertTrue($base_transformer->hasVendor("Ludwig Krajcik DVM"));
}
public function testProductImport()
{
$csv = file_get_contents( base_path() . '/tests/Feature/Import/products.csv' );
@ -256,10 +293,6 @@ class CsvImportTest extends TestCase
}