<?php
ini_set('display_errors',1);
session_start();
date_default_timezone_set('America/Lima');
require_once ("../../config/Cado.php");
require_once ("../venta/cVenta.php");
$oVenta = new cVenta();
require_once("../formatos/formato.php");
if($_POST['action']=="insertar")
{
	$docid = '';
	$fec = date('d-m-Y');
}
?>
 <style type="text/css">
.ui-autocomplete
{
    z-index: 99999; 
}
</style>
<div class="col-lg-12 col-sm-12 col-xs-12" >
	<form id="for_notdeb" action="POST">
	<div class="col-lg-12" style="font-weight:bold;font-size:2rem;">		
		Información de Nota de debito			
	</div>
					<div class="col-lg-2 col-sm-3 col-xs-6">
						<label for="txt_notdeb_fec">Fecha:</label>
						<input name="txt_notdeb_fec" type="text"  class="form-control" id="txt_notdeb_fec" value="<?php echo $fec?>"  maxlength="10"> 
					</div>	
					<div class="col-lg-3 col-sm-4 col-xs-6">
						<label for="cmb_notdeb_docid">Documento:</label>
						<select   name="cmb_notdeb_docid" id="cmb_notdeb_docid" class=" form-control" ></select>
					</div>
					
					<input name="txt_notdeb_numdoc" type="hidden" id="txt_notdeb_numdoc" readonly> 
					
					<div class="col-lg-4 col-sm-6 col-xs-6">
						<label for="cmb_notdeb_tip">Tipo Nota Debito:</label>
						<select style="background-color: white;"  name="cmb_notdeb_tip" id="cmb_notdeb_tip" class="form-control" >
						<option value="1">INTERES POR MORA</option>
						<option value="2">AUMENTO EN EL VALOR</option>
						<option value="3">PENALIDADES/OTROS CONCEPTOS</option>
						</select>
					</div>	
				<div  class="col-lg-12 col-sm-12 col-xs-12">	</div>
							<div class="col-lg-5 col-xs-6">
								<label>Serie/Numero del documento:</label>
								<input type="hidden" id="hdd_venta_id" name="hdd_venta_id" class="form-control  input-sm" /> 
								<input type="text" id="txt_docnum" name="txt_docnum" class="form-control  input-sm" /> 
							</div>
							<div  class="col-lg-2 col-sm-5 col-xs-6">
									<br />							
									<span onClick="doc_tabla()" class="btn btn-primary glyphicon glyphicon-search"></span>																								
							</div> 		
		<!--	<div  class="col-lg-12 col-sm-12 col-xs-12">			
			<span class="text-red" id="text_not" style="font-size:2rem; "></span>										
			</div> 	
			-->
			
			<div  class="col-lg-12 col-sm-12 col-xs-12" height="15px" >	</div> 
			
			<div id="div_doc" class="col-lg-12 col-sm-12 col-xs-12">
																												
			</div> 	
			
	
		<input name="action_notdeb" id="action_notdeb" type="hidden" value="<?php echo $_POST['action']?>">
		
		
	</form>   
	
	
	<div class="box-footer pull-right ">
		<span title="EMITIR NOTA DE DEBITO" id="btn_save" class="btn btn-primary">GUARDAR</span>
		<span title="CERRAR VENTANA"  data-dismiss="modal" class="btn btn-danger glyphicon glyphicon-remove"></span>
	</div>
	
	</div>
				
<script type="text/javascript">
	$("#btn_save").click(function(){
			$("#for_notdeb").submit();
			// alert("vfrfr");
	});	
	
	
	function doc_tabla()
	{
		$.ajax({
			type: "POST",
			url: '../notadebito/notadebito_tabla_docs.php',
			async:true,
			dataType: "html",                      
			data: ({
				venid : $("#hdd_venta_id").val(),
				motid : $("#cmb_notdeb_tip").val()
			}),
			beforeSend: function(){
				
				$('#div_doc').addClass("ui-state-disabled");
			},
			success: function(html){
				$('#div_doc').html(html);
			},
			complete: function(){			
				$('#div_doc').removeClass("ui-state-disabled");
			
			}
		});     
	}
	
	function cmb_doc_id(idf){
 				$.ajax({
 					type: "POST",
 					url: "../documento/cmb_doc_id.php",
 					async:true,
 					dataType: "html",                      
 					data: ({
 						mod_id: '8',
 						def : '1',
 						doc_id: idf,
 						vista:'deb'
 					}),
 					beforeSend: function() {
 						$('#cmb_notdeb_docid').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_notdeb_docid').html(html);
 					},
 					complete: function(){
 						
 							//txt_notcre_numdoc();
 					
 						}
 					});
 			}
	
			
	
	
	$(function(){
			
		$("#txt_notdeb_tip,#cmb_notdeb_tip").change(function(e){
			
			doc_tabla();
	
		});
		
		$("#txt_docnum").keydown(function(e){
			if (e.which == 13){
				
				doc_tabla();
				return false;
				}
			});
			
			$("#txt_docnum" ).autocomplete({
			minLength: 1,
			source: "../venta/docnum_complete_nom.php",
			select: function(event, ui){	
			$("#hdd_venta_id").val(ui.item.id);
			
			
			
		
			doc_tabla();
	
			}
			});
	 
			
			cmb_doc_id(<?php echo $docid?>);
			
			
			$("#cmb_notdeb_docid").focus();
			$('#cmb_notdeb_docid').change( function(){
		
			});
		
			
			$('#txt_notdeb_fec').datepicker({
			format: 'dd-mm-yyyy',
			autoclose:true,
			endDate: '+0d'
			});
			
			
				
			
	$('#for_notdeb').bootstrapValidator({
	    
	      message: 'Valor no Valido',
	      feedbackIcons: {
	      	valid: 'glyphicon glyphicon-ok',
	      	invalid: 'glyphicon glyphicon-remove',
	      	validating: 'glyphicon glyphicon-refresh'
	      },
	      fields: { 
	      	      	      
	      	txt_notcre_fec: {
	      		group: '.col-lg-2',
	      		validators: { 
	      			date: {
	      				format: 'DD-MM-YYYY',
	      				message: 'Fecha no Valida'
	      			}
	      		}
	      	},
	    
			txt_notcre_numdoc: {
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
			txt_notcre_tip:{
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
			txt_docnum:{
	      		group: '.col-lg-5',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
	      
	      }
	  }).on('success.form.bv', function(e) {  	
	  	$.ajax({
	  		type: "POST",
	  		url: "../notadebito/notadebito_reg.php",
	  		async:false,
	  		dataType: "json",
	  		data: $("#for_notdeb").serialize(),
	  		beforeSend: function(){
					
				},
				success: function(data)
				{	
				
				if(data.estreem==1){
					
				toastr["warning"](data.monreem);
				
				}
								
				if(data.est==0){
					toastr["error"](data.msj);
				}else if(data.est==3){
					toastr["warning"](data.msj);
				}else{
					toastr["info"](data.msj);
				
					$('.modal').modal('hide');
					$('.in').removeClass('modal-backdrop');		
					//venta_anular(data.venid);
					notadebito_ticket_datos(data.notdebid,data.venid);
					
				}
				
				},
				complete: function(){
					
				}
			});	
	  });
			
		});
	</script>