Refactor webhook handler

This commit is contained in:
David Bomba 2020-11-25 11:23:39 +11:00
parent ffc87c4dc1
commit 2f4b46e435
13 changed files with 157 additions and 30 deletions

View File

@ -160,6 +160,18 @@ class InvoiceSum
{ {
$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; return $this;
} }

View File

@ -66,7 +66,7 @@ class InvoiceSumInclusive
->calculateCustomValues() ->calculateCustomValues()
->calculateInvoiceTaxes() ->calculateInvoiceTaxes()
->setTaxMap() ->setTaxMap()
// ->calculateTotals() ->calculateTotals() //just don't add the taxes!!
->calculateBalance() ->calculateBalance()
->calculatePartial(); ->calculatePartial();
@ -170,7 +170,19 @@ class InvoiceSumInclusive
private function calculateTotals() 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; return $this;
} }

View File

@ -70,7 +70,7 @@ class UpdateOrCreateProduct implements ShouldQueue
continue; 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->product_key = $item->product_key;
$product->notes = isset($item->notes) ? $item->notes : ''; $product->notes = isset($item->notes) ? $item->notes : '';
@ -94,4 +94,10 @@ class UpdateOrCreateProduct implements ShouldQueue
$product->save(); $product->save();
} }
} }
public function failed($exception = null)
{
info("update create failed with = ");
info(print_r($exception->getMessage(),1));
}
} }

View File

@ -188,8 +188,6 @@ class Import implements ShouldQueue
foreach ($this->available_imports as $import) { foreach ($this->available_imports as $import) {
info("the key = {$import}");
if (! array_key_exists($import, $data)) { if (! array_key_exists($import, $data)) {
//throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration."); //throw new ResourceNotAvailableForMigration("Resource {$key} is not available for migration.");
info("Resource {$import} 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. //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']) // if($modified['is_deleted'])
$payment->service()->deletePayment(); // $payment->service()->deletePayment();
// if(isset($modified['deleted_at'])) // if(isset($modified['deleted_at']))
// $payment->delete(); // $payment->delete();

View File

@ -131,12 +131,12 @@ class Payment extends BaseModel
public function invoices() 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() 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() public function company_ledger()

View File

@ -25,6 +25,11 @@ class ClientObserver
*/ */
public function created(Client $client) public function created(Client $client)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_CREATE_CLIENT, $client, $client->company);
} }
@ -36,6 +41,11 @@ class ClientObserver
*/ */
public function updated(Client $client) public function updated(Client $client)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_CLIENT, $client, $client->company);
} }
@ -47,6 +57,11 @@ class ClientObserver
*/ */
public function deleted(Client $client) public function deleted(Client $client)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_DELETE_CLIENT, $client, $client->company);
} }

View File

@ -25,6 +25,11 @@ class ExpenseObserver
*/ */
public function created(Expense $expense) public function created(Expense $expense)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_CREATE_EXPENSE, $expense, $expense->company);
} }
@ -36,6 +41,11 @@ class ExpenseObserver
*/ */
public function updated(Expense $expense) public function updated(Expense $expense)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_EXPENSE, $expense, $expense->company);
} }
@ -47,6 +57,11 @@ class ExpenseObserver
*/ */
public function deleted(Expense $expense) public function deleted(Expense $expense)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_DELETE_EXPENSE, $expense, $expense->company);
} }

View File

@ -26,6 +26,12 @@ class InvoiceObserver
*/ */
public function created(Invoice $invoice) public function created(Invoice $invoice)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_CREATE_INVOICE, $invoice, $invoice->company);
} }
@ -37,6 +43,11 @@ class InvoiceObserver
*/ */
public function updated(Invoice $invoice) public function updated(Invoice $invoice)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company);
} }
@ -48,6 +59,11 @@ class InvoiceObserver
*/ */
public function deleted(Invoice $invoice) public function deleted(Invoice $invoice)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_DELETE_INVOICE, $invoice, $invoice->company);
} }

View File

@ -26,6 +26,12 @@ class PaymentObserver
*/ */
public function created(Payment $payment) public function created(Payment $payment)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_CREATE_PAYMENT, $payment, $payment->company);
} }
@ -47,6 +53,11 @@ class PaymentObserver
*/ */
public function deleted(Payment $payment) public function deleted(Payment $payment)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_DELETE_PAYMENT, $payment, $payment->company);
} }

View File

@ -25,6 +25,11 @@ class QuoteObserver
*/ */
public function created(Quote $quote) 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); WebhookHandler::dispatch(Webhook::EVENT_CREATE_QUOTE, $quote, $quote->company);
} }
@ -36,6 +41,11 @@ class QuoteObserver
*/ */
public function updated(Quote $quote) public function updated(Quote $quote)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_QUOTE, $quote, $quote->company);
} }
@ -47,6 +57,11 @@ class QuoteObserver
*/ */
public function deleted(Quote $quote) public function deleted(Quote $quote)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_DELETE_QUOTE, $quote, $quote->company);
} }

View File

@ -25,6 +25,12 @@ class TaskObserver
*/ */
public function created(Task $task) public function created(Task $task)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_CREATE_TASK, $task, $task->company);
} }
@ -36,6 +42,12 @@ class TaskObserver
*/ */
public function updated(Task $task) public function updated(Task $task)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_UPDATE_TASK, $task, $task->company);
} }
@ -47,6 +59,13 @@ class TaskObserver
*/ */
public function deleted(Task $task) public function deleted(Task $task)
{ {
$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); WebhookHandler::dispatch(Webhook::EVENT_DELETE_TASK, $task, $task->company);
} }

View File

@ -87,7 +87,15 @@ class PaymentMigrationRepository extends BaseRepository
/*Fill the payment*/ /*Fill the payment*/
$payment->fill($data); $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(); $payment->save();
/*Ensure payment number generated*/ /*Ensure payment number generated*/
@ -104,7 +112,7 @@ class PaymentMigrationRepository extends BaseRepository
$invoice_totals = array_sum(array_column($data['invoices'], 'amount')); $invoice_totals = array_sum(array_column($data['invoices'], 'amount'));
$refund_totals = array_sum(array_column($data['invoices'], 'refunded')); $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); $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) { if (array_key_exists('credits', $data) && is_array($data['credits']) && count($data['credits']) > 0) {
$credit_totals = array_sum(array_column($data['credits'], 'amount')); $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); $payment->credits()->saveMany($credits);

View File

@ -72,7 +72,7 @@ class PaymentRepository extends BaseRepository
$this->processExchangeRates($data, $payment); $this->processExchangeRates($data, $payment);
$is_existing_payment = false; $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*/ /*We only update the paid to date ONCE per payment*/
if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) { if (array_key_exists('invoices', $data) && is_array($data['invoices']) && count($data['invoices']) > 0) {