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

377 lines
11 KiB
PHP
Raw Normal View History

2020-12-15 05:31:49 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Import;
2020-12-16 11:06:20 +01:00
use App\Factory\ClientFactory;
2020-12-19 04:49:15 +01:00
use App\Factory\InvoiceFactory;
2020-12-17 13:00:32 +01:00
use App\Factory\ProductFactory;
2020-12-16 11:06:20 +01:00
use App\Http\Requests\Client\StoreClientRequest;
2020-12-19 04:49:15 +01:00
use App\Http\Requests\Invoice\StoreInvoiceRequest;
2020-12-17 13:00:32 +01:00
use App\Http\Requests\Product\StoreProductRequest;
2020-12-16 11:06:20 +01:00
use App\Import\Transformers\ClientTransformer;
2020-12-19 08:28:58 +01:00
use App\Import\Transformers\InvoiceItemTransformer;
2020-12-19 04:49:15 +01:00
use App\Import\Transformers\InvoiceTransformer;
2020-12-17 13:00:32 +01:00
use App\Import\Transformers\ProductTransformer;
2020-12-15 05:31:49 +01:00
use App\Libraries\MultiDB;
2020-12-16 11:06:20 +01:00
use App\Models\Client;
2020-12-16 01:01:15 +01:00
use App\Models\Company;
2020-12-16 11:06:20 +01:00
use App\Models\Currency;
2020-12-19 04:49:15 +01:00
use App\Models\Invoice;
2020-12-16 11:06:20 +01:00
use App\Models\User;
use App\Repositories\ClientContactRepository;
use App\Repositories\ClientRepository;
2020-12-19 04:49:15 +01:00
use App\Repositories\InvoiceRepository;
2020-12-17 13:00:32 +01:00
use App\Repositories\ProductRepository;
2020-12-15 05:31:49 +01:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2020-12-16 12:52:40 +01:00
use Illuminate\Support\Facades\Auth;
2020-12-15 05:31:49 +01:00
use Illuminate\Support\Facades\Cache;
2020-12-16 12:52:40 +01:00
use Illuminate\Support\Facades\Validator;
2020-12-16 01:01:15 +01:00
use League\Csv\Reader;
use League\Csv\Statement;
2020-12-15 05:31:49 +01:00
class CSVImport implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $invoice;
public $company;
public $hash;
public $entity_type;
2020-12-16 01:01:15 +01:00
public $skip_header;
public $column_map;
2020-12-16 11:06:20 +01:00
public $import_array;
public $error_array;
public $maps;
2020-12-16 01:01:15 +01:00
public function __construct(array $request, Company $company)
2020-12-15 05:31:49 +01:00
{
$this->company = $company;
2020-12-16 01:01:15 +01:00
$this->hash = $request['hash'];
$this->entity_type = $request['entity_type'];
2020-12-15 05:31:49 +01:00
2020-12-16 01:01:15 +01:00
$this->skip_header = $request['skip_header'];
2020-12-15 05:31:49 +01:00
2020-12-16 01:01:15 +01:00
$this->column_map = $request['column_map'];
2020-12-15 05:31:49 +01:00
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
MultiDB::setDb($this->company->db);
2020-12-16 11:06:20 +01:00
$this->company->owner()->setCompany($this->company);
Auth::login($this->company->owner(), true);
$this->buildMaps();
//sort the array by key
ksort($this->column_map);
info("import".ucfirst($this->entity_type));
2020-12-17 13:00:32 +01:00
$this->{"import".ucfirst($this->entity_type)}();
2020-12-18 05:34:40 +01:00
2020-12-19 05:11:15 +01:00
info("errors");
2020-12-19 08:28:58 +01:00
info(print_r($this->error_array,1));
2020-12-19 05:11:15 +01:00
2020-12-17 13:00:32 +01:00
}
public function failed($exception)
{
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
private function importProduct()
{
2020-12-18 05:34:40 +01:00
info("importing products");
2020-12-17 13:00:32 +01:00
$product_repository = new ProductRepository();
2020-12-18 01:31:27 +01:00
$product_transformer = new ProductTransformer($this->maps);
2020-12-17 13:00:32 +01:00
$records = $this->getCsvData();
if ($this->skip_header)
array_shift($records);
foreach ($records as $record)
{
$keys = $this->column_map;
$values = array_intersect_key($record, $this->column_map);
2020-12-18 05:34:40 +01:00
2020-12-17 13:00:32 +01:00
$product_data = array_combine($keys, $values);
$product = $product_transformer->transform($product_data);
2020-12-18 05:34:40 +01:00
$validator = Validator::make($product, (new StoreProductRequest())->rules());
2020-12-17 13:00:32 +01:00
if ($validator->fails()) {
$this->error_array[] = ['product' => $product, 'error' => json_encode($validator->errors())];
} else {
$product = $product_repository->save($product, ProductFactory::create($this->company->id, $this->setUser($record)));
$product->save();
$this->maps['products'][] = $product->id;
}
}
}
2020-12-19 04:49:15 +01:00
private function importInvoice()
{
2020-12-19 05:11:15 +01:00
$invoice_transformer = new InvoiceTransformer($this->maps);
2020-12-19 04:49:15 +01:00
info("import invoices");
2020-12-19 08:28:58 +01:00
info("column_map");
info(print_r($this->column_map,1));
2020-12-19 04:49:15 +01:00
$records = $this->getCsvData();
$invoice_number_key = array_search('Invoice Number', reset($records));
info("number key = {$invoice_number_key}");
2020-12-19 05:11:15 +01:00
if ($this->skip_header)
array_shift($records);
2020-12-19 08:28:58 +01:00
if(!$invoice_number_key){
info("no invoice number to use as key - returning");
return;
}
2020-12-19 04:49:15 +01:00
2020-12-19 08:28:58 +01:00
$unique_array_filter = array_unique($records[$invoice_number_key]);
2020-12-19 04:49:15 +01:00
$unique_invoices = array_intersect_key( $records, $unique_array_filter );
2020-12-19 08:28:58 +01:00
foreach($unique_invoices as $unique)
{
2020-12-19 04:49:15 +01:00
2020-12-19 08:28:58 +01:00
$keys = $this->column_map;
$values = array_intersect_key($unique, $this->column_map);
$invoice_data = array_combine($keys, $values);
2020-12-19 04:49:15 +01:00
2020-12-19 08:28:58 +01:00
$invoice = $invoice_transformer->transform($invoice_data);
2020-12-19 04:49:15 +01:00
2020-12-19 08:28:58 +01:00
foreach($unique_invoices as $val) {
2020-12-19 04:49:15 +01:00
2020-12-19 08:28:58 +01:00
$invoices = array_filter($records, function($item) use ($val, $invoice_number_key){
return $item[$invoice_number_key] == $val[$invoice_number_key];
});
}
2020-12-19 04:49:15 +01:00
2020-12-19 05:11:15 +01:00
$this->processInvoice($invoices, $invoice);
2020-12-19 08:28:58 +01:00
2020-12-19 04:49:15 +01:00
}
2020-12-19 08:28:58 +01:00
2020-12-19 04:49:15 +01:00
}
2020-12-19 05:11:15 +01:00
private function processInvoice($invoices, $invoice)
2020-12-19 04:49:15 +01:00
{
2020-12-19 08:28:58 +01:00
$invoice_repository = new InvoiceRepository();
$item_transformer = new InvoiceItemTransformer($this->maps);
2020-12-19 04:49:15 +01:00
$items = [];
foreach($invoices as $record)
{
2020-12-19 08:28:58 +01:00
2020-12-19 04:49:15 +01:00
$keys = $this->column_map;
2020-12-19 08:28:58 +01:00
$values = array_intersect_key($record, $this->column_map);
$invoice_data = array_combine($keys, $values);
2020-12-19 04:49:15 +01:00
2020-12-19 08:28:58 +01:00
$items[] = $item_transformer->transform($invoice_data);
2020-12-19 04:49:15 +01:00
}
2020-12-19 08:28:58 +01:00
$invoice['line_items'] = $items;
2020-12-19 04:49:15 +01:00
2020-12-19 08:28:58 +01:00
info(print_r($invoice->toArray(),1));
2020-12-19 04:49:15 +01:00
$validator = Validator::make($invoice, (new StoreInvoiceRequest())->rules());
if ($validator->fails()) {
2020-12-19 08:28:58 +01:00
$this->error_array[] = ['invoice' => $invoice, 'error' => json_encode($validator->errors())];
2020-12-19 04:49:15 +01:00
} else {
$invoice = $invoice_repository->save($invoice, InvoiceFactory::create($this->company->id, $this->setUser($record)));
$invoice->save();
$this->maps['invoices'][] = $invoice->id;
$this->performInvoiceActions($invoice, $record, $invoice_repository);
}
}
private function performInvoiceActions($invoice, $record, $invoice_repository)
{
$invoice = $this->actionInvoiceStatus($invoice, $record, $invoice_repository);
}
private function actionInvoiceStatus($invoice, $status, $invoice_repository)
{
switch ($status) {
case 'Archived':
$invoice_repository->archive($invoice);
$invoice->fresh();
break;
case 'Sent':
$invoice = $invoice->service()->markSent()->save();
break;
case 'Viewed';
$invoice = $invoice->service()->markSent()->save();
break;
default:
# code...
break;
}
if($invoice->balance < $invoice->amount && $invoice->status_id <= Invoice::STATUS_SENT){
$invoice->status_id = Invoice::STATUS_PARTIAL;
$invoice->save();
}
return $invoice;
}
2020-12-17 13:00:32 +01:00
//todo limit client imports for hosted version
private function importClient()
{
2020-12-16 11:06:20 +01:00
//clients
$records = $this->getCsvData();
2020-12-19 08:28:58 +01:00
info(print_r($this->column_map,1));
2020-12-16 11:06:20 +01:00
$contact_repository = new ClientContactRepository();
$client_repository = new ClientRepository($contact_repository);
$client_transformer = new ClientTransformer($this->maps);
2020-12-17 13:00:32 +01:00
if ($this->skip_header)
2020-12-16 11:06:20 +01:00
array_shift($records);
2020-12-16 12:52:40 +01:00
foreach ($records as $record) {
2020-12-18 01:31:27 +01:00
2020-12-16 11:06:20 +01:00
$keys = $this->column_map;
$values = array_intersect_key($record, $this->column_map);
$client_data = array_combine($keys, $values);
$client = $client_transformer->transform($client_data);
$validator = Validator::make($client, (new StoreClientRequest())->rules());
if ($validator->fails()) {
$this->error_array[] = ['client' => $client, 'error' => json_encode($validator->errors())];
2020-12-16 12:52:40 +01:00
} else {
2020-12-16 11:06:20 +01:00
$client = $client_repository->save($client, ClientFactory::create($this->company->id, $this->setUser($record)));
2020-12-16 12:52:40 +01:00
if (array_key_exists('client.balance', $client_data)) {
2020-12-16 11:06:20 +01:00
$client->balance = preg_replace('/[^0-9,.]+/', '', $client_data['client.balance']);
2020-12-16 12:52:40 +01:00
}
2020-12-16 11:06:20 +01:00
2020-12-16 12:52:40 +01:00
if (array_key_exists('client.paid_to_date', $client_data)) {
2020-12-16 11:06:20 +01:00
$client->paid_to_date = preg_replace('/[^0-9,.]+/', '', $client_data['client.paid_to_date']);
2020-12-16 12:52:40 +01:00
}
2020-12-16 11:06:20 +01:00
$client->save();
2020-12-17 13:00:32 +01:00
$this->maps['clients'][] = $client->id;
2020-12-16 11:06:20 +01:00
}
2020-12-16 01:01:15 +01:00
}
2020-12-15 05:31:49 +01:00
}
2020-12-16 12:52:40 +01:00
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
2020-12-16 11:06:20 +01:00
private function buildMaps()
{
$this->maps['currencies'] = Currency::all();
$this->maps['users'] = $this->company->users;
$this->maps['company'] = $this->company;
2020-12-17 13:00:32 +01:00
$this->maps['clients'] = [];
$this->maps['products'] = [];
2020-12-16 11:06:20 +01:00
return $this;
}
private function setUser($record)
{
$user_key_exists = array_search('client.user_id', $this->column_map);
2020-12-16 12:52:40 +01:00
if ($user_key_exists) {
2020-12-16 11:06:20 +01:00
return $this->findUser($record[$user_key_exists]);
2020-12-16 12:52:40 +01:00
} else {
2020-12-16 11:06:20 +01:00
return $this->company->owner()->id;
2020-12-16 12:52:40 +01:00
}
2020-12-16 11:06:20 +01:00
}
2020-12-16 12:52:40 +01:00
private function findUser($user_hash)
2020-12-16 11:06:20 +01:00
{
$user = User::where('company_id', $this->company->id)
->where(\DB::raw('CONCAT_WS(" ", first_name, last_name)'), 'like', '%' . $user_hash . '%')
->first();
2020-12-16 12:52:40 +01:00
if ($user) {
2020-12-16 11:06:20 +01:00
return $user->id;
2020-12-16 12:52:40 +01:00
} else {
2020-12-16 11:06:20 +01:00
return $this->company->owner()->id;
2020-12-16 12:52:40 +01:00
}
2020-12-16 11:06:20 +01:00
}
2020-12-16 01:01:15 +01:00
private function getCsvData()
2020-12-15 05:31:49 +01:00
{
$base64_encoded_csv = Cache::get($this->hash);
2020-12-16 01:01:15 +01:00
$csv = base64_decode($base64_encoded_csv);
2020-12-16 11:06:20 +01:00
$csv = Reader::createFromString($csv);
2020-12-16 01:01:15 +01:00
$stmt = new Statement();
$data = iterator_to_array($stmt->process($csv));
if (count($data) > 0) {
$headers = $data[0];
// Remove Invoice Ninja headers
if (count($headers) && count($data) > 4) {
$firstCell = $headers[0];
2020-12-16 11:06:20 +01:00
if (strstr($firstCell, config('ninja.app_name'))) {
2020-12-16 01:01:15 +01:00
array_shift($data); // Invoice Ninja...
array_shift($data); // <blank line>
array_shift($data); // Enitty Type Header
}
}
}
return $data;
2020-12-15 05:31:49 +01:00
}
}