147 lines
5.4 KiB
Python
147 lines
5.4 KiB
Python
from openpyxl import load_workbook
|
|
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()
|
|
|
|
|
|
class Invoice(Base):
|
|
__tablename__ = 'Invoice'
|
|
rowid = Column(Integer)
|
|
OrderID = Column(String)
|
|
InvoiceNumber = Column(String)
|
|
DateCreated = Column(String)
|
|
ProductionCharges = Column(REAL)
|
|
ShippingCharges = Column(REAL)
|
|
AdjustmentsBalance = Column(REAL)
|
|
DuePayment = Column(REAL)
|
|
ChargeCode = Column(String)
|
|
UserLogon = Column(String)
|
|
UserProfileFirstName = Column(String)
|
|
UserProfileLastName = Column(String)
|
|
InvoiceEmailAddress = Column(String)
|
|
ShippingCompany = Column(String)
|
|
ShippingFirstName = Column(String)
|
|
ShippingLastName = Column(String)
|
|
ShippingAddress1 = Column(String)
|
|
ShippingAddress2 = Column(String)
|
|
ShippingCity = Column(String)
|
|
ShippingState = Column(String)
|
|
ShippingPostalCode = Column(String)
|
|
InvoiceID = Column(Integer, primary_key=True, autoincrement=True)
|
|
InvoiceSent = Column(Boolean)
|
|
|
|
|
|
class ItemDetail(Base):
|
|
__tablename__ = 'ItemDetail'
|
|
rowid = Column(Integer, primary_key=True, autoincrement=True)
|
|
OrderID = Column(String)
|
|
DocumentID = Column(String)
|
|
GLIJobNumber = Column(String)
|
|
ProductName = Column(String)
|
|
Quantity = Column(String)
|
|
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, fileName):
|
|
|
|
# starting invoice sub-number
|
|
count = 2
|
|
|
|
#open the xlsx file from accounting and name the two sheets required
|
|
wb = load_workbook(fileName, data_only=True)
|
|
shtInvoice = wb.get_sheet_by_name('Invoice')
|
|
shtItemDetail = wb.get_sheet_by_name('Item Detail')
|
|
|
|
#loop through each row in the Invoice sheet except for the first and last
|
|
#write each row to the database
|
|
for row in shtInvoice.iter_rows(min_row=2, max_row=shtInvoice.max_row-1):
|
|
session.add(Invoice(OrderID=row[0].value, InvoiceNumber=invoiceNumber + '-' + str(count), DateCreated=row[2].value,
|
|
ProductionCharges=row[4].value, ShippingCharges=row[5].value, AdjustmentsBalance=row[6].value,
|
|
DuePayment=row[7].value, ChargeCode=row[8].value, UserLogon=row[9].value, UserProfileFirstName=row[10].value,
|
|
UserProfileLastName=row[11].value, InvoiceEmailAddress=row[12].value, ShippingCompany=row[13].value,
|
|
ShippingFirstName=row[14].value, ShippingLastName=row[15].value, ShippingAddress1=row[16].value,
|
|
ShippingAddress2=row[17].value, ShippingCity=row[18].value, ShippingState=row[19].value,
|
|
ShippingPostalCode=row[20].value))
|
|
count = count + 1
|
|
session.commit()
|
|
|
|
#loops through each row in the Item Detail sheet except for the first and last
|
|
#Writes each row to the datbase
|
|
for row in shtItemDetail.iter_rows(min_row=2, max_row=shtItemDetail.max_row-1):
|
|
session.add(ItemDetail(OrderID=row[0].value, DocumentID=row[1].value, GLIJobNumber=row[4].value, ProductName=row[5].value, Quantity=row[6].value, ItemPrice=row[7].value))
|
|
session.commit()
|
|
|
|
|
|
def just_work():
|
|
#array of orderIDs
|
|
orders = []
|
|
for order in session.query(Invoice).filter(Invoice.InvoiceSent == 0).all():
|
|
orders.append(order.OrderID)
|
|
return orders
|
|
|
|
|
|
def record_lookup(record):
|
|
#looks up the requested OrderID and returns an array of the results
|
|
result = []
|
|
for lookup in session.query(Invoice).filter(Invoice.OrderID == record):
|
|
result.append(lookup.OrderID)
|
|
result.append(lookup.DateCreated)
|
|
result.append(lookup.UserLogon)
|
|
result.append(lookup.UserProfileFirstName)
|
|
result.append(lookup.UserProfileLastName)
|
|
result.append(lookup.ChargeCode)
|
|
result.append(lookup.InvoiceNumber)
|
|
result.append(lookup.InvoiceEmailAddress)
|
|
result.append(lookup.ShippingCompany)
|
|
result.append(lookup.ShippingFirstName)
|
|
result.append(lookup.ShippingLastName)
|
|
result.append(lookup.ShippingAddress1)
|
|
result.append(lookup.ShippingAddress2)
|
|
result.append(lookup.ShippingCity)
|
|
result.append(lookup.ShippingState)
|
|
result.append(lookup.ShippingPostalCode)
|
|
result.append(lookup.ShippingCharges)
|
|
result.append(lookup.DuePayment)
|
|
result.append(lookup.InvoiceID)
|
|
return result
|
|
|
|
|
|
def all_items(orderId):
|
|
all = []
|
|
for items in session.query(ItemDetail).filter(ItemDetail.OrderID == orderId):
|
|
all.append(items.DocumentID)
|
|
return all
|
|
|
|
|
|
def item_lookup(item):
|
|
result = []
|
|
for lookup in session.query(ItemDetail).filter(ItemDetail.DocumentID == item):
|
|
result.append(lookup.Quantity)
|
|
result.append(lookup.DocumentID)
|
|
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()
|
|
|