<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');
$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];
$page_title="Sales By Product/Service Summary";



?>


<!DOCTYPE html>
<html lang="en">


<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->

<?php
  do_calander('#fdate');
  do_calander('#tdate');

  ?>
  

<body>

  <!-- ======= Header ======= -->
 
  
  <?php 
  require_once('inc_files/inc_header.php');
  ?>
  
  <!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <?php  require_once('inc_files/inc_menu.php'); ?><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
		  <li class="breadcrumb-item"> Reports</li>    
          <li class="breadcrumb-item active"><?php echo $page_title?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <section class="section">
      <div class="row">
	  
	
		<div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
            <p class="card-title" style="font-size:14px; margin-bottom:0; padding-bottom:0;"><a href="61_reports_all.php?active_id=901&menu_id=9">< Back To Report List </a></p>

              <!-- No Labels Form -->
           <form class="row g-3" action="" method="post" name="codz" id="codz">
   		       
			
	<table width="80%" border="0" align="center">
      
      
      <tr height="50">
        <td align="right">Category:&nbsp;&nbsp;</td>
        <td colspan="3">
		 <?php
			 auto_complete_from_db_sql($conn,'SELECT concat(c.id," :: ",c.item_category), concat(c.id," :: ",c.item_category) FROM item_category c
			WHERE 1 order by c.id','category_type');
		?>
                  <input type="text" class="form-control" id="category_type" name="category_type" value="<?php echo $_REQUEST['category_type']?>" >		</td>
        <td rowspan="4" align="center" ><strong>
          &nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> Run Report&nbsp;&nbsp;</button>
        </strong></td>
      </tr>
      <tr height="50">
        <td align="right">Customer:&nbsp;&nbsp;</td>
        <td colspan="3">
				<?php 
				 auto_complete_from_db_sql($conn,'SELECT concat(a.ref_no," :: ",a.full_name), concat(a.ref_no," :: ",a.full_name) 
				 FROM accounts_reference a, reference_type t WHERE a.ref_type=t.id and a.ref_type=1','acc_reference'); 
				 ?>
                  <input type="text" class="form-control" id="acc_reference" name="acc_reference" value="<?php echo $_REQUEST['acc_reference']?>" >	
		</td>
      </tr>
	  
	  <tr height="50">
        <td align="right">Employee:&nbsp;&nbsp;</td>
        <td colspan="3">
			<?php
			 auto_complete_from_db_sql($conn,'SELECT concat(emp_code," :: ",emp_name), concat(emp_code," :: ",emp_name) FROM employee_info
			WHERE 1 order by emp_code','employee_name');
			?>
                  <input type="text" class="form-control" id="employee_name" name="employee_name" value="<?php echo $_REQUEST['employee_name']?>" >		</td>
      </tr>
	  
      
      <tr height="50">
        <td align="right">Date Interval:&nbsp;&nbsp;</td>
        <td width="156"><strong>
          <input type="text" name="fdate" id="fdate" class="form-control" style="width:150px;" value="<?php echo isset($_REQUEST['fdate'])?$_REQUEST['fdate']:date('Y-m-01');?>" />
        </strong></td>
        <td width="61" align="center" ><strong> -to- </strong></td>
        <td width="208" ><strong>
          <input type="text" name="tdate" id="tdate" class="form-control" style="width:150px;" value="<?php echo isset($_REQUEST['tdate'])?$_REQUEST['tdate']:date('Y-m-d');?>" />
        </strong></td>
      </tr>
    </table>
               
  </form><!-- End No Labels Form -->

            </div>
          </div>
		
		</div>
		
		
	  
	  
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			<br>
			
			
             
			
			 
             

              <!-- Table with stripped rows -->
              <table width="36%" class="table table-sm" style="font-size:12px;">
                <thead>
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#EAEAEA; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col">&nbsp;Type</th>
                    <th width="8%" scope="col">SKU</th>
                    <th width="23%" scope="col">Item Name</th>
                    <th width="7%" scope="col">UOM</th>
                    <th width="13%" scope="col"><div style="text-align:right">Quantity</div> </th>
                    <th width="11%" scope="col"><div style="text-align:right">Amount</div></th>
                    <th width="13%" scope="col"><div style="text-align:right">AVG Price</div></th>
                    <th width="15%" scope="col"><div style="text-align:right">% of Sales</div></th>
                  </tr>
                </thead>
                <tbody>
<?php
				
  $category_type = $_REQUEST['category_type'];
  $category=explode(' :: ',$category_type);
  $item_category=$category[0];
  
if($item_category!=''){
$category_con=" and i.item_category='".$item_category."'";
}

  $acc_reference = $_REQUEST['acc_reference'];
  $reference=explode(' :: ',$acc_reference);
  $ref_no=$reference[0];
  
  
   $employee_name = $_REQUEST['employee_name'];
   $employee=explode(' :: ',$employee_name);
   $emp_code=$employee[0];
  
  
if($ref_no!=''){
$ref_con=" and s.ref_no='".$ref_no."' ";
}

if($emp_code!=''){
$emp_con=" and s.emp_code='".$emp_code."' ";
}

if($_REQUEST['fdate']==''){
$_REQUEST['fdate']=date('Y-m-01');
}

if($_REQUEST['tdate']==''){
$_REQUEST['tdate']=date('Y-m-d');
}

//if($_REQUEST['fdate']!=''&&$_REQUEST['tdate']!='')
$date_con .= ' and m.jv_date between "'.$_REQUEST['fdate'].'" and "'.$_REQUEST['tdate'].'"';

$warehouse_id=$_SESSION['user']['depot'];

if($warehouse_id!=''){
$warehouse_con=" and j.warehouse_id='".$warehouse_id."'";
}

		 $sql = "select id, item_type from item_type where id group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $item_type[$info->id]=$info->item_type;
		 }
		 
		 $sql = 'select sum(s.total_amt) as total_sales_amt  from item_category c, item_info i, sales_invoice s 
		 where i.item_id=s.item_id and c.id=i.item_category and s.jv_date between "'.$_REQUEST['fdate'].'" and "'.$_REQUEST['tdate'].'"
		  '.$category_con.$ref_con.$emp_con.' ';
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $total_sales_amt=$info->total_sales_amt;
		 }

		 
		 $sql = "select s.item_id, sum(s.total_unit) as sales_qty, sum(s.total_amt) as sales_amt from sales_invoice s 
		 where s.jv_date between '".$_REQUEST['fdate']."' and '".$_REQUEST['tdate']."' ".$ref_con.$emp_con." group by s.item_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $sales_qty[$info->item_id]=$info->sales_qty;
		 $sales_amt[$info->item_id]=$info->sales_amt;
		 $avg_price[$info->item_id]=$sales_amt[$info->item_id]/$sales_qty[$info->item_id];
		 }
		 
		?>
	
				
	<?php
  $sql_grp = 'select c.id, c.item_category from item_category c, item_info i, sales_invoice s where i.item_id=s.item_id and c.id=i.item_category and
 s.jv_date between "'.$_REQUEST['fdate'].'" and "'.$_REQUEST['tdate'].'" '.$category_con.$ref_con.$emp_con.' group by i.item_category order by i.item_category';
	$query_grp = mysqli_query($conn,$sql_grp);
	while ($data_grp = mysqli_fetch_object($query_grp)) {
	?>		
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; background:#f6f9ff; font-weight:700; border-top: 1px solid #CCCCCC;">
                    <th width="10%" scope="col" >&nbsp;</th>
                    <th width="8%" scope="col" >&nbsp;</th>
                    <th width="23%" scope="col" ><?php echo $data_grp->item_category; ?> </th>
                    <th width="7%" scope="col" >&nbsp;</th>
                    <th width="13%" scope="col" ><div style="text-align:right">&nbsp;</div> </th>
                    <th width="11%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                    <th width="13%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                    <th width="15%" scope="col" >&nbsp;</th>
                  </tr>
               
		
				
				
					<?php

						$sql = 'select i.* from item_info i, sales_invoice s where i.item_id=s.item_id and
						s.jv_date between "'.$_REQUEST['fdate'].'" and "'.$_REQUEST['tdate'].'" and i.item_category="'.$data_grp->id.'" '.$ref_con.$emp_con.' group by s.item_id order by i.item_id';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td>&nbsp;<?php echo $item_type[$data->item_type];?></td>
                   <td><?php echo $data->sku_code;?></td>
                   <td>
				   <a href="61_sales_11.php?show=show&fdate=<?=$_REQUEST['fdate']?>&tdate=<?=$_REQUEST['tdate']?>&item_name=<?=$data->item_id." :: ".$data->item_name." >>> [Category: ".$data_grp->item_category."]"?>&acc_reference=<?=$_REQUEST['acc_reference'];?>&employee_name=<?=$_REQUEST['employee_name'];?>" target="_blank" style="text-decoration:none; ">
				   <?php echo $data->item_name;?></a></td>
                   <td><?php echo $data->unit_name;?></td>
                   <td align="right"><?php echo number_format($sales_qty[$data->item_id],2); $tot_sales_qty +=$sales_qty[$data->item_id];?></td>
                   <td align="right"><?php echo number_format($sales_amt[$data->item_id],2); $tot_sales_amt +=$sales_amt[$data->item_id];?></td>
                   <td align="right"><?php echo number_format($avg_price[$data->item_id],3); ?></td>
                   <td align="right"><?php echo number_format($percentage_of_sales=($sales_amt[$data->item_id]/$total_sales_amt)*100,2); ?>%</td>
                 </tr>
				  <?php }?>
	
                 <tr>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="center"><strong>SUB TOTAL</strong></td>
                   <td align="right">&nbsp;</td>
                   <td align="right"><strong><?php echo number_format($tot_sales_qty,2);?></strong></td>
                   <td align="right"><strong><?php echo number_format($tot_sales_amt,2); $net_tot_sales_amt+=$tot_sales_amt;?></strong></td>
                   <td align="right">&nbsp;</td>
                   <td align="right">&nbsp;</td>
                 </tr>
				 <?php
				 $tot_sales_qty=0;
				 $tot_sales_amt=0;
				
				  }?>
				  
				  <tr>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="center"><strong>NET TOTAL</strong></td>
                   <td align="right">&nbsp;</td>
                   <td align="right">&nbsp;</td>
                   <td align="right"><strong><?php echo number_format($net_tot_sales_amt,2);?></strong></td>
                   <td align="right">&nbsp;</td>
                   <td align="right">&nbsp;</td>
                 </tr>
                </tbody>
              </table>
			  
			
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

  </main><!-- End #main -->

  
 <!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= End Footer ======= -->

</body>

</html>