<?php
/**
 * phpaga
 *
 * Print operations
 *
 * Print operations matching query options to pdf file
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: operations_pdf.php 595 2005-08-21 00:19:53Z worf $
 *
 * 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");
include_once(PHPAGA_LIBPATH."pdf.php");

$list_operations = "";

/* define which fields to print in the listing and which not if a parameter
 * is set it will not be printed in the listing (as the parameters would
 * always be the same in every row) */

$show = array("date" => _("Date"),
              "duration" => _("Duration"));

$image_args = "";           # arguments to opcat stats image

$search_params_pdf = array();

/* first collect all query parameters */

/* company id */
if (isset ($_REQUEST["cpn_id"]) && is_numeric ($_REQUEST["cpn_id"]))
{
    $opInfo["cpn_id"] = $_REQUEST["cpn_id"];
    $image_args .= "&cpn_id=".$_REQUEST["cpn_id"];

    $result = phpaga_companies_getInfo ($_REQUEST["cpn_id"], $cpnInfo);
    if ($result == PHPAGA_ERR_NONE)
        $search_params_pdf[] = array("param" => _("Company"),
                                     "value" => $cpnInfo["cpn_name"]);
}
else
{
    $opInfo["cpn_id"] = "";
}

/* id of the company issuing the bills */
if (isset($_REQUEST["cpn_id_issuebill"]) && is_numeric($_REQUEST["cpn_id_issuebill"]))
{
    $opInfo["cpn_id_issuebill"] = $_REQUEST["cpn_id_issuebill"];

    $result = phpaga_companies_getInfo ($_REQUEST["cpn_id_issuebill"], $cpnInfo);
    if ($result == PHPAGA_ERR_NONE)
        $search_params_pdf[] = array("param" => _("Company issuing the bill"),
                                     "value" => $cpnInfo["cpn_name"]);
}
else
{
    $opInfo["cpn_id_issuebill"] = "";
}

/* project id */
if (isset ($_REQUEST["prj_id"]) && is_numeric ($_REQUEST["prj_id"]))
{
    $opInfo["prj_id"] = $_REQUEST["prj_id"];
    $image_args .= "&prj_id=".$_REQUEST["prj_id"];

    $result = phpaga_projects_getInfo ($_REQUEST["prj_id"], $prjInfo);
    if ($result == PHPAGA_ERR_NONE)
        $search_params_pdf[] = array("param" => _("Project"),
                                     "value" => $prjInfo["prj_title"]);
}
else
{
    $opInfo["prj_id"] = "";
    $show["project"] = ("Project");
}

/* person id */
if (isset ($_REQUEST["pe_id"]) && is_numeric ($_REQUEST["pe_id"]))
{
    $opInfo["pe_id"] = $_REQUEST["pe_id"];
    $image_args .= "&pe_id=".$_REQUEST["pe_id"];

    $result = phpaga_persons_getInfo ($_REQUEST["pe_id"], $peInfo);
    if ($result == PHPAGA_ERR_NONE)
        $search_params_pdf[] = array("param" => _("Person"),
                                     "value" => $peInfo["pe_person"]);
}
else
{
    $opInfo["pe_id"] = "";
    $show["person"] = ("Person");
}

/* project status */
if (isset($_REQUEST["prj_status"]) && is_numeric($_REQUEST["prj_status"]))
{
    $opInfo["prj_status"] = $_REQUEST["prj_status"];

    $result = phpaga_projects_getStatusList($prjstatList);
    if (($result == PHPAGA_ERR_NONE) && isset($prjstatList[$_REQUEST["prj_status"]]))
        $search_params_pdf[] = array("param" => _("Project status"),
                                     "value" => $prjstatList[$_REQUEST["prj_status"]]);
}
else
{
    $opInfo["prj_status"] = "";
}

/* project category id */
if (isset($_REQUEST["prjcat_id"]) && is_numeric($_REQUEST["prjcat_id"]))
{
    $opInfo["prjcat_id"] = $_REQUEST["prjcat_id"];

    $result = phpaga_projects_getCategoryList($prjcatList);
    if (($result == PHPAGA_ERR_NONE) && isset($prjcatList[$_REQUEST["prjcat_id"]]))
        $search_params_pdf[] = array("param" => _("Project category"),
                                     "value" => $prjcatList[$_REQUEST["prjcat_id"]]);
}
else
{
    $opInfo["prjcat_id"] = "";
}

/* job category id */
if (isset($_REQUEST["jcat_id"]) && is_numeric($_REQUEST["jcat_id"]))
{
    $opInfo["jcat_id"] = $_REQUEST["jcat_id"];

    $result = phpaga_jobcat_getList($jcatList);
    if (($result == PHPAGA_ERR_NONE) && isset($jcatList[$_REQUEST["jcat_id"]]))
        $search_params_pdf[] = array("param" => _("Job category"),
                                     "value" => $jcatList[$_REQUEST["jcat_id"]]);
}
else
{
    $opInfo["jcat_id"] = "";
}

/* operation category id */
if (isset ($_REQUEST["opcat_id"])
    && is_numeric ($_REQUEST["opcat_id"]))
{
    $opInfo["opcat_id"] = $_REQUEST["opcat_id"];
    $image_args .= "&opcat_id=".$_REQUEST["opcat_id"];

    $result = phpaga_operations_getCategoryList($opcatList);
    if (($result == PHPAGA_ERR_NONE) && isset($opcatList[$_REQUEST["opcat_id"]]))
        $search_params_pdf[] = array("param" => _("Task category"),
                                     "value" => $opcatList[$_REQUEST["opcat_id"]]);
}
else
{
    $opInfo["opcat_id"] = "";
}

/* operation description */
if (isset ($_REQUEST["op_desc"]) && strlen($_REQUEST["op_desc"]))
{
    $opInfo["op_desc"] = $_REQUEST["op_desc"];
    $image_args .= "&op_desc=".$_REQUEST["op_desc"];
    $search_params_pdf[] = array("param" => _("Task description"),
                                 "value" => $_REQUEST["op_desc"]);
}
else
{
    $opInfo["op_desc"] = "";
}

/* operation billed? */
if (isset($_REQUEST["op_billed"])
    && is_numeric($_REQUEST["op_billed"])
    && (($_REQUEST["op_billed"] == PHPAGA_BILL_BILLED)
        || ($_REQUEST["op_billed"] == PHPAGA_BILL_NOTBILLED)))
{
    $opInfo["op_billed"] = $_REQUEST["op_billed"];
    $search_params_pdf[] = array("param" => _("Billing status"),
                                 "value" =>
                                 ($_REQUEST["op_billed"] == PHPAGA_BILL_BILLED) ? _("billed") : _("not billed"));
}
else
{
    $opInfo["op_billed"] = "";
}

/* start date */
if (isset ($_REQUEST["op_startdatefrom"])
    && ($_REQUEST["op_startdatefrom"] != ""))
{
    $opInfo["op_startdatefrom"] = $_REQUEST["op_startdatefrom"];
    $image_args .= "&op_startdatefrom=".$_REQUEST["op_startdatefrom"];
    $search_params_pdf[] = array("param" => _("Date from"),
                                 "value" => $_REQUEST["op_startdatefrom"]);
}
else
{
    $opInfo["op_startdatefrom"] = "";
}

/* end date */
if (isset ($_REQUEST["op_startdateto"])
    && ($_REQUEST["op_startdateto"] != ""))
{
    $opInfo["op_startdateto"] = $_REQUEST["op_startdateto"];
    $image_args .= "&op_startdateto=".$_REQUEST["op_startdateto"];
    $search_params_pdf[] = array("param" => _("Date to"),
                                 "value" => $_REQUEST["op_startdateto"]);
}
else
{
    $opInfo["op_startdateto"] = "";
}

/* Always show the task description */
$show["task"] = _("Task");


$search_result = phpaga_operations_search($rows, $count, $duration,
                                          $opIds, $opInfo);

if ($search_result != PHPAGA_ERR_NONE)
{
    $status = phpaga_returnerror($phpaga_error[$search_result]);
}
else
{
    $status = phpaga_returnmessage(_("Information"),
                                   sprintf(_("%s tasks matching your query were found totalling %s hours."),
                                           $count,
                                           $duration));

    /* show results if any matching recors were found */

    if ($count > 0)
    {
        $pdf = new phpaga_pdf_document("tasks.pdf");
        $pdf->add_oplist($rows, $search_params_pdf, $show, null, false);
        $pdf->spitout();
    }
}

?>
