client = $client; $this->credit = $credit; } public function run() { /* Return immediately if status is not draft */ if ($this->credit->status_id != Credit::STATUS_DRAFT) { return $this->credit; } $this->credit->markInvitationsSent(); $this->credit ->service() ->setStatus(Credit::STATUS_SENT) ->applyNumber() ->adjustBalance($this->credit->amount) ->touchPdf() ->save(); $this->client ->service() ->adjustCreditBalance($this->credit->amount) ->save(); event(new CreditWasMarkedSent($this->credit, $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null))); $subscriptions = Webhook::where('company_id', $this->credit->company_id) ->where('event_id', Webhook::EVENT_SENT_CREDIT) ->exists(); if ($subscriptions) { WebhookHandler::dispatch(Webhook::EVENT_SENT_CREDIT, $this->credit, $this->credit->company)->delay(0); } return $this->credit; } }