#!/bin/sh
###
### nanoMAG
###
### Copyright (C) 2005 Daniele Giacomini daniele@swlibero.org
###                                      daniele.giacomini@poste.it
###
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
###
### This program is distributed in the hope that it will be useful, but
### WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
### General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
###
##
## nmag DB_FILE
##
#
# Temporary file.
#
TEMPORARY=`tempfile`
touch $TEMPORARY
#
# Command line arguments.
#
PROGRAM_NAME="$0"
DB_FILE="$1"
PROGRAM_DIR=`dirname $PROGRAM_NAME`
PROGRAM_EXE=`basename $PROGRAM_NAME`
#
# End of work.
#
end_of_work () {
    #
    rm -f $TEMPORARY
    #
    exit
}
#
# Help.
#
help_message () {
    #
    echo "  nmag DB_FILE"
    #
    # See what was typed.
    #
    echo ""
    echo "You wrote:"
    echo "$PROGRAM_NAME $DB_FILE"
}
#
# Main menu.
#
main_menu () {
    #
    # Menu selection.
    #
    local SELECTION=""
    #
    #
    #
    while dialog   \
        --clear \
        --title "nanoMAG" \
        --menu "nanoMAG menu\n" \
        0 0 0 \
        "inserimento comandi"     "Accesso alla riga di comando"  \
        "listato articoli"        "Stampa degli articoli di magazzino"  \
        "listato causali"         "Stampa delle causali di magazzino"  \
        "listato clienti"         "Stampa dei clienti"  \
        "listato fornitori"       "Stampa dei fornitori"  \
        "listato movimenti"       "Stampa dei movimenti"  \
        "listato mov dettagliato" "Stampa dettagliata movimenti"  \
        "listato situazione"      "Stampa della situazione del magazzino"  \
        "applica costo medio"     "Costo medio agli scarichi indeterminati" \
        "fine lavoro"             "Conclusione"         \
        2> $TEMPORARY
    do
        SELECTION=`cat $TEMPORARY`
        echo "" > "$TEMPORARY"
        #
        # Do the selected work.
        #
        do_as_selected "$SELECTION"
    done
}
#
# Do the selected work.
#
do_as_selected () {
    #
    SELECTION="$1"
    #
    if   [ "$SELECTION" = "fine lavoro" ]
    then
        end_of_work
    elif [ "$SELECTION" = "inserimento comandi" ]
    then
        sqlite3 -nullvalue "NULL" -header -column "$DB_FILE"
    elif [ "$SELECTION" = "listato articoli" ]
    then
        select_to_print "$DB_FILE" "Listato_articoli" 100
    elif [ "$SELECTION" = "listato causali" ]
    then
        select_to_print "$DB_FILE" "Listato_causali" 100
    elif [ "$SELECTION" = "listato clienti" ]
    then
        select_to_print "$DB_FILE" "Listato_clienti" 200
    elif [ "$SELECTION" = "listato fornitori" ]
    then
        select_to_print "$DB_FILE" "Listato_fornitori" 200
    elif [ "$SELECTION" = "listato movimenti" ]
    then
        select_to_print "$DB_FILE" "Listato_movimenti" 100
    elif [ "$SELECTION" = "listato mov dettagliato" ]
    then
        select_to_print "$DB_FILE" "Listato_movimenti_dettagliato" 180
    elif [ "$SELECTION" = "listato situazione" ]
    then
        select_to_print "$DB_FILE" "Listato_situazione" 80
    elif [ "$SELECTION" = "applica costo medio" ]
    then
	#
	ARTICOLO=0
	COSTO_MEDIO=0
	#
	for ARTICOLO in `echo "SELECT articolo FROM Articoli;" | sqlite3 "$DB_FILE"`
	do
	    COSTO_MEDIO=`echo "SELECT costo_medio FROM Situazione_magazzino WHERE articolo = $ARTICOLO;" | sqlite3 "$DB_FILE"`
	    if [ ! "$COSTO_MEDIO" = "" ]
	    then
		echo "[$0] articolo $ARTICOLO; costo medio $COSTO_MEDIO"
	        (echo "UPDATE Movimenti"
	         echo "       SET valore = ($COSTO_MEDIO * quantita)"
	         echo "       WHERE articolo = $ARTICOLO AND valore IS NULL;") \
		 |  sqlite3 "$DB_FILE"
		sleep 1
	    fi
	done
	sleep 1
    fi
}
#
# Select to print.
#
select_to_print () {
    #
    local DB_FILE="$1"
    local VIEW="$2"
    local WIDTH="$3"
    local DATE=`date`
    local FONT_W=0
    local FONT_H=0
    #
    echo "SELECT * FROM $VIEW;" \
         | sqlite3 -nullvalue "NULL" -header -column "$DB_FILE" \
         > "$TEMPORARY.print"
    #
    dialog --title "$VIEW" --textbox "$TEMPORARY.print" 0 0
    #
    #
    if dialog                               \
        --clear                             \
        --title "$VIEW"  \
        --yesno "Stampo?\n" \
        0 0                                 \
        2> $TEMPORARY
    then
        echo "" > "$TEMPORARY"
        #
        # Font calculation.
        #
        FONT_W=`echo "1140 / $WIDTH"  | bc -l`
        FONT_H=`echo "$FONT_W * 1.5" | bc -l`
        #
        (echo "Stampato da $USER il giorno $DATE";
         cat "$TEMPORARY.print") \
            | /usr/bin/enscript -1 \
                                -M a4 \
                                -r \
                                -f Courier@$FONT_W/$FONT_H -B \
                                --margin=72:72:72:72 \
                                -o - | lpr
    fi
    #
    # Remove extra temporary file.
    #
    rm -f "$TEMPORARY.print"
    #
}
##
##
## Start of program.
##
##
#
# Check arguments.
#
if   [ $# = 1 ] && [ -f "$DB_FILE" ]
then
    #
    # Ok.
    #
    main_menu
    #
elif [ $# = 1 ]
then
    #
    # Try to create the database.
    #
    sqlite3 "$DB_FILE" < $PROGRAM_DIR/$PROGRAM_EXE.sql
    if   [ -f "$DB_FILE" ]
    then
        main_menu
    fi
else
    #
    #
    #
    help_message
fi
#
#
#
end_of_work
#
