Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added script bksystem.sh for billing. incl doc

  • Loading branch information...
commit 6f2852c6036fe6fcc4c6dc177828c42f3a35f8ef 1 parent 2ad370c
@LeBlue LeBlue authored
View
405 bksystem.sh
@@ -0,0 +1,405 @@
+#!/bin/sh
+# bksystem admin script.
+# Structure of this file:
+# Help message
+# Config: paths, filenames, enabled scripts
+#
+# setup logfile
+# get command line options
+# set/test DB
+# set date
+# set other paths/filenames
+# enable/disable scripts
+# exec scripts
+# cleanup
+
+ VERSION=0.9
+
+ help_message()
+ {
+ echo "Usage:"
+ echo " ${0} [options] [-f DBNAME]"
+ echo " ${0} [options]"
+ echo "Functions: "
+ echo " set environment for all bksystem scripts: generate bill, export, backup, cleanup "
+ echo ""
+ echo "Options:"
+ echo ""
+ echo " -f : use DBNAME as database"
+ echo " e.g. -f myDB.sqlite"
+ echo " -l : send output to stdout (console) and NOT logfile"
+ echo " all info commands imply this behavior"
+ echo ""
+ echo " -a : exec all configured functions, overwrites other options"
+ echo " -b : enable backup, not with -r"
+ echo " -e : enable export, not with -r"
+ echo " -i : enable insert reference date for bill, not with -r"
+ echo " -r : restore database from backup (old will not be overwritten), performs no other action"
+ echo ""
+ echo " -s : show set variables and exit"
+ echo " tmp variables will be set to default, unless you set them first:"
+ echo " e.g. export BKS_BILL_DIR=/home/user/Path/to/bills"
+ echo " -c : show config and exit"
+ echo " config can be set in $pwd/${0}"
+ echo " -d : show default variables and exit"
+ echo " default variables: can be set in ${0}"
+ echo " usabale for defining tmp variables"
+ echo " e.g. export BKS_TMP_DIR=\${DEF_ROOT_DIR}/mytmp"
+ echo ""
+ echo " -h : show this help and exit"
+ echo " uses same path and name as reading a DB"
+ echo " -v : display version and exit"
+ exit 0;
+ }
+
+# Configuration
+
+ # Default variables, should be set while install
+
+ # adjust if necessary (for crontab) or permanent links
+ # for temorary links overwrite with export (all BKS_* will be set to $DEF_* unless already set)
+ DEF_ROOT_DIR="${HOME}/Projekte/bksystem/build"
+ # Database
+ DEF_DB_DIR="${DEF_ROOT_DIR}/build"
+ DEF_DB_NAME="bksystem.sqlite"
+ # Scripts are located here
+ DEF_SCR_DIR="${DEF_ROOT_DIR}/scripts"
+ # bills
+ DEF_ADD_DATE_BILL_DIR=1 # USE subdir and add the current date to the BILL_SUB_DIR_NAME: 0 disabled/ 1 enabled
+ # get something like DEF_ROOT_DIR/DEF_BILL_DIR_NAME/DEF_SUB_DIR_NAME_DATE/file.csv
+ #
+ DEF_BILL_SUB_DIR_NAME="Abrechnungen" #create a subdir for each bill: disable subdir with "."
+ DEF_BILL_DIR="${DEF_ROOT_DIR}/build/bills"
+ # temporary dir
+ DEF_TMP_DIR="${DEF_ROOT_DIR}/build/tmp"
+ # here will be the log file
+ DEF_LOG_DIR="${DEF_ROOT_DIR}/build/log" # send to console with -c option
+ DEF_LOG_FILE="bill_script.log"
+ # here will be the backup
+ DEF_BKP_DIR="${DEF_ROOT_DIR}/build/bkp"
+
+ # enable/disable scripts permanently here, tmp use options
+ # enabled: VARNAME=1
+ # disabled VARNAME=0
+
+ FIXBILLDATE=1
+ EXPSIMPLE=1
+ EXPCOMPLCOMB=1
+ EXPCOMPLSEP=0
+ EXPCOMPLHTML=1
+ BACKUP=0
+ CLEAN=0 # TODO
+ #always disabled, enable with -r switch
+ RESTORE=0
+
+ # more messages
+ show_default_variables()
+ {
+ echo "default variables: can be set in ${0}"
+ echo "usabale for defining tmp variables"
+ echo "e.g. export BKS_TMP_DIR=\${DEF_ROOT_DIR}/mytmp"
+ echo " DEF_ROOT_DIR=${DEF_ROOT_DIR}"
+ echo " DEF_DATE=${DEF_DATE} (now or last inserted date!)"
+ echo " DEF_DB_DIR=${DEF_DB_DIR}"
+ echo " DEF_DB_NAME=${DEF_DB_NAME}"
+ echo " DEF_BILL_DIR=${DEF_BILL_DIR}"
+ echo " DEF_TMP_DIR=${DEF_TMP_DIR}"
+ echo " DEF_LOG_DIR=${DEF_LOG_DIR}"
+ echo " DEF_LOG_FILE=${DEF_LOG_FILE}"
+ echo " DEF_BKP_DIR=${DEF_BKP_DIR}"
+ echo " DEF_SCR_DIR=${DEF_SCR_DIR}"
+ }
+
+ show_set_variables()
+ {
+ echo "These tmp variables will be set to default, unless you set them:"
+ echo " BKS_DATE=${BKS_DATE}"
+ echo " BKS_TMP_DIR=${BKS_TMP_DIR}"
+ echo " BKS_BILL_DIR=${BKS_BILL_DIR}"
+ echo " BKS_LOG_DIR=${BKS_LOG_DIR}"
+ echo " BKS_LOG_FILE=${BKS_LOG_FILE}"
+ echo " BKS_BKP_DIR=${BKS_BKP_DIR}"
+ echo " BKS_DB_DIR=${BKS_DB_DIR}"
+ echo " BKS_DB_NAME=${BKS_DB_NAME}"
+ echo " BKS_SCR_DIR=${BKS_SCR_DIR}"
+ }
+
+ show_conf()
+ {
+ echo "Following tasks are enabled=0, disabled=1"
+ echo " to set, edit ${0}"
+ echo " insert_bill_reference_date.sh : ${FIXBILLDATE}"
+ echo " export_bill_simple.sh: ${EXPSIMPLE}"
+ echo " export_bill_list_comb: ${EXPCOMPLCOMB}"
+ echo " export_bill_list_sep : ${EXPCOMPLSEP}"
+ echo " export_bill_list_html: ${EXPCOMPLHTML}"
+ echo " backup.sh : ${BACKUP}"
+ echo " cleanup.sh : ${CLEAN}"
+ echo " restore_db.sh : ${RESTORE}"
+ }
+
+ # dont edit this, default date is current date
+ DEF_DATE=$(date +"%0Y-%0m-%0d_%0k:%0M:%0S")
+
+ # check for help
+ case $1 in
+ -h | -help | --h | --help | -usage | --usage | usage | help | h) help_message ;;
+ esac
+
+# setup logfile/output
+
+ if [ -z $BKS_LOG_DIR ]; then
+ BKS_LOG_DIR=$DEF_LOG_DIR
+ fi
+ if [ -z $BKS_LOG_FILE ]; then
+ BKS_LOG_FILE=$DEF_LOG_FILE
+ fi
+ #if [ -z $BKS_LOG ]; then
+ #BKS_LOG="${DEF_LOG_DIR}/${BKS_DATE}_${BKS_LOG_FILE}"
+ BKS_LOG="${BKS_LOG_DIR}/${BKS_LOG_FILE}"
+ BKS_TMP_LOG="${BKS_LOG_DIR}/${BKS_LOG_FILE}_tmp"
+ #fi
+ if ! [ -d $BKS_LOG_DIR ]; then
+ mkdir $BKS_LOG_DIR
+ fi
+
+ echo "LOG on scripted bill generation for bksystem:" > $BKS_TMP_LOG 2>&1
+ echo "${0} ${@}, version: ${VERSION}" >> $BKS_TMP_LOG 2>&1
+ echo "${DEF_DATE}" >> $BKS_TMP_LOG 2>&1
+
+ # test sqlite3 command exists
+ command -v $PROGS > /dev/null || { echo "ERROR: tee not found. Please install it. Aborting!" >> $BKS_TMP_LOG 2>&1; echo "ERROR: tee not found. Please install it. Aborting!"; mv $BKS_TMP_LOG $BKS_LOG 2>/dev/null; exit 1; }
+ for PROGS in sqlite3 getopts; do
+ command -v $PROGS > /dev/null || { echo "ERROR: ${PROGS} not found. Please install it. Aborting!" 2>&1 | tee -a $BKS_TMP_LOG; mv $BKS_TMP_LOG $BKS_LOG 2>/dev/null; exit 1; }
+ done
+
+ # clear these, used to remember chosen options
+ ENEXP=0
+ ENBKP=0
+ ENINS=0
+ ENINSERT=0
+ EXECALL=0
+ DB=
+ OPTION_SELECTED=0
+ NOLOG=0
+
+# get commandline args
+ while getopts abcdef:hilrsv opt
+ do
+ case $opt in
+ a) OPTION_SELECTED=1; EXECALL=1 ;;
+ b) OPTION_SELECTED=1; ENBKP=1 ;;
+ c) OPTION_SELECTED=1; show_conf; NOLOG=1 ; exit 0;;
+ d) OPTION_SELECTED=1; show_default_variables; BKS_LOG=/dev/null ; exit 0;;
+ e) OPTION_SELECTED=1; ENEXP=1 ;;
+ f) OPTION_SELECTED=1; DB=$OPTARG; DBARG="1" ;;
+ h) OPTION_SELECTED=1; help_message;;
+ i) OPTION_SELECTED=1; ENINSERT=1 ;;
+ l) OPTION_SELECTED=1; NOLOG=1 ;;
+ s) OPTION_SELECTED=1; show_set_variables;NOLOG=1 ; exit 0;;
+ r) OPTION_SELECTED=1; RESTORE=1 ;;
+ v) OPTION_SELECTED=1; echo "version: ${VERSION}"; NOLOG=1 ; exit 0;;
+ *) echo "unrecognized option";BKS_LOG=/dev/null ; help_message; exit 0;;
+ esac
+ done
+ # if logfile disabled
+ if ! [ $NOLOG -eq 1 ]; then
+ mv $BKS_TMP_LOG $BKS_LOG
+ else
+ rm $BKS_TMP_LOG
+ $BKS_LOG=/dev/null
+ fi
+ # set all path and name variables that are not yet defined to default
+ # some need the date current date OR the last inserted date, therefore init DB and log first, then get date, init rest.
+
+
+# set DB to use
+ # set DB vars to default if not set
+ if [ -z $BKS_DB_DIR ]; then
+ BKS_DB_DIR=$DEF_DB_DIR
+ fi
+ if [ -z $BKS_DB_NAME ]; then
+ BKS_DB_NAME=$DEF_DB_NAME
+ fi
+ # set DB from commandline
+ if [ -z $DB ]; then
+ DB="${BKS_DB_DIR}/${BKS_DB_NAME}"
+ else
+ echo "option: DB from -f argument" 2>&1 | tee -a $BKS_LOG
+ fi
+ # test DB exist
+ if [ -e $DB ]; then
+ echo "Database:" 2>&1 | tee -a $BKS_LOG
+ echo $DB 2>&1 | tee -a $BKS_LOG
+ else
+ if [ 0 -eq $RESTORE ]; then
+ echo "ERROR: Database ${DB} not found. Please adjust settings in ${SCR_DIR}/${0}. Aborting!" 2>&1 | tee -a $BKS_LOG
+ exit 1
+ else
+ echo "DB not existent: restoring..." 2>&1 | tee -a $BKS_LOG
+ fi
+ fi
+ export DB
+
+# set date
+ # now get current date to insert or read from DB
+ if ! [ $RESTORE -eq 1 ]; then
+ if ([ $ENINSERT -eq 1 ] || [ $FIXBILLDATE -eq 1 ]); then
+ # declared DATE via EXPORT
+ if [ -z $BKS_DATE ]; then
+ BKS_DATE=$DEF_DATE
+ fi
+ else
+ # get last date, if we dont insert current date (make a bill now)
+ # replace space with _ and remove "
+ BKS_DATE=$(sqlite3 -csv database/bksystem.sqlite "SELECT max(timestamp) FROM bill_dates;" | sed 's/\ /_/g' | sed 's/\"//g')
+ fi
+ else
+ BKS_DATE=$DEF_DATE
+ fi
+ export BKS_DATE
+
+#set directories and paths
+ # Scripts are located here
+ if [ -z $BKS_SCR_DIR ]; then
+ export BKS_SCR_DIR=$DEF_SCR_DIR
+ fi
+
+ # Here go the generated/exported files
+ # complete bill path
+ if [ -z $BKS_BILL_DIR ]; then
+ BKS_BILL_DIR="${DEF_BILL_DIR}"
+ fi
+ if ! [ -d $BKS_BILL_DIR ]; then
+ mkdir $BKS_BILL_DIR
+ fi
+ # complete bill path + subdir
+ if [ 1 -eq $DEF_ADD_DATE_BILL_DIR ]; then
+ BKS_BILL_DIR="${BKS_BILL_DIR}/${DEF_BILL_SUB_DIR_NAME}_${BKS_DATE}"
+ if ! [ -d $BKS_BILL_DIR ]; then
+ mkdir $BKS_BILL_DIR
+ fi
+ fi
+ export BKS_BILL_DIR
+
+ # temporary dir
+ if [ -z $BKS_TMP_DIR ]; then
+ export BKS_TMP_DIR=$DEF_TMP_DIR
+ fi
+ if ! [ -d $BKS_TMP_DIR ]; then
+ mkdir $BKS_TMP_DIR
+ fi
+
+ # backup: here will be the backup
+ if [ -z $BKS_BKP_DIR ]; then
+ export BKS_BKP_DIR=$DEF_BKP_DIR
+ fi
+ if ! [ -d $BKS_BKP_DIR ]; then
+ mkdir $BKS_BKP_DIR
+ fi
+
+# enable/disable scripts
+ if [ 0 -eq $OPTION_SELECTED ]; then
+ echo "no OPTION selected: use ${0} -a to perform all following tasks:"
+ show_conf;
+ echo "or use ${0} -h to get help"
+ exit 1
+ fi
+
+ #log selected options
+ # and disable unselected actions
+ # restore will overwrite all -a , -e, -i, -b (all, export, insert, backup)
+ if [ 1 -eq $RESTORE ]; then
+ echo "OPTION: restore DB, will not perform other tasks!" 2>&1 | tee -a $BKS_LOG
+ else
+ # -a
+ if [ 1 -eq $EXECALL ]; then
+ echo "OPTION: performing all configured tasks"
+ RESTORE=0
+ else
+ FIXBILLDATE=0
+ EXPSIMPLE=0
+ EXPCOMPLCOMB=0
+ EXPCOMPLSEP=0
+ EXPCOMPLHTML=0
+ BACKUP=0
+ CLEAN=0
+ fi
+ # process enable options
+ if [ 1 -eq $ENINSERT ]; then
+ echo "OPTIONS: enable insert reference date" 2>&1 | tee -a $BKS_LOG
+ FIXBILLDATE=1
+ fi
+ if [ 1 -eq $ENEXP ]; then
+ echo "OPTION: enable export" 2>&1 | tee -a $BKS_LOG
+ EXPSIMPLE=1
+ EXPCOMPLCOMB=1
+ EXPCOMPLSEP=1
+ EXPCOMPLHTML=1
+ fi
+ if [ 1 -eq $ENBKP ]; then
+ echo "OPTION: enable backup" 2>&1 | tee -a $BKS_LOG
+ BACKUP=1
+ fi
+ show_set_variables 2>&1 | tee -a $BKS_LOG
+ fi
+ echo "Starting scripts from ${0}" 2>&1 | tee -a $BKS_LOG
+
+
+# Here are all the scripts
+ # to enable/disable scripts set variables at start of script
+ # saves return code of scripts, cause pipe tee destroys it.
+ export ERRCODE="${BKS_TMP_DIR}/errcode"
+
+ if ! [ 1 -eq $RESTORE ]; then
+ if [ 1 -eq $FIXBILLDATE ]; then
+ $BKS_SCR_DIR/insert_bill_reference_date_into_db.sh 2>&1 | tee -a $BKS_LOG
+ echo $REPLY
+ if ! [ $(cat $ERRCODE) = "0" ]; then
+ echo "ERROR: inserting date unsuccesful: insert_date_to_db.sh failed, ABORTING" | tee -a $BKS_LOG
+ echo "${DATE}" > "${BKS_BILL_DIR}/ERROR_README.txt"
+ echo "Invoked command ${0} ${@}, version ${VERSION}" >> "${BKS_BILL_DIR}/ERROR_README.txt"
+ echo "Error while inserting date, see ${BKS_LOG} for more Info" >> "${BKS_BILL_DIR}/ERROR_README.txt"
+ echo "Try command ${0} -i" >> "${BKS_BILL_DIR}/ERROR_README.txt"
+ exit 1;
+ fi
+ fi
+ # exports current_bill/consumption
+ if [ 1 -eq $EXPSIMPLE ]; then
+ $BKS_SCR_DIR/export_bill_basic.sh 2>&1 | tee -a $BKS_LOG
+ fi
+ # generate komplex bill X consumption pivot table and exports csv
+ if [ 1 -eq $EXPCOMPLCOMB ]; then
+ $BKS_SCR_DIR/export_bill_complete_comb.sh 2>&1 | tee -a $BKS_LOG
+ fi
+ # generate komplex bill X consumption pivot table and exports csv (seperate last/firstname)
+ if [ 1 -eq $EXPCOMPLSEP ]; then
+ $BKS_SCR_DIR/export_bill_complete_sep.sh 2>&1 | tee -a $BKS_LOG
+ fi
+ # generate komplex bill X consumption pivot table and exports csv
+ if [ 1 -eq $EXPCOMPLHTML ]; then
+ $BKS_SCR_DIR/export_bill_html.sh 2>&1 | tee -a $BKS_LOG
+ fi
+ # backups useraccounts and products
+ if [ 1 -eq $BACKUP ]; then
+ $BKS_SCR_DIR/backup.sh 2>&1 | tee -a $BKS_LOG
+ fi
+ # deletes unecessary sales
+ if [ 1 -eq $CLEAN ]; then
+ $BKS_SCR_DIR/delete_old_sales.sh 2>&1 | tee -a $BKS_LOG
+ fi
+ else
+ $BKS_SCR_DIR/restore_db.sh 2>&1 | tee -a $BKS_LOG
+ fi
+
+#cleanup
+
+ #if [ $1 ]; then
+ #remove errcode file
+ #rm $ERRCODE >/dev/null 2>&1
+ #remove old log (if not overwritten)
+
+ #remove tmp files (don't remove tmp, could be system tmp)
+ # rm $BKS_TMP_DIR/*
+ #fi
+ echo "All tasks completed!" 2>&1 | tee -a $BKS_LOG
View
128 doc/script_doc.html
@@ -0,0 +1,128 @@
+<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
+ <head>
+ <title>Dokumentation bksystem scripte</title>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+ </head>
+ <body>
+ <h1>Dokumentation bksystem script</h1>
+ <div id="toc_div">
+ <h3>Übersicht</h3>
+
+ <ol id="toc">
+ <li><a href=#general>allgemein</a></li>
+ <ul>
+ <li><a href=#shell>Shell</a></li>
+ <li><a href=#software>Software: sqlite3</a></li>
+ </ul>
+ <li><a href=#user_accounts>master script: all_bill_jobs.sh</a></li>
+ <ul>
+ <li><a href=#function_master>Function</a></li>
+ <li><a href=#config_paths>Pfade konfigurieren</a></li>
+ <li><a href=#active>einzelne script de/aktivieren</a></li>
+ <li><a href=#crontab>stand alone</a></li>
+ </ul>
+ <li><a href=#insert>Jetzt abrechnen: insert_bill_date.sh </a></li>
+ <ul>
+ <li><a href=#></a></li>
+ </ul>
+ <li><a href=#export>Export: export_bill_basic.sh</a></li>
+ <ul>
+ <li><a href=#></a></li>
+ </ul>
+ <li><a href=#pivot_table>pivot Tabelle generieren + export: export_bill_complete(_sep).sh</a></li>
+ <ul>
+ <li><a href=#></a></li>
+ </ul>
+ <li><a href=#cleanup>cleanup: export_bill_basic.sh</a></li>
+ <ul>
+ <li><a href=#></a></li>
+ </ul>
+ <li><a href=#backup>Backup: backup.sh und restore.sh</a></li>
+ <li><a href=#crontab>Crontab</a></li>
+ </ol>
+ </div>
+ <div id="content_div">
+ <ol id="content">
+ <h3 id="general"><li>allgemein</li></h3>
+ <ul>
+ <h4 id="shell"><li>Shell</li></h4>
+ <p>Es ist gedacht, dass alle Skripte regelmäßig automatisch mit Hilfe von <a href=#crontab title="zu Crontab springen">crontab</a> ausgeführt werden. </p>
+ <p><ul>
+ <li>BKS_DATE: Abrechnungsdatum</li>
+ <li>BKS_SCR_DIR: Verzeichnis der Skripte</li>
+ <li>BKS_DB_DIR: Pfad und Datenbankname</li>
+ <li>BKS_DB_NAME: Datenbankname</li>
+ <li>BKS_BILL_DIR: Pfad zum Verzeichnis für Abrechnungen</li>
+ <li>BKS_TMP_DIR: Pfad zum temporären Verzeichnis</li>
+ <li>BKS_BKP_DIR: Pfad zum Verzeichnis zum Backups schreiben oder lesen</li>
+ <li>BKS_LOG_DIR: Pfad zum Verzeichnis für log Datei</li>
+ <li>BKS_LOG_FILE:Log-Dateiname</li>
+ </ul></p>
+ <h4 id="software"><li>Software: sqlite3</li></h4>
+ <p>Alle Skripte benötigen das commandline tool sqlite3, um die Datenabfragen zu stellen.</p>
+ <p><a href="http://www.google.de/search?q=man+sqlite3&btnI=aufgutglück" title="Manual sqlite3 anzeigen">Manual sqlite3</a></p>
+ </ul>
+ <h3 id="all_jobs"><li>master script: bksystem.sh</li></h3>
+ <ul>
+ <h4 id="function_master"><li>Funktion</li></h4>
+ <p>In bksystem.sh sind alle Konfigurationen gespeichert. Das sind zum einen Defaultwerte für die Pfad und Dateiname. Zum anderen sind das die Funktionen, die automatisch, bzw. regelmäßig ausgeführt werden sollen. Des weiteren akzeptiert das script eine Reihe von Komandozeilen Optionen.</p>
+
+ <h4 id="config_paths"><li>Pfade konfigurieren</li></h4>
+ <p>Eigentlich sollte es reichen, nur das DEF_ROOT_DIR zu setzen e.g. /home/bierwart/bksystem. Alle anderen Pfade werden relativ dazu gesetzt</p>
+ <p><table border=1>
+ <tr><th>VARNAME</th> <th>Bedeutung</th> <th>initial Wert</th></tr>
+ <tr><th>DEF_ROOT_DIR</th> <th>Hauptverzeichnis</th><th>nicht gesetzt</th></tr>
+ <tr><th>DEF_DATE</th> <th>Abrechnungsdatum</th> <th>Setze auf "Jetzt"</th></tr>
+ <tr><th>DEF_SCR_DIR</th> <th>Verzeichnis der Skripte</th> <th>ROOT_DIR/scripts</th></tr>
+ <tr><th>DEF_DB_DIR</th> <th>Pfad zur Datenbank</th> <th>ROOT_DIR/</th></tr>
+ <tr><th>DEF_DB_NAME</th> <th>Datenbankname</th> <th>bksystem.sqlite</th></tr>
+ <tr><th>DEF_BILL_DIR</th> <th>Pfad zum Verzeichnis für Abrechnungen</th> <th>ROOT_DIR/bills</th></tr>
+ <tr><th>DEF_TMP_DIR</th> <th>Pfad zum temporären Verzeichnis</th> <th>ROOT_DIR/tmp</th></tr>
+ <tr><th>DEF_BKP_DIR</th> <th>Pfad zum Verzeichnis zum Backups schreiben oder lesen</th> <th>ROOT_DIR/bkp</th></tr>
+ <tr><th>DEF_LOG_DIR</th> <th>Pfad zum Verzeichnis für log Datei</th> <th>ROOT_DIR/log</th></tr>
+ <tr><th>DEF_LOG_FILE</th> <th>Log-Dateiname</th> <th>bill_script.log</th></tr>
+ </table></p>
+ <h4 id="active"><li>Konfiguration: einzelne scripte de/aktivieren</li></h4>
+ <p></p>
+ <h4 id="crontab"><li>stand alone</li></h4>
+ <h4 id="structure"><li>Aufbau</li></h4>
+ </ul>
+ <h3 id="subscripte">Sub-scripte</h3>
+ <p>Diese Scripte werden alle vom masterscript bksystem.sh aufgerufen. Es lassen sich aber auch alle in einer shell direkt ausführen. Dabei kann die Datenbank (evtl. mit Pfad) als erstes Argument angegeben werden. Alle Dateien werden dann ins/vom Arbeitsverzeichnis geschrieben/gelesen. Alternativ können folgende Pfadvariable gesetzt werden mit <a href="http://www.google.de/search?q=man+export&btnI=auf+gut+glück" title="Manual export">export</a>. Benutzte Variablen und dateien stehen bei den Skriptbeschreibungen.
+ <h3 id="insert"><li>Jetzt abrechnen: insert_bill_date.sh </li></h3>
+ <ul>
+ <h4 id="insert_func"><li>Funktion</li></h4>
+ <h4 id="insert_structure"><li>Aufbau</li></h4>
+ </ul>
+ <h3 id="export"><li>Export: export_bill_basic.sh</li></h3>
+ <p></p>
+ <ul>
+ <h4 id="export_func"><li>Funktion</li></h4>
+ <h4 id="export_structure"><li>Aufbau</li></h4>
+ </ul>
+ <h3 id="pivot_table"><li>pivot Tabelle generieren + export: export_bill_complete(_sep).sh</li></h3>
+ <ul>
+ <h4 id="pivot_table_func"><li>Fuinktion</li></h4>
+ <h4 id="pivot_table_structure"><li>Aufbau</li></h4>
+ </ul>
+ <h3 id="pivot_table_html"><li>pivot Tabelle generieren + export als html mit layout: export_bill_html.sh</li></h3>
+ <ul>
+ <h4 id="pivot_table_html_func"><li>Fuinktion</li></h4>
+ <h4 id="pivot_table_html_structure"><li>Aufbau</li></h4>
+ </ul>
+ <h3 id="cleanup"><li>cleanup: clean_old_sales.sh</li></h3>
+ <ul>
+ <h4 id="cleanup_func"><li>Funktionen</li></h4>
+ <h4 id="cleanup_structure"><li>Aufbau</li></h4>
+ </ul>
+ <h3 id="backup"><li>Backup: backup.sh und restore.sh</li></h3>
+ <ul>
+ <h4 id="backup_func"><li>Funktionen</li></h4>
+ <h4 id="backup_structure"><li>Aufbau</li></h4>
+ </ul>
+ <h3 id="crontab"><li>Crontab</li></h3>
+ </ol>
+
+ </div>
+ </body>
View
42 scripts/backup.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+#
+
+echo ""
+echo "Executing script file:"
+echo "${0}:"
+
+if [ -z $DB ]; then
+ DB="./bksystem.sqlite"
+fi
+
+if [ -z $BKS_BKP_DIR ]; then
+ BKS_BKP_DIR="."
+fi
+
+SCHEM="${BKS_BKP_DIR}/bksystem_schema1.sql"
+USER="${BKS_BKP_DIR}/user_accounts.sql"
+PROD="${BKS_BKP_DIR}/products.sql"
+SALE="${BKS_BKP_DIR}/sales.sql"
+BDATES="${BKS_BKP_DIR}/bill_dates.sql"
+
+sqlite3 -batch $DB ".dump products" > $SCHEM
+if [ $? -eq 0 ]; then
+ echo " ${SCHEM} written"
+fi
+sqlite3 -batch $DB ".dump user_accounts" > $USER
+if [ $? -eq 0 ]; then
+ echo " ${USER} written"
+fi
+sqlite3 -batch $DB ".dump products" > $PROD
+if [ $? -eq 0 ]; then
+ echo " ${PROD} written"
+fi
+sqlite3 -batch $DB ".dump sales" > $SALE
+if [ $? -eq 0 ]; then
+ echo " ${SALE} written"
+fi
+sqlite3 -batch $DB ".dump bill_dates" > $BDATES
+#sqlite3 -batch $DB "SELECT * FROM bill_dates WHERE bill_id != 0;" > $BDATES
+if [ $? -eq 0 ]; then
+ echo " ${BDATES} written"
+fi
View
65 scripts/export_bill_basic.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+# This script is intended to generate a bill from bksystem.sqlite database, export to csv.
+# 3 tables:
+# Account balance list
+# Consumption of products list
+# Consumption of each user list
+# Steps:
+# get current date and time
+# sqlite querry and export tables as .csv
+
+
+ echo ""
+ echo "Executing script file:"
+ echo "${0}:"
+
+# DB and date should be defined, but if not:
+ if [ -z $DB ]; then
+ DB="./bksystem.sqlite"
+ fi
+ if [ -z $BKS_DATE ]; then
+ BKS_DATE=$(date +"%0Y-%0m-%0d_%0k:%0M:%0S")
+ fi
+ # here go the exported files, should be defined, if not:
+ if [ -z $BKS_BILL_DIR ]; then
+ BKS_BILL_DIR="."
+ fi
+
+ if ! [ -e $DB ]; then
+ echo " Database missing or not found!"
+ exit 1
+ fi
+ echo " Started exporting to files"
+
+#filenames + paths
+ FILENAMEA="Kontostand"
+ FILENAMEC="Verbrauch"
+ FILENAMEL="Konsumliste"
+ CSVFA="${BKS_BILL_DIR}/${FILENAMEA}_${BKS_DATE}.csv"
+ CSVFC="${BKS_BILL_DIR}/${FILENAMEC}_${BKS_DATE}.csv"
+ CSVFL="${BKS_BILL_DIR}/${FILENAMEL}_${BKS_DATE}.csv"
+ #CSVFA="${BKS_BILL_DIR}/${FILENAMEA}.csv"
+ #CSVFC="${BKS_BILL_DIR}/${FILENAMEC}.csv"
+ #CSVFL="${BKS_BILL_DIR}/${FILENAMEL}.csv"
+
+# export bill and consumption table as csv
+ sqlite3 -csv -header $DB "SELECT * FROM previous_account_balance;" > $CSVFA
+ if [ $? -eq 0 ]; then
+ echo " ${CSVFA} written."
+ else
+ echo " ${CSVFA} writing failed"
+ fi
+
+ sqlite3 -csv -header $DB "SELECT * FROM previous_consumption;" > $CSVFC
+ if [ $? -eq 0 ]; then
+ echo " ${CSVFC} written."
+ else
+ echo " ${CSVFC} writing failed"
+ fi
+
+ sqlite3 -csv -header $DB "SELECT * FROM previous_account_list;" > $CSVFL
+ if [ $? -eq 0 ]; then
+ echo " ${CSVFL} written."
+ else
+ echo " ${CSVFL} writing failed"
+ fi
View
164 scripts/export_bill_complete_comb.sh
@@ -0,0 +1,164 @@
+#!/bin/sh
+# This script is intended to export a bill from bksystem.sqlite database to csv. It will generate a user(rows) X products(colums) pivot table, with count as data.
+# same as export_bill_complete_sep.sh but combined first and lastname
+#
+# Steps:
+#
+# get current date and time
+# export tables user, product and bill as .csv
+# generate sqlfile with statements for:
+# CREATE TABLE with products as columns
+# INSERT user rows (each user)
+# UPDATE user rows with consumption (each user and product)
+# query table and add 2 first and last lines
+# export table as .csv
+# cleanup af tmp files
+
+ echo ""
+ echo "Executing script file:"
+ echo "${0}:"
+ echo " Started exporting complete bill..."
+
+# DB and date should be defined, but if not:
+ if [ -z $DB ]; then
+ DB="./bksystem.sqlite"
+ fi
+ if [ -z $BKS_DATE ]; then
+ DATE=$(date +"%0Y-%0m-%0d_%0k:%0M:%0S")
+ else
+ DATE=$BKS_DATE
+ fi
+ DATEFILE="${DATE}"
+ DATEPRINT="${DATE:8:2}.${DATE:5:2}.${DATE:0:4} ${DATE:11:8}"
+
+#filenames + paths for the exported files, should be defined, if not:
+ # exported will be written here
+ if [ -z $BKS_BILL_DIR ]; then
+ BKS_BILL_DIR="."
+ fi
+ FILENAME="Abrechnung"
+ CSVF="${BKS_BILL_DIR}/${FILENAME}_${DATEFILE}_table.csv"
+ #CSVF="${BKS_BILL_DIR}/${FILENAME}_table.csv"
+ echo " Filename:"
+ echo " ${CSVF}"
+
+
+ # set tmp directory
+ if [ -z $BKS_TMP_DIR ]; then
+ BKS_TMP_DIR="."
+ fi
+ BKS_TMP_SUB_DIR="${BKS_TMP_DIR}/comb"
+ if ! [ -d $BKS_TMP_SUB_DIR ]; then
+ mkdir $BKS_TMP_SUB_DIR
+ fi
+ # tmp files
+ # For CEATE TABLE stmt
+ CSV_PROD_ID="${BKS_TMP_SUB_DIR}/product_ids.csv"
+ PROD_LIST="${BKS_TMP_SUB_DIR}/product_list_sql.csv"
+ # For INSERT user
+ CSV_USER="${BKS_TMP_SUB_DIR}/user.csv"
+ # For UPDATE user
+ CSV_BILL="${BKS_TMP_SUB_DIR}/bill.csv"
+ # completed sql batch querry to generate and fill table
+ SQL_Q="${BKS_TMP_SUB_DIR}/create_querry.sql"
+ # for header lines
+ CSV_PROD_NAME="${BKS_TMP_SUB_DIR}/product_names.csv"
+ CSV_PROD_PRICE="${BKS_TMP_SUB_DIR}/product_prices.csv"
+ CSV_PROD_NAME_ID="${BKS_TMP_SUB_DIR}/product_name_prices.csv"
+ # for tail line
+ SUM_ROW="${BKS_TMP_SUB_DIR}/sum_row.csv"
+
+ # ensure tmp files be empty
+ rm $CSV_PROD_ID $PROD_LIST $CSV_USER $CSV_BILL $SQL_Q $CSV_PROD_NAME $CSV_PROD_PRICE $CSV_PROD_NAME_ID $SUM_ROW >/dev/null 2>&1
+
+# generate SQL batch file
+ echo " Generating batch file:"
+ echo " ${SQL_Q}"
+
+ # get product list, user list and user/product accumulated list, these tables are
+
+ # get product list
+ sqlite3 -csv "${DB}" "SELECT EAN, price FROM previous_consumption;" > $CSV_PROD_ID
+ # test for data existence
+ if ! [ -s $CSV_PROD_ID ]; then
+ echo "WARNING: Seems to be nothing sold. Aborting generation" 1>&2
+ exit 0;
+ fi
+
+ # Product List: Quote all ID numbers and append type definiton, then remove linebreaks
+ sed 's/\([0-9][0-9]*,[0-9]*.[0-9]*\)/\"\1EUR\" INTEGER DEFAULT 0,/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g' >$PROD_LIST
+ #sed 's/\([0-9][0-9]*\)/\"\1\" INTEGER,/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g' >$PROD_LIST
+
+ # create file with all sql commands
+ # creat temporary table uid, names, productlist, sum
+ echo " insert CREATE TABLE ..."
+ echo "CREATE TEMP TABLE \"bill_comp\" ( UID INTEGER PRIMARY KEY, name VARCHAR, $(cat ${PROD_LIST}) sum FLOAT);" > $SQL_Q
+
+
+ # loop on user-list
+ echo " insert INSERT users..."
+ # get user list
+ sqlite3 -csv "${DB}" "SELECT userid, lastname, firstname, sum FROM previous_account_balance;" > $CSV_USER
+ # remove quotes
+ sed 's/\"//g' $CSV_USER |
+ # parse user info, generate INSERT statement for each user
+ # insert lastname, firstname into one field
+ while IFS=, read -r USERID LNAME FNAME SUM ; do
+ echo "INSERT INTO \"bill_comp\" ( UID, name, sum) VALUES (${USERID}, \"${LNAME}, ${FNAME}\", ${SUM});" >> $SQL_Q
+ done
+
+ # loop on complete_bill-list to write update statement for each product, only fields that are not = 0 are in this list
+ echo " insert UPDATE users.."
+ # get user/product accumulated list
+ sqlite3 -csv "${DB}" "SELECT userid, EAN, price, count FROM previous_account_list;" > $CSV_BILL
+ while IFS=, read -r CUR_UID CUR_PROD_ID CUR_PRICE CUR_PROD_VAL ; do
+ echo "UPDATE \"bill_comp\" SET \"${CUR_PROD_ID},${CUR_PRICE}EUR\"=${CUR_PROD_VAL} WHERE UID = ${CUR_UID};" >> $SQL_Q
+ done < $CSV_BILL
+ echo " batch file completed..."
+ # cat $SQL_Q
+
+# create 2 header lines/ 1.replace EANs with name 2. with price
+ echo " creating table header..."
+ # get all sold product with name and price, order by ID
+ sqlite3 -csv -separator '#' $DB "SELECT products.name, previous_consumption.price FROM previous_consumption, products WHERE products.EAN=previous_consumption.EAN ORDER by products.EAN" > $CSV_PROD_NAME_ID
+ # parse list, remove quotes around name, new quotes around combined name+price, append to new list
+
+ while IFS='#' read -r CUR_PROD_NAME CUR_PROD_PRICE ; do
+ echo "\"$(echo ${CUR_PROD_NAME} | sed 's/\"//g' )\"," >> $CSV_PROD_NAME
+ echo "${CUR_PROD_PRICE}," >> $CSV_PROD_PRICE
+ done < $CSV_PROD_NAME_ID
+ # first header line (remove linebreaks) leer,"Produkt",list product names, leer
+ echo ",\"Produkt\",$(sed ':a;N;$!ba;s/\n//g' ${CSV_PROD_NAME})" > $CSVF
+ # second header line (remove linebreaks) "UID","Name/Preis",list product price
+ echo "\"UID\",\"Name/Preis EUR\",$(sed ':a;N;$!ba;s/\n//g' ${CSV_PROD_PRICE})\"Summe EUR\"" >> $CSVF
+
+# create table with batch file and fetch it
+ echo " executing batch file..."
+ sqlite3 -csv -separator ',' -init $SQL_Q "${DB}" "SELECT UID, name, $(sed 's/\([0-9][0-9]*,[0-9]*.[0-9]*\)/\"\1EUR\",/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g') sum FROM \"bill_comp\" ORDER BY name;">> $CSVF
+ #sqlite3 -csv -init $SQL_Q "${DB}" "SELECT * FROM \"bill_comp\" ORDER BY name;">> $CSVF
+
+ if [ $? -eq 0 ]; then
+ echo " ${CSVF} written."
+ else
+ echo " ERROR: sqlite3 aborted with ${?}: export failed!" 1>&2
+ cp $SQL_Q $BKS_LOG_DIR 1>&2
+ echo " check ${SQL_Q}, " 1>&2
+ fi
+
+
+# add sum up row
+ echo " creating sum row..."
+ # first entries are empty
+ echo ",Verbrauch Anzahl" > $SUM_ROW
+
+ sqlite3 -csv "${DB}" "SELECT count FROM previous_consumption;" >> $SUM_ROW
+ sqlite3 -csv "${DB}" "SELECT round(sum(\"sum\"),2) AS summe FROM previous_account_balance;" >> $SUM_ROW
+ #cat $SUM_ROW
+ # replace newline with ,
+ sed ':a;N;$!ba;s/\n/,/g' $SUM_ROW >> $CSVF
+ #cat $CSVF
+
+# cleanup
+ echo " removing tmp files..."
+ rm -r $BKS_TMP_SUB_DIR
+ #rm -f $CSV_PROD $PROD_LIST $CSV_USER $CSV_BILL $SUM_ROW #$SQL_Q
View
164 scripts/export_bill_complete_sep.sh
@@ -0,0 +1,164 @@
+#!/bin/sh
+# This script is intended to export a bill from bksystem.sqlite database to csv. It will generate a user(rows) X products(colums) pivot table, with count as data.
+# same as export_bill_complete.sh but seperate first and lastname
+#
+# Steps:
+#
+# get current date and time
+# export tables user, product and bill as .csv
+# generate sqlfile with statements for:
+# CREATE TABLE with products as columns
+# INSERT user rows (each user)
+# UPDATE user rows with consumption (each user and product)
+# query table and add first and last line
+# export table
+# cleanup af tmp files
+
+ echo ""
+ echo "Executing script file:"
+ echo "${0}:"
+ echo " Started exporting complete bill..."
+
+# DB and date should be defined, but if not:
+ if [ -z $DB ]; then
+ DB="./bksystem.sqlite"
+ fi
+ if [ -z $BKS_DATE ]; then
+ DATE=$(date +"%0Y-%0m-%0d_%0k:%0M:%0S")
+ else
+ DATE=$BKS_DATE
+ fi
+ DATEFILE="${DATE}"
+ DATEPRINT="${DATE:8:2}.${DATE:5:2}.${DATE:0:4} ${DATE:11:8}"
+
+#filenames + paths for the exported files, should be defined, if not:
+ # exported will be written here
+ if [ -z $BKS_BILL_DIR ]; then
+ BKS_BILL_DIR="."
+ fi
+ FILENAME="Abrechnung"
+ CSVF="${BKS_BILL_DIR}/${FILENAME}_${DATEFILE}_table_sep.csv"
+ #CSVF="${BKS_BILL_DIR}/${FILENAME}_table_sep.csv"
+ echo " Filename:"
+ echo " ${CSVF}"
+
+
+ # set tmp directory
+ if [ -z $BKS_TMP_DIR ]; then
+ BKS_TMP_DIR="."
+ fi
+ BKS_TMP_SUB_DIR="${BKS_TMP_DIR}/sep"
+ if ! [ -d $BKS_TMP_SUB_DIR ]; then
+ mkdir $BKS_TMP_SUB_DIR
+ fi
+
+ # tmp files
+ # For CEATE TABLE stmt
+ CSV_PROD_ID="${BKS_TMP_SUB_DIR}/product_ids_sep.csv"
+ PROD_LIST="${BKS_TMP_SUB_DIR}/product_list_sql_sep.csv"
+ # For INSERT user
+ CSV_USER="${BKS_TMP_SUB_DIR}/user_sep.csv"
+ # For UPDATE user
+ CSV_BILL="${BKS_TMP_SUB_DIR}/bill_sep.csv"
+ # completed sql batch querry to generate and fill table
+ SQL_Q="${BKS_TMP_SUB_DIR}/create_querry_sep.sql"
+ # for header lines
+ CSV_PROD_NAME="${BKS_TMP_SUB_DIR}/product_names_sep.csv"
+ CSV_PROD_PRICE="${BKS_TMP_SUB_DIR}/product_prices_sep.csv"
+ CSV_PROD_NAME_ID="${BKS_TMP_SUB_DIR}/product_name_prices_sep.csv"
+ # for tail line
+ SUM_ROW="${BKS_TMP_SUB_DIR}/sum_row_sep.csv"
+
+ # ensure tmp files be empty
+ rm $CSV_PROD_ID $PROD_LIST $CSV_USER $CSV_BILL $SQL_Q $CSV_PROD_NAME $CSV_PROD_PRICE $CSV_PROD_NAME_ID $SUM_ROW >/dev/null 2>&1
+# generate SQL batch file
+ echo " Generating batch file:"
+ echo " ${SQL_Q}"
+
+ # get product list, user list and user/product accumulated list, these tables are
+
+ # get product list
+ sqlite3 -csv "${DB}" "SELECT EAN, price FROM previous_consumption;" > $CSV_PROD_ID
+ # test for data existance
+ if ! [ -s $CSV_PROD_ID ]; then
+ echo "WARNING: Seems to be nothing sold. Aborting generation" 1>&2
+ exit 0;
+ fi
+ # Product List: Quote all ID numbers and append type definiton, then remove linebreaks
+ sed 's/\([0-9][0-9]*,[0-9]*.[0-9]*\)/\"\1EUR\" INTEGER DEFAULT 0,/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g' >$PROD_LIST
+ #sed 's/\([0-9][0-9]*\)/\"\1\" INTEGER,/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g' >$PROD_LIST
+
+ # create file with all sql commands
+ # creat temporary table uid, names, productlist, sum
+ echo " insert CREATE TABLE ..."
+ echo "CREATE TEMP TABLE \"bill_comp\" ( UID INTEGER PRIMARY KEY, lastname VARCHAR, firstname VARCHAR, $(cat ${PROD_LIST}) sum FLOAT);" > $SQL_Q
+
+
+ # loop on user-list
+ echo " insert INSERT users..."
+ # get user list
+ sqlite3 -csv "${DB}" "SELECT userid, lastname, firstname, sum FROM previous_account_balance;" > $CSV_USER
+ # remove quotes
+ sed 's/\"//g' $CSV_USER |
+ # parse user info, generate INSERT statement for each user
+ # insert lastname, firstname into one field
+ while IFS=, read -r USERID LNAME FNAME SUM ; do
+ echo "INSERT INTO \"bill_comp\" ( UID, lastname, firstname, sum) VALUES (${USERID}, \"${LNAME}\", \"${FNAME}\", ${SUM});" >> $SQL_Q
+ done
+
+ # loop on complete_bill-list to write update statement for each product, only fields that are not = 0 are in this list
+ echo " insert UPDATE users.."
+ # get user/product accumulated list
+ sqlite3 -csv "${DB}" "SELECT userid, EAN, price, count FROM previous_account_list;" > $CSV_BILL
+ while IFS=, read -r CUR_UID CUR_PROD_ID CUR_PRICE CUR_PROD_VAL ; do
+ echo "UPDATE \"bill_comp\" SET \"${CUR_PROD_ID},${CUR_PRICE}EUR\"=${CUR_PROD_VAL} WHERE UID = ${CUR_UID};" >> $SQL_Q
+ done < $CSV_BILL
+ echo " batch file completed..."
+ # cat $SQL_Q
+
+# create 2 header lines/ 1.replace EANs with name 2. with price
+ echo " creating table header..."
+ # get all sold product with name and price, order by ID
+ sqlite3 -csv -separator '#' $DB "SELECT products.name, previous_consumption.price FROM previous_consumption, products WHERE products.EAN=previous_consumption.EAN ORDER by products.EAN" > $CSV_PROD_NAME_ID
+ # parse list, remove quotes around name, new quotes around combined name+price, append to new list
+
+ while IFS='#' read -r CUR_PROD_NAME CUR_PROD_PRICE ; do
+ echo "\"$(echo ${CUR_PROD_NAME} | sed 's/\"//g' )\"," >> $CSV_PROD_NAME
+ echo "${CUR_PROD_PRICE}," >> $CSV_PROD_PRICE
+ done < $CSV_PROD_NAME_ID
+ # first header line (remove linebreaks) leer,"Produkt",list product names, leer
+ echo "\"\",\"\",\"Produkt\",$(sed ':a;N;$!ba;s/\n//g' ${CSV_PROD_NAME})\"\"" > $CSVF
+ # second header line (remove linebreaks) "UID","Name/Preis",list product price
+ echo "\"UID\",\"Nachname\",\"Vorname/Preis EUR\",$(sed ':a;N;$!ba;s/\n//g' ${CSV_PROD_PRICE})\"Summe EUR\"" >> $CSVF
+
+# create table with batch file and fetch it
+ echo " executing batch file..."
+ sqlite3 -csv -separator ',' -init $SQL_Q "${DB}" "SELECT UID, lastname,firstname, $(sed 's/\([0-9][0-9]*,[0-9]*.[0-9]*\)/\"\1EUR\",/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g') sum FROM \"bill_comp\" ORDER BY lastname, firstname;">> $CSVF
+ #sqlite3 -csv -init $SQL_Q "${DB}" "SELECT * FROM \"bill_comp\" ORDER BY lastname,firstname;">> $CSVF
+
+ if [ $? -eq 0 ]; then
+ echo " ${CSVF} written."
+ else
+ echo " ERROR: sqlite3 aborted with ${?}: export failed!" 1>&2
+ cp $SQL_Q $BKS_LOG_DIR
+ echo " check ${SQL_Q}" 1>&2
+ fi
+
+
+# add sum up row
+ echo " creating sum row..."
+ # first entries are empty
+ echo ",,Verbrauch Anzahl" > $SUM_ROW
+
+ sqlite3 -csv "${DB}" "SELECT count FROM previous_consumption;" >> $SUM_ROW
+ sqlite3 -csv "${DB}" "SELECT round(sum(\"sum\"),2) AS summe FROM previous_account_balance;" >> $SUM_ROW
+ #cat $SUM_ROW
+ # replace newline with ,
+ sed ':a;N;$!ba;s/\n/,/g' $SUM_ROW >> $CSVF
+ #cat $CSVF
+
+# cleanup
+ echo " removing tmp files..."
+ rm -r $BKS_TMP_SUB_DIR
+ #rm -f $CSV_PROD $PROD_LIST $CSV_USER $CSV_BILL $SUM_ROW #$SQL_Q
+# dont clean up .sql: will be kept on error :
View
243 scripts/export_bill_html.sh
@@ -0,0 +1,243 @@
+#!/bin/sh
+# This script is intended to export a bill from bksystem.sqlite database to html.
+# It will generate a user(rows) X products(colums) pivot table, with count as data.
+# It will tag some fields to get some formating with css style-sheets
+#
+# Steps:
+#
+# get current date and time
+# export tables user, product and bill as .csv
+# generate sqlfile with statements for:
+# CREATE TABLE with products as columns
+# INSERT user rows (each user)
+# UPDATE user rows with consumption (each user and product)
+# query table and add 2 first and last lines as html
+# export table as .html
+# add tag to certain fields (e.g. sum, uid, etc.
+# cleanup af tmp files
+
+ echo ""
+ echo "Executing script file:"
+ echo "${0}:"
+ echo " Started exporting complete bill..."
+
+# CONFIG: here you can set the entries for certain field e.g. total sum etc.
+
+ # 1./2./last row # field 1/2/last
+ R1F1=""; R1F2="Produkte"; R1FL=""
+ R2F1="UID"; R2F2="Name/Preis in &euro;"; R2FL="Summe &euro;"
+ LRF1=""; RLF2="Verbrauch Anzahl"
+
+ # layout to use (will be embedded)
+ LAYOUT="${BKS_SCR_DIR}/layout.css"
+
+ # subscript all html after </table> except both </body> and </html>, if too long replace with file
+ SUB_TEXT="<p>Karlsruhe, den $(date +%0d.%0m.%0Y), Bierwart</p>"
+ SUBTEXTF="${BKS_SCR_DIR}/sub_txt_tmpl" # also replace occurence of $SUB_TEXT with " sed 's/DATE_PLACEHOLDER/"$DATEPRINT"/g' <$SUBTEXTF >>$HTMLF"
+
+# DB and date should be defined, but if not:
+ if [ -z $DB ]; then
+ DB="./bksystem.sqlite"
+ fi
+ if [ -z $BKS_DATE ]; then
+ DATE=$(date +"%0Y-%0m-%0d_%0k:%0M:%0S")
+ else
+ DATE=$BKS_DATE
+ fi
+ DATEFILE="${DATE}"
+ DATEPRINT="${DATE:8:2}.${DATE:5:2}.${DATE:0:4} ${DATE:11:8}"
+
+ # table caption
+ TBL_CAP="Abrechnung vom ${DATEPRINT}"
+
+#filenames + paths for the exported files, should be defined, if not:
+ # exported will be written here
+ if [ -z $BKS_BILL_DIR ]; then
+ BKS_BILL_DIR="."
+ fi
+ FILENAME="Abrechnung"
+ HTMLF="${BKS_BILL_DIR}/${FILENAME}_${DATEFILE}_table.html"
+ #HTMLF="${BKS_BILL_DIR}/gen_table.html"
+ echo " Filename:"
+ echo " ${HTMLF}"
+
+
+ # set tmp directory
+ if [ -z $BKS_TMP_DIR ]; then
+ BKS_TMP_DIR="."
+ fi
+ BKS_TMP_SUB_DIR="${BKS_TMP_DIR}/html"
+ if ! [ -d $BKS_TMP_SUB_DIR ]; then
+ mkdir $BKS_TMP_SUB_DIR
+ fi
+ # tmp files
+ # For CEATE TABLE stmt
+ CSV_PROD_ID="${BKS_TMP_SUB_DIR}/product_ids.csv"
+ PROD_LIST="${BKS_TMP_SUB_DIR}/product_list_sql.csv"
+ # For INSERT user
+ CSV_USER="${BKS_TMP_SUB_DIR}/user.csv"
+ # For UPDATE user
+ CSV_BILL="${BKS_TMP_SUB_DIR}/bill.csv"
+ # completed sql batch querry to generate and fill table
+ SQL_Q="${BKS_TMP_SUB_DIR}/create_querry_html.sql"
+ # for header lines
+ CSV_PROD_NAME="${BKS_TMP_SUB_DIR}/product_names.csv"
+ CSV_PROD_PRICE="${BKS_TMP_SUB_DIR}/product_prices.csv"
+ CSV_PROD_NAME_ID="${BKS_TMP_SUB_DIR}/product_name_prices.csv"
+ # for tail line
+ SUM_ROW="${BKS_TMP_SUB_DIR}/sum_row.csv"
+
+ # ensure tmp files be empty
+ rm $CSV_PROD_ID $PROD_LIST $CSV_USER $CSV_BILL $SQL_Q $CSV_PROD_NAME $CSV_PROD_PRICE $CSV_PROD_NAME_ID $SUM_ROW >/dev/null 2>&1
+
+# generate SQL batch file
+ echo " Generating batch file:"
+ echo " ${SQL_Q}"
+
+ # get product list, user list and user/product accumulated list, these tables are
+
+ # get product list
+ sqlite3 -csv "${DB}" "SELECT EAN, price FROM previous_consumption;" > $CSV_PROD_ID
+ # test for data existence
+
+ if ! [ -s $CSV_PROD_ID ]; then
+ echo "WARNING: Seems to be nothing sold. Aborting generation" 1>&2
+ exit 0;
+ fi
+
+ # Product List: Quote all ID numbers and append type definiton, then remove linebreaks
+ sed 's/\([0-9][0-9]*,[0-9]*.[0-9]*\)/\"\1EUR\" INTEGER DEFAULT 0,/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g' >$PROD_LIST
+ #sed 's/\([0-9][0-9]*\)/\"\1\" INTEGER,/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g' >$PROD_LIST
+
+ # create file with all sql commands
+ # creat temporary table uid, names, productlist, sum
+ echo " insert CREATE TABLE ..."
+ echo "CREATE TEMP TABLE \"bill_comp\" ( UID INTEGER PRIMARY KEY, name VARCHAR, $(cat ${PROD_LIST}) sum FLOAT);" > $SQL_Q
+
+
+ # loop on user-list
+ echo " insert INSERT users..."
+ # get user list
+ sqlite3 -csv "${DB}" "SELECT userid, lastname, firstname, sum FROM previous_account_balance;" > $CSV_USER
+ # remove quotes
+ sed 's/\"//g' $CSV_USER |
+ # parse user info, generate INSERT statement for each user
+ # insert lastname, firstname into one field
+ while IFS=, read -r USERID LNAME FNAME SUM ; do
+ echo "INSERT INTO \"bill_comp\" ( UID, name, sum) VALUES (${USERID}, \"${LNAME}, ${FNAME}\", ${SUM});" >> $SQL_Q
+ done
+
+ # loop on complete_bill-list to write update statement for each product, only fields that are not = 0 are in this list
+ echo " insert UPDATE users.."
+ # get user/product accumulated list
+ sqlite3 -csv "${DB}" "SELECT userid, EAN, price, count FROM previous_account_list;" > $CSV_BILL
+ while IFS=, read -r CUR_UID CUR_PROD_ID CUR_PRICE CUR_PROD_VAL ; do
+ echo "UPDATE \"bill_comp\" SET \"${CUR_PROD_ID},${CUR_PRICE}EUR\"=${CUR_PROD_VAL} WHERE UID = ${CUR_UID};" >> $SQL_Q
+ done < $CSV_BILL
+ echo " batch file completed..."
+ # cat $SQL_Q
+
+#HTML GENERATION
+
+ # input templates
+ echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">" >$HTMLF
+ echo "<html><head>" >>$HTMLF
+ echo "<META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=utf-8\">" >>$HTMLF
+ echo "<title>${TBL_CAP}</title>" >>$HTMLF
+ cat $LAYOUT >>$HTMLF
+ #echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"screen\" href=\"../styles-screen.css\">">>$HTMLF
+ #echo "<link rel=\"stylesheet\" type=\"text/css\" media=\"print\" href=\"../styles-print.css\">">>$HTMLF
+ echo "</head><body>" >>$HTMLF
+ # may replaced by template file
+
+ # insert head line
+ echo "<h3>${TBL_CAP}</h3>" >>$HTMLF
+ echo "<table id=\"bill\">" >>$HTMLF
+
+# create 2 header lines/ 1.replace EANs with name 2. with price
+ echo " creating table header..."
+ # get all sold product with name and price, order by ID
+ sqlite3 -csv -separator '#' $DB "SELECT products.name, previous_consumption.price FROM previous_consumption, products WHERE products.EAN=previous_consumption.EAN ORDER by products.EAN;" > $CSV_PROD_NAME_ID
+ # parse list, remove quotes around name, new quotes around combined name+price, append to new list
+
+ while IFS='#' read -r CUR_PROD_NAME CUR_PROD_PRICE ; do
+ # this line does the rotation also esp. width=\"16\" height=\"160\" AND rotate(270, 12, 0) translate(-140,0) height+ translat are in relation
+ echo "<TD class=\"prod_name\"><svg width=\"16\" height=\"116\"><text id=\"prod_name_rot\" transform=\"rotate(270, 12, 0) translate(-100,0)\">$(echo ${CUR_PROD_NAME} | sed 's/\"//g' )</text></svg></TD>" >> $CSV_PROD_NAME
+ echo "<TD class=\"prod_price\">${CUR_PROD_PRICE}</TD>" >> $CSV_PROD_PRICE
+ done < $CSV_PROD_NAME_ID
+ #cat $CSV_PROD_NAME
+ #cat $CSV_PROD_PRICE
+ #echo "<COLGROUP #class=\"sum_col\"></COLGROUP>" >>$HTMLF
+
+ echo "<thead>" >>$HTMLF
+ # first header line (remove linebreaks) leer,"Produkt",list product names, leer
+ echo "<TR id=\"prod_names_row\">" >>$HTMLF
+ echo "<TD>${R1F1}</TD>" >>$HTMLF
+ echo "<TD>${R1F2}</TD>" >>$HTMLF
+ echo "$(cat ${CSV_PROD_NAME})" >>$HTMLF
+ echo "<TD>${R1FL}</TD></TR>" >>$HTMLF
+
+ # second header line (remove linebreaks) "UID","Name/Preis",list product price
+ echo "<TR id=\"prod_price_row\">" >>$HTMLF
+ echo "<TD>${R2F1}</TD>" >>$HTMLF
+ echo "<TD>${R2F2}</TD>" >>$HTMLF
+ echo "$(cat ${CSV_PROD_PRICE}) " >>$HTMLF
+ echo "<TD class=\"sum\", id=\"sum_text\">${R2FL}</TD></TR>" >>$HTMLF
+
+
+ echo "</thead>" >>$HTMLF
+ echo "</tbody>" >>$HTMLF
+ #cat $CSVF
+
+
+# create table with batch file and fetch it
+ echo " executing batch file..."
+ # get as html: recreate column IDs from csv: "EAN,priceEUR", then remove linebreaks, => insert it to the SELECT statement
+ # then search for (.Number) append zero (e.g. 0.9 => 0.90, 0.95 => 0.95)
+ sqlite3 -html -init $SQL_Q "${DB}" "SELECT UID, name, $(sed 's/\([0-9][0-9]*,[0-9]*.[0-9]*\)/\"\1EUR\",/g' $CSV_PROD_ID | sed ':a;N;$!ba;s/\n//g') sum FROM \"bill_comp\" ORDER BY name;" | sed 's/\([.][0-9]\)\([^0-9]\)/\10\2/g' >>$HTMLF
+ #sqlite3 -csv -init $SQL_Q "${DB}" "SELECT * FROM \"bill_comp\" ORDER BY name;">>$HTMLF
+
+ # TODO check will fail cause $? is exitcode of sed, not sqlite3
+ if [ $? -eq 0 ]; then
+ echo " ${HTMLF} written."
+ else
+ echo " ERROR: sqlite3 aborted with ${?}: export failed!" 1>&2
+ cp $SQL_Q $BKS_LOG_DIR 1>&2
+ echo " check ${SQL_Q}" 1>&2
+ fi
+
+ echo "</tbody>" >>$HTMLF
+ echo "<tfoot>" >>$HTMLF
+
+# add sum up row
+ echo " creating sum row..."
+ # first entries are empty
+ echo "<TR id=\"sum_row\">" >>$HTMLF
+ echo "<TD>${LRF1}</TD>" >>$HTMLF
+ echo "<TD>${RLF2}</TD>" >>$HTMLF
+
+ sqlite3 -html "${DB}" "SELECT count FROM previous_consumption;" >$SUM_ROW
+
+ #cat $SUM_ROW
+ # kick TR tags, ,
+ sed 's/<TR>//g' $SUM_ROW | sed 's/<\/TR>//g' | sed 's/<TD>/<TD class=\"sum_row_fields\">/g' >>$HTMLF
+ sqlite3 -html "${DB}" "SELECT round(sum(\"sum\"),2) AS summe FROM previous_account_balance;" \
+ | sed 's/<TR>//g' | sed 's/<\/TR>//g' | sed 's/<TD>/<TD id=\"sum_col_row\">/g' >>$HTMLF
+
+
+ echo "</TR>" >>$HTMLF
+ echo "</tfoot>" >>$HTMLF
+ echo "</table>" >>$HTMLF
+ # text $SUB_TEXT
+ #echo "${SUB_TEXT}" >>$HTMLF
+ # text in file $SUB_TEXT
+ # insert current! not BKS_DATE
+ sed s/DATE_PLACEHOLDER/"$(date +%d.%m.%Y)"/g < $SUBTEXTF >> $HTMLF
+ echo "</body></html>" >>$HTMLF
+
+
+ # cat $CSVF
+# cleanup
+ echo " removing tmp files..."
+ rm -r $BKS_TMP_SUB_DIR
+ #rm $CSV_PROD $PROD_LIST $CSV_PROD_ID $PROD_LIST $CSV_USER $CSV_BILL $SQL_Q $CSV_PROD_NAME $CSV_PROD_PRICE $CSV_PROD_NAME_ID $SUM_ROW
View
35 scripts/insert_bill_reference_date_into_db.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+# script inserts current date and time into the database
+# DB and date should be defined
+
+ echo ""
+ echo "Executing script file:"
+ echo "${0}:"
+
+# check variables
+ if [ -z $DB ]; then
+ DB="./bksystem.sqlite"
+ fi
+
+ if [ -z $BKS_DATE ]; then
+ DATE=$(date +"%0Y-%0m-%0d_%0k:%0M:%0S")
+ else
+ DATE=$BKS_DATE
+ fi
+ DATESQL="${DATE:0:10} ${DATE:11:8}"
+
+ echo " Started inserting ${DATESQL} to DB"
+
+ if ! [ -e $DB ]; then
+ echo "ERROR: Database missing or not found! Aborting." 1>&2
+ exit 1
+ fi
+
+# insert current date to DB to trigger bill table generation
+ STMT="INSERT INTO bill_dates (timestamp) VALUES ('${DATESQL}');"
+ #for testing: STMT="INSERT INTO bill_dates (timestamp) VALUES ('2013-01-01 12:12:12');"
+ # echo sqlite3 -column $DB $STMT
+ sqlite3 -column "${DB}" "$STMT"
+ REPLY=$?
+ echo $REPLY > $ERRCODE
+ exit $REPLY
View
67 scripts/layout.css
@@ -0,0 +1,67 @@
+<style type="text/css">
+/* Layout for displaying the bills*/
+
+/*applies to whole table*/
+#bill {
+ border: 3px solid black;
+ border-collapse: collapse;
+ background-color: white;
+}
+table#bill {
+ font: sans-serif;
+ font-style: normal;
+ font-size: x-small;
+ border: 3px solid black;
+ border-collapse: collapse;
+ background-color: white;
+}
+/* all fiels contaning a uid*/
+tr > td:first-child {
+ border: 1px solid black;
+ text-align: right;
+}
+/* all inner fields */
+td {
+ border: 1px solid black;
+ text-align: right;
+ border-collapse: collapse;
+}
+/* fields in name col name*/
+tr td:nth-child(2) {
+ border-right: 3px solid black;
+}
+/* fields with name*/
+tbody tr td:nth-child(2) {
+ text-align: left;
+}
+/* all fields with sums on the right*/
+tr > td:last-child {
+ border-left: 3px solid black;
+ font-weight: bold;
+ text-align: right;
+}
+/* each bottom field with a sum of count*/
+tfoot {
+
+}
+/* bottom right most field*/
+tfoot tr:last-child td:last-child {
+ border: 4px solid black;
+ font-weight: bold;
+ text-align: right;
+}
+/*top two rows*/
+thead {
+ border: 3px solid black;
+}
+thead tr:nth-child(2) td:first-child {
+ text-align:bottom;
+}
+/* complete bottom row*/
+tfoot {
+ border: 3px solid black;
+ font-weight: bold;
+ text-align: right;
+}
+
+</style>
View
39 scripts/restore_db.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+# read backup data from files
+#
+# use export DB="path/to/database/name.sqlite
+
+ echo ""
+ echo "Executing script file:"
+ echo " ${0}:"
+if [ -z $BKS_BKP_DIR ]; then
+ BKS_BKP_DIR="."
+fi
+
+cd $BKS_BKP_DIR
+echo "cd ${BKS_BKP_DIR}"
+
+# check variables
+ if [ -z $DB ]; then
+ DB="./bksystem.sqlite"
+ fi
+ if [ -e $DB ]; then
+ echo " File ${DB} already exists. aborting!"
+ echo " Use: export DB=\"path/to/database/name.sqlite\" to define a new name."
+ exit 1
+ fi
+
+echo "schema reading.."
+sqlite3 -batch $DB ".read ${BKS_BKP_DIR}/bksystem_scheme.sql"
+echo "products reading..."
+sqlite3 -batch $DB ".read products.sql"
+echo "user_accounts reading...."
+sqlite3 -batch $DB ".read user_accounts.sql"
+echo "sales reading....."
+sqlite3 -batch $DB ".read sales.sql"
+echo "bill_dates reading....."
+sqlite3 -batch $DB ".read bill_dates.sql"
+echo "finished restore."
+
+cd -
+exit 0
View
1  scripts/sub_txt_tmpl
@@ -0,0 +1 @@
+<p>Karlsruhe, den DATE_PLACEHOLDER</p>
Please sign in to comment.
Something went wrong with that request. Please try again.