dol_include_once('/fichinter/class/fichinter.class.php');
dol_include_once('/asset/class/asset.class.php');
dol_include_once('/core/lib/product.lib.php');
$PDOdb = new TPDOdb();
$type_object = GETPOST('type_object');
$id = (int) GETPOST('id');
$dispatch = new TDispatch();
$dispatch->loadByObject($PDOdb, $id, $type_object);
$action = GETPOST('action');
switch ($action) {
    case 'save':
        $TLine = GETPOST('TLine');
        if (!empty($TLine[-1]['serial_number']) && (!empty($TLine[-1]['fk_object']) || GETPOST('type_object') === 'ticketsup')) {
            $asset = new TAsset();
            $asset->loadReference($PDOdb, $TLine[-1]['serial_number']);
            if ($asset->getId() > 0) {
                $k = $dispatch->addChild($PDOdb, 'TDispatchAsset');
                $dispatch->TDispatchAsset[$k]->fk_asset = $asset->getId();
                $dispatch->TDispatchAsset[$k]->fk_object = $TLine[-1]['fk_object'];
                $dispatch->TDispatchAsset[$k]->object_type = $type_object;
                $dispatch->TDispatchAsset[$k]->asset = $asset;
                $dispatch->save($PDOdb);
            }
        }
        break;
    case 'delete-line':
        $k = (int) GETPOST('k');
        $dispatch->TDispatchAsset[$k]->to_delete = true;
        $dispatch->save($PDOdb);
        break;
}
function tabImport(&$TImport, &$commande)
{
    global $langs, $db, $conf;
    $PDOdb = new TPDOdb();
    $form = new TFormCore();
    $formDoli = new Form($db);
    $formproduct = new FormProduct($db);
    if ($commande->statut >= 5 || $commande->statut <= 2) {
        $form->type_aff = "view";
    }
    if ($commande->statut <= 2 || $commande->statut >= 6) {
        print $langs->trans("OrderStatusNotReadyToDispatch");
    }
    _show_product_ventil($TImport, $commande, $form);
    print count($TImport) . ' équipement(s) dans votre réception';
    ?>
	<script type="text/javascript">
		$(document).ready(function() {
			$("#dispatchAsset").change(function() {
				$("#actionVentilation").addClass("error").html("<?php 
    echo $langs->trans('SaveBeforeVentil');
    ?>
");
			});
		});
	</script>
	<table width="100%" class="border" id="dispatchAsset">
		<tr class="liste_titre">
			<td>Produit</td>
			<td>Numéro de Série</td>
			<td>Numéro de Lot</td>
			<td><?php 
    echo $langs->trans('Warehouse');
    ?>
</td>
			<?php 
    if ($conf->global->ASSET_SHOW_DLUO) {
        ?>
				<td>DLUO</td>
			<?php 
    }
    if (empty($conf->global->DISPATCH_USE_ONLY_UNIT_ASSET_RECEPTION)) {
        ?>
			<td>Quantité</td>
			<?php 
        if (!empty($conf->global->DISPATCH_SHOW_UNIT_RECEPTION)) {
            echo '<td>Unité</td>';
        }
    }
    if ($conf->global->clinomadic->enabled) {
        ?>
				<td>IMEI</td>
				<td>Firmware</td>
				<?php 
    }
    ?>
			<td>&nbsp;</td>
		</tr>

	<?php 
    $prod = new Product($db);
    $warning_asset = false;
    if (is_array($TImport)) {
        foreach ($TImport as $k => $line) {
            if ($prod->id == 0 || $line['ref'] != $prod->ref) {
                if (empty($line['fk_product']) === false) {
                    $prod->fetch($line['fk_product']);
                } else {
                    if (empty($line['ref']) === false) {
                        $prod->fetch('', $line['ref']);
                    } else {
                        continue;
                    }
                }
            }
            ?>
<tr>
					<td><?php 
            echo $prod->getNomUrl(1) . $form->hidden('TLine[' . $k . '][fk_product]', $prod->id) . $form->hidden('TLine[' . $k . '][ref]', $prod->ref) . " - " . $prod->label;
            ?>
</td>
					<td><?php 
            echo $form->texte('', 'TLine[' . $k . '][numserie]', $line['numserie'], 30);
            $asset = new TAsset();
            if (empty($line['numserie'])) {
                echo img_picto($langs->trans('SerialNumberNeeded'), 'warning.png');
                $warning_asset = true;
            } else {
                if ($asset->loadReference($PDOdb, $line['numserie'])) {
                    echo '<a href="' . dol_buildpath('/asset/fiche.php?id=' . $asset->getId(), 1) . '">' . img_picto('Equipement lié à cet import', 'info.png') . '</a>';
                } else {
                    echo img_picto('Aucun équipement créé en Base', 'warning.png');
                    $warning_asset = true;
                }
            }
            echo $form->hidden('TLine[' . $k . '][commande_fournisseurdet_asset]', $line['commande_fournisseurdet_asset'], 30);
            ?>
					</td>
					<td><?php 
            echo $form->texte('', 'TLine[' . $k . '][lot_number]', $line['lot_number'], 30);
            ?>
</td>
					<td rel="entrepotChild" fk_product="<?php 
            echo $prod->id;
            ?>
"><?php 
            $formproduct = new FormProduct($db);
            $formproduct->loadWarehouses();
            if (count($formproduct->cache_warehouses) > 1) {
                print $formproduct->selectWarehouses($line['fk_warehouse'], 'TLine[' . $k . '][entrepot]', '', 1, 0, $prod->id, '', 0, 1);
            } elseif (count($formproduct->cache_warehouses) == 1) {
                print $formproduct->selectWarehouses($line['fk_warehouse'], 'TLine[' . $k . '][entrepot]', '', 0, 0, $prod->id, '', 0, 1);
            } else {
                print $langs->trans("NoWarehouseDefined");
            }
            ?>
</td>
					<?php 
            if (!empty($conf->global->ASSET_SHOW_DLUO)) {
                ?>
					<td><?php 
                echo $form->calendrier('', 'TLine[' . $k . '][dluo]', date('d/m/Y', strtotime($line['dluo'])));
                ?>
</td>
					<?php 
            }
            if (empty($conf->global->DISPATCH_USE_ONLY_UNIT_ASSET_RECEPTION)) {
                ?>
						<td><?php 
                echo $form->texte('', 'TLine[' . $k . '][quantity]', $line['quantity'], 10);
                ?>
</td><?php 
                if (!empty($conf->global->DISPATCH_SHOW_UNIT_RECEPTION)) {
                    echo '<td>' . ($commande->statut < 5) ? $formproduct->select_measuring_units('TLine[' . $k . '][quantity_unit]', 'weight', $line['quantity_unit']) : measuring_units_string($line['quantity_unit'], 'weight') . '</td>';
                }
            } else {
                echo $form->hidden('TLine[' . $k . '][quantity]', $line['quantity']);
                echo $form->hidden('TLine[' . $k . '][quantity_unit]', $line['quantity_unit']);
            }
            if ($conf->global->clinomadic->enabled) {
                ?>
						<td><?php 
                echo $form->texte('', 'TLine[' . $k . '][imei]', $line['imei'], 30);
                ?>
</td>
						<td><?php 
                echo $form->texte('', 'TLine[' . $k . '][firmware]', $line['firmware'], 30);
                ?>
</td>
						<?php 
            }
            ?>
					<td>
						<?php 
            if ($commande->statut < 5) {
                echo '<a href="?action=DELETE_LINE&k=' . $k . '&id=' . $commande->id . '&rowid=' . $line['commande_fournisseurdet_asset'] . '">' . img_delete() . '</a>';
            }
            ?>
					</td>
				</tr>
				<?php 
        }
    }
    if ($commande->statut < 5 && $commande->statut > 2) {
        $pListe[0] = "Sélectionnez un produit";
        foreach ($commande->lines as $line) {
            if ($line->fk_product) {
                $pListe[$line->fk_product] = $line->product_ref . " - " . $line->product_label;
            }
        }
        $defaultDLUO = '';
        if ($conf->global->DISPATCH_DLUO_BY_DEFAULT) {
            $defaultDLUO = date('d/m/Y', strtotime(date('Y-m-d') . " " . $conf->global->DISPATCH_DLUO_BY_DEFAULT));
        }
        echo $defaultDLUO;
        ?>
<tr style="background-color: lightblue;">
					<td><?php 
        print $form->combo('', 'new_line_fk_product', $pListe, '');
        ?>
</td>
					<td><?php 
        echo $form->texte('', 'TLine[-1][numserie]', '', 30);
        ?>
</td>
					<td><?php 
        echo $form->texte('', 'TLine[-1][lot_number]', '', 30);
        ?>
</td>
					<td><?php 
        $formproduct = new FormProduct($db);
        $formproduct->loadWarehouses();
        if (count($formproduct->cache_warehouses) > 1) {
            print $formproduct->selectWarehouses('', 'TLine[-1][entrepot]', '', 1, 0, $prod->id, '', 0, 1);
        } elseif (count($formproduct->cache_warehouses) == 1) {
            print $formproduct->selectWarehouses('', 'TLine[-1][entrepot]', '', 0, 0, $prod->id, '', 0, 1);
        } else {
            print $langs->trans("NoWarehouseDefined");
        }
        ?>
</td>
					<?php 
        if (!empty($conf->global->ASSET_SHOW_DLUO)) {
            ?>
						<td><?php 
            echo $form->calendrier('', 'TLine[-1][dluo]', $defaultDLUO);
            ?>
</td>
					<?php 
        }
        if (empty($conf->global->DISPATCH_USE_ONLY_UNIT_ASSET_RECEPTION)) {
            ?>
					<td><?php 
            echo $form->texte('', 'TLine[-1][quantity]', '', 10);
            ?>
</td><?php 
            if (!empty($conf->global->DISPATCH_SHOW_UNIT_RECEPTION)) {
                echo '<td>' . $formproduct->select_measuring_units('TLine[-1][quantity_unit]', 'weight') . '</td>';
            }
        }
        if ($conf->global->clinomadic->enabled) {
            ?>
						<td><?php 
            echo $form->texte('', 'TLine[-1][imei]', '', 30);
            ?>
</td>
						<td><?php 
            echo $form->texte('', 'TLine[-1][firmware]', '', 30);
            ?>
</td>
						<?php 
        }
        ?>
					<td>Nouveau
					</td>
				</tr>
			<?php 
    }
    ?>


	</table>
	<?php 
    if ($commande->statut < 5 || $warning_asset) {
        if ($commande->statut < 5) {
            echo '<div class="tabsAction">' . $form->btsubmit('Enregistrer', 'bt_save') . '</div>';
        }
        $form->type_aff = 'edit';
        ?>
		<hr />
		<?php 
        echo '<div id="actionVentilation">';
        echo 'Date de réception : ' . $form->calendrier('', 'date_recep', time());
        echo ' - ' . $langs->trans("Comment") . ' : ' . $form->texte('', 'comment', $_POST["comment"] ? GETPOST("comment") : $langs->trans("DispatchSupplierOrder", $commande->ref), 60, 128);
        echo ' ' . $form->btsubmit($langs->trans('AssetVentil'), 'bt_create');
        echo '</div>';
    }
}