<?php
// $Id: cod.module,v 1.8.2.2.2.5 2007/03/07 05:38:42 neclimdul Exp $

/********************************************************************
 * Drupal Hooks
 ********************************************************************/

/**
 * Implementation of hook_help().
 */
function cod_help($section = 'admin/help#cod') {

  switch ($section) {
    case 'admin/modules#description':
      return t('<b>EC:</b> Process payments using cash on delivery.');
  }
}

/**
 * Implementation of hook_menu()
 */
function cod_menu($may_cache) {
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/store/settings/cod',
      'title' => 'C.O.D.',
      'callback' => 'cod_ec_settings',
      'access' => user_access('administer store'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Implementation of hook_settings().
 */
function cod_ec_settings() {
  $form['cod_title'] = array(
   '#type' => 'textfield', 
   '#title' => t('Title to use COD'), 
   '#default_value' => variable_get('cod_title', t('COD')), 
   '#size' => 70, 
   '#maxlength' => 780, 
   '#weight' => -1,
   '#desciption' => t('Description to show shoppers for this type of payment.')
  );
  $form['cod_url'] = array(
   '#type' => 'textfield', 
   '#title' => t('Page to go to after payment'), 
   '#default_value' => variable_get('cod_url', 'node'), 
   '#size' => 70, 
   '#maxlength' => 780, 
   '#weight' => -1,
   '#desciption' => t('Page to go to after payment is completed.')
  );
  $form['cod_txn_paid'] = array(
    '#type' => 'radios', 
    '#title' => t('Mark transaction as paid?'), 
    '#default_value' => variable_get('cod_txn_paid', 0), 
    '#options' => array(t('No'), t('Yes')),
    '#desciption' => t('Whether to mark transactions as paid immediately, or wait till the store admin marks them as paid') 
  );
  $form['cod_txn_completed'] = array(
    '#type' => 'radios', 
    '#title' => t('Mark transaction workflow as completed?'), 
    '#default_value' => variable_get('cod_txn_completed', 0), 
    '#options' => array(t('No'), t('Yes')),
    '#desciption' => t('Marks transactions that have no shippable items as completed in the workflow') 
  );

  return system_settings_form('cod_ec_settings_form', $form);
}

/**
 * The Controller to create a new payment interface,
 * provided user has 'Make a COD Payment' access permission.
 */
function cod_paymentapi(&$txn, $op, $arg = '') {
  if (user_access('Make a COD Payment')) {
    switch ($op) {
      case 'display name':
        return variable_get('cod_title', t('COD'));;

      case 'payment page':
        return cod_goto($txn);
        break;
    }
  }
}

function cod_goto($txn) {
  $edit = array();

  if (variable_get('cod_txn_paid', 0)) {
    $edit['payment_status'] = payment_get_status_id('completed');

    drupal_set_message(t('Your purchase has been completed.'));

    if (variable_get('cod_txn_completed', 0)) {
      // Check if we have shippable items
      $has_shippable_item = FALSE;
      foreach($txn->items as $key => $value) {
        if (product_is_shippable($value->vid)) {
          $has_shippable_item = TRUE;
        }
      }

      if (!$has_shippable_item) {
        $edit['workflow'] = EC_WORKFLOW_COMPLETED;
      }
    }

    $edit['txnid'] = $txn->txnid;
    // Update the transaction
    $txnid = store_transaction_save($edit);
    if ($txnid) {
      // Send an email notification
      store_send_invoice_email($txnid);
    }
  }
  else {
    store_send_invoice_email($txn->txnid);
    drupal_set_message(t('Your purchase has been received.'));
  }

  return strtr(variable_get('cod_url', 'node'), array('%txnid' => $txn->txnid));
}

/** Implementation of hook_perm() to control access
 *  to COD payment method
 */

function cod_perm() { 
  return array('Make a COD Payment');
} 
