<?php
/**
 * phpaga
 *
 * Export invoices
 *
 * Export invoices matching query options to csv file
 *
 * @author Mark Parssey <markpa@users.sourceforge.net>
 * @version $Id: bills_csv.php,v 1.0 2005/03/08 21:47:24
 *
 * 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");

$perms = array(PHPAGA_PERM_VIEW_INVOICES, PHPAGA_PERM_MANAGE_INVOICES);
phpaga_user_pageperm($perms);

/* get search criteria */

/* company id */
if (isset ($_REQUEST["cpn_id"]) && is_numeric ($_REQUEST["cpn_id"]))
{
    $billInfo["cpn_id"] = $_REQUEST["cpn_id"];
}
else
{
    $billInfo["cpn_id"] = "";
}

/* billing method plugin id */
if (isset ($_REQUEST["bmt_id"]) && is_numeric ($_REQUEST["bmt_id"]))
{
    $billInfo["bmt_id"] = $_REQUEST["bmt_id"];
}
else
{
    $billInfo["bmt_id"] = "";
}

/* currency id */
if (isset ($_REQUEST["curr_id"]) && is_numeric ($_REQUEST["curr_id"]))
{
    $billInfo["curr_id"] = $_REQUEST["curr_id"];
}
else
{
    $billInfo["curr_id"] = "";
}

/* year */
if (isset ($_REQUEST["bill_Year"]) && is_numeric ($_REQUEST["bill_Year"]))
{
    $billInfo["bill_year"] = $_REQUEST["bill_Year"];
}
else
{
    $billInfo["bill_year"] = "";
}

/* bill number */
if (isset ($_REQUEST["bill_number"]) && strlen ($_REQUEST["bill_number"]))
{
    $billInfo["bill_number"] = $_REQUEST["bill_number"];
}
else
{
    $billInfo["bill_number"] = "";
}

/* has the invoice/bill been paid? */
if (isset($_REQUEST["bill_paid"]) &&
    is_numeric($_REQUEST["bill_paid"]) &&
    (($_REQUEST["bill_paid"] ==  PHPAGA_BILL_PAID) ||
     ($_REQUEST["bill_paid"] == PHPAGA_BILL_NOTPAID)))
{
    $billInfo["bill_paid"] = $_REQUEST["bill_paid"];
}
else
{
    $billInfo["bill_paid"] = "";
}

/* has the invoice/bill been sent to the recipient? */
if (isset($_REQUEST["bill_sent"]) &&
    is_numeric($_REQUEST["bill_sent"]) &&
    (($_REQUEST["bill_sent"] == PHPAGA_BILL_SENT) ||
     ($_REQUEST["bill_sent"] == PHPAGA_BILL_NOTSENT)))
{
    $billInfo["bill_sent"] = $_REQUEST["bill_sent"];
}
else
{
    $billInfo["bill_sent"] = "";
}

/* billing from date */
if (isset($_REQUEST["bill_datefrom"]) && ($_REQUEST["bill_datefrom"] != ""))
{
    $billInfo["bill_datefrom"] = rawurldecode($_REQUEST["bill_datefrom"]);
}
else
{
    $billInfo["bill_datefrom"] = "";
}

/* billing to date */
if (isset($_REQUEST["bill_dateto"]) && ($_REQUEST["bill_dateto"] != ""))
{
    $billInfo["bill_dateto"] = rawurldecode($_REQUEST["bill_dateto"]);
}
else
{
    $billInfo["bill_dateto"] = "";
}


$result = phpaga_bills_search($rows, $count, $billInfo);

if ($result != PHPAGA_ERR_NONE)
{
    phpaga_header(array("menuitem" => "finance"));
    $status = phpaga_returnerror($phpaga_error[$result]);
    phoaga_footer();
}
else
{
    /* export results if any matching records were found */

    if ($count > 0)
    {
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename=data.csv');

        print(sprintf("%s,%s,%s,%s\r\n",
                      _("Invoice no."),
                      _("Date"),
                      _("Company"),
                      _("Amount incl. VAT")));

        foreach($rows as $row) {
            print(sprintf("%s,%s,\"%s\",%s\r\n",
                          $row["bill_number"],
                          $row["bill_date"],
                          $row["cpn_name"],
                          $row["bill_endsum"]));
        }
    }
}

?>