2020-12-12 11:01:53 +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\Http\Controllers;
|
|
|
|
|
2020-12-14 23:59:41 +01:00
|
|
|
use App\Http\Requests\Import\ImportRequest;
|
2020-12-12 11:01:53 +01:00
|
|
|
use App\Http\Requests\Import\PreImportRequest;
|
2020-12-14 23:59:41 +01:00
|
|
|
use App\Jobs\Import\CSVImport;
|
2020-12-12 11:01:53 +01:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
use Illuminate\Support\Str;
|
2020-12-14 05:59:15 +01:00
|
|
|
use League\Csv\Reader;
|
|
|
|
use League\Csv\Statement;
|
2020-12-12 11:01:53 +01:00
|
|
|
|
|
|
|
class ImportController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param StoreImportRequest $request
|
|
|
|
* @return Response
|
|
|
|
*
|
|
|
|
* @OA\Post(
|
|
|
|
* path="/api/v1/preimport",
|
|
|
|
* operationId="preimport",
|
|
|
|
* tags={"imports"},
|
|
|
|
* summary="Pre Import checks - returns a reference to the job and the headers of the CSV",
|
|
|
|
* description="Pre Import checks - returns a reference to the job and the headers of the CSV",
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/include"),
|
2020-12-12 11:35:42 +01:00
|
|
|
* @OA\RequestBody(
|
|
|
|
* description="The CSV file",
|
|
|
|
* required=true,
|
|
|
|
* @OA\MediaType(
|
|
|
|
* mediaType="multipart/form-data",
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="binary"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
* ),
|
2020-12-12 11:01:53 +01:00
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Returns a reference to the file",
|
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
|
|
|
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
*
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
|
|
|
*/
|
|
|
|
public function preimport(PreImportRequest $request)
|
|
|
|
{
|
|
|
|
//create a reference
|
|
|
|
$hash = Str::random(32);
|
|
|
|
|
|
|
|
//store the csv in cache with an expiry of 10 minutes
|
2020-12-16 11:06:20 +01:00
|
|
|
Cache::put($hash, base64_encode(file_get_contents($request->file('file')->getPathname())), 3600);
|
2020-12-12 11:01:53 +01:00
|
|
|
|
|
|
|
//parse CSV
|
|
|
|
$csv_array = $this->getCsvData(file_get_contents($request->file('file')->getPathname()));
|
2020-12-12 11:35:42 +01:00
|
|
|
|
2020-12-16 01:01:15 +01:00
|
|
|
$class_map = $this->getEntityMap($request->input('entity_type'));
|
|
|
|
|
2020-12-14 11:43:07 +01:00
|
|
|
$data = [
|
2020-12-12 11:35:42 +01:00
|
|
|
'hash' => $hash,
|
2020-12-16 01:01:15 +01:00
|
|
|
'available' => $class_map::importable(),
|
2020-12-12 11:35:42 +01:00
|
|
|
'headers' => array_slice($csv_array, 0, 2)
|
|
|
|
];
|
|
|
|
|
|
|
|
return response()->json($data);
|
2020-12-12 11:01:53 +01:00
|
|
|
}
|
|
|
|
|
2020-12-14 23:59:41 +01:00
|
|
|
public function import(ImportRequest $request)
|
|
|
|
{
|
2020-12-16 01:01:15 +01:00
|
|
|
CSVImport::dispatch($request->all(), auth()->user()->company());
|
2020-12-14 23:59:41 +01:00
|
|
|
|
|
|
|
return response()->json(['message' => 'Importing data, email will be sent on completion'], 200);
|
|
|
|
}
|
2020-12-12 11:01:53 +01:00
|
|
|
|
2020-12-16 01:01:15 +01:00
|
|
|
private function getEntityMap($entity_type)
|
|
|
|
{
|
|
|
|
return sprintf('App\\Import\\Definitions\%sMap', ucfirst($entity_type));
|
|
|
|
}
|
|
|
|
|
2020-12-12 11:01:53 +01:00
|
|
|
private function getCsvData($csvfile)
|
|
|
|
{
|
|
|
|
if (! ini_get('auto_detect_line_endings')) {
|
|
|
|
ini_set('auto_detect_line_endings', '1');
|
|
|
|
}
|
|
|
|
|
|
|
|
$csv = Reader::createFromString($csvfile);
|
|
|
|
$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-18 01:31:27 +01:00
|
|
|
|
|
|
|
if (strstr($firstCell, (string)config('ninja.app_name'))) {
|
2020-12-12 11:01:53 +01:00
|
|
|
array_shift($data); // Invoice Ninja...
|
|
|
|
array_shift($data); // <blank line>
|
|
|
|
array_shift($data); // Enitty Type Header
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
}
|