#!/usr/bin/perl

# add.pl : ajoute des références au bon de commande de l'utilisateur.
# entrées: cod-$i, qty-$i et com-$i : le code, la quantité et si il faut ou non commander
#		la référence.
# note: l'ordre des paramètres est important. Le code doit etre en premier.
# note: si com-$i ne vaut pas 'on', il n'existe pas.
use CGI;
use CGI::Carp qw(fatalsToBrowser); # redéfinit die()
use DBI;

require "./look.pm";
require "./data.pm";

$user = 'greywolf';	# utilisateur de la base de donnée
$pass = '';		# mot de passe de la base de donnée
$server = 'localhost';	# Et machine de la base.
#$data_source = 'dbi:Pg:dbname=mafma';
$data_source = 'dbi:mSQL:mafma';

$EURO = 6.55957;

$query = new CGI;

&init;

@pairs = split( /&/, $query->query_string );	
foreach $pair(@pairs) {
	my($name,$value) = split( /=/, $pair );
	$name  =~ tr/+/ /;
	$name  =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
	
	my($param,$code) = split( /-/, $name );
	if ($param eq "cod") { push @codes, $value; push @commandes, 0; } 
	if ($param eq "qty") { push @quantites, $value; }
	if ($param eq "com") { pop @commandes; push @commandes, 1; }
}

&html_begin;

print "<center>\n";
print "<form action=\"/cgi-bin/order.pl\" method=\"get\">\n";

print "<h2>Vous voulez commander les r&eacute;f&eacute;rences suivantes :</h2>\n";
print "<p>\n";
print "<table bgcolor=\"blanchedalmond\" border>\n";
print "<tr><td>code</td><td>echelle</td><td>type</td><td>fabricant</td>";
print "<td>Marque / Brand</td><td>description</td><td>Prix unitaire</td>";
print "<td>Qt&eacute;</td><td>Prix total</tr>\n";

$QUERY_GETDATA  = "select products.echelle,products.type,fabs.des,brands.des,";
$QUERY_GETDATA .= "products.des,products.price ";
$QUERY_GETDATA .= "from products,fabs,brands ";
$QUERY_GETDATA .= "where products.code=? and products.fab=fabs.code and products.brand=brands.code";
$sth = $dbh->prepare( $QUERY_GETDATA ) || die $dbh->errstr;
$total = 0;
while (@codes) {
	$cod = shift(@codes);
	$com = shift(@commandes);
	$qty = shift(@quantites);

	if ($com) {
		$qty = &data_normalize_qty($qty);
		$rv = $sth->execute( $cod ) || die $dbh->errstr;
		($ech,$typ,$fab,$bra,$des,$pri) = $sth->fetchrow_array;
		$francs = sprintf "%2.2f", $pri;
		$sousto = sprintf "%2.2f", $pri*$qty;	# Sous total HT
		$sn = &data_scale($ech);
		$tn = &data_type($typ);
		print "<tr>\n";
		print "<td>$cod</td><td>$sn</td><td>$tn</td>\n";
		print "<td>$fab</td><td>$bra</td><td>$des</td><td align=right>$francs</td>\n";
		print "<td align=right><input type=text name=qty-$cod value=\"$qty\" size=2></td>\n";
		print "<td align=right>$sousto</td>\n";
		print "</tr>\n";
		$total += $pri*$qty;
	}
}
$tot = sprintf "%2.2f FF<br>%2.2f Euros", $total, $total/$EURO;
print "<tr><td colspan=8 align=right>Total</td><td>$tot</td></tr>\n";
print "</table>\n";

print << "EOP";

<p>Entrez vos r&eacute;f&eacute;rences
<p>Votre nom:
<p><input type="text" name="nam-1">
<p>Adresse de livraison:
<p><input type="text" name="adr-1">
<p>Pays:
<p><input type="text" name="cou-1">
<p>E-mail:
<p><input type="text" name="mai-1">

<p><input type="submit" value="Envoyer / Send">
EOP

print "</form>\n";
print "</center>\n";
&done;

sub init {
	# Connection à la base
	$dbh = DBI->connect( $data_source, $user, $pass ) || die $DBI::errstr; 
	print "Content-type: text/html\n\n";
	print "<html>\n";
	print "<head><title>Done</title></head>\n";
	print "<body bgcolor=white>\n";
}

sub done {
	$dbh->disconnect;
	print "</body>\n";
	print "</html>";
	exit;
}
