Browse Source

fatture in cloud

Roberto Santini 3 weeks ago
parent
commit
aa024e3fc1
2 changed files with 40 additions and 2 deletions
  1. 12
    0
      app/Services/FatturaFicService.php
  2. 28
    2
      app/Services/FattureInCloudClient.php

+ 12
- 0
app/Services/FatturaFicService.php View File

@@ -119,6 +119,8 @@ class FatturaFicService
119 119
             'address_province',
120 120
             'country',
121 121
             'email',
122
+            'ei_code',
123
+            'certified_email',
122 124
         ] as $field) {
123 125
             $value = trim((string) ($client[$field] ?? ''));
124 126
             if ($value !== '') {
@@ -128,6 +130,12 @@ class FatturaFicService
128 130
         if (empty($entity['name'])) {
129 131
             $entity['name'] = $fattura->ragione_sociale ?: 'Cliente';
130 132
         }
133
+        if (empty($entity['country'])) {
134
+            $entity['country'] = 'Italia';
135
+        }
136
+        if (empty($entity['ei_code'])) {
137
+            $entity['ei_code'] = '0000000';
138
+        }
131 139
 
132 140
         $items = [];
133 141
         foreach ($fattura->righe as $riga) {
@@ -156,6 +164,10 @@ class FatturaFicService
156 164
                 'status' => 'not_paid',
157 165
             ]],
158 166
             'e_invoice' => true,
167
+            'ei_data' => [
168
+                'vat_kind' => 'I',
169
+                'payment_method' => 'MP05',
170
+            ],
159 171
         ];
160 172
 
161 173
         if ($fattura->periodo_da && $fattura->periodo_a) {

+ 28
- 2
app/Services/FattureInCloudClient.php View File

@@ -455,10 +455,36 @@ class FattureInCloudClient
455 455
             return $response->json() ?? [];
456 456
         }
457 457
 
458
-        $message = data_get($response->json(), 'error.message')
459
-            ?: data_get($response->json(), 'message')
458
+        $json = $response->json();
459
+        $message = data_get($json, 'error.message')
460
+            ?: data_get($json, 'message')
460 461
             ?: 'Fatture in Cloud ha risposto con errore '.$response->status().'.';
462
+        $details = $this->validationDetails(data_get($json, 'error.validation_result'));
463
+        if ($details !== '') {
464
+            $message .= ' '.$details;
465
+        }
461 466
 
462 467
         throw new RuntimeException((string) $message);
463 468
     }
469
+
470
+    private function validationDetails(mixed $result): string
471
+    {
472
+        if (is_string($result)) {
473
+            return trim($result);
474
+        }
475
+        if (! is_array($result)) {
476
+            return '';
477
+        }
478
+
479
+        $parts = [];
480
+        foreach ($result as $key => $value) {
481
+            $text = $this->validationDetails($value);
482
+            if ($text === '') {
483
+                continue;
484
+            }
485
+            $parts[] = is_string($key) ? $key.': '.$text : $text;
486
+        }
487
+
488
+        return implode(' ', $parts);
489
+    }
464 490
 }

Loading…
Cancel
Save