From acdc9da2c8250154e49c5b577bbd333bfca20ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Fri, 26 Jun 2020 13:40:19 +0300 Subject: [PATCH] added db transaction to jobs --- app/Jobs/Auth/CreatePermission.php | 8 +- app/Jobs/Auth/CreateRole.php | 14 +- app/Jobs/Auth/CreateUser.php | 53 ++++---- app/Jobs/Auth/DeletePermission.php | 4 +- app/Jobs/Auth/DeleteRole.php | 6 +- app/Jobs/Auth/DeleteUser.php | 6 +- app/Jobs/Auth/UpdatePermission.php | 4 +- app/Jobs/Auth/UpdateRole.php | 10 +- app/Jobs/Auth/UpdateUser.php | 28 ++-- app/Jobs/Banking/CreateAccount.php | 18 ++- .../Banking/CreateDocumentTransaction.php | 26 ++-- app/Jobs/Banking/CreateReconciliation.php | 48 +++---- app/Jobs/Banking/CreateTransaction.php | 22 ++-- app/Jobs/Banking/CreateTransfer.php | 124 +++++++++--------- app/Jobs/Banking/DeleteAccount.php | 4 +- app/Jobs/Banking/DeleteReconciliation.php | 16 ++- app/Jobs/Banking/DeleteTransaction.php | 6 +- app/Jobs/Banking/DeleteTransfer.php | 8 +- app/Jobs/Banking/UpdateAccount.php | 14 +- app/Jobs/Banking/UpdateReconciliation.php | 32 ++--- app/Jobs/Banking/UpdateTransaction.php | 18 +-- app/Jobs/Banking/UpdateTransfer.php | 124 +++++++++--------- app/Jobs/Common/CreateCompany.php | 18 +-- app/Jobs/Common/CreateContact.php | 14 +- app/Jobs/Common/CreateDashboard.php | 14 +- app/Jobs/Common/CreateItem.php | 18 ++- app/Jobs/Common/CreateReport.php | 8 +- app/Jobs/Common/DeleteCompany.php | 16 ++- app/Jobs/Common/DeleteContact.php | 10 +- app/Jobs/Common/DeleteDashboard.php | 6 +- app/Jobs/Common/DeleteItem.php | 4 +- app/Jobs/Common/DeleteReport.php | 4 +- app/Jobs/Common/UpdateCompany.php | 72 +++++----- app/Jobs/Common/UpdateContact.php | 10 +- app/Jobs/Common/UpdateDashboard.php | 10 +- app/Jobs/Common/UpdateItem.php | 14 +- app/Jobs/Common/UpdateReport.php | 4 +- app/Jobs/Purchase/CancelBill.php | 12 +- app/Jobs/Purchase/CreateBill.php | 24 ++-- app/Jobs/Purchase/DeleteBill.php | 15 ++- app/Jobs/Purchase/DuplicateBill.php | 19 ++- app/Jobs/Purchase/UpdateBill.php | 28 ++-- app/Jobs/Sale/CancelInvoice.php | 12 +- app/Jobs/Sale/CreateInvoice.php | 24 ++-- app/Jobs/Sale/DeleteInvoice.php | 15 ++- app/Jobs/Sale/DuplicateInvoice.php | 10 +- app/Jobs/Sale/UpdateInvoice.php | 28 ++-- app/Jobs/Setting/CreateCategory.php | 8 +- app/Jobs/Setting/CreateCurrency.php | 18 ++- app/Jobs/Setting/CreateTax.php | 8 +- app/Jobs/Setting/DeleteCategory.php | 4 +- app/Jobs/Setting/DeleteCurrency.php | 4 +- app/Jobs/Setting/DeleteTax.php | 4 +- app/Jobs/Setting/UpdateCategory.php | 4 +- app/Jobs/Setting/UpdateCurrency.php | 14 +- app/Jobs/Setting/UpdateTax.php | 4 +- app/Traits/Jobs.php | 5 +- 57 files changed, 606 insertions(+), 469 deletions(-) diff --git a/app/Jobs/Auth/CreatePermission.php b/app/Jobs/Auth/CreatePermission.php index d25030879..82f5612cd 100644 --- a/app/Jobs/Auth/CreatePermission.php +++ b/app/Jobs/Auth/CreatePermission.php @@ -7,6 +7,8 @@ use App\Models\Auth\Permission; class CreatePermission extends Job { + protected $permission; + protected $request; /** @@ -26,8 +28,10 @@ class CreatePermission extends Job */ public function handle() { - $permission = Permission::create($this->request->all()); + \DB::transaction(function () { + $this->permission = Permission::create($this->request->all()); + }); - return $permission; + return $this->permission; } } diff --git a/app/Jobs/Auth/CreateRole.php b/app/Jobs/Auth/CreateRole.php index 6ec20d1b9..576f376ed 100644 --- a/app/Jobs/Auth/CreateRole.php +++ b/app/Jobs/Auth/CreateRole.php @@ -7,6 +7,8 @@ use App\Models\Auth\Role; class CreateRole extends Job { + protected $role; + protected $request; /** @@ -26,12 +28,14 @@ class CreateRole extends Job */ public function handle() { - $role = Role::create($this->request->input()); + \DB::transaction(function () { + $this->role = Role::create($this->request->input()); - if ($this->request->has('permissions')) { - $role->permissions()->attach($this->request->get('permissions')); - } + if ($this->request->has('permissions')) { + $this->role->permissions()->attach($this->request->get('permissions')); + } + }); - return $role; + return $this->role; } } diff --git a/app/Jobs/Auth/CreateUser.php b/app/Jobs/Auth/CreateUser.php index 7d2edd902..a9149b053 100644 --- a/app/Jobs/Auth/CreateUser.php +++ b/app/Jobs/Auth/CreateUser.php @@ -8,6 +8,8 @@ use Artisan; class CreateUser extends Job { + protected $user; + protected $request; /** @@ -27,41 +29,44 @@ class CreateUser extends Job */ public function handle() { - $user = User::create($this->request->input()); + \DB::transaction(function () { + $this->user = User::create($this->request->input()); - // Upload picture - if ($this->request->file('picture')) { - $media = $this->getMedia($this->request->file('picture'), 'users'); + // Upload picture + if ($this->request->file('picture')) { + $media = $this->getMedia($this->request->file('picture'), 'users'); - $user->attachMedia($media, 'picture'); - } + $this->user->attachMedia($media, 'picture'); + } - if ($this->request->has('dashboards')) { - $user->dashboards()->attach($this->request->get('dashboards')); - } + if ($this->request->has('dashboards')) { + $this->user->dashboards()->attach($this->request->get('dashboards')); + } - if ($this->request->has('permissions')) { - $user->permissions()->attach($this->request->get('permissions')); - } + if ($this->request->has('permissions')) { + $this->user->permissions()->attach($this->request->get('permissions')); + } - if ($this->request->has('roles')) { - $user->roles()->attach($this->request->get('roles')); - } + if ($this->request->has('roles')) { + $this->user->roles()->attach($this->request->get('roles')); + } - if ($this->request->has('companies')) { - $user->companies()->attach($this->request->get('companies')); - } + if ($this->request->has('companies')) { + $this->user->companies()->attach($this->request->get('companies')); + } - // Add User Dashboard - if (!empty($user->companies)) { - foreach ($user->companies as $company) { + if (empty($this->user->companies)) { + return; + } + + foreach ($this->user->companies as $company) { Artisan::call('user:seed', [ - 'user' => $user->id, + 'user' => $this->user->id, 'company' => $company->id, ]); } - } + }); - return $user; + return $this->user; } } diff --git a/app/Jobs/Auth/DeletePermission.php b/app/Jobs/Auth/DeletePermission.php index af007048f..a9939a77d 100644 --- a/app/Jobs/Auth/DeletePermission.php +++ b/app/Jobs/Auth/DeletePermission.php @@ -25,7 +25,9 @@ class DeletePermission extends Job */ public function handle() { - $this->permission->delete(); + \DB::transaction(function () { + $this->permission->delete(); + }); return true; } diff --git a/app/Jobs/Auth/DeleteRole.php b/app/Jobs/Auth/DeleteRole.php index 25018e2e3..c7298075a 100644 --- a/app/Jobs/Auth/DeleteRole.php +++ b/app/Jobs/Auth/DeleteRole.php @@ -25,9 +25,11 @@ class DeleteRole extends Job */ public function handle() { - $this->role->delete(); + \DB::transaction(function () { + $this->role->delete(); - $this->role->flushCache(); + $this->role->flushCache(); + }); return true; } diff --git a/app/Jobs/Auth/DeleteUser.php b/app/Jobs/Auth/DeleteUser.php index f34a0d707..384b2f6da 100644 --- a/app/Jobs/Auth/DeleteUser.php +++ b/app/Jobs/Auth/DeleteUser.php @@ -27,9 +27,11 @@ class DeleteUser extends Job { $this->authorize(); - $this->user->delete(); + \DB::transaction(function () { + $this->user->delete(); - $this->user->flushCache(); + $this->user->flushCache(); + }); return true; } diff --git a/app/Jobs/Auth/UpdatePermission.php b/app/Jobs/Auth/UpdatePermission.php index 632631e1f..5f6acbd6e 100644 --- a/app/Jobs/Auth/UpdatePermission.php +++ b/app/Jobs/Auth/UpdatePermission.php @@ -30,7 +30,9 @@ class UpdatePermission extends Job */ public function handle() { - $this->permission->update($this->request->all()); + \DB::transaction(function () { + $this->permission->update($this->request->all()); + }); return $this->permission; } diff --git a/app/Jobs/Auth/UpdateRole.php b/app/Jobs/Auth/UpdateRole.php index 665201253..16ce9184c 100644 --- a/app/Jobs/Auth/UpdateRole.php +++ b/app/Jobs/Auth/UpdateRole.php @@ -30,11 +30,13 @@ class UpdateRole extends Job */ public function handle() { - $this->role->update($this->request->all()); + \DB::transaction(function () { + $this->role->update($this->request->all()); - if ($this->request->has('permissions')) { - $this->role->permissions()->sync($this->request->get('permissions')); - } + if ($this->request->has('permissions')) { + $this->role->permissions()->sync($this->request->get('permissions')); + } + }); return $this->role; } diff --git a/app/Jobs/Auth/UpdateUser.php b/app/Jobs/Auth/UpdateUser.php index 3a1cd66e3..15c66c0ca 100644 --- a/app/Jobs/Auth/UpdateUser.php +++ b/app/Jobs/Auth/UpdateUser.php @@ -38,24 +38,24 @@ class UpdateUser extends Job unset($this->request['password_confirmation']); } - $this->user->update($this->request->input()); + \DB::transaction(function () { + $this->user->update($this->request->input()); - // Upload picture - if ($this->request->file('picture')) { - $media = $this->getMedia($this->request->file('picture'), 'users'); + // Upload picture + if ($this->request->file('picture')) { + $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')) { - $this->user->roles()->sync($this->request->get('roles')); - } + if ($this->request->has('roles')) { + $this->user->roles()->sync($this->request->get('roles')); + } - // Sync companies - if ($this->request->has('companies')) { - $this->user->companies()->sync($this->request->get('companies')); - } + if ($this->request->has('companies')) { + $this->user->companies()->sync($this->request->get('companies')); + } + }); return $this->user; } diff --git a/app/Jobs/Banking/CreateAccount.php b/app/Jobs/Banking/CreateAccount.php index 67202a5c4..15414c879 100644 --- a/app/Jobs/Banking/CreateAccount.php +++ b/app/Jobs/Banking/CreateAccount.php @@ -7,6 +7,8 @@ use App\Models\Banking\Account; class CreateAccount extends Job { + protected $account; + protected $request; /** @@ -26,14 +28,16 @@ class CreateAccount extends Job */ public function handle() { - $account = Account::create($this->request->all()); + \DB::transaction(function () { + $this->account = Account::create($this->request->all()); - // Set default account - if ($this->request['default_account']) { - setting()->set('default.account', $account->id); - setting()->save(); - } + // Set default account + if ($this->request['default_account']) { + setting()->set('default.account', $this->account->id); + setting()->save(); + } + }); - return $account; + return $this->account; } } diff --git a/app/Jobs/Banking/CreateDocumentTransaction.php b/app/Jobs/Banking/CreateDocumentTransaction.php index 69c02fe13..93aca01e2 100644 --- a/app/Jobs/Banking/CreateDocumentTransaction.php +++ b/app/Jobs/Banking/CreateDocumentTransaction.php @@ -17,6 +17,8 @@ class CreateDocumentTransaction extends Job protected $request; + protected $transaction; + /** * Create a new job instance. * @@ -40,20 +42,22 @@ class CreateDocumentTransaction extends Job $this->checkAmount(); - $transaction = $this->dispatch(new CreateTransaction($this->request)); + \DB::transaction(function () { + $this->transaction = $this->dispatch(new CreateTransaction($this->request)); - // Upload attachment - if ($this->request->file('attachment')) { - $media = $this->getMedia($this->request->file('attachment'), 'transactions'); + // Upload attachment + if ($this->request->file('attachment')) { + $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() @@ -137,9 +141,9 @@ class CreateDocumentTransaction extends Job 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) { $this->dispatch(new CreateInvoiceHistory($this->model, 0, $history_desc)); diff --git a/app/Jobs/Banking/CreateReconciliation.php b/app/Jobs/Banking/CreateReconciliation.php index bf54d7711..af20953cd 100644 --- a/app/Jobs/Banking/CreateReconciliation.php +++ b/app/Jobs/Banking/CreateReconciliation.php @@ -8,6 +8,8 @@ use App\Models\Banking\Transaction; class CreateReconciliation extends Job { + protected $reconciliation; + protected $request; /** @@ -27,32 +29,34 @@ class CreateReconciliation extends Job */ public function handle() { - $reconcile = $this->request->get('reconcile'); - $transactions = $this->request->get('transactions'); + \DB::transaction(function () { + $reconcile = $this->request->get('reconcile'); + $transactions = $this->request->get('transactions'); - $reconciliation = Reconciliation::create([ - 'company_id' => $this->request['company_id'], - 'account_id' => $this->request->get('account_id'), - 'started_at' => $this->request->get('started_at'), - 'ended_at' => $this->request->get('ended_at'), - 'closing_balance' => $this->request->get('closing_balance'), - 'reconciled' => $reconcile ? 1 : 0, - ]); + $this->reconciliation = Reconciliation::create([ + 'company_id' => $this->request['company_id'], + 'account_id' => $this->request->get('account_id'), + 'started_at' => $this->request->get('started_at'), + 'ended_at' => $this->request->get('ended_at'), + 'closing_balance' => $this->request->get('closing_balance'), + 'reconciled' => $reconcile ? 1 : 0, + ]); - if ($transactions) { - foreach ($transactions as $key => $value) { - if (empty($value)) { - continue; + if ($transactions) { + foreach ($transactions as $key => $value) { + if (empty($value)) { + 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; } } diff --git a/app/Jobs/Banking/CreateTransaction.php b/app/Jobs/Banking/CreateTransaction.php index 37ff1f4e3..40e2a485d 100644 --- a/app/Jobs/Banking/CreateTransaction.php +++ b/app/Jobs/Banking/CreateTransaction.php @@ -9,10 +9,10 @@ use App\Models\Banking\Transaction; class CreateTransaction extends Job { - protected $request; - protected $transaction; + protected $request; + /** * Create a new job instance. * @@ -32,17 +32,19 @@ class CreateTransaction extends Job { event(new TransactionCreating($this->request)); - $this->transaction = Transaction::create($this->request->all()); + \DB::transaction(function () { + $this->transaction = Transaction::create($this->request->all()); - // Upload attachment - if ($this->request->file('attachment')) { - $media = $this->getMedia($this->request->file('attachment'), 'transactions'); + // Upload attachment + if ($this->request->file('attachment')) { + $media = $this->getMedia($this->request->file('attachment'), 'transactions'); - $this->transaction->attachMedia($media, 'attachment'); - } + $this->transaction->attachMedia($media, 'attachment'); + } - // Recurring - $this->transaction->createRecurring(); + // Recurring + $this->transaction->createRecurring(); + }); event(new TransactionCreated($this->transaction)); diff --git a/app/Jobs/Banking/CreateTransfer.php b/app/Jobs/Banking/CreateTransfer.php index 1daaac4a6..38b115199 100644 --- a/app/Jobs/Banking/CreateTransfer.php +++ b/app/Jobs/Banking/CreateTransfer.php @@ -11,6 +11,8 @@ use App\Models\Setting\Currency; class CreateTransfer extends Job { + protected $transfer; + protected $request; /** @@ -30,76 +32,78 @@ class CreateTransfer extends Job */ 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(); - $income_currency_code = Account::where('id', $this->request->get('to_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(); - $expense_transaction = Transaction::create([ - 'company_id' => $this->request['company_id'], - 'type' => 'expense', - 'account_id' => $this->request->get('from_account_id'), - 'paid_at' => $this->request->get('transferred_at'), - 'currency_code' => $expense_currency_code, - 'currency_rate' => $currencies[$expense_currency_code], - 'amount' => $this->request->get('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'), - ]); + $expense_transaction = Transaction::create([ + 'company_id' => $this->request['company_id'], + 'type' => 'expense', + 'account_id' => $this->request->get('from_account_id'), + 'paid_at' => $this->request->get('transferred_at'), + 'currency_code' => $expense_currency_code, + 'currency_rate' => $currencies[$expense_currency_code], + 'amount' => $this->request->get('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'), + ]); - // Convert amount if not same currency - if ($expense_currency_code != $income_currency_code) { - $default_currency = setting('default.currency', 'USD'); + // Convert amount if not same currency + if ($expense_currency_code != $income_currency_code) { + $default_currency = setting('default.currency', 'USD'); - $default_amount = $this->request->get('amount'); + $default_amount = $this->request->get('amount'); - if ($default_currency != $expense_currency_code) { - $default_amount_model = new Transfer(); + if ($default_currency != $expense_currency_code) { + $default_amount_model = new Transfer(); - $default_amount_model->default_currency_code = $default_currency; - $default_amount_model->amount = $this->request->get('amount'); - $default_amount_model->currency_code = $expense_currency_code; - $default_amount_model->currency_rate = $currencies[$expense_currency_code]; + $default_amount_model->default_currency_code = $default_currency; + $default_amount_model->amount = $this->request->get('amount'); + $default_amount_model->currency_code = $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; - $transfer_amount->amount = $default_amount; - $transfer_amount->currency_code = $income_currency_code; - $transfer_amount->currency_rate = $currencies[$income_currency_code]; + $this->transfer = Transfer::create([ + 'company_id' => $this->request['company_id'], + 'expense_transaction_id' => $expense_transaction->id, + 'income_transaction_id' => $income_transaction->id, + ]); + }); - $amount = $transfer_amount->getAmountConvertedFromDefault(); - } 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; + return $this->transfer; } } diff --git a/app/Jobs/Banking/DeleteAccount.php b/app/Jobs/Banking/DeleteAccount.php index 5e0d123a4..be9e67027 100644 --- a/app/Jobs/Banking/DeleteAccount.php +++ b/app/Jobs/Banking/DeleteAccount.php @@ -27,7 +27,9 @@ class DeleteAccount extends Job { $this->authorize(); - $this->account->delete(); + \DB::transaction(function () { + $this->account->delete(); + }); return true; } diff --git a/app/Jobs/Banking/DeleteReconciliation.php b/app/Jobs/Banking/DeleteReconciliation.php index 33f51561d..ca5ae50e0 100644 --- a/app/Jobs/Banking/DeleteReconciliation.php +++ b/app/Jobs/Banking/DeleteReconciliation.php @@ -26,14 +26,16 @@ class DeleteReconciliation extends Job */ public function handle() { - $this->reconciliation->delete(); + \DB::transaction(function () { + $this->reconciliation->delete(); - Transaction::where('account_id', $this->reconciliation->account_id) - ->reconciled() - ->whereBetween('paid_at', [$this->reconciliation->started_at, $this->reconciliation->ended_at])->each(function ($transaction) { - $transaction->reconciled = 0; - $transaction->save(); - }); + Transaction::where('account_id', $this->reconciliation->account_id) + ->reconciled() + ->whereBetween('paid_at', [$this->reconciliation->started_at, $this->reconciliation->ended_at])->each(function ($transaction) { + $transaction->reconciled = 0; + $transaction->save(); + }); + }); return true; } diff --git a/app/Jobs/Banking/DeleteTransaction.php b/app/Jobs/Banking/DeleteTransaction.php index 1b7895d13..69a36ab51 100644 --- a/app/Jobs/Banking/DeleteTransaction.php +++ b/app/Jobs/Banking/DeleteTransaction.php @@ -28,8 +28,10 @@ class DeleteTransaction extends Job { $this->authorize(); - $this->transaction->recurring()->delete(); - $this->transaction->delete(); + \DB::transaction(function () { + $this->transaction->recurring()->delete(); + $this->transaction->delete(); + }); return true; } diff --git a/app/Jobs/Banking/DeleteTransfer.php b/app/Jobs/Banking/DeleteTransfer.php index 69e6fce57..6bbd58ac5 100644 --- a/app/Jobs/Banking/DeleteTransfer.php +++ b/app/Jobs/Banking/DeleteTransfer.php @@ -25,9 +25,11 @@ class DeleteTransfer extends Job */ public function handle() { - $this->transfer->expense_transaction->delete(); - $this->transfer->income_transaction->delete(); - $this->transfer->delete(); + \DB::transaction(function () { + $this->transfer->expense_transaction->delete(); + $this->transfer->income_transaction->delete(); + $this->transfer->delete(); + }); return true; } diff --git a/app/Jobs/Banking/UpdateAccount.php b/app/Jobs/Banking/UpdateAccount.php index a8ab00a0b..8854ba1ea 100644 --- a/app/Jobs/Banking/UpdateAccount.php +++ b/app/Jobs/Banking/UpdateAccount.php @@ -32,13 +32,15 @@ class UpdateAccount extends Job { $this->authorize(); - $this->account->update($this->request->all()); + \DB::transaction(function () { + $this->account->update($this->request->all()); - // Set default account - if ($this->request['default_account']) { - setting()->set('default.account', $this->account->id); - setting()->save(); - } + // Set default account + if ($this->request['default_account']) { + setting()->set('default.account', $this->account->id); + setting()->save(); + } + }); return $this->account; } diff --git a/app/Jobs/Banking/UpdateReconciliation.php b/app/Jobs/Banking/UpdateReconciliation.php index b7fa2f413..344dc83dd 100644 --- a/app/Jobs/Banking/UpdateReconciliation.php +++ b/app/Jobs/Banking/UpdateReconciliation.php @@ -31,25 +31,27 @@ class UpdateReconciliation extends Job */ public function handle() { - $reconcile = $this->request->get('reconcile'); - $transactions = $this->request->get('transactions'); + \DB::transaction(function () { + $reconcile = $this->request->get('reconcile'); + $transactions = $this->request->get('transactions'); - $this->reconciliation->reconciled = $reconcile ? 1 : 0; - $this->reconciliation->save(); + $this->reconciliation->reconciled = $reconcile ? 1 : 0; + $this->reconciliation->save(); - if ($transactions) { - foreach ($transactions as $key => $value) { - if (empty($value)) { - continue; + if ($transactions) { + foreach ($transactions as $key => $value) { + if (empty($value)) { + 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; } diff --git a/app/Jobs/Banking/UpdateTransaction.php b/app/Jobs/Banking/UpdateTransaction.php index 131a16320..b444e57fd 100644 --- a/app/Jobs/Banking/UpdateTransaction.php +++ b/app/Jobs/Banking/UpdateTransaction.php @@ -32,17 +32,19 @@ class UpdateTransaction extends Job { $this->authorize(); - $this->transaction->update($this->request->all()); + \DB::transaction(function () { + $this->transaction->update($this->request->all()); - // Upload attachment - if ($this->request->file('attachment')) { - $media = $this->getMedia($this->request->file('attachment'), 'transactions'); + // Upload attachment + if ($this->request->file('attachment')) { + $media = $this->getMedia($this->request->file('attachment'), 'transactions'); - $this->transaction->attachMedia($media, 'attachment'); - } + $this->transaction->attachMedia($media, 'attachment'); + } - // Recurring - $this->transaction->updateRecurring(); + // Recurring + $this->transaction->updateRecurring(); + }); return $this->transaction; } diff --git a/app/Jobs/Banking/UpdateTransfer.php b/app/Jobs/Banking/UpdateTransfer.php index 4437936e2..d9193a778 100644 --- a/app/Jobs/Banking/UpdateTransfer.php +++ b/app/Jobs/Banking/UpdateTransfer.php @@ -34,78 +34,80 @@ class UpdateTransfer extends Job */ 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(); - $income_currency_code = Account::where('id', $this->request->get('to_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(); - $expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id); - $income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id); + $expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id); + $income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id); - $expense_transaction->update([ - 'company_id' => $this->request['company_id'], - 'type' => 'expense', - 'account_id' => $this->request->get('from_account_id'), - 'paid_at' => $this->request->get('transferred_at'), - 'currency_code' => $expense_currency_code, - 'currency_rate' => $currencies[$expense_currency_code], - 'amount' => $this->request->get('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'), - ]); + $expense_transaction->update([ + 'company_id' => $this->request['company_id'], + 'type' => 'expense', + 'account_id' => $this->request->get('from_account_id'), + 'paid_at' => $this->request->get('transferred_at'), + 'currency_code' => $expense_currency_code, + 'currency_rate' => $currencies[$expense_currency_code], + 'amount' => $this->request->get('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'), + ]); - // Convert amount if not same currency - if ($expense_currency_code != $income_currency_code) { - $default_currency = setting('default.currency', 'USD'); + // Convert amount if not same currency + if ($expense_currency_code != $income_currency_code) { + $default_currency = setting('default.currency', 'USD'); - $default_amount = $this->request->get('amount'); + $default_amount = $this->request->get('amount'); - if ($default_currency != $expense_currency_code) { - $default_amount_model = new Transfer(); + if ($default_currency != $expense_currency_code) { + $default_amount_model = new Transfer(); - $default_amount_model->default_currency_code = $default_currency; - $default_amount_model->amount = $this->request->get('amount'); - $default_amount_model->currency_code = $expense_currency_code; - $default_amount_model->currency_rate = $currencies[$expense_currency_code]; + $default_amount_model->default_currency_code = $default_currency; + $default_amount_model->amount = $this->request->get('amount'); + $default_amount_model->currency_code = $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; - $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'); - } - - $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, - ]); + $this->transfer->update([ + 'company_id' => $this->request['company_id'], + 'expense_transaction_id' => $expense_transaction->id, + 'income_transaction_id' => $income_transaction->id, + ]); + }); return $this->transfer; } diff --git a/app/Jobs/Common/CreateCompany.php b/app/Jobs/Common/CreateCompany.php index e85bbb7d3..339a05692 100644 --- a/app/Jobs/Common/CreateCompany.php +++ b/app/Jobs/Common/CreateCompany.php @@ -8,10 +8,10 @@ use Artisan; class CreateCompany extends Job { - protected $request; - protected $company; + protected $request; + /** * Create a new job instance. * @@ -29,15 +29,17 @@ class CreateCompany extends Job */ public function handle() { - $this->company = Company::create($this->request->all()); + \DB::transaction(function () { + $this->company = Company::create($this->request->all()); - // Clear settings - setting()->setExtraColumns(['company_id' => $this->company->id]); - setting()->forgetAll(); + // Clear settings + setting()->setExtraColumns(['company_id' => $this->company->id]); + setting()->forgetAll(); - $this->callSeeds(); + $this->callSeeds(); - $this->updateSettings(); + $this->updateSettings(); + }); return $this->company; } diff --git a/app/Jobs/Common/CreateContact.php b/app/Jobs/Common/CreateContact.php index 2cf3710bc..9f14e88d5 100644 --- a/app/Jobs/Common/CreateContact.php +++ b/app/Jobs/Common/CreateContact.php @@ -9,6 +9,8 @@ use App\Models\Common\Contact; class CreateContact extends Job { + protected $contact; + protected $request; /** @@ -28,13 +30,15 @@ class CreateContact extends Job */ public function handle() { - if ($this->request->get('create_user', 'false') === 'true') { - $this->createUser(); - } + \DB::transaction(function () { + 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() diff --git a/app/Jobs/Common/CreateDashboard.php b/app/Jobs/Common/CreateDashboard.php index a610d508b..99b196af2 100644 --- a/app/Jobs/Common/CreateDashboard.php +++ b/app/Jobs/Common/CreateDashboard.php @@ -9,6 +9,8 @@ use App\Utilities\Widgets; class CreateDashboard extends Job { + protected $dashboard; + protected $request; /** @@ -30,13 +32,15 @@ class CreateDashboard extends Job { $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')) { - $this->createWidgets(); - } + if ($this->request->has('with_widgets')) { + $this->createWidgets(); + } + }); return $this->dashboard; } diff --git a/app/Jobs/Common/CreateItem.php b/app/Jobs/Common/CreateItem.php index 76d850bad..121ac4995 100644 --- a/app/Jobs/Common/CreateItem.php +++ b/app/Jobs/Common/CreateItem.php @@ -7,6 +7,8 @@ use App\Models\Common\Item; class CreateItem extends Job { + protected $item; + protected $request; /** @@ -26,15 +28,17 @@ class CreateItem extends Job */ public function handle() { - $item = Item::create($this->request->all()); + \DB::transaction(function () { + $this->item = Item::create($this->request->all()); - // Upload picture - if ($this->request->file('picture')) { - $media = $this->getMedia($this->request->file('picture'), 'items'); + // Upload picture + if ($this->request->file('picture')) { + $media = $this->getMedia($this->request->file('picture'), 'items'); - $item->attachMedia($media, 'picture'); - } + $this->item->attachMedia($media, 'picture'); + } + }); - return $item; + return $this->item; } } diff --git a/app/Jobs/Common/CreateReport.php b/app/Jobs/Common/CreateReport.php index cd697f4ae..d02038297 100644 --- a/app/Jobs/Common/CreateReport.php +++ b/app/Jobs/Common/CreateReport.php @@ -7,6 +7,8 @@ use App\Models\Common\Report; class CreateReport extends Job { + protected $report; + protected $request; /** @@ -26,8 +28,10 @@ class CreateReport extends Job */ public function handle() { - $report = Report::create($this->request->all()); + \DB::transaction(function () { + $this->report = Report::create($this->request->all()); + }); - return $report; + return $this->report; } } diff --git a/app/Jobs/Common/DeleteCompany.php b/app/Jobs/Common/DeleteCompany.php index cd23f9b9b..3996900c4 100644 --- a/app/Jobs/Common/DeleteCompany.php +++ b/app/Jobs/Common/DeleteCompany.php @@ -31,14 +31,16 @@ class DeleteCompany extends Job { $this->authorize(); - $this->deleteRelationships($this->company, [ - 'accounts', 'bills', 'bill_histories', 'bill_items', 'bill_item_taxes', 'bill_totals', 'categories', - 'contacts', 'currencies', 'dashboards', 'email_templates', 'invoices', 'invoice_histories', 'invoice_items', - 'invoice_item_taxes', 'invoice_totals', 'items', 'modules', 'module_histories', 'reconciliations', - 'recurring', 'reports', 'settings', 'taxes', 'transactions', 'transfers', 'widgets', - ]); + \DB::transaction(function () { + $this->deleteRelationships($this->company, [ + 'accounts', 'bills', 'bill_histories', 'bill_items', 'bill_item_taxes', 'bill_totals', 'categories', + 'contacts', 'currencies', 'dashboards', 'email_templates', 'invoices', 'invoice_histories', 'invoice_items', + '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; } diff --git a/app/Jobs/Common/DeleteContact.php b/app/Jobs/Common/DeleteContact.php index 6b7e96489..ba7bc19f7 100644 --- a/app/Jobs/Common/DeleteContact.php +++ b/app/Jobs/Common/DeleteContact.php @@ -31,11 +31,13 @@ class DeleteContact extends Job { $this->authorize(); - if ($user = $this->contact->user) { - $this->dispatch(new DeleteUser($user)); - } + \DB::transaction(function () { + if ($user = $this->contact->user) { + $this->dispatch(new DeleteUser($user)); + } - $this->contact->delete(); + $this->contact->delete(); + }); return true; } diff --git a/app/Jobs/Common/DeleteDashboard.php b/app/Jobs/Common/DeleteDashboard.php index debf4d0ff..73fe976a6 100644 --- a/app/Jobs/Common/DeleteDashboard.php +++ b/app/Jobs/Common/DeleteDashboard.php @@ -30,9 +30,11 @@ class DeleteDashboard extends Job { $this->authorize(); - $this->deleteRelationships($this->dashboard, ['widgets']); + \DB::transaction(function () { + $this->deleteRelationships($this->dashboard, ['widgets']); - $this->dashboard->delete(); + $this->dashboard->delete(); + }); return true; } diff --git a/app/Jobs/Common/DeleteItem.php b/app/Jobs/Common/DeleteItem.php index 6f9cfeace..8b194adc1 100644 --- a/app/Jobs/Common/DeleteItem.php +++ b/app/Jobs/Common/DeleteItem.php @@ -27,7 +27,9 @@ class DeleteItem extends Job { $this->authorize(); - $this->item->delete(); + \DB::transaction(function () { + $this->item->delete(); + }); return true; } diff --git a/app/Jobs/Common/DeleteReport.php b/app/Jobs/Common/DeleteReport.php index 7af2e789c..303b04f9d 100644 --- a/app/Jobs/Common/DeleteReport.php +++ b/app/Jobs/Common/DeleteReport.php @@ -25,7 +25,9 @@ class DeleteReport extends Job */ public function handle() { - $this->report->delete(); + \DB::transaction(function () { + $this->report->delete(); + }); return true; } diff --git a/app/Jobs/Common/UpdateCompany.php b/app/Jobs/Common/UpdateCompany.php index 0e214d83c..06301c110 100644 --- a/app/Jobs/Common/UpdateCompany.php +++ b/app/Jobs/Common/UpdateCompany.php @@ -35,45 +35,47 @@ class UpdateCompany extends Job { $this->authorize(); - $this->company->update($this->request->all()); + \DB::transaction(function () { + $this->company->update($this->request->all()); - // Clear current and load given company settings - setting()->setExtraColumns(['company_id' => $this->company->id]); - setting()->forgetAll(); - setting()->load(true); + // Clear current and load given company settings + setting()->setExtraColumns(['company_id' => $this->company->id]); + setting()->forgetAll(); + 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); + if ($this->request->has('name')) { + setting()->set('company.name', $this->request->get('name')); } - } - setting()->save(); - setting()->forgetAll(); + 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; } diff --git a/app/Jobs/Common/UpdateContact.php b/app/Jobs/Common/UpdateContact.php index 504228e7d..9c8c9531c 100644 --- a/app/Jobs/Common/UpdateContact.php +++ b/app/Jobs/Common/UpdateContact.php @@ -33,11 +33,13 @@ class UpdateContact extends Job { $this->authorize(); - if ($this->request->get('create_user', 'false') === 'true') { - $this->createUser(); - } + \DB::transaction(function () { + 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; } diff --git a/app/Jobs/Common/UpdateDashboard.php b/app/Jobs/Common/UpdateDashboard.php index 729a47034..736c3763a 100644 --- a/app/Jobs/Common/UpdateDashboard.php +++ b/app/Jobs/Common/UpdateDashboard.php @@ -35,11 +35,13 @@ class UpdateDashboard extends Job { $this->authorize(); - $this->dashboard->update($this->request->all()); + \DB::transaction(function () { + $this->dashboard->update($this->request->all()); - if ($this->request->has('users')) { - $this->dashboard->users()->sync($this->request->get('users')); - } + if ($this->request->has('users')) { + $this->dashboard->users()->sync($this->request->get('users')); + } + }); return $this->dashboard; } diff --git a/app/Jobs/Common/UpdateItem.php b/app/Jobs/Common/UpdateItem.php index ed4e0bc66..1ebc52a53 100644 --- a/app/Jobs/Common/UpdateItem.php +++ b/app/Jobs/Common/UpdateItem.php @@ -30,14 +30,16 @@ class UpdateItem extends Job */ public function handle() { - $this->item->update($this->request->all()); + \DB::transaction(function () { + $this->item->update($this->request->all()); - // Upload picture - if ($this->request->file('picture')) { - $media = $this->getMedia($this->request->file('picture'), 'items'); + // Upload picture + if ($this->request->file('picture')) { + $media = $this->getMedia($this->request->file('picture'), 'items'); - $this->item->attachMedia($media, 'picture'); - } + $this->item->attachMedia($media, 'picture'); + } + }); return $this->item; } diff --git a/app/Jobs/Common/UpdateReport.php b/app/Jobs/Common/UpdateReport.php index 4f5eb3733..20cae5f0a 100644 --- a/app/Jobs/Common/UpdateReport.php +++ b/app/Jobs/Common/UpdateReport.php @@ -30,7 +30,9 @@ class UpdateReport extends Job */ public function handle() { - $this->report->update($this->request->all()); + \DB::transaction(function () { + $this->report->update($this->request->all()); + }); return $this->report; } diff --git a/app/Jobs/Purchase/CancelBill.php b/app/Jobs/Purchase/CancelBill.php index 11d7350a2..b86438364 100644 --- a/app/Jobs/Purchase/CancelBill.php +++ b/app/Jobs/Purchase/CancelBill.php @@ -26,12 +26,14 @@ class CancelBill extends Job */ public function handle() { - $this->deleteRelationships($this->bill, [ - 'transactions', 'recurring' - ]); + \DB::transaction(function () { + $this->deleteRelationships($this->bill, [ + 'transactions', 'recurring' + ]); - $this->bill->status = 'cancelled'; - $this->bill->save(); + $this->bill->status = 'cancelled'; + $this->bill->save(); + }); return $this->bill; } diff --git a/app/Jobs/Purchase/CreateBill.php b/app/Jobs/Purchase/CreateBill.php index a75356ab8..12093253c 100644 --- a/app/Jobs/Purchase/CreateBill.php +++ b/app/Jobs/Purchase/CreateBill.php @@ -14,10 +14,10 @@ class CreateBill extends Job { use Currencies, DateTime; - protected $request; - protected $bill; + protected $request; + /** * Create a new job instance. * @@ -41,20 +41,22 @@ class CreateBill extends Job event(new BillCreating($this->request)); - $this->bill = Bill::create($this->request->all()); + \DB::transaction(function () { + $this->bill = Bill::create($this->request->all()); - // Upload attachment - if ($this->request->file('attachment')) { - $media = $this->getMedia($this->request->file('attachment'), 'bills'); + // Upload attachment + 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(); - $this->bill->update($this->request->input()); + $this->bill->update($this->request->input()); - $this->bill->createRecurring(); + $this->bill->createRecurring(); + }); event(new BillCreated($this->bill)); diff --git a/app/Jobs/Purchase/DeleteBill.php b/app/Jobs/Purchase/DeleteBill.php index 0bc472922..d80b80c86 100644 --- a/app/Jobs/Purchase/DeleteBill.php +++ b/app/Jobs/Purchase/DeleteBill.php @@ -3,7 +3,6 @@ namespace App\Jobs\Purchase; use App\Abstracts\Job; -use App\Models\Purchase\Bill; use App\Observers\Transaction; class DeleteBill extends Job @@ -29,15 +28,17 @@ class DeleteBill extends Job { $this->authorize(); - Transaction::mute(); + \DB::transaction(function () { + Transaction::mute(); - $this->deleteRelationships($this->bill, [ - 'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals' - ]); + $this->deleteRelationships($this->bill, [ + 'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals' + ]); - $this->bill->delete(); + $this->bill->delete(); - Transaction::unmute(); + Transaction::unmute(); + }); return true; } diff --git a/app/Jobs/Purchase/DuplicateBill.php b/app/Jobs/Purchase/DuplicateBill.php index de49f19e7..e04eefdf5 100644 --- a/app/Jobs/Purchase/DuplicateBill.php +++ b/app/Jobs/Purchase/DuplicateBill.php @@ -3,13 +3,15 @@ namespace App\Jobs\Purchase; use App\Abstracts\Job; +use App\Events\Purchase\BillCreated; use App\Models\Purchase\Bill; -use App\Models\Purchase\BillHistory; class DuplicateBill extends Job { protected $bill; + protected $clone; + /** * Create a new job instance. * @@ -27,17 +29,12 @@ class DuplicateBill extends Job */ public function handle() { - $clone = $this->bill->duplicate(); + \DB::transaction(function () { + $this->clone = $this->bill->duplicate(); + }); - // Add bill history - BillHistory::create([ - 'company_id' => session('company_id'), - 'bill_id' => $clone->id, - 'status' => 'draft', - 'notify' => 0, - 'description' => trans('messages.success.added', ['type' => $clone->bill_number]), - ]); + event(new BillCreated($this->clone)); - return $clone; + return $this->clone; } } diff --git a/app/Jobs/Purchase/UpdateBill.php b/app/Jobs/Purchase/UpdateBill.php index fbee067b2..5d4563ea1 100644 --- a/app/Jobs/Purchase/UpdateBill.php +++ b/app/Jobs/Purchase/UpdateBill.php @@ -43,26 +43,28 @@ class UpdateBill extends Job event(new BillUpdating($this->bill, $this->request)); - // Upload attachment - if ($this->request->file('attachment')) { - $media = $this->getMedia($this->request->file('attachment'), 'bills'); + \DB::transaction(function () { + // Upload attachment + 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) { - $this->request['status'] = 'partial'; - } + if (($bill_paid) && $this->request['amount'] > $bill_paid) { + $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)); diff --git a/app/Jobs/Sale/CancelInvoice.php b/app/Jobs/Sale/CancelInvoice.php index de9215994..9240539e6 100644 --- a/app/Jobs/Sale/CancelInvoice.php +++ b/app/Jobs/Sale/CancelInvoice.php @@ -26,12 +26,14 @@ class CancelInvoice extends Job */ public function handle() { - $this->deleteRelationships($this->invoice, [ - 'transactions', 'recurring' - ]); + \DB::transaction(function () { + $this->deleteRelationships($this->invoice, [ + 'transactions', 'recurring' + ]); - $this->invoice->status = 'cancelled'; - $this->invoice->save(); + $this->invoice->status = 'cancelled'; + $this->invoice->save(); + }); return $this->invoice; } diff --git a/app/Jobs/Sale/CreateInvoice.php b/app/Jobs/Sale/CreateInvoice.php index c22608fc5..2bf832e9f 100644 --- a/app/Jobs/Sale/CreateInvoice.php +++ b/app/Jobs/Sale/CreateInvoice.php @@ -14,10 +14,10 @@ class CreateInvoice extends Job { use Currencies, DateTime; - protected $request; - protected $invoice; + protected $request; + /** * Create a new job instance. * @@ -41,20 +41,22 @@ class CreateInvoice extends Job event(new InvoiceCreating($this->request)); - $this->invoice = Invoice::create($this->request->all()); + \DB::transaction(function () { + $this->invoice = Invoice::create($this->request->all()); - // Upload attachment - if ($this->request->file('attachment')) { - $media = $this->getMedia($this->request->file('attachment'), 'invoices'); + // Upload attachment + 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(); - $this->invoice->update($this->request->all()); + $this->invoice->update($this->request->all()); - $this->invoice->createRecurring(); + $this->invoice->createRecurring(); + }); event(new InvoiceCreated($this->invoice)); diff --git a/app/Jobs/Sale/DeleteInvoice.php b/app/Jobs/Sale/DeleteInvoice.php index 1a6acfd05..693e79e2c 100644 --- a/app/Jobs/Sale/DeleteInvoice.php +++ b/app/Jobs/Sale/DeleteInvoice.php @@ -3,7 +3,6 @@ namespace App\Jobs\Sale; use App\Abstracts\Job; -use App\Models\Sale\Invoice; use App\Observers\Transaction; class DeleteInvoice extends Job @@ -29,15 +28,17 @@ class DeleteInvoice extends Job { $this->authorize(); - Transaction::mute(); + \DB::transaction(function () { + Transaction::mute(); - $this->deleteRelationships($this->invoice, [ - 'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals' - ]); + $this->deleteRelationships($this->invoice, [ + 'items', 'item_taxes', 'histories', 'transactions', 'recurring', 'totals' + ]); - $this->invoice->delete(); + $this->invoice->delete(); - Transaction::unmute(); + Transaction::unmute(); + }); return true; } diff --git a/app/Jobs/Sale/DuplicateInvoice.php b/app/Jobs/Sale/DuplicateInvoice.php index cf401a11a..510a34e07 100644 --- a/app/Jobs/Sale/DuplicateInvoice.php +++ b/app/Jobs/Sale/DuplicateInvoice.php @@ -10,6 +10,8 @@ class DuplicateInvoice extends Job { protected $invoice; + protected $clone; + /** * Create a new job instance. * @@ -27,10 +29,12 @@ class DuplicateInvoice extends Job */ 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; } } diff --git a/app/Jobs/Sale/UpdateInvoice.php b/app/Jobs/Sale/UpdateInvoice.php index 86c52d051..34f147fd6 100644 --- a/app/Jobs/Sale/UpdateInvoice.php +++ b/app/Jobs/Sale/UpdateInvoice.php @@ -43,26 +43,28 @@ class UpdateInvoice extends Job event(new InvoiceUpdating($this->invoice, $this->request)); - // Upload attachment - if ($this->request->file('attachment')) { - $media = $this->getMedia($this->request->file('attachment'), 'invoices'); + \DB::transaction(function () { + // Upload attachment + 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) { - $this->request['status'] = 'partial'; - } + if (($invoice_paid) && $this->request['amount'] > $invoice_paid) { + $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)); diff --git a/app/Jobs/Setting/CreateCategory.php b/app/Jobs/Setting/CreateCategory.php index 1a1bac422..58d13d0cc 100644 --- a/app/Jobs/Setting/CreateCategory.php +++ b/app/Jobs/Setting/CreateCategory.php @@ -7,6 +7,8 @@ use App\Models\Setting\Category; class CreateCategory extends Job { + protected $category; + protected $request; /** @@ -26,8 +28,10 @@ class CreateCategory extends Job */ public function handle() { - $category = Category::create($this->request->all()); + \DB::transaction(function () { + $this->category = Category::create($this->request->all()); + }); - return $category; + return $this->category; } } diff --git a/app/Jobs/Setting/CreateCurrency.php b/app/Jobs/Setting/CreateCurrency.php index 7b6e219bd..15db00ee6 100644 --- a/app/Jobs/Setting/CreateCurrency.php +++ b/app/Jobs/Setting/CreateCurrency.php @@ -7,6 +7,8 @@ use App\Models\Setting\Currency; class CreateCurrency extends Job { + protected $currency; + protected $request; /** @@ -31,14 +33,16 @@ class CreateCurrency extends Job $this->request['rate'] = '1'; } - $currency = Currency::create($this->request->all()); + \DB::transaction(function () { + $this->currency = Currency::create($this->request->all()); - // Update default currency setting - if ($this->request->get('default_currency')) { - setting()->set('default.currency', $this->request->get('code')); - setting()->save(); - } + // Update default currency setting + if ($this->request->get('default_currency')) { + setting()->set('default.currency', $this->request->get('code')); + setting()->save(); + } + }); - return $currency; + return $this->currency; } } diff --git a/app/Jobs/Setting/CreateTax.php b/app/Jobs/Setting/CreateTax.php index 9c1a7c5e7..3084813e0 100644 --- a/app/Jobs/Setting/CreateTax.php +++ b/app/Jobs/Setting/CreateTax.php @@ -7,6 +7,8 @@ use App\Models\Setting\Tax; class CreateTax extends Job { + protected $tax; + protected $request; /** @@ -26,8 +28,10 @@ class CreateTax extends Job */ public function handle() { - $tax = Tax::create($this->request->all()); + \DB::transaction(function () { + $this->tax = Tax::create($this->request->all()); + }); - return $tax; + return $this->tax; } } diff --git a/app/Jobs/Setting/DeleteCategory.php b/app/Jobs/Setting/DeleteCategory.php index 82b3906f6..68a55e5f7 100644 --- a/app/Jobs/Setting/DeleteCategory.php +++ b/app/Jobs/Setting/DeleteCategory.php @@ -28,7 +28,9 @@ class DeleteCategory extends Job { $this->authorize(); - $this->category->delete(); + \DB::transaction(function () { + $this->category->delete(); + }); return true; } diff --git a/app/Jobs/Setting/DeleteCurrency.php b/app/Jobs/Setting/DeleteCurrency.php index 7ea36011f..9ff5e844f 100644 --- a/app/Jobs/Setting/DeleteCurrency.php +++ b/app/Jobs/Setting/DeleteCurrency.php @@ -27,7 +27,9 @@ class DeleteCurrency extends Job { $this->authorize(); - $this->currency->delete(); + \DB::transaction(function () { + $this->currency->delete(); + }); return true; } diff --git a/app/Jobs/Setting/DeleteTax.php b/app/Jobs/Setting/DeleteTax.php index 4f7fb8a45..21bf97ef3 100644 --- a/app/Jobs/Setting/DeleteTax.php +++ b/app/Jobs/Setting/DeleteTax.php @@ -27,7 +27,9 @@ class DeleteTax extends Job { $this->authorize(); - $this->tax->delete(); + \DB::transaction(function () { + $this->tax->delete(); + }); return true; } diff --git a/app/Jobs/Setting/UpdateCategory.php b/app/Jobs/Setting/UpdateCategory.php index a537f5a74..e5c3b8839 100644 --- a/app/Jobs/Setting/UpdateCategory.php +++ b/app/Jobs/Setting/UpdateCategory.php @@ -32,7 +32,9 @@ class UpdateCategory extends Job { $this->authorize(); - $this->category->update($this->request->all()); + \DB::transaction(function () { + $this->category->update($this->request->all()); + }); return $this->category; } diff --git a/app/Jobs/Setting/UpdateCurrency.php b/app/Jobs/Setting/UpdateCurrency.php index b11410436..808215aae 100644 --- a/app/Jobs/Setting/UpdateCurrency.php +++ b/app/Jobs/Setting/UpdateCurrency.php @@ -37,13 +37,15 @@ class UpdateCurrency extends Job $this->request['rate'] = '1'; } - $this->currency->update($this->request->all()); + \DB::transaction(function () { + $this->currency->update($this->request->all()); - // Update default currency setting - if ($this->request->get('default_currency')) { - setting()->set('default.currency', $this->request->get('code')); - setting()->save(); - } + // Update default currency setting + if ($this->request->get('default_currency')) { + setting()->set('default.currency', $this->request->get('code')); + setting()->save(); + } + }); return $this->currency; } diff --git a/app/Jobs/Setting/UpdateTax.php b/app/Jobs/Setting/UpdateTax.php index dd49f2362..ff2644e01 100644 --- a/app/Jobs/Setting/UpdateTax.php +++ b/app/Jobs/Setting/UpdateTax.php @@ -32,7 +32,9 @@ class UpdateTax extends Job { $this->authorize(); - $this->tax->update($this->request->all()); + \DB::transaction(function () { + $this->tax->update($this->request->all()); + }); return $this->tax; } diff --git a/app/Traits/Jobs.php b/app/Traits/Jobs.php index c8fcb1a20..731467584 100644 --- a/app/Traits/Jobs.php +++ b/app/Traits/Jobs.php @@ -2,6 +2,9 @@ namespace App\Traits; +use Exception; +use Throwable; + trait Jobs { /** @@ -48,7 +51,7 @@ trait Jobs 'data' => $data, 'message' => '', ]; - } catch(\Exception $e) { + } catch (Exception | Throwable $e) { $response = [ 'success' => false, 'error' => true,