<?php
/**
 * phpaga
 *
 * add expense
 *
 * Add expenses to a project
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: add_expense.php 857 2007-09-19 20:12:30Z florian $
 *
 * 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.
 */

include_once("./config.php");

$status = "";
$userInfo = array();
$prjInfo = array();
$users = array();
$select_user = array();
$select_currency = array();
$ismember = false;

if (!isset($_REQUEST["prj_id"]) || !is_numeric($_REQUEST["prj_id"]))
{
    phpaga_header(array("menuitem" => "core"));
    phpaga_error(_("Invalid project ID"));
    phpaga_footer();
    exit;
}

$prj_id = $_REQUEST["prj_id"];

$ismember = phpaga_project_is_member($prj_id, $_SESSION["auth_user"]["pe_id"]);
$can_manage = phpaga_project_canmanage($prj_id);

/* user can add expenses if she is a project member, if she has the
 * right to manage other people's projects, or when this is her
 * project */

if (!$ismember && !$can_manage)
{
    phpaga_header(array("menuitem" => "core"));
    phpaga_message (_("No access"),
                    _("You can't file expenses for this project because you are not a project member."));
    phpaga_footer();
    exit;
}

/* if we're invoked by post, we have to save the modifications */
if ($_SERVER["REQUEST_METHOD"] == "POST")
{

    $expInfo["prj_id"] = $prj_id;

    /* user can file expenses for other users if she has the right to
     * manage other people's projects or when this is her project */

    if (!$can_manage)
        $expInfo["pe_id"] = $_SESSION["auth_user"]["pe_id"];
    else
        $expInfo["pe_id"] = $_REQUEST["pe_id"];

    $expInfo["exp_date"] = $_REQUEST["exp_date"];
    $expInfo["exp_desc"] = $_REQUEST["exp_desc"];
    $expInfo["exp_sum"] = $_REQUEST["exp_sum"];
    $expInfo["curr_id"] = $_REQUEST["curr_id"];
    $expInfo["exp_sum_foreign"] = $_REQUEST["exp_sum_foreign"];
    $expInfo["curr_id_foreign"] = $_REQUEST["curr_id_foreign"];
    $expInfo["exp_exchange_rate"] = $_REQUEST["exp_exchange_rate"];

    $result = phpaga_expenses_validdata($expInfo);

    if (PhPagaError::isError($result))
        $status = $result->getFormattedMessage();
    else {

        $add_result = phpaga_expenses_add($expInfo);

        if (PhPagaError::isError($add_result))
            $status = $add_result->getFormattedMessage();
        else {
            $status = phpaga_returnmessage(_("Information"), _("The expense has been inserted successfully."));

            $expInfo["exp_date"] = date("Y-m-d");
            $expInfo["exp_desc"] = "";
            $expInfo["exp_sum"] = "";
            $expInfo["curr_id"] = PHPAGA_DEFAULTCURRENCY;
            $expInfo["exp_sum_foreign"] = "";
            $expInfo["curr_id_foreign"] = "";
            $expInfo["exp_exchange_rate"] = "";

        }
    }
}
else
{
    /* this is first invocation, so we have to read from the GET vars */

    $expInfo["exp_date"] = date("Y-m-d");
    $expInfo["pe_id"] = $_SESSION["auth_user"]["pe_id"];
    $expInfo["prj_id"] = $prj_id;
    $expInfo["exp_id"] = "";
    $expInfo["exp_desc"] = "";
    $expInfo["exp_sum"] = "";
    $expInfo["curr_id"] = PHPAGA_DEFAULTCURRENCY;
    $expInfo["exp_sum_foreign"] = "";
    $expInfo["curr_id_foreign"] = "";
    $expInfo["exp_exchange_rate"] = "";

}

$prjInfo = phpaga_projects_getInfo($prj_id);
if (PhPagaError::isError($prjInfo)) {
        phpaga_header(array("menuitem" => "core"));
        $prjInfo->printMessage();
        phpaga_footer();
        exit;
}

phpaga_header(array("calendar" => true, "menuitem" => "core"));

$duration = phpaga_projects_getDuration($prjInfo["prj_id"]);

/* The user can file expenses for other users if she has the right to
 * manage other people's projects or when this is her project */

if (!$can_manage) {

    $peInfo = phpaga_persons_getInfo($_SESSION["auth_user"]["pe_id"]);

    $select_user[$peInfo["pe_id"]] = sprintf("%s (%s)",
                                             $peInfo["pe_person"],
                                             $_SESSION["auth_user"]["usr_login"]);
} else {

    $userInfo["prj_id"] = $prj_id;
    $users = phpaga_user_search($userInfo);

    foreach ($users as $u)
        $select_user[$u["pe_id"]] = $u["pe_name"]." (".$u["usr_login"].")";
}

$select_currency = phpaga_currencies_getCurrencyList();

$select_currency_foreign = $select_currency;
phpaga_arrayAddOption ($select_currency_foreign, PHPAGA_OPTION_NONE);

$tpl = new psmarty;

$tpl->assign("prjInfo", $prjInfo);
$tpl->assign("expinfo", $expInfo);
$tpl->assign("HOURS_SO_FAR", $duration);
$tpl->assign("STATUSMSG", $status);
$tpl->assign("is_owner_or_manager", $can_manage);
$tpl->assign("select_person", $select_user);
$tpl->assign("select_curr", $select_currency);
$tpl->assign("select_curr_foreign", $select_currency_foreign);
$tpl->assign("FORM_ACTION", basename($_SERVER["PHP_SELF"]));

$tpl->display("edit_expense.tpl.html");

phpaga_footer();

?>