<!-- 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/tasks.js"></script>
<script type="text/javascript" src="js/material.js"></script>

{literal}
<script type="text/javascript">
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 class="stdtable" id="billtable" cellspacing="0">
  <caption><em>{t}Create invoice{/t}</em></caption>
  <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 format-y-m-d divider-dash no-transparency" name="bill_date" value="{$billinfo.bill_date|wash}" size="10" maxlength="10" id="rdate">
      <span class="label">{t}Date due{/t}:</span> <input type="text" class="tinput format-y-m-d divider-dash no-transparency" name="bill_date_due" value="{$billinfo.bill_date_due|wash}" size="10" maxlength="10" id="ddate">
      <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 format-y-m-d divider-dash no-transparency" name="rbill_end" value="{$billinfo.rbill_end|wash}" size="10" maxlength="10" id="rbedate">
      </span>
      {/if}
    </td>
  </tr>
</table>

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

<table class="stdtable" id="tbllineitems" cellspacing="0">
  <caption><em>{t}Line items{/t}</em></caption>
  <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>
  {section name=lit loop=$billinfo.lineitems}
  <tr class="{cycle values="odd,even"}">
    <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" onclick="viewTaskDetails({$billinfo.lineitems[lit].op_id}, 'vimgb')"><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" onclick="viewMaterialDetails({$billinfo.lineitems[lit].mat_id}, 'vimgc')"><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}
</table>

{if count($billinfo.expenses)}
<table class="stdtable" id="tblexpenses" cellspacing="0">
  <caption><em>{t}Expenses{/t}</em></caption>
  <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>

  {section name=exp loop=$billinfo.expenses}
  <tr class="{cycle values="odd,even"}">
    <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"> {$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}
</table>
{/if}

<div class="buttons">
  <input type="button" name="add" onclick="addLineItem();" value="{t}Add line item{/t}" />
  <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="stdtable 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="stdtable 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>
    <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>
    {section name=pr loop=$products}
    <tr class="{cycle values="odd,even"} 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}
  </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 class="stdtable" cellspacing="0">
  <caption><em>{t}Human cost{/t}</em></caption>
  <tr>
    <th> {t}Person{/t} </th>
    <th> {t}Duration{/t} </th>
    <th> {t}Hourly wage rate{/t} </th>
    <th> {t}Cost{/t} </th>
  </tr>
  {section name=members loop=$humancost.details}
  <tr class="{cycle values="odd,even"}">
    <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"> {$humancost.details[members].prjmem_hourlyrate|wash|phpaga_formatamount} </td>
    <td class="number"> {$humancost.details[members].amount|phpaga_formatamount} </td>
  </tr>
  {/section}
  <tr>
    <td> {t}Total{/t} </td>
    <td class="time"> {$humancost.total_duration|phpaga_minutes2duration} </td>
    <td>  </td>
    <td class="number"> {$humancost.total_amount|phpaga_formatamount} </td>
  </tr>
</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 class="stdtable" cellspacing="0">
  <caption><em>{t}Associated tasks{/t}</em></caption>
  <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 to 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>
  {section name=idx loop=$operations}
  <tr class="{cycle values="odd,even"}">
    <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))} onmouseover="return escape('{$operations[idx].op_desc|phpaga_popup_sanitizestr}')"{/if}>
      {$operations[idx].op_desc|wash|trnc:$smarty.const.PHPAGA_SHOWOPDESCLENGHT:"...":true}
    <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" onclick="viewTaskDetails({$operations[idx].op_id}, 'vimg')"><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}
  <tr>
    <td class="time" colspan="6"> {t}Total duration{/t}: </td>
    <td class="time"> {$TOTALHOURS} </td>
    <td>&nbsp;</td>
  </tr>
</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} -->
