<?php


  require('includes/application_top.php');
  
  require_once DIR_WS_CLASSES."search_cache.php";

  require(DIR_WS_CLASSES . 'currencies.php');
  $currencies = new currencies();

  switch ($HTTP_GET_VARS['action']) {
    case 'update_invoice':
      $oID = (int)$HTTP_GET_VARS['oID'];
      if ($HTTP_POST_VARS['beneficiary'] != '-') {
        $receiver = $HTTP_POST_VARS['beneficiary'];
       //$account_class2 = $HTTP_POST_VARS['beneficiary2'];
        $account_class = $HTTP_POST_VARS['account_class'];
      } else
      $receiver = $HTTP_POST_VARS['receiver'];
      $specifikation = $HTTP_POST_VARS['specifikation'];
    $deb_cred = $HTTP_POST_VARS['deb_cred'];
$vat_osuus = $HTTP_POST_VARS['vat_osuus'];
      $account_class = $HTTP_POST_VARS['account_class'];
      $sum = $HTTP_POST_VARS['sum'];
      $date = $HTTP_POST_VARS['invoice_date'];
      $expires = $HTTP_POST_VARS['expires'];
      $comment = $HTTP_POST_VARS['comment'];
      $sum = ereg_replace(',', '.', $sum);
      tep_db_query ("update " .TABLE_ACCOUNTING ." set deb_cred = '$deb_cred', account_class_booked  = '$account_class',receiver = '$receiver',specifikation = '$specifikation', sum = '$sum',vat_osuus  = '$vat_osuus', invoice_date = '$date', expires = '$expires', comment = '$comment' where invoices_id = '$oID'");
      
     // tep_db_query ("update " .TABLE_ACCOUNTING ." set a.account_class_booked = b.account_class from accounting as a inner join b on b.1 = a.1 ");
      
      


   
   
   
      if ($HTTP_POST_VARS['add_beneficiary'] && strlen($receiver) > 0)
        tep_db_query("insert ignore into ".TABLE_ACCOUNTING_CLASS." (name, account_class) values ('".$receiver."','".$account_class."')");
      
      tep_redirect(tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action'))));
      break;
    case 'new_invoice':
      if ($HTTP_POST_VARS['beneficiary'] != '-') {
        $receiver = $HTTP_POST_VARS['beneficiary'];
         $account_class = $HTTP_POST_VARS['account_class'];
      } else
        $receiver = $HTTP_POST_VARS['receiver'];
            $specifikation = $HTTP_POST_VARS['specifikation'];
 $vat_osuus = $HTTP_POST_VARS['vat_osuus'];
    $deb_cred = $HTTP_POST_VARS['deb_cred'];
 $account_class = $HTTP_POST_VARS['account_class_booked'];
       $sum_vat_no = $HTTP_POST_VARS['sum_vat_no'];
      $sum = $HTTP_POST_VARS['sum'];
       $vat_percent = $HTTP_POST_VARS['vat_percent'];
      
      $date = $HTTP_POST_VARS['invoice_date'];
      $expires = $HTTP_POST_VARS['expires'];
      $comment = $HTTP_POST_VARS['comment'];
      $sum = ereg_replace(',', '.', $sum);
      tep_db_query ("insert into " .TABLE_ACCOUNTING ." (receiver,deb_cred,specifikation,account_class_booked,sum_vat_no, sum,vat_osuus, vat_percent, invoice_date, expires, comment) values ('$receiver','$deb_cred','$specifikation','$account_class','$sum_vat_no', '$sum','$vat_osuus', '$vat_percent', '$date', '$expires', '$comment')");
      
      if ($HTTP_POST_VARS['add_beneficiary'] && strlen($receiver) > 0)
        tep_db_query("insert ignore into ".TABLE_ACCOUNTING_CLASS." (name, account_class) values ('".$receiver."','".$account_class."' )");
      
      tep_redirect(tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action'))));
      break;
    case 'pay':
      $oID = (int)($HTTP_GET_VARS['oID']);
      $invoice_query = tep_db_query("select paid from " .TABLE_ACCOUNTING ." where invoices_id = '$oID'");
      if (tep_db_num_rows($invoice_query)) {
        if (!tep_db_result($invoice_query, 0, 'paid')) {
          tep_db_query ("update " .TABLE_ACCOUNTING ." set paid = now(), on_hold = '0' where invoices_id = '$oID'");
        }
      }
      tep_redirect(tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action'))));
      break;
    case 'deleteconfirm':
      $oID = (int)($HTTP_GET_VARS['oID']);

      tep_db_query ("delete from " .TABLE_ACCOUNTING ." where invoices_id = '$oID'");

      tep_redirect(tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action'))));
      break;
    case 'put_on_hold':
      $oID = (int)($HTTP_GET_VARS['oID']);
      tep_db_query ("update " .TABLE_ACCOUNTING ." set on_hold = '1' where invoices_id = '$oID'");

      tep_redirect(tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action'))));
      break;
    case 'release':
      $oID = (int)($HTTP_GET_VARS['oID']);
      tep_db_query ("update " .TABLE_ACCOUNTING ." set on_hold = '0' where invoices_id = '$oID'");

      tep_redirect(tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action'))));
      break;
  }

  if ( ($HTTP_GET_VARS['action'] == 'edit') && ($HTTP_GET_VARS['oID']) ) {
    $oID = tep_db_prepare_input($HTTP_GET_VARS['oID']);

    $invoice_query = tep_db_query("select invoices_id, receiver, specifikation,account_class_booked, sum,vat_osuus, invoice_date, expires, paid, comment from " . TABLE_ACCOUNTING . " where invoices_id = '" . tep_db_input($oID) . "'");
    $invoice_exists = true;
    if (!tep_db_num_rows($invoice_query)) {
      $invoice_exists = false;
      $messageStack->add(sprintf(ERROR_INVOICE_DOES_NOT_EXIST, $oID), 'error');
    } else {
      $invoice = tep_db_fetch_array($invoice_query);
      $oInfo = new objectInfo($invoice);
    }
  }
  
  if ($_POST['search'] && !$_GET['search_id']) {
    $search_cache = new SearchCache('invoice_search', '');
    
    $search_params = $_POST;
    $search_cache->pushParameters($search_params);
    $HTTP_GET_VARS['search_id'] = $search_cache->getSID();
  } else if ($_GET['search_id']) {
    $search_cache = new SearchCache('invoice_search', $_GET['search_id']);
    $search_params = $search_cache->getParameters();
  } else
    $search_params = array();
  
  $beneficiaries = array('0'=>'--');
  $beneficiary_array = array();
  $query = tep_db_query("select name, account_class from ".TABLE_ACCOUNTING_CLASS." order by name");
  while ($beneficiary = tep_db_fetch_array($query)) {
    $beneficiaries[] = array('id'=>$beneficiary['account_class'] .' '. $beneficiary['name'], 'text'=>$beneficiary['account_class'] .' '. $beneficiary['name'], 'fakta'=>$beneficiary['account_class']);
    $beneficiary_array[] = $beneficiary['name'];
    
  }
  tep_db_free_result($query);


  





  
  $sort = $HTTP_GET_VARS['sort'];
  $desc = $HTTP_GET_VARS['d'];
  
  function DrawSortLink ($str, $s)
  {
    global $desc, $sort;
    return '<a href="' .tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('sort', 'd', 'page')) ."sort=$str&amp;d=" .(($sort == $str) && !$desc? '1': '0')) .'">' .$s .($str == $sort? ($desc? '-': '+'): '') .'</a>';
  }
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/javascript/spiffyCal/spiffyCal_v2_1.css">
<script language="JavaScript" src="includes/javascript/spiffyCal/spiffyCal_v2_1.js"></script>
<?php  if ( $HTTP_GET_VARS['action'] == 'new' || $HTTP_GET_VARS['action'] == 'edit' ) { ?>
<script language="javascript"><!--
  var dateInvoice = new ctlSpiffyCalendarBox("dateInvoice", "invoice", "invoice_date","btnDate2","<?php echo ($invoice_exists? $oInfo->invoice_date: date('Y-m-d')); ?>",scBTNMODE_CUSTOMBLUE);
  var dateExpires = new ctlSpiffyCalendarBox("dateExpires", "invoice", "expires","btnDate1","<?php echo ($invoice_exists? $oInfo->expires: date('Y-m-d')); ?>",scBTNMODE_CUSTOMBLUE);
//-->
</script>
<?php } else { ?>
<script language="javascript"><!--
  var dateExpiresFrom = new ctlSpiffyCalendarBox("dateExpiresFrom", "search", "expfr","btnDate1","<?php echo $search_params['expfr']; ?>",scBTNMODE_CUSTOMBLUE);
  var dateExpiresTo = new ctlSpiffyCalendarBox("dateExpiresTo", "search", "expto","btnDate2","<?php echo $search_params['expto']; ?>",scBTNMODE_CUSTOMBLUE);
  var datePaidFrom = new ctlSpiffyCalendarBox("datePaidFrom", "search", "paidfr","btnDate3","<?php echo $search_params['paidfr']; ?>",scBTNMODE_CUSTOMBLUE);
  var datePaidTo = new ctlSpiffyCalendarBox("datePaidTo", "search", "paidto","btnDate4","<?php echo $search_params['paidto']; ?>",scBTNMODE_CUSTOMBLUE);
  var dateInvoiceFrom = new ctlSpiffyCalendarBox("dateInvoiceFrom", "search", "invfr","btnDate5","<?php echo $search_params['invfr']; ?>",scBTNMODE_CUSTOMBLUE);
  var dateInvoiceTo = new ctlSpiffyCalendarBox("dateInvoiceTo", "search", "invto","btnDate6","<?php echo $search_params['invto']; ?>",scBTNMODE_CUSTOMBLUE);
//-->
</script>  
<?php } ?>
<script type="text/javascript" language="JavaScript">
<!--
  function benefChange (field)
  {
    if (field == 'dropdown') {
      document.getElementById('benef_text').value = '';
       document.getElementById('account_class_text').value = '';


      
    } else {
      document.getElementById('benef_dropdown').selectedIndex = 0;
    }
  }
//-->
</script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php
  require(DIR_WS_INCLUDES . 'header.php');
?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<?php
  if ( $HTTP_GET_VARS['action'] == 'new' ) {
?>

      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
            <td class="pageHeading" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action'))) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
          </tr>
        </table></td>
      </tr>
      
      
            

      
  


      
      
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td colspan="3"><?php echo tep_draw_separator(); ?></td>
          </tr>
          <tr>
            <td colspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="3" class="main"><b><?php echo HEADING_TITLE_NEW; ?></b></td>
          </tr>
          <tr>
            <td colspan="3"><?php echo tep_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
          <tr>
            <td valign="top"><table  border="0" cellspacing="0" cellpadding="2">
              <?php echo tep_draw_form('invoice', FILENAME_ACCOUNTING, tep_get_all_get_params(array('action')) . 'action=new_invoice'); ?>
              
               <tr>
                <td class="main" valign="top" ><?php echo 'Debit/Credit:'; ?></td>
                <td class="main" align="right" valign="top">
               <select name="deb_cred" id="deb_cred" class="flatField" style="width:150px;">
               <option value="Debit">Debit</option>
               <option value="Credit">Credit</option>
             
               </select>
               </td>
              </tr>
                     <tr>
                <td class="main" valign="top"><?php echo 'Vat (%):'; ?></td>
               
                 
                 
                 
               <td class="main" align="right" valign="top">
               <select name="vat_percent" id="vat_percent" class="flatField" style="width:150px;">
               <option value="24">24%</option>
               <option value="13">13%</option>
               <option value="10">10%</option>
               <option value="0">0%</option>
               </select>
               </td>
                 
                 
              </tr>
              
              <tr>
                <td class="main" valign="top"><?php echo ENTRY_RECEIVER; ?></td>
                <td class="main" align="right"><?php echo tep_draw_pull_down_menu('beneficiary', $beneficiaries, '', 'id="benef_dropdown" class="flatField" style="width:150px;" onChange="benefChange(\'dropdown\')"'); ?></td>
              
              </tr>
              
              
              
              
              <tr>
                <td class="smallText" valign="top"><?php echo TEXT_OR_ENTER; ?></td>
                 <td class="main" align="right"><?php echo tep_draw_input_field('account_class_booked', '', 'size="10" id="account_class_text" class="flatField" onChange="benefChange(\'text\')"'); ?><?php echo tep_draw_input_field('receiver', '', 'size="30" id="benef_text" class="flatField" onChange="benefChange(\'text\')"'); ?></td>
            
               
                <td class="smallText"><?php echo '&nbsp;'.tep_draw_checkbox_field('add_beneficiary', '1').' '.TEXT_ADD_BENEFICIARY; ?></td>
              </tr>
            
              
               <tr>
                <td class="main" valign="top"><?php echo 'Specifikation:'; ?></td>
                <td class="main" align="right"><?php echo tep_draw_input_field('specifikation', '', 'size="18" class="flatField" style="width:150px;"'); ?></td>
              </tr>
              
              
        
             
          
               <tr>
                <td class="main" valign="top"><?php echo 'Vat Tot.:'; ?></td>
                <td class="main" align="right"><?php echo tep_draw_input_field('vat_osuus', '', 'size="18" class="flatField" style="width:150px;"'); ?></td>
              </tr>
              
  
              
         
      
      
<script type="text/javascript">
	function vatclassauto() {
    var totalAmt = document.invoice.total.value;
    var val = 0;
		
		for( i = 0; i < document.invoice.discount_type.length; i++ ) {
		if( document.invoice.discount_type[i].checked == true )
		val = document.invoice.discount_type[i].value;
		}
		dType = document.invoice.discount_type.value;
	totalR = eval(totalAmt - dType);
		document.getElementById('update').innerHTML = totalR;
	}
  
  document.getElementById('update').innerHTML = totalR;
</script>     
     
              
  
	
	<input type="text" name="discount_type"  onkeyup="vatclassauto()"/>
	<input type="hidden" name="total" value="100" />
           
              
              
              
              <tr>
                <td class="main" valign="top"><?php echo 'Tot 0%:'; ?></td>
                <td class="main" align="right"><?php echo tep_draw_input_field('sum_vat_no', '', 'id="discount_type"  onkeyup="vatclassauto()" size="18" class="flatField" style="width:150px;"'); ?></td>
              </tr>
              <tr>
                <td class="main" valign="top"><?php echo 'Tot.incl'; ?></td>
                <td class="main" align="right"><?php echo tep_draw_input_field('sum', '', 'size="18" class="flatField" style="width:150px;"'); ?><span id="update">0,00</span></td>
              </tr>
              <tr>
                <td class="main" valign="top"><?php echo ENTRY_INVOICE_DATE; ?></td>
                <td class="main" align="right"><script language="javascript">dateInvoice.writeControl(); dateInvoice.dateFormat="yyyy-MM-dd";</script></td>
              </tr>
              <tr>
                <td class="main" valign="top"><?php echo ENTRY_EXPIRES; ?></td>
                <td class="main" align="right"><script language="javascript">dateExpires.writeControl(); dateExpires.dateFormat="yyyy-MM-dd";</script></td>
              </tr>
              <tr>
                <td class="main" valign="top" colspan="1"><?php echo ENTRY_COMMENT; ?></td>
                 <td class="main" align="right"><?php echo tep_draw_textarea_field('comment', 'soft', '45', '4', '', 'class="flatField" style="width:150px;" '); ?></td>
              </tr>
              <tr>
                <td colspan="2" align="right"><?php echo tep_image_submit('button_insert.gif', IMAGE_INSERT); ?></td>
             </tr>
             </form>
            </table></td>
          </tr>
        </table></td>
      </tr>

<?php
  } else if ( ($HTTP_GET_VARS['action'] == 'edit') && ($invoice_exists) ) {
    
    if (in_array($oInfo->receiver, $beneficiary_array)) {
      $beneficiary_dropdown = $oInfo->receiver;
      $receiver = '';
    } else {
      $beneficiary_dropdown = '';
      $receiver = $oInfo->receiver;
    }
?>

      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
            <td class="pageHeading" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action'))) . '">' . tep_image_button('button_back.gif', IMAGE_BACK) . '</a>'; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="0" cellpadding="2">
          <tr>
            <td colspan="3"><?php echo tep_draw_separator(); ?></td>
          </tr>
          <tr>
            <td colspan="3">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="3" class="main"><b><?php echo sprintf(HEADING_TITLE_EDIT, $oInfo->invoices_id); ?></b></td>
          </tr>
          <tr>
            <td colspan="3"><?php echo tep_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
          </tr>
          <tr>
            <td valign="top"><table  border="0" cellspacing="0" cellpadding="2">
              <?php echo tep_draw_form('invoice', FILENAME_ACCOUNTING, tep_get_all_get_params(array('action')) . 'action=update_invoice'); ?>
              <tr>
                <td class="main" valign="top"><?php echo ENTRY_RECEIVER; ?></td>
                <td class="main" align="right"><?php echo tep_draw_pull_down_menu('beneficiary', $beneficiaries, $beneficiary_dropdown, 'id="benef_dropdown" class="flatField" onChange="benefChange(\'dropdown\')"'); ?></td>
                
                
     
                
                
                  <td colspan="3" class="main"><b><?php echo $beneficiary['fakta']; ?></b></td>
              </tr>
          
              <tr>
                <td class="main" valign="top"><?php echo ENTRY_SUM; ?></td>
                <td class="main" align="right"><?php echo tep_draw_input_field('sum', number_format($oInfo->sum, 2, ',', ''), 'size="18" class="flatField"'); ?></td>
              </tr>
              <tr>
                <td class="main" valign="top"><?php echo ENTRY_INVOICE_DATE; ?></td>
                <td class="main" align="right"><script language="javascript">dateInvoice.writeControl(); dateInvoice.dateFormat="yyyy-MM-dd";</script></td>
              </tr>
              <tr>
                <td class="main" valign="top"><?php echo ENTRY_EXPIRES; ?></td>
                <td class="main" align="right"><script language="javascript">dateExpires.writeControl(); dateExpires.dateFormat="yyyy-MM-dd";</script></td>
              </tr>
              <tr>
                <td class="main" valign="top" colspan="2"><?php echo ENTRY_COMMENT; ?><br>
                  <?php echo tep_draw_textarea_field('comment', 'soft', '45', '3', $oInfo->comment, 'class="flatField"'); ?></td>
              </tr>
              <tr>
                <td colspan="2" align="right"><?php echo tep_image_submit('button_update.gif', IMAGE_UPDATE); ?></td>
             </tr>
             </form>
            </table></td>
          </tr>
        </table></td>
      </tr>


<?php
  } else {
    
    $on_hold_menu = array(
      array('id'=>'', 'text'=>'-'),
      array('id'=>'on', 'text'=>TEXT_MENU_ON_HOLD),
      array('id'=>'not', 'text'=>TEXT_MENU_NOT_HOLD)
      );
    $paid_menu = array(
      array ('id'=>'', 'text'=>'-'),
      array ('id'=>'paid', 'text'=>TEXT_PAID_INVOICES),
      array ('id'=>'unpaid', 'text'=>TEXT_UNPAID_INVOICES)
      );
?>
      <tr>
        <td width="100%"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
            <td align="right"><table border="0" width="100%" cellspacing="0" cellpadding="0">
              
              
              
          
              <tr><?php echo tep_draw_form('show', FILENAME_ACCOUNTING, '', 'get'); ?>
                 <td class="smallText" align="right">
                
                <td class="smallText" align="right"><?php
 echo HEADING_TITLE_SHOW;
 $show_o = array();
 $show_o[] = array ('id'=>'', 'text'=>TEXT_ALL_INVOICES);
 $show_o[] = array ('id'=>'paid', 'text'=>TEXT_PAID_INVOICES);
 $show_o[] = array ('id'=>'unpaid', 'text'=>TEXT_UNPAID_INVOICES);
 echo ' ' . tep_draw_pull_down_menu('show', $show_o, '', 'onChange="this.form.submit();"');
              ?></td>
              </form>
          
          
          
          
          </tr>            
            </table></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="dataTableHeadingRow">
                  <td class="dataTableHeadingContent" align="left" width="80"><?php echo DrawSortLink('expires', TABLE_HEADING_DATE); ?></td>
                    <td class="dataTableHeadingContent"width="80"><?php echo DrawSortLink('receiver', 'Verification ID'); ?></td>
                <td class="dataTableHeadingContent"><?php echo DrawSortLink('receiver', 'Specifikation'); ?></td>
                   <td class="dataTableHeadingContent" align="left" width="80"><?php echo DrawSortLink('expires', 'Debit/Credit'); ?></td>
                <td class="dataTableHeadingContent"width="80"><?php echo DrawSortLink('receiver', TABLE_HEADING_RECEIVER); ?></td>
                
                <td class="dataTableHeadingContent" align="right"><?php echo DrawSortLink('sum_vat_no', 'Tot.excl'); ?></td>
              <td class="dataTableHeadingContent" align="right"><?php echo DrawSortLink('vat_percent', 'VAT %'); ?></td>
                 <td class="dataTableHeadingContent" align="right"><?php echo DrawSortLink('sum', 'VAT Sum'); ?></td>
                 <td class="dataTableHeadingContent" align="right"><?php echo DrawSortLink('sum', 'Total Sum'); ?></td>
                
                
                <td class="dataTableHeadingContent" align="right"><?php //echo DrawSortLink('expires', TABLE_HEADING_EXPIRES); ?></td>
                <td class="dataTableHeadingContent" align="center"><?php //echo DrawSortLink('paid', TABLE_HEADING_PAID); ?></td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
<?php
    $show_q = "";
    if ($search_params['show']) {
      $show = $search_params['show'];
      if ($show == 'paid') {
        $show_q = 'and i.paid is not null';
      } else if ($show == 'unpaid') {
        $show_q = 'and i.paid is null';
      }
    }
    switch ($search_params['hold']) {
      case 'on':
        $show_q .= " and i.on_hold = '1'";
        break;
      case 'not':
        $show_q .= " and i.on_hold = '0'";
        break;
    }
    
    $benf_sql = '';
    if (is_array($search_params['benf']) && sizeof($search_params['benf'])) {
      foreach ($search_params['benf'] as $key => $value) {
        if ($value != '-') {
          if ($benf_sql)
            $benf_sql .= " or ";
          $benf_sql .= "i.receiver = '".addslashes($value)."'";
        }
      }
      if ($benf_sql)
        $show_q .= " and ($benf_sql)";
    }
    if (!$benf_sql && $search_params['recv']) {
      $show_q .= " and i.receiver like '%".addslashes($search_params['recv'])."%'";
    }
      if (!$benf_sql && $search_params['aclass']) {
      $show_q .= " and i.account_class_booked like '%".addslashes($search_params['aclass'])."%'";
    }
    if ($search_params['inv'])
      $show_q .= " and i.invoices_id = '".(int)$search_params['inv']."'";
    if ($search_params['comm'])
      $show_q .= " and i.comment like '%".addslashes($search_params['comm'])."%'";
    if ($search_params['sumfr']) {
      $sum_from = (float)str_replace(',', '.', $search_params['sumfr']);
      $show_q .= " and i.sum >= ".$sum_from;
    }
    if ($search_params['sumto']) {
      $sum_to = (float)str_replace(',', '.', $search_params['sumto']);
      $show_q .= " and i.sum <= ".$sum_to;
    }
    
    
      if ($search_params['month'])
      $show_q .= " and i.invoice_date = '".addslashes($search_params['month'])."'";
    
    if ($search_params['invfr'])
      $show_q .= " and i.invoice_date >= '".addslashes($search_params['invfr'])."'";
    if ($search_params['invto'])
      $show_q .= " and i.invoice_date <= '".addslashes($search_params['invto'])." 23:59:59'";
    if ($search_params['expfr'])
      $show_q .= " and i.expires >= '".addslashes($search_params['expfr'])."'";
    if ($search_params['expto'])
      $show_q .= " and i.expires <= '".addslashes($search_params['expto'])." 23:59:59'";
    if ($search_params['paidfr'])
      $show_q .= " and i.paid >= '".addslashes($search_params['paidfr'])."'";
    if ($search_params['paidto'])
      $show_q .= " and i.paid <= '".addslashes($search_params['paidto'])." 23:59:59'";
    
    $orderq = "order by expires";
    if ($sort != "") {
      $orderq = "order by $sort" .($desc? ' desc': '');
    }
    
    $invoices_query_raw = "select i.invoices_id, i.deb_cred, i.receiver, i.specifikation,i.account_class_booked, i.sum,i.sum_vat_no, i.vat_percent, i.vat_osuus, i.invoice_date, i.expires, i.paid, i.on_hold, i.comment from " .TABLE_ACCOUNTING ." as i where 1 $show_q  $orderq";
    $invoices_split = new splitPageResults($_GET['page'], MAX_DISPLAY_SEARCH_RESULTS, $invoices_query_raw, $invoices_query_numrows);
    $invoices_query = tep_db_query($invoices_query_raw);
    while ($invoices = tep_db_fetch_array($invoices_query)) {
      if (((!$_GET['oID']) || ($_GET['oID'] == $invoices['invoices_id'])) && (!$oInfo)) {
        $oInfo = new objectInfo($invoices);
      }

      if ( (is_object($oInfo)) && ($invoices['invoices_id'] == $oInfo->invoices_id) ) {
        echo '              <tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'hand\'" onclick="document.location.href=\'' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action')) . 'oID=' . $oInfo->invoices_id . '&action=edit') . '\'">' . "\n";
      } else {
        echo '              <tr class="dataTableRow" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'hand\'" onmouseout="this.className=\'dataTableRow\'" onclick="document.location.href=\'' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID')) . 'oID=' . $invoices['invoices_id']) . '\'">' . "\n";
      }
?>
              
              
               <td class="dataTableContent" align="left"><?php echo tep_date_short($invoices['invoice_date']); ?></td>
               <td class="dataTableContent" width="10"><?php echo $invoices['invoices_id']; ?></td>
               <td class="dataTableContent"><?php echo $invoices['specifikation']; ?></td>
               <td class="dataTableContent"><?php echo $invoices['deb_cred']; ?></td>
             
              
               <td class="dataTableContent"><?php echo $invoices['account_class_booked'], ' ',$invoices['receiver']; ?></td>
                <td class="dataTableContent" align="right"><?php echo $currencies->format($invoices['sum_vat_no']); ?></td>
               <td class="dataTableContent" align="right"><?php echo $invoices['vat_percent']; ?>%</td>
               <td class="dataTableContent" align="right"><?php echo $currencies->format($invoices['vat_osuus']); ?></td>
              
                <td class="dataTableContent" align="right"><?php echo $currencies->format($invoices['sum']); ?></td>
               
                <td class="dataTableContent" align="right"><?php
      $t = tep_date_short($invoices['expires']);
      if (strtotime($invoices['expires']) < time() && $invoices['paid'] == "")
        $t = "<font color=\"red\">$t</font>";
     // echo $t;
              ?></td>
                <td class="dataTableContent" align="center">
<?php
//       if ($invoices['paid']) {
//         echo tep_date_short($invoices['paid']);
//       } else if ($invoices['on_hold']) {
//         echo TEXT_ON_HOLD;
//       } else
//         echo '&nbsp;';
?>
                </td>
                <td class="dataTableContent" align="right"><?php if ( (is_object($oInfo)) && ($invoices['invoices_id'] == $oInfo->invoices_id) ) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID')) . 'oID=' . $invoices['invoices_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?>&nbsp;</td>
              </tr>
<?php
    }
?>
              <tr>
                <td colspan="6"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                  <tr>
                    <td class="smallText" valign="top"><?php echo $invoices_split->display_count($invoices_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $_GET['page'], TEXT_DISPLAY_NUMBER_OF_INVOICES); ?></td>
                    <td class="smallText" align="right"><?php echo $invoices_split->display_links($invoices_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $_GET['page'], tep_get_all_get_params(array('page', 'oID', 'action'))); ?></td>
                  </tr>
                </table></td>
              </tr>
              <tr>
               
               <td >
                 
                 <?php  echo	'<a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action')).'action=new'). '">' .'<button class="button_update">'.'New Debit'.'</button>'. '</a>';?> 
                 
                 <?php  echo	'<a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('action')).'action=new'). '">' .'<button class="button_update">'.'New Credit'.'</button>'. '</a>';?>
                
                </td>
 

                
                <td colspan="3" align="right">
                                   <?php  echo	'<a href="' . tep_href_link(FILENAME_ACCOUNTING_PRINT, tep_get_all_get_params(array('action')).'view=order&amp;orig=order'). '">' .'<button class="button_update">'.IMAGE_PRINT.'</button>'. '</a>';?>


                  
               </td>
                
                
              </tr>
            </table></td>
<?php
  $heading = array();
  $contents = array();
  switch ($_GET['action']) {
    case 'delete':
      $heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_DELETE_INVOICE . '</b>');

      $contents = array('form' => tep_draw_form('invoices', FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action')) . 'oID=' . $oInfo->invoices_id . '&action=deleteconfirm'));
      $contents[] = array('text' => TEXT_INFO_DELETE_INTRO . '<br>');
      $contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_delete.gif', IMAGE_DELETE) . ' <a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action')) . 'oID=' . $oInfo->invoices_id) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>');
      break;
    default:
      if (is_object($oInfo)) {
        $heading[] = array('text' => '<b>[' . $oInfo->invoices_id . ']&nbsp;&nbsp;' . tep_date_short($oInfo->expires) . '</b>');

        $contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action')) . 'oID=' . $oInfo->invoices_id . '&action=edit') . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT) . '</a> <a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action')) . 'oID=' . $oInfo->invoices_id . '&action=delete') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE) . '</a>');
        if ($oInfo->paid == "") {
          $_t = '<a href="' . tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action')) . 'oID=' . $oInfo->invoices_id . '&action=pay') . '">' . tep_image_button('button_pay.gif', IMAGE_PAY) . '</a>';
          if (!$oInfo->on_hold)
            $_t .= ' <a href="'.tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action')) . 'oID=' . $oInfo->invoices_id . '&action=put_on_hold') . '">' . tep_image_button('button_put_on_hold.gif', IMAGE_PUT_ON_HOLD) . '</a>';
          else
            $_t .= ' <a href="'.tep_href_link(FILENAME_ACCOUNTING, tep_get_all_get_params(array('oID', 'action')) . 'oID=' . $oInfo->invoices_id . '&action=release') . '">' . tep_image_button('button_release.gif', IMAGE_RELEASE) . '</a>';
          $contents[] = array('align' => 'center', 'text' => $_t);
        }
        $contents[] = array('text' => '<br>' . TEXT_INFO_COMMENT . '<br> ' . $oInfo->comment);
      }
      break;
  }

  if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
    echo '            <td width="15%" valign="top">' . "\n";

    $box = new box;
    echo $box->infoBox($heading, $contents);

    echo '            </td>' . "\n";
  }
?>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo tep_draw_separator('pixel_trans.gif', 1, 20); ?></td>
      </tr>
      <tr>
        <td><table border="0" cellpadding="2" cellspacing="0">
          <?php echo tep_draw_form('search', FILENAME_ACCOUNTING, '', 'post').tep_draw_hidden_field('search', 1); ?>
          <tr>
            <td class="main" colspan="9"><b><?php echo TEXT_SEARCH; ?></b></td>
          </tr>
          <tr>
            <td colspan="9"><?php echo tep_draw_separator(); ?></td>
          </tr>
          <tr>
            <td colspan="9"><?php echo tep_draw_separator('pixel_trans.gif', 1, 1); ?></td>
          </tr>
          <tr>
            <td class="dataTableContent"><?php echo ENTRY_RECEIVER.'&nbsp;'; ?></td>
            <td colspan="3"><?php echo tep_draw_input_field('aclass', $search_params['aclass'], 'class="flatField" id="benef_text" size="32" onChange="benefChange(\'text\')"'); ?></td>
            <td rowspan="9"><?php echo tep_draw_separator('pixel_trans.gif', 10, 1); ?></td>
            <td class="dataTableContent"><?php echo ENTRY_INVOICE_ID.'&nbsp;'; ?></td>
            <td colspan="3"><?php echo tep_draw_input_field('inv', $search_params['inv'], 'class="flatField" size="10"'); ?></td>
          </tr>
          <tr>
            <td rowspan="8" valign="top" class="dataTableContent"><?php echo TEXT_OR.'&nbsp;'; ?></td>
            <td colspan="3" rowspan="8" valign="top"><select name="benf[]" multiple size="10" onChange="benefChange('dropdown')" id="benef_dropdown" class="flatField">
<?php
  foreach ($beneficiaries as $beneficiary) {
    if (is_array($search_params['benf'], $search_params['aclass']) && in_array($beneficiary['id'], $search_params['benf'], $search_params['aclass'])) {
      $params = ' selected';
    } else
      $params = '';
    echo '<option value="'.$beneficiary['id'].'"'.$params.'>'.$beneficiary['accountc'],' ', $beneficiary['text'] .'</option>';
  }
?>
            </select></td>
          </tr>
          <tr>
            <td class="dataTableContent"><?php echo ENTRY_COMMENT.'&nbsp;'; ?></td>
            <td colspan="3"><?php echo tep_draw_input_field('comm', $search_params['comm'], 'class="flatField" size="32"'); ?></td>
          </tr>
          <tr>
            <td class="dataTableContent"><?php echo ENTRY_INVOICE_DATE.'&nbsp;'; ?></td>
            <td><script language="javascript">dateInvoiceFrom.writeControl(); dateInvoiceFrom.dateFormat="yyyy-MM-dd";</script></td>
            <td class="dataTableContent">-</td>
            <td><script language="javascript">dateInvoiceTo.writeControl(); dateInvoiceTo.dateFormat="yyyy-MM-dd";</script></td>
          </tr>
          
        
          
          
          <tr>
            <td class="dataTableContent"><?php echo ENTRY_EXPIRES.'&nbsp;'; ?></td>
            <td><script language="javascript">dateExpiresFrom.writeControl(); dateExpiresFrom.dateFormat="yyyy-MM-dd";</script></td>
            <td class="dataTableContent">-</td>
            <td><script language="javascript">dateExpiresTo.writeControl(); dateExpiresTo.dateFormat="yyyy-MM-dd";</script></td>
          </tr>
          <tr>
            <td class="dataTableContent"><?php echo ENTRY_PAID.'&nbsp;'; ?></td>
            <td><script language="javascript">datePaidFrom.writeControl(); datePaidFrom.dateFormat="yyyy-MM-dd";</script></td>
            <td class="dataTableContent">-</td>
            <td><script language="javascript">datePaidTo.writeControl(); datePaidTo.dateFormat="yyyy-MM-dd";</script></td>
          </tr>
          <tr>
            <td class="dataTableContent"><?php echo ENTRY_SUM.'&nbsp'; ?></td>
            <td><?php echo tep_draw_input_field('sumfr', $search_params['sumfr'], 'class="flatField" size="10"'); ?></td>
            <td class="dataTableContent">-</td>
            <td><?php echo tep_draw_input_field('sumto', $search_params['sumto'], 'class="flatField" size="10"'); ?></td>
          </tr>
          <tr>
            <td class="dataTableContent"><?php echo ENTRY_ON_HOLD.'&nbsp'; ?></td>
            <td colspan="3"><?php echo tep_draw_pull_down_menu('hold', $on_hold_menu, $search_params['hold'], 'class="flatField"'); ?></td>
          </tr>
          <tr>
            <td class="dataTableContent"><?php echo ENTRY_SHOW_ONLY.'&nbsp'; ?></td>
            <td colspan="3"><?php echo tep_draw_pull_down_menu('show', $paid_menu, $search_params['show'], 'class="flatField"'); ?></td>
          </tr>
          <tr>
            <td colspan="9"><?php echo tep_draw_separator('pixel_trans.gif', 1, 1); ?></td>
          </tr>
          <tr>
            <td colspan="9" align="right"><?php echo '<a href="'.tep_href_link(FILENAME_ACCOUNTING).'">'.tep_image_button('button_reset.gif', IMAGE_RESET).'</a> '.tep_image_submit('button_search.gif', IMAGE_SEARCH).'&nbsp;'; ?></td>
          </tr>
          </form>
          
          
          
          
          
          
          
          
            <tr>
            <td colspan="9">           <?php echo tep_draw_form('search', FILENAME_ACCOUNTING, '', 'post').tep_draw_hidden_field('search', 1); ?>

</td>
          </tr>
<!--                 <tr>
             <td class="main" align="right" valign="top">
               <select name="invfr" id="invfr" class="flatField" style="width:150px;">
               <option value="2019-*-*">2019</option>
               <option value="2020-1-1">2020</option>
               </select>
               </td>
                  </tr> -->
              <tr>
             <td class="main" align="right" valign="top">
               <select name="month" id="month" class="flatField" style="width:150px;">
               <option value="YYYY-10-dd">January</option>
               <option value="2019-11-*">February</option>
               </select>
               </td>
                  </tr>
          <tr>
            <td colspan="1" align="left"><?php echo '<a href="'.tep_href_link(FILENAME_ACCOUNTING).'">'.tep_image_button('button_reset.gif', IMAGE_RESET).'</a> '.tep_image_submit('button_search.gif', IMAGE_SEARCH).'&nbsp;'; ?></td>
          </tr>
</form>
          






        </table></td>
      </tr>
<?php
  }
?>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php
    require(DIR_WS_INCLUDES . 'footer.php');
?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
