Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.56 KB | None | 0 0
  1. add_action(
  2.   'AHEE__EE_Registration_Processor__trigger_registration_update_notifications',
  3.   'act_ee4_assign_invoicenr', 10, 2
  4. ); // 1, 2
  5.  
  6. function act_ee4_assign_invoicenr( $registration, $update_params ) {
  7.  
  8.   // get registration status. $registration is passed through the hook
  9.   $registration_status = $registration->status_ID();
  10.  
  11.   $reg_status_value = EEM_Status::instance()->get_var(
  12.      array(
  13.         array(
  14.           'Registration.STS_ID' => $registration_status
  15.         ),
  16.         'limit' => 1
  17.       ),
  18.       'STS_code'
  19.   );
  20.  
  21.   // get transaction
  22.   $transaction = $registration->transaction();
  23.   $transaction_status = $transaction->status_ID();
  24.  
  25.   // admin question id
  26.   $invoice_question_id = 21;
  27.  
  28.  
  29.   // Assemble custom invoice number if the registration is approved
  30.  
  31.   if ($registration_status == "RAP") {
  32.      
  33.     // get invoice number, year and prefix from options table
  34.     $current_year = (int) date( 'Y' );
  35.     $invoice_prefix = "20"; //get_option( 'invoice_prefix' );
  36.     $invoice_year = (int) get_option( 'invoice_year' ); // e.g. 2017
  37.     $invoice_order = (int) get_option( 'invoice_order' ); // e.g. 24
  38.    
  39.     // increment year value if current year is higher
  40.     if ( $invoice_year != $current_year ) {
  41.       update_option( 'invoice_year', $current_year );
  42.       update_option( 'invoice_order', 1 );
  43.     }
  44.  
  45.     // increment invoice number for next invoice
  46.     update_option( 'invoice_order', $invoice_order + 1 );
  47.  
  48.     // assemble invoice number
  49.     $invoice_number = $invoice_prefix . $invoice_year
  50.       . str_pad( $invoice_order, 4, '0', STR_PAD_LEFT );
  51.  
  52.   }
  53.   else{
  54.     // Otherwise temporarily populate the answer with the Registration Status Value
  55.     $invoice_number = $reg_status_value;
  56.   }
  57.  
  58.  
  59.  
  60.   // Assign Invoice Number
  61.  
  62.   // Check if there is an answer for this question already
  63.  
  64.   $prev_answer_value = EEM_Answer::instance()->get_answer_value_to_question($registration, $invoice_question_id, false);
  65.  
  66.   if( $prev_answer_value ) {  
  67.  
  68.     // If answer exists, update it
  69.     $prev_answer_object = EEM_Answer::instance()->get_registration_question_answer_object($registration, $invoice_question_id, false);
  70.     $prev_answer_object->set('ANS_value', $invoice_number);
  71.     $prev_answer_object->save();
  72.   }
  73.   else{
  74.  
  75.     // if it doesn't exist insert it
  76.     EEM_Answer::instance()->insert(
  77.       array(
  78.         'REG_ID' => $registration->ID(),
  79.         'QST_ID' => $invoice_question_id, // admin only question 'invoice number'
  80.         'ANS_value' => $invoice_number
  81.       )    
  82.     );
  83.   }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement