Skip to content

Commit

Permalink
Merge pull request #461 from metabrainz/fix-invoice-number-assignment
Browse files Browse the repository at this point in the history
Fix invoice numbers
  • Loading branch information
mayhem committed Apr 5, 2024
2 parents bd438fc + d1073d8 commit ba81f16
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
18 changes: 14 additions & 4 deletions metabrainz/admin/quickbooks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def add_new_invoice(invoice, cust, start, end, today, qty, price):


@staticmethod
def create_invoices(client, invoices):
def create_invoices(client, invoices, latest_invoice_num):
'''
Given a set of existing invoices, fetch the invoice from QuickBooks, make a copy, update it
with new values and then have QuickBooks save the new invoice. Invoices are not sent,
Expand All @@ -65,9 +65,10 @@ def create_invoices(client, invoices):
flash("Cannot fetch old invoice!")
break

latest_invoice_num += 1
new_invoice = invoice_list[0]
new_invoice.Id = None
new_invoice.DocNumber = None
new_invoice.DocNumber = latest_invoice_num
new_invoice.DueDate = None
new_invoice.TxnDate = None
new_invoice.ShipDate = None
Expand Down Expand Up @@ -162,6 +163,12 @@ def index(self):
year -= 1
(ppq_start, ppq_end) = self.calculate_quarter_dates(year ,ppq)

# Find the latest invoice number by iterating over all the invoices
latest_invoice = 0
for invoice in invoices:
latest_invoice = max(latest_invoice, int(invoice.DocNumber or "0"))
flash("latest invoice is %d" % latest_invoice)

# Iterate over all the invoices, parse their dates and arrange them into the invoice dict, by customer
invoice_dict = {}
for invoice in invoices:
Expand Down Expand Up @@ -206,6 +213,7 @@ def index(self):
'price' : invoice.Line[0].SalesItemLineDetail.UnitPrice
})


# Finally, classify customers into the three bins
ready_to_invoice = []
wtf = []
Expand Down Expand Up @@ -308,14 +316,16 @@ def index(self):
cache.set("qb_access_token", session['access_token'], 3600)
cache.set("qb_refresh_token", session['refresh_token'], 3600)

return render_template("quickbooks/index.html", ready=ready_to_invoice, wtf=wtf, current=current)
return render_template("quickbooks/index.html", ready=ready_to_invoice,
wtf=wtf, current=current, latest_invoice=latest_invoice)

@expose('/', methods=['POST'])
def submit(self):
'''
Parse the form submission, load invoices, copy the invoices, update the pertinent details and then save new invoice.
'''

latest_invoice = int(request.form.get("latest_invoice"))
customer = 0
invoices = []
while True:
Expand Down Expand Up @@ -348,7 +358,7 @@ def submit(self):
# Now call create invoices and deal with possible errors
try:
client = get_client(realm, refresh_token)
self.create_invoices(client, invoices)
self.create_invoices(client, invoices, latest_invoice)

except AuthClientError as err:
flash("Authorization failed, please try again: %s" % err)
Expand Down
1 change: 1 addition & 0 deletions metabrainz/templates/quickbooks/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ <h3>Current</h3>
{% endblock %}
{% macro section(customers, show_check) -%}
{% if customers %}
<input type="hidden" name="latest_invoice" value="{{ latest_invoice }}">
<table class="table">
{% for customer in customers %}
{% set outer_loop = loop %}
Expand Down

0 comments on commit ba81f16

Please sign in to comment.