<?
//include ('pdf_header_reverse.php');
include('includes/config/config.php');
include('includes/functions.php');
header("Content-type:application/octet-stream");

header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=yearly_expense_report_excel.xls");
header("Pragma: no-cache");
header("Expires: 0");
error_reporting(0);
?>
<?php

$year = $_POST['year'];
$year2 = $year + 1;
$ohc = $_SESSION['current_ohcttype'];



$ZeroAllowed = "N";
error_log("key val before"  . $ZeroAllowed);
$ZeroAllowedKey = getConfigKey("ZERO_QUANTITY_ALLOWED");
error_log("key val "  . $ZeroAllowedKey);
if ($ZeroAllowedKey == "TRUE") {
  $ZeroAllowed = "Y";
}
error_log("key val after "  . $ZeroAllowed);

?>

<style type="text/css">
  body,
  div,
  table,
  thead,
  tbody,
  tfoot,
  tr,
  th,
  td,
  p {
    font-family: "Calibri";
    font-size: x-small
  }

  a.comment-indicator:hover+comment {
    background: #ffd;
    position: absolute;
    display: block;
    border: 1px solid black;
    padding: 0.5em;
  }

  a.comment-indicator {
    background: red;
    display: inline-block;
    border: 1px solid black;
    width: 0.5em;
    height: 0.5em;
  }

  comment {
    display: none;
  }

  .btn {
    cursor: pointer;
  }
</style>

<body>
  <?php include('excel_ohc_header.php'); ?>

  <table width="100%">
    <tr>

      <td align="center" style="font-size: 15px"><strong>Yearly Medicine Expense Report, <?= getTableFieldValue('ohc_type', 'ohc_type_name', 'ohc_type_id', $ohc) ?></strong></td>

    </tr>
  </table>
  <br>


  <table width="100%" border="1" cellspacing="0">
    <colgroup width="36"></colgroup>
    <colgroup width="142"></colgroup>
    <colgroup width="66"></colgroup>
    <colgroup width="43"></colgroup>
    <colgroup width="44"></colgroup>
    <colgroup span="5" width="39"></colgroup>
    <colgroup width="47"></colgroup>
    <colgroup span="4" width="39"></colgroup>
    <colgroup width="66"></colgroup>
    <colgroup span="7" width="39"></colgroup>
    <colgroup width="46"></colgroup>
    <colgroup width="42"></colgroup>
    <colgroup width="44"></colgroup>
    <colgroup span="2" width="39"></colgroup>
    <colgroup width="62"></colgroup>
    <!-- <colgroup span="2" width="64"></colgroup> -->
    <tr>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" colspan=16 height="48" align="center" valign=middle bgcolor="#DEEBF7"><b>
          <font size=4 color="#000000">MONTHLY WISE MEDICINE RECEIVED BY DISPENSARY
          </font>
        </b></td>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" colspan=14 align="center" valign=middle bgcolor="#BDD7EE"><b>
          <font size=4 color="#000000">MONTHLY WISE EXPENCES OF MEDICINE </font>
        </b></td>
      <!-- <td align="left" valign=bottom>
        <font color="#000000"><br></font>
      </td> -->
    </tr>
    <tr>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" colspan=16 height="34" align="center" valign=middle bgcolor="#DEEBF7"><b>
          <font size=4 color="#000000"> RECEIVED</font>
        </b></td>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" colspan=14 align="center" valign=middle bgcolor="#BDD7EE"><b>
          <font size=4 color="#000000"> ISSUED</font>
        </b></td>
      <!-- <td align="left" valign=bottom>
        <font color="#000000"><br></font>
      </td> -->
    </tr>
    <tr>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" colspan=16 height="30" align="center" valign=middle bgcolor="#DEEBF7"><b>
          <font size=4>YEAR: <?= $year ?>-<?= $year2 ?></font>
        </b></td>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" colspan=14 align="center" valign=middle bgcolor="#BDD7EE"><b>
          <font size=4 color="#000000">YEAR: <?= $year ?>-<?= $year2 ?></font>
        </b></td>
      <!-- <td align="left" valign=bottom>
        <font color="#000000"><br></font>
      </td> -->
    </tr>
    <tr>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" height="50" align="center" valign=middle bgcolor="#92D050"><b>SL.NO</b></td>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="center" valign=middle bgcolor="#92D050"><b>ITEMS</b></td>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="center" valign=middle bgcolor="#92D050"><b>PREVIOUS STOCK</b></td>
      <?php for ($i = 4; $i <= 12; $i++) {
        $month_name = date("F", mktime(0, 0, 0, $i, 10)); ?>
        <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="center" valign=middle bgcolor="#92D050"><b><?= $month_name ?></b></td>
      <? } ?>

      <?php for ($i = 1; $i <= 3; $i++) {
        $month_name = date("F", mktime(0, 0, 0, $i, 10)); ?>
        <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="center" valign=middle bgcolor="#92D050"><b><?= $month_name ?></b></td>
      <? } ?>
      <td style="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="center" valign=middle bgcolor="#FFFF00"><b>
          <font size=4>Total<br>Rec.</font>
        </b></td>
      <?php for ($i = 4; $i <= 12; $i++) {
        $month_name = date("F", mktime(0, 0, 0, $i, 10)); ?>

        <td style="border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="center" valign=middle bgcolor="#F4B183"><b><?= $month_name ?></b></td>
      <? } ?>

      <?php for ($i = 1; $i <= 3; $i++) {
        $month_name = date("F", mktime(0, 0, 0, $i, 10)); ?>

        <td style="border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="center" valign=middle bgcolor="#F4B183"><b><?= $month_name ?></b></td>
      <? } ?>
      <td style="border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000" align="center" valign=middle bgcolor="#C55A11"><b>
          <font size=3>Total<br>Issue</font>
        </b></td>
      <td align="center" valign=middle bgcolor="#00B050"><b>
          <font size=4 color="#000000">Current<br>Stock</font>
        </b></td>
      <!-- <td align="left" valign=bottom>
        <font color="#000000"><br></font>
      </td> -->
    </tr>
    <?

    error_reporting(E_ERROR | E_PARSE);
    $count = 0;
    //echo $emp_id;
    $data = "";
    $sql = "select cat_id from tbl_categories where cat_code not in ('SMSC','CNC') order by sequence asc";

    error_log("SSSSSSS$$$$$$$$$" . $sql);

    $result = mysqli_query($conn,$sql);

    $med_cat = array();

    function procurement_details($item_id, $m, $y, $ohc)
    {

      $sql_procurement = "select ri.received_qty from received_issue_items ri right join received_master rm on ri.received_id = rm.received_id where ri.item_id = '$item_id' and month(rm.received_date ) = '$m' and year(rm.received_date )='$y' and ri.ohc_location_id='$ohc' ";


      error_log("procurement:" . $sql_procurement);

      $result_procurement = mysqli_query($GLOBALS['conn'],$sql_procurement);

      $procurement_qty_total = 0;

      while ($row_procurement = mysqli_fetch_assoc($result_procurement)) {

        $procurement_qty_total += $row_procurement['received_qty'];
      }

      return $procurement_qty_total;
    }

    function total($prev_balance, $new_received)
    {
      $total = $prev_balance + $new_received;

      return $total;
    }


    function previous_balance($item_id, $y, $ohc)
    {
      $prev_balance_sql = "select stock_qty from item_stock_dispensary_daily_balance where item_id='" . $item_id . "' and month(record_date) = '3' and year(record_date) = '$y' and day(record_date) = '31' and ohc_location_id='$ohc'";

      error_log('$prev_balance_sql:' . $prev_balance_sql);

      $result_prev_balance = mysqli_query($GLOBALS['conn'],$prev_balance_sql);
      $previous_balance = "";

      while ($row_prev_balance = mysqli_fetch_assoc($result_prev_balance)) {

        $previous_balance += $row_prev_balance['stock_qty'];
      }

      if ($previous_balance == 0) {
        return 0;
      } else {
        return $previous_balance;
      }
    }

    function issued_details($item_id, $a, $m, $y, $ohc)
    {

      $consume_detail = '';
      $issued_qty_total = 0;

      $sql_diect_issue_query = "SELECT ifnull(dd.issue_qty,0) as direct_issue FROM direct_medicine_issue_log_details dd LEFT JOIN direct_medicine_issue_log dm ON dd.issue_log_id = dm.issue_log_id WHERE item_id='$item_id' and month(dm.date_of_issue)='$m' and year(dm.date_of_issue)='$y' and ohc_location_id = '" . $ohc . "'";

      error_log("sql_diect_issue_query: " . $sql_diect_issue_query);

      $result_sql_diect_issue_query = mysqli_query($GLOBALS['conn'],$sql_diect_issue_query);

      while ($row_result_sql_diect_issue_query = mysqli_fetch_array($result_sql_diect_issue_query)) {

        $consume_detail .= 'DIRECT ISSUE ' . ':' . $row_result_sql_diect_issue_query['direct_issue'] . ' : ';
        $issued_qty_total += $row_result_sql_diect_issue_query['direct_issue'];
      }

      error_log("issued_qty_total:::" . $issued_qty_total);

      $sql_opd_consumables = "SELECT ifnull(c.issued_qty,0) as consumable_issue,e.ticket_no FROM opd_consumables c LEFT JOIN employee_appointment e ON c.consume_id = e.appointment_id where medicine = '$item_id' and month(e.appointment_date) = '$m' and year(e.appointment_date) = '$y' and ohc_type_id = '" . $ohc . "' ";

      error_log("sql_opd_consumables: " . $sql_opd_consumables);

      $result_sql_opd_consumables = mysqli_query($GLOBALS['conn'],$sql_opd_consumables);

      while ($row_result_sql_opd_consumables = mysqli_fetch_array($result_sql_opd_consumables)) {

        $consume_detail .= 'ID-' . $row_result_sql_opd_consumables['ticket_no'] . ':' . $row_result_sql_opd_consumables['consumable_issue'] . ' : ';
        $issued_qty_total += $row_result_sql_opd_consumables['consumable_issue'];
      }

      error_log("issued_qty_total_2:::" . $issued_qty_total);

      $sql_consume = "select a.appointment_id, (ifnull(a.issued_qty,0)+ifnull(c.issued_qty,0)) as total_issued,b.ticket_no from treatment a left join employee_appointment b on a.appointment_id = b.appointment_id left join detention_intake c on b.appointment_id = c.det_id  where item_id = '$item_id' and month(b.appointment_date) = '$m' and year(b.appointment_date) = '$y' and b.ohc_type_id = '" . $ohc . "' ";

      error_log("cccccccons" . $sql_consume);

      $result_consume = mysqli_query($GLOBALS['conn'],$sql_consume);


      while ($row_consume = mysqli_fetch_array($result_consume)) {
        $consume_detail .= 'ID-' . $row_consume['ticket_no'] . ':' . $row_consume['total_issued'] . ' : ';

        $issued_qty_total += $row_consume['total_issued'];

        error_log("detaiilllllllllll" . $row_consume['total_issued']);
      }

      error_log("issued_qty_total_3:::" . $issued_qty_total);



      if ($a == 1) {
        return $consume_detail;
      } else {
        return $issued_qty_total;
      }
    }


    function balance($total_balance, $total_consumed)
    {
      $b = $total_balance - $total_consumed;
      return $b;
    }

    while ($row_m = mysqli_fetch_array($result)) {

      array_push($med_cat, $row_m['cat_id']);
    }

    foreach ($med_cat as $i => $value) {

      $newCat = true;

      $query = "select i.item_id, i.item_name from tbl_items i where i.cat='$value' and i.status = '1'";

      error_log("###########QQQQQQQQ" . $query);

      $result = mysqli_query($conn,$query);

      $total = mysqli_num_rows($result);

      if ($total != 0) { ?>

        <!-- <tr rowspan="2">
          <th colspan='30' bgcolor="black;">
            <center><?= getTableFieldValue("tbl_categories", "cat_name", "cat_id", $value) ?></center>
          </th>
        </tr> -->
        <?

        while ($row = mysqli_fetch_assoc($result)) {

          $total_procurement = 0.00;
          $issued_total_per_year = 0.00;
          for ($j = 4; $j <= 12; $j++) {
            $total_procurement += procurement_details($row['item_id'], $j, $year, $ohc);
            $issued_total_per_year += issued_details($row['item_id'], 0, $j, $year, $ohc);
          }
          for ($j = 1; $j <= 3; $j++) {
            $total_procurement += procurement_details($row['item_id'], $j, $year2, $ohc);
            $issued_total_per_year += issued_details($row['item_id'], 0, $j, $year2, $ohc);
          }
          if ($issued_total_per_year == 0.00 &&  $total_procurement == 0.00 &&  $ZeroAllowed == "N") {
          } else {
        ?>
            <?php if ($newCat) {
              $newCat = false; ?>
              <tr rowspan="2">
                <th colspan='30' bgcolor="black;">
                  <center><?= getTableFieldValue("tbl_categories", "cat_name", "cat_id", $value) ?></center>
                </th>
              </tr>
            <?php } ?>
            <tr>
              <td align="left"><?= ++$count ?></td>
              <td align="left"><?= $row['item_name'] ?></td>

              <td align="left"><?= previous_balance($row['item_id'], $year, $ohc) ?></td>


              <!-- <td align="left"><?= $total_procurement ?></td> -->
              <? for ($j = 4; $j <= 12; $j++) { ?>
                <td align="left"><?= ceil(procurement_details($row['item_id'], $j, $year, $ohc)) ?></td>
              <? } ?>

              <? for ($j = 1; $j <= 3; $j++) { ?>
                <td align="left"><?= ceil(procurement_details($row['item_id'], $j, $year2, $ohc)) ?></td>
              <? } ?>
              <td align="left" bgcolor="#FFFF00"> <?= total(previous_balance($row['item_id'], $year, $ohc), $total_procurement) ?></td>

              <? for ($j = 4; $j <= 12; $j++) { ?>
                <td align="left"><?= ceil(issued_details($row['item_id'], 0, $j, $year, $ohc)) ?></td>
              <? } ?>

              <? for ($j = 1; $j <= 3; $j++) { ?>
                <td align="left"><?= ceil(issued_details($row['item_id'], 0, $j, $year2, $ohc)) ?></td>
              <? } ?>


              <td align="left" bgcolor="#C55A11"><?= $issued_total_per_year ?></td>

              <td align="left"> <?= ceil(balance(total(previous_balance($row['item_id'], $year, $ohc), $total_procurement), $issued_total_per_year)) ?></td>

            </tr>

    <?php }
        }
      }
    } ?>
  </table>

</body>