Skip to content

Instantly share code, notes, and snippets.

@nickpersico
Created October 8, 2013 16:30
Show Gist options
  • Select an option

  • Save nickpersico/6887427 to your computer and use it in GitHub Desktop.

Select an option

Save nickpersico/6887427 to your computer and use it in GitHub Desktop.
This code calculates the amount of invoiced opportunities are created and changed to "invoice" status in Close.io.
##### TIME DATA ######
upcomingsunday = today + relativedelta.relativedelta(weeks=0, weekday=6)
ifmonday = today + relativedelta.relativedelta(weeks=-1, weekday=0)
monday = today.isoweekday() in range (1, 6)
day = datetime.datetime.today().weekday()
if today == current_monday:
rightmonday = today
elif today != current_monday:
rightmonday = ifmonday
###### Invoices Out This Week
week_inv_url = 'https://app.close.io/api/v1/activity/status_change/opportunity/?date_created__gt=%s&date_created__lt=%s' % (rightmonday, upcomingsunday)
week_inv_created_url = 'https://app.close.io/api/v1/opportunity/?status_id=%s&date_created__gt=%s&date_created__lt=%s' % (invoice_status, rightmonday, upcomingsunday)
week_inv = 0 # Number of opps that were changed to "invoiced" opp status
week_inv_created = 0 # Number of opps that were created and set to "invoiced" opp status
week_inv_value = 0 # The value of the opps that were changed to "invoiced" opp status
week_inv_value_c = 0 # The value of the opps that were created and set to "invoiced" opp status
has_more = True
offset = 0
week_inv_ids = []
week_inv_id = []
while has_more:
week_inv_created_resp = requests.get(week_inv_created_url, auth=(api_key, ''), params={'_skip': offset, '_limit': 100}, headers={'Content-Type': 'application/json'})
week_inv_created_data = week_inv_created_resp.json['data']
for opportunity in week_inv_created_data:
if opportunity['status_id'] == '%s' % invoice_status:
week_inv_ids.append([opportunity['id']]) # Appends opportunity_ids to dedupe against opps in the opp status tracking below
week_inv_created = week_inv_created + 1 # Adds to the count of invoiced opps that were created
week_inv_value_c += opportunity['value'] # Adds to the value count of invoiced opps that were created
offset += len(week_inv_created_data)
has_more = week_inv_created_resp.json['has_more']
has_more = True
offset = 0
while has_more:
week_inv_resp = requests.get(week_inv_url, auth=(api_key, ''), params={'_skip': offset, '_limit': 100}, headers={'Content-Type': 'application/json'})
week_inv_data = week_inv_resp.json['data']
for invoice in week_inv_data:
if invoice['new_status_id'] == '%s' % invoice_status:
for opp in week_inv_ids:
if invoice['opportunity_id'] in opp: # Dedupes matching opp id's from the ones that were "created", avoids duplicate counting
week_inv = week_inv - 1 # if matched, subtract one from the count
week_inv_value += invoice['opportunity_value'] # If matched, subtract value from the count, probably not correct
else:
week_inv = week_inv + 1 # if no match, add to the count
week_inv_value += invoice['opportunity_value'] # if no match, add value to the value totals
offset += len(week_inv_data)
has_more = week_inv_resp.json['has_more']
update.pushNumber("invoiceweek", week_inv + week_inv_created) # pushes the total of both loops to Leftronic dashboard
update.pushNumber("weekinvoicevalue", {"prefix": "$", "number": week_inv_value_c/100.0 + week_inv_value/100.0}) # pushes the total value of both loops to Leftronic dashboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment