Roberto Santini 3 недель назад
Родитель
Сommit
aa024e3fc1
2 измененных файлов: 40 добавлений и 2 удалений
  1. 12
    0
      app/Services/FatturaFicService.php
  2. 28
    2
      app/Services/FattureInCloudClient.php

+ 12
- 0
app/Services/FatturaFicService.php Просмотреть файл

119
             'address_province',
119
             'address_province',
120
             'country',
120
             'country',
121
             'email',
121
             'email',
122
+            'ei_code',
123
+            'certified_email',
122
         ] as $field) {
124
         ] as $field) {
123
             $value = trim((string) ($client[$field] ?? ''));
125
             $value = trim((string) ($client[$field] ?? ''));
124
             if ($value !== '') {
126
             if ($value !== '') {
128
         if (empty($entity['name'])) {
130
         if (empty($entity['name'])) {
129
             $entity['name'] = $fattura->ragione_sociale ?: 'Cliente';
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
         $items = [];
140
         $items = [];
133
         foreach ($fattura->righe as $riga) {
141
         foreach ($fattura->righe as $riga) {
156
                 'status' => 'not_paid',
164
                 'status' => 'not_paid',
157
             ]],
165
             ]],
158
             'e_invoice' => true,
166
             'e_invoice' => true,
167
+            'ei_data' => [
168
+                'vat_kind' => 'I',
169
+                'payment_method' => 'MP05',
170
+            ],
159
         ];
171
         ];
160
 
172
 
161
         if ($fattura->periodo_da && $fattura->periodo_a) {
173
         if ($fattura->periodo_da && $fattura->periodo_a) {

+ 28
- 2
app/Services/FattureInCloudClient.php Просмотреть файл

455
             return $response->json() ?? [];
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
             ?: 'Fatture in Cloud ha risposto con errore '.$response->status().'.';
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
         throw new RuntimeException((string) $message);
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
 }

Загрузка…
Отмена
Сохранить