From 2f4b46e43596a119ca0ce560207c91219ea468ba Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 25 Nov 2020 11:23:39 +1100 Subject: [PATCH] Refactor webhook handler --- app/Helpers/Invoice/InvoiceSum.php | 12 +++++++++ app/Helpers/Invoice/InvoiceSumInclusive.php | 16 ++++++++++-- app/Jobs/Product/UpdateOrCreateProduct.php | 8 +++++- app/Jobs/Util/Import.php | 8 +++--- app/Models/Payment.php | 4 +-- app/Observers/ClientObserver.php | 21 +++++++++++++--- app/Observers/ExpenseObserver.php | 21 +++++++++++++--- app/Observers/InvoiceObserver.php | 22 +++++++++++++--- app/Observers/PaymentObserver.php | 15 +++++++++-- app/Observers/QuoteObserver.php | 19 ++++++++++++-- app/Observers/TaskObserver.php | 25 ++++++++++++++++--- .../Migration/PaymentMigrationRepository.php | 14 ++++++++--- app/Repositories/PaymentRepository.php | 2 +- 13 files changed, 157 insertions(+), 30 deletions(-) diff --git a/app/Helpers/Invoice/InvoiceSum.php b/app/Helpers/Invoice/InvoiceSum.php index 4ed998ba4b4a..d3e54e607861 100644 --- a/app/Helpers/Invoice/InvoiceSum.php +++ b/app/Helpers/Invoice/InvoiceSum.php @@ -160,6 +160,18 @@ class InvoiceSum { $this->total += $this->total_taxes; + if($this->invoice->custom_value1 > 0) + $this->total += $this->invoice->custom_value1; + + if($this->invoice->custom_value2 > 0) + $this->total += $this->invoice->custom_value2; + + if($this->invoice->custom_value3 > 0) + $this->total += $this->invoice->custom_value3; + + if($this->invoice->custom_value4 > 0) + $this->total += $this->invoice->custom_value4; + return $this; } diff --git a/app/Helpers/Invoice/InvoiceSumInclusive.php b/app/Helpers/Invoice/InvoiceSumInclusive.php index e4671896141f..20f10b609aba 100644 --- a/app/Helpers/Invoice/InvoiceSumInclusive.php +++ b/app/Helpers/Invoice/InvoiceSumInclusive.php @@ -66,7 +66,7 @@ class InvoiceSumInclusive ->calculateCustomValues() ->calculateInvoiceTaxes() ->setTaxMap() -// ->calculateTotals() + ->calculateTotals() //just don't add the taxes!! ->calculateBalance() ->calculatePartial(); @@ -170,7 +170,19 @@ class InvoiceSumInclusive private function calculateTotals() { - $this->total += $this->total_taxes; + //$this->total += $this->total_taxes; + + if($this->invoice->custom_value1 > 0) + $this->total += $this->invoice->custom_value1; + + if($this->invoice->custom_value2 > 0) + $this->total += $this->invoice->custom_value2; + + if($this->invoice->custom_value3 > 0) + $this->total += $this->invoice->custom_value3; + + if($this->invoice->custom_value4 > 0) + $this->total += $this->invoice->custom_value4; return $this; } diff --git a/app/Jobs/Product/UpdateOrCreateProduct.php b/app/Jobs/Product/UpdateOrCreateProduct.php index 210046f6fced..4f7098b34ae8 100644 --- a/app/Jobs/Product/UpdateOrCreateProduct.php +++ b/app/Jobs/Product/UpdateOrCreateProduct.php @@ -70,7 +70,7 @@ class UpdateOrCreateProduct implements ShouldQueue continue; } - $product = Product::firstOrNew(['product_key' => $item->product_key, 'company_id' => $this->invoice->company->id]); + $product = Product::withTrashed()->firstOrNew(['product_key' => $item->product_key, 'company_id' => $this->invoice->company->id]); $product->product_key = $item->product_key; $product->notes = isset($item->notes) ? $item->notes : ''; @@ -94,4 +94,10 @@ class UpdateOrCreateProduct implements ShouldQueue $product->save(); } } + + public function failed($exception = null) + { + info("update create failed with = "); + info(print_r($exception->getMessage(),1)); + } } diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 5c1e6f1b51ac..305dcfb67220 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -188,8 +188,6 @@ class Import implements ShouldQueue foreach ($this->available_imports as $import) { - info("the key = {$import}"); - if (! array_key_exists($import, $data)) { //throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration."); info("Resource {$import} is not available for migration."); @@ -846,10 +844,10 @@ class Import implements ShouldQueue ]; //depending on the status, we do a final action. - $payment = $this->updatePaymentForStatus($payment, $modified['status_id']); + //s$payment = $this->updatePaymentForStatus($payment, $modified['status_id']); - if($modified['is_deleted']) - $payment->service()->deletePayment(); + // if($modified['is_deleted']) + // $payment->service()->deletePayment(); // if(isset($modified['deleted_at'])) // $payment->delete(); diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 3fcad72d5be5..33b5760b1a59 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -131,12 +131,12 @@ class Payment extends BaseModel public function invoices() { - return $this->morphedByMany(Invoice::class, 'paymentable')->withPivot('amount', 'refunded')->withTimestamps(); + return $this->morphedByMany(Invoice::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps(); } public function credits() { - return $this->morphedByMany(Credit::class, 'paymentable')->withPivot('amount', 'refunded')->withTimestamps(); + return $this->morphedByMany(Credit::class, 'paymentable')->withTrashed()->withPivot('amount', 'refunded')->withTimestamps(); } public function company_ledger() diff --git a/app/Observers/ClientObserver.php b/app/Observers/ClientObserver.php index 61e2494f7b62..e6336c5eb2c3 100644 --- a/app/Observers/ClientObserver.php +++ b/app/Observers/ClientObserver.php @@ -25,7 +25,12 @@ class ClientObserver */ public function created(Client $client) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company); + $subscriptions = Webhook::where('company_id', $client->company->id) + ->where('event_id', Webhook::EVENT_CREATE_CLIENT) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company); } /** @@ -36,7 +41,12 @@ class ClientObserver */ public function updated(Client $client) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_CLIENT, $client, $client->company); + $subscriptions = Webhook::where('company_id', $client->company->id) + ->where('event_id', Webhook::EVENT_UPDATE_CLIENT) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_UPDATE_CLIENT, $client, $client->company); } /** @@ -47,7 +57,12 @@ class ClientObserver */ public function deleted(Client $client) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_CLIENT, $client, $client->company); + $subscriptions = Webhook::where('company_id', $client->company->id) + ->where('event_id', Webhook::EVENT_DELETE_CLIENT) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_DELETE_CLIENT, $client, $client->company); } /** diff --git a/app/Observers/ExpenseObserver.php b/app/Observers/ExpenseObserver.php index f1f3a80c672d..e67abbd9e620 100644 --- a/app/Observers/ExpenseObserver.php +++ b/app/Observers/ExpenseObserver.php @@ -25,7 +25,12 @@ class ExpenseObserver */ public function created(Expense $expense) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company); + $subscriptions = Webhook::where('company_id', $expense->company->id) + ->where('event_id', Webhook::EVENT_CREATE_EXPENSE) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company); } /** @@ -36,7 +41,12 @@ class ExpenseObserver */ public function updated(Expense $expense) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_EXPENSE, $expense, $expense->company); + $subscriptions = Webhook::where('company_id', $expense->company->id) + ->where('event_id', Webhook::EVENT_UPDATE_EXPENSE) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_UPDATE_EXPENSE, $expense, $expense->company); } /** @@ -47,7 +57,12 @@ class ExpenseObserver */ public function deleted(Expense $expense) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_EXPENSE, $expense, $expense->company); + $subscriptions = Webhook::where('company_id', $expense->company->id) + ->where('event_id', Webhook::EVENT_DELETE_EXPENSE) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_DELETE_EXPENSE, $expense, $expense->company); } /** diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php index 13310982423d..224456d68477 100644 --- a/app/Observers/InvoiceObserver.php +++ b/app/Observers/InvoiceObserver.php @@ -26,7 +26,13 @@ class InvoiceObserver */ public function created(Invoice $invoice) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company); + + $subscriptions = Webhook::where('company_id', $invoice->company->id) + ->where('event_id', Webhook::EVENT_CREATE_INVOICE) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company); } /** @@ -37,7 +43,12 @@ class InvoiceObserver */ public function updated(Invoice $invoice) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company); + $subscriptions = Webhook::where('company_id', $invoice->company->id) + ->where('event_id', Webhook::EVENT_UPDATE_INVOICE) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company); } /** @@ -48,7 +59,12 @@ class InvoiceObserver */ public function deleted(Invoice $invoice) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_INVOICE, $invoice, $invoice->company); + $subscriptions = Webhook::where('company_id', $invoice->company->id) + ->where('event_id', Webhook::EVENT_DELETE_INVOICE) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_DELETE_INVOICE, $invoice, $invoice->company); } /** diff --git a/app/Observers/PaymentObserver.php b/app/Observers/PaymentObserver.php index 0706c5600966..5eaa05de5a91 100644 --- a/app/Observers/PaymentObserver.php +++ b/app/Observers/PaymentObserver.php @@ -26,7 +26,13 @@ class PaymentObserver */ public function created(Payment $payment) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company); + + $subscriptions = Webhook::where('company_id', $payment->company->id) + ->where('event_id', Webhook::EVENT_CREATE_PAYMENT) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company); } /** @@ -47,7 +53,12 @@ class PaymentObserver */ public function deleted(Payment $payment) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment, $payment->company); + $subscriptions = Webhook::where('company_id', $payment->company->id) + ->where('event_id', Webhook::EVENT_DELETE_PAYMENT) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment, $payment->company); } /** diff --git a/app/Observers/QuoteObserver.php b/app/Observers/QuoteObserver.php index 0c69048d7b9e..600382a0d8d8 100644 --- a/app/Observers/QuoteObserver.php +++ b/app/Observers/QuoteObserver.php @@ -25,6 +25,11 @@ class QuoteObserver */ public function created(Quote $quote) { + $subscriptions = Webhook::where('company_id', $quote->company->id) + ->where('event_id', Webhook::EVENT_CREATE_QUOTE) + ->exists(); + + if($subscriptions) WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote, $quote->company); } @@ -36,7 +41,12 @@ class QuoteObserver */ public function updated(Quote $quote) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_QUOTE, $quote, $quote->company); + $subscriptions = Webhook::where('company_id', $quote->company->id) + ->where('event_id', Webhook::EVENT_UPDATE_QUOTE) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_UPDATE_QUOTE, $quote, $quote->company); } /** @@ -47,7 +57,12 @@ class QuoteObserver */ public function deleted(Quote $quote) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_QUOTE, $quote, $quote->company); + $subscriptions = Webhook::where('company_id', $quote->company->id) + ->where('event_id', Webhook::EVENT_DELETE_QUOTE) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_DELETE_QUOTE, $quote, $quote->company); } /** diff --git a/app/Observers/TaskObserver.php b/app/Observers/TaskObserver.php index 9bbfc38d6be4..e31371276c7c 100644 --- a/app/Observers/TaskObserver.php +++ b/app/Observers/TaskObserver.php @@ -25,7 +25,13 @@ class TaskObserver */ public function created(Task $task) { - WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company); + + $subscriptions = Webhook::where('company_id', $task->company->id) + ->where('event_id', Webhook::EVENT_CREATE_TASK) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company); } /** @@ -36,7 +42,13 @@ class TaskObserver */ public function updated(Task $task) { - WebhookHandler::dispatch(Webhook::EVENT_UPDATE_TASK, $task, $task->company); + + $subscriptions = Webhook::where('company_id', $task->company->id) + ->where('event_id', Webhook::EVENT_UPDATE_TASK) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_UPDATE_TASK, $task, $task->company); } /** @@ -47,7 +59,14 @@ class TaskObserver */ public function deleted(Task $task) { - WebhookHandler::dispatch(Webhook::EVENT_DELETE_TASK, $task, $task->company); + + + $subscriptions = Webhook::where('company_id', $task->company->id) + ->where('event_id', Webhook::EVENT_DELETE_TASK) + ->exists(); + + if($subscriptions) + WebhookHandler::dispatch(Webhook::EVENT_DELETE_TASK, $task, $task->company); } /** diff --git a/app/Repositories/Migration/PaymentMigrationRepository.php b/app/Repositories/Migration/PaymentMigrationRepository.php index 6e2a435e5fdf..d4144ac42168 100644 --- a/app/Repositories/Migration/PaymentMigrationRepository.php +++ b/app/Repositories/Migration/PaymentMigrationRepository.php @@ -87,7 +87,15 @@ class PaymentMigrationRepository extends BaseRepository /*Fill the payment*/ $payment->fill($data); - $payment->status_id = Payment::STATUS_COMPLETED; + //$payment->status_id = Payment::STATUS_COMPLETED; + + if(!array_key_exists('status_id', $data)){ + info("payment with no status id?"); + info(print_r($data,1)); + } + + $payment->status_id = $data['status_id']; + $payment->deleted_at = $data['deleted_at'] ?: NULL; $payment->save(); /*Ensure payment number generated*/ @@ -104,7 +112,7 @@ class PaymentMigrationRepository extends BaseRepository $invoice_totals = array_sum(array_column($data['invoices'], 'amount')); $refund_totals = array_sum(array_column($data['invoices'], 'refunded')); - $invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->get(); + $invoices = Invoice::whereIn('id', array_column($data['invoices'], 'invoice_id'))->withTrashed()->get(); $payment->invoices()->saveMany($invoices); @@ -118,7 +126,7 @@ class PaymentMigrationRepository extends BaseRepository if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) { $credit_totals = array_sum(array_column($data['credits'], 'amount')); - $credits = Credit::whereIn('id', array_column($data['credits'], 'credit_id'))->get(); + $credits = Credit::whereIn('id', array_column($data['credits'], 'credit_id'))->withTrashed()->get(); $payment->credits()->saveMany($credits); diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index 673d701b96b2..55bf444c9a05 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -72,7 +72,7 @@ class PaymentRepository extends BaseRepository $this->processExchangeRates($data, $payment); $is_existing_payment = false; - $client = Client::where('id', $data['client_id'])->withTrashed()->first(); + $client = Client::where('id', $data['client_id'])->withTrashed()->first; /*We only update the paid to date ONCE per payment*/ if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {