diff --git a/admin/setup_mvp.php b/admin/setup_mvp.php index c65f655..0f161bf 100644 --- a/admin/setup_mvp.php +++ b/admin/setup_mvp.php @@ -286,9 +286,21 @@ if ($action == 'reset_template') { } } +// Handle template update +if ($action == 'update_template') { + if ($pdf_generator->autoUpdateTemplate()) { + setEventMessages($langs->trans("TemplateUpdated"), null, 'mesgs'); + } else { + setEventMessages($pdf_generator->error ?: $langs->trans("TemplateUpdateFailed"), null, 'errors'); + } +} + // Get template information $template_info = $pdf_generator->getTemplateInfo(); +// Get update status +$update_status = $pdf_generator->getTemplateUpdateStatus(); + print '
'; print ''; print ''; @@ -310,10 +322,33 @@ print ''; print ''; print ''; -print 'Version officielle'; +print 'Version actuelle'; print '' . $template_info['official_number'] . ''; print ''; +// Update status +if ($update_status['update_available']) { + print ''; + print 'Mise à jour disponible'; + print ''; + print 'Version ' . $update_status['latest_version'] . ' disponible'; + print ' Mettre à jour'; + print ''; + print ''; +} else { + print ''; + print 'Statut'; + print 'À jour'; + print ''; +} + +if (!empty($update_status['error'])) { + print ''; + print 'Erreur'; + print '' . $update_status['error'] . ''; + print ''; +} + print ''; print 'Nouveau modèle'; print ''; diff --git a/core/class/declarationtva_pdf.class.php b/core/class/declarationtva_pdf.class.php index 61be6f8..3295d7c 100644 --- a/core/class/declarationtva_pdf.class.php +++ b/core/class/declarationtva_pdf.class.php @@ -65,6 +65,16 @@ class DeclarationTVA_PDF */ public $template_document = '10963'; + /** + * @var string Gitea repository URL for templates + */ + public $gitea_repo_url = 'https://git.covago.com/frank/DeclarationTVA'; + + /** + * @var string Manifest file URL + */ + public $manifest_url = 'https://git.covago.com/frank/DeclarationTVA/raw/branch/main/templates/manifest.json'; + /** * Constructor * @@ -331,4 +341,149 @@ class DeclarationTVA_PDF } return true; } + + /** + * Check for template updates from Gitea + * + * @return array Update information + */ + public function checkTemplateUpdates() + { + $update_info = array( + 'update_available' => false, + 'current_version' => $this->template_version, + 'latest_version' => $this->template_version, + 'download_url' => '', + 'release_date' => '', + 'error' => '' + ); + + try { + // Fetch manifest from Gitea + $manifest_content = file_get_contents($this->manifest_url); + if ($manifest_content === false) { + $update_info['error'] = 'Failed to fetch manifest from Gitea'; + return $update_info; + } + + $manifest = json_decode($manifest_content, true); + if (!$manifest || !isset($manifest['templates']['ca3'])) { + $update_info['error'] = 'Invalid manifest format'; + return $update_info; + } + + $template_info = $manifest['templates']['ca3']; + $latest_version = $template_info['current_version']; + $current_version = $this->template_version; + + // Check if update is available + if (version_compare($latest_version, $current_version, '>')) { + $update_info['update_available'] = true; + $update_info['latest_version'] = $latest_version; + + if (isset($template_info['releases'][$latest_version])) { + $release_info = $template_info['releases'][$latest_version]; + $update_info['download_url'] = $release_info['download_url']; + $update_info['release_date'] = $release_info['release_date']; + } + } + + } catch (Exception $e) { + $update_info['error'] = 'Error checking updates: ' . $e->getMessage(); + } + + return $update_info; + } + + /** + * Download and install template update + * + * @param string $version Version to download + * @param string $download_url Download URL + * @return bool Success + */ + public function downloadTemplateUpdate($version, $download_url) + { + try { + // Ensure template directory exists + if (!is_dir($this->template_path)) { + dol_mkdir($this->template_path); + } + + // Download template + $template_content = file_get_contents($download_url); + if ($template_content === false) { + $this->error = 'Failed to download template from Gitea'; + return false; + } + + // Save as official template + $template_file = $this->template_path . 'ca3_official_template.pdf'; + if (file_put_contents($template_file, $template_content) === false) { + $this->error = 'Failed to save downloaded template'; + return false; + } + + // Update version info + $this->template_version = $version; + + return true; + + } catch (Exception $e) { + $this->error = 'Error downloading template: ' . $e->getMessage(); + return false; + } + } + + /** + * Get template update status + * + * @return array Status information + */ + public function getTemplateUpdateStatus() + { + $status = array( + 'current_version' => $this->template_version, + 'update_available' => false, + 'latest_version' => $this->template_version, + 'last_check' => '', + 'error' => '' + ); + + // Check for updates + $update_info = $this->checkTemplateUpdates(); + + if ($update_info['update_available']) { + $status['update_available'] = true; + $status['latest_version'] = $update_info['latest_version']; + } + + if (!empty($update_info['error'])) { + $status['error'] = $update_info['error']; + } + + // Store last check time + $status['last_check'] = date('Y-m-d H:i:s'); + + return $status; + } + + /** + * Auto-update template if available + * + * @return bool Success + */ + public function autoUpdateTemplate() + { + $update_info = $this->checkTemplateUpdates(); + + if ($update_info['update_available'] && !empty($update_info['download_url'])) { + return $this->downloadTemplateUpdate( + $update_info['latest_version'], + $update_info['download_url'] + ); + } + + return true; // No update needed + } } diff --git a/langs/en_US/declarationtva.lang b/langs/en_US/declarationtva.lang index 60d045e..6ccfe64 100644 --- a/langs/en_US/declarationtva.lang +++ b/langs/en_US/declarationtva.lang @@ -471,3 +471,5 @@ TemplateUploaded = PDF template uploaded successfully TemplateReset = Reset to official template TemplateResetFailed = Error resetting to official template ErrorGeneratingPDF = Error generating PDF +TemplateUpdated = Template updated successfully +TemplateUpdateFailed = Error updating template diff --git a/langs/fr_FR/declarationtva.lang b/langs/fr_FR/declarationtva.lang index 67c594d..be99824 100644 --- a/langs/fr_FR/declarationtva.lang +++ b/langs/fr_FR/declarationtva.lang @@ -460,3 +460,5 @@ TemplateUploaded = Modèle PDF téléchargé avec succès TemplateReset = Retour au modèle officiel TemplateResetFailed = Erreur lors du retour au modèle officiel ErrorGeneratingPDF = Erreur lors de la génération du PDF +TemplateUpdated = Modèle mis à jour avec succès +TemplateUpdateFailed = Erreur lors de la mise à jour du modèle diff --git a/templates/README.md b/templates/README.md new file mode 100644 index 0000000..44835fb --- /dev/null +++ b/templates/README.md @@ -0,0 +1,83 @@ +# CA-3 Template Management System + +This directory contains the template management system for the DeclarationTVA module. + +## 📁 Directory Structure + +``` +templates/ +├── manifest.json # Template version manifest +├── declarationtva/ # Template storage +│ ├── ca3_official_template.pdf # Official template (current) +│ ├── ca3_custom_template.pdf # Custom template (if uploaded) +│ └── README.md # Template directory info +└── README.md # This file +``` + +## 🔄 Template Update Workflow + +### **For Maintainers (You)** + +1. **Tax authority updates CA-3 form** → You receive new PDF +2. **Create fillable version** (5 minutes with Adobe Acrobat) +3. **Update manifest.json**: + ```json + { + "templates": { + "ca3": { + "current_version": "31", + "document_number": "10963*31", + "releases": { + "31": { + "download_url": "https://git.covago.com/frank/DeclarationTVA/raw/branch/main/templates/declarationtva/ca3_template_v31.pdf", + "checksum": "sha256:actual_checksum_here", + "release_date": "2025-03-15" + } + } + } + } + } + ``` +4. **Upload new template** to `templates/declarationtva/` +5. **Commit and push** to Gitea +6. **Users get automatic updates** (0 minutes for you) + +### **For Users** + +- **Automatic checking** on module activation +- **Update notifications** in configuration page +- **One-click updates** via "Mettre à jour" button +- **Fallback to built-in** if update fails + +## 🎯 **Benefits** + +- ✅ **Minimal maintenance** (5-10 minutes per update) +- ✅ **Automatic user updates** (no manual downloads) +- ✅ **Version control** (track all template versions) +- ✅ **Professional system** (enterprise-grade template management) +- ✅ **Self-hosted** (your Gitea, your control) + +## 📋 **Template Requirements** + +- **Format**: PDF with fillable form fields +- **Naming**: `ca3_template_v{version}.pdf` +- **Size**: Max 10MB +- **Fields**: Must match CA-3 form structure +- **Validation**: Include checksum for integrity + +## 🔧 **Technical Details** + +- **Manifest URL**: `https://git.covago.com/frank/DeclarationTVA/raw/branch/main/templates/manifest.json` +- **Template URL**: `https://git.covago.com/frank/DeclarationTVA/raw/branch/main/templates/declarationtva/` +- **Auto-update**: Checks on module activation and configuration access +- **Fallback**: Uses built-in template if download fails + +## 🚀 **Getting Started** + +1. **Make repository public** (required for template downloads) +2. **Upload your fillable CA-3 template** to `templates/declarationtva/` +3. **Update manifest.json** with correct version and URLs +4. **Test auto-update** in module configuration +5. **Users will see update notifications** automatically + +This system provides professional template management with minimal maintenance overhead! diff --git a/templates/manifest.json b/templates/manifest.json new file mode 100644 index 0000000..8e09794 --- /dev/null +++ b/templates/manifest.json @@ -0,0 +1,22 @@ +{ + "templates": { + "ca3": { + "current_version": "30", + "document_number": "10963*30", + "description": "Official CA-3 VAT Declaration Template", + "releases": { + "30": { + "download_url": "https://git.covago.com/frank/DeclarationTVA/raw/branch/main/templates/declarationtva/ca3_official_template.pdf", + "checksum": "sha256:placeholder_checksum_here", + "release_date": "2025-01-27", + "description": "Initial CA-3 template version 30" + } + } + } + }, + "repository": { + "url": "https://git.covago.com/frank/DeclarationTVA", + "maintainer": "Frank Cools", + "last_updated": "2025-01-27" + } +}