include './resources/init.php';
fAuthorization::requireLoggedIn();
$tmpl->place('header');
?>
<script type="text/javascript" src="./js/document-pi-view.js"></script>
<?php 
$tmpl->place('menu');
?>
<div id="content" class="span-24 last">
	<?php 
$tmpl->place('menuDocument');
if (!isset($_GET['id'])) {
    echo "<div class=\"span-24 ui-state-error ui-corner-all\">\n\t\t\t\t\t<span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin-right: 30px;\"></span>\n\t\t\t\t\tYou arrived to this page in error</div>";
} else {
    try {
        $production = new Production_issue($_GET['id']);
        $production_details = Production_issue_detail::findDetail($_GET['id']);
        ?>
	<h2>Production Issue Form</h2>
  <div align=left> <a href=report/pi-pdf.php?PInum=<?php 
        echo $_GET['id'];
        ?>
><b>Download PDF</b></a></div><br>
	<div class="form-frame span-23 last">
		<h3>Production Issue Form</h3><br />
		<label for="doc_num">Document Number </label><span id="docNum"><?php 
        echo $production->prepareDocNumber();
        ?>
</span><br />
		<label for="doc_date">Document Date </label><?php 
        echo $production->prepareDocDate("j F Y");
    }
    ?>
		</tbody>
	</table>
	<?php 
}
?>
	<h3>List (Completed)</h3>
	<table>
		<thead>
			<tr><th>Document Number</th><th>Document Date</th><th>Issuer and Receiver</th><th style="width: 100px;">Status</th></tr>
		</thead>
		<tbody>
			<?php 
try {
    $productionEntries = Production_issue::findByStatus('completed', 10);
    foreach ($productionEntries as $productionEntry) {
        echo "<tr class=\"linkable\"><td class=\"docNumber\">" . $productionEntry->prepareDocNumber() . "</td>";
        echo "<td>" . $productionEntry->prepareDocDate("j F Y") . "</td>";
        echo "<td>" . $productionEntry->prepareIssuer() . "</td>";
        echo "<td>" . $productionEntry->prepareStatus() . "</td></tr>";
    }
} catch (fExpectedException $e) {
    echo $e->printMessage();
}
?>
		</tbody>
	</table>
</div>
<?php 
$tmpl->place('footer');
         if ($_POST['issuer'] != "") {
             $production->setIssuer($_POST['issuer']);
             $production->setIssuerDate($_POST['issuer_date']);
         }
         if ($_POST['receiver'] != "") {
             $production->setReceiver($_POST['receiver']);
             $production->setReceiverDate($_POST['receiver_date']);
         }
         $production->store();
     } catch (fExpectedException $e) {
         echo $e->printMessage();
     }
 } else {
     if ($_POST['type'] == "cancel") {
         try {
             $production = new Production_issue($_POST['key']);
             $production->setStatus("cancelled");
             $production->store();
         } catch (fExpectedException $e) {
             echo $e->printMessage();
         }
     } else {
         if ($_POST['type'] == "count") {
             //$records = Production_issue::findAll();
             $records = Production_issue::findByBranch(fRequest::get('branch', 'string'), fRequest::get('doctype', 'string'));
             echo sprintf("%03d", $records->count() + 1);
         } else {
             if ($_POST['type'] == "deleteDetail") {
                 Production_issue_detail::deleteDetail($_POST['key']);
             }
         }