<?php
/**
 * phpaga
 *
 * Currency management functionality.
 *
 * This file contains the necessary routines to manage currencies.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id$
 *
 * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *    * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *
 *    * Neither the name of Florian Lanthaler nor the names of his
 *      contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * Gets a list of currencies from the database and returns them as the
 * content of an array.
 *
 * @return array  $currList  Array containing currencies. A PhPagaError object on failure.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_currencies_getCurrencyList()
{

    $currList = array();

    $d = new PhPagaDbData;
    $r = $d->queryAll('SELECT curr_id, curr_name FROM currencies ORDER BY curr_name');

    if (PhPagaError::isError($r))
        return $r;

    foreach ($r as $qrow) {
        phpaga_sanitizeArray ($qrow);
        $currList[$qrow['curr_id']] = $qrow['curr_name'];
    }

    return $currList;
}


/**
 * Gets the information of a currency.
 *
 * @param  int    $currID    Currency's ID
 *
 * @return array  $currInfo  Currency information. A PhPagaError object on failure
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_currencies_getCurrencyInfo($currID)
{

    if (!isset($currID) || !strlen($currID) || !is_numeric($currID))
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_CURRENCY_INVALIDID);

    $d = new PhPagaDbData;
    $r = $d->queryRow('SELECT curr_id, curr_name, curr_decimals FROM currencies WHERE curr_id = ?', $currID);

    if (PhPagaError::isError($r))
        return $r;

    phpaga_sanitizeArray ($r);

    return $r;
}


/**
 * Loads all currencies into an array.
 *
 * @return array  $result    Array with currencies. A PhPagaError object on failure.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_currencies_getCurrencies()
{
    $currInfo = array();

    $d = new PhPagaDbData;

    $r = $d->queryAll('SELECT curr_id, curr_name, curr_decimals FROM currencies ORDER BY curr_name');

    if (PhPagaError::isError($r))
        return $r;

    foreach ($r as $qrow) {
        phpaga_sanitizeArray($qrow);
        $currInfo[] = $qrow;
    }

    return $currInfo;
}


/**
 * Add a new currency.
 *
 * @param  string  $curr_name      Currency to be added
 * @param  int     $curr_decimals  decimals
 *
 * @return int     $id             New currency's Id. A PhPagaError object on failure.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_currencies_addCurrency($curr_name, $curr_decimals)
{

    if (!isset($curr_name) || !strlen ($curr_name))
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_CURR_INVALIDNAME);
    elseif (!isset($curr_decimals) || !is_numeric($curr_decimals))
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_CURR_INVALIDDECIMALS);

    $d = new PhPagaDbData;

    $id = $d->nextId('currencies_curr_id');

    $fields = array('curr_id' => $id,
                    'curr_name' => $curr_name,
                    'curr_decimals' => $curr_decimals);

    $result = $d->insert('currencies', $fields);

    if (PhPagaError::isError($result))
        return $result;

    return $id;
}


/**
 * Delete a currency.
 *
 * @param  int     $curr_id    Currency to be deleted
 *
 * @return bool    $result     True if successful, a PhPagaError otherwise.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_currencies_deleteCurrency ($curr_id)
{
    if (!isset($curr_id) || !is_numeric($curr_id))
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_CURR_INVALIDID);

    $d = new PhPagaDbData;

    /* First check, if this curr is needed by any table relationship,
     * delete it only if it is not needed. */

    $n = $d->queryOneVal('SELECT COUNT(curr_id) FROM bills WHERE curr_id = ?', $curr_id);

    if (PhPagaError::isError($n))
        return $n;

    if ($n > 0)
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_DEL_DEPENDENCIES);

    $n = $d->queryOneVal('SELECT COUNT(curr_id) FROM quotations WHERE curr_id = ?', $curr_id);

    if (PhPagaError::isError($n))
        return $n;

    if ($n > 0)
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_DEL_DEPENDENCIES);

    $n = $d->queryOneVal('SELECT COUNT(curr_id) FROM expenses WHERE curr_id = ? OR curr_id_foreign = ?', array($curr_id, $curr_id));

    if (PhPagaError::isError($n))
        return $n;

    if ($n > 0)
        return PhPagaError::raiseErrorByCode(PHPAGA_ERR_DEL_DEPENDENCIES);

    $result = $d->execute('DELETE FROM currencies WHERE curr_id = ?', $curr_id);

    if (PhPagaError::isError($result))
        return $result;

    return true;
}

?>