/**
 * Implements hook_menu().
 */
function ebay_menu() {
  $items['admin/bing/ebay/transaction/%/invoice'] = array(
    'title' => t('eBay Transaction Invoice'),
    'type' => MENU_CALLBACK,
    'page callback' => 'ebay_transaction_invoice',
    'page arguments' => array(4),
    'file' => 'ebay.transaction.inc',
    'access arguments' => array('access ebay api tools'),
  );
  $items['admin/bing/ebay/transaction/%/invoice/print'] = array(
    'title' => t('Print Invoice'),
    'type' => MENU_CALLBACK,
    'page callback' => 'ebay_transaction_invoice',
    'page arguments' => array(4, TRUE),
    'file' => 'ebay.transaction.inc',
    'access arguments' => array('access ebay api tools'),
  );
  return $items;
}

/**
 * Page callback for transaction invoice.
 */
function ebay_transaction_invoice($sales_number) {
  $transaction = db_query("SELECT * FROM {ebay_orders_import} WHERE sales_number = :sales_number", array('sales_number' => $sales_number))->fetchAssoc();
  $form = drupal_get_form('ebay_transaction_invoice_form', $transaction);

  // 打印模版借鉴了ubercart模块中uc_order.module的函数uc_order_view_invoice
  $build = array(
    '#theme' => 'ebay_invoice',
    '#transaction' => $transaction,
  );

  if ($print) {
    drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
    print theme('uc_order_invoice_page', array('content' => drupal_render($build)));
    exit();
  }
  else {
    return drupal_render($form) . theme('ebay_invoice', array('transaction' => $transaction));
  }
}

/**
 * Sets recipients of an invoice, then mails it.
 *
 * @see ebay_transaction_invoice_form_submit()
 * @ingroup forms
 */
function ebay_transaction_invoice_form($form, &$form_state, $transaction) {
  $form['sales_number'] = array(
    '#type' => 'hidden',
    '#value' => $transaction['sales_number'],
  );
  $form['email'] = array(
    '#type' => 'textfield',
    '#default_value' => $transaction['buyer_email'],
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Mail Invoice'),
  );
  return $form;
}

/**
 * Form submit handler for ebay_transaction_invoice_form().
 *
 * @see ebay_transaction_invoice_form()
 */
function ebay_transaction_invoice_form_submit($form, &$form_state) {
  $transaction = db_query("SELECT * FROM {ebay_orders_import} WHERE sales_number = :sales_number", array('sales_number' => $form_state['values']['sales_number']))->fetchAssoc();
  isset($transaction) ? $params = array('transaction' => $transaction) : $params = array();
  $recipient = $form_state['values']['email'];

  $sent = drupal_mail('ebay', 'ebay_invoice', $recipient, uc_store_mail_recipient_language($recipient), $params, uc_store_email_from());
  if (!$sent) {
    drupal_set_message(t('E-mail invoice failed'));
  }
  else {
    $message = t('Invoice e-mailed to @email.', array('@email' => $recipient));
    drupal_set_message($message);
  }
}

/**
 * Implements hook_mail().
 */
function ebay_mail($key, &$message, $params) {
  $language = isset($message['language']) ? $message['language']->language : NULL;
  if ($key == 'ebay_invoice') {
    $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
    $message['subject'] = t('Your eBay Transaction Invoice', array(), array('langcode' => $language));
    $message['from'] = uc_store_email_from();
    $message['body'][] = theme('ebay_invoice', $params);
  }
}

/**
 * Implements hook_theme().
 * @see ebay-invoice.tpl.php
 */
function ebay_theme() {
  return array(
    'ebay_invoice' => array(
      'template' => 'ebay-invoice',
      'variables' => array(
        'transaction' => NULL
      )
    )
  );
}

/**
 * ebay-invoice.tpl.php
 * @ingroup themeable
 */

<?php print theme('image', array('path' => drupal_get_path('theme', 'mars') . '/images/invoice.png')); ?>

<dl>
  <dt><strong>Mars Performance Pty.Ltd.</strong></dt>
  <dd style="margin: 0; padding: 0;">The auto spirit creators...</dd>
  <dd style="margin: 0; padding: 0;">ABN: 70133736302</dd>
</dl>

<h2>Invoice:</h2>
<table style="width: 100%;">
  <tr>
    <td style="width: 33%;">No: <?php print $transaction['sales_number']; ?></td>
    <td style="width: 33%;">eBay Member ID: <?php print $transaction['buyer_ebay_uid']; ?></td>
    <td style="width: 33%;">Date: <?php print format_date($transaction['created'], 'custom', 'j/F/Y'); ?></td>
  </tr>
</table>

<table style="width: 100%;">
  <tr>
    <td style="width: 50%;">
      <dl>
        <dt><strong>FROM:</strong></dt>
        <dd style="margin: 0; padding: 0;">Mars Performance Pty.Ltd.</dd>
        <dd style="margin: 0; padding: 0;"><?php print variable_get('uc_store_street1', NULL); ?></dd>
        <dd style="margin: 0; padding: 0;"><?php print variable_get('uc_store_city', NULL); ?>, VIC, <?php print variable_get('uc_store_postal_code', NULL); ?></dd>
        <dd style="margin: 0; padding: 0;"><strong>Fax:</strong> <?php print variable_get('uc_store_fax', NULL); ?></dd>
        <dd style="margin: 0; padding: 0;"><strong>Phone:</strong> <?php print variable_get('uc_store_phone', NULL); ?></dd>
        <dd style="margin: 0; padding: 0;"><strong>Email:</strong> <?php print variable_get('uc_store_email', NULL); ?>
      </dl>
    </td>
    <td style="width: 50%;">
      <dl>
        <dt><strong>Shipping to:</strong></dt>
        <dd style="margin: 0; padding: 0;"><?php print $transaction['buyer_fullname']; ?></dd>
        <dd style="margin: 0; padding: 0;"><?php print $transaction['buyer_street1'] . ', ' . $transaction['buyer_street2']; ?></dd>
        <dd style="margin: 0; padding: 0;"><?php print $transaction['buyer_city'] . ', ' . $transaction['buyer_state'] . ', ' . $transaction['buyer_country'] . ', ' . $transaction['buyer_postcode']; ?></dd>
        <dd style="margin: 0; padding: 0;"><strong>Phone:</strong> <?php print $transaction['buyer_phone']; ?></dd>
        <dd style="margin: 0; padding: 0;"><strong>Email:</strong> <?php print $transaction['buyer_email']; ?></dd>
      </dl>
    </td>
  </tr>
</table>

<table style="width: 100%;">
  <thead>
  <tr style="font-weight: bold;">
    <td>Qty</td>
    <td>Product</td>
    <td>SKU</td>
    <td>Price</td>
  </tr>
  </thead>
  <tbody>
  <tr>
    <td><?php print $transaction['quantity']; ?></td>
    <td><?php print $transaction['item_title']; ?></td>
    <td><?php print $transaction['item_sku']; ?></td>
    <td><?php print uc_currency_format($transaction['total']); ?></td>
  </tr>

  <?php $data = unserialize($transaction['data']); $total = $transaction['total']; ?>
  <?php if (isset($data['ShippingService']) && isset($data['ShippingServiceCost'])): ?>
    <tr>
      <td></td>
      <td></td>
      <td><strong><?php print $data['ShippingService']; ?></strong></td>
      <td><?php $total += $data['ShippingServiceCost']; print uc_currency_format($data['ShippingServiceCost']); ?></td>
    </tr>
  <?php endif; ?>

  <tr>
    <td></td>
    <td></td>
    <td><strong>GST Included</strong></td>
    <td><?php print uc_currency_format($total / 11); ?></td>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td><strong>Total</strong></td>
    <td><?php print uc_currency_format($total); ?></td>
  </tr>
  </tbody>
</table>

<ul style="list-style: none;">
  <li>Thank you for your purchase!</li>
  <li>1 year warranty provided from the transaction date (see website for terms and conditions).</li>
  <li>Professional installation receipt is required for all warranty claims.</li>
</ul>

<ul style="list-style: none;">
  <li><strong>Bank Details:</strong></li>
  <li><strong>Account Name:</strong> Mars Performance Pty. Ltd.</li>
  <li><strong>BSB:</strong> 063115; <strong>Account NO.:</strong> 10263933</li>
  <li><strong>Bank name:</strong> Commonwealth Bank Australia</li>
</ul>

<ul style="list-style: none;">
  <li>Thanks for visiting MARS PERFORMANCE</li>
  <li>http://marsperformance.com.au/</li>
</ul>