CSV Payment import

This commit is contained in:
David Bomba 2022-02-03 11:45:03 +11:00
parent e8a4ac2a27
commit 816cc283a0
2 changed files with 71 additions and 0 deletions

View File

@ -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])) {

View File

@ -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'));
}