<?php

include_once __DIR__.'/../../core.php';

$id_module = $html->form('id_module');
$id_record = $html->form('id_record');
$idriga = $html->form('idriga');

$dir = get_var_tipodoc2($id_record,'dir');	

// Controllo sulla direzione monetaria
$uscite = [
    'Fatture di acquisto',
    'Ddt di acquisto',
    'Ordini fornitore',
];


$data = [
    'fat' => [
        'table' => 'co_righe_documenti',
        'id' => 'iddocumento',
    ],
    'ddt' => [
        'table' => 'dt_righe_ddt',
        'id' => 'idddt',
    ],
    'ord' => [
        'table' => 'or_righe_ordini',
        'id' => 'idordine',
    ],
    'int' => [
        'table' => 'mg_articoli_interventi',
        'id' => 'idintervento',
    ],
];
$modulo = 'fat';

$table = $data[$modulo]['table'];
$id = $data[$modulo]['id'];
$riga = str_replace('id', 'id_riga_', $id);



$rs = $dbo->fetchArray('SELECT mg_articoli.id AS idarticolo, mg_articoli.codice, mg_articoli.descrizione, '.$table.'.qta FROM '.$table.' INNER JOIN mg_articoli ON '.$table.'.idarticolo=mg_articoli.id WHERE '.$table.'.'.$id.'='.prepare($id_record).' AND '.$table.'.id='.prepare($idriga));

	$info = $dbo->fetchArray('SELECT * FROM mg_prodotti WHERE serial IS NOT NULL AND '.$riga.'='.prepare($idriga));
	$serials = array_column($info, 'serial');

	$in = [];
    foreach ($serials as $value) {
        $in[] = prepare($value);
    }
    $in = implode(',', $in);


echo '
<p>'.tr('Articolo').': '.$rs[0]['codice'].' - '.$rs[0]['descrizione'].'</p>

<form action="'.$rootdir.'/editor.php?id_module='.$id_module.'&id_record='.$id_record.'" method="post">
    <input type="hidden" name="op" value="add_serial">
    <input type="hidden" name="backto" value="record-edit">
    <input type="hidden" name="idriga" value="'.$idriga.'">
    <input type="hidden" name="idarticolo" value="'.$rs[0]['idarticolo'].'">
    <input type="hidden" name="dir" value="'.$dir.'">';


	if ($dir == 'entrata') {	
		$idart=$rs[0]['idarticolo'];
		$n=1;
		$num_ser=2;			
		if( $n>0 ){
			$show_btn = true;		
			echo "	<div class='form'>\n";	
			?>
			<div class="clearfix"></div>
			<br>	
			<div class="row">
				<div class="col-md-12">
					<table class="table table-striped table-hover table-condensed">
					<tr>
						<th width="35%">Codice Seriale</th>
					</tr>
					<?php
					$totale = 0.00;	
					for( $i=0; $i<$n; $i++ ){
						//Descrizione
						echo "		<tr>\n";
						echo "		<td class='text-left' >\n";
		
						echo "		<input type='hidden' name='idrigadocumento[]' value=\"".$rs[$i]['id']."\" />\n";				
						echo ' 					
							{[ "type": "select",  "name": "serial[]", "multiple": 1 , "value": "'.implode(',', $serials).'", "values": "query=SELECT DISTINCT serial AS id, serial AS descrizione , id_riga_documento , idarticolo FROM mg_prodotti WHERE dir=\'uscita\' AND id_riga_documento=\'0\' and idarticolo=\''.$idart.'\' AND serial NOT IN (SELECT serial FROM mg_prodotti WHERE dir=\'entrata\'  and serial NOT IN (SELECT serial FROM mg_prodotti WHERE '.$riga.' = \''.$idriga.'\'))'.(!empty($in) ? ' OR serial IN ('.$in.')' : '' ).'" , "extra": "data-max-options=\"'.intval($num_ser).'\"" ]}		
							';						
						echo "		</td></tr>\n";					
					}					
					echo "		</table>\n";
					echo "	</div>\n";
				echo "</div>\n";
		}	
		else{
			$show_btn = false;
			echo "<b>"._("Non ci sono seriali da evadere in questo documento")."...</b><br>\n";
		}
	}
		
		else{
			echo '
			<p>'.tr('I numeri seriali possono essere inseriti sono in fase di caricamento merce').'</p>';
	}







echo '

    <!-- PULSANTI -->
	<div class="row">
		<div class="col-md-12 text-right">
			<button type="submit" class="btn btn-primary pull-right"><i class="fa fa-barcode"></i> '.tr('Aggiorna').'</button>
		</div>
    </div>
</form>';

echo '
	<script src="'.$rootdir.'/lib/init.js"></script>';
