PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/2.071.0/_facture_niveau_relance.lib.php

https://github.com/crepeausucre/soothERP
PHP | 185 lines | 167 code | 18 blank | 0 comment | 50 complexity | 887378042794fde9900cf339947e7ba2 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0
  1. <?php
  2. function get_Factures_pour_niveau_relance($niveau_relance, $ref_contact_filtre=""){
  3. global $bdd;
  4. $result = array();
  5. $ref_contact = "";
  6. $query_where = "";
  7. $query_niveau_relance = "";
  8. if (is_array($niveau_relance)){
  9. $query_niveau_relance = "&& niveau_relance IN (";
  10. foreach ($niveau_relance as $niveau){
  11. $query_niveau_relance .= $niveau.",";
  12. }
  13. $query_niveau_relance = substr($query_niveau_relance,0,-1).")";
  14. }else{
  15. $niveau_relance = intval($niveau_relance);
  16. $query_niveau_relance = "&& niveau_relance=".$niveau_relance;
  17. }
  18. if($ref_contact_filtre != "")
  19. $query_where = "AND ref_contact = '".$ref_contact_filtre."' ";
  20. $query = "SELECT d.ref_contact,d.ref_doc
  21. FROM documents d
  22. LEFT JOIN doc_fac df ON d.ref_doc = df.ref_doc
  23. LEFT JOIN factures_relances_niveaux frn ON df.id_niveau_relance = frn.id_niveau_relance
  24. WHERE id_etat_doc=18 ".$query_niveau_relance." ".$query_where."
  25. ORDER BY ref_contact,ref_doc ASC;";
  26. $resultat = $bdd->query($query);
  27. while ($doc = $resultat->fetchObject()){
  28. if ( $ref_contact != $doc->ref_contact){
  29. if ( isset($result[$ref_contact]) ){
  30. if ( count($result[$ref_contact]) <= 0 ){
  31. unset($result[$ref_contact]);
  32. $ref_contact = $doc->ref_contact;
  33. }else{
  34. $ref_contact = $doc->ref_contact;
  35. $result[$ref_contact] = array();
  36. }
  37. }else{
  38. $ref_contact = $doc->ref_contact;
  39. $result[$ref_contact] = array();
  40. }
  41. }
  42. $facture = open_doc($doc->ref_doc);
  43. if ( $facture->getRef_doc() ){
  44. $facture_niveau_relance = new facture_niveau_relance( $facture->getId_niveau_relance() );
  45. if ( $facture_niveau_relance && $facture_niveau_relance->getActif() ){
  46. $last_relance = $facture->getDate_last_relance();
  47. $delai_before_next = $facture_niveau_relance->getDelai_before_next();
  48. if ( is_null($last_relance) || ( strtotime(strftime("%Y-%m-%d")." 00:00") >= strtotime("+$delai_before_next day",strtotime($last_relance." 00:00")) ) ){
  49. if ( ($facture->getMontant_echu() >= $facture_niveau_relance->getMontant_mini()) || $facture_niveau_relance->getSuite_avant_echeance() ){
  50. $ref_doc = array();
  51. $ref_doc["ref_doc"] = $facture->getRef_doc();
  52. $ref_doc["etat_doc"] = $facture->getLib_etat_doc();
  53. $ref_doc["nom_contact"] = $facture->getNom_contact();
  54. $ref_doc["date_creation"] = $facture->getDate_creation();
  55. $ref_doc["id_niveau_relance"] = $facture->getId_niveau_relance();
  56. $ref_doc["montant"] = $facture->getMontant_ttc();
  57. $ref_doc["echeances"] = array();
  58. $echeances = $facture->getEcheancier();
  59. foreach ($echeances as $echeance){
  60. if ($echeance->etat == 3 || $facture_niveau_relance->getSuite_avant_echeance()){
  61. $ref_doc["echeances"][] = $echeance;
  62. }
  63. }
  64. $result[$ref_contact][$facture->getRef_doc()] = $ref_doc;
  65. }
  66. if ( isset($result[$ref_contact][$facture->getRef_doc()]["echeances"]) ){
  67. if(count($result[$ref_contact][$facture->getRef_doc()]["echeances"]) == 0)
  68. unset($result[$ref_contact][$facture->getRef_doc()]);
  69. }
  70. }
  71. }
  72. }
  73. if ( isset($result[$ref_contact]) ){
  74. if(count($result[$ref_contact]) == 0)
  75. unset($result[$ref_contact]);
  76. }
  77. }
  78. return $result;
  79. }
  80. function get_nb_Factures_pour_niveau_relance($niveau_relance, $ref_contact_filtre=""){
  81. global $bdd;
  82. $nb_fact = 0;
  83. $result = array();
  84. $ref_contact = "";
  85. $query_where = "";
  86. $query_niveau_relance = "";
  87. if (is_array($niveau_relance)){
  88. $query_niveau_relance = "&& niveau_relance IN (";
  89. foreach ($niveau_relance as $niveau){
  90. $query_niveau_relance .= $niveau.",";
  91. }
  92. $query_niveau_relance = substr($query_niveau_relance,0,-1).")";
  93. }else{
  94. $niveau_relance = intval($niveau_relance);
  95. $query_niveau_relance = "&& niveau_relance=".$niveau_relance;
  96. }
  97. if($ref_contact_filtre != "")
  98. $query_where = "AND ref_contact = '".$ref_contact_filtre."' ";
  99. $query = "SELECT d.ref_contact,d.ref_doc
  100. FROM documents d
  101. LEFT JOIN doc_fac df ON d.ref_doc = df.ref_doc
  102. LEFT JOIN factures_relances_niveaux frn ON df.id_niveau_relance = frn.id_niveau_relance
  103. WHERE id_etat_doc=18 ".$query_niveau_relance." ".$query_where."
  104. ORDER BY ref_contact,ref_doc ASC;";
  105. $resultat = $bdd->query($query);
  106. while ($doc = $resultat->fetchObject()){
  107. if ( $ref_contact != $doc->ref_contact){
  108. if ( isset($result[$ref_contact]) ){
  109. if ( count($result[$ref_contact]) <= 0 ){
  110. unset($result[$ref_contact]);
  111. $ref_contact = $doc->ref_contact;
  112. }else{
  113. $ref_contact = $doc->ref_contact;
  114. $result[$ref_contact] = array();
  115. }
  116. }else{
  117. $ref_contact = $doc->ref_contact;
  118. $result[$ref_contact] = array();
  119. }
  120. }
  121. $facture = open_doc($doc->ref_doc);
  122. if ( $facture->getRef_doc() ){
  123. $facture_niveau_relance = new facture_niveau_relance( $facture->getId_niveau_relance() );
  124. if ( $facture_niveau_relance && $facture_niveau_relance->getActif() ){
  125. $last_relance = $facture->getDate_last_relance();
  126. $delai_before_next = $facture_niveau_relance->getDelai_before_next();
  127. if ( is_null($last_relance) || ( strtotime(strftime("%Y-%m-%d")." 00:00") >= strtotime("+$delai_before_next day",strtotime($last_relance." 00:00")) ) ){
  128. if ( ($facture->getMontant_echu() >= $facture_niveau_relance->getMontant_mini()) || $facture_niveau_relance->getSuite_avant_echeance() ){
  129. $ref_doc = array();
  130. $ref_doc["ref_doc"] = $facture->getRef_doc();
  131. $ref_doc["etat_doc"] = $facture->getLib_etat_doc();
  132. $ref_doc["nom_contact"] = $facture->getNom_contact();
  133. $ref_doc["date_creation"] = $facture->getDate_creation();
  134. $ref_doc["id_niveau_relance"] = $facture->getId_niveau_relance();
  135. $ref_doc["montant"] = $facture->getMontant_ttc();
  136. $ref_doc["echeances"] = array();
  137. $echeances = $facture->getEcheancier();
  138. foreach ($echeances as $echeance){
  139. if ($echeance->etat == 3 || $facture_niveau_relance->getSuite_avant_echeance()){
  140. $ref_doc["echeances"][] = $echeance;
  141. }
  142. }
  143. $result[$ref_contact][$facture->getRef_doc()] = $ref_doc;
  144. }
  145. if ( isset($result[$ref_contact][$facture->getRef_doc()]["echeances"]) ){
  146. $nb_fact++;
  147. if(count($result[$ref_contact][$facture->getRef_doc()]["echeances"]) == 0){
  148. unset($result[$ref_contact][$facture->getRef_doc()]);$nb_fact--;}
  149. }
  150. }
  151. }
  152. }
  153. if ( isset($result[$ref_contact]) ){
  154. if(count($result[$ref_contact]) == 0)
  155. unset($result[$ref_contact]);
  156. }
  157. }
  158. return $nb_fact;
  159. }
  160. function generer_relance_client($ref_client,$id_niveau_relance,$id_edition_mode){
  161. $client = new contact($ref_client);
  162. if ( $ref_client == $client->getRef_contact()){
  163. $relances = get_Factures_pour_niveau_relance($id_niveau_relance,$ref_client);
  164. _vardump($relances);
  165. }
  166. }
  167. ?>