PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/library/include/expiry_calc.php

https://bitbucket.org/marcor/gazie
PHP | 164 lines | 115 code | 5 blank | 44 comment | 18 complexity | 3e3d2f79b42e57fd8df57759279beb44 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-2.0
  1. <?php
  2. /*
  3. --------------------------------------------------------------------------
  4. GAzie - Gestione Azienda
  5. Copyright (C) 2004-2012 - Antonio De Vincentiis Montesilvano (PE)
  6. (www.devincentiis.it)
  7. <http://gazie.it>
  8. --------------------------------------------------------------------------
  9. Questo programma e` free software; e` lecito redistribuirlo e/o
  10. modificarlo secondo i termini della Licenza Pubblica Generica GNU
  11. come e` pubblicata dalla Free Software Foundation; o la versione 2
  12. della licenza o (a propria scelta) una versione successiva.
  13. Questo programma e` distribuito nella speranza che sia utile, ma
  14. SENZA ALCUNA GARANZIA; senza neppure la garanzia implicita di
  15. NEGOZIABILITA` o di APPLICABILITA` PER UN PARTICOLARE SCOPO. Si
  16. veda la Licenza Pubblica Generica GNU per avere maggiori dettagli.
  17. Ognuno dovrebbe avere ricevuto una copia della Licenza Pubblica
  18. Generica GNU insieme a questo programma; in caso contrario, si
  19. scriva alla Free Software Foundation, Inc., 59
  20. Temple Place, Suite 330, Boston, MA 02111-1307 USA Stati Uniti.
  21. --------------------------------------------------------------------------
  22. */
  23. class Expiry {
  24. public function CalcExpiry($amount,$date,$effect='D',$start_day=0,$expiry_number=1,$periodicity='M',$foll_month=0,$fix_day=0)
  25. {
  26. /* Questa funzione serve per il calcolo delle scadenze.
  27. Restituisce una matrice (array) bidimensionale dove
  28. sull'indice 'amount' c'č l'importo e
  29. sull'indice 'date' c'č la data di scadenza.
  30. $effect puň avere i valori D,G,F
  31. $start_day sono i giorni di decorrenza della prima scadenza
  32. $expiry_number č il numero di rate
  33. $periodicity č la periodicitŕ in mesi
  34. $foll_month č il mese da saltare (es.agosto)
  35. $fix_day č il giorno fisso di scadenza quando $effect vale 'G'
  36. */
  37. // definisco le variabili comuni
  38. $this->year=intval(substr($date,0,4));
  39. $this->month=intval(substr($date,5,2));
  40. $this->day=intval(substr($date,8,2));
  41. $this->effect=strtoupper(substr($effect,0,1));
  42. $this->start_day=intval($start_day);
  43. $this->expiry_number=intval($expiry_number);
  44. $this->periodicity=strtoupper(substr($periodicity,0,1));
  45. $this->foll_month=intval($foll_month);
  46. $this->fix_day=intval($fix_day);
  47. // variabili di flusso
  48. $this->ctrl_day=$this->day;
  49. $this->ctrl_month=$this->month;
  50. $this->ctrl_year=$this->year;
  51. // main
  52. $this->expiry=array();
  53. $partial=0.00;
  54. for ($c=1; $c<=$this->expiry_number; $c++) {
  55. if ($c==$this->expiry_number && $this->expiry_number==1) { // rata unica
  56. $this->expiry[$c]['amount']= number_format($amount,2,'.','');
  57. } elseif ($c==$this->expiry_number && $this->expiry_number>1) { // ultima rata
  58. $this->expiry[$c]['amount']= number_format(($amount-$partial),2,'.','');
  59. } elseif ($c<$this->expiry_number && $this->expiry_number>1) { // rate intermedie e prima
  60. $this->expiry[$c]['amount']= number_format(($amount/$this->expiry_number),2,'.','');
  61. }
  62. $partial+=$this->expiry[$c]['amount'];
  63. // chiamo la funzione per il calcolo della data
  64. $this->expiry[$c]['date']= $this->_Date($c);
  65. }
  66. // end main
  67. return $this->expiry;
  68. }
  69. // calcolo date
  70. protected function _Date($c) {
  71. if ($c==1) { // alla prima scadenza si devono aggiungere i giorni di decorrenza
  72. switch($this->effect) {
  73. case "D": //caso in cui la scadenza fa riferimento alla data della fattura
  74. $uts = mktime(0,0,0,$this->month,$this->day+$this->start_day,$this->year);
  75. $this->ctrl_day = strftime("%d",$uts);
  76. $this->ctrl_month = strftime("%m",$uts);
  77. $this->ctrl_year = strftime("%Y",$uts);
  78. break;
  79. case "G": //caso in cui la scadenza fa riferimento ad un giorno fisso impostato sul relativo campo
  80. $uts = mktime(0,0,0,$this->month,$this->day+$this->start_day,$this->year);
  81. $this->ctrl_day = strftime("%d",$uts);
  82. $this->ctrl_month = strftime("%m",$uts);
  83. $this->ctrl_year = strftime("%Y",$uts);
  84. if ($this->fix_day<$this->ctrl_day) { // salto un mese se va a cadere in un giorno precedente
  85. $this->ctrl_month++;
  86. }
  87. $uts = mktime(0,0,0,$this->ctrl_month,$this->fix_day,$this->ctrl_year);
  88. $this->ctrl_day = strftime("%d",$uts);
  89. $this->ctrl_month = strftime("%m",$uts);
  90. $this->ctrl_year = strftime("%Y",$uts);
  91. break;
  92. case "F": //caso in cui la scadenza deve far riferimento al fine mese rispetto alla data della fattura
  93. // prima porto il riferimento al primo finemese
  94. $uts = mktime(0,0,0,$this->ctrl_month+1,0,$this->ctrl_year);
  95. $this->ctrl_day = strftime("%d",$uts);
  96. $this->ctrl_month = strftime("%m",$uts);
  97. $this->ctrl_year = strftime("%Y",$uts);
  98. // poi aumento dei giorni di decorrenza (-2 per compensare febbraio)
  99. $uts = mktime(0,0,0,$this->ctrl_month,$this->ctrl_day+$this->start_day-2,$this->ctrl_year);
  100. $this->ctrl_month = strftime("%m",$uts);
  101. $this->ctrl_year = strftime("%Y",$uts);
  102. // quindi riporto a fine mese
  103. $uts = mktime(0,0,0,$this->ctrl_month+1,0,$this->ctrl_year);
  104. $this->ctrl_day = strftime("%d",$uts);
  105. $this->ctrl_month = strftime("%m",$uts);
  106. $this->ctrl_year = strftime("%Y",$uts);
  107. if ($this->fix_day>0){ // eventualmente vado al giorno successivo
  108. $uts = mktime(0,0,0,$this->ctrl_month+1,$this->fix_day,$this->ctrl_year);
  109. $this->ctrl_day = strftime("%d",$uts);
  110. $this->ctrl_month = strftime("%m",$uts);
  111. $this->ctrl_year = strftime("%Y",$uts);
  112. }
  113. break;
  114. }
  115. if ($this->foll_month>0 && $this->foll_month==$this->ctrl_month) {
  116. $this->ctrl_month++;
  117. }
  118. } else { // le scadenze successive
  119. switch($this->periodicity) {
  120. case "Q": // quindicinale
  121. if ($c%2==0) {
  122. $n_m=0;
  123. $this->ctrl_day += 15;
  124. } else {
  125. $n_m=1;
  126. $this->ctrl_day -= 15;
  127. }
  128. break;
  129. case "M":$n_m= 1;break; // mensile
  130. case "B":$n_m= 2;break; // bimestrale
  131. case "T":$n_m= 3;break; // trimestrale
  132. case "U":$n_m= 4;break; // quadrimestrale
  133. case "E":$n_m= 6;break; // semestrale
  134. case "A":$n_m=12;break; // annuale
  135. }
  136. $this->ctrl_month+=$n_m;
  137. if ($this->effect=='F' && $this->periodicity!='Q' && $this->fix_day==0) { // quando si deve andare a fine mese
  138. $this->ctrl_day=0;
  139. $this->ctrl_month++;
  140. }
  141. $uts = mktime(0,0,0,$this->ctrl_month,$this->ctrl_day,$this->ctrl_year);
  142. $this->ctrl_day = strftime("%d",$uts);
  143. $this->ctrl_month = strftime("%m",$uts);
  144. $this->ctrl_year = strftime("%Y",$uts);
  145. if ($this->foll_month>0 && $this->foll_month==$this->ctrl_month) {
  146. $this->ctrl_month++;
  147. }
  148. }
  149. $uts = mktime(0,0,0,$this->ctrl_month,$this->ctrl_day,$this->ctrl_year);
  150. $this->ctrl_day = strftime("%d",$uts);
  151. $this->ctrl_month = strftime("%m",$uts);
  152. $this->ctrl_year = strftime("%Y",$uts);
  153. return strftime("%Y-%m-%d",mktime(0,0,0,$this->ctrl_month,$this->ctrl_day,$this->ctrl_year));
  154. }
  155. // fine calcolo date
  156. }
  157. ?>