Last active
November 21, 2017 18:56
-
-
Save siebird/8e948ff26e679abfa4916bf88cd2b309 to your computer and use it in GitHub Desktop.
Userscript Harvest invoice update hook to send POST to Zapier webhook URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Harvest Invoice Hook | |
// @namespace https://siebird.com | |
// @version 0.1 | |
// @description Harvest invoice status hook to send POST to Zapier to update Google spreadsheet row | |
// @include /^https:\/\/\w+\.harvestapp\.com\/invoices\/[0-9]+/ | |
// @exclude /^https:\/\/\w+\.harvestapp\.com\/invoices\/[0-9]+\/duplicate | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js | |
// ==/UserScript== | |
$(function() { | |
// Set invoice variables to be sent to Zapier | |
var status = 'draft'; | |
var paymentDate = 'YYYY-MM-DD'; | |
// Are we viewing or editing the invoice? | |
if ($('#invoice_issued_at_human_format').length) { | |
var invoiceSubject = $('#invoice_subject').val(); | |
var invoiceId = $('#invoice_number').val(); | |
var issueDateArr = $('#invoice_issued_at_human_format').val().split('/'); | |
var totalAmount = parseFloat($('#total_amount').text().replace('$','').replace(',',''), 2); | |
} else { | |
var invoiceSubject = $('.subject .definition').first().text(); | |
var invoiceId = $('.client-doc-details .definition strong').first().text(); | |
var issueDateArr = $('.definition:eq(1)').text().split('/'); | |
var totalAmount = parseFloat($('#total-amount').text().replace('$','').replace(',',''), 2); | |
} | |
var issueDate = issueDateArr[2] + '-' + issueDateArr[0] + '-' + issueDateArr[1]; | |
var postData = { | |
"invoice_id": invoiceId, | |
"invoice_subject": invoiceSubject, | |
"invoice_status": status, | |
"invoice_total": totalAmount, | |
"invoice_issueDate": issueDate, | |
"invoice_paymentDate": "", | |
"invoice_paymentAmount": 0 | |
}; | |
console.log(invoiceId, invoiceSubject, status, totalAmount, issueDate, paymentDate); | |
// Submit new invoice or mark invoice as sent | |
$('#send_invoice_form form :submit, #mark_as_sent_form form :submit').on('click', function(e){ | |
// e.preventDefault(); | |
var formTarget = $(this).closest('form'); | |
postData.invoice_status = "sent"; | |
// Post data to Zapier hook | |
postWebhook(formTarget, postData); | |
}); | |
// A payment has been made | |
$('#payment_form :submit').on('click', function(e){ | |
// e.preventDefault(); | |
// update values if changed | |
var totalAmount = parseFloat($('#total-amount').text().replace('$','').replace(',',''), 2); | |
var payment = $('#payment_amount').val(); | |
var paymentDate = $('#payment_paid_at').val().split("/"); | |
var status = (payment < totalAmount) ? "sent" : "paid"; | |
paymentDate = paymentDate[2] + '-' + paymentDate[0] + '-' + paymentDate[1]; | |
//$('#payment_paid_at').val(paymentDate); | |
var formTarget = $('#payment_form form:first'); | |
postData.invoice_status = status; | |
postData.invoice_paymentDate = paymentDate; | |
postData.invoice_paymentAmount = payment; | |
// Post data to Zapier hook | |
postWebhook(formTarget, postData); | |
}); | |
// Edit/update invoice | |
$('#edit_invoice_form :submit').on('click', function(e){ | |
//e.preventDefault(); | |
// update values if changed | |
invoiceId = $('#invoice_number').val(); | |
invoiceSubject = $('#invoice_subject').val(); | |
totalAmount = parseFloat($('#total_amount').text().replace('$','').replace(',',''), 2); | |
issueDateArr = $('#invoice_issued_at_human_format').val().split('/'); | |
issueDate = issueDateArr[2] + '-' + issueDateArr[0] + '-' + issueDateArr[1]; | |
var formTarget = $('#edit_invoice_form'); | |
postData.invoice_id = invoiceId; | |
postData.invoice_subject = invoiceSubject; | |
postData.invoice_total = totalAmount; | |
postData.invoice_issueDate = issueDate; | |
// Post data to Zapier hook | |
postWebhook(formTarget, postData); | |
}); | |
}); // end doc ready | |
function postWebhook(target, dataObject) { | |
$.ajax({ | |
type: 'POST', | |
url: 'https://hooks.zapier.com/hooks/catch/.../.../', // Update with Zapier's webhook URL | |
dataType: 'json', | |
success: function (msg) { | |
if (msg) { | |
target.submit(); | |
} else { | |
alert('The HOOK data was not posted.'); | |
} | |
}, | |
data: dataObject | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script recently broke with changes Harvest made.
Changelog:
postData