1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Http/Controllers/ImportController.php

153 lines
5.9 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-11-18 15:40:50 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
2017-04-02 15:54:07 +02:00
use Illuminate\Http\Request;
2017-01-30 20:40:43 +01:00
use App\Services\ImportService;
2017-04-02 15:54:07 +02:00
use App\Jobs\ImportData;
2015-11-18 17:08:37 +01:00
use Exception;
2015-11-18 15:40:50 +01:00
use Input;
use Redirect;
2017-01-30 20:40:43 +01:00
use Session;
use Utils;
use View;
2017-04-02 15:54:07 +02:00
use Auth;
2015-11-18 15:40:50 +01:00
class ImportController extends BaseController
{
public function __construct(ImportService $importService)
{
$this->importService = $importService;
}
2017-04-02 15:54:07 +02:00
public function doImport(Request $request)
2015-11-18 15:40:50 +01:00
{
2017-04-02 17:10:24 +02:00
if (! Auth::user()->confirmed) {
return redirect('/settings/' . ACCOUNT_IMPORT_EXPORT)->withError(trans('texts.confirm_account_to_import'));
}
2015-11-18 18:16:23 +01:00
$source = Input::get('source');
2015-11-24 20:45:38 +01:00
$files = [];
2017-04-02 15:54:07 +02:00
$timestamp = time();
2015-11-18 18:16:23 +01:00
2015-11-24 20:45:38 +01:00
foreach (ImportService::$entityTypes as $entityType) {
2017-04-02 15:54:07 +02:00
$fileName = $entityType;
if ($request->hasFile($fileName)) {
$file = $request->file($fileName);
2017-04-30 10:58:56 +02:00
$destinationPath = env('FILE_IMPORT_PATH') ?: storage_path() . '/import';
2017-06-28 19:14:38 +02:00
$extension = strtolower($file->getClientOriginalExtension());
2017-04-02 15:54:07 +02:00
2017-04-27 10:34:00 +02:00
if ($source === IMPORT_CSV) {
if ($extension != 'csv') {
return redirect()->to('/settings/' . ACCOUNT_IMPORT_EXPORT)->withError(trans('texts.invalid_file'));
}
} elseif ($source === IMPORT_JSON) {
if ($extension != 'json') {
return redirect()->to('/settings/' . ACCOUNT_IMPORT_EXPORT)->withError(trans('texts.invalid_file'));
}
} else {
if (! in_array($extension, ['csv', 'xls', 'xlsx', 'json'])) {
return redirect()->to('/settings/' . ACCOUNT_IMPORT_EXPORT)->withError(trans('texts.invalid_file'));
}
2015-12-15 21:25:12 +01:00
}
2017-04-02 15:54:07 +02:00
$newFileName = sprintf('%s_%s_%s.%s', Auth::user()->account_id, $timestamp, $fileName, $extension);
$file->move($destinationPath, $newFileName);
2017-04-30 10:58:56 +02:00
$files[$entityType] = $destinationPath . '/' . $newFileName;
2015-11-18 15:40:50 +01:00
}
2015-11-24 20:45:38 +01:00
}
2015-11-18 18:16:23 +01:00
2017-01-30 17:05:31 +01:00
if (! count($files)) {
2016-09-25 20:13:14 +02:00
Session::flash('error', trans('texts.select_file'));
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
}
2015-11-24 20:45:38 +01:00
try {
if ($source === IMPORT_CSV) {
$data = $this->importService->mapCSV($files);
2017-04-02 15:54:07 +02:00
return View::make('accounts.import_map', [
'data' => $data,
'timestamp' => $timestamp,
]);
2016-06-02 21:03:59 +02:00
} elseif ($source === IMPORT_JSON) {
2017-03-22 10:50:55 +01:00
$includeData = filter_var(Input::get('data'), FILTER_VALIDATE_BOOLEAN);
$includeSettings = filter_var(Input::get('settings'), FILTER_VALIDATE_BOOLEAN);
2017-04-02 15:54:07 +02:00
if (config('queue.default') === 'sync') {
$results = $this->importService->importJSON($files[IMPORT_JSON], $includeData, $includeSettings);
$message = $this->importService->presentResults($results, $includeSettings);
} else {
$settings = [
'files' => $files,
'include_data' => $includeData,
'include_settings' => $includeSettings,
];
$this->dispatch(new ImportData(Auth::user(), IMPORT_JSON, $settings));
2017-04-02 19:19:10 +02:00
$message = trans('texts.import_started');
2017-04-02 15:54:07 +02:00
}
2015-11-24 20:45:38 +01:00
} else {
2017-04-02 15:54:07 +02:00
if (config('queue.default') === 'sync') {
$results = $this->importService->importFiles($source, $files);
$message = $this->importService->presentResults($results);
} else {
$settings = [
'files' => $files,
'source' => $source,
];
$this->dispatch(new ImportData(Auth::user(), false, $settings));
2017-04-02 19:19:10 +02:00
$message = trans('texts.import_started');
2017-04-02 15:54:07 +02:00
}
2015-11-18 18:16:23 +01:00
}
2017-04-02 15:54:07 +02:00
return redirect('/settings/' . ACCOUNT_IMPORT_EXPORT)->withWarning($message);
2015-11-24 20:45:38 +01:00
} catch (Exception $exception) {
2015-12-08 11:10:20 +01:00
Utils::logError($exception);
2015-11-24 20:45:38 +01:00
Session::flash('error', $exception->getMessage());
2017-01-30 20:40:43 +01:00
2015-12-15 21:25:12 +01:00
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
2015-11-18 18:16:23 +01:00
}
}
public function doImportCSV()
{
2015-11-25 10:35:24 +01:00
try {
2017-04-02 15:54:07 +02:00
$map = Input::get('map');
$headers = Input::get('headers');
$timestamp = Input::get('timestamp');
2017-04-30 10:58:56 +02:00
2017-04-02 15:54:07 +02:00
if (config('queue.default') === 'sync') {
$results = $this->importService->importCSV($map, $headers, $timestamp);
$message = $this->importService->presentResults($results);
} else {
$settings = [
'timestamp' => $timestamp,
'map' => $map,
'headers' => $headers,
];
$this->dispatch(new ImportData(Auth::user(), IMPORT_CSV, $settings));
2017-04-02 19:19:10 +02:00
$message = trans('texts.import_started');
2017-04-02 15:54:07 +02:00
}
2017-01-30 20:40:43 +01:00
2017-04-02 15:54:07 +02:00
return redirect('/settings/' . ACCOUNT_IMPORT_EXPORT)->withWarning($message);
2015-11-25 10:35:24 +01:00
} catch (Exception $exception) {
2015-12-08 11:10:20 +01:00
Utils::logError($exception);
2015-11-25 10:35:24 +01:00
Session::flash('error', $exception->getMessage());
2017-01-30 20:40:43 +01:00
2015-12-15 21:25:12 +01:00
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
}
}
2017-06-05 11:13:24 +02:00
public function cancelImport()
{
try {
$path = env('FILE_IMPORT_PATH') ?: storage_path() . '/import';
foreach ([ENTITY_CLIENT, ENTITY_INVOICE, ENTITY_PAYMENT, ENTITY_QUOTE, ENTITY_PRODUCT] as $entityType) {
$fileName = sprintf('%s/%s_%s_%s.csv', $path, Auth::user()->account_id, request()->timestamp, $entityType);
\File::delete($fileName);
}
} catch (Exception $exception) {
Utils::logError($exception);
}
return Redirect::to('/settings/' . ACCOUNT_IMPORT_EXPORT);
}
2015-11-18 15:40:50 +01:00
}