/**
 * Handle amazon payment import request.
 *
 * @return The url to display on completion.
 */
function command_amazon_payment_import()
{
    if (!user_access('payment_edit')) {
        error_register('User does not have permission: payment_edit');
        return crm_url('payments');
    }
    if (!array_key_exists('payment-file', $_FILES)) {
        error_register('No payment file uploaded');
        return crm_url('payments&tab=import');
    }
    $csv = file_get_contents($_FILES['payment-file']['tmp_name']);
    $data = csv_parse($csv);
    $count = 0;
    message_register("Processing " . count($data) . " row(s)");
    foreach ($data as $row) {
        // Ignore withdrawals, holds, and failures
        if (strtolower($row['Type']) !== 'payment') {
            message_register("Ignoring row of type: " . $row['Type']);
            continue;
        }
        if (strtolower($row['To/From']) !== 'from') {
            message_register("Ignoring outgoing payment");
            continue;
        }
        if (strtolower($row['Status']) !== 'completed') {
            message_register("Ignoring payment with status: " . $row['Status']);
            continue;
        }
        // Skip transactions that have already been imported
        $payment_opts = array('filter' => array('confirmation' => $row['Transaction ID']));
        $data = payment_data($payment_opts);
        if (count($data) > 0) {
            message_register("Skipping previously imported payment: " . $row['Transaction ID']);
            continue;
        }
        // Parse value
        $value = payment_parse_currency($row['Amount']);
        // Create payment object
        $payment = array('date' => date('Y-m-d', strtotime($row['Date'])), 'code' => $value['code'], 'value' => $value['value'], 'description' => $row['Name'] . ' Amazon Payment', 'method' => 'amazon', 'confirmation' => $row['Transaction ID'], 'notes' => $row['notes'], 'amazon_name' => $row['Name']);
        // Check if the amazon name is linked to a contact
        $opts = array('filter' => array('amazon_name' => $row['Name']));
        $contact_data = amazon_payment_contact_data($opts);
        if (count($contact_data) > 0) {
            $payment['credit_cid'] = $contact_data[0]['cid'];
        }
        // Save the payment
        $payment = payment_save($payment);
        $count++;
    }
    message_register("Successfully imported {$count} payment(s)");
    return crm_url('payments');
}
/**
 * Handle paypal payment import request.
 *
 * @return The url to display on completion.
 */
function command_paypal_payment_import()
{
    if (!user_access('payment_edit')) {
        error_register('User does not have permission: payment_edit');
        return crm_url('payments');
    }
    if (!array_key_exists('payment-file', $_FILES)) {
        error_register('No payment file uploaded');
        return crm_url('payments&tab=import');
    }
    $csv = file_get_contents($_FILES['payment-file']['tmp_name']);
    $data = csv_parse($csv);
    $count = 0;
    foreach ($data as $row) {
        // Skip transactions that have already been imported
        $payment_opts = array('filter' => array('confirmation' => $row['Transaction ID']));
        $data = payment_data($payment_opts);
        if (count($data) > 0) {
            continue;
        }
        // Parse value
        $value = payment_parse_currency($row['Gross']);
        // Create payment object
        $payment = array('date' => date('Y-m-d', strtotime($row['Date'])), 'code' => $value['code'], 'value' => $value['value'], 'description' => $row['Name'] . ' Paypal Payment', 'method' => 'paypal', 'confirmation' => $row['Transaction ID'], 'notes' => $row['Item Title'], 'paypal_email' => $row['From Email Address']);
        // Check if the paypal email is linked to a contact
        $opts = array('filter' => array('paypal_email' => $row['From Email Address']));
        $contact_data = paypal_payment_contact_data($opts);
        if (count($contact_data) > 0) {
            $payment['credit_cid'] = $contact_data[0]['cid'];
        }
        // Save the payment
        $payment = payment_save($payment);
        $count++;
    }
    message_register("Successfully imported {$count} payment(s)");
    return crm_url('payments');
}