<?php
session_start();
require_once "main.php";
Logger::configure('log4php.xml');
$log = Logger::getLogger('nwoinfo');
global $db;
$comz = $_SESSION['comnum'];
?>
<?
if ($_SESSION['access']<4){
if ($_GET['submitType']=='woAdd') {

$test = bill_insert_wo($_SESSION['comnum'], $_GET['masterid'], $_GET['mid'], $_GET['addr'],
        $_GET['cinfo1'], $_GET['cinfo2'], $_GET['rinfo1'], $_GET['rinfo2'], 
        date('Y-m-d', strtotime($_GET['cdate'])), date('Y-m-d', strtotime($_GET['rdate'])),
        date('Y-m-d', strtotime('now')),
        $_GET['centered'], $_GET['rentered'], $_GET['rtech'], $_GET['part1'], $_GET['part2'],
        $_GET['part3'], $_GET['part4'], $_GET['part5'], $_GET['cost1'], $_GET['cost2'],
        $_GET['cost3'], $_GET['cost4'], $_GET['cost5'],$_GET['cost_total'], $_GET['comment'], $_GET['metertype'], $_GET['metermiu']);
if ($test) {
    $log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum']);

?>
<script>dynamic_Select('includes/acinfo.php', '#accinfo','worder', <?echo $_GET['masterid'];?>, <?echo $_SESSION['comnum']?>);</script>
<?
} else {
    $log->warn($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);

    echo '<script>alert("Work order was not created!'.$sql.'");</script>';
}
} else if ($_GET['submitType']=='woUpdate') {

$test = bill_update_wo($_GET['id'], $_GET['mid'], $_GET['maddr'], $_GET['cinfo1'], $_GET['cinfo2'],
    $_GET['rinfo1'], $_GET['rinfo2'], date('Y-m-d', strtotime($_GET['cdate'])),
        date('Y-m-d', strtotime('now')), $_GET['centered'], $_GET['rentered'],
    $_GET['rtech'], $_GET['part1'], $_GET['part2'], $_GET['part3'], $_GET['part4'], $_GET['part5'],
    $_GET['cost1'], $_GET['cost2'], $_GET['cost3'], $_GET['cost4'], $_GET['cost5'], $_GET['cost_total'], $_GET['comment'], $_GET['metertype'], $_GET['metermiu']);
if ($test) {
    $log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);

    ?>
<script>dynamic_Select('includes/acinfo.php', '#accinfo','worder', <?echo $_GET['masterid'];?>, <?echo $_SESSION['comnum']?>);</script>
<?
} else {
    $log->warn($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);

    echo 'There was a database error';
}    
} else if ($_GET['submitType']=='Edit') {
    $result = bill_get_worder($_GET['id']);
    if ($result[0]['mid']>0) {

        $mresult = bill_get_customer1($_SESSION['comnum'], $result[0]['mid']);
        } else {
        $mresult = bill_get_customer3($result[0]['masterid']);
        }

    ?>
    <script>
   function setType(type)
   {
      //formName is the name of your form
      document.accinform.submitType.value = type;
   }
    </script>
    <script>
   function setTotal()
   {
      //formName is the name of your form
      var type1 = 0.0;
      type1 = type1 + parseFloat(document.accinform.cost1.value);
      type1 = type1 + parseFloat(document.accinform.cost2.value);
      type1 = type1 + parseFloat(document.accinform.cost3.value);
      type1 = type1 + parseFloat(document.accinform.cost4.value);
      type1 = type1 + parseFloat(document.accinform.cost5.value);
      document.accinform.cost_total.value = type1.toFixed(2);
   }
</script>
	<script>
	$(function() {
                $( "#cdate" ).datepicker({
			showOn: "button",
			buttonImage: "assets/img/calendar.gif",
			buttonImageOnly: true
		});
	});
	</script>
<form name="accinform" action="#" onsubmit="jQuery.ajax({url:'includes/nwoinfo.php',
    data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#accinfo').html(data);}});return false;">
    <table border="1">
        <tr>
            <td>
    <label for="masterid">Account</label>
            </td>
            <td>
    <input type="hidden" name="masterid" id="masterid" value="<?echo $result[0]['masterid']?>">
    <?echo $mresult[0]['account']?>
            </td>
            <td>
    <label for="mid">Meter ID</label>
            </td>
            <td>
    <input type="text" name="mid" id="mid" value="<?echo $result[0]['mid'];?>">
            </td>
        </tr>
        <tr>
            <td> 
    <label for="addr">Address</label>
            </td>
            <td>
    <input type="text" name="addr" id="addr" value="<?echo $result[0]['addr']?>">
            </td>
            <td>
    <label for="maddr">Meter Address</label>
            </td>
            <td>
    <input type="text" name="maddr" id="maddr" value="<?echo $mresult[0]['maddr'];?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="metertype">Meter Type</label>
            </td><td>
    <input type="text" name="metertype" id="metertype" value="<?echo $result[0]['metertype']?>">
            </td>
            <td>
    <label for="metermiu">Radio Serial Number</label>
            </td>
            <td>
    <input type="text" name="metermiu" id="metermiu" value="<?if ($result[0]['metermiu']>0) {echo $result[0]['metermiu'];};?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="rinfo1">Problem</label>
            </td>
            <td>
    <input type="text" name="rinfo1" id="rinfo1" value="<?echo $result[0]['rinfo1']?>">
            </td>
            <td>
    <label for="rinfo2">Problem ext.</label>
            </td>
            <td>
    <input type="text" name="rinfo2" id="rinfo2" value="<?echo $result[0]['rinfo2']?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="cinfo1">Completion</label>
            </td>
            <td>
    <input type="text" name="cinfo1" id="cinfo1" value="<?echo $result[0]['cinfo1']?>">
            </td>
            <td>
    <label for="lat">Latitude</label>
            </td>
            <td>
    <input type="text" name="lat" id="lat" value="<?echo $mresult[0]['lat'];?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="cinfo2">Completion ext</label>
            </td>
            <td>
    <input type="text" name="cinfo2" id="cinfo2" value="<?echo $result[0]['cinfo2']?>">
            </td>
            <td>
    <label for="lon">Longitude</label>
            </td>
            <td>
    <input type="text" name="lon" id="lon" value="<?echo $mresult[0]['lon'];?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="rtech">Assigned to</label>
            </td>
            <td>
    <input type="text" name="rtech" id="rtech" value="<?echo $result[0]['rtech']?>">
            </td>
            <td>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>
    <label for="rentered">Entered by</label>
            </td>
            <td>
    <input type="text" name="rentered" id="rentered" value="<?echo $result[0]['rentered']?>">
            </td>
            <td>
    <label for="bdate">Date Entered</label>
            </td>
            <td>
    <input type="text" name="bdate" id="bdate" value="<?echo date('m/d/Y', strtotime($result[0]['rdate']));?>" disabled="disabled">
            </td>
        </tr>
        <tr>
            <td>
    <label for="centered">Completed By</label>
            </td>
            <td>
    <input type="text" name="centered" id="centered" value="<?echo $result[0]['centered']?>">
            </td>
            <td>
    <label for="cdate">Date Completed</label>
            </td>
            <td>
    <input type="text" name="cdate" id="cdate" value="<?echo date('m/d/Y', strtotime($result[0]['cdate']));?>">
            </td>
        </tr>
    </table>
    <table border="1">
        <tr>
            <th>Work / Part Description</th>
            <th>Cost</th>
        </tr>
        <tr>
            <td>
    <input type="text" name="part1" id="part1" value="<?echo $result[0]['part1']?>">
            </td>
            <td>
    <input type="text" name="cost1" id="cost1" value="<?echo $result[0]['cost1']?>" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <input type="text" name="part2" id="part2" value="<?echo $result[0]['part2']?>" >
            </td>
            <td>
    <input type="text" name="cost2" id="cost2" value="<?echo $result[0]['cost2']?>" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <input type="text" name="part3" id="part3" value="<?echo $result[0]['part3']?>">
            </td>
            <td>                
    <input type="text" name="cost3" id="cost3" value="<?echo $result[0]['cost3']?>" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <input type="text" name="part4" id="part4" value="<?echo $result[0]['part4']?>">
            </td>
            <td>
    <input type="text" name="cost4" id="cost4" value="<?echo $result[0]['cost4']?>" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <input type="text" name="part5" id="part5" value="<?echo $result[0]['part5']?>">
            </td>
            <td>
    <input type="text" name="cost5" id="cost5" value="<?echo $result[0]['cost5']?>" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <label for="cost_total">Total Cost</label>
            </td>
            <td>
    <input type="text" name="cost_total" id="cost_total" value="<?echo $result[0]['cost_total']?>">
            </td>
        </tr>
        <tr>
            <td> 
    <label for="comment">Comments</label>
            </td>
            <td>
    <input type="text" name="comment" id="comment" value="<?echo $result[0]['comment']?>">
            </td>
        </tr>
        <tr>
            <td>
    <label title="This will update on the record automatically anytime you click on the Add Work Order Button." for="udate">Last Updated</label>
            </td>
            <td>
    <input title="This will update on the record automatically anytime you click on the Add Work Order Button." type="text" name="udate" id="udate" value="<?echo date('m/d/Y', strtotime($result[0]['udate']));?>" disabled="disabled"/>
            </td>
        </tr>
        <tr>
            <td>
    <input type="hidden" name="submitType">
    <input type="hidden" name="id" value="<?echo $result['0']['id']; ?>">
    <input type="submit" onclick="setType('woUpdate')" value="Update Work Order"/>
            </td>
        </tr>
    </table>
</form>
<button type="button" title="Work Orders for this customer." value="Add sCharge" onClick="dynamic_Select('includes/acinfo.php', '#accinfo', 'worder', <?echo $mresult[0]['account'];?>,'0')">Back to Work Order List</button>
<button type="button" title="Print this work order." value="Add sCharge" onClick="window.open('includes/woprint.php?submitType=workorder&id=<?php echo $result[0]['id'];?>', '_blank'); return false;">Print Work Order</button>
<?php
} else if ($_GET['submitType']=='cust'){
    $test= $result = bill_get_customer($_SESSION['comnum'], $_GET['id']);
    if ($test){
?>
    <script>
   function setTotal()
   {
      //formName is the name of your form
      var type1 = parseFloat(document.accinform.cost1.value);
      type1 = type1 + parseFloat(document.accinform.cost2.value);
      type1 = type1 + parseFloat(document.accinform.cost3.value);
      type1 = type1 + parseFloat(document.accinform.cost4.value);
      type1 = type1 + parseFloat(document.accinform.cost5.value);
      document.accinform.cost_total.value = type1.toFixed(2);
   }
</script>
	<script>
	$(function() {
		$( "#rdate" ).datepicker({
			showOn: "button",
			buttonImage: "assets/img/calendar.gif",
			buttonImageOnly: true
		});
                $( "#cdate" ).datepicker({
			showOn: "button",
			buttonImage: "assets/img/calendar.gif",
			buttonImageOnly: true
		});
	});
	</script>
<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/nwoinfo.php',
    data: jQuery(this).serialize(), cache: false, success: function(data){jQuery('#accinfo').html(data);}});return false;">
    <table border="1">
        <tr>
            <td>
    <label for="masterid">Account</label>
            </td>
            <td>
    <input type="hidden" name="masterid" id="masterid" value="<?echo $result[0]['masterid']?>">
    <?echo $result[0]['account']?>
            </td>
            <td>
    <label for="mid">Meter ID</label>
            </td>
            <td>
    <input type="text" name="mid" id="mid" value="<?if ($result[0]['meternum']>0) {echo $result[0]['meternum'];};?>">
            </td>
        </tr>
        <tr>
                <td>
    <label for="addr">Address</label>
            </td><td>
    <input type="text" name="addr" id="addr" value="<?echo $result[0]['addr']?>">
            </td>
            <td>
    <label for="maddr">Meter Address</label>
            </td>
            <td>
    <input type="text" name="maddr" id="maddr" value="<?if ($result[0]['meternum']>0) {echo $result[0]['maddr'];};?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="metertype">Meter Type</label>
            </td><td>
    <input type="text" name="metertype" id="metertype" value="<?echo $result[0]['metertype']?>">
            </td>
            <td>
    <label for="metermiu">Radio Serial Number</label>
            </td>
            <td>
    <input type="text" name="metermiu" id="metermiu" value="<?if ($result[0]['metermiu']>0) {echo $result[0]['metermiu'];};?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="rinfo1">Problem</label>
            </td>
            <td>
    <input type="text" name="rinfo1" id="rinfo1" >
            </td>
            <td>
    <label for="rinfo2">Problem ext.</label>
            </td>
            <td>
    <input type="text" name="rinfo2" id="rinfo2" >
            </td>
        </tr>
        <tr>
            <td>
    <label for="cinfo1">Completion</label>
            </td>
            <td>
    <input type="text" name="cinfo1" id="cinfo1" >
            </td>
            <td>
    <label for="lat">Latitude</label>
            </td>
            <td>
    <input type="text" name="lat" id="lat" value="<?if ($result[0]['meternum']>0) {echo $result[0]['lat'];};?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="cinfo2">Completion ext</label>
            </td>
            <td>
    <input type="text" name="cinfo2" id="cinfo2" >
            </td>
            <td>
    <label for="lon">Longitude</label>
            </td>
            <td>
    <input type="text" name="lon" id="lon" value="<?if ($result[0]['meternum']>0) {echo $result[0]['lon'];};?>">
            </td>
        </tr>
        <tr>
            <td>
    <label for="rtech">Assigned to</label>
            </td>
            <td>
    <input type="text" name="rtech" id="rtech" >
            </td>
            <td>
    <label for="udate">Date Last Updated</label>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>
    <label for="rentered">Entered by</label>
            </td>
            <td>
    <input type="text" name="rentered" id="rentered" >
            </td>
            <td>
    <label for="rdate">Date Entered</label>
            </td>
            <td>
    <input type="text" name="rdate" id="rdate" >
            </td>
        </tr>
        <tr>
            <td>
    <label for="centered">Completed By</label>
            </td>
            <td>
    <input type="text" name="centered" id="centered" >
            </td>
            <td>
    <label for="cdate">Date Completed</label>
            </td>
            <td>
    <input type="text" name="cdate" id="cdate" >
            </td>
        </tr>
    </table>
    <table border="1">
        <tr>
            <th>Work / Part Description</th>
            <th>Cost</th>
        </tr>
        <tr>
            <td>
    <input type="text" name="part1" id="part1" >
            </td>
            <td>
    <input type="text" name="cost1" id="cost1" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <input type="text" name="part2" id="part2" >
            </td>
            <td>
    <input type="text" name="cost2" id="cost2" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <input type="text" name="part3" id="part3" >
            </td>
            <td>               
    <input type="text" name="cost3" id="cost3" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <input type="text" name="part4" id="part4" >
            </td>
            <td>
    <input type="text" name="cost4" id="cost4" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <input type="text" name="part5" id="part5" >
            </td>
            <td>
    <input type="text" name="cost5" id="cost5" onblur="setTotal()">
            </td>
        </tr>
        <tr>
            <td>
    <label for="cost_total">Total Cost</label>
            </td>
            <td>
    <input type="text" name="cost_total" id="cost_total" >
            </td>
        </tr>
        <tr>
            <td> 
    <label for="comment">Comments</label>
            </td>
            <td>
    <input type="text" name="comment" id="comment" >
            </td>
        </tr>
        <tr>
            <td>
    <input type="hidden" name="submitType">
    <input type="submit" onclick="setType('woAdd')" value="Add Work Order"/>
            </td>
        </tr>
    </table>
</form>
<button type="button" title="Work Orders for this customer." value="Add sCharge" onClick="dynamic_Select('includes/acinfo.php', '#accinfo', 'worder', <?echo $result[0]['masterid'];?>,'0')">Cancel Add - Back to Work Order List</button>
<?php
} else {
?>
<script>dynamic_Select('includes/acinfo.php', '#accinfo','worder', <?echo $_GET['id'];?>, <?echo $_SESSION['comnum']?>);</script>
<?
}
}
} else {
?>
<script>alert('You do not have a high enough access level to post payments\n\
or charges!\n\
Please see your administrator.');</script>
    <?    
}
?>