From 4115a6837beaffc447c2babd3569f0bdf7cd2bf6 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Feb 2022 17:38:23 +1100 Subject: [PATCH] Vendor Import --- app/Import/Providers/Csv.php | 43 +++++++++++- .../Transformer/Csv/VendorTransformer.php | 70 +++++++++++++++++++ app/Import/Transformers/BaseTransformer.php | 2 +- app/Services/Invoice/ApplyPayment.php | 2 +- tests/Feature/Import/CSV/CsvImportTest.php | 41 +++++++++-- 5 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 app/Import/Transformer/Csv/VendorTransformer.php diff --git a/app/Import/Providers/Csv.php b/app/Import/Providers/Csv.php index 912b15a996a5..9d0d687c0f9c 100644 --- a/app/Import/Providers/Csv.php +++ b/app/Import/Providers/Csv.php @@ -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) { diff --git a/app/Import/Transformer/Csv/VendorTransformer.php b/app/Import/Transformer/Csv/VendorTransformer.php new file mode 100644 index 000000000000..1c0fd06c3e5c --- /dev/null +++ b/app/Import/Transformer/Csv/VendorTransformer.php @@ -0,0 +1,70 @@ +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, + ]; + } +} diff --git a/app/Import/Transformers/BaseTransformer.php b/app/Import/Transformers/BaseTransformer.php index efc686841d66..97c21a2614bb 100644 --- a/app/Import/Transformers/BaseTransformer.php +++ b/app/Import/Transformers/BaseTransformer.php @@ -85,7 +85,7 @@ class BaseTransformer nlog("found via contact"); } } - nlog("did not find client"); + // nlog("did not find client"); return null; } diff --git a/app/Services/Invoice/ApplyPayment.php b/app/Services/Invoice/ApplyPayment.php index b23e3389ff08..65a6b901a51f 100644 --- a/app/Services/Invoice/ApplyPayment.php +++ b/app/Services/Invoice/ApplyPayment.php @@ -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 diff --git a/tests/Feature/Import/CSV/CsvImportTest.php b/tests/Feature/Import/CSV/CsvImportTest.php index 6db44514e3d3..71fb152fa8f5 100644 --- a/tests/Feature/Import/CSV/CsvImportTest.php +++ b/tests/Feature/Import/CSV/CsvImportTest.php @@ -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 - - - - }