Skip to content

Instantly share code, notes, and snippets.

@leylaso
Created November 4, 2011 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leylaso/1340416 to your computer and use it in GitHub Desktop.
Save leylaso/1340416 to your computer and use it in GitHub Desktop.
Billing workflow for LedgerSMB
// ==UserScript==
// @name Koumbit LedgerSMB billing workflow
// @match https://ledger.koumbit.net/*
// ==/UserScript==
/**
* @file
* Email templates and interface tweaks for ledger SMB emails
*
* Licensed under GPLv2.
* Developed and maintained by Sofian Benaissa. (sofian@koumbit.org)
*/
/**
* Load jQuery and call a callback function when jQuery has finished loading
*/
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
/**
* Determine which UI to display and display it
*/
function ui() {
var context;
// Account for frames
if (window.document.location.pathname == '/oe.pl' || window.document.location.pathname == '/is.pl') {
context = window.document;
}
else if (window.frames[1].document.location.pathname == '/oe.pl' || window.frames[1].document.location.pathname == '/is.pl') {
context = window.frames[1].document;
}
else {
context = false;
}
/**
* Return a given email template
*/
mailTemplates = function(tpl) {
var content = new Array();
var subject = new Array();
content['drupal-consult-fr'] = "\
Bonjour,\r\n\r\n\
Voici la facture pour votre consultation Drupal.\r\n\r\n\
Merci.\r\n\r\n\
Veuillez contacter comptabilite@koumbit.org pour toute question ou commentaire.\r\n\r\n";
subject['drupal-consult-fr'] = "Facture pour votre consultation Drupal";
content['drupal-consult-en'] = "\
Hi,\r\n\r\n\
Here is the invoice for your Drupal consultation.\r\n\r\n\
Thank you\r\n\r\n\
Please contact comptabilite@koumbit.org for any questions or comments.\r\n\r\n";
subject['drupal-consult-en'] = "Bill for your drupal consultation";
content['hosting-year-fr'] = "\
Bonjour,\r\n\r\n\
Voici la facture pour votre hébergement Internet pour l'année à venir.\r\n\r\n\
Merci.\r\n\r\n\
Veuillez contacter comptabilite@koumbit.org pour toute question ou commentaire.\r\n\r\n";
subject['hosting-year-fr'] = "Facture pour votre compte d'hébergement";
content['hosting-year-en'] = "\
Hi,\r\n\r\n\
Here is the hosting bill for the coming year.\r\n\r\n\
Thank you\r\n\r\n\
Please contact comptabilite@koumbit.org for any questions or comments.\r\n\r\n";
subject['hosting-year-en'] = "Bill for your hosting account";
var output = new Array();
output['subject'] = subject[tpl];
output['content'] = content[tpl];
return output;
}
/**
* Retrieve user inputted RT ticket number to complete the templates
*/
fetchRT = function() {
var ticket = prompt("RT ticket number:");
return ticket;
}
/**
* Given a template to use, fetch the rt number and modify the form appropriately
*/
ledgerMail = function (tpl, context) {
template = mailTemplates(tpl);
RT = fetchRT();
template['subject'] = '[Koumbit #' + RT + '] ' + template['subject'];
subject = document.getElementsByName('subject');
jQuery('input[name="subject"]', context).val(template['subject']);
jQuery('textarea[name="message"]', context).val(template['content']);
// Deal with the cc field
template['bcc'] = jQuery('input[name="bcc"]').val();
if (jQuery(template['bcc']).length==0) {
jQuery('input[name="bcc"]', context).val('facturation@koumbit.org');
}
else {
template['bcc'] = template['bcc'] + ', facturation@koumbit.org';
jQuery('input[name="bcc"]', context).val(template['bcc']);
}
}
/**
* Return the UI for the email forms
*/
mailUI = function(context) {
jQuery('body form', context).prepend('\
<div style="position: absolute; right: 10px; border: thin solid green; padding: 1em 1em 1em 3em;">\r\n\
<h4 class="kt-ledger-title">MAIL TEMPLATES</h4>\r\n\
<ul class="kt-ledger-actions" style="background: #fff">\r\n\
<li><a class="jq-template-links" id="drupal-consult-fr" href="#drupal-consult-fr">FR: Drupal Consultation</a></li>\r\n\
<li><a class="jq-template-links" id="drupal-consult-en" href="#drupal-consult-en">EN: Drupal Consultation</a></li>\r\n\
<li><a class="jq-template-links" id="hosting-year-fr" href="#hosting-year-fr">FR: Hosting Annual</a></li>\r\n\
<li><a class="jq-template-links" id="hosting-year-en" href="#hosting-year-en">EN: Hosting Annual</a></li>\r\n\
</ul>\r\n\
</div>\r\n\
');
jQuery('a.jq-template-links', context).click( function(event) {
tpl = jQuery(this).attr('id');
event.preventDefault();
ledgerMail(tpl, context);
});
}
/**
* Display a checklist for bills
*/
billChecklist = function(context) {
jQuery('body form', context).prepend('\
<div style="position: absolute; right: 10px; border: thin solid green; padding: 1em 1em 1em 3em;">\r\n\
<h4 class="kt-ledger-title">CHECKLIST</h4>\r\n\
<ol class="kt-ledger-actions" style="background: #fff">\r\n\
<li>Send an email</li>\r\n\
<li>Resolve the RT ticket</li>\r\n\
<li>Save the bill</li>\r\n\
</ol>\r\n\
</div>\r\n\
');
}
/**
* Display a checklist for sales orderes
*/
orderChecklist = function(context) {
jQuery('body form', context).prepend('\
<div style="position: absolute; right: 10px; border: thin solid green; padding: 1em 1em 1em 3em;">\r\n\
<h4 class="kt-ledger-title">CHECKLIST</h4>\r\n\
<ol class="kt-ledger-actions" style="background: #fff">\r\n\
<li>Ajouter le numéro de référence RT</li>\r\n\
<li>Confirmer le vendeur</li>\r\n\
<li>Verifier la configuration du client</li>\r\n\
<li>Verifier si le client a un crédit à son compte</li>\r\n\
<li>Verifier les taxes et le montant total</li>\r\n\
<li>Confirmer les codes de prestation de vente</li>\r\n\
<li>Verifier que la bonne langue est utilisé pour la commande</li>\r\n\
</ol>\r\n\
</div>\r\n\
');
}
mailTest = jQuery('table table input[name="email"]', context).val();
hiddenType = jQuery('body form input[name="type"]', context).val();
if (context) {
if (mailTest) {
mailUI(context);
}
else {
if (hiddenType == 'invoice') {
billChecklist(context);
}
else if (hiddenType == 'sales_order') {
orderChecklist(context);
}
// All checklists should strikethrough on a click
jQuery('.kt-ledger-actions li').click( function(event) {
jQuery(this).css('text-decoration', 'line-through');
});
}
jQuery('.kt-ledger-title', context).click( function(event) {
if (jQuery(this).hasClass('kt-ledger-hidden')) {
jQuery(this).parent().css('background', '#fff');
jQuery(this).removeClass('kt-ledger-hidden');
}
else {
jQuery(this).parent().css('background', 'transparent');
jQuery(this).addClass('kt-ledger-hidden');
}
jQuery('.kt-ledger-actions', context).toggle();
});
jQuery('.kt-ledger-title', context).click();
}
}
addJQuery(ui);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment