<?php

/**
 * @file
 * Provide Donate Form Data.
 */

/**
 * Function to display donate form.
 */
function donate_onlinepay_settlement_edit_form($form, $form_state) {
  $form = array();
  global $user;
  $edit_id = arg(1);
  $donate_query = db_query("SELECT * FROM {donate} where id = :id", array(':id'=> $edit_id), array('fetch' => PDO::FETCH_ASSOC));
  foreach($donate_query as $dr){
      $donarname = $dr['donarname'];
      $fathername = $dr['fathername'];
      $donaremail = $dr['donaremail'];
      $donarmobileno = $dr['donarmobileno'];
      $address = $dr['address'];
      $comment = $dr['comment'];
      $amountdonated = $dr['amountdonated'];
      $amountdonatedon = $dr['amountdonatedon'];
      $paymode = $dr['paymode'];
      $paymentcollectionstatus = $dr['paymentcollectionstatus'];
      $receiptstatus = $dr['receiptstatus'];
      $displayonwebsite = $dr['displayonwebsite'];
      if($dr['online_payment_settlement_date_entered_by_user']!=0){
            $online_payment_settlement_date = date("Y-m-d",$dr['online_payment_settlement_date_entered_by_user']);
      }else{
            $online_payment_settlement_date='';
      }
      $online_payment_settlement_status = $dr['online_payment_settlement_status'];
      $online_payment_settlement_paymentid = $dr['online_payment_settlement_paymentid'];
      $online_payment_settlement_transactionid = $dr['online_trackid'];
      $online_payment_settlement_remark = $dr['online_payment_settlement_remark'];
      if($online_payment_settlement_status==1){
          $disabled = TRUE;
      }else{
          $disabled=FALSE;
      }
  }
   
  $form['donarname'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $donarname,  
  );
  
  
  
  $form['donaremail'] = array(
    '#title' => t('Email'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $donaremail, 
  );
  
  $form['donarmobileno'] = array(
    '#title' => t('Mobile No'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE,
    '#default_value' => $donarmobileno, 
  );   
  
  $form['amountdonated'] = array(
    '#title' => t('Amount Donated (in Rs.)'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => $amountdonated, 
  ); 
  
  $form['amountdonatedon'] = array(
    '#title' => t('Amount Donated On Date'),
    '#type' => 'textfield',
    '#required' => TRUE,
    '#disabled' => TRUE, 
    '#default_value' => format_date($amountdonatedon), 
  ); 
  
 
   
  $paymentcollectionstatus_options = array('0' => t('Online Payment Pending'), '1' => t('Online Payment Done and Settled'));
  $form['online_payment_settlement_status'] = array(
    '#type' => 'radios',
    '#title' => t('Payment Settlement Status'),     
    '#options' => $paymentcollectionstatus_options,
    '#required' => TRUE,
    '#disabled' => $disabled,
    '#default_value' => $online_payment_settlement_status, 
  );
  $form['online_payment_settlement_paymentid'] = array(
    '#title' => t('Payment ID'),
    '#type' => 'textfield',
    '#required' => TRUE, 
    '#disabled' => $disabled,
    '#default_value' => $online_payment_settlement_paymentid,
  ); 
  $form['online_payment_settlement_transactionid'] = array(
    '#title' => t('Transaction ID'),
    '#type' => 'textfield',
    '#required' => TRUE, 
    '#disabled' => $disabled,
    '#default_value' => $online_payment_settlement_transactionid,
  ); 
  $form['online_payment_settlement_date'] = array(
    '#type' => 'date_popup', //you can find a list of available types in the form api
    '#title' => 'Settlement Date (Y-M-D)',
    '#default_value' => $online_payment_settlement_date,
    '#required' => TRUE, 
    '#disabled' => $disabled,
    '#date_format' => 'Y-m-d',
    '#date_year_range' => '-90:+1', 
    //'#datepicker_options' => array('minDate' => 0),
    '#size' => '40',
  );
  /*$form['online_payment_settlement_receipt'] = array(
    '#title' => t('Upload Online Payment Receipt'),
    '#type' => 'file', 
    '#required' => TRUE,  
  );  */
  $form['online_payment_settlement_remark'] = array(
    '#title' => t('Settlement Remark'),
    '#type' => 'textarea',
    '#disabled' => $disabled,
    '#default_value' => $online_payment_settlement_remark, 
  );
  if($online_payment_settlement_status==1){
        $form['submitval'] = array(
            '#markup' => 'Online Payment is settled',
             
        ); 
  }else{
        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('Settle Online Payment'),
        );
  }
  
  return $form;
}
  
/**
 * Implements hook_form_validate().
 */
function donate_onlinepay_settlement_edit_form_validate($form, &$form_state) {   
    if ($form_state['values']['op'] == t('Settle Online Payment')) {         

    }
}

/**
 * Implements hook_form_submit().
 */
function donate_onlinepay_settlement_edit_form_submit($form, &$form_state) {  
    
    global $user;
    $id = arg(1);
    $datevalue = time();
    $ip_address_of_user = $_SERVER['REMOTE_ADDR'];     
    $online_payment_settlement_status = $form_state['values']['online_payment_settlement_status'];
    $online_payment_settlement_paymentid = $form_state['values']['online_payment_settlement_paymentid'];
    $online_payment_settlement_transactionid = $form_state['values']['online_payment_settlement_transactionid'];
    $online_payment_settlement_date = strtotime($form_state['values']['online_payment_settlement_date']);
    //$online_payment_settlement_receipt = $form_state['values']['online_payment_settlement_receipt'];
    $online_payment_settlement_receipt = '';
    $online_payment_settlement_remark = $form_state['values']['online_payment_settlement_remark'];
    
    $donaremail = $form_state['values']['donaremail'];
    $donarname  = $form_state['values']['donarname'];
    $amount     = $form_state['values']['amountdonated'];

    
    if($online_payment_settlement_status==1){
        $paymentstatus=1;
        $online_process_status='success';
        $online_hash_response='Hashing Response Successful';
    }else{
        $paymentstatus=0;
        $online_process_status='pending';
        $online_hash_response='';
    }
     
    $update_entry = array(        
                        'online_payment_settlement_status'  => $online_payment_settlement_status,
                        'online_payment_settlement_paymentid' => $online_payment_settlement_paymentid,
                        'online_trackid' => $online_payment_settlement_transactionid,
                        'online_payment_settlement_date_entered_by_user' => $online_payment_settlement_date,
                        'online_payment_settlement_receipt' => $online_payment_settlement_receipt,
                        'online_payment_settlement_remark' => $online_payment_settlement_remark,
                        'online_payment_settlement_done_by' => $user->uid,
                        'online_payment_settlement_done_on' => $datevalue,
                        'online_payment_settlement_ip_address' => $ip_address_of_user,
                        'paymentstatus' => $paymentstatus,
                        'online_process_status' => $online_process_status,
                        'online_hash_response' => $online_hash_response,
                   );
    
    if(is_numeric($id)){
        $query_updated = db_update('donate')  
                                ->fields($update_entry)
                                ->condition('id', $id, '=')
                                ->execute();
    
        //////////////// send mail
        
        /*$sub = "Thanks for your donation";

        $message = "Dear ".$donarname."<br><br>Thanks for donating Rs. ".$amount.". We have received the amount you donated. Your efforts can enlighten the life of someone.<br><br>Thanks & Regards";
        //$mailaccount = "info@iaseuniversityonline.org";
        $mailaccount = "societygvm@gmail.com";

        $New_Line = "\r\n";
        $headers = "MIME-Version: 1.0" .$New_Line;
        $headers .= "Content-type: text/html; charset=iso-8859-1" .$New_Line;
        $headers .= "Content-Transfer-Encode: 7bit " .$New_Line;
        $headers .= "From: $mailaccount ".$New_Line;
        'Reply-To:  '.$mailaccount  . "\r\n" .
        'X-Mailer: PHP/' . phpversion();                 	
        $mail = mail($donaremail,$sub,$message,$headers);*/

        drupal_set_message(t("Online Payment of Rs.@entry by user @username is settled successfully.", array('@entry' => $amount,'@username' => $donarname)));
        
    }else{
        drupal_set_message(t("Cannot settle online payment as page url is not correct. It is not having the required numeric value."));
    }   
     
}

 