-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathserver.py
More file actions
1467 lines (1353 loc) · 70.5 KB
/
Copy pathserver.py
File metadata and controls
1467 lines (1353 loc) · 70.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Fatture in Cloud MCP Server - v2.0.0
MCP Server per integrare Fatture in Cloud con Claude AI.
Permette di gestire fatture elettroniche italiane tramite conversazione.
Author: Mediaform s.c.r.l. (https://media-form.it)
License: MIT
"""
import json
import os
import traceback
from datetime import datetime, timedelta
import fattureincloud_python_sdk as fic
from fattureincloud_python_sdk.api.issued_documents_api import IssuedDocumentsApi
from fattureincloud_python_sdk.api.issued_e_invoices_api import IssuedEInvoicesApi
from fattureincloud_python_sdk.api.received_documents_api import ReceivedDocumentsApi
from fattureincloud_python_sdk.api.clients_api import ClientsApi
from fattureincloud_python_sdk.api.companies_api import CompaniesApi
from fattureincloud_python_sdk.api.info_api import InfoApi
from mcp.server import Server
from mcp.server.stdio import stdio_server
from mcp.types import Tool, TextContent, ToolAnnotations
import cache
def _ann(read_only=False, destructive=False, idempotent=False, open_world=True):
"""Shorthand for MCP tool annotations. openWorld defaults to True since
every tool talks to the FattureInCloud API."""
return ToolAnnotations(
readOnlyHint=read_only,
destructiveHint=destructive,
idempotentHint=idempotent,
openWorldHint=open_world,
)
ACCESS_TOKEN = os.getenv("FIC_ACCESS_TOKEN", "")
COMPANY_ID = int(os.getenv("FIC_COMPANY_ID", "0"))
SENDER_EMAIL = os.getenv("FIC_SENDER_EMAIL", "")
configuration = fic.Configuration()
configuration.access_token = ACCESS_TOKEN
api_client = fic.ApiClient(configuration)
issued_api = IssuedDocumentsApi(api_client)
einvoice_api = IssuedEInvoicesApi(api_client)
received_api = ReceivedDocumentsApi(api_client)
clients_api = ClientsApi(api_client)
companies_api = CompaniesApi(api_client)
info_api = InfoApi(api_client)
app = Server("fattureincloud")
def get_total_from_doc(d):
payments = d.get('payments_list', [])
if payments:
return sum(p.get('amount', 0) for p in payments)
items = d.get('items_list', [])
return sum((i.get('qty', 0) * i.get('gross_price', 0)) for i in items)
def get_client_by_id(client_id, *, company_id=None):
if company_id is None:
company_id = COMPANY_ID
resource = f"client_{client_id}"
hit = cache.get(resource, company_id, ttl=timedelta(hours=24))
if hit is not None:
return hit
try:
response = clients_api.get_client(company_id=company_id, client_id=client_id)
data = response.data.to_dict()
cache.put(resource, company_id, data)
return data
except:
return None
def get_ei_code_for_client(client_id, *, company_id=None):
if company_id is None:
company_id = COMPANY_ID
try:
client = get_client_by_id(client_id, company_id=company_id)
if client:
ei_code = (client.get('ei_code') or '').strip()
if ei_code:
return ei_code
pec = (client.get('certified_email') or '').strip()
if pec:
return '0000000'
return '0000000'
except:
return '0000000'
@cache.cached("cost_centers", ttl=timedelta(hours=24))
def fetch_cost_centers(*, company_id):
"""Cost centers (FIC `/info/cost_centers`). Used to validate `cost_center`
on received documents only."""
try:
response = info_api.list_cost_centers(company_id=company_id)
return list(response.data or [])
except Exception:
return []
@cache.cached("revenue_centers", ttl=timedelta(hours=24))
def fetch_revenue_centers(*, company_id):
"""Revenue centers (FIC `/info/revenue_centers`). Used to validate
`revenue_center` on issued documents only.
FIC keeps cost and revenue centers as two separate registries; the
`list_cost_centers` MCP tool returns their union (matching the
'Analisi centri c/r' view in the FIC UI), but mutation validation
has to use the type-specific list to match how the FIC API itself
accepts/rejects values."""
try:
response = info_api.list_revenue_centers(company_id=company_id)
return list(response.data or [])
except Exception:
return []
def build_entity_from_client(client_id, client_data=None):
if not client_data:
client_data = get_client_by_id(client_id)
if not client_data:
return None
ei_code = get_ei_code_for_client(client_id)
entity = {
"id": client_id,
"name": client_data.get("name", ""),
"vat_number": client_data.get("vat_number", ""),
"tax_code": client_data.get("tax_code", ""),
"address_street": client_data.get("address_street", ""),
"address_city": client_data.get("address_city", ""),
"address_postal_code": client_data.get("address_postal_code", ""),
"address_province": client_data.get("address_province", ""),
"country": client_data.get("country", "Italia"),
"ei_code": ei_code,
}
pec = (client_data.get("certified_email") or "").strip()
if pec:
entity["certified_email"] = pec
return entity
def build_items_list(items_data, negate=False):
items_list = []
for item in items_data:
vat_rate = item.get("vat_rate", 22)
net_price = item["net_price"]
if negate:
net_price = -abs(net_price)
items_list.append({
"name": item["name"],
"description": item.get("description", ""),
"qty": item["qty"],
"net_price": net_price,
"vat": {"id": 0, "value": vat_rate}
})
return items_list
def build_issued_document(doc_type, client_id, items_data, date_str, payment_days,
visible_subject, negate_prices=False, source_invoice_id=None,
revenue_center=None):
client_data = get_client_by_id(client_id)
if not client_data:
return None, f"Cliente con ID {client_id} non trovato"
if revenue_center:
known = fetch_revenue_centers(company_id=COMPANY_ID)
if revenue_center not in known:
return None, (
f"revenue_center '{revenue_center}' non esiste. "
f"Disponibili: {known}. Crearli da web FIC (Impostazioni → Centri di costo/ricavo)."
)
entity = build_entity_from_client(client_id, client_data)
items_list = build_items_list(items_data, negate=False)
invoice_date = datetime.strptime(date_str, "%Y-%m-%d")
due_date = invoice_date + timedelta(days=payment_days)
total_abs = sum(
abs(i["qty"] * i["net_price"]) * (1 + i["vat"]["value"] / 100)
for i in items_list
)
result_total = -total_abs if negate_prices else total_abs
body_data = {
"type": doc_type,
"entity": entity,
"date": date_str,
"visible_subject": visible_subject,
"items_list": items_list,
"payments_list": [{
"amount": round(total_abs, 2),
"due_date": due_date.strftime("%Y-%m-%d"),
"status": "not_paid",
"payment_terms": {"days": payment_days, "type": "standard"}
}]
}
if revenue_center:
body_data["rc_center"] = revenue_center
if doc_type in ("invoice", "credit_note"):
body_data["e_invoice"] = True
body_data["ei_data"] = {"payment_method": "MP05"}
if source_invoice_id:
body_data["original_document"] = {"id": source_invoice_id}
response = issued_api.create_issued_document(
company_id=COMPANY_ID,
create_issued_document_request={"data": body_data}
)
d = response.data.to_dict()
result = {
"success": True,
"id": d.get("id"),
"number": d.get("number"),
"date": str(d.get("date", "")),
"due_date": due_date.strftime("%Y-%m-%d"),
"client": client_data.get("name"),
"ei_code": entity.get("ei_code", "N/A"),
"total": round(result_total, 2),
"type": doc_type,
"status": "bozza",
}
if revenue_center:
result["revenue_center"] = revenue_center
if source_invoice_id:
result["linked_to_invoice"] = source_invoice_id
return result, None
@app.list_tools()
async def list_tools():
item_schema = {
"type": "object",
"properties": {
"name": {"type": "string", "description": "Nome prodotto/servizio"},
"description": {"type": "string", "description": "Descrizione estesa"},
"qty": {"type": "number", "description": "Quantità"},
"net_price": {"type": "number", "description": "Prezzo netto unitario (sempre positivo)"},
"vat_rate": {"type": "number", "description": "Aliquota IVA (es. 22)"}
},
"required": ["name", "qty", "net_price"]
}
return [
Tool(
name="list_invoices",
description="Lista documenti emessi. Parametri: year (int), month (int opzionale), query (str opzionale), type (str opzionale: invoice, credit_note, proforma — default: invoice)",
inputSchema={
"type": "object",
"properties": {
"year": {"type": "integer", "description": "Anno (es. 2024)"},
"month": {"type": "integer", "description": "Mese 1-12 (opzionale)"},
"query": {"type": "string", "description": "Filtro testuale (opzionale)"},
"type": {"type": "string", "description": "Tipo documento: invoice (default), credit_note, proforma"}
},
"required": ["year"]
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="get_invoice",
description="Dettaglio documento per ID (fattura, NDC, proforma)",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID documento"}
},
"required": ["document_id"]
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="get_pdf_url",
description="Restituisce URL PDF e link web di un documento (fattura, NDC, proforma)",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID documento"}
},
"required": ["document_id"]
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="list_clients",
description="Lista clienti",
inputSchema={
"type": "object",
"properties": {
"query": {"type": "string", "description": "Filtro nome/ragione sociale (opzionale)"}
}
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="get_company_info",
description="Info azienda collegata",
inputSchema={"type": "object", "properties": {}},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="create_client",
description="Crea nuovo cliente in anagrafica",
inputSchema={
"type": "object",
"properties": {
"name": {"type": "string", "description": "Nome/Ragione sociale"},
"vat_number": {"type": "string", "description": "Partita IVA (opzionale)"},
"tax_code": {"type": "string", "description": "Codice fiscale (opzionale)"},
"ei_code": {"type": "string", "description": "Codice destinatario SDI (opzionale)"},
"certified_email": {"type": "string", "description": "PEC (opzionale)"},
"email": {"type": "string", "description": "Email ordinaria (opzionale)"},
"address_street": {"type": "string", "description": "Indirizzo (opzionale)"},
"address_city": {"type": "string", "description": "Città (opzionale)"},
"address_postal_code": {"type": "string", "description": "CAP (opzionale)"},
"address_province": {"type": "string", "description": "Provincia (opzionale)"},
"country": {"type": "string", "description": "Paese (default: Italia)"},
"phone": {"type": "string", "description": "Telefono (opzionale)"}
},
"required": ["name"]
},
annotations=_ann(),
),
Tool(
name="update_client",
description="Aggiorna dati cliente esistente. Passa solo i campi da modificare.",
inputSchema={
"type": "object",
"properties": {
"client_id": {"type": "integer", "description": "ID cliente"},
"name": {"type": "string", "description": "Nome/Ragione sociale (opzionale)"},
"vat_number": {"type": "string", "description": "Partita IVA (opzionale)"},
"tax_code": {"type": "string", "description": "Codice fiscale (opzionale)"},
"ei_code": {"type": "string", "description": "Codice destinatario SDI (opzionale)"},
"certified_email": {"type": "string", "description": "PEC (opzionale)"},
"email": {"type": "string", "description": "Email ordinaria (opzionale)"},
"address_street": {"type": "string", "description": "Indirizzo (opzionale)"},
"address_city": {"type": "string", "description": "Città (opzionale)"},
"address_postal_code": {"type": "string", "description": "CAP (opzionale)"},
"address_province": {"type": "string", "description": "Provincia (opzionale)"},
"phone": {"type": "string", "description": "Telefono (opzionale)"}
},
"required": ["client_id"]
},
annotations=_ann(idempotent=True),
),
Tool(
name="create_invoice",
description="Crea nuova fattura (bozza). IMPORTANTE: Chiedere sempre conferma all'utente prima di eseguire.",
inputSchema={
"type": "object",
"properties": {
"client_id": {"type": "integer", "description": "ID cliente"},
"items": {"type": "array", "items": item_schema},
"date": {"type": "string", "description": "Data YYYY-MM-DD (default: oggi)"},
"payment_days": {"type": "integer", "description": "Giorni pagamento (default: 30)"},
"visible_subject": {"type": "string", "description": "Oggetto visibile"},
"revenue_center": {"type": "string", "description": "Centro di ricavo (opzionale, deve esistere — vedi list_cost_centers)"}
},
"required": ["client_id", "items"]
},
annotations=_ann(),
),
Tool(
name="create_credit_note",
description="Crea nota di credito (bozza). Importi POSITIVI in input, resi negativi automaticamente. IMPORTANTE: Chiedere sempre conferma all'utente prima di eseguire.",
inputSchema={
"type": "object",
"properties": {
"client_id": {"type": "integer", "description": "ID cliente"},
"items": {"type": "array", "items": item_schema},
"date": {"type": "string", "description": "Data YYYY-MM-DD (default: oggi)"},
"payment_days": {"type": "integer", "description": "Giorni pagamento (default: 30)"},
"visible_subject": {"type": "string", "description": "Oggetto visibile"},
"source_invoice_id": {"type": "integer", "description": "ID fattura originale da stornare (opzionale)"},
"revenue_center": {"type": "string", "description": "Centro di ricavo (opzionale, deve esistere — vedi list_cost_centers)"}
},
"required": ["client_id", "items"]
},
annotations=_ann(),
),
Tool(
name="create_proforma",
description="Crea proforma (bozza). Non inviabile allo SDI. IMPORTANTE: Chiedere sempre conferma all'utente prima di eseguire.",
inputSchema={
"type": "object",
"properties": {
"client_id": {"type": "integer", "description": "ID cliente"},
"items": {"type": "array", "items": item_schema},
"date": {"type": "string", "description": "Data YYYY-MM-DD (default: oggi)"},
"payment_days": {"type": "integer", "description": "Giorni pagamento (default: 30)"},
"visible_subject": {"type": "string", "description": "Oggetto visibile"},
"revenue_center": {"type": "string", "description": "Centro di ricavo (opzionale, deve esistere — vedi list_cost_centers)"}
},
"required": ["client_id", "items"]
},
annotations=_ann(),
),
Tool(
name="convert_proforma_to_invoice",
description="Converte una proforma in fattura elettronica (bozza). Di default elimina la proforma originale. IMPORTANTE: Chiedere sempre conferma all'utente prima di eseguire.",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID proforma da convertire"},
"date": {"type": "string", "description": "Data fattura YYYY-MM-DD (default: data proforma)"},
"keep_proforma": {"type": "boolean", "description": "Mantieni la proforma originale (default: false)"},
"revenue_center": {"type": "string", "description": "Centro di ricavo (opzionale, eredita da proforma se non passato)"}
},
"required": ["document_id"]
},
annotations=_ann(destructive=True),
),
Tool(
name="update_document",
description="Modifica parziale di un documento BOZZA (fattura, NDC, proforma). Passa solo i campi da aggiornare. Funziona solo su documenti non ancora inviati allo SDI. IMPORTANTE: Chiedere sempre conferma all'utente prima di eseguire.",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID documento da modificare"},
"date": {"type": "string", "description": "Nuova data YYYY-MM-DD (opzionale)"},
"visible_subject": {"type": "string", "description": "Nuovo oggetto visibile (opzionale)"},
"payment_days": {"type": "integer", "description": "Nuovi giorni pagamento (opzionale)"},
"items": {
"type": "array",
"items": item_schema,
"description": "Nuove righe documento (opzionale). Per NDC, importi sempre positivi."
},
"revenue_center": {"type": "string", "description": "Centro di ricavo (opzionale, mantiene quello esistente se non passato)"}
},
"required": ["document_id"]
},
annotations=_ann(idempotent=True),
),
Tool(
name="duplicate_invoice",
description="Duplica una fattura esistente con nuova data (crea bozza). IMPORTANTE: Chiedere sempre conferma all'utente prima di eseguire.",
inputSchema={
"type": "object",
"properties": {
"source_document_id": {"type": "integer", "description": "ID fattura da duplicare"},
"new_date": {"type": "string", "description": "Nuova data YYYY-MM-DD (default: oggi)"},
"payment_days": {"type": "integer", "description": "Giorni pagamento (default: eredita da originale)"},
"description_replace": {
"type": "object",
"description": "Sostituzioni testo nella descrizione (es. 2025->2026)",
"properties": {
"old": {"type": "string"},
"new": {"type": "string"}
}
},
"revenue_center": {"type": "string", "description": "Centro di ricavo (opzionale, eredita dalla fattura sorgente se non passato)"}
},
"required": ["source_document_id"]
},
annotations=_ann(),
),
Tool(
name="delete_invoice",
description="Elimina un documento BOZZA (fattura, NDC, proforma). ATTENZIONE: Azione irreversibile! Chiedere SEMPRE conferma esplicita all'utente.",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID documento da eliminare"}
},
"required": ["document_id"]
},
annotations=_ann(destructive=True, idempotent=True),
),
Tool(
name="send_to_sdi",
description="Invia fattura/NDC allo SDI. ATTENZIONE: Azione irreversibile! Chiedere SEMPRE conferma esplicita all'utente.",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID documento da inviare"}
},
"required": ["document_id"]
},
annotations=_ann(),
),
Tool(
name="get_invoice_status",
description="Controlla stato e-invoice/SDI di un documento",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID documento"}
},
"required": ["document_id"]
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="send_email",
description="Invia copia cortesia via email al cliente. Requires FIC_SENDER_EMAIL to be configured in extension settings. IMPORTANTE: Chiedere conferma prima di eseguire.",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID documento"},
"recipient_email": {"type": "string", "description": "Email destinatario (opzionale)"},
"subject": {"type": "string", "description": "Oggetto email (opzionale)"},
"body": {"type": "string", "description": "Corpo email (opzionale)"}
},
"required": ["document_id"]
},
annotations=_ann(),
),
Tool(
name="list_received_documents",
description="Lista fatture PASSIVE (ricevute dai fornitori). Parametri: year, month (opzionale), type (opzionale: expense, credit_note)",
inputSchema={
"type": "object",
"properties": {
"year": {"type": "integer", "description": "Anno"},
"month": {"type": "integer", "description": "Mese 1-12 (opzionale)"},
"type": {"type": "string", "description": "Tipo: expense, credit_note (default: expense)"},
"query": {"type": "string", "description": "Filtro testuale (opzionale)"}
},
"required": ["year"]
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="get_situation",
description="Dashboard anno: fatturato netto (fatture - NDC), incassato, da incassare, costi, margine. Supporta filtro per cliente.",
inputSchema={
"type": "object",
"properties": {
"year": {"type": "integer", "description": "Anno (default: corrente)"},
"client_name": {"type": "string", "description": "Filtro per nome cliente (opzionale, ricerca parziale)"}
}
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="check_numeration",
description="Verifica continuità numerica delle fatture emesse per un dato anno.",
inputSchema={
"type": "object",
"properties": {
"year": {"type": "integer", "description": "Anno da verificare (es. 2025)"}
},
"required": ["year"]
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="list_cost_centers",
description="Lista combinata di centri di costo e ricavo configurati in FattureInCloud. L'API FIC espone due liste separate (cost_centers per documenti ricevuti, revenue_centers per documenti emessi); questo tool ne ritorna l'unione deduplicata e ordinata, coerente con la vista 'Analisi centri c/r' della UI FIC. Le validation interne dei tool di mutazione sono type-specific: create_invoice/credit_note/proforma/update_document/duplicate_invoice/convert_proforma_to_invoice validano `revenue_center` contro la sola lista revenue_centers; create_received_document valida `cost_center` contro la sola lista cost_centers. Read-only.",
inputSchema={"type": "object", "properties": {}},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="get_received_document",
description="Dettaglio fattura passiva (ricevuta da fornitore) per ID. Read-only.",
inputSchema={
"type": "object",
"properties": {
"document_id": {"type": "integer", "description": "ID documento ricevuto"}
},
"required": ["document_id"]
},
annotations=_ann(read_only=True, idempotent=True),
),
Tool(
name="create_received_document",
description="Crea documento passivo (fattura ricevuta o NDC ricevuta) registrando una spesa. IMPORTANTE: Chiedere conferma all'utente prima di eseguire.",
inputSchema={
"type": "object",
"properties": {
"supplier_name": {"type": "string", "description": "Nome/Ragione sociale del fornitore"},
"supplier_vat_number": {"type": "string", "description": "Partita IVA del fornitore (opzionale)"},
"type": {"type": "string", "description": "Tipo: expense (default) o credit_note"},
"date": {"type": "string", "description": "Data documento YYYY-MM-DD (default: oggi)"},
"amount_net": {"type": "number", "description": "Importo netto"},
"amount_vat": {"type": "number", "description": "Importo IVA"},
"category": {"type": "string", "description": "Categoria spesa (opzionale)"},
"description": {"type": "string", "description": "Descrizione/oggetto (opzionale)"},
"cost_center": {"type": "string", "description": "Centro di costo (opzionale, deve esistere — vedi list_cost_centers)"}
},
"required": ["supplier_name", "amount_net"]
},
annotations=_ann(),
),
]
@app.call_tool()
async def call_tool(name: str, arguments: dict) -> list[TextContent]:
try:
if name == "list_invoices":
year = arguments.get("year", 2024)
month = arguments.get("month")
query = arguments.get("query")
doc_type = arguments.get("type", "invoice")
q = f"date >= '{year}-01-01' and date <= '{year}-12-31'"
if month:
last_day = 31 if month in [1,3,5,7,8,10,12] else 30 if month in [4,6,9,11] else 29
q = f"date >= '{year}-{month:02d}-01' and date <= '{year}-{month:02d}-{last_day}'"
response = issued_api.list_issued_documents(
company_id=COMPANY_ID, type=doc_type, q=q, per_page=100, fieldset="detailed"
)
invoices = []
for doc in (response.data or []):
d = doc.to_dict()
inv = {
"id": d.get("id"),
"number": d.get("number"),
"date": str(d.get("date", "")),
"client": d.get("entity", {}).get("name") if d.get("entity") else None,
"total": get_total_from_doc(d),
"subject": d.get("subject"),
"description": d.get("visible_subject")
}
if d.get("rc_center"):
inv["revenue_center"] = d["rc_center"]
if query:
search_text = f"{inv['client']} {inv['subject']} {inv['description']}".lower()
if query.lower() not in search_text:
continue
invoices.append(inv)
return [TextContent(type="text", text=json.dumps(invoices, indent=2, ensure_ascii=False))]
elif name == "get_invoice":
doc_id = arguments["document_id"]
response = issued_api.get_issued_document(
company_id=COMPANY_ID, document_id=doc_id, fieldset="detailed"
)
d = response.data.to_dict()
items = []
for i in d.get("items_list", []):
items.append({
"name": i.get("name"),
"description": i.get("description"),
"qty": i.get("qty"),
"net_price": i.get("net_price", 0),
"gross_price": i.get("gross_price", 0),
"vat": i.get("vat", {}).get("value") if i.get("vat") else None
})
payments = []
for p in d.get("payments_list", []):
pa = p.get("payment_account")
if hasattr(pa, 'to_dict'):
pa = pa.to_dict()
payments.append({
"amount": p.get("amount"),
"due_date": str(p.get("due_date", "")),
"status": str(p.get("status", "")).replace("IssuedDocumentStatus.", ""),
"paid_date": str(p.get("paid_date", "")) if p.get("paid_date") else None,
"payment_account_id": pa.get("id") if isinstance(pa, dict) else None
})
result = {
"id": d.get("id"),
"number": d.get("number"),
"date": str(d.get("date", "")),
"type": d.get("type"),
"client_id": d.get("entity", {}).get("id") if d.get("entity") else None,
"client": d.get("entity", {}).get("name") if d.get("entity") else None,
"total": get_total_from_doc(d),
"subject": d.get("subject"),
"description": d.get("visible_subject"),
"items": items,
"payments": payments,
"ei_status": d.get("ei_status"),
"original_document": d.get("original_document")
}
if d.get("rc_center"):
result["revenue_center"] = d["rc_center"]
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "get_pdf_url":
doc_id = arguments["document_id"]
response = issued_api.get_issued_document(
company_id=COMPANY_ID, document_id=doc_id, fieldset="detailed"
)
d = response.data.to_dict()
attachment_url = d.get("attachment_url") or d.get("url") or ""
web_url = f"https://secure.fattureincloud.it/issued-documents-view-{doc_id}"
result = {
"id": doc_id,
"number": d.get("number"),
"type": d.get("type", "invoice"),
"client": d.get("entity", {}).get("name") if d.get("entity") else "",
"attachment_url": attachment_url,
"web_url": web_url,
"note": "attachment_url è il PDF diretto (se disponibile). web_url apre il documento nel browser FIC."
}
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "list_clients":
query = arguments.get("query")
response = clients_api.list_clients(company_id=COMPANY_ID, per_page=100)
clients = []
for c in (response.data or []):
cd = c.to_dict()
client = {"id": cd.get("id"), "name": cd.get("name"),
"vat": cd.get("vat_number"), "tax_code": cd.get("tax_code"),
"email": cd.get("email")}
if query and query.lower() not in (client['name'] or '').lower():
continue
clients.append(client)
return [TextContent(type="text", text=json.dumps(clients, indent=2, ensure_ascii=False))]
elif name == "get_company_info":
response = companies_api.get_company_info(company_id=COMPANY_ID)
d = response.data.to_dict()
info = d.get("info", d)
result = {"name": info.get("name"), "vat": info.get("vat_number"),
"email": info.get("email"), "address": info.get("address_street"),
"city": info.get("address_city"), "province": info.get("address_province")}
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "create_client":
client_data = {
"name": arguments["name"],
"vat_number": arguments.get("vat_number", ""),
"tax_code": arguments.get("tax_code", ""),
"ei_code": arguments.get("ei_code", ""),
"certified_email": arguments.get("certified_email", ""),
"email": arguments.get("email", ""),
"address_street": arguments.get("address_street", ""),
"address_city": arguments.get("address_city", ""),
"address_postal_code": arguments.get("address_postal_code", ""),
"address_province": arguments.get("address_province", ""),
"country": arguments.get("country", "Italia"),
"phone": arguments.get("phone", ""),
}
response = clients_api.create_client(
company_id=COMPANY_ID,
create_client_request={"data": client_data}
)
d = response.data.to_dict()
result = {
"success": True,
"id": d.get("id"),
"name": d.get("name"),
"vat_number": d.get("vat_number"),
"message": f"Cliente '{d.get('name')}' creato con ID {d.get('id')}."
}
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "update_client":
client_id = arguments["client_id"]
orig = get_client_by_id(client_id)
if not orig:
return [TextContent(type="text", text=json.dumps({"success": False, "error": f"Cliente {client_id} non trovato"}, ensure_ascii=False))]
fields = ["name", "vat_number", "tax_code", "ei_code", "certified_email",
"email", "address_street", "address_city", "address_postal_code",
"address_province", "phone"]
client_data = {}
for f in fields:
if f in arguments:
client_data[f] = arguments[f]
elif orig.get(f) is not None:
client_data[f] = orig[f]
response = clients_api.modify_client(
company_id=COMPANY_ID,
client_id=client_id,
modify_client_request={"data": client_data}
)
d = response.data.to_dict()
result = {
"success": True,
"id": d.get("id"),
"name": d.get("name"),
"message": f"Cliente '{d.get('name')}' aggiornato con successo."
}
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "create_invoice":
result, error = build_issued_document(
doc_type="invoice",
client_id=arguments["client_id"],
items_data=arguments["items"],
date_str=arguments.get("date", datetime.now().strftime("%Y-%m-%d")),
payment_days=arguments.get("payment_days", 30),
visible_subject=arguments.get("visible_subject", ""),
revenue_center=arguments.get("revenue_center"),
)
if error:
return [TextContent(type="text", text=json.dumps({"success": False, "error": error}, ensure_ascii=False))]
result["message"] = f"Fattura #{result['number']} creata come bozza. SDI: {result['ei_code']}. Usa send_to_sdi per inviarla."
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "create_credit_note":
result, error = build_issued_document(
doc_type="credit_note",
client_id=arguments["client_id"],
items_data=arguments["items"],
date_str=arguments.get("date", datetime.now().strftime("%Y-%m-%d")),
payment_days=arguments.get("payment_days", 30),
visible_subject=arguments.get("visible_subject", ""),
negate_prices=True,
source_invoice_id=arguments.get("source_invoice_id"),
revenue_center=arguments.get("revenue_center"),
)
if error:
return [TextContent(type="text", text=json.dumps({"success": False, "error": error}, ensure_ascii=False))]
msg = f"NDC #{result['number']} creata come bozza. Totale: {result['total']}."
if result.get("linked_to_invoice"):
msg += f" Collegata a fattura ID {result['linked_to_invoice']}."
msg += " Usa send_to_sdi per inviarla."
result["message"] = msg
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "create_proforma":
result, error = build_issued_document(
doc_type="proforma",
client_id=arguments["client_id"],
items_data=arguments["items"],
date_str=arguments.get("date", datetime.now().strftime("%Y-%m-%d")),
payment_days=arguments.get("payment_days", 30),
visible_subject=arguments.get("visible_subject", ""),
revenue_center=arguments.get("revenue_center"),
)
if error:
return [TextContent(type="text", text=json.dumps({"success": False, "error": error}, ensure_ascii=False))]
result["message"] = f"Proforma #{result['number']} creata come bozza. Non inviabile allo SDI."
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "convert_proforma_to_invoice":
doc_id = arguments["document_id"]
keep_proforma = arguments.get("keep_proforma", False)
orig_resp = issued_api.get_issued_document(
company_id=COMPANY_ID, document_id=doc_id, fieldset="detailed"
)
orig = orig_resp.data.to_dict()
if orig.get("type") != "proforma":
return [TextContent(type="text", text=json.dumps({
"success": False,
"error": f"Il documento {doc_id} non è una proforma (tipo: {orig.get('type')})"
}, ensure_ascii=False))]
client_id = orig.get("entity", {}).get("id")
client_data = get_client_by_id(client_id) if client_id else None
entity = build_entity_from_client(client_id, client_data) if (client_id and client_data) else orig.get("entity", {})
date_str = arguments.get("date") or str(orig.get("date", datetime.now().strftime("%Y-%m-%d")))
items_list = []
for i in orig.get("items_list", []):
items_list.append({
"name": i.get("name", ""),
"description": i.get("description", ""),
"qty": i.get("qty"),
"net_price": abs(i.get("net_price", 0)),
"vat": {"id": 0, "value": i.get("vat", {}).get("value", 22)}
})
orig_payments = orig.get("payments_list", [{}])
payment_days = orig_payments[0].get("payment_terms", {}).get("days", 30) if orig_payments else 30
invoice_date = datetime.strptime(date_str[:10], "%Y-%m-%d")
due_date = invoice_date + timedelta(days=payment_days)
total_gross = sum(i["qty"] * i["net_price"] * (1 + i["vat"]["value"] / 100) for i in items_list)
revenue_center = arguments.get("revenue_center") or orig.get("rc_center")
if revenue_center:
known = fetch_revenue_centers(company_id=COMPANY_ID)
if revenue_center not in known:
return [TextContent(type="text", text=json.dumps({
"success": False,
"error": f"revenue_center '{revenue_center}' non esiste. Disponibili: {known}."
}, ensure_ascii=False))]
body_data = {
"type": "invoice",
"e_invoice": True,
"ei_data": {"payment_method": "MP05"},
"entity": entity,
"date": date_str[:10],
"visible_subject": orig.get("visible_subject", ""),
"items_list": items_list,
"payments_list": [{
"amount": round(total_gross, 2),
"due_date": due_date.strftime("%Y-%m-%d"),
"status": "not_paid",
"payment_terms": {"days": payment_days, "type": "standard"}
}]
}
if revenue_center:
body_data["rc_center"] = revenue_center
body = {"data": body_data}
response = issued_api.create_issued_document(
company_id=COMPANY_ID, create_issued_document_request=body
)
d = response.data.to_dict()
if not keep_proforma:
issued_api.delete_issued_document(company_id=COMPANY_ID, document_id=doc_id)
result = {
"success": True,
"invoice_id": d.get("id"),
"invoice_number": d.get("number"),
"date": date_str[:10],
"due_date": due_date.strftime("%Y-%m-%d"),
"client": (client_data or {}).get("name", entity.get("name", "")),
"ei_code": entity.get("ei_code", "N/A"),
"total": round(total_gross, 2),
"proforma_deleted": not keep_proforma,
"message": f"Fattura #{d.get('number')} creata da proforma #{orig.get('number')}. {'Proforma eliminata.' if not keep_proforma else 'Proforma mantenuta.'} Usa send_to_sdi per inviarla."
}
if revenue_center:
result["revenue_center"] = revenue_center
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
elif name == "update_document":
doc_id = arguments["document_id"]
orig_resp = issued_api.get_issued_document(
company_id=COMPANY_ID, document_id=doc_id, fieldset="detailed"
)
orig = orig_resp.data.to_dict()
current_status = orig.get("ei_status")
if current_status and current_status not in [None, "not_sent"]:
return [TextContent(type="text", text=json.dumps({
"success": False,
"error": f"Impossibile modificare: documento già inviato allo SDI. Stato: {current_status}"
}, ensure_ascii=False))]
doc_type = orig.get("type", "invoice")
is_credit_note = (doc_type == "credit_note")
date_str = arguments.get("date") or str(orig.get("date", datetime.now().strftime("%Y-%m-%d")))
visible_subject = arguments.get("visible_subject") if "visible_subject" in arguments else (orig.get("visible_subject") or "")
if "items" in arguments:
items_list = build_items_list(arguments["items"], negate=False)
else:
items_list = []
for i in orig.get("items_list", []):
items_list.append({
"name": i.get("name", ""),
"description": i.get("description", ""),
"qty": i.get("qty"),
"net_price": abs(i.get("net_price", 0)),
"vat": {"id": 0, "value": i.get("vat", {}).get("value", 22)}
})
if "payment_days" in arguments:
payment_days = arguments["payment_days"]
else:
orig_payments = orig.get("payments_list", [{}])
payment_days = orig_payments[0].get("payment_terms", {}).get("days", 30) if orig_payments else 30
invoice_date = datetime.strptime(date_str[:10], "%Y-%m-%d")
due_date = invoice_date + timedelta(days=payment_days)
total_abs = sum(
abs(i["qty"] * i["net_price"]) * (1 + i["vat"]["value"] / 100)
for i in items_list
)
result_total = -total_abs if is_credit_note else total_abs
client_id = orig.get("entity", {}).get("id")
client_data = get_client_by_id(client_id) if client_id else None
entity = build_entity_from_client(client_id, client_data) if (client_id and client_data) else orig.get("entity", {})
if "revenue_center" in arguments:
revenue_center = arguments["revenue_center"]
else:
revenue_center = orig.get("rc_center")
if revenue_center:
known = fetch_revenue_centers(company_id=COMPANY_ID)
if revenue_center not in known:
return [TextContent(type="text", text=json.dumps({
"success": False,
"error": f"revenue_center '{revenue_center}' non esiste. Disponibili: {known}."
}, ensure_ascii=False))]
body_data = {
"type": doc_type,
"entity": entity,
"date": date_str[:10],
"visible_subject": visible_subject,
"items_list": items_list,
"payments_list": [{
"amount": round(total_abs, 2),
"due_date": due_date.strftime("%Y-%m-%d"),
"status": "not_paid",
"payment_terms": {"days": payment_days, "type": "standard"}