akaunting 3.0 (the last dance)

This commit is contained in:
Burak Civan
2022-06-01 10:15:55 +03:00
parent cead09f6d4
commit d9c0764572
3812 changed files with 126831 additions and 102949 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\InvitationCreated;
use App\Models\Auth\UserInvitation;
use Illuminate\Support\Str;
class CreateInvitation extends Job
{
protected $invitation;
protected $user;
protected $company;
public function __construct($user, $company)
{
$this->user = $user;
$this->company = $company;
}
public function handle(): UserInvitation
{
\DB::transaction(function () {
if ($this->user->hasPendingInvitation($this->company->id)) {
$pending_invitation = $this->user->getPendingInvitation($this->company->id);
$this->dispatch(new DeleteInvitation($pending_invitation));
}
$this->invitation = UserInvitation::create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'token' => (string) Str::uuid(),
]);
});
event(new InvitationCreated($this->invitation));
return $this->invitation;
}
}

View File

@@ -3,6 +3,8 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\RoleCreated;
use App\Events\Auth\RoleCreating;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
use App\Interfaces\Job\ShouldCreate;
@@ -12,6 +14,8 @@ class CreateRole extends Job implements HasOwner, HasSource, ShouldCreate
{
public function handle(): Role
{
event(new RoleCreating($this->request));
\DB::transaction(function () {
$this->model = Role::create($this->request->input());
@@ -20,6 +24,8 @@ class CreateRole extends Job implements HasOwner, HasSource, ShouldCreate
}
});
event(new RoleCreated($this->model, $this->request));
return $this->model;
}
}

View File

@@ -3,13 +3,14 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\UserCreated;
use App\Events\Auth\UserCreating;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
use App\Interfaces\Job\ShouldCreate;
use App\Events\Auth\UserCreated;
use App\Events\Auth\UserCreating;
use App\Models\Auth\User;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate
{
@@ -18,6 +19,10 @@ class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate
event(new UserCreating($this->request));
\DB::transaction(function () {
if (! app()->runningInConsole() && ! request()->isInstall()) {
$this->request->merge(['password' => Str::random(40)]);
}
$this->model = User::create($this->request->input());
// Upload picture
@@ -64,6 +69,12 @@ class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate
'user' => $this->model->id,
'company' => $company->id,
]);
if (app()->runningInConsole() || request()->isInstall()) {
continue;
}
$this->dispatch(new CreateInvitation($this->model, $company));
}
});

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldDelete;
class DeleteInvitation extends Job implements ShouldDelete
{
public function handle(): bool
{
\DB::transaction(function () {
$this->model->delete();
});
return true;
}
}

View File

@@ -3,18 +3,24 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\RoleDeleted;
use App\Events\Auth\RoleDeleting;
use App\Interfaces\Job\ShouldDelete;
class DeleteRole extends Job implements ShouldDelete
{
public function handle(): bool
{
event(new RoleDeleting($this->model));
\DB::transaction(function () {
$this->model->delete();
$this->model->flushCache();
});
event(new RoleDeleted($this->model));
return true;
}
}

View File

@@ -3,6 +3,8 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\UserDeleted;
use App\Events\Auth\UserDeleting;
use App\Interfaces\Job\ShouldDelete;
class DeleteUser extends Job implements ShouldDelete
@@ -11,12 +13,16 @@ class DeleteUser extends Job implements ShouldDelete
{
$this->authorize();
event(new UserDeleting($this->model));
\DB::transaction(function () {
$this->model->delete();
$this->model->flushCache();
});
event(new UserDeleted($this->model));
return true;
}

View File

@@ -3,6 +3,8 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Events\Auth\RoleUpdated;
use App\Events\Auth\RoleUpdating;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Auth\Role;
@@ -10,6 +12,8 @@ class UpdateRole extends Job implements ShouldUpdate
{
public function handle(): Role
{
event(new RoleUpdating($this->model, $this->request));
\DB::transaction(function () {
$this->model->update($this->request->all());
@@ -18,6 +22,8 @@ class UpdateRole extends Job implements ShouldUpdate
}
});
event(new RoleUpdated($this->model, $this->request));
return $this->model;
}
}

View File

@@ -3,10 +3,12 @@
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Events\Auth\UserUpdated;
use App\Events\Auth\UserUpdating;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Auth\User;
use App\Models\Common\Company;
use Illuminate\Support\Facades\Artisan;
class UpdateUser extends Job implements ShouldUpdate
{
@@ -16,6 +18,7 @@ class UpdateUser extends Job implements ShouldUpdate
// Do not reset password if not entered/changed
if (empty($this->request['password'])) {
unset($this->request['current_password']);
unset($this->request['password']);
unset($this->request['password_confirmation']);
}
@@ -38,7 +41,7 @@ class UpdateUser extends Job implements ShouldUpdate
if ($this->request->has('companies')) {
if (app()->runningInConsole() || request()->isInstall()) {
$this->model->companies()->sync($this->request->get('companies'));
$sync = $this->model->companies()->sync($this->request->get('companies'));
} else {
$user = user();
@@ -47,7 +50,7 @@ class UpdateUser extends Job implements ShouldUpdate
});
if ($companies->isNotEmpty()) {
$this->model->companies()->sync($companies->toArray());
$sync = $this->model->companies()->sync($companies->toArray());
}
}
}
@@ -55,6 +58,31 @@ class UpdateUser extends Job implements ShouldUpdate
if ($this->model->contact) {
$this->model->contact->update($this->request->input());
}
if (isset($sync) && !empty($sync['attached'])) {
foreach ($sync['attached'] as $id) {
$company = Company::find($id);
Artisan::call('user:seed', [
'user' => $this->model->id,
'company' => $company->id,
]);
$this->dispatch(new CreateInvitation($this->model, $company));
}
}
if (isset($sync) && !empty($sync['detached'])) {
foreach ($sync['detached'] as $id) {
$company = Company::find($id);
if ($this->model->hasPendingInvitation($company->id)) {
$pending_invitation = $this->model->getPendingInvitation($company->id);
$this->dispatch(new DeleteInvitation($pending_invitation));
}
}
}
});
event(new UserUpdated($this->model, $this->request));

View File

@@ -7,7 +7,6 @@ use App\Jobs\Banking\CreateTransaction;
use App\Jobs\Document\CreateDocumentHistory;
use App\Events\Document\PaidAmountCalculated;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Banking\Account;
use App\Models\Banking\Transaction;
use App\Models\Document\Document;
use App\Traits\Currencies;
@@ -71,20 +70,6 @@ class CreateBankingDocumentTransaction extends Job implements ShouldCreate
$this->request['category_id'] = isset($this->request['category_id']) ? $this->request['category_id'] : $this->model->category_id;
$this->request['payment_method'] = isset($this->request['payment_method']) ? $this->request['payment_method'] : setting('default.payment_method');
$this->request['notify'] = isset($this->request['notify']) ? $this->request['notify'] : 0;
if ($this->request['mark_paid'] && ($this->request['account_id'] == setting('default.account'))) {
$account = Account::find((int) $this->request['account_id']);
$code = $account->currency_code;
$rate = config('money.' . $account->currency_code . '.rate');
if ($account->currency_code != $this->model->currency_code) {
$this->request['currency_code'] = $code;
$this->request['currency_rate'] = $rate;
$this->request['amount'] = $this->convertBetween($this->request['amount'], $this->model->currency_code, $this->model->currency_rate, $code, $rate);
}
}
}
protected function checkAmount(): bool

View File

@@ -11,10 +11,11 @@ use App\Models\Banking\Account;
use App\Models\Banking\Transfer;
use App\Models\Setting\Category;
use App\Traits\Currencies;
use App\Traits\Transactions;
class CreateTransfer extends Job implements HasOwner, HasSource, ShouldCreate
{
use Currencies;
use Currencies, Transactions;
public function handle(): Transfer
{
@@ -28,6 +29,7 @@ class CreateTransfer extends Job implements HasOwner, HasSource, ShouldCreate
$expense_transaction = $this->dispatch(new CreateTransaction([
'company_id' => $this->request['company_id'],
'type' => 'expense',
'number' => $this->getNextTransactionNumber(),
'account_id' => $this->request->get('from_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $expense_currency_code,
@@ -51,6 +53,7 @@ class CreateTransfer extends Job implements HasOwner, HasSource, ShouldCreate
$income_transaction = $this->dispatch(new CreateTransaction([
'company_id' => $this->request['company_id'],
'type' => 'income',
'number' => $this->getNextTransactionNumber(),
'account_id' => $this->request->get('to_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $income_currency_code,

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
use App\Events\Document\PaidAmountCalculated;
use App\Jobs\Document\CreateDocumentHistory;
use App\Models\Banking\Transaction;
use App\Models\Document\Document;
use App\Traits\Currencies;
class MatchBankingDocumentTransaction extends Job
{
use Currencies;
protected $transaction;
public function __construct(Document $model, Transaction $transaction)
{
$this->model = $model;
$this->transaction = $transaction;
}
public function handle(): Transaction
{
$this->checkAmount();
\DB::transaction(function () {
$this->transaction = $this->dispatch(new UpdateTransaction($this->transaction, ['document_id' => $this->model->id]));
$this->model->save();
$this->createHistory();
});
return $this->transaction;
}
protected function checkAmount(): bool
{
$code = $this->transaction->currency_code;
$rate = $this->transaction->currency_rate;
$precision = config('money.' . $code . '.precision');
$amount = $this->transaction->amount = round($this->transaction->amount, $precision);
if ($this->model->currency_code != $code) {
$converted_amount = $this->convertBetween($amount, $code, $rate, $this->model->currency_code, $this->model->currency_rate);
$amount = round($converted_amount, $precision);
}
$this->model->paid_amount = $this->model->paid;
event(new PaidAmountCalculated($this->model));
$total_amount = round($this->model->amount - $this->model->paid_amount, $precision);
unset($this->model->reconciled);
unset($this->model->paid_amount);
$compare = bccomp($amount, $total_amount, $precision);
if ($compare === 1) {
$error_amount = $total_amount;
if ($this->model->currency_code != $code) {
$converted_amount = $this->convertBetween($total_amount, $this->model->currency_code, $this->model->currency_rate, $code, $rate);
$error_amount = round($converted_amount, $precision);
}
$message = trans('messages.error.over_match', ['type' => ucfirst($this->model->type), 'amount' => money($error_amount, $code, true)]);
throw new \Exception($message);
} else {
$this->model->status = ($compare === 0) ? 'paid' : 'partial';
}
return true;
}
protected function createHistory(): void
{
$history_desc = money((double) $this->transaction->amount, (string) $this->transaction->currency_code, true)->format() . ' ' . trans_choice('general.payments', 1);
$this->dispatch(new CreateDocumentHistory($this->model, 0, $history_desc));
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
use App\Events\Banking\TransactionSent;
use App\Models\Banking\Transaction;
use App\Notifications\Banking\Transaction as Notification;
class SendTransactionAsCustomMail extends Job
{
public function __construct($request, $template_alias)
{
$this->request = $request;
$this->template_alias = $template_alias;
}
public function handle(): void
{
$transaction = Transaction::find($this->request->get('transaction_id'));
$custom_mail = $this->request->only(['to', 'subject', 'body']);
if ($this->request->has('user_email')) {
$custom_mail['cc'] = user()->email;
}
// Notify the contact
$transaction->contact->notify(new Notification($transaction, $this->template_alias, true, $custom_mail));
event(new TransactionSent($transaction));
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Document\Document;
use Illuminate\Support\Str;
class SplitTransaction extends Job implements ShouldUpdate
{
public function handle(): array
{
$this->checkAmount();
\DB::transaction(function () {
foreach ($this->request->items as $item) {
$transaction = $this->model->duplicate();
$transaction->split_id = $this->model->id;
$transaction->amount = $item['amount'];
$transaction->save();
$item['split'] = $transaction;
// Match only if document_id is given
if (empty($item['document_id'])) {
return;
}
$document = Document::find($item['document_id']);
if (empty($document)) {
return;
}
$this->dispatch(new MatchBankingDocumentTransaction($document, $transaction));
}
$this->model->type = config('type.transaction.' . $this->model->type . '.split_type');
$this->model->save();
});
return $this->request->items;
}
protected function checkAmount(): bool
{
$total_amount = 0;
foreach ($this->request->items as $item) {
$total_amount += $item['amount'];
}
$precision = config('money.' . $this->model->currency_code . '.precision');
$compare = bccomp($total_amount, $this->model->amount, $precision);
if ($compare !== 0) {
$error_amount = $this->model->amount;
$message = trans('messages.error.same_amount', ['transaction' => ucfirst(trans_choice('general.' . Str::plural($this->model->type), 1)), 'amount' => money($error_amount, $this->model->currency_code, true)]);
throw new \Exception($message);
}
return true;
}
}

View File

@@ -0,0 +1,124 @@
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
use App\Jobs\Banking\CreateTransaction;
use App\Jobs\Document\CreateDocumentHistory;
use App\Events\Document\PaidAmountCalculated;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Banking\Transaction;
use App\Models\Document\Document;
use App\Traits\Currencies;
use App\Utilities\Date;
class UpdateBankingDocumentTransaction extends Job implements ShouldUpdate
{
use Currencies;
public function __construct(Document $model, Transaction $transaction, $request)
{
$this->model = $model;
$this->transaction = $transaction;
parent::__construct($request);
}
public function handle(): Transaction
{
$this->prepareRequest();
$this->checkAmount();
\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');
$this->transaction->attachMedia($media, 'attachment');
}
$this->model->save();
$this->createHistory();
});
return $this->transaction;
}
protected function prepareRequest(): void
{
if (!isset($this->request['amount'])) {
$this->model->paid_amount = $this->model->paid;
event(new PaidAmountCalculated($this->model));
$this->request['amount'] = $this->model->amount - $this->model->paid_amount;
}
$currency_code = !empty($this->request['currency_code']) ? $this->request['currency_code'] : $this->model->currency_code;
$this->request['company_id'] = $this->model->company_id;
$this->request['currency_code'] = isset($this->request['currency_code']) ? $this->request['currency_code'] : $this->model->currency_code;
$this->request['paid_at'] = isset($this->request['paid_at']) ? $this->request['paid_at'] : Date::now()->format('Y-m-d');
$this->request['currency_rate'] = config('money.' . $currency_code . '.rate');
$this->request['account_id'] = isset($this->request['account_id']) ? $this->request['account_id'] : setting('default.account');
$this->request['document_id'] = isset($this->request['document_id']) ? $this->request['document_id'] : $this->model->id;
$this->request['contact_id'] = isset($this->request['contact_id']) ? $this->request['contact_id'] : $this->model->contact_id;
$this->request['category_id'] = isset($this->request['category_id']) ? $this->request['category_id'] : $this->model->category_id;
$this->request['payment_method'] = isset($this->request['payment_method']) ? $this->request['payment_method'] : setting('default.payment_method');
$this->request['notify'] = isset($this->request['notify']) ? $this->request['notify'] : 0;
}
protected function checkAmount(): bool
{
$code = $this->request['currency_code'];
$rate = $this->request['currency_rate'];
$precision = config('money.' . $code . '.precision');
$amount = $this->request['amount'] = round($this->request['amount'], $precision);
if ($this->model->currency_code != $code) {
$converted_amount = $this->convertBetween($amount, $code, $rate, $this->model->currency_code, $this->model->currency_rate);
$amount = round($converted_amount, $precision);
}
$this->model->paid_amount = $this->model->paid;
event(new PaidAmountCalculated($this->model));
$total_amount = round($this->model->amount - $this->model->paid_amount, $precision);
unset($this->model->reconciled);
unset($this->model->paid_amount);
$compare = bccomp($amount, $total_amount, $precision);
if ($compare === 1) {
$error_amount = $total_amount;
if ($this->model->currency_code != $code) {
$converted_amount = $this->convertBetween($total_amount, $this->model->currency_code, $this->model->currency_rate, $code, $rate);
$error_amount = round($converted_amount, $precision);
}
$message = trans('messages.error.over_payment', ['amount' => money($error_amount, $code, true)]);
throw new \Exception($message);
} else {
$this->model->status = ($compare === 0) ? 'paid' : 'partial';
}
return true;
}
protected function createHistory(): void
{
$history_desc = money((double) $this->transaction->amount, (string) $this->transaction->currency_code, true)->format() . ' ' . trans_choice('general.payments', 1);
$this->dispatch(new CreateDocumentHistory($this->model, 0, $history_desc));
}
}

View File

@@ -6,8 +6,9 @@ use App\Abstracts\Job;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Auth\User;
use App\Jobs\Auth\CreateUser;
use App\Models\Auth\Role;
use App\Models\Auth\User;
use App\Models\Common\Contact;
use Illuminate\Support\Str;
@@ -42,16 +43,17 @@ class CreateContact extends Job implements HasOwner, HasSource, ShouldCreate
throw new \Exception($message);
}
$data = $this->request->all();
$data['locale'] = setting('default.locale', 'en-GB');
$customer_role = Role::all()->filter(function ($role) {
return $role->hasPermission('read-client-portal');
})->first();
})->pluck('id')->toArray();
$user = User::create($data);
$user->roles()->attach($customer_role);
$user->companies()->attach($data['company_id']);
$this->request->merge([
'locale' => setting('default.locale', 'en-GB'),
'roles' => $customer_role,
'companies' => [$this->request->get('company_id')],
]);
$user = $this->dispatch(new CreateUser($this->request));
$this->request['user_id'] = $user->id;
}

View File

@@ -95,7 +95,13 @@ class CreateDashboard extends Job implements HasOwner, HasSource, ShouldCreate
$sort = 1;
if ($this->request->has('default_widgets')) {
$widgets = Widgets::getClasses($this->request->get('default_widgets'), false);
$default_widgets = $this->request->get('default_widgets');
if (! is_array($default_widgets) && ($default_widgets == 'core')) {
Widgets::optimizeCoreWidgets();
}
$widgets = Widgets::getClasses($default_widgets, false);
$this->createWidgets($widgets, $sort);
}

View File

@@ -3,6 +3,7 @@
namespace App\Jobs\Common;
use App\Abstracts\Job;
use App\Events\Auth\UserCreated;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Auth\Role;
use App\Models\Auth\User;
@@ -66,6 +67,9 @@ class UpdateContact extends Job implements ShouldUpdate
$user = User::create($data);
$user->roles()->attach($customer_role);
$user->companies()->attach($data['company_id']);
$this->request->merge(['companies' => array($data['company_id'])]);
event(new UserCreated($user, $this->request));
$this->request['user_id'] = $user->id;
}

View File

@@ -149,7 +149,7 @@ class CreateDocumentItemsAndTotals extends Job implements HasOwner, HasSource, S
'document_id' => $this->document->id,
'code' => 'total',
'name' => 'invoices.total',
'amount' => $this->request['amount'],
'amount' => $this->request['amount'],
'sort_order' => $sort_order,
'created_from' => $this->request['created_from'],
'created_by' => $this->request['created_by'],
@@ -255,10 +255,12 @@ class CreateDocumentItemsAndTotals extends Job implements HasOwner, HasSource, S
$sub = (double) $item['price'] * (double) $item['quantity'];
if (! empty($this->request['discount'])) {
if ($item['discount_type'] === 'percentage') {
$sub -= ($sub * ($item['discount'] / 100));
} else {
$sub -= $item['discount'];
if (isset($item['discount']) && isset($item['discount_type'])) {
if ($item['discount_type'] === 'percentage') {
$sub -= ($sub * ($item['discount'] / 100));
} else {
$sub -= $item['discount'];
}
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Jobs\Document;
use App\Abstracts\Job;
use App\Events\Document\DocumentSent;
use App\Models\Document\Document;
class SendDocumentAsCustomMail extends Job
{
public function __construct($request, $template_alias)
{
$this->request = $request;
$this->template_alias = $template_alias;
}
public function handle(): void
{
$document = Document::find($this->request->get('document_id'));
$custom_mail = $this->request->only(['to', 'subject', 'body']);
if ($this->request->has('user_email')) {
$custom_mail['cc'] = user()->email;
}
$notification = config('type.document.' . $document->type . '.notification.class');
// Notify the contact
$document->contact->notify(new $notification($document, $this->template_alias, true, $custom_mail));
event(new DocumentSent($document));
}
}

View File

@@ -39,6 +39,8 @@ class DisableModule extends Job
{
$this->authorize();
event(new \App\Events\Module\Disabling($this->alias, $this->company_id));
$command = "module:disable {$this->alias} {$this->company_id} {$this->locale}";
$result = Console::run($command);

View File

@@ -39,6 +39,8 @@ class UninstallModule extends Job
{
$this->authorize();
event(new \App\Events\Module\Uninstalling($this->alias, $this->company_id));
$command = "module:uninstall {$this->alias} {$this->company_id} {$this->locale}";
$result = Console::run($command);

View File

@@ -6,7 +6,7 @@ use App\Abstracts\Job;
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
use App\Interfaces\Job\ShouldCreate;
use App\Models\Common\EmailTemplate;
use App\Models\Setting\EmailTemplate;
class CreateEmailTemplate extends Job implements HasOwner, HasSource, ShouldCreate
{

View File

@@ -4,7 +4,7 @@ namespace App\Jobs\Setting;
use App\Abstracts\Job;
use App\Interfaces\Job\ShouldUpdate;
use App\Models\Common\EmailTemplate;
use App\Models\Setting\EmailTemplate;
class UpdateEmailTemplate extends Job implements ShouldUpdate
{