diff --git a/InvoiceDatabase.py b/InvoiceDatabase.py index 685c50f..d0e8d3b 100644 --- a/InvoiceDatabase.py +++ b/InvoiceDatabase.py @@ -1,8 +1,9 @@ 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.orm import sessionmaker + engine = create_engine('sqlite:///cffInvoice.db') engine.connect() Base = declarative_base() @@ -10,7 +11,7 @@ Base = declarative_base() class Invoice(Base): __tablename__ = 'Invoice' - rowid = Column(Integer, primary_key=True, autoincrement=True) + rowid = Column(Integer) OrderID = Column(String) InvoiceNumber = Column(String) DateCreated = Column(String) @@ -31,6 +32,8 @@ class Invoice(Base): ShippingCity = Column(String) ShippingState = Column(String) ShippingPostalCode = Column(String) + InvoiceID = Column(Integer, primary_key=True, autoincrement=True) + InvoiceSent = Column(Boolean) class ItemDetail(Base): @@ -44,17 +47,24 @@ class ItemDetail(Base): 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 = Session() -def load_sheet(invoiceNumber): +def load_sheet(invoiceNumber, fileName): # starting invoice sub-number count = 2 #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') shtItemDetail = wb.get_sheet_by_name('Item Detail') @@ -81,7 +91,7 @@ def load_sheet(invoiceNumber): def just_work(): #array of orderIDs orders = [] - for order in session.query(Invoice).all(): + for order in session.query(Invoice).filter(Invoice.InvoiceSent == 0).all(): orders.append(order.OrderID) return orders @@ -108,6 +118,7 @@ def record_lookup(record): result.append(lookup.ShippingPostalCode) result.append(lookup.ShippingCharges) result.append(lookup.DuePayment) + result.append(lookup.InvoiceID) return result @@ -126,3 +137,10 @@ def item_lookup(item): result.append(lookup.ProductName) result.append(lookup.ItemPrice) 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() + diff --git a/TableSetup b/TableSetup index dd82e55..4c22323 100644 --- a/TableSetup +++ b/TableSetup @@ -1,32 +1,43 @@ -create table Invoice( -OrderID TEXT NOT NULL, -InvoiceNumber TEXT NOT NULL, -DateCreated TEXT NOT NULL, -ProductionCharges REAL NOT NULL, -ShippingCharges REAL NOT NULL, -AdjustmentsBalance REAL NOT NULL, -DuePayment REAL NOT NULL, -ChargeCode TEXT, -UserLogon TEXT NOT NULL, -UserProfileFirstName TEXT NOT NULL, -UserProfileLastName TEXT NOT NULL, -InvoiceEmailAddress TEXT NOT NULL, -ShippingCompany TEXT, -ShippingFirstName TEXT NOT NULL, -ShippingLastName TEXT NOT NULL, -ShippingAddress1 TEXT NOT NULL, -ShippingAddress2 TEXT, -ShippingCity TEXT NOT NULL, -ShippingState TEXT NOT NULL, -ShippingPostalCode TEXT NOT NULL +create table Invoice +( + OrderID TEXT not null, + InvoiceNumber TEXT not null, + DateCreated TEXT not null, + ProductionCharges REAL not null, + ShippingCharges REAL not null, + AdjustmentsBalance REAL not null, + DuePayment REAL not null, + ChargeCode TEXT, + UserLogon TEXT not null, + UserProfileFirstName TEXT not null, + UserProfileLastName TEXT not null, + InvoiceEmailAddress TEXT not null, + ShippingCompany TEXT, + ShippingFirstName TEXT not null, + ShippingLastName TEXT not null, + ShippingAddress1 TEXT not null, + ShippingAddress2 TEXT, + ShippingCity TEXT not null, + ShippingState TEXT not null, + ShippingPostalCode TEXT not null, + InvoiceID INTEGER + constraint Invoice_pk + primary key autoincrement, + InvoiceSent boolean default false ); - -create table ItemDetail( -OrderID TEXT NOT NULL, -DocumentID TEXT NOT NULL, -GLIJobNumber TEXT NOT NULL, -ProductName TEXT NOT NULL, -Quantity INT NOT NULL, -ItemPrice REAL NOT NULL +create table ItemDetail +( + OrderID TEXT not null, + DocumentID TEXT not null, + GLIJobNumber TEXT not null, + ProductName TEXT not null, + Quantity INT not null, + ItemPrice REAL not null ); + +create table SentInvoices +( + InvoiceID INT not null, + SentDate DATE not null +); \ No newline at end of file diff --git a/main.py b/main.py index 30833d9..33eca49 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import sendEmail import InvoiceDatabase running = True +fileName = '1.xlsx' while running is True: 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: print('Enter invoice number') invoiceNumber = str(input()) - InvoiceDatabase.load_sheet(invoiceNumber) + InvoiceDatabase.load_sheet(invoiceNumber, fileName) elif option == 2: sendEmail.send_email(True, 'ALL') elif option == 3: diff --git a/sendEmail.py b/sendEmail.py index 8250d4e..b6b2d5c 100644 --- a/sendEmail.py +++ b/sendEmail.py @@ -13,10 +13,13 @@ def send_email(test, record): # smtp_server = 'mail.keystonerelay.com' smtp_server = 'printingconcepts-com.mail.protection.outlook.com' + sent_orders = [] + orders = InvoiceDatabase.just_work() for each in orders: info = InvoiceDatabase.record_lookup(each) + InvoiceID = info[18] OrderNumber = info[0] OrderDate = datetime.strptime(info[1], '%Y-%m-%d %I:%M:%S') UserLogon = info[2] @@ -28,6 +31,7 @@ def send_email(test, record): BalanceDue = info[17] InvoiceDate = datetime.today().strftime('%m/%d/%Y') + #shipping information ShippingCompany = info[8] ShippingFirstName = info[9] @@ -187,10 +191,14 @@ def send_email(test, record): msg.add_header('Content-Type', 'text/html') msg.attach(bodyHTML) - try: - with smtplib.SMTP(smtp_server, port, timeout=120) as server: - # server.sendmail(msg['From'], msg['To'] + ',' + msg['Cc'] + ',' + msg['Bcc'], msg.as_string()) - server.send_message(msg) - except Exception as e: - print(e) + # try: + # with smtplib.SMTP(smtp_server, port, timeout=120) as server: + # # server.sendmail(msg['From'], msg['To'] + ',' + msg['Cc'] + ',' + msg['Bcc'], msg.as_string()) + # server.send_message(msg) + # except Exception as e: + # print(e) + if test is True: + InvoiceDatabase.mark_sent(InvoiceID, InvoiceDate) + sent_orders.append(OrderNumber) + print(sent_orders)