2024-08-29 03:17:38 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
2024-08-29 07:51:48 +02:00
|
|
|
namespace App\Services\EDocument\Jobs;
|
2024-08-29 03:17:38 +02:00
|
|
|
|
2024-08-29 03:19:09 +02:00
|
|
|
use App\Utils\Ninja;
|
2024-08-29 07:16:01 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\Activity;
|
2024-08-29 03:17:38 +02:00
|
|
|
use Illuminate\Bus\Queueable;
|
2024-08-29 07:16:01 +02:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2024-08-29 03:17:38 +02:00
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2024-08-29 07:16:01 +02:00
|
|
|
use App\Services\EDocument\Standards\Peppol;
|
2024-08-29 03:17:38 +02:00
|
|
|
use Illuminate\Queue\Middleware\WithoutOverlapping;
|
2024-08-29 07:16:01 +02:00
|
|
|
use App\Services\EDocument\Gateway\Storecove\Storecove;
|
2024-08-29 03:17:38 +02:00
|
|
|
|
|
|
|
class SendEDocument implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
|
|
|
|
|
|
|
public $tries = 2;
|
|
|
|
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
|
|
|
public function __construct(private string $entity, private int $id, private string $db)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function backoff()
|
|
|
|
{
|
|
|
|
return [rand(5, 29), rand(30, 59)];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
MultiDB::setDB($this->db);
|
|
|
|
|
|
|
|
$model = $this->entity::find($this->id);
|
|
|
|
|
2024-08-29 03:19:09 +02:00
|
|
|
if(Ninja::isSelfHost() && ($model instanceof Invoice) && $model->company->legal_entity_id)
|
|
|
|
{
|
2024-08-29 07:16:01 +02:00
|
|
|
|
|
|
|
$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,
|
2024-10-11 17:55:41 +02:00
|
|
|
'e_invoicing_token' => $model->company->e_invoicing_token,
|
|
|
|
|
|
|
|
// include whitelabel key.
|
2024-08-29 07:16:01 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2024-08-29 03:19:09 +02:00
|
|
|
//self hosted sender
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Ninja::isHosted() && ($model instanceof Invoice) && $model->company->legal_entity_id)
|
|
|
|
{
|
|
|
|
//hosted sender
|
2024-08-29 07:16:01 +02:00
|
|
|
$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());
|
|
|
|
}
|
|
|
|
|
2024-08-29 03:19:09 +02:00
|
|
|
}
|
|
|
|
|
2024-08-29 07:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2024-08-30 07:44:01 +02:00
|
|
|
$activity->notes = str_replace('"', '', $guid);
|
|
|
|
|
2024-08-29 07:16:01 +02:00
|
|
|
$activity->save();
|
|
|
|
|
2024-10-09 03:49:17 +02:00
|
|
|
$std = new \stdClass;
|
|
|
|
$std->guid = str_replace('"', '', $guid);
|
|
|
|
$model->backup = $std;
|
2024-08-29 07:51:48 +02:00
|
|
|
$model->saveQuietly();
|
|
|
|
|
2024-08-29 07:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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",
|
|
|
|
];
|
2024-08-29 03:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)];
|
|
|
|
}
|
|
|
|
}
|