<?php
/**
 * phpaga
 *
 * finances functionality.
 *
 * This file contains the necessary routines for displaying some financial information.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @version $Id: finances.php 653 2005-10-03 21:53:26Z 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.
 */

/**
 * Returns an array containing the amount of money billed per month.
 *
 * @param array  &$billedPerMonth  Array containing amount of money
 *                                 billed per month for the currency selected
 * @param int    $curr_id          Currency ID
 * @param int    $year             Fiscal year
 *
 * @return int   $result  A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_finances_billedPerMonth (&$billedPerMonth,
                                         $curr_id,
                                         $year = "")
{
    $billedPerMonth = array ();

    if (!isset($year) || !is_numeric($year))
        $year = phpaga_finances_getfiscalyear(date("Y-m-d"));

    $fy_dates = phpaga_finances_getfiscalyeardates($year);

    phpaga_db_connect($db);

    $query = "SELECT SUM(bill_endsum) AS amount, ".
        "MONTH(bill_date) AS month, ".
        "curr_name, bills.curr_id ".
        "FROM bills ".
        "LEFT JOIN currencies ON currencies.curr_id = bills.curr_id ".
        "WHERE ((bill_date >= ".$db->quoteSmart($fy_dates["start"]).") ".
        "AND (bill_date <= ".$db->quoteSmart($fy_dates["end"]).")) ".
        "AND (bills.curr_id = ".$db->quoteSmart($curr_id).") ".
        "GROUP BY MONTH(bill_date), curr_name, bills.curr_id ".
        "ORDER BY month(bill_date) ";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        while ($qrow = $result->fetchRow())
            $billedPerMonth[] = $qrow;

    $result->free();
    return PHPAGA_ERR_NONE;
}


/**
 * Returns an array containing the amount of money received through paid bills
 * per month.
 *
 * @param array  &$paidPerMonth  Array containing amount of money
 *                                 billed per month for the currency selected
 * @param int    $curr_id          Currency ID
 * @param int    $year             Fiscal year
 *
 * @return int   $result  A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_finances_paidPerMonth (&$paidPerMonth,
                                       $curr_id,
                                       $year = "")
{
    $paidPerMonth = array();

    if (!isset($year) || !is_numeric($year))
        $year = phpaga_finances_getfiscalyear(date("Y-m-d"));

    $fy_dates = phpaga_finances_getfiscalyeardates($year);

    phpaga_db_connect($db);

    $query = "SELECT SUM(bill_endsum) AS amount, ".
        "MONTH(bill_paydate) AS month, ".
        "curr_name, bills.curr_id ".
        "FROM bills ".
        "LEFT JOIN currencies ON currencies.curr_id = bills.curr_id ".
        "WHERE  ((bill_paydate >= ".$db->quoteSmart($fy_dates["start"]).") ".
        "AND (bill_paydate <= ".$db->quoteSmart($fy_dates["end"]).")) ".
        "AND (bills.curr_id = ".$db->quoteSmart($curr_id).") ".
        "GROUP BY MONTH(bill_paydate), curr_name, bills.curr_id ".
        "ORDER BY month(bill_paydate) ";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        while ($qrow = $result->fetchRow())
            $paidPerMonth[] = $qrow;

    $result->free();
    return PHPAGA_ERR_NONE;
}


/**
 * Returns an array containing the amount of money billed per company. Should
 * help you find out who is your biggest client. :)
 *
 * @param array  &$billedPerCompany  Array containing amount of money
 *                                   billed per company for the currency selected
 * @param int    $curr_id            Currency ID
 * @param int    $year               Fiscal year
 *
 * @return int   $result  A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.1
 */

function phpaga_finances_billedPerCompany (&$billedPerCompany,
                                           $curr_id,
                                           $year = null)
{
    $billedPerCompany = array ();

    if (!isset($year) || !is_numeric($year))
        $year = phpaga_finances_getfiscalyear(date("Y-m-d"));

    $fy_dates = phpaga_finances_getfiscalyeardates($year);

    phpaga_db_connect($db);

    $query =
        "SELECT companies.cpn_name, bills.cpn_id, SUM(bill_endsum) AS amount, ".
        "curr_name, curr_decimals ".
        "FROM bills ".
        "LEFT JOIN companies ON companies.cpn_id = bills.cpn_id ".
        "LEFT JOIN currencies ON currencies.curr_id = bills.curr_id ".
        "WHERE ((bill_date >= ".$db->quoteSmart($fy_dates["start"]).") ".
        "AND (bill_date <= ".$db->quoteSmart($fy_dates["end"]).")) ".
        "AND (bills.curr_id = ".$db->quoteSmart($curr_id).") ".
        "GROUP BY bills.cpn_id, companies.cpn_name, curr_name, curr_decimals ".
        "ORDER BY amount DESC ";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        while ($qrow = $result->fetchRow())
        {
            $qrow["amount_src"] = $qrow["amount"];
            $qrow["amount"] =
                trim (number_format(
                          $qrow["amount"], $qrow["curr_decimals"],
                          PHPAGA_SEPARATOR_DECIMALS,
                          PHPAGA_SEPARATOR_THOUSANDS));
            $billedPerCompany[] = $qrow;
        }

    $result->free();
    return PHPAGA_ERR_NONE;
}


/**
 * Returns the fiscal year for a given date.
 *
 * @param  string $date      Date
 *
 * @return int    $year      Fiscal year for the given date or false on error.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.3
 */

function phpaga_finances_getfiscalyear($date)
{

    if (!strlen($date))
        return false;

    $fy_dates = phpaga_finances_getfiscalyeardates(date("Y", strtotime($date)));

    $tstamp = strtotime($date);
    $tstamp_end = strtotime($fy_dates["end"]);

    if ($tstamp < $tstamp_end)
        return((int)date("Y", $tstamp_end));
    else
        return((int)date("Y", $tstamp_end) + 1);


}


/**
 * Returns the start end end date for a given fiscal year. (Each
 * fiscal year is identified by the calendar year in which it ends.)
 *
 * @param  int    $year      Fiscal year
 *
 * @return array  $dates     Start end end dates for the fiscal year
 *                           ($date["start"] and $date["end"])
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.3
 */

function phpaga_finances_getfiscalyeardates($fy)
{

    $dates = array("start" => false,
                   "end" => false);

    if (!strlen($fy) || !is_numeric($fy))
        return $dates;

    $t = strtotime(sprintf("%s-%s-%s",
                           $fy,
                           PHPAGA_FIN_FISCYEAR_MONTH,
                           PHPAGA_FIN_FISCYEAR_DAY));

    $md = date("m-d", strtotime("-1 day", $t));

    $dates["end"] = sprintf("%s-%s", $fy, $md);
    $dates["start"] = date("Y-m-d",
                         strtotime("-1 year +1 day",
                                   strtotime($dates["end"])));

    return $dates;

}


/**
 * Returns an array containing the amount of money billed, received,
 * and missing per customer.
 *
 * @param array  &$details           Array containing amount of money
 *                                   billed, received, and missing per
 *                                   company for the selected curreny
 *                                   and year
 * @param int    $curr_id            Currency ID
 * @param int    $year               Fiscal year
 *
 * @return int   $result  A result code.
 *
 * @author Florian Lanthaler <florian@phpaga.net>
 * @since phpaga 0.3
 */

function phpaga_finances_getSumsPerCustomerYearly (&$details,
                                                   $curr_id,
                                                   $year = null)
{
    $details = array("details" => array(),
                     "totals" => array("billed" => 0,
                                       "paid" => 0,
                                       "missing" => 0));
    $cpn = array();

    if (!isset($year) || !is_numeric($year))
        $year = phpaga_finances_getfiscalyear(date("Y-m-d"));

    $fy_dates = phpaga_finances_getfiscalyeardates($year);

    phpaga_db_connect($db);

    $query =
        "SELECT companies.cpn_name, bills.cpn_id, SUM(bill_endsum) AS amount ".
        "FROM bills ".
        "LEFT JOIN companies ON companies.cpn_id = bills.cpn_id ".
        "WHERE ((bill_date >= ".$db->quoteSmart($fy_dates["start"]).") ".
        "AND (bill_date <= ".$db->quoteSmart($fy_dates["end"]).")) ".
        "AND (bills.curr_id = ".$db->quoteSmart($curr_id).") ".
        "GROUP BY bills.cpn_id, companies.cpn_name ".
        "ORDER BY companies.cpn_name";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        while ($qrow = $result->fetchRow())
        {
            $cpn[$qrow["cpn_name"]."-".$qrow["cpn_id"]] = array("cpn_id" => $qrow["cpn_id"],
                                          "cpn_name" => $qrow["cpn_name"],
                                          "sum_billed" => $qrow["amount"],
                                          "sum_paid" => 0,
                                          "sum_missing" => 0);

            $details["totals"]["billed"] += $qrow["amount"];
        }

    $query =
        "SELECT companies.cpn_name, bills.cpn_id, SUM(bill_endsum) AS amount ".
        "FROM bills ".
        "LEFT JOIN companies ON companies.cpn_id = bills.cpn_id ".
        "WHERE ((bill_paydate >= ".$db->quoteSmart($fy_dates["start"]).") ".
        "AND (bill_paydate <= ".$db->quoteSmart($fy_dates["end"]).")) ".
        "AND (bills.curr_id = ".$db->quoteSmart($curr_id).") ".
        "GROUP BY bills.cpn_id, companies.cpn_name";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        while ($qrow = $result->fetchRow())
        {
            if (isset($cpn[$qrow["cpn_name"]."-".$qrow["cpn_id"]]))
                $cpn[$qrow["cpn_name"]."-".$qrow["cpn_id"]]["sum_paid"] = $qrow["amount"];
            else
                $cpn[$qrow["cpn_name"]."-".$qrow["cpn_id"]] = array("cpn_id" => $qrow["cpn_id"],
                                              "cpn_name" => $qrow["cpn_name"],
                                              "sum_billed" => 0,
                                              "sum_paid" => $qrow["amount"],
                                              "sum_missing" => 0);

            $details["totals"]["paid"] += $qrow["amount"];
        }

    $query =
        "SELECT companies.cpn_name, bills.cpn_id, SUM(bill_endsum) AS amount ".
        "FROM bills ".
        "LEFT JOIN companies ON companies.cpn_id = bills.cpn_id ".
        "WHERE ((bill_date >= ".$db->quoteSmart($fy_dates["start"]).") ".
        "AND (bill_date <= ".$db->quoteSmart($fy_dates["end"]).")) ".
        "AND bill_paydate IS NULL ".
        "AND (bills.curr_id = ".$db->quoteSmart($curr_id).") ".
        "GROUP BY bills.cpn_id, companies.cpn_name";

    $result = $db->query($query);

    if (DB::isError($result))
        return PHPAGA_ERR_DB_EXEC_QUERY;

    if ($result->numRows() > 0)
        while ($qrow = $result->fetchRow())
        {
            if (isset($cpn[$qrow["cpn_name"]."-".$qrow["cpn_id"]]))
                $cpn[$qrow["cpn_name"]."-".$qrow["cpn_id"]]["sum_missing"] = $qrow["amount"];
            else
                $cpn[$qrow["cpn_name"]."-".$qrow["cpn_id"]] = array("cpn_id" => $qrow["cpn_id"],
                                              "cpn_name" => $qrow["cpn_name"],
                                              "sum_billed" => 0,
                                              "sum_paid" => 0,
                                              "sum_missing" => $qrow["amount"]);

                $details["totals"]["missing"] += $qrow["amount"];
        }

    $result->free();

    foreach ($cpn as $c)
        $details["details"][] = $c;

    return PHPAGA_ERR_NONE;
}

?>