<?php
/* Debug: Show supplier data for a product */

if (!defined('NOREQUIREMENU')) define('NOREQUIREMENU', '1');
if (!defined('NOREQUIREHTML')) define('NOREQUIREHTML', '1');
if (!defined('NOREQUIREAJAX')) define('NOREQUIREAJAX', '1');

$res = 0;
if (!$res && file_exists("../../main.inc.php")) $res = @include "../../main.inc.php";
if (!$res && file_exists("../../../main.inc.php")) $res = @include "../../../main.inc.php";
if (!$res) die("Dolibarr nicht geladen");

require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';

header('Content-Type: text/plain; charset=utf-8');

$barcode = GETPOST('barcode', 'alphanohtml');
if (empty($barcode)) {
	echo "Bitte ?barcode=XXX angeben\n";
	exit;
}

// Find product
$product = new Product($db);
$sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."product WHERE barcode = '".$db->escape($barcode)."'";
$resql = $db->query($sql);
if ($resql && $db->num_rows($resql) > 0) {
	$obj = $db->fetch_object($resql);
	$product->fetch($obj->rowid);
} else {
	echo "Produkt nicht gefunden fuer Barcode: $barcode\n";
	exit;
}

echo "=== PRODUKT ===\n";
echo "ID: ".$product->id."\n";
echo "Ref: ".$product->ref."\n";
echo "Label: ".$product->label."\n";
echo "\n";

// Get supplier prices
$productFourn = new ProductFournisseur($db);
$suppliers = $productFourn->list_product_fournisseur_price($product->id);

echo "=== LIEFERANTEN ===\n";
if ($suppliers) {
	foreach ($suppliers as $i => $s) {
		echo "\n--- Lieferant ".($i+1)." ---\n";
		echo "fourn_id: ".$s->fourn_id."\n";
		echo "fourn_name: ".$s->fourn_name."\n";
		echo "ref_fourn (ref_supplier): ".$s->ref_supplier."\n";
		echo "fourn_ref: ".$s->fourn_ref."\n";
		echo "fourn_price: ".$s->fourn_price."\n";

		// Get societe URL
		$sqlSoc = "SELECT rowid, nom, url FROM ".MAIN_DB_PREFIX."societe WHERE rowid = ".(int)$s->fourn_id;
		$resSoc = $db->query($sqlSoc);
		$soc = $db->fetch_object($resSoc);
		echo "societe.nom: ".$soc->nom."\n";
		echo "societe.url: '".$soc->url."'\n";
	}
} else {
	echo "Keine Lieferanten gefunden\n";
}
