<?
//include ('pdf_header_reverse.php');
include('includes/config/config.php');
include('includes/functions.php');

?>
<?php
$month = $_POST['month'];
$month_name = date("F", mktime(0, 0, 0, $month, 10));
$y = $_POST['year'];
$d = cal_days_in_month(CAL_GREGORIAN, $month, $y);
//$from_date = date('Y-m-d ', strtotime($from_date));
//echo $from_date;
$ohc = $_SESSION['current_ohcttype'];

$query_for_company = "select company_name, company_logo, image_type from company_profile ";
//echo $query_for_company;
$result_for_company = @mysqli_query($conn,$query_for_company);
if ($row_for_company = @mysqli_fetch_array($result_for_company)) {
  @extract($row_for_company);
}
?>
<link href="includes/css-js/admin.css" rel="stylesheet" type="text/css" />
<style>
  @page {
    margin: 15px;
  }

  .btn {
    background-color: #4CAF50;
    border-radius: 5%;
    /* Green */
    border: none;
    color: white;
    padding: 5px 8px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 12px;
    margin: 4px 2px;
    cursor: pointer;
  }

  @media print {
    #printPageButton {
      display: none;
    }
  }
</style>

<body>
  <?php include('pdf_ohc_header.php') ?>

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

      <td align="center" style="font-size: 15px"><strong>Daily Medicine Expense Report</strong></td>

    </tr>
    <tr>
      <td align="left" style="font-size: 12px"><strong>For Month: <?php echo $month_name; ?></strong></td>
      <td align="right" style="font-size: 15px"><button align="center" id="printPageButton" class="btn btn-success" onClick="window.print();">Print</button></td>
    </tr>
  </table>
  <br>

  <table width="100%" border="1" cellspacing="0">
    <tr>
      <th width="2%" align="center">Sr</th>
      <th align="center" width="7%">Date</th>
      <?phpfor ($i = 1; $i <= $d; $i++) { ?>


        <th width="2%" align="center"><?= $i ?></th>

      <?php } ?>
      <th align="center" width="5%">Total</th>



    </tr>
    <?

    //echo $to_date;  
    $count = 0;

    // $emp_id=$_POST['empName'];
    error_reporting(E_ERROR | E_PARSE);

    //echo $emp_id;
    $data = "";

    $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);

    $sql = "select cat_id from tbl_categories order by sequence asc";

    error_log("cat id selecting from tbl_categories" . $sql);

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

    $med_cat = array();


    function total($item_id, $m, $y, $ohc)
    {
      $issued_qty_total = 0;

      $sql_diect_issue_query = "SELECT ifnull(sum(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 . "' group by item_id";

      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)) {
        $issued_qty_total += $row_result_sql_diect_issue_query['direct_issue'];
      }

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

      $sql_opd_consumables = "SELECT ifnull(sum(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 . "' group by item_id ";

      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)) {
        $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(sum(a.issued_qty),0)+ifnull(sum(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 . "' group by item_id ";

      error_log("consume::" . $sql_consume);

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


      while ($row_consume = mysqli_fetch_array($result_consume)) {

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

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

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

      return $issued_qty_total;
    }


    function consume_details($item_id, $d, $m, $y, $ohc)
    {
      $issued_qty_total = 0;

      $sql_diect_issue_query = "SELECT ifnull(sum(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 day(dm.date_of_issue) = '$d' and month(dm.date_of_issue)='$m' and year(dm.date_of_issue)='$y' and ohc_location_id = '" . $ohc . "' group by item_id";

      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)) {
        $issued_qty_total += $row_result_sql_diect_issue_query['direct_issue'];
      }

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

      $sql_opd_consumables = "SELECT ifnull(sum(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 day(e.appointment_date)='$d' and month(e.appointment_date) = '$m' and year(e.appointment_date) = '$y' and ohc_type_id = '" . $ohc . "' group by item_id ";

      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)) {
        $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(sum(a.issued_qty),0)+ifnull(sum(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 day(b.appointment_date)='$d' and month(b.appointment_date) = '$m' and year(b.appointment_date) = '$y' and b.ohc_type_id = '" . $ohc . "' group by item_id ";

      error_log("consume::" . $sql_consume);

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


      while ($row_consume = mysqli_fetch_array($result_consume)) {

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

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

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

      return $issued_qty_total;
    }


    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 a.item_id,c.item_name from item_stock a,  tbl_items c  where a.item_id = c.item_id and  c.cat = '$value' and c.status = '1' group by a.item_id order by a.item_id asc ";

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

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

      $total = mysqli_num_rows($result);

      if ($total != 0) { ?>



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

        <?

        while ($row = mysqli_fetch_assoc($result)) {
          $total_consumed =  ceil(total($row['item_id'], $month, $y, $ohc));
          if ($total_consumed == '0' && $ZeroAllowed == "N") {
          } else {

        ?>
            <?php if ($newCat) {
              $newCat = false; ?>
              <tr rowspan="2">
                <th colspan='34'>
                  <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>
              <?phpfor ($i = 1; $i <= $d; $i++) { ?>
                <td align="left"><?= ceil(consume_details($row['item_id'], $i, $month, $y, $ohc)) ?></td>
              <?php} ?>
              <td align="left"><?= ceil(total($row['item_id'], $month, $y, $ohc)) ?></td>

            </tr>

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

<script>
  $(document).ready(function() {
    window.print();

  });
</script>