<?php
/**
 * phpaga
 *
 * Payments 
 *
 * This is the payments management interface that allows to add, remove
 * and edit payments. 
 *
 * @author Michael Kimmick <support@nichewares.com>
 * @version $Id: payments.php 1 2007-09-19 20:12:30Z Michael Kimmick$
 *
 * Copyright (c) 2008 Michael Kimmick <support@nichewares.com>
 *
 * 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");

$opIds = array();
$pmtInfo = array("cpn_id" => "",
                "cpn_id_issuebill" => "",
                "pe_id" => "",
                "prj_id" => "",
                "prj_status" => "",
                "prjcat_id" => "",
                "opcat_id" => "",
                "op_desc" => "",
                "op_billed" => "",
                "prj_billabletype" => "",
                "op_startdatefrom" => "",
                "op_startdateto" => "",
                "jcat_id" => "");

$statusmsg = "";
$action = "";
$bill_id = "";
$pmt_id = "";
$pmt_amt = "";
$pmt_note = "";
$pmt_date = "";
$curPmt = null;
$add_result = null;

if (!isset($REQUEST_DATA["bill_id"]) || !is_numeric($REQUEST_DATA["bill_id"]))
    phpaga_error(PHPAGA_ERR_INVALID_PAYMENTID);
else {

    $bill_id = $REQUEST_DATA["bill_id"];

     if (isset($REQUEST_DATA["pmt_amt"]) && is_numeric($REQUEST_DATA["pmt_amt"]) && (isset($REQUEST_DATA["add_payment"]) && $REQUEST_DATA["add_payment"]=='true' )) {
        $pmtamt = $REQUEST_DATA["pmt_amt"];

	if (isset($REQUEST_DATA["pmt_date"]) && strlen($REQUEST_DATA["pmt_date"]) == 10) {
	  $pmtdate = $REQUEST_DATA["pmt_date"];
	} else {
	  $pmtdate = date('Y-m-d'); 
	}

	if (isset($REQUEST_DATA["pmt_note"])) {
	  $pmtnote = $REQUEST_DATA["pmt_note"];
	} else {
	  $pmtnote = '';
	}

        $add_result = phpaga_payment_add($bill_id, $pmtamt, $pmtdate, $pmtnote);

        if (!PhPagaError::isError($add_result)) {
            header("Location: bill.php?id=".$bill_id);
            exit;
        }
    } elseif ((isset($REQUEST_DATA['pmt_id']) && is_numeric($REQUEST_DATA['pmt_id'])) && (isset($REQUEST_DATA['del_payment']) && $REQUEST_DATA['del_payment']=='true')) {
	$pmt_id = $REQUEST_DATA['pmt_id'];
	$del_result = phpaga_payment_delete($pmt_id, $bill_id);

	if (!PhPagaError::isError($del_result)) {
	    header("Location: bill.php?id=".$bill_id);
	    exit;
	} 
    } elseif (isset($REQUEST_DATA['pmt_id']) && isset($REQUEST_DATA['pmt_amt']) && isset($REQUEST_DATA['update_payment'])) {
	if (is_numeric($REQUEST_DATA['pmt_id']) && is_numeric($REQUEST_DATA['pmt_amt']) && $REQUEST_DATA['update_payment']=='true') {
	    $pmt_id = $REQUEST_DATA['pmt_id'];
	    $pmt_amt = $REQUEST_DATA['pmt_amt'];

	    if (isset($REQUEST_DATA["pmt_date"]) && strlen($REQUEST_DATA["pmt_date"]) == 10) {
		$pmtdate = $REQUEST_DATA["pmt_date"];
	    } else {
		$pmtdate = date('Y-m-d');
	    }


            if (isset($REQUEST_DATA['pmt_note'])) {
                $pmt_note = $REQUEST_DATA['pmt_note'];
	    } else {
		$pmt_note = '';
            }

            $update_result = phpaga_payment_update($pmt_id, $pmt_amt, $pmtdate, $pmt_note);

	    if (!PhPagaError::isError($update_result)) {
                header("Location: bill.php?id=".$bill_id);
                exit;
	    } else {
		phpaga_error('An Unknown error occurred.');
	    }
	}
    }

    phpaga_header(array("menuitem" => "finance"));

    if (PhPagaError::isError($add_result))
        $add_result->printMessage();

    $billInfo = array();
    $billInfo = phpaga_bills_getinfo($bill_id);

    $billPmtInfo = phpaga_bills_getPmtinfo($bill_id);

    if (PhPagaError::isError($billPmtInfo))
        $billPmtInfo->printMessage();
    elseif (PhPagaError::isError($billInfo))
	$billInfo->printMessage();
    else {

        $config = phpaga_config_get();

        $tpl = new psmarty;
        $tpl->assign("config", $config);
        $tpl->assign("ACTION", basename($_SERVER["PHP_SELF"]));

        if (isset($REQUEST_DATA['add_payment']) && $REQUEST_DATA['add_payment']=='true') {

            $tpl->assign("BILL_PMTINFO", sprintf(_("Add payment for invoice %s"),
                $billInfo["bill_number"]."/".
                $billInfo["bill_date"]));

            if (is_null(phpaga_fetch_POST('pmt_amt', null))) {
                // Not coming from form submit
                // Determine missing amount
                $curPmt = array('pmt_date' => date('Y-m-d'), 
                'pmt_amt' => $billInfo['bill_endsum']+$billInfo['feesum']['feesum']-$billInfo['pmtsum']['pmt_sum']);
            }

        } elseif (isset($REQUEST_DATA['edit_payment']) && $REQUEST_DATA['edit_payment']=='true') {
            $tpl->assign("BILL_PMTINFO", sprintf(_("Update payment for invoice %s"),
                $billInfo["bill_number"]."/".
                $billInfo["bill_date"]));
        }

        $tpl->assign("billPmtInfo", $billPmtInfo);
        $tpl->assign("billInfo", $billInfo);

        if (isset($REQUEST_DATA['pmt_id']) && is_numeric($REQUEST_DATA['pmt_id']) 
            && isset($REQUEST_DATA['edit_payment']) && $REQUEST_DATA['edit_payment']=='true') {

                $pmt_id = $REQUEST_DATA['pmt_id'];
                $curPmt = array();
                $curPmt = phpaga_payment_getinfo($pmt_id);

                if (PhPagaError::isError($curPmt)) {
                    $curPmt->printMessage();
                    $curPmt = array();
                }
            }

        $tpl->assign("curPmt", $curPmt);
        $tpl->display("payment.tpl.html");
    }

    phpaga_footer ();
}

?>
