diff --git a/app/Services/EDocument/Gateway/Storecove/Storecove.php b/app/Services/EDocument/Gateway/Storecove/Storecove.php index a42a82c8f1fa..59e7e7a533b4 100644 --- a/app/Services/EDocument/Gateway/Storecove/Storecove.php +++ b/app/Services/EDocument/Gateway/Storecove/Storecove.php @@ -51,6 +51,8 @@ class Storecove "identifier" => "1200109963131" ]; + private ?int $legal_entity_id; + public StorecoveRouter $router; public function __construct() @@ -129,6 +131,7 @@ class Storecove */ public function sendDocument(string $document, int $routing_id, array $override_payload = []) { + $this->legal_entity_id = $routing_id; $payload = [ "legalEntityId" => $routing_id, @@ -348,22 +351,32 @@ class Storecove private function httpClient(string $uri, string $verb, array $data, ?array $headers = []) { - $r = Http::withToken(config('ninja.storecove_api_key')); - try { - $r->withHeaders($this->getHeaders($headers)) + $r = Http::withToken(config('ninja.storecove_api_key')) + ->withHeaders($this->getHeaders($headers)) ->{$verb}("{$this->base_url}{$uri}", $data)->throw(); } catch (ClientException $e) { // 4xx errors + + nlog("LEI:: {$this->legal_entity_id}"); nlog("Client error: " . $e->getMessage()); nlog("Response body: " . $e->getResponse()->getBody()->getContents()); } catch (ServerException $e) { // 5xx errors + + nlog("LEI:: {$this->legal_entity_id}"); nlog("Server error: " . $e->getMessage()); nlog("Response body: " . $e->getResponse()->getBody()->getContents()); - } catch (RequestException $e) { + } catch (\Illuminate\Http\Client\RequestException $e) { + + nlog("LEI:: {$this->legal_entity_id}"); nlog("Request error: {$e->getCode()}: " . $e->getMessage()); + $responseBody = $e->response->body(); + nlog("Response body: " . $responseBody); + + return $e->response; + } return $r; // @phpstan-ignore-line diff --git a/app/Services/EDocument/Standards/Peppol.php b/app/Services/EDocument/Standards/Peppol.php index 9dc097707ea8..b6ea751536df 100644 --- a/app/Services/EDocument/Standards/Peppol.php +++ b/app/Services/EDocument/Standards/Peppol.php @@ -305,7 +305,7 @@ class Peppol extends AbstractService private function setInvoice(): self { - if($this->invoice->e_invoice) { + if($this->invoice->e_invoice && isset($this->invoice->e_invoice->Invoice)) { $this->p_invoice = $this->e->decode('Peppol', json_encode($this->invoice->e_invoice->Invoice), 'json'); diff --git a/app/Services/EDocument/Standards/Validation/Peppol/EntityLevel.php b/app/Services/EDocument/Standards/Validation/Peppol/EntityLevel.php new file mode 100644 index 000000000000..f1a77ab4ead1 --- /dev/null +++ b/app/Services/EDocument/Standards/Validation/Peppol/EntityLevel.php @@ -0,0 +1,154 @@ +company->locale()); + + $this->errors['status'] = false; + $this->errors['client'] = $entity->client ? $this->testClientState($entity) : []; + $this->errors['company'] = $this->testCompanyState($entity->company); + $this->errors['invoice'] = $entity instanceof Invoice ? $this->testInvoiceState($entity) : []; + + // $this->errors['vendor']= $entity->client ? $this->testClientState($entity) : []; + + if( + count($this->errors['client']) == 0 && + count($this->errors['company']) == 0 + ){ + $this->errors['status'] = true; + } + + return $this->errors; + + } + + private function testClientState($entity): array + { + + $errors = []; + + foreach($this->client_fields as $field) + { + + if($this->validString($entity->client->{$field})) + continue; + + $errors[] = ['field' => ctrans("texts.{$field}")]; + + } + + //If not an individual, you MUST have a VAT number + if ($client->classification != 'individual' && !$this->validString($client->vat_number)) { + $errors[] = ['field' => ctrans("texts.vat_number")]; + } + + return $errors; + + } + + private function testCompanyState($entity): array + { + + $settings_object = $entity->client ? $entity->client : $entity->company; + $errors = []; + + foreach($this->company_settings_fields as $field) + { + if($this->validString($settings_object->getSetting($field))) + continue; + + $errors[] = ['field' => ctrans("texts.{$field}")]; + + } + + //test legal entity id present + if(!is_int($entity->company->legal_entity_id)) + $errors[] = ['field' => "You have not registered a legal entity id as yet."]; + + //If not an individual, you MUST have a VAT number + if($settings_object->getSetting('classification') != 'individual' && !$this->validString($settings_object->getSetting('vat_number'))) + { + $errors[] = ['field' => ctrans("texts.vat_number")]; + } + + // foreach($this->company_fields as $field) + // { + + // } + + return $errors; + + } + + private function testInvoiceState(): array + { + $errors = []; + + foreach($this->invoice_fields as $field) + { + + } + + return $errors; + } + + // private function testVendorState(): array + // { + + // } + + + /************************************ helpers ************************************/ + private function validString(?string $string) + { + return iconv_strlen($string) > 1; + } + +} \ No newline at end of file