Author: benj
Date: Thu Feb 11 15:09:52 2016
New Revision: 1657

URL: http://svn.gna.org/viewcvs/gdtc?rev=1657&view=rev
Log:
Implement new coherence test + fix cbrecut amount computation

Modified:
    trunk/gdtc/include/coherence.php

Modified: trunk/gdtc/include/coherence.php
URL: 
http://svn.gna.org/viewcvs/gdtc/trunk/gdtc/include/coherence.php?rev=1657&r1=1656&r2=1657&view=diff
==============================================================================
--- trunk/gdtc/include/coherence.php    (original)
+++ trunk/gdtc/include/coherence.php    Thu Feb 11 15:09:52 2016
@@ -57,6 +57,7 @@
                 check_prelevement_start_date_is_fucked (),
                 check_prelevement_periodicity_is_fucked (),
                 check_prelevement_not_created(),
+                check_cbrecur_not_created(),
                 check_no_current_membership(),
                 check_cmcic_periodicity_is_fucked () , 
                 check_cmcic_amount_is_fucked () 
@@ -355,6 +356,96 @@
 
 
 
+/***** prevelement not created for current year */
+
+function check_cbrecur_not_created ()
+{
+  $results = basic_query ( "AND subscription.payment_mode = 'cbweb_recur'" .
+                          "AND subscription.end_date != 
subscription.cbrecur_expiration_date " .
+                          "AND membership.renewal_date = subscription.end_date 
" .
+                          "AND membership.renewal_date < now() " );
+  if ( ! $results ) return;
+
+  $output = Array ( "Cbrecur non créé : " . sizeof($results). " 
résultat(s)", 'cbrecur_not_created', 1 );
+  foreach ( $results as $result )
+    {
+      if ( $result [ 'actor_type' ] == 'person' )
+       {
+         $output[3][] = Array ( $result [ 'actor_id' ], 
+                                sprintf ( "%s %s", $result [ 'firstname' ], 
$result [ 'person_name' ] ) );
+       }
+      else
+       {
+         $output[3][] = Array ( $result [ 'actor_id' ], 
+                                $result [ 'entity_name' ] );
+       }
+    }
+
+  return $output;
+}
+
+
+function fix_cbrecur_not_created ( $id )
+{
+    $actor = get_all_actor_details ( $id );
+
+    if ( $actor [ 'subscription' ] [ 'prelevement_periodicity' ] != 0 )
+    {
+       preg_match ( '/([0-9]*)-([0-9]*)-([0-9]*)/', 
+                    $actor [ 'subscription' ] [ 'prelevement_start_date' ], 
+                    $matches );
+       $year = $matches [ 1 ];
+       $month = $matches [ 2 ];
+       $day = $matches [ 3 ];
+       do
+       {
+           $month += $actor [ 'subscription' ] [ 'prelevement_periodicity' ];
+       } while ( $month <= 12 );
+       $month = $month % 12;
+       $year++;
+       $amount = ( 12 / $actor [ 'subscription' ] [ 'prelevement_periodicity' 
] ) 
+           * $actor [ 'subscription' ] [ 'prelevement_amount' ];
+           
+       /* Fix missing subscription. */
+       do_query ( 
+           sprintf ( "INSERT INTO subscription " .
+                     "            ( membership_id, payment_mode, start_date, 
end_date, " .
+                     "              payment_received_date, amount, " .
+                     "              prelevement_periodicity, 
prelevement_amount, prelevement_start_date, " .
+                     "              admin_info ) " .
+                     "     VALUES ( '%s', '%s', ADDDATE('%s', INTERVAL 1 
YEAR), ADDDATE('%s', INTERVAL 1 YEAR), " .
+                     "              now(), '%s', " .
+                     "              '%s', '%s', '%s', " .
+                     "              'Correction automatique' ); ",
+                     $actor [ 'subscription' ] [ 'membership_id' ], 
+                     'cbweb_recur', 
+                     $actor [ 'subscription' ] [ 'start_date' ], 
+                     $actor [ 'subscription' ] [ 'end_date' ], 
+                     $amount,
+                     $actor [ 'subscription' ] [ 'prelevement_periodicity' ], 
+                     $actor [ 'subscription' ] [ 'prelevement_amount' ], 
+                     sprintf ( "%04d-%02d-%02d\n", $year, $month, $day ) ) );
+       record_change ( 'added', 'subscription', 'subscription_id', 
+                       mysql_insert_id(), '', '', '' );
+  
+       /* Fix membership renewal_date as well. */
+       preg_match ( '/([0-9]*)-([0-9]*)-([0-9]*)/', 
+                    $actor [ 'subscription' ] [ 'end_date' ], $matches );
+       $changes [ 'renewal_date' ] = sprintf ( "%04d-%02d-%02d\n", 
+                                               $matches [ 1 ] + 1, 
+                                               $matches [ 2 ], 
+                                               $matches [ 3 ] );
+       commit ( 'membership', 'membership_id', 
+                $actor [ 'subscription'] [ 'membership_id' ], 
+                $changes );
+ 
+       print "good";
+    }
+
+}
+
+
+
 /***** No current membership */
 
 function check_no_current_membership ( )
@@ -390,14 +481,15 @@
 
 function compute_real_cbrecur_amount ( $result )
 {
-      $start_date = preg_replace ( '/^(\d\d\d\d-\d\d)-\d\d/', '$1-05', 
$result['start_date']);
-      $end_date = preg_replace ( '/^(\d\d\d\d-\d\d)-\d\d/', '$1-05', 
$result['end_date']);
+      $start_date = $result['start_date'];
+      $end_date = $result['end_date'];
       $date1 = new DateTime($start_date);
       $date2 = new DateTime($end_date);
       $interval = $date1->diff($date2);
-      $months = $interval->format('%m') + $interval->format('%y')*12; 
-      if ( date_format($date1, 'd' ) > 5 ) $month--;
-      if ( date_format($date2, 'd' ) >= 5 ) $month++;
+      $months = $interval->format('%m') + $interval->format('%y')*12;
+      #if ( date_format($date1, 'd' ) > 5 ) $months--;
+      if ( date_format($date2, 'd' ) >= 5 ) $months++;
+      if ( date_format($date1, 'd' ) == date_format($date2, 'd' ) ) $months--;
       return $months * $result [ 'prelevement_amount' ] + $result [ 
'cbrecur_prorata' ];
 }
 


_______________________________________________
Gdtc-commits mailing list
[email protected]
https://mail.gna.org/listinfo/gdtc-commits

Reply via email to