2019-09-27 06:31:13 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2019-09-27 06:31:13 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-09-27 06:31:13 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2019-09-27 06:31:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-08-12 02:41:26 +02:00
|
|
|
use App\Http\Requests\Activity\DownloadHistoricalEntityRequest;
|
2019-09-27 06:31:13 +02:00
|
|
|
use App\Models\Activity;
|
|
|
|
use App\Transformers\ActivityTransformer;
|
2021-05-04 11:56:15 +02:00
|
|
|
use App\Utils\HostedPDF\NinjaPdf;
|
2021-10-24 10:27:12 +02:00
|
|
|
use App\Utils\Ninja;
|
2021-05-04 11:56:15 +02:00
|
|
|
use App\Utils\PhantomJS\Phantom;
|
2020-08-12 02:01:27 +02:00
|
|
|
use App\Utils\Traits\Pdf\PdfMaker;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2019-09-27 06:31:13 +02:00
|
|
|
use Illuminate\Http\Request;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Illuminate\Http\Response;
|
2021-10-20 05:05:46 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
2021-05-04 11:56:15 +02:00
|
|
|
use stdClass;
|
2019-09-27 06:31:13 +02:00
|
|
|
|
|
|
|
class ActivityController extends BaseController
|
|
|
|
{
|
2020-08-12 02:01:27 +02:00
|
|
|
use PdfMaker;
|
|
|
|
|
2019-09-27 06:31:13 +02:00
|
|
|
protected $entity_type = Activity::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = ActivityTransformer::class;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @OA\Get(
|
2019-11-18 11:46:01 +01:00
|
|
|
* path="/api/v1/actvities",
|
|
|
|
* operationId="getActivities",
|
|
|
|
* tags={"actvities"},
|
|
|
|
* summary="Gets a list of actvities",
|
|
|
|
* description="Lists all activities",
|
|
|
|
* @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"),
|
|
|
|
* @OA\Parameter(ref="#/components/parameters/index"),
|
2019-11-28 11:35:13 +01:00
|
|
|
* @OA\Parameter(
|
|
|
|
* name="rows",
|
2019-12-04 02:06:14 +01:00
|
|
|
* in="query",
|
2019-11-28 11:35:13 +01:00
|
|
|
* description="The number of activities to return",
|
|
|
|
* example="50",
|
2019-12-04 02:06:14 +01:00
|
|
|
* required=false,
|
2019-11-28 11:35:13 +01:00
|
|
|
* @OA\Schema(
|
|
|
|
* type="number",
|
|
|
|
* format="integer",
|
|
|
|
* ),
|
|
|
|
* ),
|
2019-11-18 11:46:01 +01:00
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A list of actvities",
|
2020-06-21 23:30:25 +02:00
|
|
|
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
2019-11-18 11:46:01 +01:00
|
|
|
* @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\JsonContent(ref="#/components/schemas/Activity"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=422,
|
|
|
|
* description="Validation error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
2019-12-30 22:59:12 +01:00
|
|
|
* response="default",
|
2019-11-18 11:46:01 +01:00
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param Request $request
|
|
|
|
* @return Response|mixed
|
2019-09-27 06:31:13 +02:00
|
|
|
*/
|
2019-11-28 11:35:13 +01:00
|
|
|
public function index(Request $request)
|
2019-09-27 06:31:13 +02:00
|
|
|
{
|
2019-12-16 12:52:00 +01:00
|
|
|
$default_activities = $request->has('rows') ? $request->input('rows') : 50;
|
2019-09-27 06:31:13 +02:00
|
|
|
|
2019-11-27 10:47:59 +01:00
|
|
|
$activities = Activity::orderBy('created_at', 'DESC')->company()
|
2019-11-28 11:35:13 +01:00
|
|
|
->take($default_activities);
|
2019-11-27 10:47:59 +01:00
|
|
|
|
2019-09-27 06:31:13 +02:00
|
|
|
return $this->listResponse($activities);
|
|
|
|
}
|
2020-08-12 02:01:27 +02:00
|
|
|
|
2020-08-12 02:41:26 +02:00
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @OA\Get(
|
2020-08-12 02:41:26 +02:00
|
|
|
* path="/api/v1/actvities/download_entity/{activity_id}",
|
|
|
|
* operationId="getActivityHistoricalEntityPdf",
|
|
|
|
* tags={"actvities"},
|
|
|
|
* summary="Gets a PDF for the given activity",
|
|
|
|
* description="Gets a PDF for the given activity",
|
|
|
|
* @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(
|
|
|
|
* name="activity_id",
|
|
|
|
* in="path",
|
|
|
|
* description="The Activity Hashed ID",
|
|
|
|
* example="D2J234DFA",
|
|
|
|
* required=true,
|
|
|
|
* @OA\Schema(
|
|
|
|
* type="string",
|
|
|
|
* format="string",
|
|
|
|
* ),
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="PDF 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=404,
|
|
|
|
* description="No file exists for the given record",
|
|
|
|
* ),
|
|
|
|
* @OA\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="Unexpected Error",
|
|
|
|
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
|
|
|
* ),
|
|
|
|
* )
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param DownloadHistoricalEntityRequest $request
|
|
|
|
* @param Activity $activity
|
|
|
|
* @return JsonResponse|StreamedResponse
|
2020-08-12 02:41:26 +02:00
|
|
|
*/
|
|
|
|
public function downloadHistoricalEntity(DownloadHistoricalEntityRequest $request, Activity $activity)
|
2020-08-12 02:01:27 +02:00
|
|
|
{
|
2020-08-12 02:13:34 +02:00
|
|
|
$backup = $activity->backup;
|
2021-10-20 05:05:46 +02:00
|
|
|
$html_backup = '';
|
2020-08-12 02:01:27 +02:00
|
|
|
|
2021-10-20 05:05:46 +02:00
|
|
|
/* Refactor 20-10-2021
|
|
|
|
*
|
|
|
|
* We have moved the backups out of the database and into object storage.
|
|
|
|
* In order to handle edge cases, we still check for the database backup
|
|
|
|
* in case the file no longer exists
|
|
|
|
*/
|
|
|
|
|
|
|
|
if($backup && $backup->filename && Storage::disk(config('filesystems.default'))->exists($backup->filename)){ //disk
|
2021-10-24 10:27:12 +02:00
|
|
|
|
|
|
|
if(Ninja::isHosted())
|
|
|
|
$html_backup = file_get_contents(Storage::disk(config('filesystems.default'))->url($backup->filename));
|
|
|
|
else
|
|
|
|
$html_backup = file_get_contents(Storage::disk(config('filesystems.default'))->path($backup->filename));
|
|
|
|
|
2021-10-20 05:05:46 +02:00
|
|
|
}
|
|
|
|
elseif($backup && $backup->html_backup){ //db
|
|
|
|
$html_backup = $backup->html_backup;
|
|
|
|
}
|
|
|
|
elseif (! $backup || ! $backup->html_backup) { //failed
|
2021-01-24 23:24:13 +01:00
|
|
|
return response()->json(['message'=> ctrans('texts.no_backup_exists'), 'errors' => new stdClass], 404);
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2020-08-12 02:13:34 +02:00
|
|
|
|
2021-05-09 13:30:31 +02:00
|
|
|
if (config('ninja.phantomjs_pdf_generation') || config('ninja.pdf_generator') == 'phantom') {
|
2021-10-20 05:05:46 +02:00
|
|
|
$pdf = (new Phantom)->convertHtmlToPdf($html_backup);
|
2021-05-04 11:56:15 +02:00
|
|
|
}
|
2021-05-09 13:30:31 +02:00
|
|
|
elseif(config('ninja.invoiceninja_hosted_pdf_generation') || config('ninja.pdf_generator') == 'hosted_ninja'){
|
2021-10-20 05:05:46 +02:00
|
|
|
$pdf = (new NinjaPdf())->build($html_backup);
|
2021-05-04 11:56:15 +02:00
|
|
|
}
|
|
|
|
else {
|
2021-10-20 05:05:46 +02:00
|
|
|
$pdf = $this->makePdf(null, null, $html_backup);
|
2021-05-04 11:56:15 +02:00
|
|
|
}
|
2020-08-12 02:01:27 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (isset($activity->invoice_id)) {
|
2021-03-17 12:29:20 +01:00
|
|
|
$filename = $activity->invoice->numberFormatter().'.pdf';
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif (isset($activity->quote_id)) {
|
2021-03-17 12:29:20 +01:00
|
|
|
$filename = $activity->quote->numberFormatter().'.pdf';
|
2020-09-06 11:38:10 +02:00
|
|
|
} elseif (isset($activity->credit_id)) {
|
2021-03-17 12:29:20 +01:00
|
|
|
$filename = $activity->credit->numberFormatter().'.pdf';
|
2020-09-06 11:38:10 +02:00
|
|
|
} else {
|
|
|
|
$filename = 'backup.pdf';
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->streamDownload(function () use ($pdf) {
|
2020-08-12 02:01:27 +02:00
|
|
|
echo $pdf;
|
2021-07-26 00:22:29 +02:00
|
|
|
}, $filename, ['Content-Type' => 'application/pdf']);
|
2020-08-12 02:01:27 +02:00
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|