db); $model = $this->entity::find($this->id); if(Ninja::isSelfHost() && ($model instanceof Invoice) && $model->company->legal_entity_id) { $p = new Peppol($model); $p->run(); $xml = $p->toXml(); $identifiers = $p->getStorecoveMeta(); $payload = [ 'legal_entity_id' => $model->company->legal_entity_id, 'document' => base64_encode($xml), 'tenant_id' => $model->company->company_key, 'identifiers' => $identifiers, ]; $r = Http::withHeaders($this->getHeaders()) ->post(config('ninja.hosted_ninja_url')."/api/einvoice/submission", $payload); if($r->successful()) { nlog("Model {$model->number} was successfully sent for third party processing via hosted Invoice Ninja"); $data = $r->json(); return $this->writeActivity($model, $data['guid']); } if($r->failed()) { nlog("Model {$model->number} failed to be accepted by invoice ninja, error follows:"); nlog($r->getBody()->getContents()); } //self hosted sender } if(Ninja::isHosted() && ($model instanceof Invoice) && $model->company->legal_entity_id) { //hosted sender $p = new Peppol($model); $p->run(); $xml = $p->toXml(); $identifiers = $p->getStorecoveMeta(); $sc = new \App\Services\EDocument\Gateway\Storecove\Storecove(); $r = $sc->sendDocument($xml, $model->company->legal_entity_id, $identifiers); if(is_string($r)) return $this->writeActivity($model, $r); if($r->failed()) { nlog("Model {$model->number} failed to be accepted by invoice ninja, error follows:"); nlog($r->getBody()->getContents()); } } } private function writeActivity($model, string $guid) { $activity = new Activity(); $activity->user_id = $model->user_id; $activity->client_id = $model->client_id ?? $model->vendor_id; $activity->company_id = $model->company_id; $activity->activity_type_id = Activity::EMAIL_EINVOICE_SUCCESS; $activity->invoice_id = $model->id; $activity->notes = str_replace('"', '', $guid); $activity->save(); $model->backup = str_replace('"', '', $guid); $model->saveQuietly(); } /** * Self hosted request headers * * @return array */ private function getHeaders(): array { return [ 'X-API-SELF-HOST-TOKEN' => config('ninja.license_key'), "X-Requested-With" => "XMLHttpRequest", "Content-Type" => "application/json", ]; } public function failed($exception = null) { if ($exception) { nlog("EXCEPTION:: SENDEDOCUMENT::"); nlog($exception->getMessage()); } config(['queue.failed.driver' => null]); } public function middleware() { return [new WithoutOverlapping($this->entity.$this->id.$this->db)]; } }