124 lines
3.7 KiB
Python
124 lines
3.7 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import csv
|
|
import glob
|
|
import email
|
|
import quopri
|
|
import decimal
|
|
|
|
pedidos = {}
|
|
totales = {}
|
|
pasta = {'confirmada': 0, 'fallo': 0, 'recibida': 0}
|
|
|
|
translations = {
|
|
'Pack de Biocards': 'Biocard Pack',
|
|
'Camiseta Recursion Prime - L': 'Recursion Prime t-shirt - Blanco / White',
|
|
'Camiseta Recursion Prime - Negro / Black': 'Recursion Prime t-shirt - Negro / Black',
|
|
'Camiseta Recursion Prime - Verde / Green': 'Recursion Prime t-shirt - Verde / Green',
|
|
'Camiseta Recursion Prime - XXXL': 'Recursion Prime t-shirt - Verde / Green',
|
|
'Dessuadora Recursion Prime - M': 'Recursion Prime hoodie - M',
|
|
'Samarreta Recursion Prime - Negro / Black': 'Recursion Prime t-shirt - Negro / Black',
|
|
'Sudadera Recursion Prime - L': 'Recursion Prime hoodie - L',
|
|
}
|
|
|
|
|
|
def check_tpv(lines):
|
|
pago_ok = False
|
|
for line in lines:
|
|
if 'Se ha realizado el pago correcto' in line:
|
|
pago_ok = True
|
|
elif u'Número de operación' in line:
|
|
pedido = int(line.split(':')[1])
|
|
elif '- Importe:' in line:
|
|
importe = decimal.Decimal(line.split(':')[1])
|
|
if pago_ok:
|
|
return pedido, importe
|
|
return None
|
|
|
|
def check_paypal(lines):
|
|
pago_ok = False
|
|
for line in lines:
|
|
if 'Pago enviado a enlighthouse.es@gmail.com' in line:
|
|
pago_ok = True
|
|
elif u'Pago €' in line:
|
|
importe = decimal.Decimal(line.split(' ')[1][1:].replace(',', '.'))
|
|
elif 'Id. de factura' in line:
|
|
pedido = int(line.split(' ')[3][3:])
|
|
if pago_ok:
|
|
return pedido, importe
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def check_file(item):
|
|
with open(item) as fp:
|
|
msg = email.message_from_file(fp)
|
|
|
|
for part in msg.walk():
|
|
if part.get_content_type() == 'text/plain':
|
|
texto = part.get_payload(decode=True)
|
|
#print(part.get_encode())
|
|
texto = texto.decode('utf-8')
|
|
lines = texto.split('\n')
|
|
lines = [ line.strip() for line in lines ]
|
|
lines = list(filter(None, lines))
|
|
elif part.get_content_type() == 'text/html':
|
|
html = quopri.decodestring(part.get_payload())
|
|
if 'TPV' in msg['Subject']:
|
|
pedido, importe = check_tpv(lines)
|
|
elif 'Notifi' in msg['Subject']:
|
|
pedido, importe = check_paypal(lines)
|
|
else:
|
|
print 'WTF: ', item
|
|
return
|
|
|
|
try:
|
|
if pedidos[pedido]['importe'] == importe:
|
|
pasta['confirmada'] += importe
|
|
for item in pedidos[pedido]['producto']:
|
|
producto, talla, cantidad = item
|
|
if (producto, talla) in totales:
|
|
totales[(producto, talla)] += cantidad
|
|
else:
|
|
totales[(producto, talla)] = cantidad
|
|
else:
|
|
print 'FAIL ', pedidos[pedido], importe
|
|
pasta['fallo'] += importe
|
|
except KeyError:
|
|
print 'NO DATA ', pedido, importe
|
|
pasta['recibida'] += importe
|
|
|
|
|
|
|
|
|
|
with open('orders.csv') as ficheroPedidos:
|
|
reader = csv.reader(ficheroPedidos)
|
|
for item in reader:
|
|
try:
|
|
idPedido = int(item[0])
|
|
except ValueError:
|
|
continue
|
|
if item[10] in translations:
|
|
item[10] = translations[item[10]]
|
|
if idPedido in pedidos:
|
|
pedidos[idPedido]['producto'].append((item[10], item[11], int(item[12])))
|
|
pedidos[idPedido]['importe'] += int(item[12]) * decimal.Decimal(item[13])
|
|
else:
|
|
pedidos[idPedido] = {
|
|
'pedido': idPedido,
|
|
'producto': [(item[10], item[11], int(item[12]))],
|
|
'importe': int(item[12]) * decimal.Decimal(item[13])
|
|
}
|
|
|
|
mails = glob.glob('Pagos/*/*')
|
|
for item in mails:
|
|
check_file(item)
|
|
|
|
|
|
for k,v in totales.items():
|
|
print k,v
|
|
print pasta
|