<?php
/* @var $invoice IncomeInvoice */
set_page_title($invoice->getObjectName());

$currency = Currencies::getCurrency($invoice->getCurrencyId());
$currency_symbol = $currency instanceof Currency ? $currency->getSymbol() : "";

$project_member = null;
$project_ot = ObjectTypes::findByName('project');
$invoice_members = $invoice->getMembers();
foreach ($invoice_members as $member) {
	if ($member instanceof Member && $member->getObjectTypeId() == $project_ot->getId()) {
		$project_member = $member;
		break;
	}
}

$terms_text = "";
$payment_period_cp = CustomProperties::getCustomPropertyByCode($invoice->getObjectTypeId(), 'invoice_payment_period');
if ($payment_period_cp instanceof CustomProperty) {
	$payment_period_cp_val = CustomPropertyValues::getCustomPropertyValue($invoice->getId(), $payment_period_cp->getId());
	if ($payment_period_cp_val instanceof CustomPropertyValue) {
		$terms_text = $payment_period_cp_val->getValue();
	}
}

$project_and_po_number_text = "";
$inv_po_number_cp = CustomProperties::getCustomPropertyByCode($invoice->getObjectTypeId(), "po_number");
if ($inv_po_number_cp instanceof CustomProperty) {
	$cpval = CustomPropertyValues::getCustomPropertyValue($invoice->getId(), $inv_po_number_cp->getId());
	if ($cpval instanceof CustomPropertyValue) {
		$project_and_po_number_text = trim($cpval->getValue());
	}
}

// Get Project Number
$project_number = "";
$project_id_cp = CustomProperties::getCustomPropertyByCode($project_ot->getId(), "project_id");
if ($project_id_cp instanceof CustomProperty) {
    $cpval = CustomPropertyValues::getCustomPropertyValue($project_member->getObjectId(), $project_id_cp->getId());
    if ($cpval instanceof CustomPropertyValue) {
        $project_number = $cpval->getValue();
    }
}

$due_date_text = '';
if ($invoice->getExpirationDate() instanceof DateTimeValue) {
	$due_date_text = format_date($invoice->getExpirationDate(),null,0);
}


$billing_from_cp = CustomProperties::getCustomPropertyByCode(IncomeInvoices::instance()->getObjectTypeId(), 'billing_period_from');
$billing_to_cp = CustomProperties::getCustomPropertyByCode(IncomeInvoices::instance()->getObjectTypeId(), 'billing_period_to');
$billing_from_date = '';
$billing_to_date = '';
$cp_val = CustomPropertyValues::getCustomPropertyValue($invoice->getId(), $billing_from_cp->getId());
if ($cp_val && $cp_val->getValue() != '') {
	try {
		$dt = DateTimeValueLib::dateFromFormatAndString('Y-m-d', $cp_val->getValue());
		$billing_from_date = format_date($dt, null, 0);
	} catch (Exception $e) {}
}
$cp_val = CustomPropertyValues::getCustomPropertyValue($invoice->getId(), $billing_to_cp->getId());
if ($cp_val && $cp_val->getValue() != '') {
	try {
		$dt = DateTimeValueLib::dateFromFormatAndString('Y-m-d', $cp_val->getValue());
		$billing_to_date = format_date($dt, null, 0);
	} catch (Exception $e) {}
}

$project_member_complete_name = "";
if ($project_member) {
	$tmp_array = array($project_member);
	build_member_list_text_to_show_in_trees($tmp_array);
	$project_member_complete_name = $project_member->getName();
}

$project_manager_name = "";
if ($project_member) {
	$project_manager_id = get_associated_status_member_id($project_member, Dimensions::findByCode('project_managers'));
	$project_manager = Members::getMemberById($project_manager_id);
	if ($project_manager) $project_manager_name = $project_manager->getName();
}

function get_custom_property_value($ot_id, $member, $code) {
    $cp = CustomProperties::getCustomPropertyByCode($ot_id, $code);
    if($cp instanceof CustomProperty){
        $value = CustomPropertyValues::getCustomPropertyValue($member->getObjectId(), $cp->getId());
        return $value instanceof CustomPropertyValue ? $value->getValue() : '';
    } else {
        return '';
    }
}
// Get project description
$project_description = get_custom_property_value($project_ot->getId(), $project_member, 'description_special');


// Get client info
$client_id = get_associated_status_member_id($project_member, Dimensions::findByCode('customers'));
$client_member = Members::getMemberById($client_id);
$client_name = $client_member->getName();
$client_ot = ObjectTypes::findByName('customer');
$client_ot_id = $client_ot->getId();

// Get main contact info
$main_contact_name = "";
$main_contact_id = get_custom_property_value($client_ot_id, $client_member, 'main_contact');
if($main_contact_id){
    $main_contact = Contacts::findById($main_contact_id);
    $main_contact_name = $main_contact instanceof Contact ? $main_contact->getName() : '';
}

// Get billing contact info
$billing_contact_name = "";
$billing_contact_phone = "";
$billing_contact_email = "";
$billing_contact_id = get_custom_property_value($client_ot_id, $client_member, 'billing_contact');
if($billing_contact_id){
    $billing_contact = Contacts::findById($billing_contact_id);
    if($billing_contact instanceof Contact) {
        // Get name
        $billing_contact_name = $billing_contact->getName();
        // Get telephone
        $contact_telephone = ContactTelephones::findOne(array('conditions' => "contact_id=".$billing_contact_id));
        if ($contact_telephone instanceof ContactTelephone){
            $billing_contact_phone = $contact_telephone->getNumber();
        }
        //Get email
        $contact_email = $billing_contact->getEmail();
        $billing_contact_email = $contact_email instanceof ContactEmail ? $contact_email->getEmailAddress() : '';
    }
}

// Get Client Reference 
$client_reference = get_custom_property_value($client_ot_id, $client_member, 'client_reference');

// Get Client Vat Registration Number
$client_vat_number = get_custom_property_value($client_ot_id, $client_member, 'vat_number');

// Get Client invoice instructions
$invoice_instructions = get_custom_property_value($client_ot_id, $client_member, 'invoice_instructions');



?>
<style>
    * {
        margin: 0;
        padding: 0;
    }

    body {
        font: 12px/1.2 Sans-Serif;
    }

    #page-wrap {
        width: 700px;
        margin: 10px auto;
        padding: 10px 0;
    }

    table {
        border-collapse: collapse;
    }

    table td, table th {
        border: 1px solid #555;
        padding: 3px;
        font-size: 12px;
    }
    table th {
        font-size: 12px;
    }

    #customer {
        overflow: hidden;
    }

    #logo {
        text-align: right;
        float: right;
        position: relative;
        margin-top: 25px;
        border: 1px solid #fff;
        max-width: 400px;
        max-height: 100px;
        overflow: hidden;
    }

    #customer-title {
        font-size: 20px;
        font-weight: bold;
        float: left;
    }

    #meta {
        width: 300px;
        float: right;
    }

    #meta td {
        text-align: right;
    }

    #meta td.meta-head {
        text-align: left;
        background: #eee;
    }

    #meta td {
        text-align: right;
    }

    #items {
        clear: both;
        width: 100%;
        margin: 0 0 0 0;
        border: 1px solid #555;
        /*min-height: 300px;*/
    }

    #pre-items {
        clear: both;
        width: 100%;
        margin: 10px 0 0 0;
        border: 1px solid #555;
    }
    #pre-items, #pre-items td {
    	border-bottom: 0px none;
    }

    #items th, #pre-items th {
        background: #56CE72;
        text-align: center !important;
    	text-transform: uppercase;
    	
    	
    }
    #items tr {
        height: 20px!important;
    }

    #items tr.item-row td {
        border: 1px solid #555;
        margin: 0!important;
        padding: 3px!important;
        vertical-align: top;
        border-bottom: 0px none;
        border-top: 0px none;
    }

    
    #items td.description {
        width: 250px;
    }
    
    #items .right {
        text-align: right;
    }
    
    #items .left {
        text-align: left;
    }

    #items .center {
        text-align: center;
    }

    #items .short {
        width: 90px;
        max-width: 120px;
    }

    #items td.total-line {
        border: 2px solid #111;
        text-align: left;
        font-weight: bold;
    }

    #items td.total-line-tax {
        border-right: 0;
        text-align: right;
    }

    #items td.total-value {
        padding: 5px;
        height: 15px;
        background: none;
        border: 2px solid #111;
        text-align: right;
        font-weight: bold;
    }


    #items td.balance {
        background: #eee;
    }

    #items td.blank {
        border: 0;
    }

    #terms {
        text-align: center;
        margin: 20px 0 0 0;
    }

    #terms h5 {
        text-transform: uppercase;
        font: 13px Sans-Serif;
        letter-spacing: 10px;
        border-bottom: 1px solid #555;
        padding: 0 0 8px 0;
        margin: 0 0 8px 0;
    }

    #terms {
        width: 100%;
        text-align: center;
    }
    
    td.print-invoice-status {
    	font-weight: bold;
    }
    td.print-invoice-status.pending {
    }
    td.print-invoice-status.confirmed {
    	background-color: #a8ce80;
    }
    td.print-invoice-status.canceled {
    	background-color: #222;
    	color: #eee;
    }
    
    div.bill-to {
    	margin: 0px 5px;
    	float: left;
    }
    div.bill-to div {
    	margin: 4px 0px;
    }
    div.owner-company-info {
    	margin: 25px 5px 5px 5px;
    	float: left;
    }
    div.owner-company-logo {
    	float: right;
    	text-align: center;
    }
    div.owner-company-logo img{
        height: 80px;
    }
    div.project-table {
    	margin-bottom: 15px;
    }
    div.project-table td {
    	text-align: left;
    	width: 28%;
    }
    div.project-table table {
    	width: 100%;
    	text-align: center;
    }
    .invoice-info-table tr td {
    	font-size: 12px;
    }
    .invoice-info-table th, div.project-table th {
    	background: #56CE72;
    }
</style>



    <div id="page-wrap">
        <div id="customer">
            <div style="">
                <?php if ($invoice_options['invoice_logo']): ?>
                    <div class="owner-company-logo">
                        <img src="<?php echo $invoice_options['invoice_logo'] ?>" alt="logo"/>
                    </div>
                    <!--<div style="clear:both"></div>-->
                    
                <?php endif; ?>
                

                <div class="bill-to" style="margin-top: 20px;">
                    <div style="font-size:125%; margin-bottom: 15px;"><b><?php echo strtoupper('TAX INVOICE') ?></b></div>

                    <!-- Company info -->
                    <div><?php echo $main_contact_name; ?></div>
                    <div><?php echo $bill_to_information['company_name']; ?></div>
                    <div><?php echo $bill_to_information['ordered_by_name']; ?></div>
                    <div><?php echo $bill_to_information['address']; ?></div>
                    <div><?php 
                            $city_state_str = $bill_to_information['city'];
                    ?></div>
                    <div><?php echo $bill_to_information['zip_code']; ?></div>

                    <!-- Billing contact info -->
                    <?php if($billing_contact_name != '') { ?>
                        <div class="attention" style="margin-top: 15px;">
                        <div style="text-decoration: underline; font-weight: bold;"><?php echo $billing_contact_name != '' ? 'Attention: '.$billing_contact_name : ''; ?></div>
                        <div><?php echo 'Contact number: '.$billing_contact_phone; ?></div>
                        <div><?php echo 'Email: '.$billing_contact_email; ?></div>
                    </div>
                    
                     <?php } ?>

                    <!-- Vat registration info -->
                    <div style="margin-top: 30px; margin-bottom: 10px;">
                        <div style="font-weight: bold;">Our Vat Registration Number: 4170261103</div>
                        <div style="font-weight: bold;">Client Vat Registration Number: <?php echo $client_vat_number; ?></div>
                    </div>

    
                </div>
                        
	            <div style="margin:80px 5px 10px; float: right;">
		             
       				 <div class="clear"></div>
			        <div style="float: right;text-align:right;margin-bottom:10px;">
			            	
			                <table style="float: right; width: 300px;" class="invoice-info-table">
			                        <!--<tr>
			                            <th style="text-align:center; font-weight:bold; font-size:12px; min-width:100px;"><?php echo strtoupper(lang('date')) ?></th>
			                            <th style="text-align:center; font-weight:bold; font-size:12px; min-width:100px;"><?php echo strtoupper(lang('invoice')) . " #" ?></th>
			                        </tr>-->
			                        <tr>
                                        <td style="text-align:left;">DATE</td>
			                            <td style="text-align:right;"><?php echo format_date($invoice->getDate()->advance(logged_user()->getUserTimezoneValue() * -1, false), 'j F Y'); ?></td>
			                        </tr>
                                    <tr>
                                        <td style="text-align:left;">INVOICE NUMBER</td>
			                            <td style="text-align:right;"><?php echo trim($invoice->getSeries() . " " . $invoice->getInvoiceNumber()) ?></td>
			                        </tr>
                                    <tr>
                                        <td style="text-align:left;">PROJECT NO</td>
			                            <td style="text-align:right;"><?php echo $project_number; ?></td>
                                    </tr>
                                    <tr>
                                        <td style="text-align:left;">CLIENT REFERENCE</td>
			                            <td style="text-align:right;"><?php echo $client_reference; ?></td>
                                    </tr>
    
			                </table>
			                <div class="clear"></div>
			        </div>
		        </div>
                
            </div>
            
        </div>
        
        
        <div style="clear:both"></div>
        
        
        <!--<div class="project-table">
        	<table>
        		<tr><th><b>PROJECT</b></th><th><b>PROJECT MANAGER</b></th><th><b>TERMS</b></th><th><b>DUE DATE</b></th></tr>
        		<tr><td><?php echo $project_member_complete_name ?></td>
        			<td><?php echo $project_manager_name ?></td>
        			<td><?php echo $terms_text ?></td>
        			<td><?php echo $due_date_text ?></td>
        		</tr>
        	</table>
        </div> -->

        <div class="project-table">
        	<table>
        		<tr>
                    <th><b>PROJECT DESCRIPTION</b></th>
                </tr>
        		<tr>
                    <td style="padding: 20px 5px;"><?php echo $project_description; ?></td>
        		</tr>
        	</table>
        </div>
        
        <div style="clear:both"></div>

	<?php
	$invoice_template = null;
	$invoice_template_cp = CustomProperties::getCustomPropertyByCode($invoice->getObjectTypeId(), 'invoice_template');
	$invoice_template_cp_val = CustomPropertyValues::getCustomPropertyValue($invoice->getId(), $invoice_template_cp->getId());
	if ($invoice_template_cp_val) {
		$invoice_template = IncomeInvoiceTemplates::findById($invoice_template_cp_val->getValue());
	}
	
	if ($invoice_template instanceof IncomeInvoiceTemplate) {
	?>
		<div style="clear:both"></div>
		
	<?php
        	echo $invoice_template->getHtmlDetailsTableForInvoice($invoice);
        } else {
	?>        

        <table id="items" style="margin:10px 0 0 0; font-size: 12px!important">
            
            <tr>
                <th style="width:230px;"><?php echo strtoupper('SERVICES AND DISBURSEMENTS') ?></th>
                <th style="width:30px;"><?php echo strtoupper('UNIT') ?></th>
                <th style="width:30px;"><?php echo strtoupper('QTY') ?></th>
                <th style="width:60px;"><?php echo strtoupper('RATE') ?></th>
                <th style="width:110px;"><?php echo strtoupper('amount') ?></th>
                <th style="width:110px;"><?php echo strtoupper('vat') ?></th>
                <th style="width:110px;"><?php echo strtoupper('total') ?></th>
            </tr>
            <?php 
            $invoice_tax = IncomeInvoiceTaxes::findOne(array('conditions' => 'invoice_id='.$invoice->getId()));
            $tax = $invoice_tax instanceof IncomeInvoiceTax ? $invoice_tax->getValue() / 100 : 0;
            $total_tax = $invoice_tax instanceof IncomeInvoiceTax ? $invoice_tax->getFixedValue() : 0;
            foreach ($invoice->getInvoiceLines() as $invoice_line):
                $line_currency = Currencies::getCurrency($invoice_line->getCurrencyId());
                $line_currency_symbol = $line_currency instanceof Currency ? $line_currency->getSymbol() : "";
                
                ?>
                <tr class="item-row">
                    <td class="description"><?php echo nl2br($invoice_line->getDetail()) ?></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td align="right"><?php echo $line_currency_symbol . ' ' . format_amount($invoice_line->getTotal(), 2) ?></td>
                    <td align="right"><?php echo $line_currency_symbol . ' ' . format_amount($invoice_line->getTotal() * $tax, 2) ?></td>
                    <td align="right"><?php echo $line_currency_symbol . ' ' . format_amount($invoice_line->getTotal() * 1 + $tax, 2) ?></td>
                </tr>
            <?php endforeach; ?>
            <!-- Total line -->
            <tr>
				<td class="total-line" style="text-align: left;"><b>TOTAL NOW DUE:</b></td>
                <td class="total-line" colspan="3"></td>
                <td class="total-value total-line">
                    <div id="subtotal"><?php echo $currency_symbol . ' ' . format_amount($invoice->getSubtotal(), 2) ?></div>
                </td>
                <td class="total-value total-line">
                    <div id="vattotal"><?php echo $currency_symbol . ' ' . format_amount($total_tax, 2) ?></div>
                </td>
                <td class="total-value total-line">
                    <div id="total"><?php echo $currency_symbol . ' ' . format_amount($invoice->getTotal(), 2) ?></div>
                </td>
            </tr>
        </table>

	<?php } ?>

        <!-- Invoice Instructions -->
        <div style="margin:20px 0;">
            <!-- Instructions -->
            <div style="text-transformation: uppercase; font-weight: bold;"><?php echo $invoice_instructions; ?> OVERDUE ACCOUNTS SHALL ATTRACT INTEREST AT THE OVERDRAFT RATE
    CHARGED BY NEDBANK FROM TIME TO TIME, CALCULATED FROM THE DATE OF INVOICE.</div>
            </div>
            <!-- Bank info -->
            <div style="margin-top: 40px; font-weight: bold;">
                <div style="margin-bottom: 15px; text-transformation: uppercase;">Banking Details</div>
                <div>Account Holder: Triplo 4 Sustainable Solutions Pty Ltd</div>
                <div>Nedbank Current Account</div>
                <div>Account Number: 1022459295</div>
                <div>Branch Code: 198765</div>
            </div>
            <div style="margin-top: 20px; font-size: 11px; color: #FF0000; font-weight: bold;">Please quote the Invoice No as reference when making payment</div>
            <div style="margin: 30px 0; text-align: center; font-size: 11px; font-weight: bold;">We will not request you to amend our banking details via phone or email. Please contact our offices before amending any
details</div>    
        <div class="clear"></div>
        
    </div>


<?php if (!isset($dont_print) || !$dont_print) { ?>
    <script>
        window.print();
    </script>
<?php } ?>


