added db transaction to jobs

This commit is contained in:
Denis Duliçi
2020-06-26 13:40:19 +03:00
parent f20f5c9def
commit acdc9da2c8
57 changed files with 606 additions and 469 deletions

View File

@@ -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;
}
}

View File

@@ -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));

View File

@@ -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;
}
}

View File

@@ -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));

View File

@@ -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;
}
}

View File

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

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}