<?php 


define( 'SF_ROOT_DIR', "../../../" );

define( 'SF_APP',      'sys' );


require_once( SF_ROOT_DIR.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'configGlobal.php' );

//CLASES INTERNAS
set_memory_max_time();

$IdCompania                 = 1;
$FechaDocumento1             = "2016-07-31";
$FechaDocumento2             = "2018-01-03";

$o_ALM_AlmacenMastFN = new ALM_AlmacenMastFN();
$a_o_ALM_AlmacenMastFN = $o_ALM_AlmacenMastFN->select();

echo "<pre>";
//print_r($a_o_ALM_AlmacenMastFN);
echo "</pre>";

//$s_Sql = "SELECT IdItem FROM  ALM_ItemMast where Estado = 'A' and IdItem = 'MEDI0000089';";
$s_Sql = "SELECT IdItem FROM  ALM_ItemMast where Estado = 'A' and IdItem like 'MEDI%';";

$rs = mysql_query( $s_Sql );
$cnt = mysql_num_rows( $rs );
if( $cnt > 0 )
{
    while ( $a_row = mysql_fetch_array( $rs, MYSQL_ASSOC ) ) {
    
    	$a_IdItem[] = $a_row['IdItem'];
    }
}

echo count($a_o_ALM_AlmacenMastFN).";;;;;entrrrrrrrrr";
foreach ($a_o_ALM_AlmacenMastFN as $key => $o_ALM_AlmacenMastFN) {
	$IdAlmacen = $o_ALM_AlmacenMastFN->getIdAlmacen();
	//echo $IdAlmacen."**";
	foreach ($a_IdItem as $key2 => $IdItem) {
		//echo "entro";
		$o_ALM_KardexFN = new ALM_KardexFN();
		$o_ALM_KardexFN->setIdCompania($IdCompania);
		$o_ALM_KardexFN->setIdItem($IdItem);
		$o_ALM_KardexFN->setIdAlmacen($IdAlmacen);
				
        $a_ResultSelect    = $o_ALM_KardexFN->select();
        echo "<pre>";
        //print_r($a_ResultSelect);
        echo "</pre>";
        $a_LstTransKardex = ALM_KardexFN::recuperarTransxRango( $IdCompania, $IdAlmacen, "", $IdItem, $FechaDocumento1, $FechaDocumento2 );
        //print_r($a_LstTransKardex);
        $i_SaldoInicia = 0;
        foreach ( $a_LstTransKardex as $o_ALM_KardexBN_Trx )
        {
            $i_Cantidad_Trx  = floatval( $o_ALM_KardexBN_Trx->getCantidad() );
            
            if( $o_ALM_KardexBN_Trx->getTipoOperacion() == "XX"){
            	echo $IdAlmacen."--------------".$IdItem."----------<br>";
            	echo "<pre>";
            	print_r($o_ALM_KardexBN_Trx);
            	echo "</pre>";
            	
            }
            if( $o_ALM_KardexBN_Trx->getTipoOperacion() == ALM_TransaccionCabeceraBN::TIPO_INGRESO )
            {
                $i_SaldoInicia = $i_SaldoInicia + $i_Cantidad_Trx;
            }
            else
            {
                $i_SaldoInicia = $i_SaldoInicia - $i_Cantidad_Trx;
            }
        }
        //$b_CtrlSaldoInicial = true;
        $i_Acumulado = $i_SaldoInicia;
        
        $a_Acumulado[$IdAlmacen][$IdItem] = $i_Acumulado;
	}
	
}

//print_r($a_Acumulado);
$cad = '';
foreach ($a_Acumulado as $keyAlmacen => $a_item){
	foreach ($a_item as $keyItem => $sumaKardex){
		
		$s_Sql = "SELECT sum(StockActual) as SumaStock
				FROM  `ALM_ItemAlmacenStock` 
				WHERE  `IdItem` LIKE  '".$keyItem."'
				AND  `IdAlmacen` LIKE  '".$keyAlmacen."'";
		echo $sql."<br>";
		$rs = mysql_query( $s_Sql );
		$cnt = mysql_num_rows( $rs );
		if( $cnt > 0 )
		{
		    while ( $a_row = mysql_fetch_array( $rs, MYSQL_ASSOC ) ) {
		    
		    	$SumaStock = $a_row['SumaStock'];
		    	//echo $SumaStock."-------------";
		    	if($SumaStock != $sumaKardex){
		    		$cad .= "Hay diferencia en ".$keyAlmacen."  ".$keyItem."  en Kardex: ".$sumaKardex."  en ItemStock: ".$SumaStock."<br> ";
		    		
		    	}
		    }
		}
	}
}
echo $cad;

if($cad) mail("msotomayor@dataexe.com","Alerta Pet center",$cad);
if($cad) mail("mascruz13@hotmail.com","Alerta Pet center",$cad);