<?php
  /**
   * phpaga
   *
   * Print operations
   *
   * Print operations matching query options to pdf file
   *
   * @author Florian Lanthaler <florian@phpaga.net>
   * @version $Id: operations_pdf.php 978 2010-03-21 20:39:05Z 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");
include_once(PHPAGA_LIBPATH."pdf.php");

$list_operations = "";

$opInfo = array('cpn_id' => '',
                'cpn_id_issuebill' => '',
                'prj_id' => '',
                'pe_id' => '',
                'prj_status' => '',
                'prjcat_id' => '',
                'jcat_id' => '',
                'opcat_id' => '',
                'op_desc' => '',
                'op_billed' => '',
                'op_startdatefrom' => '',
                'op_startdateto' => '');

/* 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();

if (isset ($REQUEST_DATA["cpn_id"]) && is_numeric ($REQUEST_DATA["cpn_id"])) {

    $opInfo["cpn_id"] = $REQUEST_DATA["cpn_id"];
    $image_args .= "&cpn_id=".$REQUEST_DATA["cpn_id"];

    $cpnInfo = phpaga_companies_getInfo($REQUEST_DATA["cpn_id"]);
    if (!PhPagaError::isError($cpnInfo))
        $search_params_pdf[] = array("param" => _("Company"),
                                     "value" => $cpnInfo["cpn_name"]);
}

if (isset($REQUEST_DATA["cpn_id_issuebill"]) && is_numeric($REQUEST_DATA["cpn_id_issuebill"])) {

    $opInfo["cpn_id_issuebill"] = $REQUEST_DATA["cpn_id_issuebill"];

    $cpnInfo = phpaga_companies_getInfo ($REQUEST_DATA["cpn_id_issuebill"]);
    if (!PhPagaError::isError($cpnInfo))
        $search_params_pdf[] = array("param" => _("Company issuing the bill"),
                                     "value" => $cpnInfo["cpn_name"]);
}

if (isset ($REQUEST_DATA["prj_id"]) && is_numeric ($REQUEST_DATA["prj_id"])) {

    $opInfo["prj_id"] = $REQUEST_DATA["prj_id"];
    $image_args .= "&prj_id=".$REQUEST_DATA["prj_id"];

    $prjInfo = phpaga_projects_getInfo($REQUEST_DATA["prj_id"]);
    if (!PhPagaError::isError($prjInfo) && isset($prjInfo["prj_title"]))
        $search_params_pdf[] = array("param" => _("Project"),
                                     "value" => $prjInfo["prj_title"]);
} else
    $show["project"] = ("Project");

if (isset ($REQUEST_DATA["pe_id"]) && is_numeric ($REQUEST_DATA["pe_id"])) {

    $opInfo["pe_id"] = $REQUEST_DATA["pe_id"];
    $image_args .= "&pe_id=".$REQUEST_DATA["pe_id"];

    $peInfo = phpaga_persons_getInfo ($REQUEST_DATA["pe_id"]);
    if (!PhPagaError::isError($peInfo))
        $search_params_pdf[] = array("param" => _("Person"),
                                     "value" => $peInfo["pe_person"]);
} else
    $show["person"] = ("Person");

if (isset($REQUEST_DATA["prj_status"]) && is_numeric($REQUEST_DATA["prj_status"])) {

    $opInfo["prj_status"] = $REQUEST_DATA["prj_status"];

    $prjstatList = phpaga_projects_getStatusList();
    if ((!PhPagaError::isError($prjstatList)) && isset($prjstatList[$REQUEST_DATA["prj_status"]]))
        $search_params_pdf[] = array("param" => _("Project status"),
                                     "value" => $prjstatList[$REQUEST_DATA["prj_status"]]);
}

if (isset($REQUEST_DATA["prjcat_id"]) && is_numeric($REQUEST_DATA["prjcat_id"])) {

    $opInfo["prjcat_id"] = $REQUEST_DATA["prjcat_id"];

    $prjcatList = phpaga_projects_getCategoryList();
    if ((!PhPagaError::isError($prjcatList)) && isset($prjcatList[$REQUEST_DATA["prjcat_id"]]))
        $search_params_pdf[] = array("param" => _("Project category"),
                                     "value" => $prjcatList[$REQUEST_DATA["prjcat_id"]]);
}

if (isset($REQUEST_DATA["jcat_id"]) && is_numeric($REQUEST_DATA["jcat_id"])) {

    $opInfo["jcat_id"] = $REQUEST_DATA["jcat_id"];

    $jcatList = phpaga_jobcat_getList();
    if (!PhPagaError::isError($jcatList) && isset($jcatList[$REQUEST_DATA["jcat_id"]]))
        $search_params_pdf[] = array("param" => _("Job category"),
                                     "value" => $jcatList[$REQUEST_DATA["jcat_id"]]);
}

if (isset ($REQUEST_DATA["opcat_id"]) && is_numeric ($REQUEST_DATA["opcat_id"])) {

    $opInfo["opcat_id"] = $REQUEST_DATA["opcat_id"];
    $image_args .= "&opcat_id=".$REQUEST_DATA["opcat_id"];

    $opcatList = phpaga_operations_getCategoryList();
    if ((!PhPagaError::isError($opcatList)) && isset($opcatList[$REQUEST_DATA["opcat_id"]]))
        $search_params_pdf[] = array("param" => _("Task category"),
                                     "value" => $opcatList[$REQUEST_DATA["opcat_id"]]);
}

if (isset ($REQUEST_DATA["op_desc"]) && strlen($REQUEST_DATA["op_desc"])) {

    $opInfo["op_desc"] = $REQUEST_DATA["op_desc"];
    $image_args .= "&op_desc=".$REQUEST_DATA["op_desc"];
    $search_params_pdf[] = array("param" => _("Task description"),
                                 "value" => $REQUEST_DATA["op_desc"]);
}

if (isset($REQUEST_DATA["op_billed"]) && is_numeric($REQUEST_DATA["op_billed"])
    && (($REQUEST_DATA["op_billed"] == PHPAGA_BILL_BILLED)
        || ($REQUEST_DATA["op_billed"] == PHPAGA_BILL_NOTBILLED))) {

    $opInfo["op_billed"] = $REQUEST_DATA["op_billed"];
    $search_params_pdf[] = array("param" => _("Billing status"),
                                 "value" =>
                                 ($REQUEST_DATA["op_billed"] == PHPAGA_BILL_BILLED) ? _("billed") : _("not billed"));
}

if (isset ($REQUEST_DATA["op_startdatefrom"]) && ($REQUEST_DATA["op_startdatefrom"] != "")) {

    $opInfo["op_startdatefrom"] = $REQUEST_DATA["op_startdatefrom"];
    $image_args .= "&op_startdatefrom=".$REQUEST_DATA["op_startdatefrom"];
    $search_params_pdf[] = array("param" => _("Date from"),
                                 "value" => $REQUEST_DATA["op_startdatefrom"]);
}

if (isset ($REQUEST_DATA["op_startdateto"]) && ($REQUEST_DATA["op_startdateto"] != "")) {

    $opInfo["op_startdateto"] = $REQUEST_DATA["op_startdateto"];
    $image_args .= "&op_startdateto=".$REQUEST_DATA["op_startdateto"];
    $search_params_pdf[] = array("param" => _("Date to"),
                                 "value" => $REQUEST_DATA["op_startdateto"]);
}

$show["task"] = _("Task");

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

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

?>