<?php
/**
 * phpaga
 *
 * add project
 *
 * Add projects
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id$
 *
 * 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' => null,
                 'cpn_address' => null,
                 'cpn_address2' => null,
                 'cpn_city' => null,
                 'cpn_region' => null,
                 'cpn_zip' => null,
                 'ctr_id' => PHPAGA_DEFAULTCOUNTRY,
                 'ccat_id' => null,
                 'cpn_taxnr' => null,
                 'cpn_phone' => null,
                 'cpn_fax' => null,
                 'cpn_url' => null,
                 'cpn_email' => null,
                 'cpn_defaultpayterm' => null);

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 (PhPagaError::isError($result))
            $status = $result->getFormattedMessage();
        else {

            $new_cpn_id = phpaga_companies_add($cpnInfo);

            if (PhPagaError::isError($new_cpn_id))
                $status = $new_cpn_id->getFormattedMessage();
        }
    }

    $prjInfo['prjcat_id'] = $REQUEST_DATA['prjcat_id'];
    $prjInfo['prj_desc'] = $REQUEST_DATA['prj_desc'];
    $prjInfo['prj_title'] = $REQUEST_DATA['prj_title'];
    $prjInfo['prj_status'] = $REQUEST_DATA['prj_status'];
    $prjInfo['prj_priority'] = $REQUEST_DATA['prj_priority'];
    $prjInfo['prj_startdate'] = $REQUEST_DATA['prj_startdate'];
    $prjInfo['prj_estimatedend'] = $REQUEST_DATA['prj_estimatedend'];
    $prjInfo['prj_deadline'] = $REQUEST_DATA['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' => null,
                         'cpn_address' => null,
                         'cpn_address2' => null,
                         'cpn_city' => null,
                         'cpn_region' => null,
                         'cpn_zip' => null,
                         'ctr_id' => PHPAGA_DEFAULTCOUNTRY,
                         'ccat_id' => null,
                         'cpn_taxnr' => null,
                         'cpn_phone' => null,
                         'cpn_fax' => null,
                         'cpn_url' => null,
                         'cpn_email' => null,
                         'cpn_defaultpayterm' => null);

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

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

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

    $tmp_opcat = phpaga_operations_getOpcats(null, true);

    if (isset($REQUEST_DATA['opcat_rates']) && is_array($REQUEST_DATA['opcat_rates'])) {
        foreach ($REQUEST_DATA['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);
        }
    }

    if (!strlen($status)) {
        $result = phpaga_projects_validdata($prjInfo);

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

            $new_prj_id = phpaga_projects_add($prjInfo);

            if (PhPagaError::isError($new_prj_id))
                $status = $new_prj_id->getFormattedMessage();
            else {
                header('Location: project.php?id='.$new_prj_id);
                exit;
            }
        }
    }

} else {

    $prjInfo['prj_title'] = null;

    $t_prj_id = phpaga_projects_getLastProject();

    if (!is_null($t_prj_id) && is_numeric($t_prj_id)) {
        $t_prjInfo = phpaga_projects_getInfo($t_prj_id);

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

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

    $prjInfo['opcatlist'] = phpaga_operations_getOpcats();

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

        $quotInfo['quot_id'] = $REQUEST_DATA['id'];
        $rows = phpaga_quotations_search($count, $quotInfo);

        if (!PhPagaError::isError($rows) && (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('menuitem' => 'core'));

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

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

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

$select_parent_project = phpaga_projects_getProjectList();
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();

$h_persons = phpaga_user_search($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);

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

$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();

?>
