<?php if(!empty($expenses)):?>
<div id='expenseDIV' class='hide'>
  <div class='panel panel-block'>
    <div class='panel-heading'>
      <strong><?php echo $lang->contract->expenseDetail;?></strong>
    </div>
    <table class='table table-condensed table-striped table-hover'>
      <tr>
        <th class='w-80px'><?php echo $lang->trade->date;?></th>
        <th class='w-100px'><?php echo $lang->trade->handlers;?></th>
        <th class='w-200px'><?php echo $lang->trade->category;?></th>
        <th class='w-200px'><?php echo $lang->trade->trader;?></th>
        <th class='w-100px'><?php echo $lang->trade->product;?></th>
        <th class='w-80px text-right'><?php echo $lang->trade->money;?></th>
        <th><?php echo $lang->trade->desc;?></th>
        <?php if(commonModel::hasPriv('cash.trade', 'edit') or commonModel::hasPriv('cash.trade', 'delete')):?>
        <th class='w-80px'><?php echo $lang->actions;?></th>
        <?php endif;?>
      </tr>
      <?php foreach($expenses as $trade):?>
      <tr>
        <td><?php echo formatTime($trade->date, DT_DATE1);?></td>
        <td><?php foreach(explode(',', $trade->handlers) as $handler) echo zget($users, $handler, '') . ' ';?></td>
        <td title='<?php echo zget($categories, $trade->category, '');?>'><?php echo zget($categories, $trade->category, '');?></td>
        <td title='<?php echo zget($customers, $trade->trader, '');?>'><?php echo zget($customers, $trade->trader, '');?></td>
        <td title='<?php echo zget($products, $trade->product, '');?>'><?php echo zget($products, $trade->product, '');?></td>
        <td class='text-right'><?php echo formatMoney($trade->money);?></td>
        <td title='<?php echo $trade->desc;?>'><?php echo $trade->desc;?></td>
        <?php if(commonModel::hasPriv('cash.trade', 'edit') or commonModel::hasPriv('cash.trade', 'delete')):?>
        <td>
          <?php commonModel::printLink('cash.trade', 'edit',   "tradeID={$trade->id}", $lang->edit,   "class='expense'");?>
          <?php commonModel::printLink('cash.trade', 'delete', "tradeID={$trade->id}", $lang->delete, "class='deleter'");?>
        </td>
        <?php endif;?>
      </tr>
      <?php endforeach;?>
    </table>
  </div>
</div>
<?php endif;?>

<table class='companyInfo hide'>
  <tbody>
    <tr>
      <th><?php echo $lang->contract->company;?></th>
      <td><?php echo zget($companies, $contract->company, '');?></td>
    </tr>
  </tbody>
</table>

<?php if(!empty($contract->quotations)):?>
<div id='quotationBox' class='hide'>
  <div class='panel panel-block'>
    <div class='panel-heading'><strong><?php echo $lang->contract->quotation;?></strong></div>
    <?php foreach($contract->quotations as $quotationID => $quotation):?>
    <div class='panel-body'>
      <table class='table table-bordered'>
        <thead>
          <tr class='text-center'>
            <th colspan='5'>
              <?php echo $quotation->name;?>
              <?php commonModel::printLink('contract', 'editQuotation', "contractID={$contract->id}&quotationID=$quotationID", "<i class='icon-pencil'></i>", "data-toggle='modal' title='{$lang->edit}'", '', true);?>
            </th>
          </tr>
          <tr class='text-center'>
            <th><?php echo $lang->orderProduct->product;?></th>
            <th class='w-60px'><?php echo $lang->orderProduct->unit;?></th>
            <th class='w-100px'><?php echo $lang->orderProduct->price;?></th>
            <th class='w-100px'><?php echo $lang->orderProduct->amount;?></th>
            <th class='w-100px'><?php echo $lang->orderProduct->money;?></th>
          </tr>
        </thead>
        <?php foreach($quotation->orderProducts as $orderProduct):?>
        <tr class='text-center'>
          <?php $productName = zget($products, $orderProduct->product);?>
          <td title='<?php echo $productName;?>'>
            <?php if(!commonModel::printLink('product', 'view', "id={$orderProduct->product}", $productName)) echo $productName;?>
          </td>
          <td><?php echo $orderProduct->unit;?></td>
          <td><?php echo $orderProduct->price;?></td>
          <td><?php echo $orderProduct->amount;?></td>
          <td><?php echo $orderProduct->money;?></td>
        </tr>
        <?php endforeach;?>
        <tr class='text-center'>
          <th colspan='3'></th>
          <th><?php echo $lang->order->total;?></th>
          <td><?php echo $quotation->amount;?></td>
        </tr>
      </table>
    </div>
    <?php endforeach;?>
    </div> 
  </div>
</div>
<?php endif;?>

<?php
js::set('contractID', $contract->id);
$btnGroups  = '';
$quoteBtn   = '';
$expenseBtn = '';
$psiBtns    = '';
if(commonModel::hasPriv('contract', 'createQuotation')) $quoteBtn   = html::a(inlink('createQuotation', "contractID=$contract->id"), $lang->contract->quotation, "class='btn' data-toggle='modal'");
if(commonModel::hasPriv('contract', 'expense'))         $expenseBtn = html::a(inlink('expense', "contractID=$contract->id"), $lang->contract->expense, "class='btn' data-toggle='modal' data-width='600'");
if(commonModel::hasPriv('psi.sale', 'create'))          $psiBtns   .= html::a(helper::createLink('psi.sale', 'create', "contractID=$contract->id"), $lang->contract->sale, "class='btn psiBtn'");
if(commonModel::hasPriv('psi.purchase', 'create'))      $psiBtns   .= html::a(helper::createLink('psi.purchase', 'create', "contractID=$contract->id"), $lang->contract->purchase, "class='btn psiBtn'");
if($quoteBtn or $psiBtns) $btnGroups = "{$quoteBtn}{$psiBtns}<div class='divider'></div>";

$invoiceHtml = commonModel::printLink('contract', 'invoice', "contractID={$contract->id}", $lang->contract->invoiceList, "class='invoice-list btn'", false);
?>

<?php if($btnGroups):?>
<style>
.main-col .main-actions .btn-toolbar .btn{padding-left: 5px; padding-right: 5px;}
</style>
<?php endif;?>

<script>
//if($('#expenseDIV').length) $('.row-table .col-main div:first').after($('#expenseDIV').html());
if($('#quotationBox').length) $('.main-row .main-col div:first').after($('#quotationBox').html());
$('.main-actions .btn-toolbar').find('[href*=createRecord]').after(<?php echo helper::jsonEncode($expenseBtn);?>);
$('.main-actions .btn-toolbar #backButton').next('.divider').after(<?php echo helper::jsonEncode($btnGroups);?>);

var invoiceHtml = <?php echo json_encode($invoiceHtml);?>;
if(invoiceHtml)
{
    $('.main-actions > .btn-toolbar #backButton').next('.divider').after(invoiceHtml);

    $.get(createLink('invoice', 'ajaxGetInvoiceCount', "customerID=&contractID=" + v.contractID), function(invoiceCount)
    {
        if(invoiceCount == 0)
        {
            $('.invoice-list').attr('href', createLink('crm.invoice', 'create', "customerID=&contractID=" + v.contractID));
        }
        else
        {
            $('.invoice-list').modalTrigger({width: '80%'});
        }
    });

    $('.main-actions > .btn-toolbar .divider').last().remove();
    $('.main-actions > .btn-toolbar .divider').last().remove();
}


$('.side-col .table-data tr:first-child').after($('.companyInfo tbody').html());
</script>
