-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsammelbestellung.py
executable file
·135 lines (112 loc) · 4.86 KB
/
sammelbestellung.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
# Sammelbestellungs-Abrechnung
# https://github.com/mgmax/sammelbestellung
#
# (c) 2013 Max Gaukler <[email protected]>
# EML-Output, subdir, billing, ...(c) 2013 Patrick Kanzler <[email protected]>
#
# Licensed under the GNU/GPL version 2 or (at your option) any later version
"""
# TODO there are some unnecessary imports here
import os
import shutil
import sys
import getopt
import httplib
import socket
import urllib
import re
import time
import logging
import cookielib, urllib2
import copy
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import subprocess
import shlex
from string import Template
import locale
# local imports
import pricefetcher
import sammelbestellparser
import sammelbestellOutput
import stringStuff
# returns: [buyers, origin, settings, totalBasket]
def parseAndFetch(file):
[buyers, origin, settings, totalBasket]=sammelbestellparser.parse(file)
return fetchAndCalculatePrices(buyers, origin, settings, totalBasket)
# returns: [buyers, origin, settings, totalBasket, totalSums]
def fetchAndCalculatePrices(buyers, origin, settings, totalBasket):
totalBasket.fetchPrices()
for b in buyers:
b.basket.takePricesFrom(totalBasket)
subtotalSums=totalBasket.shopSums()
totalSums={}
for (shop,s) in subtotalSums.items():
if pricefetcher.shopByName(shop).factor is None or pricefetcher.shopByName(shop).shipping is None:
raise Exception("no factor or shipping set for shop %s. Please at least put !shop shopname, !setfactor 1 and !setshipping 0 for each shop at the beginning of your file" % str(shop))
totalSums[shop]=s*pricefetcher.shopByName(shop).factor + pricefetcher.shopByName(shop).shipping
print "Shop " + shop + " subtotal " + str(s) + ". * factor " + str(pricefetcher.shopByName(shop).factor) + " + shipping " + str(pricefetcher.shopByName(shop).shipping) + " = Total " + str(totalSums[shop])
for b in buyers:
b.finalSum=0
b.totalShipping=0
for (shop,s) in totalSums.items():
subtotal=b.basket.shopSum(shop)
shipping=b.basket.shopSum(shop)/subtotalSums[shop]*pricefetcher.shopByName(shop).shipping
total=subtotal*pricefetcher.shopByName(shop).factor + shipping
if total != 0:
logging.info("buyer " + b.name + ", shop "+ shop + " : " + str(total) + " subtotal (" + str(subtotal) + " * factor " + str(pricefetcher.shopByName(shop).factor) + " + shipping " + str(shipping))
b.shopFinalSums[shop]=total
b.shopShipping[shop]=shipping
b.totalShipping += shipping
b.finalSum += total
logging.info("buyer " + b.name + ", final sum: " + str(b.finalSum) + " , shops:" + str(b.shopFinalSums))
# checksumming:
buyersChecksum=0
for b in buyers:
buyersChecksum += b.finalSum
shopsChecksum=0
for (shop, s) in totalSums.items():
shopsChecksum += s
logging.info("checksum difference: " + str(shopsChecksum-buyersChecksum) + ", shop total: " + str(shopsChecksum) + ", buyer total: " + str(buyersChecksum) + "; individual shops: " + str(totalSums))
if (abs(shopsChecksum-buyersChecksum) >= 0.005):
raise Exception("Checksums do not match!")
return [buyers, origin, settings, totalBasket, totalSums]
if __name__=="__main__":
locale.setlocale(locale.LC_ALL, '')
try:
reload(sys)
sys.setdefaultencoding("utf8")
logging.basicConfig(level=logging.DEBUG)
#logging.basicConfig(level=logging.INFO)
#print "Es kostet %.2f" % fetchPrice("reichelt.de", "PFL 10", 100)
#print "Es kostet %.2f" % fetchPrice("de.rs-online.com", "666-1608", 10)
#[ 100, "Reichelt", "PFL 10" ],
#print PriceFetcherReichelt().fetchBasket("https://secure.reichelt.de/index.html?;ACTION=20;LA=5010;AWKID=619772;PROVID=2084")
#sys.exit(0)
if len(sys.argv) != 2:
sys.stderr.write("usage: '"+sys.argv[0]+"' order_file")
try:
[buyers, origin, settings, totalBasket, totalSums]=parseAndFetch(sys.argv[1])
except IOError:
sys.stderr.write("Error opening file.\n")
sys.stderr.write("usage: '"+sys.argv[0]+"' order_file\n")
sys.exit(1)
sammelbestellOutput.makeOutput(buyers, origin, settings, totalBasket, totalSums)
print "Success!"
sys.exit(0)
# TODO Mindeststückzahl-Error bei Rei? und RS
#TODO Adresse bei Buyer
#TODO Adresse des Bestellers
#TODO Posten ausrechnen und malen
#TODO pdflatex vorsichtig suchen?
#TODO extra config-Datei?
#TODO LongTable?
except Exception, e:
#pass
#print "Error:"
#print e
#sys.exit(1)
raise