<?php 
ob_start();
session_start();
include ("../_init.php");

// Redirect, If user is not logged in
if (!is_loggedin()) {
  redirect(root_url() . '/index.php?redirect_to=' . url());
}

// Redirect, If User has not Read Permission
if (user_group_id() != 1 && !has_permission('access', 'read_quotation')) {
  redirect(root_url() . '/'.ADMINDIRNAME.'/dashboard.php');
}

// Set Document Title
$document->setTitle(trans('title_quotation'));

// Add Script
$document->addScript('../assets/itsolution24/angular/modals/QuotationViewModal.js');
$document->addScript('../assets/itsolution24/angular/controllers/QuotationController.js');

// Include Header and Footer
include("header.php"); 
include ("left_sidebar.php") ;
?>

<?php 
include "conexion_a.php";
if (!empty($_POST)) {
    $alert = "";
    if (empty($_POST['id_c'])) {
        $alert = '<div class="alert alert-danger" role="alert">Todo los campos son requeridos</div>';
    } else {
        $item = $_POST['id_c'];
		//$estado = 'active';
            $sql_update = mysqli_query($conexion, "UPDATE purchase_item SET status = 'active' WHERE id = $item ");

            if ($sql_update) {
                $alert = '<div class="alert alert-success" role="alert">Se actualizo correctamente el esatdo</div>';
            } else {
                $alert = '<div class="alert alert-danger" role="alert">Error al Actualizar </div>';
            }
    }
}

?>

<!-- Content Wrapper Start -->
<div class="content-wrapper" ng-controller="QuotationController">

  <!-- Content Header Start -->
  <section class="content-header">
    <?php include ("../_inc/template/partials/apply_filter.php"); ?>
    <h1>
      <?php echo trans('VENTAS'); ?>
      <small>
        <?php echo store('name'); ?>
      </small>
    </h1>
    <ol class="breadcrumb">
      <li>
        <a href="dashboard.php">
          <i class="fa fa-dashboard"></i> 
          <?php echo trans('text_dashboard'); ?>
        </a>
      </li>
      <li>
        <?php if (isset($request->get['box_state']) && $request->get['box_state']=='open'): ?>
          <a href="quotation.php"><?php echo trans('text_quotation_title'); ?></a>  
        <?php else: ?>
          <?php echo trans('Ventas'); ?>  
        <?php endif; ?>
      </li>
      <?php if (isset($request->get['box_state']) && $request->get['box_state']=='open'): ?>
        <li class="active">
          <?php echo trans('text_add'); ?> 
        </li>
      <?php endif; ?>
    </ol>
  </section>
  <!-- Content Header end -->

  <!-- Content Start -->
  <section class="content">

    <?php if(DEMO) : ?>
    <div class="box">
      <div class="box-body">
        <div class="alert alert-info mb-0">
          <p><span class="fa fa-fw fa-info-circle"></span> <?php echo $demo_text; ?></p>
        </div>
      </div>
    </div>
    <?php endif; ?>
	
	
				
							  
	                    <div class="tab-pane" id="quotation" >
                    		<div class="box-header">
					            <h3 class="box-title">
					              <?php echo trans('Detalle de ventas de productos'); ?>  
					            </h3>
					        </div>
					        <div class="box-body">
					            <div class="table-responsive">  
					            	<?php 
									
									
									
									
									
									echo trans('DETALLLE');
									//include '../admin/actualizacion.php'; ?>
									
<table width="100%" height="1" border="1" cellspacing="1" bordercolor="#999999" >
<thead>
<tr width="100%" class="bg-blue">
</tr>
</thead>
<tbody>
 <?php
include "conexion_a.php";
			
			
$id_producto = $request->get['id_producto'];

 
$total = 0;
//pIt.item_id = $id_producto AND

            $query = mysqli_query($conexion, "SELECT *  FROM purchase_item pIt WHERE item_id = $id_producto ");
            $result = mysqli_num_rows($query);
            if ($result > 0) {
                while ($data = mysqli_fetch_assoc($query)) {
				
				
                   $total += $data['item_quantity'];
            ?>
                    
            <?php }
            } ?>
			
			  <tfoot>
			  <tr width="100%" class="bg-green">
      
		
            <td>Total Compras:</td><!-- Dejamos una columna vac�a para que coincida con el n�mero de columnas de la tabla -->
            <td><?php echo $total; ?></td>
        </tr>
    </tfoot>
        </tbody>
		
  </table>
  
  <table width="100%" height="1" border="1" cellspacing="1" bordercolor="#999999" >
								    <thead>
            <tr width="100%" class="bg-blue">
			     
            </tr>
        </thead>
        <tbody>
		
		
            <?php
       
			
			


 
$total_ventas = 0;
//pIt.item_id = $id_producto AND

            $query = mysqli_query($conexion, "SELECT *  FROM selling_item  WHERE item_id = $id_producto ");
            $result = mysqli_num_rows($query);
            if ($result > 0) {
                while ($data = mysqli_fetch_assoc($query)) {
				
				
                   $total_ventas += $data['item_quantity'];
            ?>
                   
            <?php }
            } ?>
			
			  <tfoot>
			  <tr width="100%" class="bg-green">
      
		
            <td>Total Ventas:</td><!-- Dejamos una columna vac�a para que coincida con el n�mero de columnas de la tabla -->
            <td><?php echo $total_ventas; ?></td>
        </tr>
    </tfoot>
        </tbody>
		
  </table>
  <table width="100%" height="1" border="1" cellspacing="1" bordercolor="#999999" >
								    <thead>
            <tr width="100%" class="bg-blue">
			     
            </tr>
        </thead>
        <tbody>
		
		
            <?php
          

 $saldo = 0;
$total_return = 0;
//pIt.item_id = $id_producto AND

            $query = mysqli_query($conexion, "SELECT *  FROM return_items  WHERE item_id = $id_producto ");
            $result = mysqli_num_rows($query);
            if ($result > 0) {
                while ($data = mysqli_fetch_assoc($query)) {
				
				
                   $total_return += $data['item_quantity'];
            ?>
                 
            <?php }
            } ?>
			
			  <tfoot>
			  <tr width="100%" class="bg-green">
      
		
            <td>Total Devoluciones:</td><!-- Dejamos una columna vac�a para que coincida con el n�mero de columnas de la tabla -->
            <td><?php echo $total_return; ?></td>
			
			</tr>
    </tfoot>
        </tbody>
		
  </table>
    <table width="100%" height="1" border="1" cellspacing="1" bordercolor="#999999" >
								    <thead>
            <tr width="100%" class="bg-blue">
	<td>
	<?php	


            $query = mysqli_query($conexion, "SELECT *  FROM product_to_store  WHERE product_id = $id_producto ");
            $result = mysqli_num_rows($query);
            if ($result > 0) {
                while ($data = mysqli_fetch_assoc($query)) {
                   $stock = $data['quantity_in_stock'];
            ?>
                    <tr>
					
                    </tr>
            <?php }
            } ?></td>
			<td>Saldo:</td>
			<td><?php $saldo = ($total - $total_ventas) + $total_return; 
			echo $saldo; ?></td>
			 <tr>
			 <td>STOCK:</td>
					<td><?php 	echo $stock; ?></td>
					</tr>
					 <tr>
					<td>DIFERENCIA:</td>
					<td><?php $diferencia = $saldo - $stock;	echo $diferencia; ?></td>
                    </tr>
			  </table>
					            </div>
					        </div>
	                    </div>
		              
						
						
						
    
    
       <!-- Quotation List Section End-->
    </div>
  </section>
  <!-- Content End -->
</div>
<!-- Content Wrapper End -->


<?php include ("footer.php"); ?>