<?php
class GridMovResAction extends wtActions 
{
    public function execute()
    {
        //LayoutTemplate::getInstance()->setUseBootstrap();
        $s_IdItem = $this->getRequestParameter( 'IdItem' );
        $s_FechaIni = $this->getRequestParameter( 'FechaDocumento1' );
        $s_FechaFin = $this->getRequestParameter( 'FechaDocumento2' );

        $o_ALM_ItemMastFN = new ALM_ItemMastFN();
        $this->o_ALM_ItemMastBN = $o_ALM_ItemMastFN->selectByPK( $s_IdItem );

        $o_ALM_AlmacenMastFN = new ALM_AlmacenMastFN();
        $o_ALM_AlmacenMastFN->setIdCompania( myUser::getUser()->getIdCompania() );
        //$o_ALM_AlmacenMastFN->setAlmacenVentaFlag( "S" );
        $a_ALM_AlmacenMast = $o_ALM_AlmacenMastFN->select();

        $this->a_Almacenes = array();
        
        $s_FechaAnterior = UtilDate::restarDias( $s_FechaIni, 1 );

        foreach ( $a_ALM_AlmacenMast as $a_ALM_AlmacenMastBN ) 
        {
            $CantIni = 0;

            $b_Control = false;
            $o_ALM_ItemStockDetHisFN = new ALM_ItemStockDetHisFN();
            //$o_ALM_ItemStockDetHisFN->setDebug( true );
            $o_ALM_ItemStockDetHisFN->setIdItem( $s_IdItem );
            $o_ALM_ItemStockDetHisFN->setIdAlmacen( $a_ALM_AlmacenMastBN->getIdAlmacen() );
            $o_ALM_ItemStockDetHisFN->setIdFecha( $s_FechaAnterior );
            $o_ALM_ItemStockDetHisFN->addOrderBy( ALM_ItemStockDetHisFN::FechaCreacion, false );
            $a_ALM_ItemStockDetHis = $o_ALM_ItemStockDetHisFN->select( 1 );
            if( $a_ALM_ItemStockDetHis )
            {
                $CantIni = $a_ALM_ItemStockDetHis[0]->getStockFisico();
                $b_Control = true;
            }
            else
            {
                $o_ALM_ItemStockDetHisFN = new ALM_ItemStockDetHisFN();
                //$o_ALM_ItemStockDetHisFN->setDebug( true );
                $o_ALM_ItemStockDetHisFN->setIdItem( $s_IdItem );
                $o_ALM_ItemStockDetHisFN->setIdAlmacen( $a_ALM_AlmacenMastBN->getIdAlmacen() );
                $o_ALM_ItemStockDetHisFN->addCondition( ALM_ItemStockDetHisFN::IdFecha, "<", $s_FechaIni );
                $o_ALM_ItemStockDetHisFN->addOrderBy( ALM_ItemStockDetHisFN::FechaCreacion, false );
                $a_ALM_ItemStockDetHis = $o_ALM_ItemStockDetHisFN->select( 1 );
                if( $a_ALM_ItemStockDetHis )
                {
                    $s_FechaIni = UtilDate::sumarDias( $a_ALM_ItemStockDetHis[0]->getIdFecha(), 1 );
                    $CantIni = $a_ALM_ItemStockDetHis[0]->getStockFisico();
                    $b_Control = true;
                }
            }
            if( !$b_Control )
            {
                $o_ALM_TransaccionDetalleFN = new ALM_TransaccionDetalleFN();
                $o_ALM_TransaccionDetalleFN->setIdItem( $s_IdItem );
                $o_ALM_TransaccionDetalleFN->addOrderBy( ALM_TransaccionDetalleFN::FechaDocumentoCab );
                $a_ALM_TransaccionDetalle = $o_ALM_TransaccionDetalleFN->select( 1 );
                if( $a_ALM_TransaccionDetalle )
                {
                    $s_FechaIni = $a_ALM_TransaccionDetalle[0]->getFechaDocumentoCab();
                    $CantIni = 0;
                }
            }

            $this->a_Almacenes[$a_ALM_AlmacenMastBN->getIdAlmacen()] = array( "Desc" => $a_ALM_AlmacenMastBN->getDescripcionLocal(), "CantIni" => $CantIni, "CantAcu" => $CantIni );
        }

        $a_Param['IdItem']      = $s_IdItem;
        $a_Param['FechaInicio'] = $s_FechaIni;
        $a_Param['FechaFinal']  = $s_FechaFin;

        $a_DataItem = ALM_TransaccionDetalleFN::getDataMovimentosItem( $a_Param );
        $a_Result = array();
        foreach ( $a_DataItem as $i_Key => $a_Data ) 
        {
            if( $a_Data['IdCondicion'] == "I" )
            {
                $f_Cantidad = $a_Data['Cantidad'] * 1;
            }
            else
            {
                $f_Cantidad = $a_Data['Cantidad'] * -1;
            }
            $a_Data['CantidadAcu'] = $f_Cantidad;

            $a_Result[$i_Key] = $a_Data;
        }

        $this->a_DataItem = $a_Result;

        ViewResponse::getInstance()->setMethodAjax();
    }
}