<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.mx.dla.dda.factura.listado.daos.FacturaListadoDAO">
				
	  <resultMap type='FacturaListadoDTO' id='facturaListadoResult'>	    	    
	    <result property='numero'     column='NO_FACTURA'/>
	    <result property='estudio'    column='ESTUDIO'/>
	    <result property='fecha'      column='FECHA'/>	    	    
	    <result property='approved'   column='APPROVED'/>
	    <result property='sap'        column='SAP'/>
	    <result property='amount'     column='MONTO'/>
	    <result property='idFactura'  column='ID_FACTURA'/>	  
	    <result property='facturaSap'     column='FACTURA_I14'/>
	    <result property='documentoSap'  column='DOCTO_SAP_I16'/>	  
	  </resultMap>
 
  	 <select id='getListadoFacturas' resultMap='facturaListadoResult' parameterType="map">
   		 
     		SELECT ID_FACTURA, NO_FACTURA, FECHA, MONTO, APPROVED, SAP, DDA_C_ESTUDIO.DESC_ESTUDIO AS ESTUDIO, FACTURA_I14, DOCTO_SAP_I16
				FROM  DDA_T_FACTURA
					  INNER JOIN DDA_C_ESTUDIO ON DDA_T_FACTURA.ID_ESTUDIO = DDA_C_ESTUDIO.ID_ESTUDIO				
				WHERE
				    <if test="numeroFact != null">
				        DDA_T_FACTURA.NO_FACTURA LIKE #{numeroFact}
				    </if>
				    
				    <if test="numeroFact != null and estudios != null">
				        AND
				    </if>
				    
				    <if test="estudios != null">
					   DDA_T_FACTURA.ID_ESTUDIO IN 
					    <foreach item="id" index="index" collection="estudios" open="(" separator="," close=")">
		           		  #{id}
		        		</foreach>
	        	    </if>	           			           
	        	 ORDER BY DDA_T_FACTURA.FECHA DESC			       		     		  			       		   		
	  </select>
	  	 
	  <delete id = "eliminarListadoFacturaDetalle" parameterType = "long">
	        DELETE FROM DDA_T_FACTURA_DETALLE WHERE DDA_T_FACTURA_DETALLE.ID_FACTURA = #{idFactura} 	        	                    
      </delete>
      
	  <delete id = "eliminarListadoFactura" parameterType = "long">	         	        	       
            DELETE FROM DDA_T_FACTURA         WHERE DDA_T_FACTURA.ID_FACTURA = #{idFactura}
      </delete>
	  
</mapper>