v2 first commit

This commit is contained in:
denisdulici
2019-11-16 10:21:14 +03:00
parent 5b23e9c2c4
commit 6d50fa8442
3075 changed files with 3451681 additions and 65594 deletions

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Company;
use App\Traits\Users;
use Artisan;
class CreateCompany extends Job
{
use Users;
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Company
*/
public function handle()
{
// Clear settings
setting()->forgetAll();
$company = Company::create($this->request->all());
Artisan::call('user:seed', [
'user' => user()->id,
'company' => $company->id,
]);
setting()->setExtraColumns(['company_id' => $company->id]);
if ($this->request->file('logo')) {
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $company->id);
if ($company_logo) {
$company->attachMedia($company_logo, 'company_logo');
setting()->set('company.logo', $company_logo->id);
}
}
// Create settings
setting()->set([
'company.name' => $this->request->get('name'),
'company.email' => $this->request->get('email'),
'company.address' => $this->request->get('address'),
'default.currency' => $this->request->get('currency'),
'default.locale' => $this->request->get('locale', 'en-GB'),
]);
setting()->save();
setting()->forgetAll();
return $company;
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Auth\User;
use App\Models\Common\Contact;
class CreateContact extends Job
{
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Contact
*/
public function handle()
{
if (!empty($this->request->input('create_user'))) {
$this->createUser();
}
$contact = Contact::create($this->request->all());
return $contact;
}
public function createUser()
{
// Check if user exist
if ($user = User::where('email', $this->request['email'])->first()) {
$message = trans('messages.error.customer', ['name' => $user->name]);
throw new \Exception($message);
}
$data = $this->request->all();
$data['locale'] = setting('default.locale', 'en-GB');
$user = User::create($data);
$user->roles()->attach(['3']);
$user->companies()->attach([session('company_id')]);
// St user id to request
$this->request['user_id'] = $user->id;
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Item;
class CreateItem extends Job
{
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Item
*/
public function handle()
{
$item = Item::create($this->request->all());
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
$item->attachMedia($media, 'picture');
}
return $item;
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Report;
class CreateReport extends Job
{
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Report
*/
public function handle()
{
$report = Report::create($this->request->all());
return $report;
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Company;
use App\Traits\Users;
class DeleteCompany extends Job
{
use Users;
protected $company;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($company)
{
$this->company = $company;
}
/**
* Execute the job.
*
* @return Company
*/
public function handle()
{
$this->authorize();
$this->company->delete();
return true;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
// Can't delete active company
if ($this->company->id == session('company_id')) {
$message = trans('companies.error.delete_active');
throw new \Exception($message);
}
// Check if user can access company
if (!$this->isUserCompany($this->company->id)) {
$message = trans('companies.error.not_user_company');
throw new \Exception($message);
}
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
class DeleteContact extends Job
{
protected $contact;
/**
* Create a new job instance.
*
* @param $contact
*/
public function __construct($contact)
{
$this->contact = $contact;
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
{
$this->authorize();
$this->contact->delete();
return true;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
if ($relationships = $this->getRelationships()) {
$message = trans('messages.warning.deleted', ['name' => $this->contact->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function getRelationships()
{
$rels = [
'transactions' => 'transactions',
];
if ($this->contact->type == 'customer') {
$rels['invoices'] = 'invoices';
} else {
$rels['bills'] = 'bills';
}
return $this->countRelationships($this->contact, $rels);
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
class DeleteItem extends Job
{
protected $item;
/**
* Create a new job instance.
*
* @param $item
*/
public function __construct($item)
{
$this->item = $item;
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
{
$this->authorize();
$this->item->delete();
return true;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
if ($relationships = $this->getRelationships()) {
$message = trans('messages.warning.deleted', ['name' => $this->item->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function getRelationships()
{
$rels = [
'invoice_items' => 'invoices',
'bill_items' => 'bills',
];
return $this->countRelationships($this->item, $rels);
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
class DeleteReport extends Job
{
protected $report;
/**
* Create a new job instance.
*
* @param $report
*/
public function __construct($report)
{
$this->report = $report;
}
/**
* Execute the job.
*
* @return boolean|Exception
*/
public function handle()
{
$this->report->delete();
return true;
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Company;
use App\Traits\Users;
class UpdateCompany extends Job
{
use Users;
protected $company;
protected $request;
/**
* Create a new job instance.
*
* @param $company
* @param $request
*/
public function __construct($company, $request)
{
$this->company = $company;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Company
*/
public function handle()
{
// Check if user can access company
$this->authorize();
// Update company
$this->company->update($this->request->all());
// Clear current settings
setting()->forgetAll();
// Load settings based on the given company
setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->load(true);
if ($this->request->has('name')) {
setting()->set('company.name', $this->request->get('name'));
}
if ($this->request->has('email')) {
setting()->set('company.email', $this->request->get('email'));
}
if ($this->request->has('address')) {
setting()->set('company.address', $this->request->get('address'));
}
if ($this->request->has('currency')) {
setting()->set('default.currency', $this->request->get('currency'));
}
if ($this->request->has('locale')) {
setting()->set('default.locale', $this->request->get('locale'));
}
if ($this->request->file('logo')) {
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id);
if ($company_logo) {
$this->company->attachMedia($company_logo, 'company_logo');
setting()->set('company.logo', $company_logo->id);
}
}
setting()->save();
setting()->forgetAll();
return $this->company;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
// Check if user can access company
if (!$this->isUserCompany($this->company->id)) {
$message = trans('companies.error.not_user_company');
throw new \Exception($message);
}
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Contact;
class UpdateContact extends Job
{
protected $contact;
protected $request;
/**
* Create a new job instance.
*
* @param $contact
* @param $request
*/
public function __construct($contact, $request)
{
$this->contact = $contact;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Contact
*/
public function handle()
{
$this->authorize();
if (!empty($this->request->input('create_user'))) {
$this->createUser();
}
$this->contact->update($this->request->all());
return $this->contact;
}
/**
* Determine if this action is applicable.
*
* @return void
*/
public function authorize()
{
if (($this->request['enabled'] == 0) && ($relationships = $this->getRelationships())) {
$message = trans('messages.warning.disabled', ['name' => $this->contact->name, 'text' => implode(', ', $relationships)]);
throw new \Exception($message);
}
}
public function createUser()
{
// Check if user exist
if ($user = User::where('email', $this->request['email'])->first()) {
$message = trans('messages.error.customer', ['name' => $user->name]);
throw new \Exception($message);
}
$data = $this->request->all();
$data['locale'] = setting('default.locale', 'en-GB');
$user = User::create($data);
$user->roles()->attach(['3']);
$user->companies()->attach([session('company_id')]);
// St user id to request
$this->request['user_id'] = $user->id;
}
public function getRelationships()
{
$rels = [
'transactions' => 'transactions',
];
if ($this->contact->type == 'customer') {
$rels['invoices'] = 'invoices';
} else {
$rels['bills'] = 'bills';
}
return $this->countRelationships($this->contact, $rels);
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Item;
class UpdateItem extends Job
{
protected $item;
protected $request;
/**
* Create a new job instance.
*
* @param $item
* @param $request
*/
public function __construct($item, $request)
{
$this->item = $item;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Item
*/
public function handle()
{
$this->item->update($this->request->all());
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items');
$this->item->attachMedia($media, 'picture');
}
return $this->item;
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Models\Common\Report;
class UpdateReport extends Job
{
protected $report;
protected $request;
/**
* Create a new job instance.
*
* @param $report
* @param $request
*/
public function __construct($report, $request)
{
$this->report = $report;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Report
*/
public function handle()
{
$this->report->update($this->request->all());
return $this->report;
}
}