Merge pull request #1507 from denisdulici/master

Added database transaction to jobs
This commit is contained in:
Denis Duliçi 2020-06-26 13:43:08 +03:00 committed by GitHub
commit 634c807bc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
57 changed files with 606 additions and 469 deletions

View File

@ -7,6 +7,8 @@ use App\Models\Auth\Permission;
class CreatePermission extends Job class CreatePermission extends Job
{ {
protected $permission;
protected $request; protected $request;
/** /**
@ -26,8 +28,10 @@ class CreatePermission extends Job
*/ */
public function handle() public function handle()
{ {
$permission = Permission::create($this->request->all()); \DB::transaction(function () {
$this->permission = Permission::create($this->request->all());
});
return $permission; return $this->permission;
} }
} }

View File

@ -7,6 +7,8 @@ use App\Models\Auth\Role;
class CreateRole extends Job class CreateRole extends Job
{ {
protected $role;
protected $request; protected $request;
/** /**
@ -26,12 +28,14 @@ class CreateRole extends Job
*/ */
public function handle() public function handle()
{ {
$role = Role::create($this->request->input()); \DB::transaction(function () {
$this->role = Role::create($this->request->input());
if ($this->request->has('permissions')) { if ($this->request->has('permissions')) {
$role->permissions()->attach($this->request->get('permissions')); $this->role->permissions()->attach($this->request->get('permissions'));
} }
});
return $role; return $this->role;
} }
} }

View File

@ -8,6 +8,8 @@ use Artisan;
class CreateUser extends Job class CreateUser extends Job
{ {
protected $user;
protected $request; protected $request;
/** /**
@ -27,41 +29,44 @@ class CreateUser extends Job
*/ */
public function handle() public function handle()
{ {
$user = User::create($this->request->input()); \DB::transaction(function () {
$this->user = User::create($this->request->input());
// Upload picture // Upload picture
if ($this->request->file('picture')) { if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'users'); $media = $this->getMedia($this->request->file('picture'), 'users');
$user->attachMedia($media, 'picture'); $this->user->attachMedia($media, 'picture');
} }
if ($this->request->has('dashboards')) { if ($this->request->has('dashboards')) {
$user->dashboards()->attach($this->request->get('dashboards')); $this->user->dashboards()->attach($this->request->get('dashboards'));
} }
if ($this->request->has('permissions')) { if ($this->request->has('permissions')) {
$user->permissions()->attach($this->request->get('permissions')); $this->user->permissions()->attach($this->request->get('permissions'));
} }
if ($this->request->has('roles')) { if ($this->request->has('roles')) {
$user->roles()->attach($this->request->get('roles')); $this->user->roles()->attach($this->request->get('roles'));
} }
if ($this->request->has('companies')) { if ($this->request->has('companies')) {
$user->companies()->attach($this->request->get('companies')); $this->user->companies()->attach($this->request->get('companies'));
} }
// Add User Dashboard if (empty($this->user->companies)) {
if (!empty($user->companies)) { return;
foreach ($user->companies as $company) { }
foreach ($this->user->companies as $company) {
Artisan::call('user:seed', [ Artisan::call('user:seed', [
'user' => $user->id, 'user' => $this->user->id,
'company' => $company->id, 'company' => $company->id,
]); ]);
} }
} });
return $user; return $this->user;
} }
} }

View File

@ -25,7 +25,9 @@ class DeletePermission extends Job
*/ */
public function handle() public function handle()
{ {
$this->permission->delete(); \DB::transaction(function () {
$this->permission->delete();
});
return true; return true;
} }

View File

@ -25,9 +25,11 @@ class DeleteRole extends Job
*/ */
public function handle() public function handle()
{ {
$this->role->delete(); \DB::transaction(function () {
$this->role->delete();
$this->role->flushCache(); $this->role->flushCache();
});
return true; return true;
} }

View File

@ -27,9 +27,11 @@ class DeleteUser extends Job
{ {
$this->authorize(); $this->authorize();
$this->user->delete(); \DB::transaction(function () {
$this->user->delete();
$this->user->flushCache(); $this->user->flushCache();
});
return true; return true;
} }

View File

@ -30,7 +30,9 @@ class UpdatePermission extends Job
*/ */
public function handle() public function handle()
{ {
$this->permission->update($this->request->all()); \DB::transaction(function () {
$this->permission->update($this->request->all());
});
return $this->permission; return $this->permission;
} }

View File

@ -30,11 +30,13 @@ class UpdateRole extends Job
*/ */
public function handle() public function handle()
{ {
$this->role->update($this->request->all()); \DB::transaction(function () {
$this->role->update($this->request->all());
if ($this->request->has('permissions')) { if ($this->request->has('permissions')) {
$this->role->permissions()->sync($this->request->get('permissions')); $this->role->permissions()->sync($this->request->get('permissions'));
} }
});
return $this->role; return $this->role;
} }

View File

@ -38,24 +38,24 @@ class UpdateUser extends Job
unset($this->request['password_confirmation']); unset($this->request['password_confirmation']);
} }
$this->user->update($this->request->input()); \DB::transaction(function () {
$this->user->update($this->request->input());
// Upload picture // Upload picture
if ($this->request->file('picture')) { if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'users'); $media = $this->getMedia($this->request->file('picture'), 'users');
$this->user->attachMedia($media, 'picture'); $this->user->attachMedia($media, 'picture');
} }
// Sync roles if ($this->request->has('roles')) {
if ($this->request->has('roles')) { $this->user->roles()->sync($this->request->get('roles'));
$this->user->roles()->sync($this->request->get('roles')); }
}
// Sync companies if ($this->request->has('companies')) {
if ($this->request->has('companies')) { $this->user->companies()->sync($this->request->get('companies'));
$this->user->companies()->sync($this->request->get('companies')); }
} });
return $this->user; return $this->user;
} }

View File

@ -7,6 +7,8 @@ use App\Models\Banking\Account;
class CreateAccount extends Job class CreateAccount extends Job
{ {
protected $account;
protected $request; protected $request;
/** /**
@ -26,14 +28,16 @@ class CreateAccount extends Job
*/ */
public function handle() public function handle()
{ {
$account = Account::create($this->request->all()); \DB::transaction(function () {
$this->account = Account::create($this->request->all());
// Set default account // Set default account
if ($this->request['default_account']) { if ($this->request['default_account']) {
setting()->set('default.account', $account->id); setting()->set('default.account', $this->account->id);
setting()->save(); setting()->save();
} }
});
return $account; return $this->account;
} }
} }

View File

@ -17,6 +17,8 @@ class CreateDocumentTransaction extends Job
protected $request; protected $request;
protected $transaction;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -40,20 +42,22 @@ class CreateDocumentTransaction extends Job
$this->checkAmount(); $this->checkAmount();
$transaction = $this->dispatch(new CreateTransaction($this->request)); \DB::transaction(function () {
$this->transaction = $this->dispatch(new CreateTransaction($this->request));
// Upload attachment // Upload attachment
if ($this->request->file('attachment')) { if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'transactions'); $media = $this->getMedia($this->request->file('attachment'), 'transactions');
$transaction->attachMedia($media, 'attachment'); $this->transaction->attachMedia($media, 'attachment');
} }
$this->model->save(); $this->model->save();
$this->createHistory($transaction); $this->createHistory();
});
return $transaction; return $this->transaction;
} }
protected function prepareRequest() protected function prepareRequest()
@ -137,9 +141,9 @@ class CreateDocumentTransaction extends Job
return true; return true;
} }
protected function createHistory($transaction) protected function createHistory()
{ {
$history_desc = money((double) $transaction->amount, (string) $transaction->currency_code, true)->format() . ' ' . trans_choice('general.payments', 1); $history_desc = money((double) $this->transaction->amount, (string) $this->transaction->currency_code, true)->format() . ' ' . trans_choice('general.payments', 1);
if ($this->model instanceof Invoice) { if ($this->model instanceof Invoice) {
$this->dispatch(new CreateInvoiceHistory($this->model, 0, $history_desc)); $this->dispatch(new CreateInvoiceHistory($this->model, 0, $history_desc));

View File

@ -8,6 +8,8 @@ use App\Models\Banking\Transaction;
class CreateReconciliation extends Job class CreateReconciliation extends Job
{ {
protected $reconciliation;
protected $request; protected $request;
/** /**
@ -27,32 +29,34 @@ class CreateReconciliation extends Job
*/ */
public function handle() public function handle()
{ {
$reconcile = $this->request->get('reconcile'); \DB::transaction(function () {
$transactions = $this->request->get('transactions'); $reconcile = $this->request->get('reconcile');
$transactions = $this->request->get('transactions');
$reconciliation = Reconciliation::create([ $this->reconciliation = Reconciliation::create([
'company_id' => $this->request['company_id'], 'company_id' => $this->request['company_id'],
'account_id' => $this->request->get('account_id'), 'account_id' => $this->request->get('account_id'),
'started_at' => $this->request->get('started_at'), 'started_at' => $this->request->get('started_at'),
'ended_at' => $this->request->get('ended_at'), 'ended_at' => $this->request->get('ended_at'),
'closing_balance' => $this->request->get('closing_balance'), 'closing_balance' => $this->request->get('closing_balance'),
'reconciled' => $reconcile ? 1 : 0, 'reconciled' => $reconcile ? 1 : 0,
]); ]);
if ($transactions) { if ($transactions) {
foreach ($transactions as $key => $value) { foreach ($transactions as $key => $value) {
if (empty($value)) { if (empty($value)) {
continue; continue;
}
$t = explode('_', $key);
$transaction = Transaction::find($t[1]);
$transaction->reconciled = 1;
$transaction->save();
} }
$t = explode('_', $key);
$transaction = Transaction::find($t[1]);
$transaction->reconciled = 1;
$transaction->save();
} }
} });
return $reconciliation; return $this->reconciliation;
} }
} }

View File

@ -9,10 +9,10 @@ use App\Models\Banking\Transaction;
class CreateTransaction extends Job class CreateTransaction extends Job
{ {
protected $request;
protected $transaction; protected $transaction;
protected $request;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -32,17 +32,19 @@ class CreateTransaction extends Job
{ {
event(new TransactionCreating($this->request)); event(new TransactionCreating($this->request));
$this->transaction = Transaction::create($this->request->all()); \DB::transaction(function () {
$this->transaction = Transaction::create($this->request->all());
// Upload attachment // Upload attachment
if ($this->request->file('attachment')) { if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'transactions'); $media = $this->getMedia($this->request->file('attachment'), 'transactions');
$this->transaction->attachMedia($media, 'attachment'); $this->transaction->attachMedia($media, 'attachment');
} }
// Recurring // Recurring
$this->transaction->createRecurring(); $this->transaction->createRecurring();
});
event(new TransactionCreated($this->transaction)); event(new TransactionCreated($this->transaction));

View File

@ -11,6 +11,8 @@ use App\Models\Setting\Currency;
class CreateTransfer extends Job class CreateTransfer extends Job
{ {
protected $transfer;
protected $request; protected $request;
/** /**
@ -30,76 +32,78 @@ class CreateTransfer extends Job
*/ */
public function handle() public function handle()
{ {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); \DB::transaction(function () {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
$expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first(); $expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first();
$income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first(); $income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first();
$expense_transaction = Transaction::create([ $expense_transaction = Transaction::create([
'company_id' => $this->request['company_id'], 'company_id' => $this->request['company_id'],
'type' => 'expense', 'type' => 'expense',
'account_id' => $this->request->get('from_account_id'), 'account_id' => $this->request->get('from_account_id'),
'paid_at' => $this->request->get('transferred_at'), 'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $expense_currency_code, 'currency_code' => $expense_currency_code,
'currency_rate' => $currencies[$expense_currency_code], 'currency_rate' => $currencies[$expense_currency_code],
'amount' => $this->request->get('amount'), 'amount' => $this->request->get('amount'),
'contact_id' => 0, 'contact_id' => 0,
'description' => $this->request->get('description'), 'description' => $this->request->get('description'),
'category_id' => Category::transfer(), // Transfer Category ID 'category_id' => Category::transfer(), // Transfer Category ID
'payment_method' => $this->request->get('payment_method'), 'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'), 'reference' => $this->request->get('reference'),
]); ]);
// Convert amount if not same currency // Convert amount if not same currency
if ($expense_currency_code != $income_currency_code) { if ($expense_currency_code != $income_currency_code) {
$default_currency = setting('default.currency', 'USD'); $default_currency = setting('default.currency', 'USD');
$default_amount = $this->request->get('amount'); $default_amount = $this->request->get('amount');
if ($default_currency != $expense_currency_code) { if ($default_currency != $expense_currency_code) {
$default_amount_model = new Transfer(); $default_amount_model = new Transfer();
$default_amount_model->default_currency_code = $default_currency; $default_amount_model->default_currency_code = $default_currency;
$default_amount_model->amount = $this->request->get('amount'); $default_amount_model->amount = $this->request->get('amount');
$default_amount_model->currency_code = $expense_currency_code; $default_amount_model->currency_code = $expense_currency_code;
$default_amount_model->currency_rate = $currencies[$expense_currency_code]; $default_amount_model->currency_rate = $currencies[$expense_currency_code];
$default_amount = $default_amount_model->getAmountConvertedToDefault(); $default_amount = $default_amount_model->getAmountConvertedToDefault();
}
$transfer_amount = new Transfer();
$transfer_amount->default_currency_code = $expense_currency_code;
$transfer_amount->amount = $default_amount;
$transfer_amount->currency_code = $income_currency_code;
$transfer_amount->currency_rate = $currencies[$income_currency_code];
$amount = $transfer_amount->getAmountConvertedFromDefault();
} else {
$amount = $this->request->get('amount');
} }
$transfer_amount = new Transfer(); $income_transaction = Transaction::create([
'company_id' => $this->request['company_id'],
'type' => 'income',
'account_id' => $this->request->get('to_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $income_currency_code,
'currency_rate' => $currencies[$income_currency_code],
'amount' => $amount,
'contact_id' => 0,
'description' => $this->request->get('description'),
'category_id' => Category::transfer(), // Transfer Category ID
'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'),
]);
$transfer_amount->default_currency_code = $expense_currency_code; $this->transfer = Transfer::create([
$transfer_amount->amount = $default_amount; 'company_id' => $this->request['company_id'],
$transfer_amount->currency_code = $income_currency_code; 'expense_transaction_id' => $expense_transaction->id,
$transfer_amount->currency_rate = $currencies[$income_currency_code]; 'income_transaction_id' => $income_transaction->id,
]);
});
$amount = $transfer_amount->getAmountConvertedFromDefault(); return $this->transfer;
} else {
$amount = $this->request->get('amount');
}
$income_transaction = Transaction::create([
'company_id' => $this->request['company_id'],
'type' => 'income',
'account_id' => $this->request->get('to_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $income_currency_code,
'currency_rate' => $currencies[$income_currency_code],
'amount' => $amount,
'contact_id' => 0,
'description' => $this->request->get('description'),
'category_id' => Category::transfer(), // Transfer Category ID
'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'),
]);
$transfer = Transfer::create([
'company_id' => $this->request['company_id'],
'expense_transaction_id' => $expense_transaction->id,
'income_transaction_id' => $income_transaction->id,
]);
return $transfer;
} }
} }

View File

@ -27,7 +27,9 @@ class DeleteAccount extends Job
{ {
$this->authorize(); $this->authorize();
$this->account->delete(); \DB::transaction(function () {
$this->account->delete();
});
return true; return true;
} }

View File

@ -26,14 +26,16 @@ class DeleteReconciliation extends Job
*/ */
public function handle() public function handle()
{ {
$this->reconciliation->delete(); \DB::transaction(function () {
$this->reconciliation->delete();
Transaction::where('account_id', $this->reconciliation->account_id) Transaction::where('account_id', $this->reconciliation->account_id)
->reconciled() ->reconciled()
->whereBetween('paid_at', [$this->reconciliation->started_at, $this->reconciliation->ended_at])->each(function ($transaction) { ->whereBetween('paid_at', [$this->reconciliation->started_at, $this->reconciliation->ended_at])->each(function ($transaction) {
$transaction->reconciled = 0; $transaction->reconciled = 0;
$transaction->save(); $transaction->save();
}); });
});
return true; return true;
} }

View File

@ -28,8 +28,10 @@ class DeleteTransaction extends Job
{ {
$this->authorize(); $this->authorize();
$this->transaction->recurring()->delete(); \DB::transaction(function () {
$this->transaction->delete(); $this->transaction->recurring()->delete();
$this->transaction->delete();
});
return true; return true;
} }

View File

@ -25,9 +25,11 @@ class DeleteTransfer extends Job
*/ */
public function handle() public function handle()
{ {
$this->transfer->expense_transaction->delete(); \DB::transaction(function () {
$this->transfer->income_transaction->delete(); $this->transfer->expense_transaction->delete();
$this->transfer->delete(); $this->transfer->income_transaction->delete();
$this->transfer->delete();
});
return true; return true;
} }

View File

@ -32,13 +32,15 @@ class UpdateAccount extends Job
{ {
$this->authorize(); $this->authorize();
$this->account->update($this->request->all()); \DB::transaction(function () {
$this->account->update($this->request->all());
// Set default account // Set default account
if ($this->request['default_account']) { if ($this->request['default_account']) {
setting()->set('default.account', $this->account->id); setting()->set('default.account', $this->account->id);
setting()->save(); setting()->save();
} }
});
return $this->account; return $this->account;
} }

View File

@ -31,25 +31,27 @@ class UpdateReconciliation extends Job
*/ */
public function handle() public function handle()
{ {
$reconcile = $this->request->get('reconcile'); \DB::transaction(function () {
$transactions = $this->request->get('transactions'); $reconcile = $this->request->get('reconcile');
$transactions = $this->request->get('transactions');
$this->reconciliation->reconciled = $reconcile ? 1 : 0; $this->reconciliation->reconciled = $reconcile ? 1 : 0;
$this->reconciliation->save(); $this->reconciliation->save();
if ($transactions) { if ($transactions) {
foreach ($transactions as $key => $value) { foreach ($transactions as $key => $value) {
if (empty($value)) { if (empty($value)) {
continue; continue;
}
$t = explode('_', $key);
$transaction = Transaction::find($t[1]);
$transaction->reconciled = 1;
$transaction->save();
} }
$t = explode('_', $key);
$transaction = Transaction::find($t[1]);
$transaction->reconciled = 1;
$transaction->save();
} }
} });
return $this->reconciliation; return $this->reconciliation;
} }

View File

@ -32,17 +32,19 @@ class UpdateTransaction extends Job
{ {
$this->authorize(); $this->authorize();
$this->transaction->update($this->request->all()); \DB::transaction(function () {
$this->transaction->update($this->request->all());
// Upload attachment // Upload attachment
if ($this->request->file('attachment')) { if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'transactions'); $media = $this->getMedia($this->request->file('attachment'), 'transactions');
$this->transaction->attachMedia($media, 'attachment'); $this->transaction->attachMedia($media, 'attachment');
} }
// Recurring // Recurring
$this->transaction->updateRecurring(); $this->transaction->updateRecurring();
});
return $this->transaction; return $this->transaction;
} }

View File

@ -34,78 +34,80 @@ class UpdateTransfer extends Job
*/ */
public function handle() public function handle()
{ {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray(); \DB::transaction(function () {
$currencies = Currency::enabled()->pluck('rate', 'code')->toArray();
$expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first(); $expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first();
$income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first(); $income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first();
$expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id); $expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id);
$income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id); $income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id);
$expense_transaction->update([ $expense_transaction->update([
'company_id' => $this->request['company_id'], 'company_id' => $this->request['company_id'],
'type' => 'expense', 'type' => 'expense',
'account_id' => $this->request->get('from_account_id'), 'account_id' => $this->request->get('from_account_id'),
'paid_at' => $this->request->get('transferred_at'), 'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $expense_currency_code, 'currency_code' => $expense_currency_code,
'currency_rate' => $currencies[$expense_currency_code], 'currency_rate' => $currencies[$expense_currency_code],
'amount' => $this->request->get('amount'), 'amount' => $this->request->get('amount'),
'contact_id' => 0, 'contact_id' => 0,
'description' => $this->request->get('description'), 'description' => $this->request->get('description'),
'category_id' => Category::transfer(), // Transfer Category ID 'category_id' => Category::transfer(), // Transfer Category ID
'payment_method' => $this->request->get('payment_method'), 'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'), 'reference' => $this->request->get('reference'),
]); ]);
// Convert amount if not same currency // Convert amount if not same currency
if ($expense_currency_code != $income_currency_code) { if ($expense_currency_code != $income_currency_code) {
$default_currency = setting('default.currency', 'USD'); $default_currency = setting('default.currency', 'USD');
$default_amount = $this->request->get('amount'); $default_amount = $this->request->get('amount');
if ($default_currency != $expense_currency_code) { if ($default_currency != $expense_currency_code) {
$default_amount_model = new Transfer(); $default_amount_model = new Transfer();
$default_amount_model->default_currency_code = $default_currency; $default_amount_model->default_currency_code = $default_currency;
$default_amount_model->amount = $this->request->get('amount'); $default_amount_model->amount = $this->request->get('amount');
$default_amount_model->currency_code = $expense_currency_code; $default_amount_model->currency_code = $expense_currency_code;
$default_amount_model->currency_rate = $currencies[$expense_currency_code]; $default_amount_model->currency_rate = $currencies[$expense_currency_code];
$default_amount = $default_amount_model->getAmountConvertedToDefault(); $default_amount = $default_amount_model->getAmountConvertedToDefault();
}
$transfer_amount = new Transfer();
$transfer_amount->default_currency_code = $expense_currency_code;
$transfer_amount->amount = $default_amount;
$transfer_amount->currency_code = $income_currency_code;
$transfer_amount->currency_rate = $currencies[$income_currency_code];
$amount = $transfer_amount->getAmountConvertedFromDefault();
} else {
$amount = $this->request->get('amount');
} }
$transfer_amount = new Transfer(); $income_transaction->update([
'company_id' => $this->request['company_id'],
'type' => 'income',
'account_id' => $this->request->get('to_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $income_currency_code,
'currency_rate' => $currencies[$income_currency_code],
'amount' => $amount,
'contact_id' => 0,
'description' => $this->request->get('description'),
'category_id' => Category::transfer(), // Transfer Category ID
'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'),
]);
$transfer_amount->default_currency_code = $expense_currency_code; $this->transfer->update([
$transfer_amount->amount = $default_amount; 'company_id' => $this->request['company_id'],
$transfer_amount->currency_code = $income_currency_code; 'expense_transaction_id' => $expense_transaction->id,
$transfer_amount->currency_rate = $currencies[$income_currency_code]; 'income_transaction_id' => $income_transaction->id,
]);
$amount = $transfer_amount->getAmountConvertedFromDefault(); });
} else {
$amount = $this->request->get('amount');
}
$income_transaction->update([
'company_id' => $this->request['company_id'],
'type' => 'income',
'account_id' => $this->request->get('to_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $income_currency_code,
'currency_rate' => $currencies[$income_currency_code],
'amount' => $amount,
'contact_id' => 0,
'description' => $this->request->get('description'),
'category_id' => Category::transfer(), // Transfer Category ID
'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'),
]);
$this->transfer->update([
'company_id' => $this->request['company_id'],
'expense_transaction_id' => $expense_transaction->id,
'income_transaction_id' => $income_transaction->id,
]);
return $this->transfer; return $this->transfer;
} }

View File

@ -8,10 +8,10 @@ use Artisan;
class CreateCompany extends Job class CreateCompany extends Job
{ {
protected $request;
protected $company; protected $company;
protected $request;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -29,15 +29,17 @@ class CreateCompany extends Job
*/ */
public function handle() public function handle()
{ {
$this->company = Company::create($this->request->all()); \DB::transaction(function () {
$this->company = Company::create($this->request->all());
// Clear settings // Clear settings
setting()->setExtraColumns(['company_id' => $this->company->id]); setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->forgetAll(); setting()->forgetAll();
$this->callSeeds(); $this->callSeeds();
$this->updateSettings(); $this->updateSettings();
});
return $this->company; return $this->company;
} }

View File

@ -9,6 +9,8 @@ use App\Models\Common\Contact;
class CreateContact extends Job class CreateContact extends Job
{ {
protected $contact;
protected $request; protected $request;
/** /**
@ -28,13 +30,15 @@ class CreateContact extends Job
*/ */
public function handle() public function handle()
{ {
if ($this->request->get('create_user', 'false') === 'true') { \DB::transaction(function () {
$this->createUser(); if ($this->request->get('create_user', 'false') === 'true') {
} $this->createUser();
}
$contact = Contact::create($this->request->all()); $this->contact = Contact::create($this->request->all());
});
return $contact; return $this->contact;
} }
public function createUser() public function createUser()

View File

@ -9,6 +9,8 @@ use App\Utilities\Widgets;
class CreateDashboard extends Job class CreateDashboard extends Job
{ {
protected $dashboard;
protected $request; protected $request;
/** /**
@ -30,13 +32,15 @@ class CreateDashboard extends Job
{ {
$this->request['enabled'] = $this->request['enabled'] ?? 1; $this->request['enabled'] = $this->request['enabled'] ?? 1;
$this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled'])); \DB::transaction(function () {
$this->dashboard = Dashboard::create($this->request->only(['company_id', 'name', 'enabled']));
$this->attachToUser(); $this->attachToUser();
if ($this->request->has('with_widgets')) { if ($this->request->has('with_widgets')) {
$this->createWidgets(); $this->createWidgets();
} }
});
return $this->dashboard; return $this->dashboard;
} }

View File

@ -7,6 +7,8 @@ use App\Models\Common\Item;
class CreateItem extends Job class CreateItem extends Job
{ {
protected $item;
protected $request; protected $request;
/** /**
@ -26,15 +28,17 @@ class CreateItem extends Job
*/ */
public function handle() public function handle()
{ {
$item = Item::create($this->request->all()); \DB::transaction(function () {
$this->item = Item::create($this->request->all());
// Upload picture // Upload picture
if ($this->request->file('picture')) { if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items'); $media = $this->getMedia($this->request->file('picture'), 'items');
$item->attachMedia($media, 'picture'); $this->item->attachMedia($media, 'picture');
} }
});
return $item; return $this->item;
} }
} }

View File

@ -7,6 +7,8 @@ use App\Models\Common\Report;
class CreateReport extends Job class CreateReport extends Job
{ {
protected $report;
protected $request; protected $request;
/** /**
@ -26,8 +28,10 @@ class CreateReport extends Job
*/ */
public function handle() public function handle()
{ {
$report = Report::create($this->request->all()); \DB::transaction(function () {
$this->report = Report::create($this->request->all());
});
return $report; return $this->report;
} }
} }

View File

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

View File

@ -31,11 +31,13 @@ class DeleteContact extends Job
{ {
$this->authorize(); $this->authorize();
if ($user = $this->contact->user) { \DB::transaction(function () {
$this->dispatch(new DeleteUser($user)); if ($user = $this->contact->user) {
} $this->dispatch(new DeleteUser($user));
}
$this->contact->delete(); $this->contact->delete();
});
return true; return true;
} }

View File

@ -30,9 +30,11 @@ class DeleteDashboard extends Job
{ {
$this->authorize(); $this->authorize();
$this->deleteRelationships($this->dashboard, ['widgets']); \DB::transaction(function () {
$this->deleteRelationships($this->dashboard, ['widgets']);
$this->dashboard->delete(); $this->dashboard->delete();
});
return true; return true;
} }

View File

@ -27,7 +27,9 @@ class DeleteItem extends Job
{ {
$this->authorize(); $this->authorize();
$this->item->delete(); \DB::transaction(function () {
$this->item->delete();
});
return true; return true;
} }

View File

@ -25,7 +25,9 @@ class DeleteReport extends Job
*/ */
public function handle() public function handle()
{ {
$this->report->delete(); \DB::transaction(function () {
$this->report->delete();
});
return true; return true;
} }

View File

@ -35,45 +35,47 @@ class UpdateCompany extends Job
{ {
$this->authorize(); $this->authorize();
$this->company->update($this->request->all()); \DB::transaction(function () {
$this->company->update($this->request->all());
// Clear current and load given company settings // Clear current and load given company settings
setting()->setExtraColumns(['company_id' => $this->company->id]); setting()->setExtraColumns(['company_id' => $this->company->id]);
setting()->forgetAll(); setting()->forgetAll();
setting()->load(true); setting()->load(true);
if ($this->request->has('name')) { if ($this->request->has('name')) {
setting()->set('company.name', $this->request->get('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(); if ($this->request->has('email')) {
setting()->forgetAll(); 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; return $this->company;
} }

View File

@ -33,11 +33,13 @@ class UpdateContact extends Job
{ {
$this->authorize(); $this->authorize();
if ($this->request->get('create_user', 'false') === 'true') { \DB::transaction(function () {
$this->createUser(); if ($this->request->get('create_user', 'false') === 'true') {
} $this->createUser();
}
$this->contact->update($this->request->all()); $this->contact->update($this->request->all());
});
return $this->contact; return $this->contact;
} }

View File

@ -35,11 +35,13 @@ class UpdateDashboard extends Job
{ {
$this->authorize(); $this->authorize();
$this->dashboard->update($this->request->all()); \DB::transaction(function () {
$this->dashboard->update($this->request->all());
if ($this->request->has('users')) { if ($this->request->has('users')) {
$this->dashboard->users()->sync($this->request->get('users')); $this->dashboard->users()->sync($this->request->get('users'));
} }
});
return $this->dashboard; return $this->dashboard;
} }

View File

@ -30,14 +30,16 @@ class UpdateItem extends Job
*/ */
public function handle() public function handle()
{ {
$this->item->update($this->request->all()); \DB::transaction(function () {
$this->item->update($this->request->all());
// Upload picture // Upload picture
if ($this->request->file('picture')) { if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'items'); $media = $this->getMedia($this->request->file('picture'), 'items');
$this->item->attachMedia($media, 'picture'); $this->item->attachMedia($media, 'picture');
} }
});
return $this->item; return $this->item;
} }

View File

@ -30,7 +30,9 @@ class UpdateReport extends Job
*/ */
public function handle() public function handle()
{ {
$this->report->update($this->request->all()); \DB::transaction(function () {
$this->report->update($this->request->all());
});
return $this->report; return $this->report;
} }

View File

@ -26,12 +26,14 @@ class CancelBill extends Job
*/ */
public function handle() public function handle()
{ {
$this->deleteRelationships($this->bill, [ \DB::transaction(function () {
'transactions', 'recurring' $this->deleteRelationships($this->bill, [
]); 'transactions', 'recurring'
]);
$this->bill->status = 'cancelled'; $this->bill->status = 'cancelled';
$this->bill->save(); $this->bill->save();
});
return $this->bill; return $this->bill;
} }

View File

@ -14,10 +14,10 @@ class CreateBill extends Job
{ {
use Currencies, DateTime; use Currencies, DateTime;
protected $request;
protected $bill; protected $bill;
protected $request;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -41,20 +41,22 @@ class CreateBill extends Job
event(new BillCreating($this->request)); event(new BillCreating($this->request));
$this->bill = Bill::create($this->request->all()); \DB::transaction(function () {
$this->bill = Bill::create($this->request->all());
// Upload attachment // Upload attachment
if ($this->request->file('attachment')) { if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'bills'); $media = $this->getMedia($this->request->file('attachment'), 'bills');
$this->bill->attachMedia($media, 'attachment'); $this->bill->attachMedia($media, 'attachment');
} }
$this->createItemsAndTotals(); $this->createItemsAndTotals();
$this->bill->update($this->request->input()); $this->bill->update($this->request->input());
$this->bill->createRecurring(); $this->bill->createRecurring();
});
event(new BillCreated($this->bill)); event(new BillCreated($this->bill));

View File

@ -3,7 +3,6 @@
namespace App\Jobs\Purchase; namespace App\Jobs\Purchase;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Models\Purchase\Bill;
use App\Observers\Transaction; use App\Observers\Transaction;
class DeleteBill extends Job class DeleteBill extends Job
@ -29,15 +28,17 @@ class DeleteBill extends Job
{ {
$this->authorize(); $this->authorize();
Transaction::mute(); \DB::transaction(function () {
Transaction::mute();
$this->deleteRelationships($this->bill, [ $this->deleteRelationships($this->bill, [
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals' 'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
]); ]);
$this->bill->delete(); $this->bill->delete();
Transaction::unmute(); Transaction::unmute();
});
return true; return true;
} }

View File

@ -3,13 +3,15 @@
namespace App\Jobs\Purchase; namespace App\Jobs\Purchase;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Events\Purchase\BillCreated;
use App\Models\Purchase\Bill; use App\Models\Purchase\Bill;
use App\Models\Purchase\BillHistory;
class DuplicateBill extends Job class DuplicateBill extends Job
{ {
protected $bill; protected $bill;
protected $clone;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -27,17 +29,12 @@ class DuplicateBill extends Job
*/ */
public function handle() public function handle()
{ {
$clone = $this->bill->duplicate(); \DB::transaction(function () {
$this->clone = $this->bill->duplicate();
});
// Add bill history event(new BillCreated($this->clone));
BillHistory::create([
'company_id' => session('company_id'),
'bill_id' => $clone->id,
'status' => 'draft',
'notify' => 0,
'description' => trans('messages.success.added', ['type' => $clone->bill_number]),
]);
return $clone; return $this->clone;
} }
} }

View File

@ -43,26 +43,28 @@ class UpdateBill extends Job
event(new BillUpdating($this->bill, $this->request)); event(new BillUpdating($this->bill, $this->request));
// Upload attachment \DB::transaction(function () {
if ($this->request->file('attachment')) { // Upload attachment
$media = $this->getMedia($this->request->file('attachment'), 'bills'); if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'bills');
$this->bill->attachMedia($media, 'attachment'); $this->bill->attachMedia($media, 'attachment');
} }
$this->createItemsAndTotals(); $this->createItemsAndTotals();
$bill_paid = $this->bill->paid; $bill_paid = $this->bill->paid;
unset($this->bill->reconciled); unset($this->bill->reconciled);
if (($bill_paid) && $this->request['amount'] > $bill_paid) { if (($bill_paid) && $this->request['amount'] > $bill_paid) {
$this->request['status'] = 'partial'; $this->request['status'] = 'partial';
} }
$this->bill->update($this->request->input()); $this->bill->update($this->request->input());
$this->bill->updateRecurring(); $this->bill->updateRecurring();
});
event(new BillUpdated($this->bill)); event(new BillUpdated($this->bill));

View File

@ -26,12 +26,14 @@ class CancelInvoice extends Job
*/ */
public function handle() public function handle()
{ {
$this->deleteRelationships($this->invoice, [ \DB::transaction(function () {
'transactions', 'recurring' $this->deleteRelationships($this->invoice, [
]); 'transactions', 'recurring'
]);
$this->invoice->status = 'cancelled'; $this->invoice->status = 'cancelled';
$this->invoice->save(); $this->invoice->save();
});
return $this->invoice; return $this->invoice;
} }

View File

@ -14,10 +14,10 @@ class CreateInvoice extends Job
{ {
use Currencies, DateTime; use Currencies, DateTime;
protected $request;
protected $invoice; protected $invoice;
protected $request;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -41,20 +41,22 @@ class CreateInvoice extends Job
event(new InvoiceCreating($this->request)); event(new InvoiceCreating($this->request));
$this->invoice = Invoice::create($this->request->all()); \DB::transaction(function () {
$this->invoice = Invoice::create($this->request->all());
// Upload attachment // Upload attachment
if ($this->request->file('attachment')) { if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'invoices'); $media = $this->getMedia($this->request->file('attachment'), 'invoices');
$this->invoice->attachMedia($media, 'attachment'); $this->invoice->attachMedia($media, 'attachment');
} }
$this->createItemsAndTotals(); $this->createItemsAndTotals();
$this->invoice->update($this->request->all()); $this->invoice->update($this->request->all());
$this->invoice->createRecurring(); $this->invoice->createRecurring();
});
event(new InvoiceCreated($this->invoice)); event(new InvoiceCreated($this->invoice));

View File

@ -3,7 +3,6 @@
namespace App\Jobs\Sale; namespace App\Jobs\Sale;
use App\Abstracts\Job; use App\Abstracts\Job;
use App\Models\Sale\Invoice;
use App\Observers\Transaction; use App\Observers\Transaction;
class DeleteInvoice extends Job class DeleteInvoice extends Job
@ -29,15 +28,17 @@ class DeleteInvoice extends Job
{ {
$this->authorize(); $this->authorize();
Transaction::mute(); \DB::transaction(function () {
Transaction::mute();
$this->deleteRelationships($this->invoice, [ $this->deleteRelationships($this->invoice, [
'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals' 'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals'
]); ]);
$this->invoice->delete(); $this->invoice->delete();
Transaction::unmute(); Transaction::unmute();
});
return true; return true;
} }

View File

@ -10,6 +10,8 @@ class DuplicateInvoice extends Job
{ {
protected $invoice; protected $invoice;
protected $clone;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -27,10 +29,12 @@ class DuplicateInvoice extends Job
*/ */
public function handle() public function handle()
{ {
$clone = $this->invoice->duplicate(); \DB::transaction(function () {
$this->clone = $this->invoice->duplicate();
});
event(new InvoiceCreated($clone)); event(new InvoiceCreated($this->clone));
return $clone; return $this->clone;
} }
} }

View File

@ -43,26 +43,28 @@ class UpdateInvoice extends Job
event(new InvoiceUpdating($this->invoice, $this->request)); event(new InvoiceUpdating($this->invoice, $this->request));
// Upload attachment \DB::transaction(function () {
if ($this->request->file('attachment')) { // Upload attachment
$media = $this->getMedia($this->request->file('attachment'), 'invoices'); if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
$this->invoice->attachMedia($media, 'attachment'); $this->invoice->attachMedia($media, 'attachment');
} }
$this->createItemsAndTotals(); $this->createItemsAndTotals();
$invoice_paid = $this->invoice->paid; $invoice_paid = $this->invoice->paid;
unset($this->invoice->reconciled); unset($this->invoice->reconciled);
if (($invoice_paid) && $this->request['amount'] > $invoice_paid) { if (($invoice_paid) && $this->request['amount'] > $invoice_paid) {
$this->request['status'] = 'partial'; $this->request['status'] = 'partial';
} }
$this->invoice->update($this->request->all()); $this->invoice->update($this->request->all());
$this->invoice->updateRecurring(); $this->invoice->updateRecurring();
});
event(new InvoiceUpdated($this->invoice, $this->request)); event(new InvoiceUpdated($this->invoice, $this->request));

View File

@ -7,6 +7,8 @@ use App\Models\Setting\Category;
class CreateCategory extends Job class CreateCategory extends Job
{ {
protected $category;
protected $request; protected $request;
/** /**
@ -26,8 +28,10 @@ class CreateCategory extends Job
*/ */
public function handle() public function handle()
{ {
$category = Category::create($this->request->all()); \DB::transaction(function () {
$this->category = Category::create($this->request->all());
});
return $category; return $this->category;
} }
} }

View File

@ -7,6 +7,8 @@ use App\Models\Setting\Currency;
class CreateCurrency extends Job class CreateCurrency extends Job
{ {
protected $currency;
protected $request; protected $request;
/** /**
@ -31,14 +33,16 @@ class CreateCurrency extends Job
$this->request['rate'] = '1'; $this->request['rate'] = '1';
} }
$currency = Currency::create($this->request->all()); \DB::transaction(function () {
$this->currency = Currency::create($this->request->all());
// Update default currency setting // Update default currency setting
if ($this->request->get('default_currency')) { if ($this->request->get('default_currency')) {
setting()->set('default.currency', $this->request->get('code')); setting()->set('default.currency', $this->request->get('code'));
setting()->save(); setting()->save();
} }
});
return $currency; return $this->currency;
} }
} }

View File

@ -7,6 +7,8 @@ use App\Models\Setting\Tax;
class CreateTax extends Job class CreateTax extends Job
{ {
protected $tax;
protected $request; protected $request;
/** /**
@ -26,8 +28,10 @@ class CreateTax extends Job
*/ */
public function handle() public function handle()
{ {
$tax = Tax::create($this->request->all()); \DB::transaction(function () {
$this->tax = Tax::create($this->request->all());
});
return $tax; return $this->tax;
} }
} }

View File

@ -28,7 +28,9 @@ class DeleteCategory extends Job
{ {
$this->authorize(); $this->authorize();
$this->category->delete(); \DB::transaction(function () {
$this->category->delete();
});
return true; return true;
} }

View File

@ -27,7 +27,9 @@ class DeleteCurrency extends Job
{ {
$this->authorize(); $this->authorize();
$this->currency->delete(); \DB::transaction(function () {
$this->currency->delete();
});
return true; return true;
} }

View File

@ -27,7 +27,9 @@ class DeleteTax extends Job
{ {
$this->authorize(); $this->authorize();
$this->tax->delete(); \DB::transaction(function () {
$this->tax->delete();
});
return true; return true;
} }

View File

@ -32,7 +32,9 @@ class UpdateCategory extends Job
{ {
$this->authorize(); $this->authorize();
$this->category->update($this->request->all()); \DB::transaction(function () {
$this->category->update($this->request->all());
});
return $this->category; return $this->category;
} }

View File

@ -37,13 +37,15 @@ class UpdateCurrency extends Job
$this->request['rate'] = '1'; $this->request['rate'] = '1';
} }
$this->currency->update($this->request->all()); \DB::transaction(function () {
$this->currency->update($this->request->all());
// Update default currency setting // Update default currency setting
if ($this->request->get('default_currency')) { if ($this->request->get('default_currency')) {
setting()->set('default.currency', $this->request->get('code')); setting()->set('default.currency', $this->request->get('code'));
setting()->save(); setting()->save();
} }
});
return $this->currency; return $this->currency;
} }

View File

@ -32,7 +32,9 @@ class UpdateTax extends Job
{ {
$this->authorize(); $this->authorize();
$this->tax->update($this->request->all()); \DB::transaction(function () {
$this->tax->update($this->request->all());
});
return $this->tax; return $this->tax;
} }

View File

@ -2,6 +2,9 @@
namespace App\Traits; namespace App\Traits;
use Exception;
use Throwable;
trait Jobs trait Jobs
{ {
/** /**
@ -48,7 +51,7 @@ trait Jobs
'data' => $data, 'data' => $data,
'message' => '', 'message' => '',
]; ];
} catch(\Exception $e) { } catch (Exception | Throwable $e) {
$response = [ $response = [
'success' => false, 'success' => false,
'error' => true, 'error' => true,