#!/usr/bin/perl -w

# BASE*(1+0.21)- BASE*0.19 = FINAL
# BASE + BASE * 0.21 - BASE * 0.19 = FINAL
# BASE * ( 1 + 0.21 - 0.19 ) = FINAL
# BASE * 1.02 = FINAL



# FINAL / (1-0.19) = BASE+IVA    # NO 
# BASE+IVA / (1.21) = BASE       # NO

if (@ARGV == 0) {
    die "$0 <IMPORTE FINAL>";
}elsif (0+$ARGV[0] ne $ARGV[0]){
    die "El importe debe ser un número";
}else{
    $FINAL = fromcents(tocents($ARGV[0]));
#    $BASE0 = $FINAL / 1.02;
    $BASE = fromcents(tocents( $FINAL / 1.02 ));
    $IVA = fromcents(tocents( $BASE * 0.21 ));
    $RETENCION = fromcents(tocents( $BASE * -0.19)) ;
#    $BASEZ = fromcents(tocents($FINAL)-tocents($RETENCION)-tocents($IVA));
    print "BASE     \t\t$BASE\n";
    print "IVA      \t\t$IVA\n";
#    print "TOTAL    \t\t$BASEMASIVA\n";
    print "RETENCIÓN\t\t$RETENCION\n";
    print "FINAL    \t\t$FINAL\n";    
}
exit 0;

sub tocents {
    my $cant = shift;
    $cant *= 100;
    return int ($cant);
}

sub fromcents {
    my $cant = shift;
    $cant =~ s/(\d\d)$/\.$1/;
    return $cant;
}


