<?php
/**
 * The invoice statistics view file of trade module of ZDOO.
 *
 * @copyright   Copyright 2009-2018 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
 * @license     ZPL (http://zpl.pub/page/zplv12.html)
 * @author      Gang Liu <liugang@cnezsoft.com>
 * @package     trade
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
?>
<?php include '../../../common/view/header.html.php';?>
<div class='panel panel-sm'>
  <div class='panel-heading'>
    <?php if($companies):?>
    <div class='company dropdown'>
      <button type='button' class='btn btn-sm btn-default dropdown-toggle' data-toggle='dropdown'><?php echo zget($companies, $company);?> <span class='caret'></span></button>
      <ul class='dropdown-menu'>
        <?php foreach($companies as $id => $name):?>
        <li><?php echo html::a(helper::createLink('trade', 'invoiceStatistics', "company=$id&date=$currentYear$currentMonth"), $name);?></li>
        <?php endforeach;?>
      </ul>
    </div>
    <?php endif;?>
    <?php
    $yearLabel = $currentYear;
    if($this->app->getClientLang() == 'zh-cn' or $this->app->getClientLang() == 'zh-tw')
    {
        $monthLabel = ($currentMonth && $currentMonth != '00') ? (int)$currentMonth . $lang->month : '';
        if($monthLabel) $yearLabel .= $lang->year . $monthLabel;
    }
    else
    {
        $monthLabel = zget($lang->trade->monthList, $currentMonth, '');
        if($monthLabel) $yearLabel = $monthLabel . ', ' . $yearLabel . ' ';
    }
    ?>
    <?php if($tradeYears):?>
    <div class='date dropdown'>
      <button type='button' class='btn btn-sm btn-default dropdown-toggle' data-toggle='dropdown'><?php echo $yearLabel;?> <span class="caret"></span></button>
      <ul class='dropdown-menu'>
        <?php foreach($tradeYears as $tradeYear):?>
        <li class='dropdown-submenu'>
          <?php echo html::a(helper::createLink('trade', 'invoiceStatistics', "company=$company&date=$tradeYear"), $tradeYear);?>
          <ul class='dropdown-menu'>
            <?php foreach($tradeMonths[$tradeYear] as $tradeMonth):?>
            <?php $monthLabel = (($this->app->getClientLang() == 'zh-cn' or $this->app->getClientLang() == 'zh-tw') ? $tradeMonth . $lang->month : zget($lang->trade->monthList, $tradeMonth, ''));?>
            <li><?php echo html::a(helper::createLink('trade', 'invoiceStatistics', "company=$company&date=$tradeYear$tradeMonth"), $monthLabel);?></li>
            <?php endforeach;?>
            <li><?php echo html::a(helper::createLink('trade', 'invoiceStatistics', "company=$company&date={$tradeYear}"), $lang->trade->fullYear);?></li>
          </ul>
        </li>
        <?php endforeach;?>
      </ul>
    </div>
    <?php endif;?>
  </div>
  <div class='panel-body'>
    <h5 class='text-center'><?php echo $yearLabel . $lang->trade->report->invoiceStatistics . '(' . zget($this->lang->trade->report->unitList, 1) . ')';?></h5>
    <div class='panel' data-ride='table'>
      <table class='table table-bordered table-striped table-hover'>
        <thead>
          <tr>
            <th class='w-p50 noPadding' title='<?php echo $lang->invoice->common;?>'>
              <table class='table'>
                <tr>
                  <th class='w-120px'></th>
                  <th class='w-200px'></th>
                  <th></th>
                  <th class='w-160px'></th>
                </tr>
                <tr class='text-center'>  
                  <th colspan='4'><?php echo $lang->invoice->common;?></th>
                </tr>
                <tr class='text-center'>
                  <th class='w-120px' title='<?php echo $lang->invoice->drawnDate;?>'><?php echo $lang->invoice->drawnDate;?></th>
                  <th class='w-200px' title='<?php echo $lang->invoice->sn;?>'><?php echo $lang->invoice->sn;?></th>
                  <th title='<?php echo $lang->invoice->invoiceTitle;?>'><?php echo $lang->invoice->invoiceTitle;?></th>
                  <th class='w-160px' title='<?php echo $lang->invoice->money;?>'><?php echo $lang->invoice->money;?></th>
                </tr>
              </table>
            </th>
            <th class='w-p50 noPadding' title='<?php echo $lang->trade->typeList['in'];?>'>
              <table class='table'>
                <tr>
                  <th class='w-120px'></th>
                  <th></th>
                  <th class='w-160px'></th>
                </tr>
                <tr class='text-center'>  
                  <th colspan='3'><?php echo $lang->trade->typeList['in'];?></th>
                </tr>
                <tr class='text-center'>  
                  <th class='w-120px' title='<?php echo $lang->trade->date;?>'><?php echo $lang->trade->date;?></th>
                  <th title='<?php echo $lang->trade->customer;?>'><?php echo $lang->trade->customer;?></th>
                  <th class='w-160px' title='<?php echo $lang->trade->money;?>'><?php echo $lang->trade->money;?></th>
                </tr>
              </table>
            </th>
          </tr>
        </thead>
        <?php $invoiceTotal   = 0;?>
        <?php $tradeTotal     = 0;?>
        <?php $canViewInvoice = commonModel::hasPriv('cash.invoice', 'view');?>
        <?php $canViewTrade   = commonModel::hasPriv('cash.trade', 'view');?>
        <?php foreach($statistics as $type => $rows):?>
        <?php foreach($rows as $row):?>
        <?php $invoice       = zget($row, 'invoice', array());?>
        <?php $trades        = zget($row, 'trade', array());?>
        <?php $rowspan       = count($trades) ? count($trades) : 1;?>
        <?php $invoiceID     = zget($invoice, 'id', 0);?>
        <?php $drawnDate     = formatTime(zget($invoice, 'drawnDate', ''),  DT_DATE1);?>
        <?php $sn            = zget($invoice, 'sn', '&nbsp;');?>
        <?php $title         = zget($invoice, 'invoiceTitle', '&nbsp;');?>
        <?php $invoiceMoney  = zget($invoice, 'money', 0);?>
        <?php $invoiceTotal += (float)$invoiceMoney;?>
        <tr class='text-middle'>
          <td class='w-p50 noPadding'>
            <table class='table subTable'>
              <tr>
                <td class='w-120px text-center' rowspan='<?php echo $rowspan;?>'><?php echo $drawnDate;?></td>
                <td class='w-200px' rowspan='<?php echo $rowspan;?>' title='<?php echo $sn;?>'><?php echo $sn;?></td>
                <td rowspan='<?php echo $rowspan;?>' title='<?php echo $title;?>'><?php echo $title;?></td>
                <td class='w-160px text-right' rowspan='<?php echo $rowspan;?>' title='<?php echo $lang->detail;?>'>
                  <?php if($invoiceID && $canViewInvoice):?>
                  <?php echo html::a($this->createLink('cash.invoice', 'view', "invoiceID={$invoiceID}"), number_format($invoiceMoney, 2));?>
                  <?php else:?>
                  <?php echo $invoiceMoney ? number_format($invoiceMoney, 2) : '&nbsp;';?>
                  <?php endif;?>
                </td>
              </tr>
            </table>
          </td>
          <td class='w-p50 noPadding'>
            <table class='table subTable'>
              <?php if($trades):?>
              <?php foreach($trades as $trade):?>
              <?php $tradeID     = zget($trade, 'id', 0);?>
              <?php $date        = formatTime(zget($trade, 'date', ''), DT_DATE1);?>
              <?php $customer    = zget($customers, zget($trade, 'trader', ''), '&nbsp;');?>
              <?php $noInvoice   = zget($trade, 'noInvoice', '0');?>
              <?php $tradeMoney  = zget($trade, 'money', 0);?>
              <?php $tradeTotal += (float)$tradeMoney;?>
              <tr>
                <td class='w-120px text-center'><?php echo $date;;?></td>
                <td title='<?php echo $customer;?>'><?php echo $customer;?></td>
                <td class='w-160px text-right' title='<?php echo $lang->detail;?>'>
                  <?php if($noInvoice):?>
                  <span class='label label-warning'><?php echo $lang->trade->invoicelessLabel;?></span>
                  <?php endif;?>

                  <?php if($tradeID && $canViewTrade):?>
                  <?php echo html::a($this->createLink('cash.trade', 'view', "tradeID={$tradeID}&mode=in"), number_format($tradeMoney, 2));?>
                  <?php else:?>
                  <?php echo $tradeMoney ? number_format($tradeMoney, 2) : '&nbsp;';?>
                  <?php endif;?>
                </td>
              </tr>
              <?php endforeach;?>
              <?php else:?>
              <tr>
                <td class='w-120px'>&nbsp;</td>
                <td>&nbsp;</td>
                <td class='w-160px'>&nbsp;</td>
              </tr>
              <?php endif;?>
            </table>
          </td>
        </tr>
        <?php endforeach;?>
        <?php endforeach;?>
      </table>
      <div class='page-actions'>
        <table class='table table-hover table-striped table-bordered'>
          <tr>
            <th class='w-p50 noPadding'>
              <table class='table'>
                <tr class='text-center'>
                  <th class='w-120px'></th>
                  <th class='w-200px'></th>
                  <th class='text-right'><?php echo $lang->trade->report->invoiceTotal;?></th>
                  <th class='w-160px text-right'><?php echo number_format($invoiceTotal, 2);?></th>
                </tr>
              </table>
            </th>
            <th class='w-p50 noPadding'>
              <table class='table'>
                <tr class='text-center'>  
                  <th class='w-120px'></th>
                  <th class='text-right'><?php echo $lang->trade->report->tradeTotal;?></th>
                  <th class='w-160px text-right'><?php echo number_format($tradeTotal, 2);?></th>
                </tr>
              </table>
            </th>
          </tr>
        </table>
      </div>
    </div>
  </div>
</div>
<?php include '../../../common/view/footer.html.php';?>
