<?php
/**
 * phpaga
 *
 * Currency management functionality.
 *
 * This file contains the necessary routines to manage currencies.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: currencies.php 653 2005-10-03 21:53:26Z worf $
 *
 * 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.
 *
 * @param array $currList  Array containing currencies
 *
 * @return int   $result  A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_currencies_getCurrencyList (&$currList)
{

    $currList = array();

    phpaga_db_connect($db);

    $query = "SELECT curr_id, curr_name ".
        "FROM currencies ".
        "ORDER BY curr_name";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    while ($qrow = $result->fetchRow ())
    {
        phpaga_sanitizeArray ($qrow);
        $currList[$qrow["curr_id"]] = $qrow["curr_name"];
    }

    $result->free ();
    return PHPAGA_ERR_NONE;

}


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

function phpaga_currencies_getCurrencyInfo (&$currInfo,
                                            $currID)
{
    $currInfo = "";

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

    phpaga_db_connect($db);

    $query = "SELECT curr_id, curr_name, curr_decimals ".
        "FROM currencies ".
        "WHERE curr_id = $currID ";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() < 1)
        return PHPAGA_ERR_DB_NO_ROWS;

    if ($qrow = $result->fetchRow ())
    {
        phpaga_sanitizeArray ($qrow);
        $currInfo = $qrow;
    }

    $result->free();
    return PHPAGA_ERR_NONE;

}


/**
 * Loads all currencies into an array.
 *
 * @param array  $currInfo   Array containing the results
 *
 * @return int   $result       A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

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

    phpaga_db_connect($db);

    $query = "SELECT curr_id, curr_name, curr_decimals ".
        "FROM currencies ORDER BY curr_name ";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() < 1)
        return PHPAGA_ERR_DB_NO_ROWS;

    while ($qrow = $result->fetchRow())
    {
        phpaga_sanitizeArray($qrow);
        $currInfo[] = $qrow;
    }

    $result->free ();
    return PHPAGA_ERR_NONE;
}


/**
 * Add a new currency.
 *
 * @param  string  $curr_name      Currency to be added
 * @param  int     $curr_decimals  decimals
 *
 * @return int     $result        A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_currencies_addCurrency ($curr_name, $curr_decimals)
{

    $currID = "";

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

    phpaga_db_connect($db);

    $fields = array("curr_id" => $db->nextId("currencies_curr_id"),
                    "curr_name" => $curr_name,
                    "curr_decimals" => $curr_decimals);

    $result = $db->autoExecute("currencies", $fields, DB_AUTOQUERY_INSERT);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;
    else
        return PHPAGA_ERR_NONE;
}


/**
 * Delete a currency.
 *
 * @param  int     $curr_id      Currency to be deleted
 *
 * @return int     $result       A result code.
 *
 * @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 PHPAGA_ERR_CURR_INVALIDID;

    phpaga_db_connect($db);

    /* first check, if this curr is needed by any table relationship
     * if it is not needed, then delete it, otherwise don't */

    $query = "SELECT curr_id FROM bills ".
        "WHERE curr_id = ".$db->quoteSmart($curr_id);

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        return PHPAGA_ERR_DEL_DEPENDENCIES;

    $query = "SELECT curr_id FROM expenses ".
        "WHERE curr_id = ".$db->quoteSmart($curr_id).
        " OR curr_id_foreign = ".$db->quoteSmart($curr_id);

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        return PHPAGA_ERR_DEL_DEPENDENCIES;

    $query = "DELETE FROM currencies ".
        "WHERE curr_id = ".$db->quoteSmart($curr_id);

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;
    else
        return PHPAGA_ERR_NONE;
}

?>