<div class="enquiries-view">

    <dl class="enquirySummary">
	<dt>Enquiry:</dt>
	<dd><?=$enquiry['Enquiry']['title']?></dd>
	<dt>Customer:</dt>
	<dd><?=$html->link($enquiry['Customer']['name'], array('controller'=>'customers','action'=>'view', $enquiry['Customer']['id']));?>
	</dd>

	<dt>Contact:</dt>
	<dd><?=$html->link($enquiry['Contact']['first_name'].' '.$enquiry['Contact']['last_name'], array('controller'=> 'users', 'action'=>'view', $enquiry['Contact']['id'])); ?>
	    <?=$html->link($enquiry['Contact']['email'], 'mailto:'.$enquiry['Contact']['email']);?>
	</dd>

	<?

	/* Copy+pasta. Should be a helper or an element or something. #Aug_2011_Hackfest_for_victory_and_liberation_from_this_project! */
	if ($enquiry['Status']['id'] == 3) {
	    $class = ' class="jobwon"';
	} else if ($enquiry['Status']['id'] == 4) {
	    $class = ' class="joblost"';
	} else if ($enquiry['Status']['id'] == 8) {
	    $class = ' class="joblost"';
	} else if ($enquiry['Status']['id'] == 9) {
	    $class = ' class="joblost"';
	} else if ($enquiry['Status']['id'] == 10) {
	    $class = ' class="joblost"';
	} else if ($enquiry['Status']['id'] == 6) {
	    $class = ' class="information"';
	} else if ($enquiry['Status']['id'] == 11) {
	    $class = ' class="informationsent"';
	} else if ($enquiry['Status']['id'] == 5) {
	    $class = ' class="quoted"';
	} else if ($enquiry['Status']['id'] == 1) {
	    $class = ' class="requestforquote"';
	}
	?>

	<dt>Status:</dt>
	<dd <?=$class?>> <?php echo $html->link($enquiry['Status']['name'], array('controller'=> 'statuses', 'action'=>'view', $enquiry['Status']['id'])); ?></dd>
    </dl>

    <h3>Details</h3>
    <dl><?php $i = 0;
	$class = ' class="altrow"';?>


        <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Assigned to User'); ?></dt>
        <dd<?php if ($i++ % 2 == 0) echo $class;?>>
	    <?php echo $html->link($enquiry['User']['username'], array('controller'=> 'users', 'action'=>'view', $enquiry['User']['id'])); ?>
            &nbsp;
        </dd>

        <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('State'); ?></dt>
        <dd<?php if ($i++ % 2 == 0) echo $class;?>>
	    <?php echo $html->link($enquiry['State']['name'], array('controller'=> 'states', 'action'=>'view', $enquiry['State']['id'])); ?>
            &nbsp;
        </dd>
        <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Country'); ?></dt>
        <dd<?php if ($i++ % 2 == 0) echo $class;?>>
	    <?php echo $html->link($enquiry['Country']['name'], array('controller'=> 'countries', 'action'=>'view', $enquiry['Country']['id'])); ?>
            &nbsp;
        </dd>
        <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('GST Applicable'); ?></dt>
        <dd<?php if ($i++ % 2 == 0) echo $class;?>>
	    <?php
	    if($enquiry['Enquiry']['gst'] == 0) {
		echo "No - GST Not Applicable";
	    }
	    else {
		echo "Yes - GST Applicable";
	    }
	    ?>
            &nbsp;
        </dd>
        <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Principle'); ?></dt>
        <dd<?php if ($i++ % 2 == 0) echo $class;?>>
	    <?php echo $html->link($enquiry['Principle']['name'], array('controller'=> 'principles', 'action'=>'view', $enquiry['Principle']['id'])); ?>
            &nbsp;
        </dd>
        <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Comments'); ?></dt>
        <dd<?php if ($i++ % 2 == 0) echo $class;?>>
            <span class="enquiry-view-comments">
		<?php echo $enquiry['Enquiry']['comments']; ?>
            </span>
            &nbsp;
        </dd>
        <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Billing Address'); ?></dt>
        <dd<?php if ($i++ % 2 == 0) echo $class;?>>
	    <?php
	    if($enquiry['BillingAddress']['customer_id'] != NULL) {
		echo $enquiry['BillingAddress']['address'].'<br>';
		echo $enquiry['BillingAddress']['city'].' '.$billingaddress['State']['name'].' '.$enquiry['BillingAddress']['postcode'].'<br>';
		echo '<b>'.$billingaddress['Country']['name'].'</b>';
	    }
	    else {
		echo 'No Billing Address Assigned for this Enquiry';
	    }
	    ?>
            &nbsp;

        </dd>
        <dt<?php if ($i % 2 == 0) echo $class;?>><?php __('Shipping Address'); ?></dt>
        <dd<?php if ($i++ % 2 == 0) echo $class;?>>
	    <?php
	    if($enquiry['ShippingAddress']['customer_id'] != NULL) {
		echo $enquiry['ShippingAddress']['address'].'<br>';
		echo $enquiry['ShippingAddress']['city'].' '.$shippingaddress['State']['name'].' '.$enquiry['ShippingAddress']['postcode'].'<br>';
		echo '<b>'.$shippingaddress['Country']['name'].'</b>';
	    }
	    else {
		echo 'No Shipping Address Assigned for this Enquiry';
	    }
	    ?>
            &nbsp;

        </dd>

    </dl>

    <div class="enquiry-actions">
        <ul>
            <li><?php echo $html->link(__('Edit Enquiry', true), array('action'=>'edit', $enquiry['Enquiry']['id'])); ?> </li>
        </ul>
    </div>
</div>
<div class="principle-emails">
    <?php
    $boxdata = array();
    $boxdata['enquirynumber'] = $enquiry['Enquiry']['title'];
//$boxdata['principle_emails'] = $principle_emails;
    ?>
    <?php echo $this->element('principle_contacts_email_box', array('enquirynumber' => $enquiry['Enquiry']['title'],
    'principle_emails' => $principle_emails	)); ?>


</div>


<div class="related">
  <h3><?php if($enquiry['Enquiry']['quote_count'] > 1) {
        $word = ' Quotes';
	    }
      else {
        $word = ' Quote'; 
      }
	    echo $enquiry['Enquiry']['quote_count'].$word;
?>
    </h3>
  
    <? echo $this->element('quote_table', $quotes);
    ?>


</div>

<div class="related">
    <h3>Jobs from this Enquiry</h3>

    <table cellpadding="0" cellspacing="0">
        <tr>

            <th>Job Number</th>
	    <th>Customer Order Number</th>
            <th>Date Order Received</th>
            <th>Date Scheduled Ex-Works</th>
            <th>Date Sent to Customer</th>

            <th>Domestic Freight Paid By</th>
    
            <th class="actions"><?php __('Actions');?></th>
        </tr>
	<?php
	$i = 0;
	foreach ($jobs as $job):
	    $class = null;
	    if ($i++ % 2 == 0) {
		$class = ' class="altrow"';
	    }
	    ?>
        <tr<?php echo $class;?>>


            <td>
		    <?php echo $job['Job']['title']; ?>
            </td>
            <td>
		    <?php echo $job['Job']['customer_order_number']; ?>
            </td>


            <td>
		    <?php echo $this->element('isEmptyDate',
		    array('date'=>$job['Job']['date_order_received'])); ?>
            </td>

            <td>
		    <?php echo $this->element('isEmptyDate',
		    array('date'=>$job['Job']['date_scheduled_ex_works'])); ?>
            </td>
            <td>
		    <?php echo $this->element('isEmptyDate',
		    array('date'=>$job['Job']['date_order_sent_to_customer'])); ?>
            </td>

            <td>
		    <?php echo $job['Job']['domestic_freight_paid_by']; ?>
            </td>
         
            <td class="actions">
		    <?php echo $html->link(__('View', true), array('controller'=>'jobs', 'action' => 'view', $job['Job']['id'])); ?>
		    <?php //echo $html->link(__('New Invoice for this Job', true), array('controller'=>'documents', 'action' => 'newDocument/invoice/'.$job['Job']['enquiry_id'].'/'.$job['Job']['id'])); ?>
            </td>
        </tr>
	<?php endforeach; ?>
    </table>

</div>
<div class="actions">
    <?=$javascript->link('view_enquiry');?>
    <ul>
        <li><button class="addJob" id="<?=$enquiry['Enquiry']['id'];?>">New Job</button></li>
    </ul>
</div>

<div id="jobAddedDialog" title="New Job Created">
    Job has been created.<br>
    Redirecting you now to fill in the details.
</div>

<div id="jobConfirm" title="Are you sure you want to make a new Job">
    This will create a new Job and a Job number. Are you sure you want to do this?
</div>

<div id="jobsListDialog" style="display: none;">
    Select the Job this Invoice is for.
<ul id="jobsListUL">
    <? foreach($jobs as $job):?>
    <li id="<?=$job['Job']['id'];?>"><?=$html->link($job['Job']['title'], "/documents/newDocument/invoice/{$job['Job']['enquiry_id']}/{$job['Job']['id']}");?></li>
    <? endforeach;?>
</ul>
</div>

<div class="related">
    <h3>Order Acknowledgements</h3>
    <table class="quotetable">
	<tr>
	    <th>Created</th>
	    <th>Issue Date</th>
	    <th>Job</th>
	    <th class="actions">Actions</th>
	</tr>
	<?php foreach ($order_acknowledgements as $oa):?>
	<tr>
	    <td><?=$oa['OrderAcknowledgement']['created'];?></td>
	    <td><?=$oa['OrderAcknowledgement']['issue_date'];?></td>
	    <td><?=$html->link($oa['Job']['title'], '/jobs/view/'.$oa['Job']['id']);?></td>
	    <td class="actions"><?=$html->link('View', '/documents/view/'.$oa['OrderAcknowledgement']['document_id']);?></td>
	</tr>

	<? endforeach;?>
    </table>
	<h4>View a quotation to create an Order Acknowledgement</h4>

</div>


<div class="related">
    <h3><?php __($enquiry['Enquiry']['invoice_count'].' Invoices for this Enquiry');?></h3>
    <table class="quotetable">

	<tr>

            <th>Issue Date</th>
            <th>Due Date</th>
            <th>Invoice Number</th>
            <th>Job</th>
            <th>Customer</th>
            <th>Paid / Unpaid</th>
            <th>Payment Received Date</th>
        
            <th class="actions"><?php __('Actions');?></th>
        </tr>
	<?php
	$i = 0;
	foreach ($invoices as $invoice):
	    $class = null;
	    if ($i++ % 2 == 0) {
		$class = ' class="altrow"';
	    }
	    ?>
        <tr<?php echo $class;?>>       
            <td>
		    <?php echo date('j M Y',$time->toUnix($invoice['Invoice']['issue_date'])); ?>
            </td>
            <td>
		    <?php echo date('j M Y',$time->toUnix($invoice['Invoice']['due_date'])); ?>
            </td>
            <td>
		    <?php echo $invoice['Invoice']['title']; ?>
            </td>
            <td>
		    <?php echo $html->link($invoice['Job']['title'], array('controller'=>'jobs', 'action'=>'view',$invoice['Invoice']['job_id'])); ?>
            </td>
            <td>
		    <? echo $html->link($invoice['Customer']['name'], array('controller'=> 'customers', 'action'=>'view', $invoice['Customer']['id'])); ?>
            </td>
            <td>
		    <?php
		    if($invoice['Invoice']['paid'] == 0) {
			echo "UNPAID";
			echo "</td>";
			echo "<td>";
			echo "N/A";
			echo "</td>";
		    }
		    else {
			echo "PAID";
			echo "</td>";
			echo "<td>";
			echo date('j M Y',$time->toUnix($invoice['Invoice']['payment_received_date']));
			echo "</td>";
		    }
		    ?>

            
            <td class="actions">
		    <?php echo $html->link(__('View', true), array('controller'=>'documents', 'action' => 'view', $invoice['Invoice']['document_id'])); ?>
            </td>
        </tr>
	<?php endforeach; ?>

    </table>
</div>

<div class="actions">
    <ul>
        <li><button class="addInvoice" id="<?=$enquiry['Enquiry']['id'];?>">New Empty Invoice</button></li>
	</ul>
	<h4>Note: View an OrderAcknowledgement to create an Invoice pre-filled with the same data</h4>
</div>

<div id="showemail">

</div>


<div class="related">
    <h3>
	Emails for this Enquiry<?php echo $html->image('internet-mail.png'); ?>
    </h3>
    <?php echo $this->element('email_table_ajax', array('emails' => $emails)); ?>
    <?php //echo $this->element('email_table_ajax', $enquiry, array('cache'=>'+1 day')); ?>
</div>

<?php debug($order_acknowledgements); ?>
<?php // debug($jobs);?>
<?php //debug($emails); ?>
