<?php
require 'database.inc.php';
if (Test_User($lg_user, $lg_password) == TRUE) {
	$ligne = Commande::getCommandeByNoCommande($NoCommande);
	if ($mode == 1) {
		$Dest = "Stock";
		if ($ligne2 = Client::getClientByNoClient($ligne['NoClient'])) {
			$Dest = $ligne2['Nom'];
		}
	}
	if ($resultat = CommandeArticle::getCommandeArticleByNoCommande($NoCommande)) {
		if ($mode != 2) {
			echo "document.info.Texte_libre.value='Impotation commande no $NoCommande';\n";
			echo "add_new_text_newfact();\n";
		}
		$prevType = 0;
		$multiline = "";
		foreach ($resultat as $ligne) {
			if ($mode == 2 && $prevType == 1 && $ligne['TypeData'] != 1) {
				echo "$('.ta_texte_libre').val('" . $multiline . "');\n";
				echo "add_new_text_newfact();\n";
			}
			if ($mode == 2 && $prevType == 6 && $ligne['TypeData'] != 6) {
				echo "$('.ta_texte_libre').val('" . $multiline . "');\n";
				echo "add_new_text_newfact(true);\n";
			}
			switch ($ligne['TypeData']) {
				case 0:
					echo "document.info.NoArticle.value='" . addslashes($ligne['NoArticle']) . "';\n";
					echo "document.info.Description.value='" . addslashes($ligne['Description']) . "';\n";
					if ($lignePrix = Article::getArticleByNoArticle($ligne['NoArticle'])) {
						if ($lignePrix["PrixPropre"] > 0) {
							echo "document.info.Prix.value ='" . $lignePrix['PrixPropre']  . "';\n";
						} elseif ($lignePrix["PrixImportateur"] > 0) {
							echo "document.info.Prix.value ='" . $lignePrix['PrixImportateur']  . "';\n";
						} else {
							echo "document.info.Prix.value ='" . $ligne['PrixBase']  . "';\n";
						}
					} else {
						echo "document.info.Prix.value ='" . $ligne['PrixBase']  . "';\n";
					}
					echo "document.info.TVA.value = '" . $ligne['TVA']  . "';\n";
					echo "document.info.GM.value = '" . $ligne['GM'] . "';\n";
					if ($mode == 1) {
						if ($ligne2 = Article::getArticleByNoArticle(addslashes($ligne['NoArticle']))) {
							echo "document.info.Rabais.value = '-" . $ligne2['Rabais'] . "';\n";
						} else {
							echo "document.info.Rabais.value = '" . $ligne['Rabais'] . "';\n";
						}
					} else {
						echo "document.info.Rabais.value = '" . $ligne['Rabais'] . "';\n";
					}
					echo "document.info.Quantite.value = '" . $ligne['Quantite'] . "';\n";
					if ($mode == 1) {
						echo "document.info.Destinataire.value = '" . addslashes($Dest) . "';\n";
					}
					echo "add_new_newfact(false);\n";
					break;
				case 1:
					if ($mode == 2) {
						if ($prevType == 1) {
							$multiline .= "\\n" . addslashes($ligne['Description']);
						} else {
							$multiline = addslashes($ligne['Description']);
						}
					}
					break;
				case 2:
					echo "tot_inter_newfact('" . addslashes($ligne['Description']) . "');\n";
					break;
				case 5:
					if ($mode == 2) {
						echo "tot_partiel_newfact('" . addslashes($ligne['Description']) . "');\n";
					}
					break;
				case 6:
					if ($mode == 2) {
						if ($prevType == 6) {
							$multiline .= "\\n" . addslashes($ligne['Description']);
						} else {
							$multiline = addslashes($ligne['Description']);
						}
					}
					break;
				case 7:
					if ($mode == 2) {
						echo "tot_partiel2_newfact('" . addslashes($ligne['Description']) . "');\n";
					}
					break;
			}
			$prevType = $ligne['TypeData'];
		}
		if ($mode == 2 && $prevType == 1) {
			echo "$('.ta_texte_libre').val('" . $multiline . "');\n";
			echo "add_new_text_newfact();\n";
		}
		if ($mode == 2 && $prevType == 6) {
			echo "$('.ta_texte_libre').val('" . $multiline . "');\n";
			echo "add_new_text_newfact(true);\n";
		}
	}
}
