#!/usr/bin/perl -w

use strict;
use SuperObject;
use clients;
use HTML::Template;


sub usage {
    print "usage:\n
$0 -h : ceci
";
} 



#===== mieux que getopts
sub getArgs {
    my @args=@_;
    my $oldKey="";
    my %opts=();
    for my $el (@args) {
		if ($el=~/^-/) {
			$el=~s/-+//g;
			$opts{$el}="";
			$oldKey=$el;
		}
		else {
			$opts{$oldKey}=$el;
		}
    }
    return %opts;
}


sub round{
	my($l_arg1,$l_scale)=@_;
	my $sign=1;
	if ($l_arg1<0) { $l_arg1=-$l_arg1; $sign=-1 ;}
	return 0 if ($l_arg1 == 0);
	my($l_num1,$l_num2) = 0;
	$l_num1 = 10**$l_scale * $l_arg1;
	$l_num2 = int($l_num1);
	$l_num2 = $l_num2 +1 if ($l_num1 - $l_num2 >= 0.5);
	$l_num1 =  $l_num2/10**$l_scale;
	return $l_num1*$sign; 
}


#------------------------
#   Gestion des entrées :
my %opts=();
my $debug=1;
my ($name,$output);
my $id;
my $typeDoc='html';
%opts=getArgs(@ARGV);

if((exists $opts{h}) || (exists $opts{help})) {
    &usage();
    exit 0;
}

if (exists $opts{o}) {
    $output=$opts{o};
} else {
    $output="";
}

if (exists $opts{id}) {
    $id=$opts{id};
}

if (exists $opts{t} && $opts{t}=~/(html)|(csv)|(cgi)/) {
    $typeDoc=$opts{t};
}

if (exists $opts{name}) {
    $name=$opts{name};
}

#--- Gestion output ----
if ($output) {
    open(OUT,">$output");
    *STDOUT=*OUT;
}
if ($typeDoc eq 'cgi') {
	print "Content-type:text/html\n\n";
	$typeDoc='html';
}
my $template=HTML::Template->new(filename=>'../template/listeChq.'.$typeDoc,die_on_bad_params=>0);
#--------------------------|


my ($DBHOST,$DBNAME,$DBUSER,$DBPWD)=qw(localhost pimengest pimengest pimengest);
'SuperObject'->initConnect($DBHOST,$DBNAME,$DBUSER,$DBPWD) || die 'Can t connect !';;

my @liste;
my $compte_TVA=new compte;
my $client=new client;
my $achat=new achat;
my $salaire=new salaire;
my $charge=new charges;
my $inter=new intervenant;
my $ecriture=new ecriture;
my $mvbq=new mouvement_banque;
my $ref_lmvbq;

$ref_lmvbq=$mvbq->map('libelle','uid','where libelle like \'CHEQUE %\'');
$compte_TVA->map('uid','intitule',"where id='445510'");
my $uidTVA;
{
	my @toto= keys(%{$compte_TVA->{map}});
	$uidTVA=$toto[0];
}

#my $uidTVA={keys(%{$compte_TVA->{map}})}[0];
#print STDERR $compte_TVA->{map};
$inter->map('uid','nom || \' \' || prenom');
my $ref_lecrit=$ecriture->map('intitule','uid',"where intitule like 'PAIEMENT%' and compte=$uidTVA");

while (my ($intitule,$uid)= each(%$ref_lecrit)) {
	$ecriture->loadByUid($uid);
	my $nochq;
	($nochq)=($intitule=~/PAIEMENT TVA chq no (\d+)/);
	if ($nochq) {
		push @liste,
		{
			abstract => 'Paiement TVA chq '.$nochq,
			datepaye => $ecriture->{date},
			montant  => $ecriture->{valeur},
			nocheque => $nochq
			};
	}
}


my $lach = $achat->map('nocheque','uid');

while (my ($no,$uid) = each (%$lach)) {
	my $tmpAch=new achat;
	$tmpAch->loadByUid($uid);
	$tmpAch->{montant}=$tmpAch->{montantht};
	print STDERR "No : $no ="
		."\tAbstr:".$tmpAch->{abstract}
	."\tDate:".$tmpAch->{datepaye}
	."\n" if ($no && $debug);
	push @liste,$tmpAch if ($no);
}

$lach = $charge->map('cheque','uid');

while (my ($no,$uid) = each (%$lach)) {
	my $tmpChar=new charges;
	my $tmpOrg=new organisme;
	$tmpChar->loadByUid($uid);
	undef($tmpChar->{email});
	$tmpOrg->loadByUid($tmpChar->{organisme});
	$tmpChar->{abstract}.= ' - '.$tmpOrg->{nom};
	$tmpChar->{nocheque}=$no;
	print STDERR 
		"No : $no =".$tmpChar->{abstract}
	."\n" if ($no && $debug);
	push @liste,$tmpChar if ($no);
}

$lach = $salaire->map('uid','comment');

while (my ($uid,$comment) = each (%$lach)) {
	next if (!$comment);
	my $tmpChar=new salaire;
	$tmpChar->loadByUid($uid);
	$tmpChar->{abstract}="salaire ".$inter->{map}{$tmpChar->{intervenant}};
	$tmpChar->{montant}=$tmpChar->{net};
	$tmpChar->{datepaye}="28/".$tmpChar->{mois}."/".$tmpChar->{annee};
	print STDERR $comment.":";
	($comment)=($comment=~/^chq ([0-9]*).*/);
	print STDERR $comment."\n";
	$tmpChar->{nocheque}=$comment;
	print STDERR 
		"No : $comment =".$tmpChar->{abstract}
	."\n" if ($comment && $debug);
	push @liste,$tmpChar if ($comment);
}

my $exNo=0;
@liste = sort {$a->{nocheque} cmp $b->{nocheque}} @liste;
my @tmpListe; # on merge après avec @liste
for my $l (@liste) {
	if ($debug) {
		my $n;
		$n=$l->{nocheque}-$exNo;
		#print STDERR "check for $exNo : $n\n";
		if (($n != 1) && ($n < 10)) {
			my $nochq=$exNo+1;
			#print STDERR "\n on check pour $nochq : CHEQUE $nochq\n";
			#print STDERR "alors que pour CHEQUE ".($nochq-1)." on a : ".$ref_lmvbq->{'CHEQUE '.($nochq-1).' '}."\n";
			if (exists $ref_lmvbq->{'CHEQUE '.$nochq.' '}) {
				$mvbq->loadByUid($ref_lmvbq->{'CHEQUE '.$nochq.' '});
				print STDERR "\n : je pousse \n";
				push @tmpListe,
				{
					abstract => $mvbq->{libelle},
					datepaye => $mvbq->{date},
					montant  => $mvbq->{'montant_euro'},
					nocheque => $nochq
					};
			}
			else {
				print STDERR "manque $nochq \n";
			}
		}
	}
	$exNo=$l->{nocheque};
}
push @liste,@tmpListe;
@liste = sort {$a->{nocheque} cmp $b->{nocheque}} @liste;
$template->param(loop=>\@liste);
print $template->output();

