actions_mmidocuments.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © 2023 Mathieu Moulin iProspetcive <contact@iprospective.fr>
  4. *
  5. * This file is part of MMIDOcuments.
  6. *
  7. * MMIDOcuments is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * MMIDOcuments is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with MMIDOcuments. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. dol_include_once('custom/mmicommon/class/mmi_actions.class.php');
  21. dol_include_once('/mbietransactions/class/mmi_etransactions.class.php');
  22. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  23. require_once DOL_DOCUMENT_ROOT.'/societe/class/client.class.php';
  24. class ActionsMMIDocuments extends MMI_Actions_1_0
  25. {
  26. const MOD_NAME = 'mmidocuments';
  27. function VATNotificationOnPDF($parameters, &$pdf, &$action, $hookmanager)
  28. {
  29. global $langs, $user, $conf;
  30. $error = '';
  31. // VATIsNotUsedForInvoice
  32. if (!empty($conf->global->MMIDOCUMENTS_VAT_NOTIF_PDF_DISPLAY)) {
  33. $mysoc = $parameters['mysoc'];
  34. $emetteur = $parameters['emetteur'];
  35. $object = $parameters['object'];
  36. $client = $object->thirdparty;
  37. $contacts = $object->liste_contact();
  38. foreach($contacts as $contact) {
  39. // Adresse livraison
  40. if (in_array($contact['fk_c_type_contact'], [102, 42, 61])) {
  41. $adresse_liv = new Contact($this->db);
  42. $adresse_liv->fetch($contact['id']);
  43. }
  44. // Adresse facturation
  45. elseif (in_array($contact['fk_c_type_contact'], [100, 40, 60])) {
  46. $adresse_fac = new Contact($this->db);
  47. $adresse_fac->fetch($contact['id']);
  48. }
  49. }
  50. if (empty($adresse_liv))
  51. $adresse_liv = $client;
  52. if (empty($adresse_fac))
  53. $adresse_fac = $client;
  54. //var_dump($adresse_fac->country_code); die();
  55. //var_dump($client->tva_intra && in_array($adresse_fac->country_code, $countries_eu)); die();
  56. // Mentions TVA
  57. $countries_eu = explode(',', !empty($conf->global->MAIN_COUNTRIES_IN_EEC) ?$conf->global->MAIN_COUNTRIES_IN_EEC :'AT,BE,BG,CY,CZ,DE,DK,EE,ES,FI,FR,GB,GR,HR,NL,HU,IE,IM,IT,LT,LU,LV,MC,MT,PL,PT,RO,SE,SK,SI,UK');
  58. // Pas de TVA
  59. //var_dump($client); die();
  60. //var_dump($object); die();
  61. //var_dump($mysoc); die();
  62. //var_dump($object->total_tva==0); die();
  63. // Emétteur en France uniquement !
  64. if ($emetteur->country_code == 'FR') {
  65. // Emetteur non assujetti
  66. if (empty($mysoc->tva_assuj)) {
  67. // Asso
  68. if ($mysoc->forme_juridique_code == 92)
  69. $vat_info = $langs->transnoentities("VATIsNotUsedForInvoiceAsso");
  70. // Société (AE, etc.)
  71. else
  72. $vat_info = $langs->transnoentities("VATIsNotUsedForInvoice");
  73. }
  74. // TVA
  75. elseif (!($object->total_tva == 0)) {
  76. $vat_info = '';
  77. }
  78. // Exonération de de TVA
  79. else {
  80. // Transitaire
  81. if (!empty($object->array_options['options_transitaire'])) {
  82. $vat_info = $langs->transnoentities("VATIsNotUsedForTransitaire");
  83. }
  84. // DOM : Guadeloupe, Guyane, Martinique, Mayotte ou La Réunion
  85. elseif (in_array($adresse_fac->country_code, ['FR', 'GF']) && substr($adresse_liv->zip, 0, 2)=='97') {
  86. $vat_info = $langs->transnoentities("VATIsNotUsedForDOM");
  87. }
  88. // TOM
  89. elseif (in_array($adresse_fac->country_code, ['FR', 'PF']) && substr($adresse_liv->zip, 0, 2)=='98') {
  90. $vat_info = $langs->transnoentities("VATIsNotUsedForTOM");
  91. }
  92. // FR avec code intra et tout qui va bien
  93. elseif ($client->tva_intra && $adresse_fac->country_code == 'FR') {
  94. if (!empty($object->array_options['options_appeloffre']) && !empty($object->array_options['options_appeloffre_soustraitant']))
  95. $vat_info = $langs->transnoentities("VATIsNotUsedForFRAppelOffresSSTraitant");
  96. else
  97. $vat_info = $langs->transnoentities("VATIsNotUsedForFR");
  98. }
  99. // UE avec code intra et tout qui va bien
  100. elseif ($client->tva_intra && in_array($adresse_fac->country_code, $countries_eu)) {
  101. $vat_info = $langs->transnoentities("VATIsNotUsedForEU");
  102. }
  103. elseif ($client->tva_intra) {
  104. $error = 'Exonération de TVA art. 262 ter, I du CGI => TVA Intra MAIS pays à spécifier';
  105. }
  106. // UE PRO sans code intra => a spécifier
  107. elseif (($client->idprof1 || $client->idprof2) && in_array($adresse_fac->country_code, $countries_eu)) {
  108. $error = 'Exonération de TVA art. 262 ter, I du CGI => N°TVA intracom à spécifier';
  109. }
  110. // Îles (Canaries, etc.)
  111. elseif (false) {
  112. $vat_info = $langs->transnoentities("VATIsNotUsedForIslands");
  113. }
  114. // UE sans code intra => particulier => tva du pays => ERREUR PAS TVA
  115. elseif (in_array($adresse_fac->country_code, $countries_eu)) {
  116. $error = 'Exoneration de TVA pour un PARTICULIER en UE !';
  117. }
  118. // Hors UE
  119. elseif ($adresse_fac->country_code && !in_array($adresse_fac->country_code, $countries_eu)) {
  120. $vat_info = $langs->transnoentities("VATIsNotUsedForExport");
  121. }
  122. // PRO Pays non spécifié
  123. elseif ($client->idprof1 || $client->idprof2) {
  124. $vat_info = '';
  125. $error = 'Exoneration de TVA pour un PRO, MAIS le pays du client n\'est pas spécifié !';
  126. }
  127. // Pays non spécifié
  128. else {
  129. $vat_info = '';
  130. $error = 'Exoneration de TVA pour un PARTICULIER, ET le pays du client n\'est pas spécifié';
  131. }
  132. }
  133. }
  134. //var_dump($vat_info); die();
  135. $this->resprints = $vat_info;
  136. $ret = 1;
  137. }
  138. if (!$error) {
  139. return isset($ret) ?$ret :0; // or return 1 to replace standard code
  140. } else {
  141. $this->errors[] = $error;
  142. return -1;
  143. }
  144. }
  145. }