<?php
/**
 * phpaga
 *
 * add material
 *
 * Add materials to a project
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: add_material.php 726 2006-08-05 22:34:30Z florian $
 *
 * Copyright (c) 2005, 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"];

$result = phpaga_project_is_member($prj_id, $_SESSION["auth_user"]["pe_id"], $ismember);
if ($result != PHPAGA_ERR_NONE)
{
    phpaga_error($result);
}

$can_manage = phpaga_project_canmanage($prj_id);

/* user can add materials 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 materials 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")
{


    $matInfo["prj_id"] = $prj_id;
    $matInfo["prod_id"] = $_REQUEST["prod_id"];
    $matInfo["mat_name"] = $_REQUEST["mat_name"];

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

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

    $matInfo["mat_qty"] = $_REQUEST["mat_qty"];
    $matInfo["mat_desc"] = $_REQUEST["mat_desc"];
    $matInfo["mat_price"] = $_REQUEST["mat_price"];
    $matInfo["curr_id"] = $_REQUEST["curr_id"];
    $matInfo["mat_prodcode"] = $_REQUEST["mat_prodcode"];

    $result = phpaga_materials_validdata($matInfo);

    if (phpaga_errorhappened($result))
        $status = phpaga_returnerror($result);
    else
    {
        $add_result = phpaga_materials_add($matInfo);

        if (phpaga_errorhappened($add_result))
        {
            $status = phpaga_returnerror($phpaga_error[$add_result]);

        }
        else
        {
            $status =
                phpaga_returnmessage(_("Information"),
                                     _("The material has been inserted successfully."));

            /* set default values for next material */

            $matInfo["prod_id"] = "";
            $matInfo["mat_name"] = "";
            $matInfo["mat_qty"] = "";
            $matInfo["mat_desc"] = "";
            $matInfo["mat_price"] = "";
            $matInfo["curr_id"] = PHPAGA_DEFAULTCURRENCY;
            $matInfo["mat_prodcode"] = "";

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

    $matInfo["prod_id"] = "";
    $matInfo["mat_name"] = "";
    $matInfo["mat_qty"] = "";
    $matInfo["pe_id"] = $_SESSION["auth_user"]["pe_id"];
    $matInfo["prj_id"] = $prj_id;
    $matInfo["mat_id"] = "";
    $matInfo["mat_desc"] = "";
    $matInfo["mat_price"] = "";
    $matInfo["curr_id"] = PHPAGA_DEFAULTCURRENCY;
    $matInfo["mat_prodcode"] = "";

}

$result = phpaga_projects_getInfo($prj_id, $prjInfo);
if (phpaga_errorhappened($result))
{
        phpaga_header(array("menuitem" => "core"));
        phpaga_error($result);
        phpaga_footer();
        exit;
}

phpaga_header(array("menuitem" => "core"));

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

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

if (!$can_manage)
{
    $result = phpaga_persons_getInfo(
        $_SESSION["auth_user"]["pe_id"],
        $peInfo);

    $select_user[$peInfo["pe_id"]] =
        $peInfo["pe_person"]
        ." ("
        .$_SESSION["auth_user"]["usr_login"]
        .")";
}
else
{
    $userInfo["prj_id"] = $prj_id;
    $result = phpaga_user_search($users, $userInfo);

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

phpaga_currencies_getCurrencyList ($select_currency);

$tpl = new psmarty;

$tpl->assign("prjInfo", $prjInfo);
$tpl->assign("matinfo", $matInfo);

$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("FORM_ACTION", basename($_SERVER["PHP_SELF"]));

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

phpaga_footer();

?>