1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Jobs/Import/CSVIngest.php

142 lines
4.3 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
*/
namespace App\Jobs\Import;
use App\Factory\ClientContactFactory;
use App\Factory\VendorContactFactory;
2022-02-01 07:14:27 +01:00
use App\Import\Providers\Csv;
use App\Import\Providers\Freshbooks;
use App\Import\Providers\Invoice2Go;
use App\Import\Providers\Invoicely;
use App\Import\Providers\Wave;
use App\Import\Providers\Zoho;
use App\Libraries\MultiDB;
use App\Models\Client;
2022-02-01 07:14:27 +01:00
use App\Models\Company;
use App\Models\Vendor;
2022-02-01 07:14:27 +01:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Str;
2022-02-01 07:14:27 +01:00
class CSVIngest implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2022-02-01 07:14:27 +01:00
public Company $company;
public string $hash;
public string $import_type;
public ?string $skip_header;
public ?array $column_map = [];
2022-02-17 03:27:21 +01:00
public array $request;
2022-02-01 07:14:27 +01:00
2022-02-28 22:04:54 +01:00
public $tries = 1;
public function __construct(array $request, Company $company)
{
$this->company = $company;
$this->request = $request;
$this->hash = $request['hash'];
2022-02-01 07:14:27 +01:00
$this->import_type = $request['import_type'];
$this->skip_header = $request['skip_header'] ?? null;
$this->column_map =
! empty($request['column_map']) ?
array_combine(array_keys($request['column_map']), array_column($request['column_map'], 'mapping')) : null;
2022-02-01 07:14:27 +01:00
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->company->db);
2022-02-01 07:14:27 +01:00
2022-05-07 09:10:23 +02:00
set_time_limit(0);
2022-07-31 11:11:32 +02:00
$engine = $this->bootEngine();
2022-02-01 07:14:27 +01:00
2023-04-19 07:21:50 +02:00
foreach (['client', 'product', 'invoice', 'payment', 'vendor', 'expense', 'quote', 'bank_transaction', 'recurring_invoice'] as $entity) {
2022-02-01 07:14:27 +01:00
$engine->import($entity);
}
$engine->finalizeImport();
$this->checkContacts();
}
private function checkContacts()
{
$vendors = Vendor::withTrashed()->where('company_id', $this->company->id)->doesntHave('contacts')->get();
foreach ($vendors as $vendor) {
$new_contact = VendorContactFactory::create($vendor->company_id, $vendor->user_id);
$new_contact->vendor_id = $vendor->id;
$new_contact->contact_key = Str::random(40);
$new_contact->is_primary = true;
$new_contact->save();
}
$clients = Client::withTrashed()->where('company_id', $this->company->id)->doesntHave('contacts')->get();
foreach ($clients as $client) {
$new_contact = ClientContactFactory::create($client->company_id, $client->user_id);
$new_contact->client_id = $client->id;
$new_contact->contact_key = Str::random(40);
$new_contact->is_primary = true;
$new_contact->save();
}
2023-02-16 02:36:09 +01:00
Client::with('contacts')->where('company_id', $this->company->id)->cursor()->each(function ($client) {
$contact = $client->contacts()->first();
$contact->is_primary = true;
$contact->save();
});
2023-02-16 02:36:09 +01:00
Vendor::with('contacts')->where('company_id', $this->company->id)->cursor()->each(function ($vendor) {
$contact = $vendor->contacts()->first();
$contact->is_primary = true;
$contact->save();
});
2022-02-01 07:14:27 +01:00
}
2022-07-31 11:11:32 +02:00
private function bootEngine()
2022-02-01 07:14:27 +01:00
{
2022-07-31 11:11:32 +02:00
switch ($this->import_type) {
2022-02-01 07:14:27 +01:00
case 'csv':
return new Csv($this->request, $this->company);
2022-02-01 07:14:27 +01:00
case 'waveaccounting':
return new Wave($this->request, $this->company);
2022-02-01 07:14:27 +01:00
case 'invoicely':
return new Invoicely($this->request, $this->company);
2022-02-01 07:14:27 +01:00
case 'invoice2go':
return new Invoice2Go($this->request, $this->company);
2022-02-01 07:14:27 +01:00
case 'zoho':
return new Zoho($this->request, $this->company);
2022-02-01 07:14:27 +01:00
case 'freshbooks':
return new Freshbooks($this->request, $this->company);
2022-02-01 07:14:27 +01:00
default:
2022-07-31 11:11:32 +02:00
nlog("could not return provider");
2022-02-01 07:14:27 +01:00
break;
}
}
}