<?php
/**
 * phpaga
 *
 * Expenses functionality.
 *
 * This file contains the necessary routines to manage expenses.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: expenses.php 653 2005-10-03 21:53:26Z worf $
 *
 * Copyright (c) 2003, 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.
 */

/**
 * Checks if all fields of an expense contain valid values.
 *
 *
 * @param  array  $expInfo  Array containing the expense's information.
 *
 * @return array  $errors   Array containing error codes
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.2
 */

function phpaga_expenses_validdata ($expInfo)
{

    $errors = array();

    if (!is_array($expInfo))
        $expInfo = array($expInfo);

    if (!phpaga_checkdate($expInfo["exp_date"]))
        $errors[] = PHPAGA_ERR_INVALID_DATEFORMAT;

    if (!isset($expInfo["exp_desc"]) || !strlen(trim($expInfo["exp_desc"])))
        $errors[] = PHPAGA_ERR_EXP_DESC;

    if (!isset($expInfo["prj_id"]) || !is_numeric($expInfo["prj_id"]))
        $errors[] = PHPAGA_ERR_PROJECT_INVALIDID;

    if (!isset($expInfo["pe_id"]) || !is_numeric($expInfo["pe_id"]))
        $errors[] = PHPAGA_ERR_PERSON_INVALIDID;

    if (!isset($expInfo["curr_id"]) || !is_numeric($expInfo["curr_id"]))
        $errors[] = PHPAGA_ERR_CURRENCY_INVALIDID;

    if (isset($expInfo["curr_id_foreign"])
        && strlen($expInfo["curr_id_foreign"])
        && !is_numeric($expInfo["curr_id_foreign"]))
        $errors[] = PHPAGA_ERR_CURRENCY_INVALIDID;

    if (!isset($expInfo["exp_sum"]) || !is_numeric($expInfo["exp_sum"]))
        $errors[] = PHPAGA_ERR_EXP_INVALIDSUM;

    if (isset($expInfo["exp_sum_foreign"])
        && strlen($expInfo["exp_sum_foreign"])
        && !is_numeric($expInfo["exp_sum_foreign"]))
        $errors[] = PHPAGA_ERR_EXP_INVALIDSUM;

    if (isset($expInfo["exp_exchange_rate"])
        && strlen($expInfo["exp_exchange_rate"])
        && !is_numeric($expInfo["exp_exchange_rate"]))
        $errors[] = PHPAGA_ERR_EXP_INVALIDEXCHANGERATE;

    return $errors;
}

/**
 * Adds an expense to the database
 *
 * @param array  $expInfo Array containing the expense information
 *
 * @return int   $result  A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.2
 */

function phpaga_expenses_add ($expInfo)
{

    $result = phpaga_expenses_validdata($expInfo);

    if (count($result) > 0)
        return $result;

    phpaga_db_connect($db);

    if (!isset($expInfo["exp_sum_foreign"]) || !strlen($expInfo["exp_sum_foreign"]))
        $expInfo["exp_sum_foreign"] = NULL;

    if (!isset($expInfo["curr_id_foreign"]) || !strlen($expInfo["curr_id_foreign"]))
        $expInfo["curr_id_foreign"] = NULL;

    if (!isset($expInfo["exp_exchange_rate"]) || !strlen($expInfo["exp_exchange_rate"]))
        $expInfo["exp_exchange_rate"] = NULL;

    $fields = array("exp_id" => $db->nextId("expenses_exp_id"),
                    "exp_date" => $expInfo["exp_date"],
                    "pe_id" => $expInfo["pe_id"],
                    "prj_id" => $expInfo["prj_id"],
                    "exp_desc" => $expInfo["exp_desc"],
                    "exp_sum" => $expInfo["exp_sum"],
                    "curr_id" => $expInfo["curr_id"],
                    "exp_sum_foreign" => $expInfo["exp_sum_foreign"],
                    "curr_id_foreign" => $expInfo["curr_id_foreign"],
                    "exp_exchange_rate" =>$expInfo["exp_exchange_rate"]
        );

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

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


/**
 * Fetches the expenses according to the search parameters in expInfo.
 *
 * @param array  $expInfo        Array containing search parameters
 * @param array  $expenses       Array containing the matching expenses
 *
 * @return int   $result  A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.2
 */

function phpaga_expenses_getexpenses ($expInfo, &$expenses)
{

    $expenses = array();
    $where = array();

    $db = "";
    $result = "";

    phpaga_db_connect($db);

    if (!is_array($expInfo))
        $expInfo = array();

    $query = "SELECT exp_id, e.pe_id, pe_firstname, pe_lastname, exp_date, ".
        "e.prj_id, prj_title, exp_desc, exp_sum, e.curr_id, c.curr_name, ".
        "exp_sum_foreign, e.curr_id_foreign, c2.curr_name AS curr_name_foreign, ".
        "exp_exchange_rate, e.bill_id ".
        "FROM expenses e ".
        "LEFT JOIN persons p ON p.pe_id = e.pe_id ".
        "LEFT JOIN currencies c ON c.curr_id = e.curr_id ".
        "LEFT JOIN projects s ON s.prj_id = e.prj_id ".
        "LEFT JOIN currencies c2 ON c2.curr_id = e.curr_id_foreign ";

    if (isset($expInfo["pe_id"]) && is_numeric($expInfo["pe_id"]))
        $where[] = "e.pe_id = ".$db->quoteSmart($expInfo["pe_id"]);

    if (isset($expInfo["prj_id"]) && is_numeric($expInfo["prj_id"]))
        $where[] = "e.prj_id = ".$db->quoteSmart($expInfo["prj_id"]);

    if (isset($expInfo["curr_id"]) && is_numeric($expInfo["curr_id"]))
        $where[] = "e.curr_id = ".$db->quoteSmart($expInfo["curr_id"]);

    if (isset($expInfo["curr_id_foreign"]) && is_numeric($expInfo["curr_id_foreign"]))
        $where[] = "e.curr_id_foreign = ".$db->quoteSmart($expInfo["curr_id_foreign"]);

    if (count($where) > 0)
        $query .=  "WHERE ".implode (" AND ", $where)." ";

    $query .= "ORDER BY e.exp_date ";

    $result = $db->query($query);
    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    while ($qrow = $result->fetchRow())
    {
        phpaga_sanitizeArray($qrow);

        $qrow["exp_sum"] = (float) $qrow["exp_sum"];
        $qrow["exp_sum_foreign"] = (float) $qrow["exp_sum_foreign"];
        $qrow["exp_exchange_rate"] = (float) $qrow["exp_exchange_rate"];

        $qrow["pe_person"] =
            phpaga_persons_formatname ($qrow["pe_lastname"],
                                       $qrow["pe_firstname"]);
        $expenses[] = $qrow;
    }

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

?>