<?php
session_start();
require_once "main.php";
Logger::configure('log4php.xml');
$log = Logger::getLogger('pctypeinfo');
		global $db;
if ($_SESSION['access']<3){
	if ($_GET['submitType']=='cust') {
		$result = bill_get_pctype_name($_SESSION['comnum']);
?>
		<script>
			function setType(type) {
				//formName is the name of your form
				document.accinform.submitType.value = type;
			}
		</script>
        <table>
			<tr>
		   		<td>
					<table border="1">
						<tr>
							<th>Name</th>
							<th>Description</th>
							<th>Account</th>
							<th>Other</th>
                			<th>Use Type</th>
						</tr>
						<?php
						$zrx = 'edit';
						for ($i=0;$i<count($result);$i++) {
						?>
						<tr>
							<?php if ($_SESSION['access'] > 2) { ?>                                   
                                <td><?php echo $result[$i]['name'] ;?></td>
							<?php                               
                                  } else {
							?>                                   
                                <td><button type="button" value="arates" onClick="dynamic_Select('includes/pctypeinfo.php','#setinfo','edit', <?php echo $result[$i]['id'] . "," . $_SESSION['comnum'] ." )" ;?>">Edit - <?php echo $result[$i]['name'] ; ?></button>
								</td>
							<?php } ?>
                        	<td><?php echo $result[$i]['rdesc'] . '</td>
							<td>' . $result[$i]['account'] . '</td>
							<td>' . $result[$i]['other'] . '</td>
                            <td>' . $result[$i]['usetype'] . '</td>
						</tr>';
						} ?>
            		</table>
				</td>							
                    <td>
                        Please check to make sure you use the correct Use Type.<br />
                        1 - Regular Monthly Billing Charges<br />
                        2 - Late Charge - Flat Fee<br />
                        3 - Late Charge - Percentage<br />
                        4 - Credit Card Fee<br />
                        5 - Returned Check Fee<br />
                        6 - Other Bank Charges<br />
                        7 - Miscellaneous Charges<br />
                    </td>
			</tr>		
		</table>		 
		<?php
		if ($_SESSION['access'] <= 2) {
		?>
			<button value="arates" onClick="dynamic_Select('includes/pctypeinfo.php', '#setinfo','add', '0')">Add a Charge Type</button>
		<?php
        }
	} 
	else if ($_GET['submitType']=='rcUpdate') {
		$test = bill_update_pctype( $_GET['id'], $_GET['name'], $_GET['desc'], $_GET['account'], $_GET['other'], $_GET['usetype'] );
		$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
		$result = bill_get_pctype_name($_SESSION['comnum']);
	?>
		<script>
			function setType(type) {
				//formName is the name of your form
				document.accinform.submitType.value = type;
			}
		</script>
        <table>
			<tr>
		   		<td>
					<table border="1">
						<tr>
							<th>Name</th>
							<th>Description</th>
							<th>Account</th>
							<th>Other</th>
                			<th>Use Type</th>
						</tr>
						<?php
						$zrx = 'edit';
						for ($i=0;$i<count($result);$i++) {
						?>
						<tr>
							<?php if ($_SESSION['access'] > 2) { ?>                                   
                                <td><?php echo $result[$i]['name'] ;?></td>
							<?php                               
                                  } else {
							?>                                   
                                <td><button type="button" value="arates" onClick="dynamic_Select('includes/pctypeinfo.php','#setinfo','edit', <?php echo $result[$i]['id'] . "," . $_SESSION['comnum'] ." )" ;?>">Edit - <?php echo $result[$i]['name'] ; ?></button>
								</td>
							<?php } ?>
                        	<td><?php echo $result[$i]['rdesc'] . '</td>
							<td>' . $result[$i]['account'] . '</td>
							<td>' . $result[$i]['other'] . '</td>
                            <td>' . $result[$i]['usetype'] . '</td>
						</tr>';
						} ?>
            		</table>
				</td>							
                    <td>
                        Please check to make sure you use the correct Use Type.<br />
                        1 - Regular Monthly Billing Charges<br />
                        2 - Late Charge - Flat Fee<br />
                        3 - Late Charge - Percentage<br />
                        4 - Credit Card Fee<br />
                        5 - Returned Check Fee<br />
                        6 - Other Bank Charges<br />
                        7 - Miscellaneous Charges<br />
                    </td>
			</tr>		
		</table>		 
		<?php
		if ($_SESSION['access'] <= 2) {
		?>
			<button value="arates" onClick="dynamic_Select('includes/pctypeinfo.php', '#setinfo','add', '0',<?php echo $_SESSION['comnum']; ?>)">Add a Payment Type</button>
		<?php
        }
	} 
	else if ($_GET['submitType']=='edit') {
		$result = bill_get_pctype( $_GET['id'] );
?>
		<script>
			function setType(type) {
				//formName is the name of your form
				document.accinform.submitType.value = type;
			}
		</script>
		<form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/pctypeinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#setinfo').html(data);}});return false;">
        	<table>
            	<tr>
					<td>
                        <table border="1">
							<tr>
								<td>
									<input type="hidden" name="id" id="id" value="<?php echo $result[0]['id']?>">
									<label for="name">Name</label>
								</td>
								<td><input type="text" name="name" id="name" value="<?php echo $result[0]['name']?>"></td>
							</tr>
							<tr>
								<td><label for="rdesc">Description</label></td>
								<td><input type="text" name="desc" id="desc" value="<?php echo $result[0]['rdesc']?>"></td>
							</tr>
							<tr>
								<td><label for="account">Account</label></td>
								<td><input type="text" name="account" id="account" value="<?php echo $result[0]['account']?>"></td>
							</tr>
							<tr>
								<td><label for="other">Other</label></td>
								<td><input type="text" name="other" id="other" value="<?php echo $result[0]['other']?>"></td>
							</tr>
							<tr>
                            	<td><label for="usetype">Use Type</label></td>
                                <td><input type="text" name="usetype" id="usetype" value="<?php echo $result[0]['usetype']?>"></td>
                            </tr>
                            <tr>
								<td>
									<input type="hidden" name="submitType">
									<input type="submit" onclick="setType('rcUpdate')" value="Update Charge Type"/>
								</td>
								<td><input type="submit" onclick="setType('cust')" value="Cancel"/></td>
							</tr>
						</table>
                    </td>
                    <td>
                        Please check to make sure you use the correct Use Type.<br />
                        1 - Regular Monthly Billing Charges<br />
                        2 - Late Charge - Flat Fee<br />
                        3 - Late Charge - Percentage<br />
                        4 - Credit Card Fee<br />
                        5 - Returned Check Fee<br />
                        6 - Other Bank Charges<br />
                        7 - Miscellaneous Charges<br />
                    </td>
				</tr>
            </table>
		</form>
<?php
	} 
	else if ($_GET['submitType']=='add') {
?>
		<script>
			function setType(type) {
				//formName is the name of your form
				document.accinform.submitType.value = type;
			}
		</script>
		<form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/pctypeinfo.php', data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#setinfo').html(data);}});return false;">
        	<table>
				<tr>
                    <td>
                        <table border="1">
							<tr>
								<td><label for="name">Name</label></td>
								<td><input type="text" name="name" id="name" ></td>
							</tr>
							<tr>
								<td><label for="desc">Description</label></td>
								<td><input type="text" name="desc" id="desc"></td>
							</tr>
							<tr>
								<td><label for="account">Account</label></td>
								<td><input type="text" name="account" id="account"></td>
							</tr>
							<tr>
								<td><label for="other">Other</label></td>
								<td><input type="text" name="other" id="other"></td>
							</tr>
                            <tr>
                                <td><label for="usetype">Use Type</label></td>
                                <td><input type="text" name="usetype" id="usetype"></td>
                            </tr>
							<tr>
								<td>
									<input type="hidden" name="submitType">
									<input type="submit" onclick="setType('rcAdd')" value="Add Charge Type"/>
								</td>
								<td><input type="submit" onclick="setType('cust')" value="Cancel"/></td>
							</tr>
						</table>
                    </td>
                    <td>
                        Please check to make sure you use the correct Use Type.<br />
                        1 - Regular Monthly Billing Charges<br />
                        2 - Late Charge - Flat Fee<br />
                        3 - Late Charge - Percentage<br />
                        4 - Credit Card Fee<br />
                        5 - Returned Check Fee<br />
                        6 - Other Bank Charges<br />
                        7 - Miscellaneous Charges<br />
                    </td>
            	</tr>
			</table>
		</form>
<?php
	} 
	else if ($_GET['submitType']=='rcAdd') {
		$test = bill_insert_pctype( $_SESSION['comnum'], $_GET['name'], $_GET['desc'], $_GET['account'], $_GET['other'], $_GET['usetype']  );
		$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
		$result = bill_get_pctype_name($_SESSION['comnum']);
		if ($test) {
			echo '<script type=text/javascript>alert("Account updated successfully!");</script>';
		}		
?>
		<script>
			function setType(type) {
				//formName is the name of your form
				document.accinform.submitType.value = type;
			}
		</script>
        <table>
			<tr>
        	<td>
				<table border="1">
					<tr>
						<th>Name</th>
						<th>Description</th>
						<th>Account</th>
						<th>Other</th>
                    	<th>Use Type</th>
					</tr>
					<?php
					$zrx = 'edit';
					for ($i=0;$i<count($result);$i++) {
					?>
						<tr>
							<td><button type="button" value="arates" onClick="dynamic_Select('includes/pctypeinfo.php', '#setinfo','edit', <?php echo $result[$i]['id']; ?>,<?php echo $_SESSION['comnum']; ?>)"><?php echo 'Edit # ' . $result[$i]['name']; ?></button></td>
							<td><?php echo $result[$i]['rdesc'] . '</td>
							<td>' . $result[$i]['account'] . '</td>
							<td>' . $result[$i]['other'] . '</td>
                            <td>' . $result[$i]['usetype'] . '</td>
						</tr>';
					}
								?>
				</table>
			</td>
            <td>
                        Please check to make sure you use the correct Use Type.<br />
                        1 - Regular Monthly Billing Charges<br />
                        2 - Late Charge - Flat Fee<br />
                        3 - Late Charge - Percentage<br />
                        4 - Credit Card Fee<br />
                        5 - Returned Check Fee<br />
                        6 - Other Bank Charges<br />
                        7 - Miscellaneous Charges<br />
            </td>
			</tr>
		</table>
		<button type="button" value="arates" onClick="dynamic_Select('includes/pctypeinfo.php', '#setinfo','add', '0',<?php echo $_SESSION['comnum']; ?>)">Add a Charge Type</button> 
<?php
	}
} else {
?>
	<script>
		alert('You do not have a high enough access level to make these cahnges.\n\
		Please see your administrator.');
	</script> 
<?php } ?>