2018-10-22 14:04:37 +02:00
|
|
|
<?php
|
2019-05-11 05:32:07 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2018-10-22 14:04:37 +02:00
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2019-06-24 02:13:53 +02:00
|
|
|
use App\DataMapper\DefaultSettings;
|
2018-10-24 05:50:15 +02:00
|
|
|
use App\Http\Requests\Company\CreateCompanyRequest;
|
2019-06-17 02:15:42 +02:00
|
|
|
use App\Http\Requests\Company\DestroyCompanyRequest;
|
|
|
|
use App\Http\Requests\Company\EditCompanyRequest;
|
2019-06-17 01:58:33 +02:00
|
|
|
use App\Http\Requests\Company\ShowCompanyRequest;
|
2019-06-17 02:15:42 +02:00
|
|
|
use App\Http\Requests\Company\StoreCompanyRequest;
|
|
|
|
use App\Http\Requests\Company\UpdateCompanyRequest;
|
2018-10-22 14:04:37 +02:00
|
|
|
use App\Http\Requests\SignupRequest;
|
2018-10-24 05:50:15 +02:00
|
|
|
use App\Jobs\Company\CreateCompany;
|
2019-06-25 07:08:07 +02:00
|
|
|
use App\Jobs\Company\CreateCompanyToken;
|
2018-10-22 14:04:37 +02:00
|
|
|
use App\Jobs\RegisterNewAccount;
|
2019-10-02 23:59:27 +02:00
|
|
|
use App\Jobs\Util\UploadAvatar;
|
2019-06-26 01:44:08 +02:00
|
|
|
use App\Models\Account;
|
2019-06-17 01:58:33 +02:00
|
|
|
use App\Models\Company;
|
2019-09-17 07:42:10 +02:00
|
|
|
use App\Models\CompanyUser;
|
2019-06-17 02:15:42 +02:00
|
|
|
use App\Repositories\CompanyRepository;
|
2019-06-26 01:44:08 +02:00
|
|
|
use App\Transformers\AccountTransformer;
|
2019-06-17 01:58:33 +02:00
|
|
|
use App\Transformers\CompanyTransformer;
|
2019-09-17 07:42:10 +02:00
|
|
|
use App\Transformers\CompanyUserTransformer;
|
2019-06-17 01:58:33 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2018-10-22 14:04:37 +02:00
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
2019-01-27 00:22:57 +01:00
|
|
|
/**
|
|
|
|
* Class CompanyController
|
|
|
|
* @package App\Http\Controllers
|
|
|
|
*/
|
2019-03-28 22:34:58 +01:00
|
|
|
class CompanyController extends BaseController
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
|
|
|
use DispatchesJobs;
|
2019-06-17 01:58:33 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
protected $entity_type = Company::class;
|
|
|
|
|
|
|
|
protected $entity_transformer = CompanyTransformer::class;
|
2018-10-22 14:04:37 +02:00
|
|
|
|
2019-06-17 02:15:42 +02:00
|
|
|
protected $company_repo;
|
2019-06-24 02:13:53 +02:00
|
|
|
|
|
|
|
public $forced_includes = [];
|
|
|
|
|
2019-01-27 00:22:57 +01:00
|
|
|
/**
|
|
|
|
* CompanyController constructor.
|
|
|
|
*/
|
2019-06-17 02:15:42 +02:00
|
|
|
public function __construct(CompanyRepository $company_repo)
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-03-28 22:34:58 +01:00
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
|
2019-06-17 02:15:42 +02:00
|
|
|
$this->company_repo = $company_repo;
|
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2019-06-17 02:15:42 +02:00
|
|
|
|
2019-06-21 00:15:53 +02:00
|
|
|
$companies = Company::whereAccountId(auth()->user()->company()->account->id);
|
2019-06-17 02:15:42 +02:00
|
|
|
|
|
|
|
return $this->listResponse($companies);
|
2018-10-22 14:04:37 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-06-17 02:15:42 +02:00
|
|
|
public function create(CreateCompanyRequest $request)
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-06-17 02:15:42 +02:00
|
|
|
|
|
|
|
$company = CompanyFactory::create(auth()->user()->company()->account->id);
|
|
|
|
|
|
|
|
return $this->itemResponse($company);
|
2018-10-22 14:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \App\Http\Requests\SignupRequest $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-06-17 02:15:42 +02:00
|
|
|
public function store(StoreCompanyRequest $request)
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-06-25 05:55:02 +02:00
|
|
|
$this->forced_includes = ['company_user'];
|
2018-10-22 14:04:37 +02:00
|
|
|
|
2019-06-20 08:20:14 +02:00
|
|
|
$company = CreateCompany::dispatchNow($request->all(), auth()->user()->company()->account);
|
2018-10-22 14:04:37 +02:00
|
|
|
|
2019-10-02 23:59:27 +02:00
|
|
|
if($request->file('logo'))
|
|
|
|
{
|
2019-10-05 02:11:04 +02:00
|
|
|
|
2019-10-02 23:59:27 +02:00
|
|
|
$path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key);
|
|
|
|
|
|
|
|
if($path){
|
2019-10-05 02:11:04 +02:00
|
|
|
|
|
|
|
$settings = $company->settings;
|
|
|
|
$settings->logo_url = config('ninja.site_url').$path;
|
|
|
|
$company->settings = $settings;
|
2019-10-02 23:59:27 +02:00
|
|
|
$company->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-24 02:13:53 +02:00
|
|
|
auth()->user()->companies()->attach($company->id, [
|
|
|
|
'account_id' => $company->account->id,
|
|
|
|
'is_owner' => 1,
|
|
|
|
'is_admin' => 1,
|
|
|
|
'is_locked' => 0,
|
|
|
|
'permissions' => json_encode([]),
|
|
|
|
'settings' => json_encode(DefaultSettings::userSettings()),
|
|
|
|
]);
|
|
|
|
|
2019-06-24 13:05:47 +02:00
|
|
|
/*
|
|
|
|
* Required dependencies
|
|
|
|
*/
|
2019-06-25 05:55:02 +02:00
|
|
|
auth()->user()->setCompany($company);
|
2019-06-24 13:05:47 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create token
|
|
|
|
*/
|
|
|
|
$company_token = CreateCompanyToken::dispatchNow($company, auth()->user());
|
|
|
|
|
2019-06-26 01:44:08 +02:00
|
|
|
//todo Need to discuss this with Hillel which is the best representation to return
|
|
|
|
//when a company is created. Do we send the entire account? Do we only send back the created CompanyUser?
|
2019-09-17 07:42:10 +02:00
|
|
|
$this->entity_transformer = CompanyUserTransformer::class;
|
|
|
|
$this->entity_type = CompanyUser::class;
|
2019-09-16 04:05:30 +02:00
|
|
|
|
2019-09-17 07:42:10 +02:00
|
|
|
//return $this->itemResponse($company);
|
|
|
|
$ct = CompanyUser::whereUserId(auth()->user()->id);
|
|
|
|
|
|
|
|
return $this->listResponse($ct);
|
2018-10-22 14:04:37 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-06-17 01:58:33 +02:00
|
|
|
public function show(ShowCompanyRequest $request, Company $company)
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-06-17 02:15:42 +02:00
|
|
|
|
|
|
|
return $this->itemResponse($company);
|
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-06-17 02:15:42 +02:00
|
|
|
public function edit(EditCompanyRequest $request, Company $company)
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-06-17 02:15:42 +02:00
|
|
|
|
|
|
|
return $this->itemResponse($company);
|
|
|
|
|
2018-10-22 14:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-06-17 02:15:42 +02:00
|
|
|
public function update(UpdateCompanyRequest $request, Company $company)
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-06-17 02:15:42 +02:00
|
|
|
$company = $this->company_repo->save($request->all(), $company);
|
2019-10-05 05:18:52 +02:00
|
|
|
\Log::error($request->all());
|
2019-10-02 23:59:27 +02:00
|
|
|
if($request->file('logo'))
|
|
|
|
{
|
2019-10-05 05:18:52 +02:00
|
|
|
\Log::error('logo present');
|
2019-10-02 23:59:27 +02:00
|
|
|
$path = UploadAvatar::dispatchNow($request->file('logo'), $company->company_key);
|
|
|
|
|
|
|
|
if($path){
|
2019-10-05 03:58:33 +02:00
|
|
|
|
2019-10-05 02:11:04 +02:00
|
|
|
$settings = $company->settings;
|
|
|
|
$settings->logo_url = config('ninja.site_url').$path;
|
|
|
|
$company->settings = $settings;
|
2019-10-02 23:59:27 +02:00
|
|
|
$company->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-17 02:15:42 +02:00
|
|
|
return $this->itemResponse($company);
|
2018-10-22 14:04:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2019-06-17 02:15:42 +02:00
|
|
|
public function destroy(DestroyCompanyRequest $request, Company $company)
|
2018-10-22 14:04:37 +02:00
|
|
|
{
|
2019-06-17 02:15:42 +02:00
|
|
|
|
|
|
|
$company->delete();
|
|
|
|
|
|
|
|
return response()->json([], 200);
|
2018-10-22 14:04:37 +02:00
|
|
|
}
|
|
|
|
}
|