<?php
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-12" >
	<form id="for_notcre" action="POST">
	<div class="col-lg-12" style="font-weight:bold;font-size:2rem;">		
		Información de Nota de credito			
	</div>
	
	
	
					<div class="row">
					
					<div class="col-lg-3 col-sm-3 col-6">
						<label for="txt_notcre_fec">Fecha:</label>
						<input name="txt_notcre_fec" type="text"  class="form-control " id="txt_notcre_fec" value="<?php echo $fec?>"  maxlength="10"> 
					</div>	
					<div class="col-lg-3 col-sm-4 col-6">
						<label for="cmb_notcre_docid">Documento:</label>
						<select name="cmb_notcre_docid" id="cmb_notcre_docid" class=" form-control" ></select>
					</div>
					
					<div class="col-lg-3 col-sm-3 col-6">
 					<label for="txt_notcre_numdoc">N° Doc:</label>
 					<input name="txt_notcre_numdoc" type="text" class="form-control text-yellow" id="txt_notcre_numdoc" 
						style="text-align:right; font-size:14px"  value="<?php echo $numdoc?>" >						 
 					</div>
					
					<div class="col-lg-6 col-sm-6 col-6">
						<label for="txt_notcre_tip">Tipo Nota Credito:</label>
						<select style=""  name="txt_notcre_tip" id="txt_notcre_tip" class="form-control" >
						<option value="1">ANULACION DE LA OPERACION</option>
						<option value="4">DESCUENTO GLOBAL</option>
						<option value="5">DESCUENTO POR ITEM</option>
						<option value="6">DEVOLUCION TOTAL</option>
						</select>
					</div>	
				
				
							<div class="col-lg-4 col-sm-6 col-9">
								<label>Serie/Numero del documento:</label>
								<input type="hidden" id="hdd_venta_id" name="hdd_venta_id" /> 
								<input type="text" id="txt_docnum" name="txt_docnum" class="form-control" /> 
							</div>
							<div  class="col-lg-2 col-sm-5 col-3"  style="display: inline-block; white-space: nowrap;padding-top:30px;" >			
									<span onClick="doc_tabla()" STYLE="cursor:pointer" class="btn btn-primary"><i class="fas fa-search"></i></span>																								
							</div> 
							
							
		
			
			<div  class="col-lg-12 col-sm-12 col-12" height="15px" >	</div> 
			
			<div  class="col-lg-12 col-sm-12 col-12" >				
			<div id="div_doc" ></div> 	
			</div> 
			
	
			<input name="action_notcre" id="action_notcre" type="hidden" value="<?php echo $_POST['action']?>">
		
		</div> 		
		
		<div  class="col-lg-12 col-sm-12 col-12"  style="display: inline-block; white-space: nowrap;" height="15px" >	</div> 
		
	</form>   
	
	
	<div class="box-footer float-right ">
		<span STYLE="cursor:pointer" title="EMITIR NOTA DE CREDITO" id="btn_save" class="btn btn-primary">GUARDAR</span>
		<span STYLE="cursor:pointer" title="CERRAR VENTANA"  data-dismiss="modal" class="btn btn-default">CANCELAR</span>
	</div>
	
	</div>
				
<script type="text/javascript">
	var clicando = false;
	$("#btn_save").click(function(){

		if(clicando){		 	
			toastr["error"]('Hemos quitado el proceso de doble registro.');
		}else{
		clicando= true;

			$("#for_notcre").submit();
			// alert("vfrfr");

		}
	});	
	
	
	function doc_tabla()
	{
		$.ajax({
			type: "POST",
			url: '../notacredito/notacredito_tabla_docs.php',
			async:true,
			dataType: "html",                      
			data: ({
				venid : $("#hdd_venta_id").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: '7',
 						def : '1',
 						doc_id: idf,
 						vista:	''
 					}),
 					beforeSend: function() {
 						$('#cmb_notcre_docid').html('<option value="">Cargando...</option>');
 					},
 					success: function(html){			
 						$('#cmb_notcre_docid').html(html);
 					},
 					complete: function(){
 						
 							txt_notcre_numdoc();
 					
 						}
 					});
 			}
	
	
		function txt_notcre_numdoc()
 			{
 				$.ajax({
 					type: "POST",
 					url: "../venta/venta_txt_numdoc.php",
 					async:false,
 					dataType: "json",                      
 					data: ({
 						doc_id: $('#cmb_notcre_docid').val()
 					}),
 					beforeSend: function() {
 						$('#txt_notcre_numdoc').val('Cargando...');
 					},
 					success: function(data){			
 						$('#txt_notcre_numdoc').val(data.numero);
 						if(data.msj!="")
 						{
							toastr["success"](data.msj);
 						}
 						else
 						{
 						}
 					},
 					complete: function(){
 												
 						}
 					});
 			}
			
	$(function(){
			
			
	$('#cmb_notcre_docid').change( function(){
		$("#hdd_venta_id").val('');
		doc_tabla();
		
	}); 
			
			$("#txt_docnum").keydown(function(e){
			if(e.which==13)
			{
				return false;
			}
		});
		
		$("#txt_notcre_tip").change(function(e){
			
			doc_tabla();
	
		});
		
		
			$("#txt_docnum" ).autocomplete({
			minLength: 1,
			source: "../venta/docnum_complete_nom.php",
			select: function(event, ui){	
			$("#hdd_venta_id").val(ui.item.id);
			
			/* if(ui.item.est==0){
				$("#text_not").html('LA VENTA YA ESTA ANULADA.');
					
			}else{
				$("#text_not").html('La Nota de credito se emitira con este docuemnto.*');
				
			} */
			
		
			doc_tabla();
	
			}
			});
	
		
			
			
			cmb_doc_id(<?php echo $docid?>);
			
			
			$("#cmb_notcre_docid").focus();
			$('#cmb_notcre_docid').change( function(){
		
			});
		
			
			$('#txt_notcre_fec').datepicker({
			format: 'dd-mm-yyyy',
			autoclose:true,
			endDate: '+0d',
			startDate: '-6d'
			});
			
			
				
			
	$('#for_notcre').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'
	      			}
	      		}
	      	},
			cmb_notcre_docid:{
	      		group: '.col-lg-3',
	      		validators: {
	      			notEmpty: {
	      				message: '* Requerido'
	      			}
	      		}
	      	},
	      
	      }
	  }).on('success.form.bv', function(e) {  	
	  	$.ajax({
	  		type: "POST",
	  		url: "../notacredito/notacredito_reg.php",
	  		async:false,
	  		dataType: "json",
	  		data: $("#for_notcre").serialize(),
	  		beforeSend: function(){
					
				},
				success: function(data)
				{	
				
				if(data.estreem==1){
					
				toastr["warning"](data.monreem);
				
				}
								
				if(data.est==0){
					toastr["danger"](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);
					notacredito_ticket_datos(data.notcreid,data.venid);
					
				}
				
				},
				complete: function(){
					notacredito_tabla();
				}
			});	
	  });
			
		});
	</script>