<?php
/**
 * phpaga
 *
 * add project
 *
 * Add projects
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: add_project.php 726 2006-08-05 22:34:30Z florian $
 *
 * 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.
 */

include_once ("./config.php");

phpaga_user_pageperm(PHPAGA_PERM_MANAGE_PROJECTS);

$status = null;
$new_prj_id = null;
$new_cpn_id = null;

$userInfo = array();
$persons = array();
$prjmanage = false;
$nohide = false;

$prjInfo = array();
$prjInfo["opcatlist"] = array();

$cpnInfo = array("cpn_name" => "",
                 "cpn_address" => "",
                 "cpn_address2" => "",
                 "cpn_city" => "",
                 "cpn_region" => "",
                 "cpn_zip" => "",
                 "ctr_id" => PHPAGA_DEFAULTCOUNTRY,
                 "ccat_id" => "",
                 "cpn_taxnr" => "",
                 "cpn_phone" => "",
                 "cpn_fax" => "",
                 "cpn_url" => "",
                 "cpn_email" => "",
                 "cpn_defaultpayterm" => "");

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

    if (!isset($_POST["new_cpn"]) || ($_POST["new_cpn"] != '1')) {
        $prjInfo["new_cpn"] = 0;
    } else {

        $prjInfo["new_cpn"] = 1;

        $cpnInfo["cpn_name"] = $_POST["cpn_name"];
        $cpnInfo["cpn_address"] = $_POST["cpn_address"];
        $cpnInfo["cpn_address2"] = $_POST["cpn_address2"];
        $cpnInfo["cpn_city"] = $_POST["cpn_city"];
        $cpnInfo["cpn_region"] = $_POST["cpn_region"];
        $cpnInfo["cpn_zip"] = $_POST["cpn_zip"];
        $cpnInfo["ctr_id"] = $_POST["ctr_id"];
        $cpnInfo["ccat_id"] = $_POST["ccat_id"];
        $cpnInfo["cpn_taxnr"] = $_POST["cpn_taxnr"];
        $cpnInfo["cpn_phone"] = $_POST["cpn_phone"];
        $cpnInfo["cpn_fax"] = $_POST["cpn_fax"];
        $cpnInfo["cpn_url"] = $_POST["cpn_url"];
        $cpnInfo["cpn_email"] = $_POST["cpn_email"];
        $cpnInfo["cpn_defaultpayterm"] = $_POST["cpn_defaultpayterm"];
        $cpnInfo["pe_id"] = $_SESSION["auth_user"]["pe_id"];

        $result = phpaga_companies_validdata($cpnInfo);

        if (phpaga_errorhappened($result))
            $status = phpaga_returnerror($result);
        else
        {
            $add_result = phpaga_companies_add($cpnInfo, $new_cpn_id);

            if (phpaga_errorhappened($add_result))
                $status = phpaga_returnerror($add_result);
        }

    }

    if (!strlen($status)) {
        $prjInfo["prjcat_id"] = $_REQUEST["prjcat_id"];
        $prjInfo["prj_desc"] = $_REQUEST["prj_desc"];
        $prjInfo["prj_title"] = $_REQUEST["prj_title"];
        $prjInfo["prj_status"] = $_REQUEST["prj_status"];
        $prjInfo["prj_priority"] = $_REQUEST["prj_priority"];
        $prjInfo["prj_startdate"] = $_REQUEST["prj_startdate"];
        $prjInfo["prj_estimatedend"] = $_REQUEST["prj_estimatedend"];
        $prjInfo["prj_deadline"] = $_REQUEST["prj_deadline"];

        if (strlen($new_cpn_id) && is_numeric($new_cpn_id)) {
            $prjInfo["cpn_id"] = $new_cpn_id;
            $prjInfo["new_cpn"] = 0;

            $cpnInfo = array("cpn_name" => "",
                             "cpn_address" => "",
                             "cpn_address2" => "",
                             "cpn_city" => "",
                             "cpn_region" => "",
                             "cpn_zip" => "",
                             "ctr_id" => PHPAGA_DEFAULTCOUNTRY,
                             "ccat_id" => "",
                             "cpn_taxnr" => "",
                             "cpn_phone" => "",
                             "cpn_fax" => "",
                             "cpn_url" => "",
                             "cpn_email" => "",
                             "cpn_defaultpayterm" => "");

        } elseif ($_REQUEST["cpn_id"] != -10)
            $prjInfo["cpn_id"] = $_REQUEST["cpn_id"];
        else
            $prjInfo["cpn_id"] = null;

        $prjInfo["quot_id"] = $_REQUEST["quot_id"];
        $prjInfo["cpn_id_issuebill"] = $_REQUEST["cpn_id_issuebill"];
        $prjInfo["pe_id"] = $_SESSION["auth_user"]["pe_id"];
        $prjInfo["prj_id"] = 0;
        $prjInfo["prj_prj_id"] = $_REQUEST["prj_prj_id"];
        $prjInfo["prj_planned_manhours"] = $_REQUEST["prj_planned_manhours"];
        $prjInfo["prj_planned_cost_manhours"] = $_REQUEST["prj_planned_cost_manhours"];
        $prjInfo["prj_planned_cost_material"] = $_REQUEST["prj_planned_cost_material"];
        $prjInfo["prj_billabletype"] = $_REQUEST["prj_billabletype"];
        $prjInfo["prj_manager_pe_id"] = $_REQUEST["prj_manager_pe_id"];

        $oid = null;
        $orate = null;
        $otitle = null;
        $ocolor = null;

        $tmp_opcat = array();
        phpaga_operations_getOpcats($tmp_opcat, null, true);

        if (isset($_REQUEST["opcat_rates"]) && is_array($_REQUEST["opcat_rates"])) {
            foreach ($_REQUEST["opcat_rates"] as $oid => $orate) {

                if (isset($tmp_opcat[$oid])) {
                    $otitle = $tmp_opcat[$oid]["opcat_title"];
                    $ocolor = $tmp_opcat[$oid]["opcat_color"];
                }

                $prjInfo["opcatlist"][] = array("opcat_id" => $oid,
                                                "opcat_title" => $otitle,
                                                "opcat_color" => $ocolor,
                                                "opcat_hourlyrate" => $orate);
            }

        }

        $result = phpaga_projects_validdata($prjInfo);

        if (phpaga_errorhappened($result))
            $status = phpaga_returnerror($result);
        else
        {
            $add_result = phpaga_projects_add($prjInfo, $new_prj_id);

            if (phpaga_errorhappened($add_result))
                $status = phpaga_returnerror($add_result);
            else
                header("Location: project.php?id=".$new_prj_id);
        }
    }
}
else
{

    /* this is the first invocation */

    /* fetch information about the latest project */
    phpaga_projects_getLastProject($t_prj_id);
    phpaga_projects_getInfo($t_prj_id, $t_prjInfo);

    /* if the last project's title was numeric then default the new
     * project's title to the next higher value
     */

    if (isset($t_prjInfo["prj_title"]) && (is_numeric($t_prjInfo["prj_title"])))
        $prjInfo["prj_title"] = $t_prjInfo["prj_title"] + 1;
    else
        $prjInfo["prj_title"] = "";

    $prjInfo["prj_id"] = 0;
    $prjInfo["prjcat_id"] = "";
    $prjInfo["prj_desc"] = "";
    $prjInfo["prj_status"] = PHPAGA_PROJECTSTATUS_OPEN;
    $prjInfo["prj_priority"] = "";
    $prjInfo["prj_startdate"] = date("Y-m-d");
    $prjInfo["prj_estimatedend"] = "";
    $prjInfo["prj_deadline"] = "";
    $prjInfo["cpn_id"] = "";
    $prjInfo["quot_id"] = "";
    $prjInfo["prj_prj_id"] = "";
    $prjInfo["prj_manager_pe_id"] = "";
    $prjInfo["prj_planned_manhours"] = "";
    $prjInfo["prj_planned_cost_manhours"] = "";
    $prjInfo["prj_planned_cost_material"] = "";
    $prjInfo["prj_billabletype"] = PHPAGA_BILLABLETYPE_YES;
    $prjInfo["new_cpn"] = false;

    phpaga_operations_getOpcats($prjInfo["opcatlist"]);

    if (isset($_REQUEST["id"]) && is_numeric($_REQUEST["id"])
        && isset($_REQUEST["action"]) && ($_REQUEST["action"] == ACTION_FROMQUOTATION))
    {
        /* create a bill from a quotation */

        $quotInfo["quot_id"] = $_REQUEST["id"];
        $result = phpaga_quotations_search($rows, $count, $quotInfo);

        if (($result == PHPAGA_ERR_NONE) && (count($rows) == 1))
        {
            $quotInfo = $rows[0];

            $prjInfo["cpn_id"] = (int)$quotInfo["cpn_id"];
            $prjInfo["quot_id"] = (int)$quotInfo["quot_id"];
            $prjInfo["prj_planned_cost_manhours"] = $quotInfo["quot_startsum"];
        }
    }
}

$nohide = ($prjInfo["new_cpn"] == 1);

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

phpaga_projects_getStatusList($select_prjstat);
phpaga_projects_getPriorityList($select_priority);

phpaga_companies_getCompanyList($select_cpn);
phpaga_arrayAddOption($select_cpn, PHPAGA_OPTION_SELECT);
phpaga_arrayAddOption($select_cpn, PHPAGA_OPTION_NEW, -10);

phpaga_projects_getCategoryList($select_prjcat);
phpaga_arrayAddOption($select_prjcat, PHPAGA_OPTION_SELECT);

phpaga_projects_getProjectList($select_parent_project);
phpaga_arrayAddOption($select_parent_project, PHPAGA_OPTION_NONE);

$select_cpn_issuebill = $select_cpn;
phpaga_arrayAddOption ($select_cpn_issuebill, PHPAGA_OPTION_NONE);

$s_userInfo = array("sort" => "person");
$select_persons = array();
$h_persons = array();

$result = phpaga_user_search($h_persons, $s_userInfo);
foreach ($h_persons as $person) {
    $select_persons[$person["pe_id"]] = sprintf("%s (%s)", $person["pe_name"], $person["usr_login"]);
}

phpaga_arrayAddOption($select_persons, PHPAGA_OPTION_SELECT);

phpaga_companies_getCategories($select_ccat);
phpaga_countries_getCountryList($select_ctr);

$tpl = new psmarty;

$tpl->assign("prjinfo", $prjInfo);
$tpl->assign("cpnInfo", $cpnInfo);
$tpl->assign("from_project", true);
$tpl->assign("select_cpn", $select_cpn);
$tpl->assign("select_cpn_issuebill", $select_cpn_issuebill);
$tpl->assign("select_prjcat", $select_prjcat);
$tpl->assign("select_prjstat", $select_prjstat);
$tpl->assign("select_priority", $select_priority);
$tpl->assign("select_prj_manager", $select_persons);
$tpl->assign("select_parent_project", $select_parent_project);
$tpl->assign("select_prj_billabletype", $phpaga_projects_billabletype);
$tpl->assign("select_ctr", $select_ctr);
$tpl->assign("select_ccat", $select_ccat);
$tpl->assign("STATUSMSG", $status);
$tpl->assign("FORM_ACTION", basename ($_SERVER["PHP_SELF"]));

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

phpaga_footer ();

?>