<?php
include_once(__DIR__."/../../core.php");
 
?><form action="" method="post" id="edit-form">
	<input type="hidden" name="op" value="update">
	<input type="hidden" name="backto" value="record-edit">
	<input type="hidden" name="id_record" value="<?php echo $id_record ?>">


	<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
		<div class="panel-heading">
			<h3 class="panel-title">Dettagli scadenza</h3>
		</div>
		
		<div class="panel-body">
			<div class="row">
				<!-- Info scadenza -->
				<div class="col-md-7">
					<table class="table table-striped table-hover table-condensed table-bordered">
					<?php
					$rs = $dbo->fetchArray("SELECT * FROM (co_documenti INNER JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id) INNER JOIN an_anagrafiche ON co_documenti.idanagrafica=an_anagrafiche.idanagrafica WHERE co_documenti.id=\"".$records[0]['iddocumento']."\"");
					( $rs[0]['numero_esterno'] != '' ) ? $numero=$rs[0]['numero_esterno'] : $numero=$rs[0]['numero'];
					
					if( $rs[0]['dir'] == 'entrata' ){
						$dir = 'entrata';
						$modulo = "Fatture di vendita";
						echo "<tr><th width='120'>Cliente:</th><td><a href=\"".$rootdir."/editor.php?id_module=".$modules_info['Anagrafiche']['id']."&id_record=".$rs[0]['idanagrafica']."\">".$rs[0]['ragione_sociale']."</a></td></tr>\n";
					}
					
					else{
						$dir = 'uscita';
						$modulo = "Fatture di acquisto";
						echo "<tr><th width='120'>Fornitore:</th><td>".$rs[0]['ragione_sociale']."</td></tr>\n";
					}
					
					
					echo "<tr><th>Documento:</th><td>".$rs[0]['descrizione']."</td></tr>\n";
					echo "<tr><th>Numero:</th><td>".$numero."</td></tr>\n";
					echo "<tr><th>Data:</th><td>".date("d/m/Y", strtotime($rs[0]['data']))."</td></tr>\n";
					?>
					</table>
					
					<a href="<?php echo $rootdir."/editor.php?id_module=14&id_record=".$records[0]['iddocumento'] ?>" class="btn btn-primary"><i class="fa fa-folder-open"></i> Apri documento</a>
				</div>
				
				<!-- Elenco scadenze -->
				<div class="col-md-5">
					<table class="table table-hover table-condensed table-bordered">
					<tr><th width="100">Data</th><th width="150">Importo</th><th width="150">Pagato</th></tr>

					<?php
					$totale_da_pagare	= 0;
					$totale_pagato		= 0;
					
					$rs = $dbo->fetchArray("SELECT * FROM co_scadenziario WHERE iddocumento=(SELECT iddocumento FROM co_scadenziario s WHERE id=\"".$id_record."\") ORDER BY scadenza ASC");
	
					for( $i=0; $i<sizeof($rs); $i++ ){
						if( $rs[$i]['da_pagare'] == $rs[$i]['pagato'] ){
							$class = "success";
							$can_edit = true;
						}
						
						else if( abs($rs[$i]['pagato']) == 0 ){
							$class = "danger";
							$can_edit = true;
						}
						
						else if( abs($rs[$i]['pagato']) <= abs($rs[$i]['da_pagare']) ){
							$class = "warning";
							$can_edit = true;
						}
						
						else{
							$class = "danger";
							$can_edit = true;
						}
						
						echo "<tr class='".$class."'>\n";
						echo "	<td align='center'>\n";
						
						if( $can_edit ){
							echo "		<input type='text' name='data[".$rs[$i]['id']."]' class='form-control text-center datepicker' value=\"".date( "d/m/Y", strtotime($rs[$i]['scadenza']) )."\">\n";
						}
						
						else{
							echo "		".date( "d/m/Y", strtotime($rs[$i]['scadenza']) )."\n";
							echo "		<input type='hidden' name='data[".$rs[$i]['id']."]' value=\"".date( "d/m/Y", strtotime($rs[$i]['scadenza']) )."\">\n";
						}
						
						echo "	</td>\n";
		
						echo "	<td align='right'>\n";
						
						if( $can_edit ){
							echo "		<input type='text' name='scadenza[".$rs[$i]['id']."]' class='form-control inputmask-decimal' value=\"".number_format( $rs[$i]['da_pagare'], get_var("Cifre decimali"), ".", "" )."\">\n";
						}
						
						else{
							echo "		".number_format( $rs[$i]['da_pagare'], get_var("Cifre decimali"), ",", "." )."\n";
							echo "		<input type='hidden' name='scadenza[".$rs[$i]['id']."]' class='form-control inputmask-decimal' value=\"".number_format( $rs[$i]['da_pagare'], get_var("Cifre decimali"), ".", "" )."\">\n";
						}
						
						echo "	</td>\n";
				
						echo "	<td align='right'>\n";
						
						if( $can_edit ){
							echo "		<input type='text' name='pagato[".$rs[$i]['id']."]' class='form-control inputmask-decimal' value=\"".number_format( $rs[$i]['pagato'], get_var("Cifre decimali"), ".", "" )."\">\n";
						}
						
						else{
							echo "		".number_format( $rs[$i]['pagato'], get_var("Cifre decimali"), ",", "." )."\n";
							echo "		<input type='hidden' name='pagato[".$rs[$i]['id']."]' class='form-control inputmask-decimal' value=\"".number_format( $rs[$i]['pagato'], get_var("Cifre decimali"), ".", "" )."\">\n";
						}
						
						echo "	</td>\n";
						echo "</tr>\n";
						
						$totale_da_pagare	+= $rs[$i]['da_pagare'];
						$totale_pagato		+= $rs[$i]['pagato'];
					}
					
					echo "<tr>\n";
					echo "	<td align='right'><b>Totale</b></td>\n";
					echo "	<td align='right' id='totale_utente'>".number_format( $totale_da_pagare, get_var("Cifre decimali"), ",", "." )."</td>\n";
					echo "	<td align='right'></td>\n";
					echo "</tr>\n";
					?>
	
					</table>
					
					<a onclick="launch_modal( 'Aggiungi prima nota', '<?php echo $rootdir ?>/add.php?id_module=<?php echo $modules_info['Prima nota']['id'] ?>&iddocumento=<?php echo $records[0]['iddocumento'] ?>&dir=<?php echo $dir ?>', 1 );" class="btn btn-sm btn-primary pull-right"><i class="fa fa-euro"></i> Aggiungi prima nota...</a>
					<br>
					<p class="text-danger hide" id="totale">Il totale da pagare deve essere pari a <b><?php echo number_format( $totale_da_pagare, get_var("Cifre decimali"), ",", "." ) ?> &euro;</b>.<br><span id="diff"></span> &euro; di differenza.</p>
					
					<input type="hidden" id="totale_da_pagare" value="<?php echo number_format( $totale_da_pagare, get_var("Cifre decimali"), ",", "" ) ?>">
				</div>
			</div>
			</br>	
			<div class="pull-right">

				<button type="submit" id="btn-saves" class="btn-app btn-success"><i class="fa fa-check"></i> Salva</button>
				<button type="button" class="btn-app btn-primary" 
				onclick="launch_modal( 'Invio documento tramite E-mail', '<?php echo $rootdir ?>/modules/scadenzario/mail_pdf.php?iddocumento=<?php echo $records[0]['iddocumento'] ?>&id_module=<?php echo $id_module ?>&idscadenza=<?php echo $id_record ?>',1);">
				<i class="fa fa-envelope"></i> Mail Sollecito</button>
			</div>
			
			<div class="col-md-2">			
				{[ "type": "date", "label": "Data Sollecito", "maxlength": 10, "name": "data_sollecito", "required": 0, "class": "text-center date-mask", "value": "$data_sollecito$", "extra": "readonly" ]}																				
			</div>
			
			
		</div>
	</div>
	
	<div class="panel panel-primary">					
		<div class="panel-body">
			<div class="row">
				<div class="col-md-12">
					<table class="table table-striped table-hover table-condensed">
						<tr>
							<th width="10%">Fattura</th>
							<th width="5%" style='text-align:center;'>Data Documento</th>
							<th width="5%" style='text-align:center;'>Data Sollecito</th>
							<th width="5%" style='text-align:center;'>Totale Documento</th>
							<th width="10%" style='text-align:center;'>Email sollecito</th>
							<th width="60%" style='text-align:center;'>Testo Mail</th>
							
						</tr>					
						<?php include($docroot."/modules/scadenzario/row-list-solleciti.php"); ?>
					</table>
				</div>
			</div>					
		</div>  
	</div> 
	
	
	
	<div class="panel panel-<?php echo $_SESSION['profilo_pannello'] ?>">
		<div class="panel-heading">
			<h3 class="panel-title">Pagamenti ricevuti</h3>
		</div>
		<div class="panel-body">
			<a class="btn btn-sm btn-info" data-href="<?php echo $rootdir ?>/modules/scadenzario/add_importo.php?iddocumento=<?php echo $records[0]['iddocumento'] ?>&id_record=<?php echo $records[0]['id'] ?>" data-toggle="modal" data-title="Nuovo Acconto" data-target="#bs-popup"><i class="fa fa-plus"></i> Acconto</a>
						
			<div class="row">											
				<div class="col-md-12">
					<table class="table table-striped table-hover table-condensed">
					<tr>
						<th width="10%">Data</th>
						<th width="10%">Ricevuto</th>
						<th width="20%">Banca</th>
						<th width="40%">Note</th>
						<th width="5%"></th>
					</tr>					
					<?php include($docroot."/modules/scadenzario/row-list.php"); ?>
					</table>
				</div>														
			</div>	
		</div>	
	</div>
				
	
	
	
	<div class="clearfix"></div>
</form>


<script>
	$(document).ready( function(){
		totale_ok();
		$('input[name*=scadenza]').keyup( function(){ totale_ok(); } );
	});
	
	
	function totale_ok(){
		totale_da_pagare	= force_decimal( $('#totale_da_pagare').val() );
		totale_utente		= 0;
		
		$('input[name*=scadenza]').each( function(){
			totale_utente += force_decimal( $(this).val() );
		});
		
		if( isNaN(totale_utente) ){
			totale_utente = 0;
		}
		
		totale_utente = Math.round(totale_utente*100)/100;
		totale_da_pagare = Math.round(totale_da_pagare*100)/100;

		
		diff = Math.abs(totale_da_pagare) - Math.abs(totale_utente);
		
		if( diff == 0 ){
			$('#btn-saves').removeClass('hide');
			$('#totale').addClass('hide');
		}
		
		else{
			$('#btn-saves').addClass('hide');
			$('#totale').removeClass('hide');
		}
		
		
		
		$('#diff').html( (diff.toFixed(<?php echo get_var("Cifre decimali") ?>)).replace('.', ',') );
		$('#totale_utente').html( (totale_utente.toFixed(<?php echo get_var("Cifre decimali") ?>)).replace('.', ',') );
	}
</script>