v2 first commit
This commit is contained in:
@@ -4,7 +4,6 @@ namespace App\Observers;
|
||||
|
||||
use App\Models\Common\Company as Model;
|
||||
use Artisan;
|
||||
use Auth;
|
||||
|
||||
class Company
|
||||
{
|
||||
@@ -22,12 +21,12 @@ class Company
|
||||
]);
|
||||
|
||||
// Check if user is logged in
|
||||
if (!Auth::check()) {
|
||||
if (!auth()->check()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Attach company to user
|
||||
Auth::user()->companies()->attach($company->id);
|
||||
user()->companies()->attach($company->id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,9 +38,9 @@ class Company
|
||||
public function deleted(Model $company)
|
||||
{
|
||||
$tables = [
|
||||
'accounts', 'bill_histories', 'bill_items', 'bill_payments', 'bill_statuses', 'bills', 'categories',
|
||||
'currencies', 'customers', 'invoice_histories', 'invoice_items', 'invoice_payments', 'invoice_statuses',
|
||||
'invoices', 'items', 'payments', 'recurring', 'revenues', 'settings', 'taxes', 'transfers', 'vendors',
|
||||
'accounts', 'bill_histories', 'bill_items', 'bill_statuses', 'bills', 'categories', 'contacts',
|
||||
'currencies', 'invoice_histories', 'invoice_items', 'invoice_statuses', 'invoices', 'items', 'recurring',
|
||||
'settings', 'taxes', 'transactions', 'transfers',
|
||||
];
|
||||
|
||||
foreach ($tables as $table) {
|
||||
@@ -62,4 +61,4 @@ class Company
|
||||
$item->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
79
app/Observers/Transaction.php
Normal file
79
app/Observers/Transaction.php
Normal file
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Banking\Transaction as Model;
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
use App\Models\Expense\BillHistory;
|
||||
|
||||
class Transaction
|
||||
{
|
||||
/**
|
||||
* Listen to the deleted event.
|
||||
*
|
||||
* @param Model $transaction
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Model $transaction)
|
||||
{
|
||||
if (!empty($transaction->document_id)) {
|
||||
if ($transaction->type == 'income') {
|
||||
$this->updateInvoice($transaction);
|
||||
} else {
|
||||
$this->updateBill($transaction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function updateInvoice($transaction)
|
||||
{
|
||||
$invoice = $transaction->invoice;
|
||||
|
||||
if ($invoice->payments->count() > 1) {
|
||||
$invoice->invoice_status_code = 'partial';
|
||||
} else {
|
||||
$invoice->invoice_status_code = 'sent';
|
||||
}
|
||||
|
||||
$invoice->save();
|
||||
|
||||
$desc_amount = money((float) $transaction->amount, (string) $transaction->currency_code, true)->format();
|
||||
|
||||
$description = $desc_amount . ' ' . trans_choice('general.payments', 1);
|
||||
|
||||
// Add invoice history
|
||||
InvoiceHistory::create([
|
||||
'company_id' => $invoice->company_id,
|
||||
'invoice_id' => $invoice->id,
|
||||
'status_code' => $invoice->invoice_status_code,
|
||||
'notify' => 0,
|
||||
'description' => trans('messages.success.deleted', ['type' => $description]),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function updateBill($transaction)
|
||||
{
|
||||
$bill = $transaction->bill;
|
||||
|
||||
if ($bill->payments->count() > 1) {
|
||||
$bill->bill_status_code = 'partial';
|
||||
} else {
|
||||
$bill->bill_status_code = 'received';
|
||||
}
|
||||
|
||||
$bill->save();
|
||||
|
||||
$desc_amount = money((float) $transaction->amount, (string) $transaction->currency_code, true)->format();
|
||||
|
||||
$description = $desc_amount . ' ' . trans_choice('general.payments', 1);
|
||||
|
||||
// Add bill history
|
||||
BillHistory::create([
|
||||
'company_id' => $bill->company_id,
|
||||
'bill_id' => $bill->id,
|
||||
'status_code' => $bill->bill_status_code,
|
||||
'notify' => 0,
|
||||
'description' => trans('messages.success.deleted', ['type' => $description]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
40
app/Observers/User.php
Normal file
40
app/Observers/User.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Observers;
|
||||
|
||||
use App\Models\Auth\User as Model;
|
||||
|
||||
class User
|
||||
{
|
||||
|
||||
/**
|
||||
* Listen to the deleted event.
|
||||
*
|
||||
* @param Model $user
|
||||
* @return void
|
||||
*/
|
||||
public function deleted(Model $user)
|
||||
{
|
||||
$tables = [
|
||||
'settings', 'taxes', 'transactions', 'transfers',
|
||||
];
|
||||
|
||||
foreach ($tables as $table) {
|
||||
$this->deleteItems($user, $table);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete items in batch.
|
||||
*
|
||||
* @param Model $user
|
||||
* @param $table
|
||||
* @return void
|
||||
*/
|
||||
protected function deleteItems($user, $table)
|
||||
{
|
||||
foreach ($user->$table as $item) {
|
||||
$item->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user