1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

Refactor for observers

This commit is contained in:
David Bomba 2023-02-01 15:00:45 +11:00
parent 801740bdb4
commit a2f0c5d4fe
22 changed files with 101 additions and 95 deletions

View File

@ -205,6 +205,8 @@ class PaymentController extends BaseController
{
$payment = $this->payment_repo->save($request->all(), PaymentFactory::create(auth()->user()->company()->id, auth()->user()->id));
event('eloquent.created: App\Models\Payment', $payment);
return $this->itemResponse($payment);
}
@ -378,6 +380,8 @@ class PaymentController extends BaseController
event(new PaymentWasUpdated($payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
event('eloquent.updated: App\Models\Payment', $payment);
return $this->itemResponse($payment);
}

View File

@ -243,11 +243,19 @@ class WebhookSingle implements ShouldQueue
}
private function resolveClient()
{
{ nlog(get_class($this->entity));
//make sure it isn't an instance of the Client Model
if ($this->entity->client()->exists()) {
if (!$this->entity instanceof \App\Models\Client &&
!$this->entity instanceof \App\Models\Vendor &&
!$this->entity instanceof \App\Models\Product &&
!$this->entity instanceof \App\Models\PurchaseOrder &&
$this->entity->client()->exists()) {
return $this->entity->client;
}
return null;
}

View File

@ -249,18 +249,18 @@ class Credit extends BaseModel
if ($this->balance == 0) {
$this->status_id = self::STATUS_APPLIED;
$this->save();
$this->saveQuietly();
return;
}
$this->save();
$this->saveQuietly();
}
public function setStatus($status)
{
$this->status_id = $status;
$this->save();
$this->saveQuietly();
}
public function pdf_file_path($invitation = null, string $type = 'path', bool $portal = false)
@ -313,7 +313,7 @@ class Credit extends BaseModel
$this->invitations->each(function ($invitation) {
if (! isset($invitation->sent_date)) {
$invitation->sent_date = Carbon::now();
$invitation->save();
$invitation->saveQuietly();
}
});
}

View File

@ -141,6 +141,10 @@ class PurchaseOrder extends BaseModel
}
}
public function getEntityType()
{
return self::class;
}
public function assigned_user()
{
@ -186,7 +190,7 @@ class PurchaseOrder extends BaseModel
$this->invitations->each(function ($invitation) {
if (! isset($invitation->sent_date)) {
$invitation->sent_date = Carbon::now();
$invitation->save();
$invitation->saveQuietly();
}
});
}

View File

@ -66,75 +66,75 @@ class Webhook extends BaseModel
const EVENT_REMIND_INVOICE = 24;
const EVENT_PROJECT_CREATE = 25;//
const EVENT_PROJECT_CREATE = 25; //tested
const EVENT_PROJECT_UPDATE = 26;
const EVENT_PROJECT_UPDATE = 26; //tested
const EVENT_CREATE_CREDIT = 27;
const EVENT_CREATE_CREDIT = 27; //tested
const EVENT_UPDATE_CREDIT = 28;
const EVENT_UPDATE_CREDIT = 28; //tested
const EVENT_DELETE_CREDIT = 29;
const EVENT_DELETE_CREDIT = 29; //tested
const EVENT_PROJECT_DELETE = 30;
const EVENT_PROJECT_DELETE = 30; //tested
const EVENT_UPDATE_PAYMENT = 31;
const EVENT_UPDATE_PAYMENT = 31; //tested
const EVENT_ARCHIVE_PAYMENT = 32;
const EVENT_ARCHIVE_PAYMENT = 32; //tested
const EVENT_ARCHIVE_INVOICE = 33; //tested
const EVENT_ARCHIVE_QUOTE = 34;
const EVENT_ARCHIVE_QUOTE = 34; //tested
const EVENT_ARCHIVE_CREDIT = 35;
const EVENT_ARCHIVE_CREDIT = 35; //tested
const EVENT_ARCHIVE_TASK = 36;
const EVENT_ARCHIVE_TASK = 36; //tested
const EVENT_ARCHIVE_CLIENT = 37;
const EVENT_ARCHIVE_CLIENT = 37; //tested
const EVENT_ARCHIVE_PROJECT = 38;
const EVENT_ARCHIVE_PROJECT = 38; //tested
const EVENT_ARCHIVE_EXPENSE = 39;
const EVENT_ARCHIVE_EXPENSE = 39; //tested
const EVENT_RESTORE_PAYMENT = 40;
const EVENT_RESTORE_PAYMENT = 40; //tested
const EVENT_RESTORE_INVOICE = 41;
const EVENT_RESTORE_INVOICE = 41; //tested
const EVENT_RESTORE_QUOTE = 42;
const EVENT_RESTORE_QUOTE = 42; ///tested
const EVENT_RESTORE_CREDIT = 43;
const EVENT_RESTORE_CREDIT = 43; //tested
const EVENT_RESTORE_TASK = 44;
const EVENT_RESTORE_TASK = 44; //tested
const EVENT_RESTORE_CLIENT = 45;
const EVENT_RESTORE_CLIENT = 45; //tested
const EVENT_RESTORE_PROJECT = 46;
const EVENT_RESTORE_PROJECT = 46; //tested
const EVENT_RESTORE_EXPENSE = 47;
const EVENT_RESTORE_EXPENSE = 47; //tested
const EVENT_ARCHIVE_VENDOR = 48;
const EVENT_ARCHIVE_VENDOR = 48; //tested
const EVENT_RESTORE_VENDOR = 49;
const EVENT_RESTORE_VENDOR = 49; //tested
const EVENT_CREATE_PRODUCT = 50;
const EVENT_CREATE_PRODUCT = 50; //tested
const EVENT_UPDATE_PRODUCT = 51;
const EVENT_UPDATE_PRODUCT = 51; //tested
const EVENT_DELETE_PRODUCT = 52;
const EVENT_DELETE_PRODUCT = 52; //tested
const EVENT_RESTORE_PRODUCT = 53;
const EVENT_RESTORE_PRODUCT = 53; //tested
const EVENT_ARCHIVE_PRODUCT = 54;
const EVENT_ARCHIVE_PRODUCT = 54; //tested
const EVENT_CREATE_PURCHASE_ORDER = 55;
const EVENT_CREATE_PURCHASE_ORDER = 55; //tested
const EVENT_UPDATE_PURCHASE_ORDER = 56;
const EVENT_UPDATE_PURCHASE_ORDER = 56; //tested
const EVENT_DELETE_PURCHASE_ORDER = 57;
const EVENT_DELETE_PURCHASE_ORDER = 57; //tested
const EVENT_RESTORE_PURCHASE_ORDER = 58;
const EVENT_RESTORE_PURCHASE_ORDER = 58; //tested
const EVENT_ARCHIVE_PURCHASE_ORDER = 59;
const EVENT_ARCHIVE_PURCHASE_ORDER = 59; //tested
public static $valid_events = [
self::EVENT_CREATE_PURCHASE_ORDER,

View File

@ -32,7 +32,7 @@ class ClientObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company)->delay(0);
}
@ -59,7 +59,7 @@ class ClientObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $client, $client->company)->delay(rand(1,5));
WebhookHandler::dispatch($event, $client, $client->company)->delay(0);
}
@ -80,7 +80,7 @@ class ClientObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CLIENT, $client, $client->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CLIENT, $client, $client->company)->delay(0);
}

View File

@ -30,12 +30,12 @@ class CreditObserver
*/
public function created(Credit $credit)
{
$subscriptions = Webhook::where('company_id', $credit->company->id)
$subscriptions = Webhook::where('company_id', $credit->company_id)
->where('event_id', Webhook::EVENT_CREATE_CREDIT)
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CREDIT, $credit, $credit->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_CREDIT, $credit, $credit->company)->delay(0);
}
}
@ -55,12 +55,12 @@ class CreditObserver
if($credit->is_deleted)
$event = Webhook::EVENT_DELETE_CREDIT;
$subscriptions = Webhook::where('company_id', $credit->company->id)
$subscriptions = Webhook::where('company_id', $credit->company_id)
->where('event_id', $event)
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $credit, $credit->company)->delay(rand(1,5));
WebhookHandler::dispatch($event, $credit, $credit->company)->delay(0);
}
@ -75,12 +75,12 @@ class CreditObserver
if($credit->is_deleted)
return;
$subscriptions = Webhook::where('company_id', $credit->company->id)
$subscriptions = Webhook::where('company_id', $credit->company_id)
->where('event_id', Webhook::EVENT_ARCHIVE_CREDIT)
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $credit, $credit->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_CREDIT, $credit, $credit->company)->delay(0);
}

View File

@ -32,7 +32,7 @@ class ExpenseObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company)->delay(0);
}
@ -58,7 +58,7 @@ class ExpenseObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $expense, $expense->company)->delay(rand(1,5));
WebhookHandler::dispatch($event, $expense, $expense->company)->delay(0);
}
@ -78,7 +78,7 @@ class ExpenseObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_EXPENSE, $expense, $expense->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_EXPENSE, $expense, $expense->company)->delay(0);
}

View File

@ -32,7 +32,7 @@ class InvoiceObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company, 'client')->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company, 'client')->delay(0);
}
}
@ -58,7 +58,7 @@ class InvoiceObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $invoice, $invoice->company)->delay(rand(1,5));
WebhookHandler::dispatch($event, $invoice, $invoice->company)->delay(0);
}
/**
@ -77,7 +77,7 @@ class InvoiceObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_INVOICE, $invoice, $invoice->company, 'client')->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_INVOICE, $invoice, $invoice->company, 'client')->delay(0);
}

View File

@ -59,7 +59,7 @@ class PaymentObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $payment, $payment->company, 'invoices,client')->delay(20);
WebhookHandler::dispatch($event, $payment, $payment->company, 'invoices,client')->delay(25);
}
/**

View File

@ -33,7 +33,7 @@ class ProductObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_CREATE_PRODUCT, $product, $product->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_PRODUCT, $product, $product->company)->delay(0);
}
/**
@ -59,7 +59,7 @@ class ProductObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $product, $product->company)->delay(rand(1,5));
WebhookHandler::dispatch($event, $product, $product->company)->delay(0);
}
@ -79,7 +79,7 @@ class ProductObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PRODUCT, $product, $product->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PRODUCT, $product, $product->company)->delay(0);
}
/**

View File

@ -32,7 +32,7 @@ class ProjectObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_CREATE, $project, $project->company, 'client')->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_PROJECT_CREATE, $project, $project->company, 'client')->delay(0);
}
@ -59,7 +59,7 @@ class ProjectObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $project, $project->company, 'client')->delay(rand(1,5));
WebhookHandler::dispatch($event, $project, $project->company, 'client')->delay(0);
}
@ -79,7 +79,7 @@ class ProjectObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PROJECT, $project, $project->company, 'client')->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PROJECT, $project, $project->company, 'client')->delay(0);
}

View File

@ -34,7 +34,7 @@ class PurchaseOrderObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_CREATE_PURCHASE_ORDER, $purchase_order, $purchase_order->company, 'vendor')->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_PURCHASE_ORDER, $purchase_order, $purchase_order->company, 'vendor')->delay(0);
}
@ -61,7 +61,7 @@ class PurchaseOrderObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $purchase_order, $purchase_order->company, 'vendor')->delay(rand(1,5));
WebhookHandler::dispatch($event, $purchase_order, $purchase_order->company, 'vendor')->delay(0);
}
@ -81,7 +81,7 @@ class PurchaseOrderObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PURCHASE_ORDER, $purchase_order, $purchase_order->company, 'vendor')->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_PURCHASE_ORDER, $purchase_order, $purchase_order->company, 'vendor')->delay(0);
}

View File

@ -34,7 +34,7 @@ class QuoteObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote, $quote->company, 'client')->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote, $quote->company, 'client')->delay(0);
}
@ -61,7 +61,7 @@ class QuoteObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $quote, $quote->company, 'client')->delay(rand(1,5));
WebhookHandler::dispatch($event, $quote, $quote->company, 'client')->delay(0);
}
@ -81,7 +81,7 @@ class QuoteObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_QUOTE, $quote, $quote->company, 'client')->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_QUOTE, $quote, $quote->company, 'client')->delay(0);
}

View File

@ -33,7 +33,7 @@ class TaskObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company)->delay(0);
}
@ -59,7 +59,7 @@ class TaskObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $task, $task->company)->delay(rand(1,5));
WebhookHandler::dispatch($event, $task, $task->company)->delay(0);
}
/**
@ -78,7 +78,7 @@ class TaskObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_TASK, $task, $task->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_TASK, $task, $task->company)->delay(0);
}

View File

@ -33,7 +33,7 @@ class VendorObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch(Webhook::EVENT_CREATE_VENDOR, $vendor, $vendor->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_CREATE_VENDOR, $vendor, $vendor->company)->delay(0);
}
@ -59,7 +59,7 @@ class VendorObserver
->exists();
if ($subscriptions)
WebhookHandler::dispatch($event, $vendor, $vendor->company)->delay(rand(1,5));
WebhookHandler::dispatch($event, $vendor, $vendor->company)->delay(0);
}
/**
@ -78,7 +78,7 @@ class VendorObserver
->exists();
if ($subscriptions) {
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_VENDOR, $vendor, $vendor->company)->delay(rand(1,5));
WebhookHandler::dispatch(Webhook::EVENT_ARCHIVE_VENDOR, $vendor, $vendor->company)->delay(0);
}
}

View File

@ -85,14 +85,14 @@ class PaymentRepository extends BaseRepository {
}
$client->service()->updatePaidToDate($data['amount'])->save();
$client->save();
$client->saveQuietly();
}
else{
//this fixes an edge case with unapplied payments
$client->service()->updatePaidToDate($data['amount'])->save();
// $client->paid_to_date += $data['amount'];
$client->save();
$client->saveQuietly();
}
if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) {
@ -100,7 +100,7 @@ class PaymentRepository extends BaseRepository {
$client->service()->updatePaidToDate($_credit_totals)->save();
// $client->paid_to_date += $_credit_totals;
$client->save();
$client->saveQuietly();
}
}, 1);
@ -121,7 +121,7 @@ class PaymentRepository extends BaseRepository {
}
$payment->save();
$payment->saveQuietly();
/*Save documents*/
if (array_key_exists('documents', $data)) {
@ -198,17 +198,7 @@ class PaymentRepository extends BaseRepository {
$payment->applied += ($invoice_totals - $credit_totals); //wont work because - check tests
$payment->save();
$transaction = [
'invoice' => [],
'payment' => $payment->transaction_event(),
'client' => $payment->client->transaction_event(),
'credit' => [],
'metadata' => [],
];
// TransactionLog::dispatch(TransactionEvent::PAYMENT_MADE, $transaction, $payment->company->db);
$payment->saveQuietly();
return $payment->refresh();
}

View File

@ -67,7 +67,7 @@ class PurchaseOrderRepository extends BaseRepository
$new_invitation->purchase_order_id = $purchase_order->id;
$new_invitation->vendor_contact_id = $contact->id;
$new_invitation->key = $this->createDbHash($purchase_order->company->db);
$new_invitation->save();
$new_invitation->saveQuietly();
}
}
}

View File

@ -207,7 +207,7 @@ class ClientService
*/
public function save() :Client
{
$this->client->save();
$this->client->saveQuietly();
return $this->client->fresh();
}

View File

@ -139,7 +139,7 @@ class HandleRestore extends AbstractService
$payment->applied += $payment_adjustment;
$payment->is_deleted = false;
$payment->restore();
$payment->save();
$payment->saveQuietly();
});
return $this;

View File

@ -41,7 +41,6 @@ class MarkSent
->setStatus(PurchaseOrder::STATUS_SENT)
->applyNumber()
->adjustBalance($this->purchase_order->amount) //why was this commented out previously?
// ->touchPdf()
->save();
return $this->purchase_order;

View File

@ -60,9 +60,10 @@ class PurchaseOrderExpense
$expense->number = empty($expense->number) ? $this->getNextExpenseNumber($expense) : $expense->number;
$expense->save();
event('eloquent.created: App\Models\Expense', $expense);
$this->purchase_order->expense_id = $expense->id;
$this->purchase_order->save();
$this->purchase_order->saveQuietly();
return $expense;