From 816cc283a0798d9debadbe3acf19a2e98f0d7ae1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 3 Feb 2022 11:45:03 +1100 Subject: [PATCH] CSV Payment import --- app/Import/Providers/Csv.php | 33 +++++++++++++++++++ tests/Feature/Import/CSV/CsvImportTest.php | 38 ++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/app/Import/Providers/Csv.php b/app/Import/Providers/Csv.php index 76480484927b..912b15a996a5 100644 --- a/app/Import/Providers/Csv.php +++ b/app/Import/Providers/Csv.php @@ -12,18 +12,22 @@ namespace App\Import\Providers; use App\Factory\ClientFactory; use App\Factory\InvoiceFactory; +use App\Factory\PaymentFactory; use App\Factory\ProductFactory; 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\Import\ImportException; use App\Import\Providers\BaseImport; use App\Import\Providers\ImportInterface; 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\Repositories\ClientRepository; use App\Repositories\InvoiceRepository; +use App\Repositories\PaymentRepository; use App\Repositories\ProductRepository; use Illuminate\Support\Facades\Validator; use Symfony\Component\HttpFoundation\ParameterBag; @@ -131,6 +135,35 @@ class Csv extends BaseImport implements ImportInterface $this->entity_count['invoices'] = $invoice_count; } + + private function payment() + { + $entity_type = 'payment'; + + $data = $this->getCsvData($entity_type); + + $data = $this->preTransform($data, $entity_type); + + if (empty($data)) { + $this->entity_count['payments'] = 0; + return; + } + + $this->request_name = StorePaymentRequest::class; + $this->repository_name = PaymentRepository::class; + $this->factory_name = PaymentFactory::class; + + $this->repository = app()->make($this->repository_name); + $this->repository->import_mode = true; + + $this->transformer = new PaymentTransformer($this->company); + + $payment_count = $this->ingest($data, $entity_type); + + $this->entity_count['payments'] = $payment_count; + } + + public function preTransform(array $data, $entity_type) { if (empty($this->column_map[$entity_type])) { diff --git a/tests/Feature/Import/CSV/CsvImportTest.php b/tests/Feature/Import/CSV/CsvImportTest.php index 0fd11f94ef99..6db44514e3d3 100644 --- a/tests/Feature/Import/CSV/CsvImportTest.php +++ b/tests/Feature/Import/CSV/CsvImportTest.php @@ -207,6 +207,44 @@ class CsvImportTest extends TestCase $this->assertTrue($base_transformer->hasInvoice("801")); + + + /* Lets piggy back payments tests here to save rebuilding the test multiple times*/ + + + $csv = file_get_contents( base_path() . '/tests/Feature/Import/payments.csv' ); + $hash = Str::random( 32 ); + + $column_map = [ + 0 => 'payment.client_id', + 1 => 'payment.invoice_number', + 2 => 'payment.amount', + 3 => 'payment.date', + ]; + + $data = [ + 'hash' => $hash, + 'column_map' => [ 'payment' => [ 'mapping' => $column_map ] ], + 'skip_header' => true, + 'import_type' => 'csv', + ]; + + Cache::put( $hash . '-payment', base64_encode( $csv ), 360 ); + + $csv_importer = new Csv($data, $this->company); + + $csv_importer->import('payment'); + + $this->assertTrue($base_transformer->hasInvoice("801")); + + $invoice_id = $base_transformer->getInvoiceId("801"); + + $invoice = Invoice::find($invoice_id); + + $this->assertTrue($invoice->payments()->exists()); + $this->assertEquals(1, $invoice->payments()->count()); + $this->assertEquals(400, $invoice->payments()->sum('payments.amount')); + }