Skip to content

Instantly share code, notes, and snippets.

@cblunt
Last active December 23, 2015 12:19
Show Gist options
  • Save cblunt/6634409 to your computer and use it in GitHub Desktop.
Save cblunt/6634409 to your computer and use it in GitHub Desktop.
A simple widget for Dashing (http://http://shopify.github.io/dashing) to retrieve open and overdue invoices from FreeAgent and display their total value.

Description

A simple Dashing job to display the total value of open/overdue invoices from FreeAgent.

Dependencies

An app id/secret and access token setup via FreeAgent Developer.

Freeagent Api Gem

Add it to dashing's gemfile:

gem 'freeagent-api-ruby', github: 'fac/freeagent-api-ruby'

and run bundle install.

Usage

This job makes use of the default Number widget provided by Dashing. Put the freeagent-open-invoices.rb file in your /jobs folder.

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="freeagent-open-invoices" data-view="Number" data-title="Open Invoice" data-prefix="£" />
</li>
require 'freeagent'
outstanding_amount = 0
open_invoices = []
# You'll need a FreeAgent developer app set up at https://dev.freeagent.com/
# Use the instructions in the API docs to generate an access token via the Google oAuth playground
FreeAgent.access_details(ENV['FREEAGENT_APP_ID'], ENV['FREEAGENT_APP_SECRET'], ENV['FREEAGENT_ACCESS_TOKEN'])
SCHEDULER.every '60m', first_in: 0 do |job|
open_invoices = FreeAgent::Invoice.filter(view: "open_or_overdue")
# Fetch all invoices that are currently open or Overdue
last_outstanding_amount = outstanding_amount
# Send gross figures to the widget (net + sales_tax)
outstanding_amount = open_invoices.sum {|i| i.net_value + i.sales_tax_value }
send_event 'freeagent-open-invoices', { current: outstanding_amount, last: last_outstanding_amount }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment