-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewinv.sh
executable file
·64 lines (50 loc) · 1.58 KB
/
newinv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Sets the winvo invoices directory
INVOICES_DIR="./invoices"
# default client configuration file
SKEL_FILE="./example.conf"
[ -f custom_config.sh ] && source custom_config.sh
# create the file that keeps track of next invoice number
[ -d "${INVOICES_DIR}" ] || mkdir -p "${INVOICES_DIR}"
[ -f "${INVOICES_DIR}/n" ] || echo 1 >> "${INVOICES_DIR}/n"
# this app requires at least one argument,
# show usage if it's not provided
if [ $# -lt 1 ]
then
echo "usage: $0 [invoicename] [OPTION]"
printf "Options:\n --pro\t Generate proforma invoice, n not increment."
exit
fi
# try to find the previous configuration file with the same invoice name
previous="$(find "${INVOICES_DIR}" | grep "^\\./20.*/$1.*conf$" | sort | tail -1)"
if [ $previous ]
then
SKEL_FILE=$previous
fi
invname=$1
nf=$(cat "${INVOICES_DIR}/n")
fname=$1.$(echo $nf|sed 's/\//./')
echo $fname
# create this invoice's directory
if [ "$2" != "--pro" ]
then
folder="${INVOICES_DIR}/$(date +%Y%m)/$fname"
mkdir -p "$folder"
else
folder="${INVOICES_DIR}/pro.$(date +%Y%m)/$fname"
mkdir -p "$folder"
fi
# create this invoice's conf file, with the given invoice no
filename="$folder/$fname.conf"
ny=$(echo $nf | cut -d"/" -f1)
nn=$(echo $nf | cut -d"/" -f2)
num=$(echo $nn | sed 's/^0*//g')
cp "$SKEL_FILE" "$filename"
sed -i "s/^number: .*/number: $ny\/$nn/" $filename
# update the next invoice no
if [ "$2" != "--pro" ]
then
(( num++ ))
printf "%s/%03d\n" $ny $num > "${INVOICES_DIR}/n"
fi
echo "DONE, edit the file ${filename} and type ./winvo.py ${filename} to generate the pdf in place"