Browse Source

fatture in cloud

Roberto Santini 3 weeks ago
parent
commit
d575cb086e

+ 10
- 0
app/Http/Controllers/AziendaController.php View File

@@ -66,6 +66,8 @@ class AziendaController extends Controller implements HasMiddleware
66 66
             'nome' => 'nullable|string|max:255',
67 67
             'piva' => 'nullable|string|max:50',
68 68
             'cod_fiscale' => 'nullable|string|max:16',
69
+            'codice_sdi' => ['nullable', 'string', 'regex:/^[A-Za-z0-9]{6,7}$/'],
70
+            'pec' => 'nullable|email|max:255',
69 71
             'email' => 'nullable|email|max:255',
70 72
             'telefono' => 'nullable|string|max:255',
71 73
             'indirizzo' => 'nullable|string|max:500',
@@ -91,6 +93,12 @@ class AziendaController extends Controller implements HasMiddleware
91 93
         $azienda->cod_fiscale = isset($validated['cod_fiscale']) && trim((string) $validated['cod_fiscale']) !== ''
92 94
             ? strtoupper(trim($validated['cod_fiscale']))
93 95
             : null;
96
+        $azienda->codice_sdi = isset($validated['codice_sdi']) && trim((string) $validated['codice_sdi']) !== ''
97
+            ? strtoupper(trim($validated['codice_sdi']))
98
+            : null;
99
+        $azienda->pec = isset($validated['pec']) && trim((string) $validated['pec']) !== ''
100
+            ? trim($validated['pec'])
101
+            : null;
94 102
         $azienda->email = $validated['email'] ?? null;
95 103
         $azienda->telefono = $validated['telefono'] ?? null;
96 104
         $azienda->indirizzo = $validated['indirizzo'] ?? null;
@@ -131,6 +139,8 @@ class AziendaController extends Controller implements HasMiddleware
131 139
             'nome' => $azienda->nome,
132 140
             'piva' => $azienda->piva,
133 141
             'cod_fiscale' => $azienda->cod_fiscale,
142
+            'codice_sdi' => $azienda->codice_sdi,
143
+            'pec' => $azienda->pec,
134 144
             'email' => $azienda->email,
135 145
             'telefono' => $azienda->telefono,
136 146
             'indirizzo' => $azienda->indirizzo,

+ 6
- 0
app/Http/Controllers/UserController.php View File

@@ -86,6 +86,8 @@ class UserController extends Controller implements HasMiddleware
86 86
       'ragione_sociale' => 'nullable|string|max:255',
87 87
       'piva' => 'nullable|string|max:50',
88 88
       'cod_fiscale' => 'nullable|string|max:50',
89
+      'codice_sdi' => ['nullable', 'string', 'regex:/^[A-Za-z0-9]{6,7}$/'],
90
+      'pec' => 'nullable|email|max:255',
89 91
       'indirizzo' => 'nullable|string|max:255',
90 92
       'comune_id' => 'nullable|exists:comune,id',
91 93
       'note' => 'nullable|string',
@@ -100,6 +102,10 @@ class UserController extends Controller implements HasMiddleware
100 102
       }
101 103
     }
102 104
 
105
+    if (array_key_exists('codice_sdi', $validated) && $validated['codice_sdi']) {
106
+      $validated['codice_sdi'] = strtoupper($validated['codice_sdi']);
107
+    }
108
+
103 109
     if (array_key_exists('dominio_email', $validated)) {
104 110
       $validated['dominio_email'] = $user->is_azienda
105 111
         ? User::normalizeDominioEmail($validated['dominio_email'])

+ 8
- 0
app/Models/User.php View File

@@ -15,6 +15,14 @@ use Illuminate\Support\Facades\Storage;
15 15
 
16 16
 class User extends \App\Models\AbstractModels\AbstractUser
17 17
 {
18
+  public function getFillable()
19
+  {
20
+    return array_merge(parent::getFillable(), [
21
+      'codice_sdi',
22
+      'pec',
23
+    ]);
24
+  }
25
+
18 26
 
19 27
   use HasApiTokens;
20 28
   use HasRoles;

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

@@ -4,6 +4,7 @@ namespace App\Services;
4 4
 
5 5
 use App\Models\Config;
6 6
 use App\Models\Fattura;
7
+use App\Models\User;
7 8
 use App\Models\FatturaRiga;
8 9
 use App\Models\FicCollegamento;
9 10
 use App\Models\Prodotto;
@@ -38,7 +39,8 @@ class FatturaFicService
38 39
         try {
39 40
             $client = $this->fic->getClient((int) $collegamento->fic_client_id);
40 41
             $vatId = $this->vatId((float) ($fattura->iva_percentuale ?? Config::ivaPercentualeDefault()));
41
-            $document = $this->fic->createIssuedDocument($this->payload($fattura, $client, $vatId));
42
+            $anagrafica = User::find($collegamento->user_id);
43
+            $document = $this->fic->createIssuedDocument($this->payload($fattura, $client, $vatId, $anagrafica));
42 44
             if (! isset($document['number']) || $document['number'] === '' || $document['number'] === null) {
43 45
                 $document = $this->fic->getIssuedDocument((int) $document['id']);
44 46
             }
@@ -105,7 +107,7 @@ class FatturaFicService
105 107
      * @param  array<string, mixed>  $client
106 108
      * @return array<string, mixed>
107 109
      */
108
-    protected function payload(Fattura $fattura, array $client, int $vatId): array
110
+    protected function payload(Fattura $fattura, array $client, int $vatId, ?User $anagrafica = null): array
109 111
     {
110 112
         $data = $fattura->data ? Carbon::parse($fattura->data)->toDateString() : now()->toDateString();
111 113
         $entity = ['id' => (int) $client['id']];
@@ -133,9 +135,16 @@ class FatturaFicService
133 135
         if (empty($entity['country'])) {
134 136
             $entity['country'] = 'Italia';
135 137
         }
136
-        if (empty($entity['ei_code'])) {
138
+        $codiceSdi = strtoupper(trim((string) ($anagrafica->codice_sdi ?? '')));
139
+        if ($codiceSdi !== '') {
140
+            $entity['ei_code'] = $codiceSdi;
141
+        } elseif (empty($entity['ei_code'])) {
137 142
             $entity['ei_code'] = '0000000';
138 143
         }
144
+        $pec = trim((string) ($anagrafica->pec ?? ''));
145
+        if ($pec !== '') {
146
+            $entity['certified_email'] = $pec;
147
+        }
139 148
 
140 149
         $items = [];
141 150
         foreach ($fattura->righe as $riga) {

+ 16
- 1
app/Services/FattureInCloudClient.php View File

@@ -20,6 +20,8 @@ class FattureInCloudClient
20 20
         'address_street' => 'Indirizzo',
21 21
         'citta' => 'Città',
22 22
         'notes' => 'Note',
23
+        'codice_sdi' => 'Codice SDI',
24
+        'pec' => 'PEC',
23 25
     ];
24 26
 
25 27
     public function configured(): bool
@@ -175,6 +177,8 @@ class FattureInCloudClient
175 177
                 'address_city' => $raw['city'] ?? '',
176 178
                 'address_province' => $raw['province'] ?? '',
177 179
             ],
180
+            'codice_sdi' => ['ei_code' => strtoupper(trim((string) ($raw['value'] ?? '')))],
181
+            'pec' => ['certified_email' => trim((string) ($raw['value'] ?? ''))],
178 182
             default => [$key => $raw['value'] ?? ''],
179 183
         };
180 184
     }
@@ -205,6 +209,8 @@ class FattureInCloudClient
205 209
                 'raw' => ['city' => $city, 'province' => $province],
206 210
             ],
207 211
             'notes' => $this->pack(trim((string) $azienda->note)),
212
+            'codice_sdi' => $this->pack(strtoupper(trim((string) $azienda->codice_sdi))),
213
+            'pec' => $this->pack(strtolower(trim((string) $azienda->pec)), trim((string) $azienda->pec)),
208 214
         ];
209 215
     }
210 216
 
@@ -229,6 +235,8 @@ class FattureInCloudClient
229 235
                 'raw' => ['city' => $city, 'province' => $province],
230 236
             ],
231 237
             'notes' => $this->pack(trim((string) ($client['notes'] ?? ''))),
238
+            'codice_sdi' => $this->pack(strtoupper(trim((string) ($client['ei_code'] ?? '')))),
239
+            'pec' => $this->pack(strtolower(trim((string) ($client['certified_email'] ?? ''))), trim((string) ($client['certified_email'] ?? ''))),
232 240
         ];
233 241
     }
234 242
 
@@ -244,6 +252,8 @@ class FattureInCloudClient
244 252
             'phone' => $this->applyString($azienda, 'telefono', $value, 255),
245 253
             'address_street' => $this->applyString($azienda, 'indirizzo', $value, 255),
246 254
             'notes' => $this->applyNote($azienda, $value),
255
+            'codice_sdi' => $this->applyString($azienda, 'codice_sdi', strtoupper($value), 7),
256
+            'pec' => $this->applyEmailField($azienda, 'pec', $value),
247 257
             'citta' => $this->applyCitta($azienda, (string) ($raw['city'] ?? ''), (string) ($raw['province'] ?? '')),
248 258
             default => null,
249 259
         };
@@ -289,11 +299,16 @@ class FattureInCloudClient
289 299
     }
290 300
 
291 301
     private function applyEmail(Azienda $azienda, string $value): ?string
302
+    {
303
+        return $this->applyEmailField($azienda, 'email', $value);
304
+    }
305
+
306
+    private function applyEmailField(Azienda $azienda, string $attribute, string $value): ?string
292 307
     {
293 308
         if ($value !== '' && ! filter_var($value, FILTER_VALIDATE_EMAIL)) {
294 309
             return 'L\'email remota non è valida.';
295 310
         }
296
-        $azienda->email = $value !== '' ? $value : null;
311
+        $azienda->{$attribute} = $value !== '' ? $value : null;
297 312
 
298 313
         return null;
299 314
     }

+ 23
- 0
database/migrations/2026_10_12_230800_add_codice_sdi_and_pec_to_users_table.php View File

@@ -0,0 +1,23 @@
1
+<?php
2
+
3
+use Illuminate\Database\Migrations\Migration;
4
+use Illuminate\Database\Schema\Blueprint;
5
+use Illuminate\Support\Facades\Schema;
6
+
7
+return new class extends Migration
8
+{
9
+    public function up(): void
10
+    {
11
+        Schema::table('users', function (Blueprint $table) {
12
+            $table->string('codice_sdi', 7)->nullable()->after('cod_fiscale');
13
+            $table->string('pec', 255)->nullable()->after('codice_sdi');
14
+        });
15
+    }
16
+
17
+    public function down(): void
18
+    {
19
+        Schema::table('users', function (Blueprint $table) {
20
+            $table->dropColumn(['codice_sdi', 'pec']);
21
+        });
22
+    }
23
+};

+ 2
- 0
resources/views/azienda/modal-scripts.blade.php View File

@@ -73,6 +73,8 @@
73 73
       $('#azienda_nome').val(data.nome || '');
74 74
       $('#azienda_piva').val(data.piva || '');
75 75
       $('#azienda_cod_fiscale').val(data.cod_fiscale || '');
76
+      $('#azienda_codice_sdi').val(data.codice_sdi || '');
77
+      $('#azienda_pec').val(data.pec || '');
76 78
       $('#azienda_telefono').val(data.telefono || '');
77 79
       $('#azienda_email').val(data.email || '');
78 80
       $('#azienda_dominio_email').val(data.dominio_email || '');

+ 9
- 0
resources/views/azienda/modal.blade.php View File

@@ -36,6 +36,15 @@
36 36
               <label class="form-label" for="azienda_cod_fiscale">Codice fiscale</label>
37 37
               <input type="text" class="form-control text-uppercase" name="cod_fiscale" id="azienda_cod_fiscale" maxlength="16">
38 38
             </div>
39
+            <div class="col-md-6">
40
+              <label class="form-label" for="azienda_codice_sdi">Codice SDI</label>
41
+              <input type="text" class="form-control text-uppercase" name="codice_sdi" id="azienda_codice_sdi" maxlength="7"
42
+                placeholder="7 caratteri, oppure 0000000">
43
+            </div>
44
+            <div class="col-md-6">
45
+              <label class="form-label" for="azienda_pec">PEC</label>
46
+              <input type="email" class="form-control" name="pec" id="azienda_pec" maxlength="255">
47
+            </div>
39 48
             <div class="col-md-6">
40 49
               <label class="form-label" for="azienda_telefono">Telefono</label>
41 50
               <input type="text" class="form-control" name="telefono" id="azienda_telefono" maxlength="255">

+ 8
- 0
resources/views/user/scheda.blade.php View File

@@ -202,6 +202,14 @@
202 202
               <label class="form-label">Codice fiscale</label>
203 203
               <input type="text" name="cod_fiscale" class="form-control" value="{{ old('cod_fiscale', $user->cod_fiscale) }}">
204 204
             </div>
205
+            <div class="col-md-4">
206
+              <label class="form-label">Codice SDI</label>
207
+              <input type="text" name="codice_sdi" class="form-control text-uppercase" maxlength="7" value="{{ old('codice_sdi', $user->codice_sdi) }}" placeholder="7 caratteri">
208
+            </div>
209
+            <div class="col-md-4">
210
+              <label class="form-label">PEC</label>
211
+              <input type="email" name="pec" class="form-control" maxlength="255" value="{{ old('pec', $user->pec) }}">
212
+            </div>
205 213
             <div class="col-md-4">
206 214
               <label class="form-label">Indirizzo</label>
207 215
               <input type="text" name="indirizzo" class="form-control" value="{{ old('indirizzo', $user->indirizzo) }}">

Loading…
Cancel
Save