|
21 | 21 | use Carbon\Carbon; |
22 | 22 | use Models\Locale; |
23 | 23 | use Models\Module; |
| 24 | +use Modules\Anagrafiche\Anagrafica; |
24 | 25 | use Modules\PrimaNota\Mastrino; |
25 | 26 | use Modules\PrimaNota\Movimento; |
26 | 27 |
|
@@ -773,3 +774,218 @@ function creaMovimentoLiquidazioneIva($date_start, $date_end) |
773 | 774 | return null; |
774 | 775 | } |
775 | 776 | } |
| 777 | + |
| 778 | +/** |
| 779 | + * Genera il file XML per la LIPE (Liquidazione IVA Periodica). |
| 780 | + * |
| 781 | + * @param string $date_start Data inizio periodo |
| 782 | + * @param string $date_end Data fine periodo |
| 783 | + * |
| 784 | + * @return string Contenuto del file XML |
| 785 | + */ |
| 786 | +function generaXmlLipe($date_start, $date_end) |
| 787 | +{ |
| 788 | + // Recupera i dati dell'anagrafica azienda |
| 789 | + $azienda = Anagrafica::find(setting('Azienda predefinita')); |
| 790 | + |
| 791 | + if (empty($azienda)) { |
| 792 | + throw new Exception('Anagrafica azienda non configurata'); |
| 793 | + } |
| 794 | + |
| 795 | + // Determina i mesi da includere |
| 796 | + $data_start = new Carbon($date_start); |
| 797 | + $data_end = new Carbon($date_end); |
| 798 | + $anno = $data_start->format('Y'); |
| 799 | + |
| 800 | + // Calcola i mesi del periodo |
| 801 | + $mesi = []; |
| 802 | + $current = clone $data_start; |
| 803 | + while ($current <= $data_end) { |
| 804 | + $mesi[] = (int) $current->format('m'); |
| 805 | + $current->addMonth(); |
| 806 | + } |
| 807 | + |
| 808 | + // Recupera dati intermediario (da impostazioni o fallback ai dati azienda) |
| 809 | + $cf_dichiarante = setting('Codice fiscale dichiarante') ?: $azienda->codice_fiscale; |
| 810 | + $cf_intermediario = setting('Codice fiscale intermediario'); |
| 811 | + $identificativo_software = setting('Identificativo software'); |
| 812 | + $piva = $azienda->piva ?: ''; |
| 813 | + |
| 814 | + // Crea il documento DOM |
| 815 | + $dom = new \DOMDocument('1.0', 'UTF-8'); |
| 816 | + $dom->preserveWhiteSpace = false; |
| 817 | + $dom->formatOutput = true; |
| 818 | + |
| 819 | + // Crea elemento radice Fornitura con namespace |
| 820 | + $fornitura = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:Fornitura'); |
| 821 | + $fornitura->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ds', 'http://www.w3.org/2000/09/xmldsig#'); |
| 822 | + $dom->appendChild($fornitura); |
| 823 | + |
| 824 | + // Crea Intestazione |
| 825 | + $intestazione = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:Intestazione'); |
| 826 | + $fornitura->appendChild($intestazione); |
| 827 | + |
| 828 | + // CodiceFornitura |
| 829 | + $codiceFornitura = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:CodiceFornitura'); |
| 830 | + $codiceFornitura->appendChild($dom->createTextNode('IVP18')); |
| 831 | + $intestazione->appendChild($codiceFornitura); |
| 832 | + |
| 833 | + // CodiceFiscaleDichiarante |
| 834 | + $codiceFiscaleDichiarante = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:CodiceFiscaleDichiarante'); |
| 835 | + $codiceFiscaleDichiarante->appendChild($dom->createTextNode($cf_dichiarante ?: '')); |
| 836 | + $intestazione->appendChild($codiceFiscaleDichiarante); |
| 837 | + |
| 838 | + // CodiceCarica |
| 839 | + $codiceCarica = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:CodiceCarica'); |
| 840 | + $codiceCarica->appendChild($dom->createTextNode('1')); |
| 841 | + $intestazione->appendChild($codiceCarica); |
| 842 | + |
| 843 | + // Crea Comunicazione |
| 844 | + $comunicazione = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:Comunicazione'); |
| 845 | + $comunicazione->setAttribute('identificativo', '00001'); |
| 846 | + $fornitura->appendChild($comunicazione); |
| 847 | + |
| 848 | + // Crea Frontespizio |
| 849 | + $frontespizio = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:Frontespizio'); |
| 850 | + $comunicazione->appendChild($frontespizio); |
| 851 | + |
| 852 | + // CodiceFiscale (Partita IVA) |
| 853 | + $codiceFiscale = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:CodiceFiscale'); |
| 854 | + $codiceFiscale->appendChild($dom->createTextNode($piva ?: '')); |
| 855 | + $frontespizio->appendChild($codiceFiscale); |
| 856 | + |
| 857 | + // AnnoImposta |
| 858 | + $annoImposta = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:AnnoImposta'); |
| 859 | + $annoImposta->appendChild($dom->createTextNode($anno)); |
| 860 | + $frontespizio->appendChild($annoImposta); |
| 861 | + |
| 862 | + // PartitaIVA |
| 863 | + $partitaIVA = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:PartitaIVA'); |
| 864 | + $partitaIVA->appendChild($dom->createTextNode($piva ?: '')); |
| 865 | + $frontespizio->appendChild($partitaIVA); |
| 866 | + |
| 867 | + // CFDichiarante |
| 868 | + $cfDichiarante = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:CFDichiarante'); |
| 869 | + $cfDichiarante->appendChild($dom->createTextNode($cf_dichiarante ?: '')); |
| 870 | + $frontespizio->appendChild($cfDichiarante); |
| 871 | + |
| 872 | + // CodiceCaricaDichiarante |
| 873 | + $codiceCaricaDichiarante = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:CodiceCaricaDichiarante'); |
| 874 | + $codiceCaricaDichiarante->appendChild($dom->createTextNode('1')); |
| 875 | + $frontespizio->appendChild($codiceCaricaDichiarante); |
| 876 | + |
| 877 | + // FirmaDichiarazione |
| 878 | + $firmaDichiarazione = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:FirmaDichiarazione'); |
| 879 | + $firmaDichiarazione->appendChild($dom->createTextNode('1')); |
| 880 | + $frontespizio->appendChild($firmaDichiarazione); |
| 881 | + |
| 882 | + // CFIntermediario |
| 883 | + if ($cf_intermediario) { |
| 884 | + $cfIntermediario = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:CFIntermediario'); |
| 885 | + $cfIntermediario->appendChild($dom->createTextNode($cf_intermediario ?: '')); |
| 886 | + $frontespizio->appendChild($cfIntermediario); |
| 887 | + |
| 888 | + // ImpegnoPresentazione |
| 889 | + $impegnoPresentazione = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:ImpegnoPresentazione'); |
| 890 | + $impegnoPresentazione->appendChild($dom->createTextNode('1')); |
| 891 | + $frontespizio->appendChild($impegnoPresentazione); |
| 892 | + |
| 893 | + // DataImpegno |
| 894 | + $dataImpegno = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:DataImpegno'); |
| 895 | + $dataImpegno->appendChild($dom->createTextNode(date('dmY'))); |
| 896 | + $frontespizio->appendChild($dataImpegno); |
| 897 | + |
| 898 | + // FirmaIntermediario |
| 899 | + $firmaIntermediario = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:FirmaIntermediario'); |
| 900 | + $firmaIntermediario->appendChild($dom->createTextNode('1')); |
| 901 | + $frontespizio->appendChild($firmaIntermediario); |
| 902 | + } |
| 903 | + |
| 904 | + // IdentificativoProdSoftware |
| 905 | + if ($identificativo_software) { |
| 906 | + $identificativoProdSoftware = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:IdentificativoProdSoftware'); |
| 907 | + $identificativoProdSoftware->appendChild($dom->createTextNode($identificativo_software ?: '')); |
| 908 | + $frontespizio->appendChild($identificativoProdSoftware); |
| 909 | + } |
| 910 | + |
| 911 | + // Crea DatiContabili |
| 912 | + $datiContabili = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:DatiContabili'); |
| 913 | + $comunicazione->appendChild($datiContabili); |
| 914 | + |
| 915 | + // Genera i moduli per ogni mese |
| 916 | + $numero_modulo = 1; |
| 917 | + |
| 918 | + foreach ($mesi as $mese) { |
| 919 | + // Calcola date per il mese corrente |
| 920 | + $mese_start = $anno.'-'.str_pad($mese, 2, '0', STR_PAD_LEFT).'-01'; |
| 921 | + $mese_end = (new Carbon($mese_start))->endOfMonth()->format('Y-m-d'); |
| 922 | + |
| 923 | + // Calcola importi per il mese specifico |
| 924 | + $importi_mese = calcolaImportiLiquidazioneIva($mese_start, $mese_end); |
| 925 | + |
| 926 | + // Calcola totali operazioni per il mese |
| 927 | + $totale_attive_mese = 0; |
| 928 | + $totale_passive_mese = 0; |
| 929 | + |
| 930 | + foreach ($importi_mese['iva_vendite'] as $iva) { |
| 931 | + $totale_attive_mese += floatval($iva['subtotale']); |
| 932 | + } |
| 933 | + |
| 934 | + foreach ($importi_mese['iva_acquisti'] as $iva) { |
| 935 | + $totale_passive_mese += floatval($iva['subtotale']); |
| 936 | + } |
| 937 | + |
| 938 | + $iva_esigibile_mese = floatval($importi_mese['totale_iva_esigibile']); |
| 939 | + $iva_detraibile_mese = floatval($importi_mese['totale_iva_detraibile']); |
| 940 | + $iva_dovuta_mese = $iva_esigibile_mese - $iva_detraibile_mese; |
| 941 | + $importo_da_versare_mese = max(0, $iva_dovuta_mese); |
| 942 | + |
| 943 | + // Crea elemento Modulo |
| 944 | + $modulo = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:Modulo'); |
| 945 | + $datiContabili->appendChild($modulo); |
| 946 | + |
| 947 | + // NumeroModulo |
| 948 | + $numeroModuloEl = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:NumeroModulo'); |
| 949 | + $numeroModuloEl->appendChild($dom->createTextNode((string) $numero_modulo)); |
| 950 | + $modulo->appendChild($numeroModuloEl); |
| 951 | + |
| 952 | + // Mese |
| 953 | + $meseEl = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:Mese'); |
| 954 | + $meseEl->appendChild($dom->createTextNode((string) $mese)); |
| 955 | + $modulo->appendChild($meseEl); |
| 956 | + |
| 957 | + // TotaleOperazioniAttive |
| 958 | + $totaleAttive = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:TotaleOperazioniAttive'); |
| 959 | + $totaleAttive->appendChild($dom->createTextNode(number_format($totale_attive_mese, 2, ',', ''))); |
| 960 | + $modulo->appendChild($totaleAttive); |
| 961 | + |
| 962 | + // TotaleOperazioniPassive |
| 963 | + $totalePassive = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:TotaleOperazioniPassive'); |
| 964 | + $totalePassive->appendChild($dom->createTextNode(number_format($totale_passive_mese, 2, ',', ''))); |
| 965 | + $modulo->appendChild($totalePassive); |
| 966 | + |
| 967 | + // IvaEsigibile |
| 968 | + $ivaEsigibile = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:IvaEsigibile'); |
| 969 | + $ivaEsigibile->appendChild($dom->createTextNode(number_format($iva_esigibile_mese, 2, ',', ''))); |
| 970 | + $modulo->appendChild($ivaEsigibile); |
| 971 | + |
| 972 | + // IvaDetratta |
| 973 | + $ivaDetratta = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:IvaDetratta'); |
| 974 | + $ivaDetratta->appendChild($dom->createTextNode(number_format($iva_detraibile_mese, 2, ',', ''))); |
| 975 | + $modulo->appendChild($ivaDetratta); |
| 976 | + |
| 977 | + // IvaDovuta |
| 978 | + $ivaDovuta = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:IvaDovuta'); |
| 979 | + $ivaDovuta->appendChild($dom->createTextNode(number_format($iva_dovuta_mese, 2, ',', ''))); |
| 980 | + $modulo->appendChild($ivaDovuta); |
| 981 | + |
| 982 | + // ImportoDaVersare |
| 983 | + $importoDaVersare = $dom->createElementNS('urn:www.agenziaentrate.gov.it:specificheTecniche:sco:ivp', 'iv:ImportoDaVersare'); |
| 984 | + $importoDaVersare->appendChild($dom->createTextNode(number_format($importo_da_versare_mese, 2, ',', ''))); |
| 985 | + $modulo->appendChild($importoDaVersare); |
| 986 | + |
| 987 | + $numero_modulo++; |
| 988 | + } |
| 989 | + |
| 990 | + return $dom->saveXML(); |
| 991 | +} |
0 commit comments