refactoring

This commit is contained in:
denisdulici 2019-12-22 15:58:48 +03:00
parent f9687d9138
commit e82f948353
29 changed files with 154 additions and 288 deletions

View File

@ -2,11 +2,14 @@
namespace App\Abstracts;
use App\Traits\Jobs;
use App\Traits\Relationships;
use Artisan;
use Illuminate\Database\Eloquent\Collection;
abstract class BulkAction
{
use Jobs, Relationships;
public $model = false;
public $actions = [
@ -118,44 +121,4 @@ abstract class BulkAction
Artisan::call('cache:clear');
}
public function countRelationships($model, $relationships)
{
$counter = [];
foreach ($relationships as $relationship => $text) {
if ($c = $model->$relationship()->count()) {
$counter[] = $c . ' ' . strtolower(trans_choice('general.' . $text, ($c > 1) ? 2 : 1));
}
}
return $counter;
}
/**
* Mass delete relationships with events being fired.
*
* @param $model
* @param $relationships
*
* @return void
*/
public function deleteRelationships($model, $relationships)
{
foreach ((array) $relationships as $relationship) {
if (empty($model->$relationship)) {
continue;
}
$items = $model->$relationship->all();
if ($items instanceof Collection) {
$items = $items->all();
}
foreach ((array) $items as $item) {
$item->delete();
}
}
}
}

View File

@ -7,7 +7,6 @@ use App\Models\Auth\Permission;
class Permissions extends BulkAction
{
public $model = Permission::class;
public $actions = [

View File

@ -7,7 +7,6 @@ use App\Models\Auth\Role;
class Roles extends BulkAction
{
public $model = Role::class;
public $actions = [

View File

@ -3,12 +3,11 @@
namespace App\BulkActions\Auth;
use App\Abstracts\BulkAction;
use App\Jobs\Auth\DeleteUser;
use App\Models\Auth\User;
use Artisan;
class Users extends BulkAction
{
public $model = User::class;
public $actions = [
@ -59,20 +58,11 @@ class Users extends BulkAction
$users = $this->model::find($selected);
foreach ($users as $user) {
// Can't delete yourself
if ($user->id == user()->id) {
continue;
//$this->response->errorMethodNotAllowed(trans('auth.error.self_delete'));
try {
$this->dispatch(new DeleteUser($user));
} catch (\Exception $e) {
return $e->getMessage();
}
$user->delete();
}
// Can't delete yourself
if ($user->id == app(Auth::class)->user()->id) {
$this->response->errorMethodNotAllowed(trans('auth.error.self_delete'));
}
Artisan::call('cache:clear');
}
}

View File

@ -3,11 +3,12 @@
namespace App\BulkActions\Banking;
use App\Abstracts\BulkAction;
use App\Jobs\Banking\DeleteAccount;
use App\Jobs\Banking\UpdateAccount;
use App\Models\Banking\Account;
class Accounts extends BulkAction
{
public $model = Account::class;
public $actions = [
@ -35,21 +36,10 @@ class Accounts extends BulkAction
$accounts = $this->model::find($selected);
foreach ($accounts as $account) {
if ($account->id == setting('default.account')) {
$relationships[] = strtolower(trans_choice('general.companies', 1));
}
if (empty($relationships)) {
$account->enabled = 0;
$account->save();
$message = trans('messages.success.disabled', ['type' => $account->name]);
return $this->itemResponse($account->fresh(), new Transformer(), $message);
} else {
$message = trans('messages.warning.disabled', ['name' => $account->name, 'text' => implode(', ', $relationships)]);
$this->response->errorUnauthorized($message);
try {
$this->dispatch(new UpdateAccount($account, request()->merge(['enabled' => 1])));
} catch (\Exception $e) {
return $e->getMessage();
}
}
}
@ -66,33 +56,11 @@ class Accounts extends BulkAction
$accounts = $this->model::find($selected);
foreach ($accounts as $account) {
if ($relationships = $this->getRelationships($account)) {
if ($account->id == setting('default.account')) {
$relationships[] = strtolower(trans_choice('general.companies', 1));
}
}
if (empty($relationships)) {
$account->delete();
$message = trans('messages.success.deleted', ['type' => $account->name]);
flash($message)->success();
} else {
$message = trans('messages.warning.deleted', ['name' => $account->name, 'text' => implode(', ', $relationships)]);
$this->response->errorUnauthorized($message);
try {
$this->dispatch(new DeleteAccount($account));
} catch (\Exception $e) {
return $e->getMessage();
}
}
}
protected function getRelationships($account)
{
$relationships = $this->countRelationships($account, [
'expense_transactions' => 'transactions',
'income_transacions' => 'transactions',
]);
return $relationships;
}
}

View File

@ -8,7 +8,6 @@ use App\Models\Banking\Transaction;
class Reconciliations extends BulkAction
{
public $model = Reconciliation::class;
public $actions = [

View File

@ -3,11 +3,11 @@
namespace App\BulkActions\Banking;
use App\Abstracts\BulkAction;
use App\Jobs\Banking\DeleteTransfer;
use App\Models\Banking\Transfer;
class Transfers extends BulkAction
{
public $model = Transfer::class;
public $actions = [
@ -30,9 +30,7 @@ class Transfers extends BulkAction
$transfers = $this->model::find($selected);
foreach ($transfers as $transfer) {
$this->deleteRelationships($transfer, ['expense_transaction', 'income_transaction']);
$transfer->delete();
$this->dispatch(new DeleteTransfer($transfer));
}
}
}

View File

@ -7,7 +7,6 @@ use App\Models\Common\Company;
class Companies extends BulkAction
{
public $model = Company::class;
public $actions = [

View File

@ -8,7 +8,6 @@ use App\Models\Common\Item;
class Items extends BulkAction
{
public $model = Item::class;
public $actions = [

View File

@ -4,12 +4,12 @@ namespace App\BulkActions\Expenses;
use App\Abstracts\BulkAction;
use App\Exports\Expenses\Bills as Export;
use App\Jobs\Expense\DeleteBill;
use App\Models\Expense\Bill;
use App\Models\Expense\BillHistory;
class Bills extends BulkAction
{
public $model = Bill::class;
public $actions = [
@ -67,8 +67,7 @@ class Bills extends BulkAction
$bills = $this->model::find($selected);
foreach ($bills as $bill) {
$this->deleteRelationships($bill, ['items', 'item_taxes', 'histories', 'payments', 'recurring', 'totals']);
$bill->delete();
$this->dispatch(new DeleteBill($bill));
}
}

View File

@ -9,7 +9,6 @@ use App\Models\Setting\Category;
class Payments extends BulkAction
{
public $model = Transaction::class;
public $actions = [

View File

@ -8,7 +8,6 @@ use App\Models\Common\Contact;
class Vendors extends BulkAction
{
public $model = Contact::class;
public $actions = [

View File

@ -8,7 +8,6 @@ use App\Models\Common\Contact;
class Customers extends BulkAction
{
public $model = Contact::class;
public $actions = [

View File

@ -7,6 +7,7 @@ use App\Events\Income\InvoiceCreated;
use App\Events\Income\InvoiceSent;
use App\Events\Income\PaymentReceived;
use App\Exports\Incomes\Invoices as Export;
use App\Jobs\Income\DeleteInvoice;
use App\Models\Income\Invoice;
use Date;
@ -67,8 +68,7 @@ class Invoices extends BulkAction
$invoices = $this->model::find($selected);
foreach ($invoices as $invoice) {
$this->deleteRelationships($invoice, ['items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals']);
$invoice->delete();
$this->dispatch(new DeleteInvoice($invoice));
}
}

View File

@ -9,7 +9,6 @@ use App\Models\Setting\Category;
class Revenues extends BulkAction
{
public $model = Transaction::class;
public $actions = [

View File

@ -7,7 +7,6 @@ use App\Models\Setting\Category;
class Categories extends BulkAction
{
public $model = Category::class;
public $actions = [

View File

@ -7,7 +7,6 @@ use App\Models\Setting\Currency;
class Currencies extends BulkAction
{
public $model = Currency::class;
public $actions = [

View File

@ -7,7 +7,6 @@ use App\Models\Setting\Tax;
class Taxes extends BulkAction
{
public $model = Tax::class;
public $actions = [

View File

@ -41,7 +41,7 @@ class Companies extends ApiController
$this->owner($company);
return $this->response->item($company, new Transformer());
} catch (\HttpException $e) {
} catch (\Exception $e) {
$this->response->errorUnauthorized($e->getMessage());
}
}

View File

@ -28,6 +28,8 @@ class DeleteUser extends Job
{
$this->authorize();
$this->deleteRelationships($this->user, ['dashboards', 'dashboard_widgets']);
$this->user->delete();
Artisan::call('cache:clear');

View File

@ -4,15 +4,14 @@ 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;
protected $company;
/**
* Create a new job instance.
*
@ -33,20 +32,41 @@ class CreateCompany extends Job
// Clear settings
setting()->forgetAll();
$company = Company::create($this->request->all());
$this->company = Company::create($this->request->all());
Artisan::call('user:seed', [
'user' => user()->id,
'company' => $company->id,
$this->callSeeds();
$this->createSettings();
return $this->company;
}
protected function callSeeds()
{
// Company seeds
Artisan::call('company:seed', [
'company' => $this->company->id
]);
setting()->setExtraColumns(['company_id' => $company->id]);
// Attach company to user logged in
user()->companies()->attach($this->company->id);
// User seeds
Artisan::call('user:seed', [
'user' => user()->id,
'company' => $this->company->id,
]);
}
protected function createSettings()
{
setting()->setExtraColumns(['company_id' => $this->company->id]);
if ($this->request->file('logo')) {
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $company->id);
$company_logo = $this->getMedia($this->request->file('logo'), 'settings', $this->company->id);
if ($company_logo) {
$company->attachMedia($company_logo, 'company_logo');
$this->company->attachMedia($company_logo, 'company_logo');
setting()->set('company.logo', $company_logo->id);
}
@ -63,7 +83,5 @@ class CreateCompany extends Job
setting()->save();
setting()->forgetAll();
return $company;
}
}

View File

@ -31,6 +31,13 @@ class DeleteCompany extends Job
{
$this->authorize();
$this->deleteRelationships($this->company, [
'accounts', 'bills', 'bill_histories', 'bill_items', 'bill_item_taxes', 'bill_statuses', 'bill_totals', 'categories',
'contacts', 'currencies', 'dashboards', 'dashboard_widgets', 'email_templates', 'invoices', 'invoice_histories',
'invoice_items', 'invoice_item_taxes', 'invoice_statuses', 'invoice_totals', 'items', 'modules', 'module_histories',
'reconciliations', 'recurring', 'reports', 'settings', 'taxes', 'transactions', 'transfers', 'widgets',
]);
$this->company->delete();
return true;

View File

@ -62,6 +62,11 @@ class User extends Authenticatable
return $this->hasMany('App\Models\Common\Dashboard');
}
public function dashboard_widgets()
{
return $this->hasMany('App\Models\Common\DashboardWidget');
}
/**
* Always capitalize the name when we retrieve it
*/

View File

@ -43,6 +43,11 @@ class Company extends Eloquent
return $this->hasMany('App\Models\Banking\Account');
}
public function bills()
{
return $this->hasMany('App\Models\Expense\Bill');
}
public function bill_histories()
{
return $this->hasMany('App\Models\Expense\BillHistory');
@ -53,14 +58,19 @@ class Company extends Eloquent
return $this->hasMany('App\Models\Expense\BillItem');
}
public function bill_item_taxes()
{
return $this->hasMany('App\Models\Expense\BillItemTax');
}
public function bill_statuses()
{
return $this->hasMany('App\Models\Expense\BillStatus');
}
public function bills()
public function bill_totals()
{
return $this->hasMany('App\Models\Expense\Bill');
return $this->hasMany('App\Models\Expense\BillTotal');
}
public function categories()
@ -88,6 +98,16 @@ class Company extends Eloquent
return $this->hasMany('App\Models\Common\Dashboard');
}
public function dashboard_widgets()
{
return $this->hasMany('App\Models\Common\DashboardWidget');
}
public function email_templates()
{
return $this->hasMany('App\Models\Common\EmailTemplate');
}
public function expense_transactions()
{
return $this->transactions()->where('type', 'expense');
@ -98,6 +118,11 @@ class Company extends Eloquent
return $this->transactions()->where('type', 'income');
}
public function invoices()
{
return $this->hasMany('App\Models\Income\Invoice');
}
public function invoice_histories()
{
return $this->hasMany('App\Models\Income\InvoiceHistory');
@ -108,14 +133,19 @@ class Company extends Eloquent
return $this->hasMany('App\Models\Income\InvoiceItem');
}
public function invoice_item_taxes()
{
return $this->hasMany('App\Models\Income\InvoiceItemTax');
}
public function invoice_statuses()
{
return $this->hasMany('App\Models\Income\InvoiceStatus');
}
public function invoices()
public function invoice_totals()
{
return $this->hasMany('App\Models\Income\Invoice');
return $this->hasMany('App\Models\Income\InvoiceTotal');
}
public function items()
@ -123,11 +153,31 @@ class Company extends Eloquent
return $this->hasMany('App\Models\Common\Item');
}
public function modules()
{
return $this->hasMany('App\Models\Module\Module');
}
public function module_histories()
{
return $this->hasMany('App\Models\Module\ModuleHistory');
}
public function reconciliations()
{
return $this->hasMany('App\Models\Banking\Reconciliation');
}
public function recurring()
{
return $this->hasMany('App\Models\Common\Recurring');
}
public function reports()
{
return $this->hasMany('App\Models\Common\Report');
}
public function settings()
{
return $this->hasMany('App\Models\Setting\Setting');
@ -158,6 +208,11 @@ class Company extends Eloquent
return $this->contacts()->where('type', 'vendor');
}
public function widgets()
{
return $this->hasMany('App\Models\Common\Widget');
}
public function setSettings()
{
$settings = $this->settings;

View File

@ -1,64 +0,0 @@
<?php
namespace App\Observers;
use App\Models\Common\Company as Model;
use Artisan;
class Company
{
/**
* Listen to the created event.
*
* @param Model $company
* @return void
*/
public function created(Model $company)
{
// Create seeds
Artisan::call('company:seed', [
'company' => $company->id
]);
// Check if user is logged in
if (!auth()->check()) {
return;
}
// Attach company to user
user()->companies()->attach($company->id);
}
/**
* Listen to the deleted event.
*
* @param Model $company
* @return void
*/
public function deleted(Model $company)
{
$tables = [
'accounts', 'bill_histories', 'bill_items', 'bill_statuses', 'bills', 'categories', 'contacts',
'currencies', 'invoice_histories', 'invoice_items', 'invoice_statuses', 'invoices', 'items', 'recurring',
'settings', 'taxes', 'transactions', 'transfers',
];
foreach ($tables as $table) {
$this->deleteItems($company, $table);
}
}
/**
* Delete items in batch.
*
* @param Model $company
* @param $table
* @return void
*/
protected function deleteItems($company, $table)
{
foreach ($company->$table as $item) {
$item->delete();
}
}
}

View File

@ -2,12 +2,15 @@
namespace App\Observers;
use App\Jobs\Expense\CreateBillHistory;
use App\Jobs\Income\CreateInvoiceHistory;
use App\Models\Banking\Transaction as Model;
use App\Models\Income\InvoiceHistory;
use App\Models\Expense\BillHistory;
use App\Traits\Jobs;
class Transaction
{
use Jobs;
/**
* Listen to the deleted event.
*
@ -16,64 +19,41 @@ class Transaction
*/
public function deleted(Model $transaction)
{
if (!empty($transaction->document_id)) {
if ($transaction->type == 'income') {
$this->updateInvoice($transaction);
} else {
$this->updateBill($transaction);
}
if (empty($transaction->document_id)) {
return;
}
$function = ($transaction->type == 'income') ? 'updateInvoice' : 'updateBill';
$this->$function($transaction);
}
protected function updateInvoice($transaction)
{
$invoice = $transaction->invoice;
if ($invoice->transactions->count() > 1) {
$invoice->invoice_status_code = 'partial';
} else {
$invoice->invoice_status_code = 'sent';
}
$invoice->invoice_status_code = ($invoice->transactions->count() > 1) ? 'partial' : 'sent';
$invoice->save();
$desc_amount = money((float) $transaction->amount, (string) $transaction->currency_code, true)->format();
$description = $desc_amount . ' ' . trans_choice('general.payments', 1);
// Add invoice history
InvoiceHistory::create([
'company_id' => $invoice->company_id,
'invoice_id' => $invoice->id,
'status_code' => $invoice->invoice_status_code,
'notify' => 0,
'description' => trans('messages.success.deleted', ['type' => $description]),
]);
$this->dispatch(new CreateInvoiceHistory($invoice, 0, $this->getDescription($transaction)));
}
protected function updateBill($transaction)
{
$bill = $transaction->bill;
if ($bill->transactions->count() > 1) {
$bill->bill_status_code = 'partial';
} else {
$bill->bill_status_code = 'received';
}
$bill->bill_status_code = ($bill->transactions->count() > 1) ? 'partial' : 'received';
$bill->save();
$desc_amount = money((float) $transaction->amount, (string) $transaction->currency_code, true)->format();
$description = $desc_amount . ' ' . trans_choice('general.payments', 1);
// Add bill history
BillHistory::create([
'company_id' => $bill->company_id,
'bill_id' => $bill->id,
'status_code' => $bill->bill_status_code,
'notify' => 0,
'description' => trans('messages.success.deleted', ['type' => $description]),
]);
$this->dispatch(new CreateBillHistory($bill, 0, $this->getDescription($transaction)));
}
}
protected function getDescription($transaction)
{
$amount = money((double) $transaction->amount, (string) $transaction->currency_code, true)->format();
return trans('messages.success.deleted', ['type' => $amount . ' ' . trans_choice('general.payments', 1)]);
}
}

View File

@ -1,39 +0,0 @@
<?php
namespace App\Observers;
use App\Models\Auth\User as Model;
class User
{
/**
* Listen to the deleted event.
*
* @param Model $user
* @return void
*/
public function deleted(Model $user)
{
$tables = [
'dashboards',
];
foreach ($tables as $table) {
$this->deleteItems($user, $table);
}
}
/**
* Delete items in batch.
*
* @param Model $user
* @param $table
* @return void
*/
protected function deleteItems($user, $table)
{
foreach ($user->$table as $item) {
$item->delete();
}
}
}

View File

@ -2,9 +2,7 @@
namespace App\Providers;
use App\Models\Auth\User;
use App\Models\Banking\Transaction;
use App\Models\Common\Company;
use Illuminate\Support\ServiceProvider as Provider;
class Observer extends Provider
@ -16,8 +14,6 @@ class Observer extends Provider
*/
public function boot()
{
User::observe('App\Observers\User');
Company::observe('App\Observers\Company');
Transaction::observe('App\Observers\Transaction');
}

View File

@ -12,7 +12,7 @@ trait Jobs
*/
public function dispatch($job)
{
$function = $this->getDispatchMethod();
$function = $this->getDispatchFunction();
return $function($job);
}
@ -31,7 +31,7 @@ trait Jobs
return $result;
}
public function getDispatchMethod()
public function getDispatchFunction()
{
$config = config('queue.default');