1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Import/Providers/Csv.php

324 lines
9.1 KiB
PHP
Raw Normal View History

2022-02-01 07:14:27 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-02-01 07:14:27 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
2022-02-03 08:53:39 +01:00
2022-02-01 07:14:27 +01:00
namespace App\Import\Providers;
2022-10-25 01:25:04 +02:00
use App\Factory\BankTransactionFactory;
2022-02-01 07:14:27 +01:00
use App\Factory\ClientFactory;
2022-02-03 08:53:39 +01:00
use App\Factory\ExpenseFactory;
2022-02-01 10:04:03 +01:00
use App\Factory\InvoiceFactory;
2022-02-03 01:45:03 +01:00
use App\Factory\PaymentFactory;
2022-02-01 10:04:03 +01:00
use App\Factory\ProductFactory;
use App\Factory\QuoteFactory;
2022-02-03 07:38:23 +01:00
use App\Factory\VendorFactory;
2022-10-25 01:25:04 +02:00
use App\Http\Requests\BankTransaction\StoreBankTransactionRequest;
2022-02-01 07:14:27 +01:00
use App\Http\Requests\Client\StoreClientRequest;
2022-02-03 08:53:39 +01:00
use App\Http\Requests\Expense\StoreExpenseRequest;
2022-02-01 10:04:03 +01:00
use App\Http\Requests\Invoice\StoreInvoiceRequest;
2022-02-03 01:45:03 +01:00
use App\Http\Requests\Payment\StorePaymentRequest;
2022-02-01 10:04:03 +01:00
use App\Http\Requests\Product\StoreProductRequest;
use App\Http\Requests\Quote\StoreQuoteRequest;
2022-02-03 07:38:23 +01:00
use App\Http\Requests\Vendor\StoreVendorRequest;
2023-02-16 02:36:09 +01:00
use App\Import\Transformer\Bank\BankTransformer;
2022-02-01 07:14:27 +01:00
use App\Import\Transformer\Csv\ClientTransformer;
2022-02-03 08:53:39 +01:00
use App\Import\Transformer\Csv\ExpenseTransformer;
2022-02-02 05:56:37 +01:00
use App\Import\Transformer\Csv\InvoiceTransformer;
2022-02-03 01:45:03 +01:00
use App\Import\Transformer\Csv\PaymentTransformer;
2022-02-01 10:04:03 +01:00
use App\Import\Transformer\Csv\ProductTransformer;
use App\Import\Transformer\Csv\QuoteTransformer;
2022-02-03 07:38:23 +01:00
use App\Import\Transformer\Csv\VendorTransformer;
2022-10-25 01:25:04 +02:00
use App\Repositories\BankTransactionRepository;
2022-02-01 07:14:27 +01:00
use App\Repositories\ClientRepository;
2022-02-03 08:53:39 +01:00
use App\Repositories\ExpenseRepository;
2022-02-01 10:04:03 +01:00
use App\Repositories\InvoiceRepository;
2022-02-03 01:45:03 +01:00
use App\Repositories\PaymentRepository;
2022-02-01 10:04:03 +01:00
use App\Repositories\ProductRepository;
use App\Repositories\QuoteRepository;
2022-02-03 07:38:23 +01:00
use App\Repositories\VendorRepository;
use App\Services\Bank\BankMatchingService;
2022-11-03 09:44:38 +01:00
use App\Utils\Traits\MakesHash;
2022-02-01 07:14:27 +01:00
class Csv extends BaseImport implements ImportInterface
{
2022-11-03 09:44:38 +01:00
use MakesHash;
2022-02-01 10:04:03 +01:00
public array $entity_count = [];
2022-02-03 00:14:54 +01:00
public function import(string $entity)
{
if (
in_array($entity, [
'client',
'product',
'invoice',
'payment',
'vendor',
'expense',
'quote',
2022-10-25 01:25:04 +02:00
'bank_transaction',
2022-02-03 00:14:54 +01:00
])
) {
2022-02-01 10:04:03 +01:00
$this->{$entity}();
2022-02-03 00:14:54 +01:00
}
2022-02-01 07:14:27 +01:00
}
2022-02-03 00:14:54 +01:00
2022-10-25 01:25:04 +02:00
public function bank_transaction()
{
$entity_type = 'bank_transaction';
$data = $this->getCsvData($entity_type);
2023-02-16 02:36:09 +01:00
if (is_array($data)) {
2022-11-03 09:44:38 +01:00
$data = $this->preTransformCsv($data, $entity_type);
2022-10-25 09:48:19 +02:00
2023-02-16 02:36:09 +01:00
foreach ($data as $key => $value) {
2022-11-15 13:04:14 +01:00
$data[$key]['transaction.bank_integration_id'] = $this->decodePrimaryKey($this->request['bank_integration_id']);
2022-10-25 09:48:19 +02:00
}
2022-10-25 01:25:04 +02:00
}
if (empty($data)) {
$this->entity_count['bank_transactions'] = 0;
return;
}
$this->request_name = StoreBankTransactionRequest::class;
$this->repository_name = BankTransactionRepository::class;
$this->factory_name = BankTransactionFactory::class;
2022-11-03 09:44:38 +01:00
$this->repository = app()->make($this->repository_name);
2022-10-25 01:25:04 +02:00
$this->transformer = new BankTransformer($this->company);
$bank_transaction_count = $this->ingest($data, $entity_type);
$this->entity_count['bank_transactions'] = $bank_transaction_count;
2022-12-09 02:35:37 +01:00
nlog("bank matching co id = {$this->company->id}");
2022-10-25 01:25:04 +02:00
(new BankMatchingService($this->company->id, $this->company->db))->handle();
2022-10-25 01:25:04 +02:00
}
2022-02-09 22:53:48 +01:00
public function client()
2022-02-01 07:14:27 +01:00
{
$entity_type = 'client';
$data = $this->getCsvData($entity_type);
if (is_array($data)) {
2022-02-17 04:40:28 +01:00
$data = $this->preTransformCsv($data, $entity_type);
}
2022-02-01 10:04:03 +01:00
2022-02-03 00:14:54 +01:00
if (empty($data)) {
2022-02-01 10:04:03 +01:00
$this->entity_count['clients'] = 0;
2022-02-01 07:14:27 +01:00
return;
2022-02-01 10:04:03 +01:00
}
2022-02-01 07:14:27 +01:00
$this->request_name = StoreClientRequest::class;
$this->repository_name = ClientRepository::class;
$this->factory_name = ClientFactory::class;
2022-02-03 00:14:54 +01:00
$this->repository = app()->make($this->repository_name);
2022-02-01 07:14:27 +01:00
$this->repository->import_mode = true;
$this->transformer = new ClientTransformer($this->company);
2022-02-01 10:04:03 +01:00
$client_count = $this->ingest($data, $entity_type);
$this->entity_count['clients'] = $client_count;
2022-02-01 07:14:27 +01:00
}
2022-02-09 22:53:48 +01:00
public function product()
2022-02-01 10:04:03 +01:00
{
$entity_type = 'product';
$data = $this->getCsvData($entity_type);
if (is_array($data)) {
2022-02-17 04:40:28 +01:00
$data = $this->preTransformCsv($data, $entity_type);
}
2022-02-01 10:04:03 +01:00
2022-02-03 00:14:54 +01:00
if (empty($data)) {
2022-02-01 10:04:03 +01:00
$this->entity_count['products'] = 0;
2022-02-01 10:04:03 +01:00
return;
}
$this->request_name = StoreProductRequest::class;
$this->repository_name = ProductRepository::class;
$this->factory_name = ProductFactory::class;
2022-02-03 00:14:54 +01:00
$this->repository = app()->make($this->repository_name);
2022-02-01 10:04:03 +01:00
$this->repository->import_mode = true;
$this->transformer = new ProductTransformer($this->company);
2022-04-26 08:53:41 +02:00
$product_count = $this->ingestProducts($data, $entity_type);
2022-02-01 07:14:27 +01:00
2022-02-01 10:04:03 +01:00
$this->entity_count['products'] = $product_count;
}
2022-02-09 22:53:48 +01:00
public function invoice()
2022-02-01 10:04:03 +01:00
{
$entity_type = 'invoice';
$data = $this->getCsvData($entity_type);
if (is_array($data)) {
2022-02-17 04:40:28 +01:00
$data = $this->preTransformCsv($data, $entity_type);
}
2022-02-01 10:04:03 +01:00
2022-02-03 00:14:54 +01:00
if (empty($data)) {
2022-02-01 10:04:03 +01:00
$this->entity_count['invoices'] = 0;
2022-02-01 10:04:03 +01:00
return;
}
$this->request_name = StoreInvoiceRequest::class;
$this->repository_name = InvoiceRepository::class;
$this->factory_name = InvoiceFactory::class;
2022-02-03 00:14:54 +01:00
$this->repository = app()->make($this->repository_name);
2022-02-01 10:04:03 +01:00
$this->repository->import_mode = true;
2022-02-02 05:56:37 +01:00
$this->transformer = new InvoiceTransformer($this->company);
2022-02-01 10:04:03 +01:00
2022-02-03 01:18:29 +01:00
$invoice_count = $this->ingestInvoices($data, 'invoice.number');
2022-02-01 10:04:03 +01:00
$this->entity_count['invoices'] = $invoice_count;
}
2022-02-01 07:14:27 +01:00
public function quote()
{
$entity_type = 'quote';
$data = $this->getCsvData($entity_type);
if (is_array($data)) {
$data = $this->preTransformCsv($data, $entity_type);
}
if (empty($data)) {
$this->entity_count['quotes'] = 0;
return;
}
$this->request_name = StoreQuoteRequest::class;
$this->repository_name = QuoteRepository::class;
$this->factory_name = QuoteFactory::class;
$this->repository = app()->make($this->repository_name);
$this->repository->import_mode = true;
$this->transformer = new QuoteTransformer($this->company);
$quote_count = $this->ingestQuotes($data, 'quote.number');
$this->entity_count['quotes'] = $quote_count;
}
2022-02-09 22:53:48 +01:00
public function payment()
2022-02-03 01:45:03 +01:00
{
$entity_type = 'payment';
$data = $this->getCsvData($entity_type);
if (is_array($data)) {
2022-02-17 04:40:28 +01:00
$data = $this->preTransformCsv($data, $entity_type);
}
2022-02-03 01:45:03 +01:00
if (empty($data)) {
$this->entity_count['payments'] = 0;
2022-02-03 01:45:03 +01:00
return;
}
2022-06-24 04:21:31 +02:00
2022-02-03 01:45:03 +01:00
$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;
}
2022-02-09 22:53:48 +01:00
public function vendor()
2022-02-03 07:38:23 +01:00
{
$entity_type = 'vendor';
$data = $this->getCsvData($entity_type);
if (is_array($data)) {
2022-02-17 04:40:28 +01:00
$data = $this->preTransformCsv($data, $entity_type);
}
2022-02-03 07:38:23 +01:00
if (empty($data)) {
$this->entity_count['vendors'] = 0;
2022-02-03 07:38:23 +01:00
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;
}
2022-02-09 22:53:48 +01:00
public function expense()
2022-02-03 07:38:23 +01:00
{
2022-02-03 08:53:39 +01:00
$entity_type = 'expense';
$data = $this->getCsvData($entity_type);
if (is_array($data)) {
2022-02-17 04:40:28 +01:00
$data = $this->preTransformCsv($data, $entity_type);
}
2022-02-03 08:53:39 +01:00
if (empty($data)) {
$this->entity_count['expenses'] = 0;
2022-02-03 08:53:39 +01:00
return;
}
$this->request_name = StoreExpenseRequest::class;
$this->repository_name = ExpenseRepository::class;
$this->factory_name = ExpenseFactory::class;
$this->repository = app()->make($this->repository_name);
$this->repository->import_mode = true;
$this->transformer = new ExpenseTransformer($this->company);
$expense_count = $this->ingest($data, $entity_type);
$this->entity_count['expenses'] = $expense_count;
2022-02-03 07:38:23 +01:00
}
2022-02-09 22:53:48 +01:00
public function task()
2022-02-03 07:38:23 +01:00
{
}
2022-02-03 01:45:03 +01:00
2022-02-03 00:14:54 +01:00
public function transform(array $data)
{
2022-02-01 07:14:27 +01:00
}
2022-02-03 00:14:54 +01:00
}