<?php
session_start();
require_once "main.php";
Logger::configure('log4php.xml');
$log = Logger::getLogger('payinfo');
global $db;
$comz = $_SESSION['comnum'];
if ($_SESSION['access']<4){
?>
<script>
        function setType(type)
        {
            var z=0;
            var x=document.forms["accinform"]["bamount"].value;
            if (x==0 || x==null || x=="") {
                alert("An amount is required");
                return false;
            }
            var x=document.forms["accinform"]["ref1"].value;
            if (x==null || x=="") {
                alert("A reference is required");
                return false;
            }
            document.accinform.submitType.value = type;  
        }
</script>
<?php
    if ($_GET['submitType']=='payAdd') {
        $res2 = bill_get_abalance($_GET['id']);
        $zbal = $res2[0]['abalance'] - $_GET['bamount'];
        $test = bill_insert_payment($_SESSION['comnum'], $_GET['id'], $_GET['type'],
                $_GET['bamount'], $_GET['ref1'], $_GET['note1'], $_GET['other1'], $zbal);
        if ($test>1) {
            $log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum']);
?>
            <script>alert('Transaction Completed Sucessfully');dynamic_Select('includes/acinfo.php', '#accinfo','payment', <?php echo $_GET['id'];?>);</script>
<?php
        } else {
            $log->warn($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
            echo '<script>alert("There was a database error, please check account balance and payment history.
                 Please include the following in all communications with WtrFlw Services LLC.
                 Error $payinfo34");</script>';
        }  
    } else if ($_GET['submitType']=='chargeAdd') {
        $res2 = bill_get_abalance($_GET['id']);
        echo $_GET['bamount'];
        echo ' :-: ';
        echo $res2['0']['abalance'];
        $zbal =  $_GET['bamount'] + 0 + $res2['0']['abalance'] ;
        echo $zbal;
        $test = bill_insert_charge($_SESSION['comnum'], $_GET['id'], $_GET['type'], $_GET['bamount'], $_GET['ref1'], $_GET['note1'], $_GET['other1'], $zbal);
        if ($test) {
            $log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum']);
?>
            <script>dynamic_Select('includes/acinfo.php', '#accinfo','Payment', <?php echo $_GET['id'];?>);</script>
<?php
            echo 'Transaction Completed Sucessfully';
        } else {
            $log->warn($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
            echo 'There was a database error, please check account balance and payment history.
                  Please include the following in all communications with WtrFlw Services LLC.
                  Error $payinfo35';
        }
    } else if ($_GET['submitType']=='charge') {

        $result = bill_get_pctypes($_SESSION['comnum']);
?>
        <form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/payinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#accinfo').html(data);}});return false;">
        <table border="1">
            <tr>
                <td><label for="type">Charge Type</label></td>
                <td><select name="type" id="type">
<?php
                    for ($i=0;$i<count($result);$i++) {
                        $res1 = $result[$i];
                        echo '<option value="'.$res1['id'].'" title="'.$res1['rdesc'].'">'.$res1['name'].'</option>';
                    }
?>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label for="bamount">Amount</label></td>
                <td><input type="text" name="bamount" id="bamount" ></td>
            </tr>
            <tr>
                <td><label for="ref1">Reference</label></td>
                <td><input type="textarea" name="ref1" id="ref1"></td>
            </tr>
            <tr>
                <td><label for="note1">Note</label></td>
                <td><input type="textarea" name="note1" id="note1" ></td>
            </tr>
            <tr>
                <td><input type="hidden" name="submitType">
                    <input type="hidden" name="other1" value="">
                    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
                    <input type="submit" onclick="setType('chargeAdd')" value="Post Charge"/>
                </td>
            </tr>
        </table>
        </form>
<?php
    } else if ($_GET['submitType']=='payment') {

        $result = bill_get_ptypes($_SESSION['comnum']);
        $res3 = bill_get_customer($_SESSION['comnum'], $_GET['id']);
?>
        <form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/payinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#accinfo').html(data);}});return false;">
        <table border="1">
            <tr>
                <td><label for="type">Payment Type</label></td>
                <td><select name="type" id="type">
<?php
                    for ($i=0;$i<count($result);$i++) {
                        $res1 = $result[$i];
                        echo '<option value="'.$res1['id'].'" title="'.$res1['rdesc'].'">'.$res1['name'].'</option>';
                    }
?>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label for="bamount">Amount</label></td>
                <td><input type="text" name="bamount" id="bamount" value="<? echo $res3['0']['abalance'];?>"></td>
            </tr>
            <tr>
                <td><label for="ref1">Reference</label></td>
                <td><input type="textarea" name="ref1" id="ref1"></td>
            </tr>
            <tr>
                <td><label for="note1">Note</label></td>
                <td><input type="textarea" name="note1" id="note1" ></td>
            </tr>
            <tr>
                <td><input type="hidden" name="submitType">
                    <input type="hidden" name="other1" value="">
                    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
                    <input type="submit" onclick="setType('payAdd')" value="Post Payment"/>
                </td>
                <td>
                    <button type="button" title="Add a payment to this account." value="Add Payment" onClick="dynamic_Select('includes/ccpayinfo.php', '#accinfo', 'payment', <?php echo $_GET['id']; ?>,'0')">Add Electronic Payment</button>
                </td>
            </tr>
        </table>
        </form>
<?php
    } else if ($_GET['submitType']=='preverse') {

        $result = bill_get_ptypes($_SESSION['comnum']);
        $res3 = bill_get_customer($_SESSION['comnum'], $_GET['id']);
        $res4 = bill_get_bill_history1($_SESSION['comnum'],$_GET['zid']);
?>
        <form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/payinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#accinfo').html(data);}});return false;">
        <table border="1">
            <tr>
                <td><label for="type">Charge Type</label></td>
                <td><input name="type" id="type" value="7">
                </td>
            </tr>
            <tr>
                <td><label for="bamount">Amount</label></td>
                <td><input type="text" name="bamount" id="bamount" value="<? echo $res4['0']['pamount'];?>"></td>
            </tr>
            <tr>
                <td><label for="ref1">Reference</label></td>
                <td><input type="textarea" name="ref1" id="ref1" value="<? echo $res4['0']['ref1'];?>"></td>
            </tr>
            <tr>
                <td><label for="note1">Note</label></td>
                <td><input type="textarea" name="note1" id="note1" value="<? echo $res4['0']['note1'];?>"></td>
            </tr>
            <tr>
                <td><label for="other1">Reason for Reversal</label></td>
                <td><input type="textarea" name="other1" id="other1"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="submitType">
                    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
                    <input type="hidden" name="zid" value="<?php echo $_GET['zid']; ?>">
                    <input type="submit" onclick="setType('chargeAdd')" value="Post Reversal"/>
                </td>
            </tr>
        </table>
        </form>
<?php
    } else if ($_GET['submitType']=='creverse') {
        $result = bill_get_ptypes($_SESSION['comnum']);
        $res3 = bill_get_customer($_SESSION['comnum'], $_GET['id']);
         $res4 = bill_get_bill_history1($_SESSION['comnum'],$_GET['zid']);
?>
        <form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/payinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#accinfo').html(data);}});return false;">
        <table border="1">
            <tr>
                <td><label for="type">Payment Type</label></td>
                <td><select name="type" id="type">
<?php
                    for ($i=0;$i<count($result);$i++) {
                        $res1 = $result[$i];
                        echo '<option value="'.$res1['id'].'" title="'.$res1['rdesc'].'">'.$res1['name'].'</option>';
                    }
?>
                    </select>
                </td>
            </tr>
            <tr>
                <td><label for="bamount">Amount</label></td>
                <td><input type="text" name="bamount" id="bamount" value="<? echo $res4['0']['bamount'];?>"></td>
            </tr>
            <tr>
                <td><label for="ref1">Reference</label></td>
                <td><input type="textarea" name="ref1" id="ref1" value="<? echo $res4['0']['ref1'];?>"></td>
            </tr>
            <tr>
                <td><label for="note1">Note</label></td>
                <td><input type="textarea" name="note1" id="note1" value="<? echo $res4['0']['note1'];?>"></td>
            </tr>
            <tr>
                <td><label for="other1">Reason for Reversal</label></td>
                <td><input type="textarea" name="other1" id="other1"></td>
            </tr>
            <tr>
                <td><input type="hidden" name="submitType">
                    <input type="hidden" name="id" value="<?php echo $_GET['id']; ?>">
                    <input type="hidden" name="zid" value="<?php echo $_GET['zid']; ?>">
                    <input type="submit" onclick="setType('payAdd')" value="Post Reversal"/>
                </td>
            </tr>
        </table>
        </form>
<?php
    }
} else {
?>
    <script>alert('You do not have a high enough access level to post payments\n\
                   or charges!\n\
                   Please see your administrator.');</script>
<?php
}
?>