1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Http/Controllers/ClientController.php

441 lines
13 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
namespace App\Http\Controllers;
2024-02-07 23:51:40 +01:00
use App\Utils\Ninja;
use App\Models\Quote;
use App\Models\Client;
use App\Models\Credit;
use App\Models\Account;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Document;
use App\Models\SystemLog;
use Postmark\PostmarkClient;
use Illuminate\Http\Response;
2023-11-26 08:41:42 +01:00
use App\Factory\ClientFactory;
use App\Filters\ClientFilters;
2024-02-07 23:51:40 +01:00
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\Uploadable;
use App\Utils\Traits\BulkOptions;
use App\Jobs\Client\UpdateTaxData;
use App\Utils\Traits\SavesDocuments;
use App\Repositories\ClientRepository;
use App\Events\Client\ClientWasCreated;
use App\Events\Client\ClientWasUpdated;
use App\Transformers\ClientTransformer;
use Illuminate\Support\Facades\Storage;
use App\Services\Template\TemplateAction;
use App\Jobs\PostMark\ProcessPostmarkWebhook;
use App\Http\Requests\Client\BulkClientRequest;
use App\Http\Requests\Client\EditClientRequest;
2023-11-26 08:41:42 +01:00
use App\Http\Requests\Client\ShowClientRequest;
2024-02-07 23:51:40 +01:00
use App\Http\Requests\Client\PurgeClientRequest;
use App\Http\Requests\Client\StoreClientRequest;
2024-02-07 23:51:40 +01:00
use App\Http\Requests\Client\CreateClientRequest;
use App\Http\Requests\Client\UpdateClientRequest;
2021-02-15 11:52:50 +01:00
use App\Http\Requests\Client\UploadClientRequest;
2024-02-07 23:51:40 +01:00
use App\Http\Requests\Client\DestroyClientRequest;
use App\Http\Requests\Client\ClientDocumentsRequest;
use App\Http\Requests\Client\ReactivateClientEmailRequest;
use App\Models\Expense;
use App\Models\Payment;
2024-02-29 22:58:56 +01:00
use App\Models\Project;
use App\Models\RecurringExpense;
use App\Models\RecurringInvoice;
2024-02-07 23:51:40 +01:00
use App\Models\Task;
use App\Transformers\DocumentTransformer;
2019-01-27 00:22:57 +01:00
/**
* Class ClientController.
2019-04-04 11:28:53 +02:00
* @covers App\Http\Controllers\ClientController
2019-01-27 00:22:57 +01:00
*/
2019-03-28 22:34:58 +01:00
class ClientController extends BaseController
{
use MakesHash;
2019-10-07 23:17:55 +02:00
use Uploadable;
use BulkOptions;
2021-02-15 11:52:50 +01:00
use SavesDocuments;
2019-04-03 04:34:28 +02:00
protected $entity_type = Client::class;
2019-03-28 22:34:58 +01:00
2019-04-03 04:34:28 +02:00
protected $entity_transformer = ClientTransformer::class;
2019-03-28 22:34:58 +01:00
2019-01-27 00:22:57 +01:00
/**
* @var ClientRepository
*/
2019-04-23 08:19:45 +02:00
protected $client_repo;
2019-01-27 00:22:57 +01:00
/**
* ClientController constructor.
2020-10-28 11:10:49 +01:00
* @param ClientRepository $client_repo
2019-01-27 00:22:57 +01:00
*/
2019-04-23 08:19:45 +02:00
public function __construct(ClientRepository $client_repo)
{
2019-03-28 22:34:58 +01:00
parent::__construct();
2019-04-23 08:19:45 +02:00
$this->client_repo = $client_repo;
}
2019-01-27 00:22:57 +01:00
/**
2023-10-26 04:57:44 +02:00
*
2020-10-28 11:10:49 +01:00
* @param ClientFilters $filters
* @return Response
2023-10-26 04:57:44 +02:00
*
2019-01-27 00:22:57 +01:00
*/
2019-03-28 11:07:45 +01:00
public function index(ClientFilters $filters)
{
set_time_limit(45);
2019-03-28 22:34:58 +01:00
$clients = Client::filter($filters);
2019-03-28 22:34:58 +01:00
return $this->listResponse($clients);
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowClientRequest $request
* @param Client $client
* @return Response
2019-10-07 06:03:01 +02:00
*
*/
public function show(ShowClientRequest $request, Client $client)
{
return $this->itemResponse($client);
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditClientRequest $request
* @param Client $client
* @return Response
2019-10-07 06:03:01 +02:00
*
*/
public function edit(EditClientRequest $request, Client $client)
{
2019-04-03 03:17:21 +02:00
return $this->itemResponse($client);
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateClientRequest $request
* @param Client $client
* @return Response
2019-10-07 06:03:01 +02:00
*
*/
public function update(UpdateClientRequest $request, Client $client)
{
if ($request->entityIsDeleted($client)) {
return $request->disallowUpdate();
}
2019-05-10 08:08:33 +02:00
2023-09-27 08:04:29 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2019-05-10 08:08:33 +02:00
$client = $this->client_repo->save($request->all(), $client);
2019-10-07 23:17:55 +02:00
$this->uploadLogo($request->file('company_logo'), $client->company, $client);
2019-10-07 13:05:06 +02:00
2023-09-27 08:04:29 +02:00
event(new ClientWasUpdated($client, $client->company, Ninja::eventVars($user ? $user->id : null)));
2020-11-03 11:04:15 +01:00
return $this->itemResponse($client->fresh());
}
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @param CreateClientRequest $request
* @return Response
2019-10-07 06:03:01 +02:00
*
*/
public function create(CreateClientRequest $request)
{
2023-09-27 08:04:29 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$client = ClientFactory::create($user->company()->id, $user->id);
2019-04-03 04:34:28 +02:00
return $this->itemResponse($client);
}
/**
* Store a newly created resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param StoreClientRequest $request
* @return Response
2019-10-07 06:03:01 +02:00
*
*/
public function store(StoreClientRequest $request)
{
2023-09-27 08:04:29 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$client = $this->client_repo->save($request->all(), ClientFactory::create($user->company()->id, $user->id));
$client->load('contacts', 'primary_contact');
2021-06-10 12:18:01 +02:00
/* Set the client country to the company if none is set */
if (! $client->country_id && strlen($client->company->settings->country_id) > 1) {
2021-12-17 12:11:36 +01:00
$client->update(['country_id' => $client->company->settings->country_id]);
2021-06-10 12:18:01 +02:00
}
2019-10-07 23:17:55 +02:00
$this->uploadLogo($request->file('company_logo'), $client->company, $client);
2023-09-27 08:04:29 +02:00
event(new ClientWasCreated($client, $client->company, Ninja::eventVars(auth()->user() ? $user->id : null)));
2020-07-08 14:02:16 +02:00
2019-04-03 04:34:28 +02:00
return $this->itemResponse($client);
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyClientRequest $request
* @param Client $client
* @return Response
2019-10-07 06:03:01 +02:00
*
2020-10-28 11:10:49 +01:00
* @throws \Exception
*/
public function destroy(DestroyClientRequest $request, Client $client)
{
$this->client_repo->delete($client);
2019-03-26 05:46:08 +01:00
return $this->itemResponse($client->fresh());
}
/**
* Perform bulk actions on the list view.
2019-10-07 06:03:01 +02:00
*
2020-10-28 11:10:49 +01:00
* @return Response
*
*/
public function bulk(BulkClientRequest $request)
{
$action = $request->action;
2021-09-15 07:58:02 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$clients = Client::withTrashed()
->company()
->whereIn('id', $request->ids)
2023-11-26 08:41:42 +01:00
->get();
2023-11-18 08:28:28 +01:00
if($action == 'template' && $user->can('view', $clients->first())) {
$hash_or_response = $request->boolean('send_email') ? 'email sent' : \Illuminate\Support\Str::uuid();
TemplateAction::dispatch(
$clients->pluck('id')->toArray(),
$request->template_id,
Client::class,
$user->id,
$user->company(),
$user->company()->db,
$hash_or_response,
$request->boolean('send_email')
);
return response()->json(['message' => $hash_or_response], 200);
}
2024-01-14 05:05:00 +01:00
2023-11-18 08:28:28 +01:00
$clients->each(function ($client) use ($action, $user) {
if ($user->can('edit', $client)) {
$this->client_repo->{$action}($client);
}
});
return $this->listResponse(Client::query()->withTrashed()->company()->whereIn('id', $request->ids));
}
2021-02-15 11:52:50 +01:00
/**
* Update the specified resource in storage.
*
* @param UploadClientRequest $request
* @param Client $client
* @return Response
*
*/
public function upload(UploadClientRequest $request, Client $client)
{
if (! $this->checkFeature(Account::FEATURE_DOCUMENTS)) {
2021-03-07 22:32:38 +01:00
return $this->featureFailure();
}
if ($request->has('documents')) {
$this->saveDocuments($request->file('documents'), $client, $request->input('is_public', true));
}
2021-02-15 11:52:50 +01:00
return $this->itemResponse($client->fresh());
}
/**
* Update the specified resource in storage.
*
2023-04-26 03:21:20 +02:00
* @param PurgeClientRequest $request
* @param Client $client
2023-07-26 04:59:36 +02:00
* @return \Illuminate\Http\JsonResponse
*
*/
public function purge(PurgeClientRequest $request, Client $client)
{
//delete all documents
$client->documents->each(function ($document) {
2023-02-16 02:36:09 +01:00
try {
2022-10-10 05:26:53 +02:00
Storage::disk(config('filesystems.default'))->delete($document->url);
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
2022-10-10 05:26:53 +02:00
nlog($e->getMessage());
}
});
//force delete the client
$this->client_repo->purge($client);
return response()->json(['message' => 'Success'], 200);
//todo add an event here using the client name as reference for purge event
}
2021-05-31 07:27:26 +02:00
2023-10-26 04:57:44 +02:00
/**
* Update the specified resource in storage.
*
* @param PurgeClientRequest $request
* @param Client $client
* @param string $mergeable_client
* @return \Illuminate\Http\JsonResponse
*
*/
2022-07-15 07:54:19 +02:00
public function merge(PurgeClientRequest $request, Client $client, string $mergeable_client)
{
2023-09-27 08:04:29 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2022-07-15 07:54:19 +02:00
$m_client = Client::withTrashed()
->where('id', $this->decodePrimaryKey($mergeable_client))
2023-09-27 08:04:29 +02:00
->where('company_id', $user->company()->id)
2022-07-15 07:54:19 +02:00
->first();
2023-02-16 02:36:09 +01:00
if (!$m_client) {
return response()->json(['message' => "Client not found"], 400);
2023-02-16 02:36:09 +01:00
}
2022-07-15 07:54:19 +02:00
if($m_client->id == $client->id)
return response()->json(['message' => "Attempting to merge the same client is not possible."], 400);
2022-07-15 07:54:19 +02:00
$merged_client = $client->service()->merge($m_client)->save();
return $this->itemResponse($merged_client);
}
2024-01-14 05:05:00 +01:00
2023-08-09 10:29:33 +02:00
/**
* Updates the client's tax data
*
* @param PurgeClientRequest $request
* @param Client $client
* @return \Illuminate\Http\JsonResponse
*/
public function updateTaxData(PurgeClientRequest $request, Client $client)
{
2023-10-26 04:57:44 +02:00
if($client->company->account->isPaid()) {
2023-10-19 02:23:13 +02:00
(new UpdateTaxData($client, $client->company))->handle();
2023-10-26 04:57:44 +02:00
}
2024-01-14 05:05:00 +01:00
2023-08-09 10:29:33 +02:00
return $this->itemResponse($client->fresh());
}
/**
* Reactivate a client email
*
* @param ReactivateClientEmailRequest $request
* @param string $bounce_id //could also be the invitationId
* @return \Illuminate\Http\JsonResponse
*/
public function reactivateEmail(ReactivateClientEmailRequest $request, string $bounce_id)
{
/** @var \App\Models\User $user */
$user = auth()->user();
2023-10-26 04:57:44 +02:00
if(stripos($bounce_id, '-') !== false) {
$log =
SystemLog::query()
->where('company_id', $user->company()->id)
->where('type_id', SystemLog::TYPE_WEBHOOK_RESPONSE)
->where('category_id', SystemLog::CATEGORY_MAIL)
->whereJsonContains('log', ['MessageID' => $bounce_id])
->orderBy('id', 'desc')
->first();
$resolved_bounce_id = false;
2023-10-26 04:57:44 +02:00
if($log && ($log?->log['ID'] ?? false)) {
$resolved_bounce_id = $log->log['ID'] ?? false;
}
2023-10-26 04:57:44 +02:00
if(!$resolved_bounce_id) {
$ppwebhook = new ProcessPostmarkWebhook([]);
$resolved_bounce_id = $ppwebhook->getBounceId($bounce_id);
}
2023-10-26 04:57:44 +02:00
if(!$resolved_bounce_id) {
return response()->json(['message' => 'Bounce ID not found'], 400);
}
$bounce_id = $resolved_bounce_id;
}
$postmark = new PostmarkClient(config('services.postmark.token'));
try {
2024-01-14 05:05:00 +01:00
2023-11-19 03:47:22 +01:00
/** @var \Postmark\Models\DynamicResponseModel $response */
$response = $postmark->activateBounce((int)$bounce_id);
2024-01-14 05:05:00 +01:00
2023-11-26 08:41:42 +01:00
if($response && $response?->Message == 'OK' && !$response->Bounce->Inactive && $response->Bounce->Email) {
2023-11-18 09:12:54 +01:00
$email = $response->Bounce->Email;
//remove email from quarantine. //@TODO
}
return response()->json(['message' => 'Success'], 200);
2023-10-26 04:57:44 +02:00
} catch(\Exception $e) {
return response()->json(['message' => $e->getMessage(), 400]);
}
}
2024-02-07 23:51:40 +01:00
2024-02-13 05:25:18 +01:00
public function documents(ClientDocumentsRequest $request, Client $client)
2024-02-07 23:51:40 +01:00
{
2024-02-13 05:25:18 +01:00
2024-02-07 23:51:40 +01:00
$this->entity_type = Document::class;
$this->entity_transformer = DocumentTransformer::class;
2024-02-13 05:25:18 +01:00
2024-02-07 23:51:40 +01:00
$documents = Document::query()
->company()
2024-02-29 22:58:56 +01:00
->whereHasMorph('documentable', [Invoice::class, Quote::class, Credit::class, Expense::class, Payment::class, Task::class, RecurringInvoice::class, RecurringExpense::class, Project::class], function ($query) use ($client) {
2024-02-07 23:51:40 +01:00
$query->where('client_id', $client->id);
})
2024-02-13 05:25:18 +01:00
->orWhereHasMorph('documentable', [Client::class], function ($query) use ($client) {
2024-02-07 23:51:40 +01:00
$query->where('id', $client->id);
});
return $this->listResponse($documents);
}
}