<?php
session_start();
$where = " WHERE 1=1 ";
$next_page_condition = "";
$paging = "";
if($_REQUEST['start']){
    $paging .="&start=".$_REQUEST['start'];
} 

###########################################  START FILTER CONDITIONS    #####################################

$from_date = isset($_REQUEST['from_date']) ? utility::setFormatedDate($_REQUEST['from_date']) : "";
$to_date = isset($_REQUEST['to_date']) ? utility::setFormatedDate($_REQUEST['to_date']) : "";
if($from_date && $to_date){
    $where .= " AND si.delivery_date BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59' ";
    $next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']." ";
}else if($from_date){
    $where.= " AND si.delivery_date >= '$from_date' ";
    $next_page_condition.="&from_date=".$_REQUEST['from_date'];
}else if($to_date){
    $where.= " AND si.delivery_date <= '$to_date' ";
    $next_page_condition.="&to_date=".$_REQUEST['to_date'];
}

// Add search filter for part number if needed
if(isset($_REQUEST['part_number']) && !empty($_REQUEST['part_number'])){
    $part_number = mysqli_real_escape_string($conn, $_REQUEST['part_number']);
    $where .= " AND sim.part_number LIKE '%$part_number%' ";
    $next_page_condition.="&part_number=".$_REQUEST['part_number'];
}

// Add search filter for customer order number if needed
if(isset($_REQUEST['customer_order_no']) && !empty($_REQUEST['customer_order_no'])){
    $customer_order_no = mysqli_real_escape_string($conn, $_REQUEST['customer_order_no']);
    $where .= " AND si.customer_order_no LIKE '%$customer_order_no%' ";
    $next_page_condition.="&customer_order_no=".$_REQUEST['customer_order_no'];
}

#################################  Export CSV    #########################################

###########################################  End Export  #####################################

$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "production_planning";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT 
    pp.*,
    im.part_number,
    im.product_description
FROM production_planning AS pp
LEFT JOIN item_management AS im
    ON pp.part_id = im.id
$where";
$page_header = "Sales Order Stock Report";
include("../admin/paging/paging_query.inc.php");

############################################ END PAGING WITH PER PAGE  #######################################

$query = "SELECT 
    pp.*,
    im.part_number,
    im.product_description
FROM production_planning AS pp
LEFT JOIN item_management AS im
    ON pp.part_id = im.id
$where LIMIT 50";
$sql3 = mysqli_query($conn, $query);
$result = mysqli_num_rows($sql3);

// Get total count for all deliveries
$count_query = "SELECT COUNT(DISTINCT si.id) as total_deliveries 
                FROM sales_invoices AS si
                INNER JOIN sales_invoice_mapping AS sim 
                  ON si.id = sim.sales_id
                $where";
$count_result = mysqli_query($conn, $count_query);
$total_row = mysqli_fetch_assoc($count_result);
$total_deliveries = $total_row['total_deliveries'];

?>

<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><? echo $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 ------------------------------->
                    <?php 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>
                        <?php echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
                    </div>
                    <?php } ?>
                    <!----------------------- End Message ------------------------------->
                    <div class="card">
                        <div class="card-body">
                            <!-- search form -->
                            <?php include_once('op_delivery_filter.php');?>
                            <!-- /.search form -->
                            <div class="mb-3">
                                Total Deliveries: <strong><?= $total_deliveries; ?></strong> | 
                                Displaying: <strong><?= $result; ?> records</strong>
                            </div>
                          <!--  <div style="float:right;margin-bottom:4px; color:black;">
                                Export Delivery Reports &nbsp;&nbsp;
                                <a href="index?page=view_delivery_reports&action=export_delivery_details<?= $next_page_condition; ?>">
                                    <i class="fa fa-download text-primary" style="font-size:20px;"></i>
                                </a>
                            </div>-->
                            <form action="index?page=add_order" method="post" name="form1" id="form1">
                                <div class="card-footer">
                                    <?php if(!$is_archive){ ?>
                                    <button type="button" name="archive_customer" id="archive_customer"
                                        class="hide btn btn-warning pull-right">Archive</button>
                                    <?php }else{ ?>
                                    <button type="button" name="unarchive_customer" id="unarchive_customer"
                                        class=" hide btn btn-warning pull-right">Unarchive</button>
                                    <?php } ?>
                                </div>
                                
                                <div class="table-responsive">
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th >Required Date</th>
												<th>Part Number </th>
												<th>Part Description </th>
												<th> Sales Order Number </th>
												<th> Current Stock  </th>
												<th> Qty Due  </th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        <?php 
                                        $counter = 1;
                                        while($row = mysqli_fetch_array($sql3)){ 
                                            $row = utility::validate_crypt_data($row);
											$counter++;
                                        ?>
                                        <tr>
										    <td><?= date('Y-m-d', strtotime($row['req_date'])); ?></td> 
											<td><?= htmlspecialchars($row['part_number']); ?></td>
											<td><?= htmlspecialchars($row['part_description']); ?></td>
                                            <td><?= htmlspecialchars($row['RefSalesCode']); ?></td>
                                            <td><?= htmlspecialchars($row['req_qty']); ?></td>
											<td><?= number_format($row['req_qty']); ?></td>
                                        </tr>
										
                                        <?php } ?>
                                        <?php if($result == 0){ ?>
                                        <tr>
                                            <td colspan="10" class="text-center">No delivery records found.</td>
                                        </tr>
                                        <?php } ?>
                                        </tbody>
                                    </table>
                                </div>
                                <div class="box-footer clearfix hide">
                                    <!-- Paging starts here  -->
                                   <span class="text-primary"><?php 
                                    $next_page_condition .="&page=view_delivery_reports";
                                    include("../admin/paging/paging_row.inc.php") 
                                ?></span>
                                    <!-- Paging ends starts here  -->
                                </div>
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
