<?php
/**
 * The report view file of budget module of ZDOO.
 *
 * @copyright   Copyright 2009-2019 青岛易软天创网络科技有限公司(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     budget 
 * @version     $Id$
 * @link        http://www.zdoo.com
 */
?>
<?php include '../../common/view/header.html.php';?>
<?php
$clientLang = $this->app->getClientLang();
if($clientLang == 'en') $title = $year . ($period == 'week' ? $month : '') . ' ' . zget($deptPairs, $dept, '') . ' ' . $lang->budget->{$period . 'Budget'};
if($clientLang != 'en') $title = $year . $lang->year . ($period == 'week' ? zget($lang->budget->monthList, $month) : '') . zget($deptPairs, $dept, '') . $lang->budget->{$period . 'Budget'};
?>
<div id='menuActions'>
  <?php commonModel::printLink('budget', 'export', "period={$period}&year={$year}&month={$month}&week={$week}&dept={$dept}", $lang->exportIcon . $lang->export, "class='btn btn-secondary iframe' data-width='700'");?>
  <?php if($period == 'year'):?>
  <div class='btn-group'>
    <button period='button' data-toggle='dropdown' class='btn btn-primary dropdown-toggle'>
      <i class='icon icon-plus'> </i><?php echo $lang->budget->yearBudget;?> <span class='caret'></span>
    </button>
    <ul class='dropdown-menu'>
      <?php commonModel::printLink('budget', 'create', '', $lang->budget->create, "data-toggle='modal'", true, false, 'li');?>
      <?php commonModel::printLink('budget', 'batchCreate', "period=year&year={$year}&month=&week=&dept={$dept}&amebaAccount=&method=report", $lang->budget->batchCreate, '', true, false, 'li');?>
    </ul>
  </div>
  <?php endif;?>
  <?php if($period != 'week') commonModel::printLink('budget', 'batchCreate', "period=month&year={$year}&month=&week=&dept={$dept}&amebaAccount=&method=report", "<i class='icon icon-plus'> </i>" . $lang->budget->monthBudget, "class='btn btn-primary'");?>
  <?php if($period != 'year') commonModel::printLink('budget', 'batchCreate', "period=week&year={$year}&month={$month}&week=&dept={$dept}&amebaAccount=&method=report", "<i class='icon icon-plus'> </i>" . $lang->budget->weekBudget, "class='btn btn-primary'");?>
  <?php if(strpos(",{$amebaConfig->shareDepts},", ",{$dept},") !== false) commonModel::printLink('budget', 'share', "period=all&year={$year}&month={$month}&week={$week}&dept={$dept}&amebaAccount=shareExpense", "<i class='icon icon-sitemap'> </i>" . $lang->budget->share, "class='btn btn-primary' data-toggle='modal' data-width='400'");?>
</div>
<div class='panel'>
  <div class='panel-heading text-center'><strong><?php echo $title;?></strong></div>
  <div class='panel-body'>
  <table class='table table-bordered table-striped table-hover'>
    <thead>
      <tr class='text-center'>
        <th class='w-80px'><?php echo $lang->budget->item;?></th>
        <?php $class = ($clientLang == 'en' ? 'w-130px' : 'w-100px');?>
        <th class='<?php echo $class;?>'><?php echo $lang->budget->amebaAccount;?></th>
        <th class='w-140px'><?php echo $lang->budget->category;?></th>
        <th class='w-140px'><?php echo $lang->budget->itemCategory;?></th>
        <?php if($period == 'year'):?>
        <th class='w-150px'><?php echo ($year - 1) . ($clientLang != 'en' ? $lang->year : '');?></th>
        <th class='w-150px'><?php echo $year . ($clientLang != 'en' ? $lang->year : '');?></th>
        <?php $class = ($clientLang == 'en' ? 'w-130px' : 'w-100px');?>
        <th class='<?php echo $class;?>'><?php echo $lang->budget->rate;?></th>
        <th><?php echo $lang->budget->desc;?></th>
        <?php else:?>
        <?php $total = $period == 'month' ? $lang->budget->yearBudget : $lang->budget->monthBudget;?>
        <?php $dates = $period == 'month' ? $lang->budget->monthList : $weeks;?>
        <th class='w-150px'><?php echo $total;?></th>
        <?php foreach($dates as $date):?>
        <th><?php echo $date;?></th>
        <?php endforeach;?>
        <?php endif;?>
      </tr>
    </thead>
    <tbody>
      <?php
      $incomeRowCount = $expenseRowCount = 0;
      foreach($reportData as $key => $data)
      {
          if($key == 'profit' or $key == 'profitRate') continue;
      
          foreach($data as $amebaAccount => $categoryBudgets)
          {
              if($amebaAccount == 'total')
              {
                  ${$key . $amebaAccount . 'RowCount'} = 0;
              }
              else
              {
                  ${$amebaAccount . 'RowCount'} = 0;
              }

              foreach($categoryBudgets as $category => $budgets)
              {
                  if($amebaAccount == 'total')
                  {
                      ${$key . $amebaAccount . 'RowCount'} += count($budgets);
                  }
                  else
                  {
                      ${$amebaAccount . 'RowCount'} += count($budgets);
                  }

                  ${$key . 'RowCount'} += count($budgets);
              }
          }
      }

      foreach($reportData as $key => $data)
      {
          $keyIndex = 0;
          foreach($data as $amebaAccount => $categoryBudgets)
          {
              $amebaIndex = 0;
              foreach($categoryBudgets as $category => $budgets)
              {
                  $categoryIndex    = 0;
                  $categoryRowCount = count($budgets);
                  foreach($budgets as $item => $budget)
                  {
                      echo "<tr class='text-center'>";
                      if($keyIndex == 0)
                      {
                          $colspan = ($key == 'profit' or $key == 'profitRate') ? "colspan='4'" : '';
                          $rowspan = ($key == 'profit' or $key == 'profitRate') ? '' : "rowspan='" . ${$key . 'RowCount'} . "'";
                          echo "<td class='text-middle' $colspan $rowspan>{$lang->budget->$key}</td>";
                      }
                      if($key == 'income' or $key == 'expense')
                      {
                          if($amebaIndex == 0)
                          {
                              $colspan = ($amebaAccount == 'total' ? "colspan='3'" : '');
                              $label   = $lang->budget->amebaAccountList[$amebaAccount];
                              if($amebaAccount == 'total')
                              {
                                  $label = $lang->budget->$key . $label;
                                  $rowspan = "rowspan='" .  ${$key . $amebaAccount . 'RowCount'} . "'";
                              }
                              else
                              {
                                  $rowspan = "rowspan='" . ${$amebaAccount . 'RowCount'} . "'";
                              }
                              echo "<td class='text-middle' $rowspan $colspan>" . $label . '</td>';
                          }
                          if($categoryIndex == 0 && $amebaAccount != 'total')
                          {
                              $colspan = ($category == 'total' ? "colspan='2'" : '');
                              $label   = zget($incomeCategories, $category, zget($categoryPairs, $category));
                              if($category == 'total') $label = $lang->budget->amebaAccountList[$amebaAccount] . $lang->budget->amebaAccountList['total'];
                              echo "<td class='text-middle' rowspan='{$categoryRowCount}' $colspan>{$label}</td>";
                          }
                          if($category != 'total')
                          {
                              echo "<td>" . zget($incomeCategories, $item, zget($categoryPairs, $item, zget($productPairs, $item))) . '</td>';
                          }
                      }

                      if($period == 'year')
                      {
                          $lastYearBudget = isset($budget[$year - 1]) ? $budget[$year - 1] : '0.00';
                          $thisYearBudget = isset($budget[$year])     ? $budget[$year]     : '0.00';
                          echo "<td class='text-right'>{$lastYearBudget}</td>";
                          echo "<td class='text-right'>" . (empty($thisYearBudget->money) ? $thisYearBudget : $thisYearBudget->money) . '</td>';
                          echo "<td>{$budget['rate']}</td>";
                          echo "<td class='text-left'>" . (!empty($thisYearBudget->desc) ? $thisYearBudget->desc : '') . '</td>';
                      }
                      else
                      {
                          $totalBudget = isset($budget['total']) ? $budget['total'] : '0.00';
                          echo "<td class='text-right'>{$totalBudget}</td>";
                          foreach($dates as $date => $label)
                          {
                              $dateBudget = isset($budget[$date]) ? $budget[$date] : '0.00';
                              echo "<td class='text-right'>{$dateBudget}</td>";
                          }
                      }
                      echo '</tr>';
                      $keyIndex++;
                      $amebaIndex++;
                      $categoryIndex++;
                  }
              }
          }
      }
      ?>
    </tbody>
  </table>
  </div>
</div>
<?php include '../../common/view/footer.html.php';?>
