Commented out sendmail parts for testing. Added SentInvoice table that tracks when invoices were sent. Added InvoiceID and InvoiceSent columns to Invoice table. Now tracks which invoices haven't been sent and only sends those. Once hte invoice has been sent it marks it has having been sent in the database and adds the date to the SentInvoice table. Started adding parts to allow xlsx filent not to be hardcoded. Updated TableSetup using the PyCharm autogeneric SQL thing.

This commit is contained in:
Dan Dembinski
2021-04-01 15:52:47 -04:00
parent ae4d6c1a1f
commit 82ad72007c
4 changed files with 79 additions and 41 deletions

View File

@@ -1,8 +1,9 @@
from openpyxl import load_workbook from openpyxl import load_workbook
from sqlalchemy import create_engine, Column, Integer, String, REAL from sqlalchemy import create_engine, Column, Integer, String, REAL, Boolean
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///cffInvoice.db') engine = create_engine('sqlite:///cffInvoice.db')
engine.connect() engine.connect()
Base = declarative_base() Base = declarative_base()
@@ -10,7 +11,7 @@ Base = declarative_base()
class Invoice(Base): class Invoice(Base):
__tablename__ = 'Invoice' __tablename__ = 'Invoice'
rowid = Column(Integer, primary_key=True, autoincrement=True) rowid = Column(Integer)
OrderID = Column(String) OrderID = Column(String)
InvoiceNumber = Column(String) InvoiceNumber = Column(String)
DateCreated = Column(String) DateCreated = Column(String)
@@ -31,6 +32,8 @@ class Invoice(Base):
ShippingCity = Column(String) ShippingCity = Column(String)
ShippingState = Column(String) ShippingState = Column(String)
ShippingPostalCode = Column(String) ShippingPostalCode = Column(String)
InvoiceID = Column(Integer, primary_key=True, autoincrement=True)
InvoiceSent = Column(Boolean)
class ItemDetail(Base): class ItemDetail(Base):
@@ -44,17 +47,24 @@ class ItemDetail(Base):
ItemPrice = Column(REAL) ItemPrice = Column(REAL)
class SentInvoices(Base):
__tablename__ = 'SentInvoices'
rowid = Column(Integer, primary_key=True, autoincrement=True)
InvoiceID = Column(Integer)
SentDate = Column(String)
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)
session = Session() session = Session()
def load_sheet(invoiceNumber): def load_sheet(invoiceNumber, fileName):
# starting invoice sub-number # starting invoice sub-number
count = 2 count = 2
#open the xlsx file from accounting and name the two sheets required #open the xlsx file from accounting and name the two sheets required
wb = load_workbook('1.xlsx', data_only=True) wb = load_workbook(fileName, data_only=True)
shtInvoice = wb.get_sheet_by_name('Invoice') shtInvoice = wb.get_sheet_by_name('Invoice')
shtItemDetail = wb.get_sheet_by_name('Item Detail') shtItemDetail = wb.get_sheet_by_name('Item Detail')
@@ -81,7 +91,7 @@ def load_sheet(invoiceNumber):
def just_work(): def just_work():
#array of orderIDs #array of orderIDs
orders = [] orders = []
for order in session.query(Invoice).all(): for order in session.query(Invoice).filter(Invoice.InvoiceSent == 0).all():
orders.append(order.OrderID) orders.append(order.OrderID)
return orders return orders
@@ -108,6 +118,7 @@ def record_lookup(record):
result.append(lookup.ShippingPostalCode) result.append(lookup.ShippingPostalCode)
result.append(lookup.ShippingCharges) result.append(lookup.ShippingCharges)
result.append(lookup.DuePayment) result.append(lookup.DuePayment)
result.append(lookup.InvoiceID)
return result return result
@@ -126,3 +137,10 @@ def item_lookup(item):
result.append(lookup.ProductName) result.append(lookup.ProductName)
result.append(lookup.ItemPrice) result.append(lookup.ItemPrice)
return result return result
def mark_sent(InvoiceID, InvoiceDate):
session.add(SentInvoices(InvoiceID=InvoiceID, SentDate=InvoiceDate))
session.query(Invoice).filter(Invoice.InvoiceID == InvoiceID).update({Invoice.InvoiceSent: 1})
session.commit()

View File

@@ -1,32 +1,43 @@
create table Invoice( create table Invoice
OrderID TEXT NOT NULL, (
InvoiceNumber TEXT NOT NULL, OrderID TEXT not null,
DateCreated TEXT NOT NULL, InvoiceNumber TEXT not null,
ProductionCharges REAL NOT NULL, DateCreated TEXT not null,
ShippingCharges REAL NOT NULL, ProductionCharges REAL not null,
AdjustmentsBalance REAL NOT NULL, ShippingCharges REAL not null,
DuePayment REAL NOT NULL, AdjustmentsBalance REAL not null,
DuePayment REAL not null,
ChargeCode TEXT, ChargeCode TEXT,
UserLogon TEXT NOT NULL, UserLogon TEXT not null,
UserProfileFirstName TEXT NOT NULL, UserProfileFirstName TEXT not null,
UserProfileLastName TEXT NOT NULL, UserProfileLastName TEXT not null,
InvoiceEmailAddress TEXT NOT NULL, InvoiceEmailAddress TEXT not null,
ShippingCompany TEXT, ShippingCompany TEXT,
ShippingFirstName TEXT NOT NULL, ShippingFirstName TEXT not null,
ShippingLastName TEXT NOT NULL, ShippingLastName TEXT not null,
ShippingAddress1 TEXT NOT NULL, ShippingAddress1 TEXT not null,
ShippingAddress2 TEXT, ShippingAddress2 TEXT,
ShippingCity TEXT NOT NULL, ShippingCity TEXT not null,
ShippingState TEXT NOT NULL, ShippingState TEXT not null,
ShippingPostalCode TEXT NOT NULL ShippingPostalCode TEXT not null,
InvoiceID INTEGER
constraint Invoice_pk
primary key autoincrement,
InvoiceSent boolean default false
); );
create table ItemDetail
create table ItemDetail( (
OrderID TEXT NOT NULL, OrderID TEXT not null,
DocumentID TEXT NOT NULL, DocumentID TEXT not null,
GLIJobNumber TEXT NOT NULL, GLIJobNumber TEXT not null,
ProductName TEXT NOT NULL, ProductName TEXT not null,
Quantity INT NOT NULL, Quantity INT not null,
ItemPrice REAL NOT NULL ItemPrice REAL not null
);
create table SentInvoices
(
InvoiceID INT not null,
SentDate DATE not null
); );

View File

@@ -2,6 +2,7 @@ import sendEmail
import InvoiceDatabase import InvoiceDatabase
running = True running = True
fileName = '1.xlsx'
while running is True: while running is True:
print('1. load new excel file\n2. Send Test emails\n3. Send Invoices\n4. Quit') print('1. load new excel file\n2. Send Test emails\n3. Send Invoices\n4. Quit')
@@ -9,7 +10,7 @@ while running is True:
if option == 1: if option == 1:
print('Enter invoice number') print('Enter invoice number')
invoiceNumber = str(input()) invoiceNumber = str(input())
InvoiceDatabase.load_sheet(invoiceNumber) InvoiceDatabase.load_sheet(invoiceNumber, fileName)
elif option == 2: elif option == 2:
sendEmail.send_email(True, 'ALL') sendEmail.send_email(True, 'ALL')
elif option == 3: elif option == 3:

View File

@@ -13,10 +13,13 @@ def send_email(test, record):
# smtp_server = 'mail.keystonerelay.com' # smtp_server = 'mail.keystonerelay.com'
smtp_server = 'printingconcepts-com.mail.protection.outlook.com' smtp_server = 'printingconcepts-com.mail.protection.outlook.com'
sent_orders = []
orders = InvoiceDatabase.just_work() orders = InvoiceDatabase.just_work()
for each in orders: for each in orders:
info = InvoiceDatabase.record_lookup(each) info = InvoiceDatabase.record_lookup(each)
InvoiceID = info[18]
OrderNumber = info[0] OrderNumber = info[0]
OrderDate = datetime.strptime(info[1], '%Y-%m-%d %I:%M:%S') OrderDate = datetime.strptime(info[1], '%Y-%m-%d %I:%M:%S')
UserLogon = info[2] UserLogon = info[2]
@@ -28,6 +31,7 @@ def send_email(test, record):
BalanceDue = info[17] BalanceDue = info[17]
InvoiceDate = datetime.today().strftime('%m/%d/%Y') InvoiceDate = datetime.today().strftime('%m/%d/%Y')
#shipping information #shipping information
ShippingCompany = info[8] ShippingCompany = info[8]
ShippingFirstName = info[9] ShippingFirstName = info[9]
@@ -187,10 +191,14 @@ def send_email(test, record):
msg.add_header('Content-Type', 'text/html') msg.add_header('Content-Type', 'text/html')
msg.attach(bodyHTML) msg.attach(bodyHTML)
try: # try:
with smtplib.SMTP(smtp_server, port, timeout=120) as server: # with smtplib.SMTP(smtp_server, port, timeout=120) as server:
# server.sendmail(msg['From'], msg['To'] + ',' + msg['Cc'] + ',' + msg['Bcc'], msg.as_string()) # # server.sendmail(msg['From'], msg['To'] + ',' + msg['Cc'] + ',' + msg['Bcc'], msg.as_string())
server.send_message(msg) # server.send_message(msg)
except Exception as e: # except Exception as e:
print(e) # print(e)
if test is True:
InvoiceDatabase.mark_sent(InvoiceID, InvoiceDate)
sent_orders.append(OrderNumber)
print(sent_orders)