Roberto Santini 12 часов назад
Родитель
Сommit
3f5355f54a

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

@@ -25,7 +25,14 @@ class FatturaFicService
25 25
             throw new RuntimeException('Una fattura annullata non si invia.');
26 26
         }
27 27
         if ($fattura->ficDocumentId()) {
28
-            throw new RuntimeException('Questa fattura è già su Fatture in Cloud.');
28
+            if (data_get($fattura->meta, 'fic.attachment')) {
29
+                throw new RuntimeException('Questa fattura è già su Fatture in Cloud.');
30
+            }
31
+            if (! $this->allegaDettaglio($fattura, (int) $fattura->ficDocumentId())) {
32
+                throw new RuntimeException('Questa fattura è già su Fatture in Cloud.');
33
+            }
34
+
35
+            return 'Dettaglio ticket allegato alla fattura su Fatture in Cloud.';
29 36
         }
30 37
         if ($fattura->righe->isEmpty()) {
31 38
             throw new RuntimeException('La fattura non ha righe.');
@@ -76,24 +83,41 @@ class FatturaFicService
76 83
         $fattura->save();
77 84
 
78 85
         $allegato = '';
79
-        $path = $this->report->store($fattura);
80
-        if ($path !== null) {
81
-            try {
82
-                $nome = str_replace(['/', '\\'], '-', $this->report->filename($fattura));
83
-                $this->fic->uploadIssuedDocumentAttachment(
84
-                    (int) $document['id'],
85
-                    $nome,
86
-                    (string) \Illuminate\Support\Facades\Storage::disk('local')->get($path)
87
-                );
86
+        try {
87
+            if ($this->allegaDettaglio($fattura, (int) $document['id'])) {
88 88
                 $allegato = ' Il dettaglio ticket è allegato.';
89
-            } catch (RuntimeException $e) {
90
-                $allegato = ' Il dettaglio ticket non è stato allegato: '.$this->conScope($e->getMessage());
91 89
             }
90
+        } catch (RuntimeException $e) {
91
+            $allegato = ' Il dettaglio ticket non è stato allegato: '.$this->conScope($e->getMessage());
92 92
         }
93 93
 
94 94
         return 'Fattura '.$fattura->numero_documento.' allineata a Fatture in Cloud.'.$allegato.' L\'invio allo SDI lo fai da lì.';
95 95
     }
96 96
 
97
+    protected function allegaDettaglio(Fattura $fattura, int $documentId): bool
98
+    {
99
+        $path = $this->report->store($fattura);
100
+        if ($path === null) {
101
+            return false;
102
+        }
103
+
104
+        $nome = str_replace(['/', '\\'], '-', $this->report->filename($fattura));
105
+        $this->fic->uploadIssuedDocumentAttachment(
106
+            $documentId,
107
+            $nome,
108
+            (string) \Illuminate\Support\Facades\Storage::disk('local')->get($path)
109
+        );
110
+
111
+        $meta = is_array($fattura->meta) ? $fattura->meta : [];
112
+        $fic = is_array($meta['fic'] ?? null) ? $meta['fic'] : [];
113
+        $fic['attachment'] = true;
114
+        $meta['fic'] = $fic;
115
+        $fattura->meta = $meta;
116
+        $fattura->save();
117
+
118
+        return true;
119
+    }
120
+
97 121
     protected function collegamento(Fattura $fattura): ?FicCollegamento
98 122
     {
99 123
         $ids = array_values(array_filter([

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

@@ -439,14 +439,22 @@ class FattureInCloudClient
439 439
         return $this->documentOrFail($json['data'] ?? null);
440 440
     }
441 441
 
442
-    /**
443
-     * @return array<string, mixed>
444
-     */
445 442
     public function uploadIssuedDocumentAttachment(int $documentId, string $filename, string $binary): void
446 443
     {
447
-        $this->decode($this->http()->post($this->url('/issued_documents/'.$documentId.'/attachment'), [
448
-            'filename' => $filename,
449
-            'attachment' => base64_encode($binary),
444
+        $uploaded = $this->decode($this->http()
445
+            ->attach('attachment', $binary, $filename)
446
+            ->post($this->url('/issued_documents/attachment'), [
447
+                'filename' => $filename,
448
+            ]));
449
+        $token = trim((string) data_get($uploaded, 'data.attachment_token', data_get($uploaded, 'attachment_token')));
450
+        if ($token === '') {
451
+            throw new RuntimeException('Fatture in Cloud non ha restituito il token dell\'allegato.');
452
+        }
453
+
454
+        $this->decode($this->http()->put($this->url('/issued_documents/'.$documentId), [
455
+            'data' => [
456
+                'attachment_token' => $token,
457
+            ],
450 458
         ]));
451 459
     }
452 460
 

+ 13
- 0
resources/views/fatturazione/show.blade.php Просмотреть файл

@@ -45,8 +45,21 @@
45 45
     </button>
46 46
   </form>
47 47
   @endif
48
+  @php
49
+    $haDettaglioTicket = collect(data_get($fattura->meta, 'dettaglio_ticket', []))->isNotEmpty()
50
+      || $fattura->righe->contains(fn ($r) => filled($r->segnalazione_id));
51
+  @endphp
48 52
   @if($fattura->ficDocumentId())
49 53
   <span class="badge bg-label-success">Su Fatture in Cloud n. {{ data_get($fattura->meta, 'fic.number') }}{{ data_get($fattura->meta, 'fic.numeration') }}</span>
54
+  @if(! data_get($fattura->meta, 'fic.attachment') && $haDettaglioTicket)
55
+  <form method="POST" action="{{ route('admin.fatturazione.fic', $fattura->id) }}" class="d-inline"
56
+    onsubmit="return confirm('Allegare il dettaglio ticket alla fattura su Fatture in Cloud?');">
57
+    @csrf
58
+    <button type="submit" class="btn btn-label-info btn-sm">
59
+      <i class="bx bx-paperclip me-1"></i>Allega dettaglio ticket
60
+    </button>
61
+  </form>
62
+  @endif
50 63
   @elseif(! $fattura->isAnnullata())
51 64
   <form method="POST" action="{{ route('admin.fatturazione.fic', $fattura->id) }}" class="d-inline"
52 65
     onsubmit="return confirm('Creare questa fattura su Fatture in Cloud?');">

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