PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/2.071.0/profil_collab/catalogue_articles_service_abo_recherche_export_csv.php

https://github.com/crepeausucre/soothERP
PHP | 347 lines | 295 code | 30 blank | 22 comment | 31 complexity | d7b74b77b4d4698f2185e063f0d37303 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0
  1. <?php
  2. // *************************************************************************************************************
  3. // RECHERCHE DES ABONNEMENTS D'UN ARTICLE
  4. // *************************************************************************************************************
  5. require ("_dir.inc.php");
  6. require ("_profil.inc.php");
  7. require ($DIR."_session.inc.php");
  8. if (!isset($_REQUEST['ref_article'])) {
  9. echo "La référence de l'article n'est pas précisée";
  10. exit;
  11. }
  12. $article = new article ($_REQUEST['ref_article']);
  13. if (!$article->getRef_article()) {
  14. echo "La référence de l'article est inconnue";
  15. exit;
  16. }
  17. // *************************************************************************************************************
  18. // TRAITEMENTS
  19. // *************************************************************************************************************
  20. $ANNUAIRE_CATEGORIES = get_categories();
  21. // *************************************************
  22. // Profils ŕ afficher
  23. $profils = array();
  24. foreach ($_SESSION['profils'] as $profil) {
  25. if ($profil->getActif() == 0) { continue; }
  26. $profils[] = $profil;
  27. }
  28. unset ($profil);
  29. // *************************************************
  30. // Données pour le formulaire et la recherche
  31. $form['ref_client'] = "";
  32. if (isset($_REQUEST['ref_client'])){
  33. if ($_REQUEST['ref_client']!="") {
  34. $form['ref_client'] = trim(urldecode($_REQUEST['ref_client']));
  35. $search['ref_client'] = trim(urldecode($_REQUEST['ref_client']));
  36. }else{
  37. $form['ref_client'] = "";
  38. $search['ref_client'] = "";
  39. }
  40. }
  41. $form['etat_abo'] = "";
  42. if (isset($_REQUEST['etat_abo'])) {
  43. $form['etat_abo'] = trim(urldecode($_REQUEST['etat_abo']));
  44. $search['etat_abo'] = trim(urldecode($_REQUEST['etat_abo']));
  45. }
  46. $form['id_client_categ'] = "";
  47. if (isset($_REQUEST['id_client_categ']) && $_REQUEST['id_client_categ']!=0) {
  48. $form['id_client_categ'] = trim(urldecode($_REQUEST['id_client_categ']));
  49. $search['id_client_categ'] = trim(urldecode($_REQUEST['id_client_categ']));
  50. }else{
  51. $form['id_client_categ'] = "";
  52. $search['id_client_categ'] = "";
  53. }
  54. $form['date_souscription_min'] = "";
  55. if (isset($_REQUEST['date_souscription_min'])) {
  56. $form['date_souscription_min'] = trim(urldecode($_REQUEST['date_souscription_min']));
  57. $search['date_souscription_min'] = trim(urldecode($_REQUEST['date_souscription_min']));
  58. }
  59. $form['date_souscription_max'] = "";
  60. if (isset($_REQUEST['date_souscription_max'])) {
  61. $form['date_souscription_max'] = trim(urldecode($_REQUEST['date_souscription_max']));
  62. $search['date_souscription_max'] = trim(urldecode($_REQUEST['date_souscription_max']));
  63. }
  64. $form['date_echeance_min'] = "";
  65. if (isset($_REQUEST['date_echeance_min'])) {
  66. $form['date_echeance_min'] = trim(urldecode($_REQUEST['date_echeance_min']));
  67. $search['date_echeance_min'] = trim(urldecode($_REQUEST['date_echeance_min']));
  68. }
  69. $form['date_echeance_max'] = "";
  70. if (isset($_REQUEST['date_echeance_max'])) {
  71. $form['date_echeance_max'] = trim(urldecode($_REQUEST['date_echeance_max']));
  72. $search['date_echeance_max'] = trim(urldecode($_REQUEST['date_echeance_max']));
  73. }
  74. $form['date_fin_min'] = "";
  75. if (isset($_REQUEST['date_fin_min'])) {
  76. $form['date_fin_min'] = trim(urldecode($_REQUEST['date_fin_min']));
  77. $search['date_fin_min'] = trim(urldecode($_REQUEST['date_fin_min']));
  78. }
  79. $form['date_fin_max'] = "";
  80. if (isset($_REQUEST['date_fin_max'])) {
  81. $form['date_fin_max'] = trim(urldecode($_REQUEST['date_fin_max']));
  82. $search['date_fin_max'] = trim(urldecode($_REQUEST['date_fin_max']));
  83. }
  84. $form['adresse_code'] = "";
  85. if (isset($_REQUEST['adresse_code'])) {
  86. $form['adresse_code'] = trim(urldecode($_REQUEST['adresse_code']));
  87. $search['adresse_code'] = trim(urldecode($_REQUEST['adresse_code']));
  88. }
  89. $form['adresse_ville'] = "";
  90. if (isset($_REQUEST['adresse_ville'])) {
  91. $form['adresse_ville'] = trim(urldecode($_REQUEST['adresse_ville']));
  92. $search['adresse_ville'] = trim(urldecode($_REQUEST['adresse_ville']));
  93. }
  94. $form['adresse_pays'] = "";
  95. if (isset($_REQUEST['adresse_pays'])) {
  96. $form['adresse_pays'] = trim(urldecode($_REQUEST['adresse_pays']));
  97. $search['adresse_pays'] = trim(urldecode($_REQUEST['adresse_pays']));
  98. }
  99. $form['ref_article'] = ($_REQUEST['ref_article']);
  100. $search['ref_article'] = ($_REQUEST['ref_article']);
  101. // *************************************************
  102. // Résultat de la recherche
  103. $fiches = array();
  104. // Préparation de la requete
  105. $query_join = "";
  106. $query_where = "";
  107. //ref_article
  108. if (isset($search['ref_article'])) {
  109. if ($query_where) { $query_where .= " && "; }
  110. $query_where .= " aa.ref_article = '".($search['ref_article'])."'";
  111. }
  112. //catégorie de clients
  113. if ($search['id_client_categ']) {
  114. if ($query_where) { $query_where .= " && "; }
  115. $query_join .= " LEFT JOIN annu_client ac ON a.ref_contact = ac.ref_contact ";
  116. $query_where .= "ac.id_client_categ = '".$search['id_client_categ']."'";
  117. }
  118. //adresse 1
  119. if ($search['adresse_code']) {
  120. if ($query_where) { $query_where .= " && "; }
  121. $query_where .= "ad.code_postal LIKE '".$search['adresse_code']."%'";
  122. }
  123. //adresse 2
  124. if ($search['adresse_ville']) {
  125. if ($query_where) { $query_where .= " && "; }
  126. $query_where .= "ad.ville = '".$search['adresse_ville']."'";
  127. }
  128. //adresse 3
  129. if ($search['adresse_ville'] || $search['adresse_code']) {
  130. $query_join_count .= " LEFT JOIN adresses ad ON a.ref_contact = ad.ref_contact ";
  131. }
  132. //adresse 4
  133. if ($search['adresse_pays']) {
  134. $query_join .= " LEFT JOIN pays p ON ad.id_pays = p.id_pays ";
  135. if ($query_where) { $query_where .= " && "; }
  136. $query_where .= "p.pays = '".$search['adresse_pays']."'";
  137. }
  138. // etat abonnement :
  139. // 0 : TOUS
  140. // 1 : Abonnements en cours
  141. // 2 : Abonnements échus, ŕ renouveller
  142. // 3 : Abonnements terminés
  143. if ($search['etat_abo']) {
  144. if ($query_where) { $query_where .= " && "; }
  145. // 1 : Abonnements en cours
  146. if ($search['etat_abo'] == 1) { $query_where .= " aa.date_echeance > NOW() ";}
  147. // 2 : Abonnements échus, ŕ renouveller
  148. if ($search['etat_abo'] == 2) { $query_where .= " (aa.fin_abonnement > NOW() || aa.fin_abonnement = '0000-00-00 00:00:00') && aa.date_echeance < NOW() ";}
  149. // 3 : Abonnements terminés
  150. if ($search['etat_abo'] == 3) { $query_where .= " aa.fin_abonnement < NOW() && aa.fin_abonnement != '0000-00-00 00:00:00'";}
  151. }
  152. //date de souscription
  153. if($search['date_souscription_min']){
  154. $query_where .= " && aa.date_souscription > '".date_Fr_to_Us($search['date_souscription_min'])." 00:00:00'";
  155. }
  156. if($search['date_souscription_max']){
  157. $query_where .= " && aa.date_souscription < '".date_Fr_to_Us($search['date_souscription_max'])." 00:00:00'";
  158. }
  159. //date de echeance
  160. if($search['date_echeance_min']){
  161. $query_where .= " && aa.date_echeance > '".date_Fr_to_Us($search['date_echeance_min'])." 00:00:00'";
  162. }
  163. if($search['date_echeance_max']){
  164. $query_where .= " && aa.date_echeance < '".date_Fr_to_Us($search['date_echeance_max'])." 00:00:00'";
  165. }
  166. //date de fin
  167. if($search['date_fin_min']){
  168. $query_where .= " && aa.fin_abonnement > '".date_Fr_to_Us($search['date_fin_min'])." 00:00:00'";
  169. }
  170. if($search['date_fin_max']){
  171. $query_where .= " && aa.fin_abonnement < '".date_Fr_to_Us($search['date_fin_max'])." 00:00:00'";
  172. }
  173. //ref_client
  174. if ($search['ref_client']) {
  175. if ($query_where) { $query_where .= " && "; }
  176. $query_where .= "a.ref_contact = '".addslashes($search['ref_client'])."'";
  177. }
  178. if (!$query_where) {
  179. $query_where = 1;
  180. }
  181. //ad.text_adresse, ad.code_postal, ad.ville, ad.ordre, co.tel1, co.tel2, co.fax, co.ordre, email, url, si.ordre
  182. // Recherche
  183. $query = "SELECT an.ref_contact, c.lib_civ_court, an.nom, an.id_categorie, ar.lib_article,
  184. aa.ref_article, aa.id_abo, aa.date_souscription, aa.date_echeance , aa.date_preavis, aa.fin_engagement, aa.fin_abonnement,
  185. ad.text_adresse, ad.code_postal, ad.ville, ad.note, p.pays, co.tel1, co.tel2, co.fax, co.email, cc.lib_client_categ
  186. FROM articles_abonnes aa
  187. LEFT JOIN articles ar ON ar.ref_article = aa.ref_article
  188. LEFT JOIN annuaire an ON an.ref_contact = aa.ref_contact
  189. LEFT JOIN civilites c ON an.id_civilite = c.id_civilite
  190. LEFT JOIN coordonnees co ON an.ref_contact = co.ref_contact && co.ordre = 1
  191. LEFT JOIN adresses ad ON an.ref_contact = ad.ref_contact
  192. LEFT JOIN pays p ON p.id_pays = ad.id_pays
  193. LEFT JOIN sites_web si ON an.ref_contact = si.ref_contact && si.ordre = 1
  194. LEFT JOIN annu_client ac ON an.ref_contact = ac.ref_contact
  195. LEFT JOIN clients_categories cc ON ac.id_client_categ = cc.id_client_categ
  196. ".$query_join."
  197. WHERE ".$query_where."
  198. GROUP BY aa.id_abo
  199. ORDER BY aa.date_echeance DESC, an.nom ASC";
  200. //echo "<br/><hr/><br/>".nl2br($query)."<br/><hr/><br/>";
  201. $resultat = $bdd->query($query);
  202. header('Pragma: public');
  203. header('Expires: 0');
  204. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  205. header('Content-Type: application/force-download');
  206. header('Content-Type: application/octet-stream');
  207. header('Content-Type: application/download');
  208. header('Content-Type: application/csv; name="listedesabonnes'.urlencode(str_replace (CHR(13), "" ,str_replace (CHR(10), "" ,preg_replace ("#((\r\n)+)#", "", (($article->getLib_article())))))).'.csv"');
  209. header('Content-Disposition: attachment; filename=listedesabonnes'.urlencode(str_replace (CHR(13), "" ,str_replace (CHR(10), "" ,preg_replace ("#((\r\n)+)#", "", (($article->getLib_article())))))).'.csv;');
  210. $ligne = "Référence article;Libellé article;Etat;Date de souscription;Date d'écheance;Date de préavis;Date de fin d'engagement;Date de fin d'abonnement;";
  211. $ligne.= "Référence contact;Nom du client 1;Nom du client 2;Catégorie de client;Adresse 1;Adresse 2;Adresse 3;Code Postal;Ville;Pays;Informations Adresse;tel 1;tel 2;fax;email\n";
  212. echo $ligne;
  213. while ($fiche = $resultat->fetchObject()) {
  214. $ligne = ""; //il est plus rapide d'utiliser une variable "ligne" que de faire des "echo" directements
  215. //Référence article;
  216. $ligne.=$fiche->ref_article;
  217. //Libellé article;
  218. $ligne.=";".preg_replace('/\\r\\n|\\n|\\r|;/i', ',', $fiche->lib_article);
  219. //Etat;
  220. $etat = "";
  221. if ($fiche->date_echeance > date("Y-m-d H:i:s", time())) { $etat.=";en cours ";}
  222. if ($fiche->fin_abonnement > date("Y-m-d H:i:s", time()) && $fiche->date_echeance < date("Y-m-d H:i:s", time())) { $etat.=";ŕ renouveller ";}
  223. if ($fiche->fin_abonnement < date("Y-m-d H:i:s", time()) && $fiche->fin_abonnement != '0000-00-00 00:00:00') { $etat.=";expiré ";}
  224. if ($fiche->date_preavis != '0000-00-00 00:00:00')
  225. { $etat.=";préavis déposé ";}
  226. $ligne.= $etat;
  227. //Date de souscription;
  228. $ligne.=";".date_Us_to_Fr($fiche->date_souscription);
  229. //Date d'écheance;
  230. $ligne.=";".date_Us_to_Fr($fiche->date_echeance);
  231. //Date de préavis;
  232. if ($fiche->date_preavis == '0000-00-00 00:00:00' || $fiche->date_preavis == '0000-00-00')
  233. { $ligne.=";";}
  234. else{$ligne.=";".date_Us_to_Fr($fiche->date_preavis);}
  235. //Date de fin d'engagement;
  236. if ($fiche->fin_engagement == '0000-00-00 00:00:00' || $fiche->fin_engagement == '0000-00-00')
  237. { $ligne.=";";}
  238. else{$ligne.=";".date_Us_to_Fr($fiche->fin_engagement);}
  239. //Date de fin d'abonnement;
  240. if ($fiche->fin_abonnement == '0000-00-00 00:00:00' || $fiche->fin_abonnement == '0000-00-00')
  241. { $ligne.=";";}
  242. else{$ligne.=";".date_Us_to_Fr($fiche->fin_abonnement);}
  243. //Référence contact;
  244. $ligne.=";".$fiche->ref_contact;
  245. $pos = stripos($fiche->nom, "\n");
  246. if($pos == false){$pos = strlen($fiche->nom);}
  247. else{$pos--;}
  248. //Nom du client 1;
  249. if($fiche->lib_civ_court)
  250. { $ligne.=";".preg_replace('/\\r\\n|\\n|\\r|;/i', ',', $fiche->lib_civ_court." ".substr($fiche->nom, 0, $pos));}
  251. else{$ligne.=";".preg_replace('/\\r\\n|\\n|\\r|;/i', ',', substr($fiche->nom, 0, $pos));}
  252. //Nom du client 2;
  253. $ligne.=";".preg_replace('/\\r\\n|\\n|\\r|;/i', ' ', substr($fiche->nom, $pos+2));
  254. //Catégorie de client
  255. $ligne.= ";".preg_replace('/\\r\\n|\\n|\\r|;/i', ' ', $fiche->lib_client_categ);
  256. $ad = explode("\n", $fiche->text_adresse);
  257. $adresse1 = "";
  258. if(count($ad)>0)
  259. $adresse1 = preg_replace('/\\r\\n|\\n|\\r|;/i', ' ', $ad[0]);
  260. $adresse2 = "";
  261. if(count($ad)>1)
  262. $adresse2 = preg_replace('/\\r\\n|\\n|\\r|;/i', ' ', $ad[1]);
  263. $adresse3 = "";
  264. if(count($ad)>2)
  265. $adresse3 = preg_replace('/\\r\\n|\\n|\\r|;/i', ' ', $ad[2]);
  266. //Adresse 1, 2 et 3
  267. $ligne.= ";".$adresse1.";".$adresse2.";".$adresse3;
  268. //Code Postal;
  269. $ligne.= ";".$fiche->code_postal;
  270. //Ville;
  271. $ligne.= ";".$fiche->ville;
  272. //Pays;
  273. $ligne.= ";".$fiche->pays;
  274. //Informations Adresse;
  275. if($fiche->ref_contact != null)
  276. { $ligne.= ";".preg_replace('/\\r\\n|\\n|\\r|;/i', ',', $fiche->note);}
  277. else{$ligne.= ";";}
  278. //tel1, tel2 et fax
  279. $ligne.= ";".$fiche->tel1.";".$fiche->tel2.";".$fiche->fax;
  280. //email
  281. $ligne.= ";".$fiche->email;
  282. echo $ligne."\n";
  283. }
  284. ?>