<%@ page import="com.fenalco.entities.SuperOffer"%>
<%@ page import="com.fenalco.entities.SupplierProduct"%>
<%@ page import="com.fenalco.entities.User"%>
<%@ page import="com.fenalco.entities.DataAccess"%>
<%@ page import="com.fenalco.entities.PaymentMethodList"%>
<%@ page import="com.fenalco.entities.PaymentMethod"%>

<%@ page import="java.util.Vector"%>


<%
String userName = ( (User)request.getSession().getAttribute("user") ).getFirstName();
SuperOffer view = (SuperOffer)request.getAttribute("view");
SupplierProduct product = new SupplierProduct(view.getProductId());
PaymentMethodList paymentMethodList  = (PaymentMethodList)request.getAttribute("suppliersPayments");
int quantity = ((Integer) request.getAttribute("quantity")).intValue();
int paymentId = ((Integer) request.getAttribute("paymentId")).intValue();


double sub_aux = ((Double) request.getAttribute("total")).doubleValue();

double subtotal = 0.0;
double base = 0.0;
double iva = 0.0;
double total = 0.0;
double iva_por = product.getTax();
double discount = ((Double) request.getAttribute("discount")).doubleValue();
                                                    

subtotal = sub_aux/(1+(iva_por/100));

if( iva_por > 0 )
{
        base = subtotal;
}

iva = subtotal * (iva_por) / 100;
    


discount = subtotal * discount /100;

total = subtotal + iva - discount; 



%>

<html>
<head>
    <link rel='stylesheet' style='text/css' href='../css/tablas.css'>
    <title>Comprar Super Oferta</title>
</head>
<body>
<script>
    function paintMessage(message){
        document.getElementById('temp').innerHTML="<font color='#FF0000'><b>"+message+"</b></font>";
        return  true;
    }
    
    function updateDiscount(){
		var message = <%="\""+userName+"\""%>+",su solicitud está siendo procesada, espere un momento por favor......";
        document.forms[0].operationType.value="confirm";
		paintMessage(message);
        document.forms[0].submit();
    }
	
	function buySuperOffer(){
		var message = <%="\""+userName+"\""%>+", su superoferta esta siendo enviada, espere un momento por favor......";
		document.getElementById('enviar').disabled = true;
		document.getElementById('regresar').disabled = true;
		document.forms[0].operationType.value = "buy"
		paintMessage(message);
		document.forms[0].submit();
	}
	
	function submitForm(){
		
		//paintMessage()
		document.forms[0].submit();

	}
</script>
<%-- <jsp:useBean id="beanInstanceName" scope="session" class="beanPackage.BeanClassName" /> --%>
<%-- <jsp:getProperty name="beanInstanceName"  property="propertyName" /> --%>
   
<table width='280' align='center'>
  <tr> 
    <td colspan='2'> 
      <%
            String tittle = "Resumen de la compra";
        %>
      <%@ include file="/titulo.jsp" %>
    </td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td> <strong>Superoferta </strong></td>
    <td> <%=view.getProductName()%> </td>
  </tr>
  <tr> 
    <td> <strong>Cantidad</strong> </td>
    <td> <%=quantity%> </td>
  </tr>
  <tr> 
    <td> <strong>Forma de pago/Descuento </strong></td>
    <td> <form name='superOffer' action='/minorista/comprarsuperoferta' method='post' >
        <input type='hidden' value ='<%=view.getId()%>' name='id' >
        <input type='hidden' name='operationType' >
        <input type='hidden' value ='<%=quantity%>' name='quantity' >
        <select name='payment' onChange="updateDiscount()">
          <%
                                            Vector paymentList = paymentMethodList.getPaymentMethods();
                                            for( int i = 0; i < paymentList.size(); i++ )
                                            {
                                                PaymentMethod paymentMethod = ( PaymentMethod )paymentList.elementAt( i );
                                                %>
          <option value='<%=paymentMethod.getId( )%>'  <%=paymentMethod.getId( )==paymentId?"selected":""%>><%=paymentMethod.getName()+"("+paymentMethod.getDiscount()+"%)"%></option>
          <%
                                            }
                                        %>
        </select>
      </form></td>
  </tr>
  <tr> 
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td colspan="2"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#E8E8F4">
        <tr> 
          <td width="16" align="left" valign="top"><img src="images/bus1.gif" width="16" height="14"></td>
          <td width="529">&nbsp;</td>
          <td width="15" align="right" valign="top"><img src="images/bus3.gif" width="15" height="14"></td>
        </tr>
        <tr> 
          <td>&nbsp;</td>
          <td><table width="100%"  border="0" cellspacing="0" cellpadding="3">
              <tr> 
                <td><strong>TOTAL SIN I.V.A</strong></td>
                <td align="right"><strong><font color="FF0000"><%=DataAccess.getCurrency( subtotal )%></font><font color='#FF0000'></font></strong></td>
                <td>&nbsp;</td>
                <td><strong>BASE DEL I.V.A GRAVADO</strong></td>
                <td><strong><font color="FF0000"><%=DataAccess.getCurrency( base)%></font><font color='#FF0000'></font></strong></td>
              </tr>
              <tr> 
                <td width="33%">Valor del I.V.A (+)</td>
                <td width="24%" align="right"><strong><font color="FF0000"><%=DataAccess.getCurrency( iva)%></font><font color='#FF0000'></font></strong></td>
                <td width="1%">&nbsp;</td>
                <td width="29%">&nbsp; </td>
                <td width="13%"><span class="style4"></span></td>
              </tr>
              <tr> 
                <td height="5">Descuento(-)</td>
                <td height="5" align="right"><strong><font color="FF0000"><%=DataAccess.getCurrency( discount )%></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr> 
                <td colspan="2" height="5"><hr size="2"></td>
                <td></td>
                <td></td>
                <td></td>
              </tr>
              <tr> 
                <td><strong>TOTAL</strong></td>
                <td align="right"><strong><font color="FF0000"><%=DataAccess.getCurrency( total )%></font><font color='#FF0000'></font></strong></td>
                <td>&nbsp;</td>
                <td>&nbsp; </td>
                <td>&nbsp;</td>
              </tr>
            </table></td>
          <td>&nbsp;</td>
        </tr>
        <tr> 
          <td align="left" valign="bottom"><img src="images/bus2.gif" width="16" height="15"></td>
          <td>&nbsp;</td>
          <td align="right" valign="bottom"><img src="images/bus4.gif" width="15" height="15"></td>
        </tr>
      </table></td>
  </tr>
  <tr> 
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr> 
    <td colspan='2'> <b><%=userName %>, para enviar su orden por favor seleccione 
      <input name="submit" type='button' onClick="buySuperOffer()" value='Comprar' id='enviar'></td>
  </tr>
  <tr> 
    <td colspan='2'><hr></td>
  </tr>
  <tr> 
    <td> <input type='button' value='Regresar' onClick="submitForm()" id='regresar'> </td>
    <td>&nbsp; </td>
  </tr>
  <tr> 
    <td colspan='2'> <div id='temp' ></div></td>
  </tr>
</table>

</body>
</html>
// “Copyright (C) <2008> Multilateral Investment Fund of the Inter-American Development Bank.
// Este archivo es parte de <ICT4BUS-PVL>
// < ICT4BUS-PVL > es software libre: usted puede redistribuirlo y/o modificarlo bajo los términos de la Licencia Pública General GNU publicada por la Fundación para el Software Libre, ya sea la versión 3 de la Licencia, o (a su elección) cualquier versión posterior.
// < ICT4BUS-PVL > se distribuye con la esperanza de que sea útil, pero SIN GARANTÍA ALGUNA; ni siquiera la garantía implícita MERCANTIL o de APTITUD PARA UN PROPÓSITO DETERMINADO. 
// Consulte los detalles de la Licencia Pública General GNU para obtener una información más detallada. 
// Debería haber recibido una copia de la Licencia Pública General GNU junto a este programa. En caso contrario, consulte http://www.gnu.org/licenses/.”