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
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. 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;
|
|
|
|
|
2024-06-19 06:57:40 +02:00
|
|
|
use stdClass;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Ninja;
|
2024-06-19 06:57:40 +02:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Models\Activity;
|
|
|
|
use Illuminate\Http\Request;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2024-06-19 06:57:40 +02:00
|
|
|
use App\Utils\PhantomJS\Phantom;
|
|
|
|
use App\Utils\HostedPDF\NinjaPdf;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Utils\Traits\Pdf\PdfMaker;
|
2024-06-19 06:57:40 +02:00
|
|
|
use App\Utils\Traits\Pdf\PageNumbering;
|
2021-10-20 05:05:46 +02:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
2024-06-19 06:57:40 +02:00
|
|
|
use App\Transformers\ActivityTransformer;
|
|
|
|
use App\Http\Requests\Activity\StoreNoteRequest;
|
|
|
|
use App\Http\Requests\Activity\ShowActivityRequest;
|
|
|
|
use App\Http\Requests\Activity\DownloadHistoricalEntityRequest;
|
|
|
|
use App\Models\Credit;
|
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PurchaseOrder;
|
|
|
|
use App\Models\Quote;
|
|
|
|
use App\Models\RecurringExpense;
|
|
|
|
use App\Models\RecurringInvoice;
|
|
|
|
use App\Models\Task;
|
|
|
|
use App\Models\Vendor;
|
2019-09-27 06:31:13 +02:00
|
|
|
|
|
|
|
class ActivityController extends BaseController
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use PdfMaker;
|
|
|
|
use PageNumbering;
|
|
|
|
use MakesHash;
|
2020-08-12 02:01:27 +02:00
|
|
|
|
2019-09-27 06:31:13 +02:00
|
|
|
protected $entity_type = Activity::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = ActivityTransformer::class;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:35:13 +01:00
|
|
|
public function index(Request $request)
|
2019-09-27 06:31:13 +02:00
|
|
|
{
|
2023-06-24 15:04:19 +02:00
|
|
|
$default_activities = $request->has('rows') ? $request->input('rows') : 75;
|
2019-09-27 06:31:13 +02:00
|
|
|
|
2024-01-31 04:44:16 +01:00
|
|
|
/* @var App\Models\Activity[] $activities */
|
2023-03-03 22:45:41 +01:00
|
|
|
$activities = Activity::with('user')
|
|
|
|
->orderBy('created_at', 'DESC')
|
2023-01-23 01:01:27 +01:00
|
|
|
->company()
|
2019-11-28 11:35:13 +01:00
|
|
|
->take($default_activities);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-06-26 14:50:26 +02:00
|
|
|
if($request->has('reactv2')) {
|
2023-06-24 15:02:38 +02:00
|
|
|
|
|
|
|
/** @var \App\Models\User auth()->user() */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
if (!$user->isAdmin()) {
|
|
|
|
$activities->where('user_id', auth()->user()->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$system = ctrans('texts.system');
|
|
|
|
|
2023-08-03 06:30:14 +02:00
|
|
|
$data = $activities->cursor()->map(function ($activity) {
|
2024-02-13 05:25:18 +01:00
|
|
|
|
2024-01-31 04:44:16 +01:00
|
|
|
/** @var \App\Models\Activity $activity */
|
2023-06-24 15:02:38 +02:00
|
|
|
return $activity->activity_string();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return response()->json(['data' => $data->toArray()], 200);
|
|
|
|
}
|
2022-05-27 10:25:32 +02:00
|
|
|
|
2019-09-27 06:31:13 +02:00
|
|
|
return $this->listResponse($activities);
|
|
|
|
}
|
2020-08-12 02:01:27 +02:00
|
|
|
|
2023-07-18 05:15:40 +02:00
|
|
|
public function entityActivity(ShowActivityRequest $request)
|
|
|
|
{
|
|
|
|
|
|
|
|
$default_activities = request()->has('rows') ? request()->input('rows') : 75;
|
|
|
|
|
|
|
|
$activities = Activity::with('user')
|
|
|
|
->orderBy('created_at', 'DESC')
|
|
|
|
->company()
|
|
|
|
->where("{$request->entity}_id", $request->entity_id)
|
|
|
|
->take($default_activities);
|
|
|
|
|
|
|
|
/** @var \App\Models\User auth()->user() */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
if (!$user->isAdmin()) {
|
|
|
|
$activities->where('user_id', auth()->user()->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$system = ctrans('texts.system');
|
|
|
|
|
2023-08-03 06:30:14 +02:00
|
|
|
$data = $activities->cursor()->map(function ($activity) {
|
2023-07-18 05:15:40 +02:00
|
|
|
|
2024-01-31 04:44:16 +01:00
|
|
|
/** @var \App\Models\Activity $activity */
|
2023-07-18 05:15:40 +02:00
|
|
|
return $activity->activity_string();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return response()->json(['data' => $data->toArray()], 200);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-16 00:30:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* downloadHistoricalEntity
|
|
|
|
*
|
|
|
|
* @param DownloadHistoricalEntityRequest $request
|
|
|
|
* @param Activity $activity
|
|
|
|
* @return \Symfony\Component\HttpFoundation\StreamedResponse | \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
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
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
/* Refactor 20-10-2021
|
2021-10-20 05:05:46 +02:00
|
|
|
*
|
|
|
|
* We have moved the backups out of the database and into object storage.
|
2022-06-21 11:57:17 +02:00
|
|
|
* In order to handle edge cases, we still check for the database backup
|
2021-10-20 05:05:46 +02:00
|
|
|
* in case the file no longer exists
|
|
|
|
*/
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($backup && $backup->filename && Storage::disk(config('filesystems.default'))->exists($backup->filename)) { //disk
|
|
|
|
if (Ninja::isHosted()) {
|
2021-10-24 10:27:12 +02:00
|
|
|
$html_backup = file_get_contents(Storage::disk(config('filesystems.default'))->url($backup->filename));
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-10-24 10:27:12 +02:00
|
|
|
$html_backup = file_get_contents(Storage::disk(config('filesystems.default'))->path($backup->filename));
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-11-06 22:52:06 +01:00
|
|
|
} else { //failed
|
2024-01-14 05:05:00 +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') {
|
2024-01-14 05:05:00 +01:00
|
|
|
$pdf = (new Phantom())->convertHtmlToPdf($html_backup);
|
2022-05-25 14:00:17 +02:00
|
|
|
|
|
|
|
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($numbered_pdf) {
|
|
|
|
$pdf = $numbered_pdf;
|
|
|
|
}
|
|
|
|
} 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);
|
2022-05-25 14:00:17 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
|
2022-05-25 14:00:17 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($numbered_pdf) {
|
|
|
|
$pdf = $numbered_pdf;
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-20 05:05:46 +02:00
|
|
|
$pdf = $this->makePdf(null, null, $html_backup);
|
2022-05-25 14:00:17 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$numbered_pdf = $this->pageNumbering($pdf, $activity->company);
|
2022-05-25 14:00:17 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($numbered_pdf) {
|
|
|
|
$pdf = $numbered_pdf;
|
|
|
|
}
|
2021-05-04 11:56:15 +02:00
|
|
|
}
|
2020-08-12 02:01:27 +02:00
|
|
|
|
2023-07-20 08:46:10 +02:00
|
|
|
$activity->company->setLocale();
|
2024-01-14 05:05:00 +01: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
|
|
|
}
|
2024-06-19 06:57:40 +02:00
|
|
|
|
|
|
|
public function note(StoreNoteRequest $request)
|
|
|
|
{
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$entity = $request->getEntity();
|
|
|
|
|
|
|
|
$activity = new Activity();
|
|
|
|
$activity->account_id = $user->account_id;
|
|
|
|
$activity->company_id = $user->company()->id;
|
|
|
|
$activity->notes = $request->notes;
|
|
|
|
$activity->user_id = $user->id;
|
|
|
|
$activity->ip = $request->ip();
|
2024-06-21 02:29:38 +02:00
|
|
|
$activity->activity_type_id = Activity::USER_NOTE;
|
2024-06-19 06:57:40 +02:00
|
|
|
|
|
|
|
switch (get_class($entity)) {
|
|
|
|
case Invoice::class:
|
|
|
|
$activity->invoice_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
$activity->project_id = $entity->project_id;
|
|
|
|
$activity->vendor_id = $entity->vendor_id;
|
|
|
|
break;
|
|
|
|
case Credit::class:
|
|
|
|
$activity->credit_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
$activity->project_id = $entity->project_id;
|
|
|
|
$activity->vendor_id = $entity->vendor_id;
|
|
|
|
$activity->invoice_id = $entity->invoice_id;
|
|
|
|
break;
|
|
|
|
case Client::class:
|
|
|
|
$activity->client_id = $entity->id;
|
|
|
|
break;
|
|
|
|
case Quote::class:
|
|
|
|
$activity->quote_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
$activity->project_id = $entity->project_id;
|
|
|
|
$activity->vendor_id = $entity->vendor_id;
|
|
|
|
break;
|
|
|
|
case RecurringInvoice::class:
|
|
|
|
$activity->recurring_invoice_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
break;
|
|
|
|
case Expense::class:
|
|
|
|
$activity->expense_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
$activity->project_id = $entity->project_id;
|
|
|
|
$activity->vendor_id = $entity->vendor_id;
|
|
|
|
break;
|
|
|
|
case RecurringExpense::class:
|
|
|
|
$activity->recurring_expense_id = $entity->id;
|
|
|
|
$activity->expense_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
$activity->project_id = $entity->project_id;
|
|
|
|
$activity->vendor_id = $entity->vendor_id;
|
|
|
|
break;
|
|
|
|
case Vendor::class:
|
|
|
|
$activity->vendor_id = $entity->id;
|
|
|
|
break;
|
|
|
|
case PurchaseOrder::class:
|
|
|
|
$activity->purchase_order_id = $entity->id;
|
|
|
|
$activity->expense_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
$activity->project_id = $entity->project_id;
|
|
|
|
$activity->vendor_id = $entity->vendor_id;
|
|
|
|
case Task::class:
|
|
|
|
$activity->task_id = $entity->id;
|
|
|
|
$activity->expense_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
$activity->project_id = $entity->project_id;
|
|
|
|
$activity->vendor_id = $entity->vendor_id;
|
|
|
|
case Payment::class:
|
|
|
|
$activity->payment_id = $entity->id;
|
|
|
|
$activity->expense_id = $entity->id;
|
|
|
|
$activity->client_id = $entity->client_id;
|
|
|
|
$activity->project_id = $entity->project_id;
|
|
|
|
default:
|
|
|
|
# code...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$activity->save();
|
|
|
|
|
|
|
|
return $this->itemResponse($activity);
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|