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

{if strlen($BILLING_ERR)}<p>{$BILLING_ERR}</p>{/if}

<form action="{$smarty.const.PHPAGA_SELF}" method="post" name="f">
<input type="hidden" name="action" id="action" value="{$smarty.const.ACTION_BILL_MAKEIT}">
<input type="hidden" name="taskassociation"  value="{$billinfo.taskassociation}">

<script type="text/javascript" src="js/billing.js"></script>
<script type="text/javascript" src="js/material.js"></script>

{literal}
<script type="text/javascript">

    $(document).ready(function() {

        $('#bill_date, #bill_date_due, #rbill_end').datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'yy-mm-dd',
            showOn: 'both',
            buttonImage: '../img/phpaga/cal.png',
            buttonImageOnly: true
        });

        /* 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;
            });
        });

    });

function setAction(num) {
  document.f.action.value=num;
  document.f.submit();
}

var curr_elem_num = null;

function doRbillEnabler(el) {
  if (el.options[el.selectedIndex].value == 0)
    hideElement('rbilldata');
  else
    showElement('rbilldata');
}
</script>
{/literal}

{if strlen($STATUSMSG)}<p>{$STATUSMSG}</p>{/if}

<table id="billtable" cellspacing="0">
    <caption><em>{t}Create invoice{/t}</em></caption>
    <tbody>
        <tr>
            <td class="label"> {t}Invoice nr.{/t}: </td>
            <td>
                <input type="text" class="tinput" name="bill_number" value="{$billinfo.bill_number|wash}" size="10" maxlength="10">
                <span class="label">{t}Invoice date{/t}:</span> <input type="text" class="tinput" name="bill_date" value="{$billinfo.bill_date|wash}" size="10" maxlength="10" id="bill_date">
                <span class="label">{t}Date due{/t}:</span> <input type="text" class="tinput" name="bill_date_due" value="{$billinfo.bill_date_due|wash}" size="10" maxlength="10" id="bill_date_due">
                <span class="label">{t}Term of payment{/t}:</span> <input type="text" class="tinput" name="bill_payterm" id="payterm" value="{$billinfo.bill_payterm|wash}" size="30" maxlength="30">
            </td>
        </tr>

        <tr>
            <td class="label"> {t}Recipient (company){/t}: </td>
            <td>
                <select name="cpn_id" id="cpn" onchange="companyChanged()">
                    {html_options options=$select_cpn selected=$billinfo.cpn_id}
                </select>

                <span class="label">{t}Contact person{/t}:</span>
                <select name="pe_id_recipient" id="pe_id_recipient">
                    {html_options options=$persons selected=$billinfo.pe_id_recipient}
                </select>

            </td>
        </tr>

        <tr>
            <td class="label"> {t}Invoice type{/t}: </td>
            <td>
                <select name="bmt_id">
                    {html_options options=$select_bmt selected=$billinfo.bmt_id}
                </select>
                <span class="label"> {t}Currency{/t}:</span>
                <select name="curr_id">
                    {html_options options=$select_curr selected=$billinfo.curr_id}
                </select>
            </td>
        </tr>

        <tr>
            <td class="label"> {t}Recurring invoice{/t}: </td>
            <td>
                {if strlen($billinfo.rbill_id)}
                <input type="hidden" name="rbill_id"  value="{$billinfo.rbill_id}">
                {t 1=$billinfo.rbill_id}Part of recurring bill %1.{/t}
                {else}
                <select name="rbill_type" id="rbill_type" onchange="doRbillEnabler(this);">
                    {html_options options=$select_rbilltypes selected=$billinfo.rbill_type}
                </select>
                <span id="rbilldata">
                    <span class="label"> {t}End (optional){/t}:</span>
                    <input type="text" class="tinput" name="rbill_end" value="{$billinfo.rbill_end|wash}" size="10" maxlength="10" id="rbill_end">
                </span>
                {/if}
            </td>
        </tr>

        <tr>
            <td class="label"> {t}Additional information{/t}: </td>
            <td>
                <span id="bill_additional_line_data">
                    <input type="text" class="tinput" name="bill_additional_line" value="{$billinfo.bill_additional_line}" size="50" maxlength="255" id="add_line_data">
                </span>
            </td>
        </tr>
    </tbody>
</table>

{literal}
<script type="text/javascript">
  doRbillEnabler(document.getElementById('rbill_type'));
</script>
{/literal}

<table id="tbllineitems" cellspacing="0">
    <caption><em>{t}Line items{/t}</em></caption>
    <thead>
        <tr>
            <th> {t}Product code{/t} </th>
            <th> {t}Description{/t} </th>
            <th> {t}Qty./Hours{/t} </th>
            <th> {t}Unit price net{/t} </th>
            <th> {t}Net price{/t} </th>
            <th> {t}Actions{/t} </th>
        </tr>
    </thead>
    <tbody>
        {section name=lit loop=$billinfo.lineitems}
        <tr>
            <td>
                <input type="hidden" name="lit_op_id[]" value="{$billinfo.lineitems[lit].op_id}" /> {* <!-- in case the line item is linked to a task --> *}
                <input type="hidden" name="lit_mat_id[]" value="{$billinfo.lineitems[lit].mat_id}" /> {* <!-- in case the line item is linked to a material --> *}

                <input type="text" class="tinput" name="lit_prodcode[]" value="{$billinfo.lineitems[lit].lit_prodcode|wash}" size="18" maxlength="18" id="prodcode{$smarty.section.lit.index}" onblur="getProdInfo('{$smarty.section.lit.index}');">
                {if strlen($billinfo.lineitems[lit].op_id)}<br />
                {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}

                {if (!strlen($billinfo.lineitems[lit].op_id) && !strlen($billinfo.lineitems[lit].mat_id) && !strlen($billinfo.lineitems[lit].lit_prodcode))}
                <a class="ptr" onclick="getProductCatalog({$smarty.section.lit.index})"><img alt="{t}Products{/t}" src="img/phpaga/catalog.png" width="16" height="16"></a>
                {/if}
            </td>
            <td> <textarea name="lit_desc[]" cols="60" rows="2" wrap="virtual" id="prodname{$smarty.section.lit.index}">{$billinfo.lineitems[lit].lit_desc|wash}</textarea> </td>
            <td> <input type="text" class="tinput number" name="lit_qty[]" value="{$billinfo.lineitems[lit].lit_qty|wash}" size="10" maxlength="10" id="prodqty{$smarty.section.lit.index}" onblur="calcLineItemPrice('{$smarty.section.lit.index}');"> </td>
            <td class="number">
                <span id="unitnetpricetxt{$smarty.section.lit.index}">{$billinfo.lineitems[lit].lit_unitnetprice|wash|phpaga_formatamount|default:"&nbsp;"}</span>
                <input type="hidden" name="lit_unitnetprice[]" value="{$billinfo.lineitems[lit].lit_unitnetprice|wash}" size="15" id="unitnetprice{$smarty.section.lit.index}">
            </td>
            <td> <input type="text" class="tinput number" name="lit_netprice[]" value="{$billinfo.lineitems[lit].lit_netprice|wash}" size="15" maxlength="15" id="prodprice{$smarty.section.lit.index}"> </td>
            <td>
                {if !strlen($billinfo.lineitems[lit].op_id)}
                <a class="ptr" onclick="delLineItem(this.parentNode.parentNode.rowIndex)"><img alt="{t}Delete{/t}" src="img/phpaga/delete.png" width="16" height="16"></a>
                {else}
                &nbsp;
                {/if}
            </td>
        </tr>
        {/section}
    </tbody>
</table>

<a href="#bill" onclick="addLineItem();">{t}Add line item{/t}</a>

{if count($billinfo.expenses)}
<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>
            <th> {t}Actions{/t} </th>
        </tr>
    </thead>
    <tbody>
        {section name=exp loop=$billinfo.expenses}
        <tr>
            <td> {$billinfo.expenses[exp].prj_title|wash} </td>
            <td> <input type="hidden" name="exp_id[]" value="{$billinfo.expenses[exp].exp_id}"> {$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|wash}{$billinfo.expenses[exp].exp_sum|phpaga_formatamount} </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>
            <td class="last">  <a class="ptr" onclick="delExpenseLine(this.parentNode.parentNode.rowIndex)"><img alt="{t}Delete{/t}" src="img/phpaga/delete.png" width="16" height="16"></a> </td>
        </tr>
        {/section}
    </tbody>
</table>
{/if}

<div class="buttons">
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_RECALC});" value="{t}Re-calculate{/t}" />
  <input type="button" name="create" onclick="setAction({$smarty.const.ACTION_BILL_MAKEIT});" value="{t}Create invoice{/t}" />
</div>

<div class="dcaption"><em>{t}Preview{/t} <span>{t}(select Re-calculate to refresh){/t}</span></em>&nbsp;</div>

{if (is_array($bill_details) && (count($bill_details) > 0))}
<table class="billdetailstable wauto" cellspacing="0">
  {section name=details loop=$bill_details}
  <tr>
    {if $bill_details[details].text eq ""}
    <td class="btext">&nbsp;</td>
    {else}
    <td class="bdesc"> {$bill_details[details].text|wash} </td>
    {/if}
    {if $bill_details[details].amount eq ""}
    <td class="bamount">&nbsp;</td>
    {else}
    <td class="bnumber"> {$bill_details[details].amount} </td>
    {/if}
  </tr>
  {/section}
</table>
{/if}

{if $STATUSMSG != ""}<p>{$STATUSMSG}</p>{/if}

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

<div id="prodselectbox">
    <input type="hidden" id="prodselectdest" name="prodselectdest" value="">
    <table class="prodselect" cellspacing="0">
        <caption><em>{t}Products{/t}</em>
            <a class="ptr" onclick="hideElement('prodselectbox');"><img src="img/phpaga/close.png" alt="X" title="{t}Close{/t}"></a>
        </caption>
        <thead>
            <tr>
                <th> {t}Product code{/t} </th>
                <th> {t}Name{/t} </th>
                <th> {t}Price{/t} </th>
                <th> {t}Stock{/t} </th>
                <th> {t}Category{/t} </th>
            </tr>
        </thead>
        <tbody>
            {section name=pr loop=$products}
            <tr class="pointer" onclick="selectProduct('{$products[pr].prod_code|wash}');">
                <td> {$products[pr].prod_code|wash|default:"&nbsp;"} </td>
                <td> {$products[pr].prod_name|wash|default:"&nbsp;"} </td>
                <td class="number"> {$products[pr].prod_price|wash|phpaga_formatamount|default:"&nbsp;"} </td>
                <td class="number"> {$products[pr].prod_stock|wash|default:"&nbsp;"} </td>
                <td> {$products[pr].prcat_title|wash|default:"&nbsp;"} </td>
            </tr>
            {/section}
        </tbody>
    </table>
</div>

{if (isset($operations) && is_array($operations) && (count($operations) > 0))}

<div class="dcaption"><em>{t}Associated tasks{/t}</em>&nbsp;</div>

{if (isset($humancost.details) && (count($humancost.details) > 0))}
<table cellspacing="0">
    <caption><em>{t}Human cost{/t}</em></caption>
    <thead>
        <tr>
            <th> {t}Person{/t} </th>
            <th> {t}Duration{/t} </th>
            <th> {t}Hourly wage rate{/t} </th>
            <th> {t}Cost{/t} </th>
        </tr>
    </thead>
    <tbody>
        {section name=members loop=$humancost.details}
        <tr>
            <td> <a href="person.php?id={$humancost.details[members].pe_id}">{$humancost.details[members].pe_person|wash}</a> </td>
            <td class="time"> {$humancost.details[members].sum_duration|phpaga_minutes2duration} </td>
            <td class="number"> {$config.MONETARY_SYMBOL}{$humancost.details[members].prjmem_hourlyrate|wash|phpaga_formatamount} </td>
            <td class="number"> {$config.MONETARY_SYMBOL}{$humancost.details[members].amount|phpaga_formatamount} </td>
        </tr>
        {/section}
    </tbody>
    <tfoot>
        <tr>
            <td> {t}Total{/t} </td>
            <td class="time"> {$humancost.total_duration|phpaga_minutes2duration} </td>
            <td>  </td>
            <td class="number"> {$config.MONETARY_SYMBOL}{$humancost.total_amount|phpaga_formatamount} </td>
        </tr>
    </tfoot>
</table>
{/if}

{if ($billinfo.taskassociation != $smarty.const.PHPAGA_BILL_TASKASSOC_LOCKED)}
<div class="buttons">
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_RECALC});" value="{t}Re-calculate{/t}" />
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_MAKEOPITEMS});" value="{t}Create line items for associated tasks{/t}" />
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_MAKEOPITEMSPERPRJ});" value="{t}Create line items grouped by project{/t}" />
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_MAKESINGLEOPITEM});" value="{t}Create a single line item for all associated tasks{/t}" />
</div>
{/if}

<table cellspacing="0">
    <caption><em>{t}Associated tasks{/t}</em></caption>
    <thead>
        <tr>
            <td class="data datawrap" colspan="8">
                {if ($billinfo.taskassociation != $smarty.const.PHPAGA_BILL_TASKASSOC_LOCKED)}
                {t}Please deselect the tasks you don't want to be billed with this invoice.{/t}
                {else}
                {t}The following tasks are associated with this invoice{/t}
                {/if}
            </td>
        </tr>
        <tr>
            <th> &nbsp; </th>
            <th> {t}Project{/t} </th>
            <th> {t}Task{/t} </th>
            <th> {t}Date{/t} </th>
            <th> {t}Person{/t} </th>
            <th> {t}Category{/t} </th>
            <th> {t}Hours{/t} </th>
            <th> {t}Actions{/t} </th>
        </tr>
    </thead>
    <tbody>
        {section name=idx loop=$operations}
        <tr>
            <td>
                {if ($billinfo.taskassociation != $smarty.const.PHPAGA_BILL_TASKASSOC_LOCKED)}
                <input name="op_id[]" value="{$operations[idx].op_id}" type="checkbox" class="cbox" checked>
                {else}
                <input name="op_id[]" value="{$operations[idx].op_id}" type="hidden"> &nbsp;
                {/if}
            </td>
            <td> <a href="project.php?id={$operations[idx].prj_id}">{$operations[idx].prj_title|wash}</a> </td>
            <td{if ((strlen($operations[idx].op_desc) > $smarty.const.PHPAGA_SHOWOPDESCLENGHT))} class="tTip" title="{$operations[idx].op_desc|phpaga_popup_sanitizestr}"{/if}>
                {$operations[idx].op_desc|wash|trnc:$smarty.const.PHPAGA_SHOWOPDESCLENGHT:"...":true}
            </td>
            <td> {$operations[idx].op_startdate|wash} </td>
            <td> <a href="person.php?id={$operations[idx].pe_id}">{$operations[idx].op_person|wash}</a> </td>
            <td> {$operations[idx].opcat_title|wash} </td>
            <td class="time"> {$operations[idx].op_duration|phpaga_minutes2duration} </td>
            <td class="actions">
                <a class="ptr tview" id="tv{$operations[idx].op_id}"><img id="vimg{$operations[idx].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="6"> {t}Total duration{/t}: </td>
            <td class="time"> {$TOTALHOURS} </td>
            <td>&nbsp;</td>
        </tr>
    </tfoot>
</table>

{if ($billinfo.taskassociation != $smarty.const.PHPAGA_BILL_TASKASSOC_LOCKED)}
<div class="buttons">
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_RECALC});" value="{t}Re-calculate{/t}" />
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_MAKEOPITEMS});" value="{t}Create line items for associated tasks{/t}" />
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_MAKEOPITEMSPERPRJ});" value="{t}Create line items grouped by project{/t}" />
  <input type="button" name="recalc" onclick="setAction({$smarty.const.ACTION_BILL_MAKESINGLEOPITEM});" value="{t}Create a single line item for all associated tasks{/t}" />
</div>
{/if}
{/if}
</form>

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