<!-- 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;

</script>
{/literal}

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

<table class="stdtable" id="billtable" cellspacing="0">

  <tr>
    <td class="header" colspan="2"> {t}Create invoice{/t} </td>
  </tr>

  <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="rdate">
      <input type="image" src="img/phpaga/cal.png" id="btn_rdate">
      <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="getPayTerm()">
        {html_options options=$select_cpn selected=$billinfo.cpn_id}
      </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="section" colspan="2"> {t}Line items{/t} </td>
  </tr>
  <tr>
    <td colspan="2">

      <table class="substdtable" id="tbllineitems" cellspacing="0">
        <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>
    <td>
  </tr>

  <tr>
    <td colspan="2">
      <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>
    </td>
  </tr>

  <tr>
    <td class="section" colspan="2">{t}Preview (select Re-calculate to refresh){/t}: </td>
  </tr>
{if (is_array($bill_details) && (count($bill_details) > 0))}
  <tr>
    <td colspan="2">
      <table class="billdetailstable" cellspacing="0" align="center">
	{section name=details loop=$bill_details}
	<tr>
	  {if $bill_details[details].text eq ""}
	  <td nowrap class="btext">&nbsp;</td>
	  {else}
	  <td class="bdesc"> {$bill_details[details].text|wash} </td>
	  {/if}
	  {if $bill_details[details].amount eq ""}
	  <td nowrap class="bamount">&nbsp;</td>
	  {else}
	  <td class="bnumber"> {$bill_details[details].amount} </td>
	  {/if}
	</tr>
	{/section}
      </table>
    </td>
  </tr>
{/if}
</table>

{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">
    <tr>
      <td class="header" colspan="3"> {t}Products{/t} </td>
      <td class="headeraction" colspan="3"> [ <a class="ptr" onclick="hideElement('prodselectbox');">X</a> ] </td>
    </tr>
    <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))}

<table class="stdtable" cellspacing="0">
  <tr>
    <td class="header" colspan="8"> {t}Associated tasks{/t} </td>
  </tr>
  {if (isset($humancost.details) && (count($humancost.details) > 0))}
  <tr>
    <td class="section" colspan="8"> {t}Human cost{/t} </td>
  </tr>
  <tr>
    <td colspan="8">

      <table class="substdtable" cellspacing="0" width="100%">
        <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>

    </td>
  </tr>
  {/if}
  {if ($billinfo.taskassociation != $smarty.const.PHPAGA_BILL_TASKASSOC_LOCKED)}
  <tr>
    <td colspan="8">
      <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>
    </td>
  </tr>
  {/if}
  <tr>
    <td class="section" 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|truncate:$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> </td>
  </tr>
  {if ($billinfo.taskassociation != $smarty.const.PHPAGA_BILL_TASKASSOC_LOCKED)}
  <tr>
    <td colspan="7">
      <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>
    </td>
  </tr>
  {/if}
</table>
 {/if}
</form>

{literal}
<script type="text/javascript">
    Calendar.setup({
        inputField     :    "rdate",                     // id of the input field
        ifFormat       :    "%Y-%m-%d",       // format of the input field
        showsTime      :    false,                      // will display a time selector
        button         :    "btn_rdate",                 // trigger for the calendar (button ID)
        singleClick    :    true,                     // double-click mode
        showOthers     :    true                       // show overlapping months
    });
</script>
{/literal}

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