<?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 Customer Details";



?>


<!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="5" 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">Product:&nbsp;&nbsp;</td>
        <td colspan="3">
		<?php
			
					auto_complete_from_db_sql($conn,'SELECT concat(i.item_id," :: ",i.item_name," >>> [Category: ",c.item_category,"]"), concat(i.item_id," :: ",i.item_name," >>> [Category: ",c.item_category,"]") FROM item_info i, item_category c 
					 WHERE i.item_category=c.id','item_name');
				
					?>
					
					
					<input type="text" class="form-control" name="item_name" id="item_name" autocomplete="off" value="<?php echo $_REQUEST['item_name']?>"  >
		</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="9%" scope="col">&nbsp;Date</th>
                    <th width="10%" scope="col">Invoice</th>
                    <th width="21%" scope="col">Customer</th>
                    <th width="19%" scope="col">Item Name</th>
                    <th width="6%" scope="col">UOM</th>
                    <th width="13%" scope="col"><div style="text-align:right">Quantity</div> </th>
                    <th width="10%" scope="col"><div style="text-align:right">Sales Price</div></th>
                    <th width="12%" scope="col"><div style="text-align:right">Amount</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."'";
}

$item_name = $_REQUEST['item_name'];
  $item=explode(' :: ',$item_name);
   $item_id=$item[0];
  
if($item_id!=''){
$item_con=" and i.item_id='".$item_id."' ";
}


  $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 ref_no, full_name from accounts_reference where 1 group by ref_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $full_name[$info->ref_no]=$info->full_name;
		}
		
		$sql = "select jv_no, view_jv_no from journal_master where 1 group by jv_no";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $view_jv_no[$info->jv_no]=$info->view_jv_no;
		}
 
		?>
	
				
	<?php
  $sql_grp = 'select s.ref_no, r.full_name  from item_category c, item_info i, sales_invoice s, accounts_reference r
 where  c.id=i.item_category and i.item_id=s.item_id and s.ref_no=r.ref_no and s.jv_date between "'.$_REQUEST['fdate'].'" and "'.$_REQUEST['tdate'].'" 
 '.$category_con.$ref_con.$item_con.$emp_con.' group by s.ref_no order by i.item_category,i.item_id';
	$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="9%" scope="col" >&nbsp;</th>
                    <th width="10%" scope="col" >&nbsp;</th>
                    <th width="21%" scope="col" ><?php echo $data_grp->full_name; ?> </th>
                    <th width="19%" scope="col" >&nbsp;</th>
                    <th width="6%" scope="col" >&nbsp;</th>
                    <th width="13%" scope="col" ><div style="text-align:right">&nbsp;</div> </th>
                    <th width="10%" scope="col" >&nbsp;</th>
                    <th width="12%" scope="col" ><div style="text-align:right">&nbsp;</div></th>
                  </tr>
               
		
				
				
					<?php

						$sql = 'select s.*, i.sku_code, i.item_name, i.item_type, s.ref_no, r.full_name from item_info i, sales_invoice s, accounts_reference r  
						where i.item_id=s.item_id and s.ref_no=r.ref_no and s.ref_no="'.$data_grp->ref_no.'" and 
						s.jv_date between "'.$_REQUEST['fdate'].'" and "'.$_REQUEST['tdate'].'" '.$category_con.$ref_con.$item_con.$emp_con.' order by s.jv_date,s.item_id';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
		
                 
                 <tr>
                   <td>&nbsp;<?php echo date('d-M-y',strtotime($data->jv_date));?></td>
                   <td><a href="view_file/sales_invoice_print_view.php?jv_no=<?php echo $data->jv_no;?>" target="_blank"><?php echo $view_jv_no[$data->jv_no];?></a></td>
                   <td><?php echo $full_name[$data->ref_no];?></td>
                   <td> <?php echo $data->item_name;?></td>
                   <td><?php echo $data->unit_name;?></td>
                   <td align="right"><?php echo number_format($data->total_unit,2); $tot_sales_qty +=$data->total_unit;?></td>
                   <td align="right"><?php echo number_format($data->unit_price,2);?></td>
                   <td align="right"><?php echo number_format($data->total_amt,2); $tot_sales_amt +=$data->total_amt;?></td>
                  </tr>
				  <?php }?>
	
                 <tr>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="center">&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">&nbsp;</td>
                   <td align="right"><strong><?php echo number_format($tot_sales_amt,2);  $net_tot_sales_amt+=$tot_sales_amt;?></strong></td>
                  </tr>
				 <?php
				 $tot_sales_qty=0;
				 $tot_sales_amt=0;
				
				  }?>
				  <tr>
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td align="center">&nbsp;</td>
                   <td align="center"><strong>NET TOTAL</strong></td>
                   <td align="right">&nbsp;</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>
                  </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>