<!-- BEGIN {$smarty.template} -->

<script type="text/javascript" src="js/material.js"></script>
{include file="delete_payment.tpl.js"}
{include file="delete_latefee.tpl.js"}

{literal}
<script type="text/javascript">
    $(document).ready(function() {
        $("#tab_container").tabs();

        /* View task details */

        var $viewTaskDialog = $('<div><\/div>').dialog({ 
            autoOpen: false, 
            title: '{/literal}{t}Task information{/t}{literal}', 
            width: 'auto', 
            height: 'auto' 
            });

        $('.tview').each(function() {
            var $link = $(this);

            $link.click(function() {
                var id = $(this).attr('id');
                var task_id = id.replace(/^tv/g, "");
                $viewTaskDialog.load('service.php?saction='+{/literal}{$smarty.const.ACTION_SERVICE_OPDETAILS}{literal}
                    +'&id=' + task_id);
                $viewTaskDialog.dialog('open');
                return false;
            });
        });

        /* View material details */

        var $viewMaterialDialog = $('<div><\/div>').dialog({ 
            autoOpen: false, 
            title: '{/literal}{t}Material information{/t}{literal}', 
            width: 'auto', 
            height: 'auto' 
            });

        $('.mview').each(function() {
            var $link = $(this);

            $link.click(function() {
                var id = $(this).attr('id');
                var material_id = id.replace(/^mv/g, "");
                $viewMaterialDialog.load('service.php?saction='+{/literal}{$smarty.const.ACTION_SERVICE_MATDETAILS}{literal}
                    +'&id=' + material_id);
                $viewMaterialDialog.dialog('open');
                return false;
            });
        });

        /* Print dialog */
        $("#printbill").dialog({
                autoOpen: false,
                modal: true,
                buttons: {
                    "{/literal}{t}Open{/t}{literal}": function() {
                       $('#pdf_download').val('0');
                       $('#bill_print_form').submit(); 
                        $(this).dialog( "close" );
                    },
                    "{/literal}{t}Download{/t}{literal}": function() {
                       $('#pdf_download').val('1');
                       $('#bill_print_form').submit(); 
                        $(this).dialog( "close" );
                    },
                    Cancel: function() {
                        $(this).dialog( "close" );
                    }
                },
                close: function() {
                }
        });
        $('#export_pdf').click(function() { $( "#printbill" ).dialog( "open" ); });
    });

function confirm_delete(bill_id) {
    if (window.confirm('{/literal}{t}Are you sure you want to delete this invoice?{/t} {$billInfo.bill_number}/{$billInfo.bill_date}  ({$billInfo.bill_endsum|phpaga_formatamount} {$billInfo.curr_name}){literal}')) {
        location.href = 'bill.php?id='+bill_id+'&action={/literal}{$smarty.const.ACTION_DELETE}{literal}';
    }
}
</script>
{/literal}

<div id="printbill" title="{t}PDF{/t}: {t}Invoice{/t} {$billInfo.bill_number|wash}/{$billInfo.bill_date|wash}">
    <form action="bill_pdf.php" method="get" name="bill_print_form" id="bill_print_form">
        <input type="hidden" name="id" value="{$billInfo.bill_id}" />
        <input type="hidden" name="download" value="0" id="pdf_download" />

        <h2>{t}Print options{/t}</h2>
        <input type="checkbox" class="cbox" name="letterhead" id="letterhead" value="1" {if $smarty.const.PHPAGA_PDF_PRINT_LETTERHEAD_DEFAULT}checked="checked"{/if} /><label for="letterhead">{t}Print letterhead{/t}</label> <br />
        <input type="checkbox" class="cbox" name="oplist" id="oplist" value="1" /><label for="oplist">{t}Append task list{/t}</label><br />
        <input type="checkbox" class="cbox" name="timesheet" id="timesheet" value="1" /><label for="timesheet">{t}Append time sheet{/t}</label>
    </form>
</div>

<div id="opdetails"> </div>
<div id="matdetails"> </div>

<h2>{t}Invoice{/t} {$billInfo.bill_number|wash}/{$billInfo.bill_date|wash}{if strlen($billInfo.rbill_id)} <span>(recurring invoice <a href="recurrbills.php?id={$billInfo.rbill_id|wash}">#{$billInfo.rbill_id|wash}</a>)</span>{/if}</h2>

<div class="actions">
    [ {if ($perm_invoices && ($billInfo.bill_paydate eq ""))}<a class="ptr" onclick="confirm_delete({$billInfo.bill_id});">{t}Delete{/t}</a> |{/if}
    {if $perm_invoices}<a href="add_bill.php?id={$billInfo.bill_id}&amp;action={$smarty.const.ACTION_CLONE}">{t}Clone{/t}</a> |{/if}
    <a id="export_pdf" href="#">{t}Print to PDF{/t}</a>
    {if ($perm_invoices)}
    | <a href="payments.php?bill_id={$billInfo.bill_id}&amp;add_payment=true">{if ($billInfo.bill_paydate eq "")}{t}Make Payment{/t}{/if}</a>
    {if not strlen($billInfo.bill_paydate)}
    | <a href="bill.php?id={$billInfo.bill_id}&amp;add_latefee=true">{t}Add Late Fee{/t}</a>
    {/if}
    {/if}
    | <a href="bill_note.php?id={$billInfo.bill_id}">{if ($billInfo.bill_note eq "")}{t}Add Note{/t}{else}{t}Edit Note{/t}{/if}</a>
    ]
</div>

<div id="tab_container">

    <ul class="tab_items">
        <li><a href="#bill-details">{t}Invoice{/t}</a></li>
        {if count($billInfo.expenses)}
        <li><a href="#bill-expenses">{t}Expenses{/t}</a></li>
        {/if}
        <li><a href="#bill-payments">{t}Payments & Fees{/t}</a></li>
        {if isset($billInfo.operations.oprows) && is_array($billInfo.operations.oprows) && count($billInfo.operations.oprows) > 0}
        <li><a href="#bill-assoctasks">{t}Associated tasks{/t}</a></li>
        {/if}
    </ul>

    <div id="bill-details" class="tab_content">
        <div class="floatcontainer">
            <table class="detail flleft" cellspacing="0">

                <caption><em>{t}Invoice{/t} {$billInfo.bill_number|wash}/{$billInfo.bill_date|wash}{if strlen($billInfo.rbill_id)} <span>(recurring invoice <a href="recurrbills.php?id={$billInfo.rbill_id|wash}">#{$billInfo.rbill_id|wash}</a>)</span>{/if}</em>
                </caption>

                <tr>
                    <th>{t}Invoice nr.{/t}: </th>
                    <td> {$billInfo.bill_number|wash} </td>
                    <th> {t}Invoice date{/t}: </th>
                    <td> {$billInfo.bill_date|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
                </tr>
                <tr>
                    <th>{t}Company{/t}: </th>
                    <td><a href="company.php?id={$billInfo.cpn_id}">{$billInfo.cpn_name|wash}</a></td>
                    <th> {t}Date due{/t}: </th>
                    {if strlen($billInfo.bill_date_due)}
                    <td{if $billInfo.bill_overdue} class="warn"{/if}> {$billInfo.bill_date_due|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
                    {else}
                    <td> {t}not set{/t} </td>
                    {/if}
                </tr>
                <tr>
                    <th> {t}Contact person{/t}: </th>
                    <td> {$billInfo.bill_person_recipient|wash|default:"&nbsp;"} </td>
                    <th> {t}Date paid{/t}: </th>
                    {if $billInfo.bill_paydate eq ""}
                    <td> {t}not paid{/t} </td>
                    {else}
                    <td> {$billInfo.bill_paydate|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
                    {/if}
                </tr>
                <tr>
                    <th> {t}Invoice type{/t}: </th>
                    <td> {$billInfo.bmt_description|wash} </td>
                    <th>{t}Total Payments{/t}: </th>
                    {if $billInfo.pmtsum.pmt_sum}
                    <td>{$config.MONETARY_SYMBOL|wash}{$billInfo.pmtsum.pmt_sum|number_format:2:$config.SEPARATOR_DECIMALS:$config.SEPARATOR_THOUSANDS|default:"&nbsp;"}</td>
                    {else}
                    <td>{t}no payments yet{/t}</td>
                    {/if}
                </tr>
                <tr>
                    <th> {t}Currency{/t}: </th>
                    <td> {$billInfo.curr_name|default:"&nbsp;"} </td>
                    <th>{t}Balance Due{/t}: </th>
                    <td>{$billInfo.bill_balance_due|number_format:2:$config.SEPARATOR_DECIMALS:$config.SEPARATOR_THOUSANDS|default:"&nbsp;"}</td> 
                </tr>
                <tr>
                    <th> {t}Additional line{/t}: </th>
                    <td colspan="3"> {$billInfo.bill_additional_line|default:"&nbsp;"} </td>
                </tr>
                <tr>
                    <th>{t}Details{/t}: </th>
                    <td colspan="3">
                        <table class="billdetailstable" cellspacing="0">
                            {section name=details loop=$billInfo.bill_details}
                            <tr>
                                {if $billInfo.bill_details[details].text eq ""}
                                <td class="btext">&nbsp;</td>
                                {else}
                                <td class="bdesc"> {$billInfo.bill_details[details].text|wash} </td>
                                {/if}
                                {if $billInfo.bill_details[details].amount eq ""}
                                <td class="bamount">&nbsp;</td>
                                {else}
                                <td class="bnumber"> {$billInfo.bill_details[details].amount} </td>
                                {/if}
                            </tr>
                            {/section}
                        </table>
                    </td>
                </tr>
                <tr><td></td></tr>
                <tr>
                    <th>{t}Notes{/t}: </th>
                    <td colspan="3">{$billInfo.bill_note|wash}</td>
                </tr>
            </table>

            <table class="flleft" cellspacing="0">
                <caption><em>{t}Line items{/t}</em></caption>
                <thead>
                    <tr>
                        <th> {t}Product code{/t} </th>
                        <th> {t}Description{/t} </th>
                        <th class="time"> {t}Qty./Hours{/t} </th>
                        <th class="number"> {t}Net price{/t} </th>
                    </tr>
                </thead>
                <tbody>
                    {section name=lit loop=$billInfo.lineitems}
                    <tr>
                        <td>
                            {$billInfo.lineitems[lit].lit_prodcode|wash|default:"&nbsp;"}
                            {if strlen($billInfo.lineitems[lit].op_id)}
                            {if strlen($billInfo.lineitems[lit].lit_prodcode)}<br />{/if}
                            {if (is_numeric($billInfo.lineitems[lit].op_id) && ($billInfo.lineitems[lit].op_id > 0))}
                            <a class="ptr tview" id="tv{$billInfo.lineitems[lit].op_id}"><img id="vimgb{$billInfo.lineitems[lit].op_id}" src="img/phpaga/view.png" height="16" width="16" border="0" alt="{t}View{/t}" title="{t}Associated task{/t}"></a>
                            {else}
                            {t}Multiple tasks are associated with this line item{/t}
                            {/if}
                            {/if}
                            {if strlen($billInfo.lineitems[lit].mat_id)}<br />
                            <a class="ptr mview" id="mv{$billInfo.lineitems[lit].mat_id}"><img id="vimgc{$billInfo.lineitems[lit].mat_id}" src="img/phpaga/view.png" height="16" width="16" border="0" alt="{t}View{/t}" title="{t}Associated material{/t}"></a>
                            {/if}
                        </td>
                        <td class="datawrap"> {$billInfo.lineitems[lit].lit_desc|wash|nl2br} </td>
                        <td class="number"> {$billInfo.lineitems[lit].lit_qty|wash|default:"&nbsp;"} </td>
                        <td class="number lr"> {$config.MONETARY_SYMBOL|wash}{$billInfo.lineitems[lit].lit_netprice|wash|phpaga_formatamount|default:"&nbsp;"} </td>
                    </tr>
                    {/section}
                </tbody>
            </table>
        </div>
        <div class="spacer"></div>
    </div>

    <div id="bill-payments" class="tab_content">
        <!-- Begin Payments Section -->
        <table cellspacing="0">
            <caption><em>{t}Payments{/t}</em></caption>
            <thead>
                <tr>
                    <th> {t}Date{/t} </th>
                    <th> {t}Note{/t} </th>
                    {if not strlen($billInfo.bill_paydate)}
                    <th class="number"> {t}Amount{/t} </th>
                    <th> {t}Actions{/t} </th>
                    {else}
                    <th colspan="2">{t}Amount{/t} </th>
                    {/if}
                </tr>
            </thead>
            {if count($billInfo.payments)}
            <tbody>
                {section name=pmt loop=$billInfo.payments}
                <tr>
                    <td>
                        {$billInfo.payments[pmt].pmt_date|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT}
                    </td>
                    <td>
                        {$billInfo.payments[pmt].pmt_note|wash|default:"&nbsp;"}
                    </td>
                    <td class="number">
                        {$config.MONETARY_SYMBOL|wash}{$billInfo.payments[pmt].pmt_amt|number_format:2:$config.SEPARATOR_DECIMALS:$config.SEPARATOR_THOUSANDS|default:"&nbsp;"}
                    </td>
                    <td>{strip}
                        {if not strlen($billInfo.bill_paydate)}
                        <a href="payments.php?bill_id={$billInfo.payments[pmt].bill_id}&pmt_id={$billInfo.payments[pmt].pmt_id}&edit_payment=true"><img src="img/phpaga/edit.png" height="16" width="16" border="0" alt="{t}Edit{/t}" title="{t}Edit{/t}"></a>
                        <a class="ptr" onclick="confirm_delete_pmt({$billInfo.payments[pmt].pmt_id}, '{$billInfo.payments[pmt].pmt_amt|wash|default}', '{$billInfo.bill_id}');"><img src="img/phpaga/delete.png" height="16" width="16" border="0" alt="{t}Delete{/t}" title="{t}Delete{/t}"></a>
                        {/if}
                        {/strip}
                    </td>
                </tr>
                {/section}
            </tbody>
            {/if}
        </table>
        <!-- End Payments Section -->

        <!-- Begin Late Fees Sections -->
        <table cellspacing="0">
            <caption><em>{t}Late Fees{/t}</em></caption>
            <thead>
                <tr>
                    <th> {t}Date{/t} </th>
                    {if not strlen($billInfo.bill_paydate)}
                    <th class="number"> {t}Amount{/t} </th>
                    <th> {t}Actions{/t} </th>
                    {else}
                    <th colspan="2">{t}Amount{/t} </th>
                    {/if}
                </tr>
            </thead>
            {if count($billInfo.latefees)}
            <tbody>
                {section name=fee loop=$billInfo.latefees}
                <tr>
                    <td>
                        {$billInfo.latefees[fee].fee_date|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT}
                    </td>
                    <td class="number">
                        {$config.MONETARY_SYMBOL|wash}{$billInfo.latefees[fee].fee_amt|number_format:2:$config.SEPARATOR_DECIMALS:$config.SEPARATOR_THOUSANDS|default:"&dbsp;"}
                    </td>
                    <td><center> {strip}
                            {if not strlen($billInfo.bill_paydate)}
                            <a class="ptr" onclick="confirm_delete_fee({$billInfo.latefees[fee].fee_id}, '{$billInfo.latefees[fee].fee_amt|wash|default}', '{$billInfo.bill_id}');"><img src="img/phpaga/delete.png" height="16" width="16" border="0" alt="{t}Delete{/t}" title="{t}Delete{/t}"></a>
                            {/if}
                            {/strip}</center>
                    </td>
                </tr>
                {/section}
            </tbody>
            {/if}
        </table>
        <!-- End Late Fees Section -->
    </div>

    {if count($billInfo.expenses)}
    <div id="bill-expenses" class="tab_content">
        <table id="tblexpenses" cellspacing="0">
            <caption><em>{t}Expenses{/t}</em></caption>
            <thead>
                <tr>
                    <th> {t}Project{/t} </th>
                    <th> {t}Date{/t} </th>
                    <th> {t}Description{/t} </th>
                    <th class="number"> {t}Cost{/t} </th>
                    <!--<th> {t}Currency{/t} </th>-->
                    <th> {t}Person{/t} </th>
                </tr>
            </thead>
            <tbody>
                {section name=exp loop=$billInfo.expenses}
                <tr>
                    <td> {$billInfo.expenses[exp].prj_title|wash} </td>
                    <td> {$billInfo.expenses[exp].exp_date|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
                    <td> {$billInfo.expenses[exp].exp_desc|wash} </td>
                    <td class="number"> {$config.MONETARY_SYMBOL}{$billInfo.expenses[exp].exp_sum|phpaga_formatamount|default:"&nbsp;"} </td>
                    <!--<td> {$billInfo.expenses[exp].curr_name|wash} </td>-->
                    <td> <a href="person.php?id={$billInfo.expenses[exp].pe_id}">{$billInfo.expenses[exp].pe_person}</a> </td>
                </tr>
                {/section}
            </tbody>
        </table>
    </div>
    {/if}

    {if isset($billInfo.operations.oprows) && is_array($billInfo.operations.oprows) && count($billInfo.operations.oprows) > 0}
    <div id="bill-assoctasks" class="tab_content">
        <table cellspacing="0">
            <caption><em>{t}Associated tasks{/t}</em></caption>
            <thead>
                <tr>
                    <th> {t}Project{/t} </th>
                    <th> {t}Task{/t} </th>
                    <th class="time"> {t}Date{/t} </th>
                    <th> {t}Person{/t} </th>
                    <th> {t}Category{/t} </th>
                    <th class="time"> {t}Hours{/t} </th>
                    <th> {t}Actions{/t} </th>
                </tr>
            </thead>
            <tbody>
                {section name=operations loop=$billInfo.operations.oprows}
                <tr>
                    <td> <a href="project.php?id={$billInfo.operations.oprows[operations].prj_id}">{$billInfo.operations.oprows[operations].prj_title|wash}</a> </td>
                    <td {if ((strlen($billInfo.operations.oprows[operations].op_desc) > $smarty.const.PHPAGA_SHOWOPDESCLENGHT))} class="tTip" title="{$billInfo.operations.oprows[operations].op_desc|phpaga_popup_sanitizestr}"{/if}>
                        {$billInfo.operations.oprows[operations].op_desc|wash|trnc:$smarty.const.PHPAGA_SHOWOPDESCLENGHT:"...":true}
                    </td>
                    <td class="time"> {$billInfo.operations.oprows[operations].op_startdate|wash|date_format:$smarty.const.PHPAGA_DATEFORMAT_SHORT} </td>
                    <td> <a href="person.php?id={$billInfo.operations.oprows[operations].pe_id}">{$billInfo.operations.oprows[operations].op_person|wash}</a> </td>
                    <td> {$billInfo.operations.oprows[operations].opcat_title|wash} </td>
                    <td class="time"> {$billInfo.operations.oprows[operations].duration} </td>
                    <td class="actions"> <a class="ptr tview" id="tv{$billInfo.operations.oprows[operations].op_id}"><img id="vim{$billInfo.operations.oprows[operations].op_id}" src="img/phpaga/view.png" height="16" width="16" border="0" alt="{t}View{/t}" title="{t}View{/t}"></a> </td>
                </tr>
                {/section}
            </tbody>
            <tfoot>
                <tr>
                    <td class="time" colspan="5"> {t}Total duration{/t}: </td>
                    <td class="time"> {$billInfo.operations.duration} </td>
                    <td> &nbsp; </td>
                </tr>
            </tfoot>
        </table>
    </div>
    {/if}
</div>

<!-- END {$smarty.template} -->
