<?
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=daily_log_report_excel.xls");
header("Pragma: no-cache");
header("Expires: 0");


?>
<?php
$from_date1 = date('Y-m-d', strtotime($_POST['startDate1']));

error_log("from_date1:::" . $from_date1);
$rec_date =  date('Y-m-d', strtotime($from_date1 . ' - 1 days'));

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

$from_date = date('d-m-Y', strtotime($from_date1));
$ohc = $_SESSION['current_ohcttype'];

?>


<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>
  
  <table width="100%">
    <tr>

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

    </tr>
    <tr>
      <td align="left" style="font-size: 12px"><strong>For Date: <?php echo date("d-M-Y", strtotime($from_date)); ?></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="1%" align="center">Sr</th>
      <th align="center" width="5%">Items</th>
      <!-- <th align="center" width="2%">Fresh</th> -->
      <!-- <th align="center" width="2%">Previous</th> -->

      <!-- <th width="2%" align="center">Total</th> -->
      <!-- <th align="center" width="40%">Consumption Details</th> -->
      <th align="center" width="2%">Total Consumed</th>
      <th align="center" width="2%">Cost</th>
      <!-- <th align="center" width="2%">Balance</th> -->


    </tr>
    <?

    //echo $to_date;  


    // $emp_id=$_POST['empName'];

    $count = 0;
    //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);


    function item_cost($item_id,$from_date,$ohc,$total_consumed){
      $cost = 0;
    
      $sql_cost = "SELECT qty,net_value FROM procurement_items dd LEFT JOIN procurement dm ON dd.procurement_id = dm.procurement_id WHERE dd.item_id='$item_id' and date(dm.procurement_date)<=str_to_date('$from_date','%d-%m-%Y')  and dm.ohc_type_id = '$ohc' order by dm.procurement_id desc limit 1;";
    
      error_log ("sql_cost: " . $sql_cost);
    
      $result_sql_direct_issue_query = mysqli_query($GLOBALS['conn'], $sql_cost);
    
      $row_result_sql_direct_issue_query = mysqli_fetch_array($result_sql_direct_issue_query);
      $cost += $row_result_sql_direct_issue_query['net_value']/$row_result_sql_direct_issue_query['qty'];
    
    return round($cost*$total_consumed,2);
     }
    function consume_details($item_id, $from_date, $ohc)
    {
      $issued_qty_total = 0;

      $sql_direct_issue_query = "SELECT ifnull(sum(dd.issue_qty), 0) as direct_issue_total 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 date_format(date(dm.date_of_issue),'%d-%m-%Y')='$from_date' and ohc_location_id = '" . $ohc . "'";

      error_log("sql_direct_issue_query: " . $sql_direct_issue_query);

      $result_sql_direct_issue_query = mysqli_query($GLOBALS['conn'], $sql_direct_issue_query);

      $row_result_sql_direct_issue_query = mysqli_fetch_array($result_sql_direct_issue_query);
      $issued_qty_total += $row_result_sql_direct_issue_query['direct_issue_total'];

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

      $sql_opd_consumables = "SELECT ifnull(sum(c.issued_qty), 0) as consumable_issue FROM opd_consumables c LEFT JOIN employee_appointment e ON c.consume_id = e.appointment_id where medicine = '$item_id' and date_format(date(e.appointment_date),'%d-%m-%Y') = '$from_date' and ohc_type_id = '" . $ohc . "' ";

      error_log("sql_opd_consumables: " . $sql_opd_consumables);

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

      $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_detention_intake = "SELECT ifnull(sum(c.issued_qty), 0) as detention_issue FROM detention_intake c LEFT JOIN employee_appointment e ON c.det_id = e.appointment_id where c.medicine = '$item_id' and date_format(date(e.appointment_date),'%d-%m-%Y') = '$from_date' and ohc_type_id = '" . $ohc . "' ";

      error_log("sql_detention_intake: " . $sql_detention_intake);

      $result_sql_detention_intake = mysqli_query($GLOBALS['conn'], $sql_detention_intake);

      $row_result_sql_detention_intake = mysqli_fetch_array($result_sql_detention_intake);
      $issued_qty_total += $row_result_sql_detention_intake['detention_issue'];


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

      $sql_consume = "select ifnull(sum(a.issued_qty), 0) as total_issued from treatment a left join employee_appointment b on a.appointment_id = b.appointment_id where item_id = '$item_id' and date_format(date(b.appointment_date),'%d-%m-%Y') = '$from_date' and b.ohc_type_id = '" . $ohc . "' ";

      error_log("cccccccons" . $sql_consume);

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


      $row_consume = mysqli_fetch_array($result_consume);
      $issued_qty_total += $row_consume['total_issued'];

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

      return $issued_qty_total;
    }


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

    error_log("query to get data from tbl items " . $query);

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

    $total = mysqli_num_rows($result);



    if ($total != 0) { ?>

      <?

      while ($row = mysqli_fetch_assoc($result)) {
        $total_consumed = consume_details($row['item_id'], $from_date, $ohc);
        if ($total_consumed == '0' && $ZeroAllowed == "N") {
        } else {
      ?>
          <tr>
            <td align="left"><?= ++$count ?></td>
            <td align="left"><?= getItemWithFormName($row['item_id']) ?></td>
            <td align="left"><?= consume_details($row['item_id'], $from_date, $ohc) ?></td>
            <td align="left"><?= item_cost($row['item_id'],$from_date,$ohc,$total_consumed) ?></td>
       
          </tr>


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