1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 16:13:20 +01:00
invoiceninja/app/Http/Controllers/CompanyController.php

714 lines
26 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;
2023-10-26 04:57:44 +02:00
use App\DataMapper\Analytics\AccountDeleted;
2020-06-05 02:24:02 +02:00
use App\DataMapper\CompanySettings;
2023-10-26 04:57:44 +02:00
use App\Http\Requests\Company\CreateCompanyRequest;
use App\Http\Requests\Company\DefaultCompanyRequest;
use App\Http\Requests\Company\DestroyCompanyRequest;
use App\Http\Requests\Company\EditCompanyRequest;
use App\Http\Requests\Company\ShowCompanyRequest;
use App\Http\Requests\Company\StoreCompanyRequest;
use App\Http\Requests\Company\UpdateCompanyRequest;
use App\Http\Requests\Company\UploadCompanyRequest;
2023-08-09 10:29:33 +02:00
use App\Jobs\Company\CompanyTaxRate;
2023-10-26 04:57:44 +02:00
use App\Jobs\Company\CreateCompany;
use App\Jobs\Company\CreateCompanyPaymentTerms;
use App\Jobs\Company\CreateCompanyTaskStatuses;
use App\Jobs\Company\CreateCompanyToken;
use App\Jobs\Mail\NinjaMailerJob;
2021-07-19 07:38:07 +02:00
use App\Jobs\Mail\NinjaMailerObject;
use App\Mail\Company\CompanyDeleted;
2023-10-26 04:57:44 +02:00
use App\Models\Account;
use App\Models\Company;
use App\Models\CompanyUser;
2019-06-17 02:15:42 +02:00
use App\Repositories\CompanyRepository;
2019-06-17 01:58:33 +02:00
use App\Transformers\CompanyTransformer;
2019-09-17 07:42:10 +02:00
use App\Transformers\CompanyUserTransformer;
2023-10-26 04:57:44 +02:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use App\Utils\Traits\Uploadable;
use Illuminate\Foundation\Bus\DispatchesJobs;
2023-10-26 04:57:44 +02:00
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Turbo124\Beacon\Facades\LightLogs;
2019-01-27 00:22:57 +01:00
/**
* Class CompanyController.
2019-01-27 00:22:57 +01:00
*/
2019-03-28 22:34:58 +01:00
class CompanyController extends BaseController
{
use DispatchesJobs;
2019-06-17 01:58:33 +02:00
use MakesHash;
2019-10-07 23:17:55 +02:00
use Uploadable;
2021-01-13 11:12:14 +01:00
use SavesDocuments;
2019-06-17 01:58:33 +02:00
protected $entity_type = Company::class;
protected $entity_transformer = CompanyTransformer::class;
2019-06-17 02:15:42 +02:00
protected $company_repo;
public $forced_includes = [];
2019-01-27 00:22:57 +01:00
/**
* CompanyController constructor.
2020-10-28 11:10:49 +01:00
* @param CompanyRepository $company_repo
2019-01-27 00:22:57 +01:00
*/
2019-06-17 02:15:42 +02:00
public function __construct(CompanyRepository $company_repo)
{
2019-03-28 22:34:58 +01:00
parent::__construct();
2019-06-17 02:15:42 +02:00
$this->company_repo = $company_repo;
2021-08-27 01:56:42 +02:00
$this->middleware('password_protected')->only(['destroy']);
}
/**
* Display a listing of the resource.
*
2020-10-28 11:10:49 +01:00
* @return Response
2019-10-07 06:03:01 +02:00
*
* @OA\Get(
* path="/api/v1/companies",
* operationId="getCompanies",
* tags={"companies"},
* summary="Gets a list of companies",
* description="Lists companies, search and filters allow fine grained lists to be generated.
2023-06-12 13:12:33 +02:00
* Query parameters can be added to performed more fine grained filtering of the companies, these are handled by the CompanyFilters class which defines the methods available",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2019-10-07 06:03:01 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A list of companies",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:03:01 +02: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/Company"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
* ),
* @OA\Response(
* response="default",
2019-10-07 06:03:01 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
public function index()
{
2023-06-12 13:12:33 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$companies = Company::whereAccountId($user->company()->account->id);
2019-06-17 02:15:42 +02:00
return $this->listResponse($companies);
}
/**
* Show the form for creating a new resource.
*
2020-10-28 11:10:49 +01:00
* @param CreateCompanyRequest $request
* @return Response
2019-10-07 06:03:01 +02:00
*
*
*
2019-10-07 06:03:01 +02:00
* @OA\Get(
* path="/api/v1/companies/create",
* operationId="getCompaniesCreate",
* tags={"companies"},
* summary="Gets a new blank company object",
* description="Returns a blank object with default values",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2019-10-07 06:03:01 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="A blank company object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:03:01 +02: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/Company"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:03:01 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2019-06-17 02:15:42 +02:00
public function create(CreateCompanyRequest $request)
{
2023-06-12 13:12:33 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
2024-01-14 05:05:00 +01:00
$company_factory = new \App\Factory\CompanyFactory();
2023-06-12 13:12:33 +02:00
$company = $company_factory->create($user->company()->account->id);
2019-06-17 02:15:42 +02:00
return $this->itemResponse($company);
}
/**
* Store a newly created resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param StoreCompanyRequest $request
* @return Response
2019-10-07 06:03:01 +02:00
*
*
* @OA\Post(
* path="/api/v1/companies",
* operationId="storeCompany",
* tags={"companies"},
* summary="Adds a company",
* description="Adds an company to the system",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2019-10-07 06:03:01 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Response(
* response=200,
* description="Returns the saved company object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:03:01 +02: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/Company"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:03:01 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2019-06-17 02:15:42 +02:00
public function store(StoreCompanyRequest $request)
{
$this->forced_includes = ['company_user'];
2023-06-12 13:12:33 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$company = (new CreateCompany($request->all(), $user->company()->account))->handle();
(new CreateCompanyPaymentTerms($company, $user))->handle();
(new CreateCompanyTaskStatuses($company, $user))->handle();
$company = $this->company_repo->save($request->all(), $company);
2019-10-07 23:17:55 +02:00
$this->uploadLogo($request->file('company_logo'), $company, $company);
2019-10-02 23:59:27 +02:00
2023-06-12 13:12:33 +02:00
$user->companies()->attach($company->id, [
'account_id' => $company->account->id,
'is_owner' => 1,
'is_admin' => 1,
'is_locked' => 0,
'permissions' => '',
'settings' => null,
2020-06-05 02:24:02 +02:00
'notifications' => CompanySettings::notificationDefaults(),
]);
if ($company->account->companies()->where('is_large', 1)->exists()) {
$company->account->companies()->update(['is_large' => true]);
}
2019-06-24 13:05:47 +02:00
/*
* Required dependencies
*/
2023-06-12 13:12:33 +02:00
$user->setCompany($company);
2019-06-24 13:05:47 +02:00
/*
* Create token
*/
2021-10-26 11:39:33 +02:00
$user_agent = request()->has('token_name') ? request()->input('token_name') : request()->server('HTTP_USER_AGENT');
$company_token = (new CreateCompanyToken($company, auth()->user(), $user_agent))->handle();
2019-09-17 07:42:10 +02:00
$this->entity_transformer = CompanyUserTransformer::class;
$this->entity_type = CompanyUser::class;
$ct = CompanyUser::whereUserId(auth()->user()->id)->whereCompanyId($company->id);
2019-09-17 07:42:10 +02:00
return $this->listResponse($ct);
}
/**
* Display the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param ShowCompanyRequest $request
* @param Company $company
* @return Response
2019-10-07 06:03:01 +02:00
*
*
* @OA\Get(
* path="/api/v1/companies/{id}",
* operationId="showCompany",
* tags={"companies"},
* summary="Shows an company",
* description="Displays an company by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2019-10-07 06:03:01 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Company Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the company object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:03:01 +02: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/Company"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:03:01 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2019-06-17 01:58:33 +02:00
public function show(ShowCompanyRequest $request, Company $company)
{
2019-06-17 02:15:42 +02:00
return $this->itemResponse($company);
}
/**
* Show the form for editing the specified resource.
*
2020-10-28 11:10:49 +01:00
* @param EditCompanyRequest $request
* @param Company $company
* @return Response
2019-10-07 06:03:01 +02:00
*
*
2019-10-07 06:03:01 +02:00
* @OA\Get(
* path="/api/v1/companies/{id}/edit",
* operationId="editCompany",
* tags={"companies"},
* summary="Shows an company for editting",
* description="Displays an company by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2019-10-07 06:03:01 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Company Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the company object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:03:01 +02: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/Company"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:03:01 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2019-06-17 02:15:42 +02:00
public function edit(EditCompanyRequest $request, Company $company)
{
return $this->itemResponse($company);
}
/**
* Update the specified resource in storage.
*
2020-10-28 11:10:49 +01:00
* @param UpdateCompanyRequest $request
* @param Company $company
* @return Response
2019-10-07 06:03:01 +02:00
*
*
2019-10-07 06:03:01 +02:00
* @OA\Put(
* path="/api/v1/companies/{id}",
* operationId="updateCompany",
* tags={"companies"},
* summary="Updates an company",
* description="Handles the updating of an company by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2019-10-07 06:03:01 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Company Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the company object",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:03:01 +02: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/Company"),
* ),
* @OA\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:03:01 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2019-06-17 02:15:42 +02:00
public function update(UpdateCompanyRequest $request, Company $company)
2019-10-09 14:21:21 +02:00
{
if ($request->hasFile('company_logo') || (is_array($request->input('settings')) && ! array_key_exists('company_logo', $request->input('settings')))) {
$this->removeLogo($company);
}
2019-06-17 02:15:42 +02:00
$company = $this->company_repo->save($request->all(), $company);
2019-10-07 06:03:01 +02:00
if ($request->has('documents')) {
$this->saveDocuments($request->input('documents'), $company, $request->input('is_public', true));
}
2023-10-26 04:57:44 +02:00
if($request->has('e_invoice_certificate') && !is_null($request->file("e_invoice_certificate"))) {
2023-05-15 13:20:47 +02:00
$company->e_invoice_certificate = base64_encode($request->file("e_invoice_certificate")->get());
2023-05-31 06:36:35 +02:00
$settings = $company->settings;
$settings->enable_e_invoice = true;
2024-01-14 05:05:00 +01:00
2023-05-31 06:36:35 +02:00
$company->save();
2024-01-14 05:05:00 +01:00
2023-05-15 13:20:47 +02:00
}
2019-10-07 23:17:55 +02:00
$this->uploadLogo($request->file('company_logo'), $company, $company);
2019-10-02 23:59:27 +02:00
2019-06-17 02:15:42 +02:00
return $this->itemResponse($company);
}
/**
* Remove the specified resource from storage.
*
2020-10-28 11:10:49 +01:00
* @param DestroyCompanyRequest $request
* @param Company $company
* @return Response
2019-10-07 06:03:01 +02:00
*
*
2020-10-28 11:10:49 +01:00
* @throws \Exception
2019-10-07 06:03:01 +02:00
* @OA\Delete(
* path="/api/v1/companies/{id}",
* operationId="deleteCompany",
* tags={"companies"},
* summary="Deletes a company",
* description="Handles the deletion of an company by id",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2019-10-07 06:03:01 +02:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Company Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns a HTTP status",
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
2019-10-07 06:03:01 +02: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\Response(
* response=422,
* description="Validation error",
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
*
* ),
* @OA\Response(
* response="default",
2019-10-07 06:03:01 +02:00
* description="Unexpected Error",
* @OA\JsonContent(ref="#/components/schemas/Error"),
* ),
* )
*/
2019-06-17 02:15:42 +02:00
public function destroy(DestroyCompanyRequest $request, Company $company)
{
if (Ninja::isHosted() && config('ninja.ninja_default_company_id') == $company->id) {
return response()->json(['message' => 'Cannot purge this company'], 400);
}
$company_count = $company->account->companies->count();
$account = $company->account;
2021-07-02 08:36:14 +02:00
$account_key = $account->key;
if ($company_count == 1) {
$company->company_users->each(function ($company_user) {
$company_user->user->forceDelete();
$company_user->forceDelete();
});
2023-03-15 23:23:24 +01:00
try {
Storage::disk(config('filesystems.default'))->deleteDirectory($company->company_key);
2023-03-18 08:24:56 +01:00
} catch(\Exception $e) {
2023-03-15 23:23:24 +01:00
}
$account->delete();
2020-10-07 05:56:35 +02:00
if (Ninja::isHosted()) {
2023-02-04 22:41:28 +01:00
\Modules\Admin\Jobs\Account\NinjaDeletedAccount::dispatch($account_key, $request->all(), auth()->user()->email);
}
2021-07-02 08:36:14 +02:00
2020-10-07 05:56:35 +02:00
LightLogs::create(new AccountDeleted())
->increment()
2023-03-15 23:23:24 +01:00
->batch();
} else {
$company_id = $company->id;
$company->company_users->each(function ($company_user) {
$company_user->forceDelete();
});
2021-07-19 07:38:07 +02:00
$other_company = $company->account->companies->where('id', '!=', $company->id)->first();
2024-01-14 05:05:00 +01:00
$nmo = new NinjaMailerObject();
$nmo->mailable = new CompanyDeleted($company->present()->name, auth()->user(), $company->account, $company->settings);
$nmo->company = $other_company;
$nmo->settings = $other_company->settings;
$nmo->to_user = auth()->user();
(new NinjaMailerJob($nmo, true))->handle();
2023-03-15 23:23:24 +01:00
try {
Storage::disk(config('filesystems.default'))->deleteDirectory($company->company_key);
} catch(\Exception $e) {
}
$company->delete();
//If we are deleting the default companies, we'll need to make a new company the default.
if ($account->default_company_id == $company_id) {
2024-01-14 05:05:00 +01:00
2023-08-04 08:40:44 +02:00
/** @var \App\Models\Company $new_default_company **/
$new_default_company = Company::whereAccountId($account->id)->first();
$account->default_company_id = $new_default_company->id;
$account->save();
}
}
2020-06-24 23:44:25 +02:00
2021-01-24 23:24:13 +01:00
return response()->json(['message' => ctrans('texts.success')], 200);
}
2021-02-15 11:52:50 +01:00
/**
* Update the specified resource in storage.
*
* @param UploadCompanyRequest $request
2023-08-04 08:40:44 +02:00
* @param Company $company
2021-02-15 11:52:50 +01:00
* @return Response
*
*
*
* @OA\Put(
* path="/api/v1/companies/{id}/upload",
* operationId="uploadCompanies",
* tags={"companies"},
* summary="Uploads a document to a company",
* description="Handles the uploading of a document to a company",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
2021-02-15 11:52:50 +01:00
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="id",
* in="path",
* description="The Company Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the client object",
* @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\JsonContent(ref="#/components/schemas/Company"),
* ),
* @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 upload(UploadCompanyRequest $request, Company $company)
{
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'), $company, $request->input('is_public', true));
}
2021-02-15 11:52:50 +01:00
return $this->itemResponse($company->fresh());
}
2021-07-14 09:15:59 +02:00
/**
* Update the specified resource in storage.
*
2023-08-04 08:40:44 +02:00
* @param DefaultCompanyRequest $request
2023-04-26 03:21:20 +02:00
* @param Company $company
* @return Response
*
*
*
* @OA\Post(
* path="/api/v1/companies/{company}/default",
* operationId="setDefaultCompany",
* tags={"companies"},
* summary="Sets the company as the default company.",
* description="Sets the company as the default company.",
2023-02-10 10:21:10 +01:00
* @OA\Parameter(ref="#/components/parameters/X-API-TOKEN"),
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
* @OA\Parameter(ref="#/components/parameters/include"),
* @OA\Parameter(
* name="company",
* in="path",
* description="The Company Hashed ID",
* example="D2J234DFA",
* required=true,
* @OA\Schema(
* type="string",
* format="string",
* ),
* ),
* @OA\Response(
* response=200,
* description="Returns the company object",
* @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\JsonContent(ref="#/components/schemas/Company"),
* ),
* @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 default(DefaultCompanyRequest $request, Company $company)
{
$account = $company->account;
$account->default_company_id = $company->id;
$account->save();
return $this->itemResponse($company->fresh());
}
2023-08-09 10:29:33 +02:00
public function updateOriginTaxData(DefaultCompanyRequest $request, Company $company)
{
2024-01-14 05:05:00 +01:00
2023-10-26 04:57:44 +02:00
if($company->settings->country_id == "840" && !$company?->account->isFreeHostedClient()) {
2023-08-09 10:29:33 +02:00
try {
(new CompanyTaxRate($company))->handle();
} catch(\Exception $e) {
return response()->json(['message' => 'There was a problem updating the tax rates. Please try again.'], 400);
}
2023-10-26 04:57:44 +02:00
} else {
2023-08-09 10:29:33 +02:00
return response()->json(['message' => 'Tax configuration not available due to settings / plan restriction.'], 400);
2023-10-26 04:57:44 +02:00
}
2023-08-09 10:29:33 +02:00
return $this->itemResponse($company->fresh());
}
2023-09-11 03:22:10 +02:00
public function logo()
{
/** @var \App\Models\User $user */
$user = auth()->user();
$company = $user->company();
$logo = strlen($company->settings->company_logo) > 5 ? $company->settings->company_logo : 'https://pdf.invoicing.co/favicon-v2.png';
$headers = ['Content-Disposition' => 'inline'];
2024-01-14 05:05:00 +01:00
2023-10-26 04:57:44 +02:00
return response()->streamDownload(function () use ($logo) {
2023-09-11 03:22:10 +02:00
echo @file_get_contents($logo);
}, 'logo.png', $headers);
}
}