<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 
$enquiry_id = utility::app_crypt($_REQUEST['enquiry_id'],'d');
if($_REQUEST['id'] != $enquiry_id){
	die("Invaild URL");
}

###########################################  STRAT FILTER CONDITIONS    #####################################

###########################################  	END  FILTER CONITIONS 	#####################################
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "work_order_assign";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Daily Negative Stocks ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
echo $query = "
SELECT 
    wo.id AS work_assign_id,
    wo.part_id,
    i.quantity_in_stock,
    SUM(wl.quantity_made) AS total_quantity_made,
    (SUM(wl.quantity_made) - i.quantity_in_stock) AS extra_needed,
    CONCAT('Negative Stock (Extra Needed: ', (SUM(wl.quantity_made) - i.quantity_in_stock), ')') AS stock_status
FROM work_logs wl
INNER JOIN work_order_assign wo ON wl.work_assign_id = wo.id 
INNER JOIN inventory i ON wo.part_id = i.item_id
WHERE DATE(wl.start_time) = CURDATE()  
GROUP BY wo.id, wo.part_id, i.quantity_in_stock
HAVING total_quantity_made > i.quantity_in_stock
";


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


#################################  END DELETE    #########################################
?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12">
				<div class="card">
					<!----------------------- Message ------------------------------->
					<? if(isset($_SESSION['info_message'])){ ?>
					  <div class="alert alert-info alert-dismissable">
						<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
						<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
					  </div>
					  <? } ?>
					<!----------------------- End Message ------------------------------->
					<div class="">
						<div class="card-body">
						<!-- search form -->
						<?php include_once('asign_management_search_options.inc.php');?>
						<!-- /.search form -->
						
							<form action="index?page=add" method="post" name="form1" id="form1">
							<div class="card-footer">
							   <?/* if(!$is_archive){ ?>
								<button type="button" name="archive_cause" id="archive_cause" class="btn btn-warning pull-right" style="float:right;">Archive</button>
								<? }else{ ?>
								<button type="button" name="unarchive_cause" id="unarchive_cause" class="btn btn-warning pull-right" style="float:right;">Unarchive</button>
								<? }*/ ?>
							</div>
							<? if($result>0){?>
							<div class="table-responsive">
							 <table id="example1" class="table table-bordered table-striped">
								  <thead>
									<tr>
									  <th>Part Number</th>
									  <th>Quantity In Stock</th>
									  <th class="">Quantity Made</th>
									  <th>Status</th>
									</tr>
								  </thead>
								  <tbody>
									<?php
									while ($row = mysqli_fetch_array($sql)) {
									  $row = utility::validate_crypt_data($row);
									  $partNumber = $gsi->getPartNumberById($row["part_id"]);
									  $engineerName = $gsi->getEmployeeById($row["engineer_id"]);
									  $status = $row['status']; // Add a 'status' field in your table if not present
									?>
									  <tr>
										<td><?= $partNumber ?></td>
										<td class=""><?= $row['quantity_in_stock'];?></td>
										<td class=""><?= $row['total_quantity_made'];?></td>
										<td><?= $row['stock_status'];?></td>
									  </tr>
									<?php } ?>
								  </tbody>
								</table>

							 </div>
							 <? }else{ ?>
								No item found...
							 <? } ?>
							<div class="box-footer clearfix">
								<!-- Paging starts here  -->
								<? 
								$next_page_condition .="&page=view";
								include("paging/paging_row.inc.php") 
								?>
								<!-- Paging ends starts here  -->
							</div>
						</div>
						
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<script type="text/javascript">
</script>