diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index 6c8dde938f12..abed950b7cf8 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -12,6 +12,7 @@ namespace App\Models; use App\DataMapper\ClientSettings; +use App\Jobs\Util\WebhookHandler; use App\Utils\Traits\MakesHash; use App\Utils\Traits\UserSessionAttributes; use Illuminate\Database\Eloquent\Factories\HasFactory; @@ -167,7 +168,7 @@ class BaseModel extends Model */ public function resolveRouteBinding($value, $field = null) { - + if (is_numeric($value)) { throw new ModelNotFoundException("Record with value {$value} not found"); } @@ -189,12 +190,12 @@ class BaseModel extends Model public function numberFormatter() { - $number = strlen($this->number) >= 1 ? $this->translate_entity() . "_" . $this->number : class_basename($this) . "_" . Str::random(5); + $number = strlen($this->number) >= 1 ? $this->translate_entity() . "_" . $this->number : class_basename($this) . "_" . Str::random(5); $formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $number); - + $formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number); - + $formatted_number = preg_replace('/\s+/', '_', $formatted_number); return $formatted_number; @@ -205,4 +206,14 @@ class BaseModel extends Model return ctrans('texts.item'); } + public function sendEvent($event_id, $additional_data){ + $subscriptions = Webhook::where('company_id', $this->company_id) + ->where('event_id', $event_id) + ->exists(); + if ($subscriptions) { + WebhookHandler::dispatch($event_id, $this, $this->company, $additional_data)->delay(0); + } + } + + } diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 1607c91e1585..f9e3519f3b00 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -73,13 +73,7 @@ class MarkSent extends AbstractService if($fire_webhook) event('eloquent.updated: App\Models\Invoice', $this->invoice); - $subscriptions = Webhook::where('company_id', $this->invoice->company_id) - ->where('event_id', Webhook::EVENT_SENT_INVOICE) - ->exists(); - - if ($subscriptions) { - WebhookHandler::dispatch(Webhook::EVENT_SENT_INVOICE, $this->invoice, $this->invoice->company, 'client')->delay(0); - } + $this->invoice->sendEvent(Webhook::EVENT_SENT_INVOICE, "client"); return $this->invoice->fresh(); }