<?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.general.daos.FacturacionDAO">
 
	<resultMap id="resultsTransaccionFacturacionGeneral" type="FacturaGeneralDTO">		
		<result property="idFactura"     column="ID_FACTURA" />		
		<result property="numeroFactura" column="NO_FACTURA" />
 		<result property="fechaFactura"  column="FECHA" /> 		
        <result property="monto"         column="MONTO" />
		<result property="sharepoint"    column="SHAREPOINT" />
 	    <result property="approved"      column="APPROVED" />  
		<result property="sap"           column="SAP" />
		<result property="pagado"        column="PAID" />
		<result property="adeudo"        column="DUE" />
		<result property="comentario"    column="COMENTARIO" />
		<result property="idEstudio"     column="ID_ESTUDIO" />
		<result property="indicadorIva"  column="IVA" />	
		<result property="posicion"      column="POSICION" />
		<result property="fechaSapI17"   column="FECHA_SAP_I17" />
		<result property="fechaSapI32"   column="FECHA_SAP_I32" />
		<result property="docMaterial"   column="DOC_MATERIAL" />
		<result property="fechaSapI14"   column="FECHA_SAP_I14" />
		<result property="facturaI14"    column="FACTURA_I14" />
		<result property="fechaSapI16"   column="FECHA_SAP_I16" />
		<result property="doctoSapI16"   column="DOCTO_SAP_I16" />
		<result property="tipoPago"      column="TIPO_PAGO" />
	</resultMap>

	<insert id="creaFacturaGeneral" parameterType="FacturaGeneralDTO" useGeneratedKeys='true'>
	
		<selectKey keyProperty="idFactura" resultType="long"
			order="BEFORE">
			select DDA_SQT_FACTURA.nextVal from DUAL
		</selectKey>

		INSERT INTO DDA_T_FACTURA  
		  (ID_FACTURA, NO_FACTURA, FECHA, MONTO, SHAREPOINT, APPROVED, SAP,
		    PAID, DUE, COMENTARIO, ID_ESTUDIO, IVA,
		      POSICION, FECHA_SAP_I17, FECHA_SAP_I32, DOC_MATERIAL, FECHA_SAP_I14, 
		        FACTURA_I14, FECHA_SAP_I16, DOCTO_SAP_I16, TIPO_PAGO) 
		     values(
		        #{idFactura, javaType=Long, jdbcType=NUMERIC},
		        #{numeroFactura, javaType=String, jdbcType=VARCHAR},
		        #{fechaFactura, javaType=Date, jdbcType=DATE},
		        #{monto, javaType=String, jdbcType=VARCHAR},
		        #{sharepoint, javaType=Integer, jdbcType=NUMERIC},
		        #{approved, javaType=Integer, jdbcType=NUMERIC},
		        #{sap, javaType=Integer, jdbcType=NUMERIC},
		        #{pagado, javaType=String, jdbcType=VARCHAR},
		        #{adeudo, javaType=String, jdbcType=VARCHAR},
		        #{comentario, javaType=String, jdbcType=VARCHAR}, 
		        #{idEstudio, javaType=Long, jdbcType=NUMERIC},        
		        #{indicadorIva, javaType=String, jdbcType=VARCHAR},
		        #{posicion, javaType=String, jdbcType=VARCHAR},
		        #{fechaSapI17, javaType=Date, jdbcType=DATE},
		        #{fechaSapI32, javaType=Date, jdbcType=DATE},
		        #{docMaterial, javaType=String, jdbcType=VARCHAR},
		        #{fechaSapI14, javaType=Date, jdbcType=DATE},
		        #{facturaI14, javaType=Date, jdbcType=DATE},
		        #{fechaSapI16, javaType=Date, jdbcType=DATE},
		        #{doctoSapI16, javaType=String, jdbcType=VARCHAR},
		        #{tipoPago, javaType=String, jdbcType=VARCHAR}
		      )  		        
	</insert>

	<update id="actualizaFacturaGeneral" parameterType="FacturaGeneralDTO">
		UPDATE DDA_T_FACTURA
		SET		   
		   NO_FACTURA =       #{numeroFactura, javaType=String, jdbcType=VARCHAR},
		   FECHA =            #{fechaFactura, javaType=Date, jdbcType=DATE},
		   MONTO =            #{monto, javaType=String, jdbcType=VARCHAR},
		   SHAREPOINT =       #{sharepoint, javaType=Integer, jdbcType=NUMERIC},		   
		   SAP =              #{sap, javaType=Integer, jdbcType=NUMERIC},
           PAID =	          #{pagado, javaType=String, jdbcType=VARCHAR},
		   DUE =              #{adeudo, javaType=String, jdbcType=VARCHAR},
		   COMENTARIO =       #{comentario, javaType=String, jdbcType=VARCHAR},  
		   ID_ESTUDIO =       #{idEstudio, javaType=Long, jdbcType=NUMERIC},
		   TIPO_PAGO =		  #{tipoPago, javaType=String, jdbcType=VARCHAR},
		   APPROVED =         #{approved, javaType=Integer, jdbcType=NUMERIC}	
		WHERE
		   ID_FACTURA =       #{idFactura, javaType=Long, jdbcType=NUMERIC}
	</update>
	
	<update id="actualizaFacturaGeneralNotificada" parameterType="FacturaGeneralDTO">
		UPDATE DDA_T_FACTURA
		SET		   
		   APPROVED =         #{approved, javaType=Integer, jdbcType=NUMERIC}		   		   
		WHERE
		   ID_FACTURA      = #{idFactura, javaType=Long, jdbcType=NUMERIC}
	</update>

	<select id="consultaFacturaGeneral" resultMap="resultsTransaccionFacturacionGeneral"
		parameterType="map">		        
        SELECT DISTINCT  
           f.ID_FACTURA, 
           f.NO_FACTURA, 
           f.FECHA, 
           f.MONTO, 
           f.SHAREPOINT, 
           f.APPROVED,
           f.SAP, 
           f.PAID, 
           f.DUE, 
           f.COMENTARIO,
           f.ID_ESTUDIO ,
           f.IVA,
           f.POSICION,
		   f.FECHA_SAP_I17,
		   f.FECHA_SAP_I32,
		   f.DOC_MATERIAL,
		   f.FECHA_SAP_I14,
		   f.FACTURA_I14,
		   f.FECHA_SAP_I16,
		   f.DOCTO_SAP_I16,
		   f.TIPO_PAGO      
        FROM DDA_T_FACTURA f          
        WHERE    
          f.ID_FACTURA = #{facturaGeneral.idFactura}
	</select>
	
	<delete id="eliminaFacturaGeneral" parameterType="FacturaGeneralDTO">
		DELETE DDA_T_FACTURA
		WHERE
		    ID_FACTURA = #{idFactura, javaType=Long, jdbcType=NUMERIC}
	</delete>

</mapper>