<?
session_start();
require('../valtiempoinact.php');
require("../conectar.php");	
?>
<html>
<head><title>Registro proveedor</title>
<link rel="stylesheet" href="requisiciones/css/estilos.css" type="text/css">
<script type="text/javascript">
/*window.onload = function(){
		 new JsDatePick({
			useMode:2,
			target:"fi",
			dateFormat:"%Y-%m-%d"
		    });
			new JsDatePick({
			useMode:2,
			target:"ff",
			dateFormat:"%Y-%m-%d"
		    });
	    };*/
n=0;
function proveedor(id,op){
     n++;
     cont=document.getElementById(id);
	 tr=document.createElement('tr');
	 tr.id="archivo"+id+n;
	 tr.setAttribute("class","tr");
	
	 //td1 = document.createElement('td'); // primer td
	 td2 = document.createElement('td'); // segundo td
	 td3 = document.createElement('td'); // tercer td
	 td4 = document.createElement('td'); // cuarto td
	 //td5 = document.createElement('td');
	 td6 = document.createElement('td');
	 //td7 = document.createElement('td');
	 //ele1 = document.createElement('input'); // 5
	 //ele2 = document.createElement('input'); // 5
     ele3 = document.createElement('input'); // 5
     ele4 = document.createElement('input');
	 //ele5 = document.createElement('input');
	 //ele6 = document.createElement('input');
	 
	 //primera celda
	 //td1.innerHTML=id;
	 //tr.appendChild(td1);
	 //termina primera celda
	 
	 //tercera celda
	 //td2.setAttribute("align","center");
	 //ele2.name=id+"[]";
	 if(id=="proveedor"){
	 <?
	   $sql="select idmaterial,descripcion from material order by descripcion asc";
	   $eje=mysql_query($sql);
	 ?>
	 td2.name="status[]";
	 td2.innerHTML="<select name='std[]' id='std"+n+"'class='selectsa' onchange='cargareq(this.form,"+n+")'><option selected></option><?while($dat=mysql_fetch_array($eje)){?><option value='<?echo htmlentities($dat[idmaterial]);?>'><?echo htmlentities($dat[descripcion]);?></option> <?}?></select>";
	 tr.appendChild(td2);	 
	 }else{
	 td2.name="status[]";
	 td2.innerHTML="<select name='std[]' id='std"+n+"'class='selectsa'><option selected></option><option value='EN CURSO'>EN CURSO</option><option value='TERMINADA'>TERMINADA</option></select>";
	 tr.appendChild(td2);
	 }
	 
	 ele3.name="unidad[]";
	 ele3.id="unidadreq"+n;
	 ele3.type="text";
	 ele3.setAttribute("readonly","readonly");
	 ele3.setAttribute("class","inputs");
	 ele3.setAttribute("onkeyup","this.value=this.value.toUpperCase()");
	 td3.appendChild(ele3);
	 tr.appendChild(td3);
	 
	 ele4.name="prototipo[]";
	 ele4.type="text";
	 ele4.setAttribute("class","inputs");
	 ele4.setAttribute("onkeyup","this.value=this.value.toUpperCase()");
	 td4.appendChild(ele4);
	 tr.appendChild(td4);
	 //termina tercera celda

	 /*ele5.name="paquete[]";
	 ele5.type="text";
	 ele5.setAttribute("class","inputs");
	 ele5.setAttribute("onkeyup","this.value=this.value.toUpperCase()");
	 td5.appendChild(ele5);
	 tr.appendChild(td5);*/
	 
	 /*ele5.name="noviv[]";
	 ele5.type="hidden";
	 ele5.setAttribute("class","inputsa");
	 ele5.setAttribute("onkeyup","this.value=this.value.toUpperCase()");
	 td5.appendChild(ele5);
	 tr.appendChild(td5);*/
	 
	  td6.innerHTML='<div onclick="borrar(\''+id+'\',\'archivo'+id+n+'\')" class="borrar"><b style="position:absolute;left:1000px;margin:-9px auto;cursor:pointer;cursor:hand;"><img src="img/mal.png" width="20" height="20"></b></div>';
	  tr.appendChild(td6);
	  
	  	 //ele5.name="tipo"+id+"[]";
	 /*ele6.name="tipo[]";
	 ele6.type="hidden";
	 ele6.value=id;
	 ele6.size=50;
	 td7.appendChild(ele6);
	 tr.appendChild(td7);*/
	 
	 cont.appendChild(tr);

	 btnupload("subir"+n,"destino"+n,"ruta"+n);
	 document.getElementById("total").value=n;
	 
}

function borrar(id,tr){
     cont=document.getElementById(id);
     obj=document.getElementById(tr);
	 cont.removeChild(obj);
	var button = $('#fake');
	new AjaxUpload('#fake', {
        action: 'upload.php',
		onSubmit : function(file , ext){
		if (! (ext && /^(jpg|jpeg|png|gif|pdf)$/.test(ext))){
			// extensiones permitidas
			alert('Error: Solo se permiten imagenes');
			// cancela upload
			return false;
		} else {
			button.html('<b style="position:absolute;left:4px;cursor:pointer;cursor:hand;">Subiendo</b>');
			this.disable();
		}
		},
		onComplete: function(file, response){
			button.html('<b style="position:absolute;left:20px;cursor:pointer;cursor:hand;">Subir</b>');
			// enable upload button
			this.enable();			
			// Agrega archivo a la lista
			$('#fake').appendTo('.files').text(response);
		}
	});
}
function cargareq(f,n){
   var uni=document.getElementById('unidadreq'+n).value;
   var mat=document.getElementById('std'+n).value;
   //uni.length=0;
   <?
    $sqlm="SELECT idmaterial,unidad FROM material";
	$rdm=mysql_query($sqlm);
	while($ldm=mysql_fetch_object ($rdm)){
  ?>
       if(mat=='<?echo$ldm->idmaterial;?>'){
			document.getElementById('unidadreq'+n).value="<?echo$ldm->unidad;?>";		 
		}
  <?}?>
}
</script>
</head>
<body>
					   <?
					   if(isset($_POST["registrar"])){
						 $nomcom=$_POST['nomcom'];
						 $nomfis=$_POST['nomfis'];
						 $rfc=$_POST['rfc'];
						 $dir=$_POST['dir'];
						 $cp=$_POST['cp'];
						 $tel=$_POST['tel'];
						 $correo=$_POST['correo'];
						 $fpago=$_POST['fpago'];
						 $banco=$_POST['banco'];
						 $cuenta=$_POST['cuenta'];
						 $clabe=$_POST['clabe'];
						 $obsp=$_POST['obs'];
						 $fi=$_POST['fi'];
						 $ff=$_POST['ff'];
						 
						 $statusp=$_POST['std'];
						 $lonsts=strlen($statusp);
						 $prototipo=$_POST['prototipo'];
						 $paquete=$_POST['paquete'];
						 $noviv=$_POST['noviv'];
						 
						$nomdia=array("DO","LU","MA","MI","JU","VI","SA");
						$nd=Date("w");
						$nod=Date("d");
						$nm=Date("m");
						$na=Date("y");
						$h=Date("H");
						$m=Date("i");
						$s=Date("s");
						$al=rand(1,$s);
						$cod_=$nomdia[$nd].$nod.$nm.$na.$h.$m.$s.$usu_login.$al;
						 
						 
						 $sql="SELECT COUNT(idproveedor) AS no FROM proveedormat WHERE nomfiscal<>'' and nomfiscal='$nomfis'";
						 $con=mysql_query($sql);
						 $res=mysql_fetch_array($con);
						 if($res["no"]==0){	

						 if($lonsts!=0){
							foreach($statusp as $indice=>$valor){
							    $sqlpro="INSERT INTO `material_proveedor` (`foliomaterial`, `idmaterial`, `costounitario`, `observacion`) VALUES
('$cod_', $statusp[$indice],'$prototipo[$indice]','$paquete[$indice]')";
								mysql_query($sqlpro);
								}
							}
						 
						 $sqlp="INSERT INTO `proveedormat` (`nomcomercial`, `nomfiscal`, `rfc`, `direccion`, `cp`, `telefono`, `correo`, `formapago`, `banco`, `nocuenta`, `clabe`, `observaciones`, `fechareg`, `foliomaterial`) VALUES
('$nomcom', '$nomfis', '$rfc', '$dir', '$cp', '$tel', '$correo', '$fpago', '$banco', '$cuenta', '$clabe', '$obsp', '$fi', '$cod_')";
					 if(mysql_query($sqlp)){?>
					 	 <table class="tabla" style="width:100%;">
							    <tr>
						             <td colspan="4" class="titulo">El Proveedor se registr&oacute; correctamente<img src='requisiciones/img/ok.png' width='30' height='30'  style="position:absolute;margin-top:-2px;margin-left:5px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="" class="btnaction">registrar nuevo</a></td>
						        </tr>
								<?
								    $sql="SELECT * from proveedormat where nomcomercial='$nomcom'";
                                    $con=mysql_query($sql);
                                    $res=mysql_fetch_array($con);									
								?>
							</table>
							<div id="infoSocio">
							<table class="tabla" style="width:100%;" border="0">
								<tr>
								     <td colspan="4" class="sbtitulo">Informaci&oacute;n del Material</td>
								</tr>
								<tr>
								  	<td class="dato">Nombre Comercial: </td>
									 <td><?echo $res['nomcomercial'];?></td>
									 <td class="dato">Razon social: </td>
									 <td><?echo $res['nomfiscal'];?></td>
								</tr>
								<tr>
									 <td class="dato">RFC: </td>
									 <td><?echo $res['rfc'];?></td>
					            </tr>
								<tr>
									 <td class="dato">Direccion: </td>
									 <td><?echo $res['direccion'];?></td>
									 <td class="dato">C.P: </td>
									 <td><?echo $res['cp'];?></td>
								</tr>
								<tr>
						             <td class="dato">Telefono: </td>
									 <td><?echo $res['telefono'];?></td>
						             <td class="dato">Correo: </td>
									 <td><?echo $res['correo'];?></td>									 
					            </tr>
								<tr>
									 <td class="dato">Forma de pago:</td>
									 <td><?echo $res['formapago'];?></td>
									 <td class="dato">Banco:</td>
									 <td><?echo $res['banco'];?></td>									 
					            </tr>
								<tr>
								     <td class="dato">Numero de cuenta:</td>
									 <td><?echo $res['nocuenta'];?></td>
								     <td class="dato">Clabe interbancaria:</td>
									 <td><?echo $res['clabe'];?></td>
								</tr>
								<tr>
								     <td class="dato">Observaciones:</td>
									 <td colspan="3"><?echo $res['observaciones'];?></td>
								</tr>
								<tr>								     
								     <td class="dato">Fecha Registro:</td>
									 <td><?echo $res['fechareg'];?></td>
									 <!--<td class="dato">Fecha vencimiento:</td>
									 <td><?echo $res['fvencimiento'];?></td>-->
								</tr>
								<tr>
								     <td colspan="4" class="sbtitulo">Concepto</td>
								</tr>
								<tr>								     
								     <td class="dato">Material </td>
									 <td class="dato">Unidad</td>
									 <td class="dato">Costo Unitario</td>
								</tr>
								<?
								    $sqlm="SELECT mp.idmaterial,
									       mp.costounitario,
                                           m.descripcion,
										   m.unidad
									       FROM material_proveedor mp 
										   INNER JOIN material m
										   ON mp.idmaterial=m.idmaterial
										   where foliomaterial='$cod_'";
                                    $conm=mysql_query($sqlm);
                                    while($resm=mysql_fetch_array($conm)){
								?>
								<tr>								     
								  <td><?echo $resm['descripcion'];?></td>
								  <td><?echo $resm['unidad'];?></td>
								  <td><?echo $resm['costounitario'];?></td>
								</tr>
								<?}?>
							</table>
						</div>
					 
					 <?}
					 
					 }else{
						     ?>
							 <table class="tabla" style="width:100%;">
							    <tr>
						             <td colspan="3" class="titulo">El nombre fiscal del proveedor ya existen<img src='img/mal.png' width='30' height='30'  style="position:absolute;margin-top:-2px;margin-left:5px;"></td>
						        </tr>
								<tr>
						             <td><br> <a href="javascript:history.back();" class="btnaction">regresar</a></td>
					            </tr>
							 </table>
							 <?
						 }
					 }else{?>
					<form name="datosregistro" id="datosregistro" action="regproveedor.php" method="POST" onsubmit="return valida();">
				     <table class="tabla" style="width:100%;" border="0">
						<tr>
						    <td colspan="4" class="titulo"><img src="requisiciones/img/personaIcon.png" style="position:absolute;margin-top:-4px;margin-left:-35px;">Datos Proveedor</td>
						</tr>
						     <td class="dato">Fecha Registro: </td>
							 <td>
							     <input type="text" name="fi" id="fi" class="inputs" value="<?echo date('Y-m-d');?>" readonly>
							 </td>
						     <td class="dato">RFC: </td>
							 <td>
							     <input type="text" name="rfc" id="rfc" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
						<tr>
						     <td class="dato">Nombre Comercial: </td>
							 <td>
							     <input type="text" name="nomcom" id="nomcom" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
						     <td class="dato">Nombre Fiscal: </td>
							 <td>
							     <input type="text" name="nomfis" id="nomfis" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
					    </tr>
						<tr>
						     <td class="dato">Direccion: </td>
							 <td colspan="">
							     <input type="text" name="dir" id="dir" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
						     <td class="dato">Codigo Postal: </td>
							 <td>
							     <input type="text" name="cp" id="cp" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
					    </tr>
						<tr>
						     <td class="dato">Telefono: </td>
							 <td>
							     <input type="text" name="tel" id="tel" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
						     <td class="dato">Correo: </td>
							 <td>
							     <input type="text" name="correo" id="correo" class="inputs" onChange="javascript:this.value=this.value.toLowerCase();" autocomplete=off>
							 </td>
					    </tr>
						<tr>
						    <td colspan="4" class="titulo"><img src="requisiciones/img/personaIcon.png" style="position:absolute;margin-top:-4px;margin-left:-35px;">Datos de pago</td>
						</tr>
						<tr>
						     <td class="dato">Forma de pago: </td>
							 <td>
							     <select name="fpago" id="fpago" class="selects">
								   <option></option>
								   <option value="EFECTIVO">EFECTIVO</option>
								   <option value="TRANSFERENCIA">TRANSFERENCIA</option>
								   <option value="DEPOSITO">DEPOSITO</option>
								 </select>
							     <!--<input type="text" name="fpago" id="fpago" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>-->
							 </td>
						     <td class="dato">Banco: </td>
							 <td>
							     <select name="banco" class="selects">
								   <option></option>
								   <option value="BANORTE">BANORTE</option>
								   <option value="BANCOMER">BANCOMER</option>
								   <option value="IXE">IXE</option>
								   <option value="BANAMEX">BANAMEX</option>
								   <option value="HSBC">HSBC</option>
								   <option value="SANTANDER">SANTANDER</option>
								   <option value="SCOTIABANK">SCOTIABANK</option>
								   <option value="INBURSA">INBURSA</option>
								   <option value="BANCO AZTECA">BANCO AZTECA</option>
								 </select>
							     <!--<input type="text" name="banco" id="banco" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>-->
							 </td>
					    </tr>
						<tr>
						     <td class="dato">No. Cuenta: </td>
							 <td>
							     <input type="text" name="cuenta" id="cuenta" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
						     <td class="dato">Clabe interbancaria: </td>
							 <td>
							     <input type="clabe" name="clabe" id="clabe" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
					    </tr>
						<tr>
						     <td class="dato">Observaciones: </td>
							 <td  colspan="">
							     <input type="text" name="obs" id="obs" class="inputs" onChange="javascript:this.value=this.value.toUpperCase();" autocomplete=off>
							 </td>
						     <!--<td class="dato">Fecha vencimiento: </td>
							 <td  colspan="">
							     <input type="text" name="ff" id="ff" class="inputs" readonly autocomplete=off>
							 </td>-->
					    </tr>
						<tr style="display:none">
						    <td colspan="4" class="titulo"><img src="requisiciones/img/personaIcon.png" style="position:absolute;margin-top:-4px;margin-left:-35px;">Materiales</td>
						</tr>
						 <tr style="display:none">
							<td  align="left" colspan="4">Agregar Concepto <a href="javascript:proveedor('proveedor','prov')"><img src="requisiciones/img/mas.png" width="15"  height="15"></a></td>
							<input type="hidden" id="totus">
						</tr>
						 <tr id="prov" style="display:none">
							 <td colspan="4">
								 <table id="proveedor" width="100%">
									 <tr>
										 <td width="150"><b>Concepto</b></td>
										 <td width="150"><b>Unidad</b></td>
										 <td width="150"><b>Costo unitario</b></td>
										 <td></td>
									 </tr>						 
								</table>	
							 </td>
						</tr>
						<tr>
							 <td></td>
							 <td align="right"><br><input type="submit" name="registrar" id="registrar" value="Registrar" class="submit"></td>
					    </tr>						
					</table>		
				</form>
				<?}?>
</body>
</html>